forked from GoogleCloudPlatform/compute-image-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_test.go
134 lines (119 loc) · 4.33 KB
/
resource_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package daisy
import (
"context"
"testing"
)
func TestExtendPartialURL(t *testing.T) {
want := "projects/foo/zones/bar/disks/baz"
if s := extendPartialURL("zones/bar/disks/baz", "foo"); s != want {
t.Errorf("got: %q, want: %q", s, want)
}
if s := extendPartialURL("projects/foo/zones/bar/disks/baz", "gaz"); s != want {
t.Errorf("got: %q, want %q", s, want)
}
}
func TestResourcePopulate(t *testing.T) {
w := testWorkflow()
s, _ := w.NewStep("foo")
name := "name"
genName := w.genName(name)
tests := []struct {
desc string
r, wantR Resource
zone, wantName, wantZone string
wantErr bool
}{
{"defaults case", Resource{}, Resource{daisyName: name, RealName: genName, Project: w.Project}, "", genName, w.Zone, false},
{"nondefaults case", Resource{Project: "pfoo"}, Resource{daisyName: name, RealName: genName, Project: "pfoo"}, "zfoo", genName, "zfoo", false},
{"ExactName case", Resource{ExactName: true}, Resource{daisyName: name, RealName: name, Project: w.Project, ExactName: true}, "", name, w.Zone, false},
{"RealName case", Resource{RealName: "foo"}, Resource{daisyName: name, RealName: "foo", Project: w.Project}, "", "foo", w.Zone, false},
{"RealName and ExactName error case", Resource{RealName: "foo", ExactName: true}, Resource{}, "", "", "", true},
}
for _, tt := range tests {
gotName, gotZone, err := tt.r.populateWithZone(context.Background(), s, name, tt.zone)
if tt.wantErr && err == nil {
t.Errorf("%s: should have returned an error but didn't", tt.desc)
} else if !tt.wantErr && err != nil {
t.Errorf("%s: unexpected error: %v", tt.desc, err)
} else if err == nil {
if diffRes := diff(tt.r, tt.wantR, 0); diffRes != "" {
t.Errorf("%s: populated Resource does not match expectation: (-got +want)\n%s", tt.desc, diffRes)
}
if gotName != tt.wantName {
t.Errorf("%s: name population wrong; got: %q, want: %q", tt.desc, gotName, tt.wantName)
}
if gotZone != tt.wantZone {
t.Errorf("%s: zone population wrong; got: %q, want: %q", tt.desc, gotZone, tt.wantZone)
}
}
}
}
func TestResourceNameHelper(t *testing.T) {
w := testWorkflow()
want := w.genName("foo")
got := resourceNameHelper("foo", w, false)
if got != want {
t.Errorf("%q != %q", got, want)
}
want = "foo"
got = resourceNameHelper("foo", w, true)
if got != want {
t.Errorf("%q != %q", got, want)
}
}
func TestResourceValidate(t *testing.T) {
w := testWorkflow()
s, _ := w.NewStep("foo")
tests := []struct {
desc string
r Resource
wantErr bool
}{
{"good case", Resource{RealName: "good", Project: testProject}, false},
{"bad name case", Resource{RealName: "bad!", Project: testProject}, true},
{"bad project case", Resource{RealName: "good", Project: "bad!"}, true},
{"project DNE case", Resource{RealName: "good", Project: DNE}, true},
}
for _, tt := range tests {
err := tt.r.validate(context.Background(), s, "prefix")
if tt.wantErr && err == nil {
t.Errorf("%s: should have returned an error but didn't", tt.desc)
} else if !tt.wantErr && err != nil {
t.Errorf("%s: unexpected error: %v", tt.desc, err)
}
}
}
func TestResourceValidateWithZone(t *testing.T) {
w := testWorkflow()
s, _ := w.NewStep("foo")
tests := []struct {
desc, zone string
wantErr bool
}{
{"good case", testZone, false},
{"bad zone case", "bad!", true},
{"zone DNE case", DNE, true},
}
for _, tt := range tests {
r := Resource{RealName: "goodname", Project: w.Project}
err := r.validateWithZone(context.Background(), s, tt.zone, "prefix")
if tt.wantErr && err == nil {
t.Errorf("%s: should have returned an error but didn't", tt.desc)
} else if !tt.wantErr && err != nil {
t.Errorf("%s: unexpected error: %v", tt.desc, err)
}
}
}