-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance_test.go
279 lines (260 loc) · 8.05 KB
/
instance_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package instance_test
import (
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/instance"
)
type HardwareSuite struct{}
var _ = gc.Suite(&HardwareSuite{})
type parseHardwareTestSpec struct {
summary string
args []string
err string
}
func (ts *parseHardwareTestSpec) check(c *gc.C) {
hwc, err := instance.ParseHardware(ts.args...)
// Check the spec'ed error condition first.
if ts.err != "" {
c.Check(err, gc.ErrorMatches, ts.err)
// We expected an error so we don't worry about checking hwc.
return
} else if !c.Check(err, jc.ErrorIsNil) {
// We got an unexpected error so we don't worry about checking hwc.
return
}
// The error condition matched so we check hwc.
cons1, err := instance.ParseHardware(hwc.String())
if !c.Check(err, jc.ErrorIsNil) {
// Parsing didn't work so we don't worry about checking hwc.
return
}
// Compare the round-tripped HWC.
c.Check(cons1, gc.DeepEquals, hwc)
}
var parseHardwareTests = []parseHardwareTestSpec{
// Simple errors.
{
summary: "nothing at all",
}, {
summary: "empty",
args: []string{" "},
}, {
summary: "complete nonsense",
args: []string{"cheese"},
err: `malformed characteristic "cheese"`,
}, {
summary: "missing name",
args: []string{"=cheese"},
err: `malformed characteristic "=cheese"`,
}, {
summary: "unknown characteristic",
args: []string{"cheese=edam"},
err: `unknown characteristic "cheese"`,
},
// "arch" in detail.
{
summary: "set arch empty",
args: []string{"arch="},
}, {
summary: "set arch amd64",
args: []string{"arch=amd64"},
}, {
summary: "set arch i386",
args: []string{"arch=i386"},
}, {
summary: "set arch armhf",
args: []string{"arch=armhf"},
}, {
summary: "set nonsense arch 1",
args: []string{"arch=cheese"},
err: `bad "arch" characteristic: "cheese" not recognized`,
}, {
summary: "set nonsense arch 2",
args: []string{"arch=123.45"},
err: `bad "arch" characteristic: "123.45" not recognized`,
}, {
summary: "double set arch together",
args: []string{"arch=amd64 arch=amd64"},
err: `bad "arch" characteristic: already set`,
}, {
summary: "double set arch separately",
args: []string{"arch=armhf", "arch="},
err: `bad "arch" characteristic: already set`,
},
// "cores" in detail.
{
summary: "set cores empty",
args: []string{"cores="},
}, {
summary: "set cores zero",
args: []string{"cores=0"},
}, {
summary: "set cores",
args: []string{"cores=4"},
}, {
summary: "set nonsense cores 1",
args: []string{"cores=cheese"},
err: `bad "cores" characteristic: must be a non-negative integer`,
}, {
summary: "set nonsense cores 2",
args: []string{"cores=-1"},
err: `bad "cores" characteristic: must be a non-negative integer`,
}, {
summary: "set nonsense cores 3",
args: []string{"cores=123.45"},
err: `bad "cores" characteristic: must be a non-negative integer`,
}, {
summary: "double set cores together",
args: []string{"cores=128 cores=1"},
err: `bad "cores" characteristic: already set`,
}, {
summary: "double set cores separately",
args: []string{"cores=128", "cores=1"},
err: `bad "cores" characteristic: already set`,
},
// "cpu-power" in detail.
{
summary: "set cpu-power empty",
args: []string{"cpu-power="},
}, {
summary: "set cpu-power zero",
args: []string{"cpu-power=0"},
}, {
summary: "set cpu-power",
args: []string{"cpu-power=44"},
}, {
summary: "set nonsense cpu-power 1",
args: []string{"cpu-power=cheese"},
err: `bad "cpu-power" characteristic: must be a non-negative integer`,
}, {
summary: "set nonsense cpu-power 2",
args: []string{"cpu-power=-1"},
err: `bad "cpu-power" characteristic: must be a non-negative integer`,
}, {
summary: "double set cpu-power together",
args: []string{" cpu-power=300 cpu-power=1700 "},
err: `bad "cpu-power" characteristic: already set`,
}, {
summary: "double set cpu-power separately",
args: []string{"cpu-power=300 ", " cpu-power=1700"},
err: `bad "cpu-power" characteristic: already set`,
},
// "mem" in detail.
{
summary: "set mem empty",
args: []string{"mem="},
}, {
summary: "set mem zero",
args: []string{"mem=0"},
}, {
summary: "set mem without suffix",
args: []string{"mem=512"},
}, {
summary: "set mem with M suffix",
args: []string{"mem=512M"},
}, {
summary: "set mem with G suffix",
args: []string{"mem=1.5G"},
}, {
summary: "set mem with T suffix",
args: []string{"mem=36.2T"},
}, {
summary: "set mem with P suffix",
args: []string{"mem=18.9P"},
}, {
summary: "set nonsense mem 1",
args: []string{"mem=cheese"},
err: `bad "mem" characteristic: must be a non-negative float with optional M/G/T/P suffix`,
}, {
summary: "set nonsense mem 2",
args: []string{"mem=-1"},
err: `bad "mem" characteristic: must be a non-negative float with optional M/G/T/P suffix`,
}, {
summary: "set nonsense mem 3",
args: []string{"mem=32Y"},
err: `bad "mem" characteristic: must be a non-negative float with optional M/G/T/P suffix`,
}, {
summary: "double set mem together",
args: []string{"mem=1G mem=2G"},
err: `bad "mem" characteristic: already set`,
}, {
summary: "double set mem separately",
args: []string{"mem=1G", "mem=2G"},
err: `bad "mem" characteristic: already set`,
},
// "root-disk" in detail.
{
summary: "set root-disk empty",
args: []string{"root-disk="},
}, {
summary: "set root-disk zero",
args: []string{"root-disk=0"},
}, {
summary: "set root-disk without suffix",
args: []string{"root-disk=512"},
}, {
summary: "set root-disk with M suffix",
args: []string{"root-disk=512M"},
}, {
summary: "set root-disk with G suffix",
args: []string{"root-disk=1.5G"},
}, {
summary: "set root-disk with T suffix",
args: []string{"root-disk=36.2T"},
}, {
summary: "set root-disk with P suffix",
args: []string{"root-disk=18.9P"},
}, {
summary: "set nonsense root-disk 1",
args: []string{"root-disk=cheese"},
err: `bad "root-disk" characteristic: must be a non-negative float with optional M/G/T/P suffix`,
}, {
summary: "set nonsense root-disk 2",
args: []string{"root-disk=-1"},
err: `bad "root-disk" characteristic: must be a non-negative float with optional M/G/T/P suffix`,
}, {
summary: "set nonsense root-disk 3",
args: []string{"root-disk=32Y"},
err: `bad "root-disk" characteristic: must be a non-negative float with optional M/G/T/P suffix`,
}, {
summary: "double set root-disk together",
args: []string{"root-disk=1G root-disk=2G"},
err: `bad "root-disk" characteristic: already set`,
}, {
summary: "double set root-disk separately",
args: []string{"root-disk=1G", "root-disk=2G"},
err: `bad "root-disk" characteristic: already set`,
},
// "availability-zone" in detail.
{
summary: "set availability-zone empty",
args: []string{"availability-zone="},
}, {
summary: "set availability-zone non-empty",
args: []string{"availability-zone=a_zone"},
}, {
summary: "double set availability-zone together",
args: []string{"availability-zone=a_zone availability-zone=a_zone"},
err: `bad "availability-zone" characteristic: already set`,
}, {
summary: "double set availability-zone separately",
args: []string{"availability-zone=a_zone", "availability-zone="},
err: `bad "availability-zone" characteristic: already set`,
},
// Everything at once.
{
summary: "kitchen sink together",
args: []string{" root-disk=4G mem=2T arch=i386 cores=4096 cpu-power=9001 availability-zone=a_zone"},
}, {
summary: "kitchen sink separately",
args: []string{"root-disk=4G", "mem=2T", "cores=4096", "cpu-power=9001", "arch=armhf", "availability-zone=a_zone"},
},
}
func (s *HardwareSuite) TestParseHardware(c *gc.C) {
for i, t := range parseHardwareTests {
c.Logf("test %d: %s", i, t.summary)
t.check(c)
}
}