Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 package simplestreams | |
2 | |
3 import ( | |
4 "flag" | |
5 . "launchpad.net/gocheck" | |
6 "launchpad.net/juju-core/environs/jujutest" | |
7 coretesting "launchpad.net/juju-core/testing" | |
8 "net/http" | |
9 "reflect" | |
10 "testing" | |
11 ) | |
12 | |
13 var live = flag.Bool("live", false, "Include live simplestreams tests") | |
14 var vendor = flag.String("vendor", "", "The vendor representing the source of th e simplestream data") | |
15 | |
16 type liveTestData struct { | |
17 baseURL string | |
18 validCloudSpec CloudSpec | |
19 } | |
20 | |
21 var liveUrls = map[string]liveTestData{ | |
22 "ec2": { | |
23 baseURL: "http://cloud-images.ubuntu.com/releases", | |
24 validCloudSpec: CloudSpec{"us-east-1", "http://ec2.us-east-1.ama zonaws.com"}, | |
25 }, | |
26 "canonistack": { | |
27 baseURL: "https://swift.canonistack.canonical.com/v1/AUTH _a48765cc0e864be980ee21ae26aaaed4/simplestreams/data", | |
28 validCloudSpec: CloudSpec{"lcy01", "https://keystone.canonistack .canonical.com:443/v2.0/"}, | |
29 }, | |
30 } | |
31 | |
32 func Test(t *testing.T) { | |
33 if *live { | |
34 if *vendor == "" { | |
35 t.Fatal("missing vendor") | |
36 } | |
37 var ok bool | |
38 var testData liveTestData | |
39 if testData, ok = liveUrls[*vendor]; !ok { | |
40 keys := reflect.ValueOf(liveUrls).MapKeys() | |
41 t.Fatalf("Unknown vendor %s. Must be one of %s", *vendor , keys) | |
42 } | |
43 registerLiveSimpleStreamsTests(testData.baseURL, testData.validC loudSpec) | |
44 } | |
45 registerSimpleStreamsTests() | |
46 TestingT(t) | |
47 } | |
48 | |
49 var testRoundTripper = &jujutest.ProxyRoundTripper{} | |
50 | |
51 func init() { | |
52 // Prepare mock http transport for overriding metadata and images output in tests | |
53 http.DefaultTransport.(*http.Transport).RegisterProtocol("test", testRou ndTripper) | |
54 } | |
55 | |
56 var indexData = []jujutest.FileContent{ | |
57 { | |
58 "/streams/v1/index.js", ` | |
59 { | |
60 "index": { | |
61 "com.ubuntu.cloud:released:quantal": { | |
62 "updated": "Wed, 01 May 2013 13:31:26 +0000", | |
63 "clouds": [ | |
64 { | |
65 "region": "us-east-1", | |
66 "endpoint": "http://ec2.us-east-1.amazonaws.com" | |
67 } | |
68 ], | |
69 "cloudname": "aws", | |
70 "datatype": "image-ids", | |
71 "format": "products:1.0", | |
72 "products": [ | |
73 "com.ubuntu.cloud:server:12.10:amd64" | |
74 ], | |
75 "path": "streams/v1/image_metadata.js" | |
76 }, | |
77 "com.ubuntu.cloud:released:raring": { | |
78 "updated": "Wed, 01 May 2013 13:31:26 +0000", | |
79 "clouds": [ | |
80 { | |
81 "region": "us-east-1", | |
82 "endpoint": "http://ec2.us-east-1.amazonaws.com" | |
83 } | |
84 ], | |
85 "cloudname": "aws", | |
86 "datatype": "image-ids", | |
87 "format": "products:1.0", | |
88 "products": [ | |
89 "com.ubuntu.cloud:server:13.04:amd64" | |
90 ], | |
91 "path": "streams/v1/raring_metadata.js" | |
92 }, | |
93 "com.ubuntu.cloud:released:download": { | |
94 "datatype": "image-downloads", | |
95 "path": "streams/v1/com.ubuntu.cloud:released:download.js", | |
96 "updated": "Wed, 01 May 2013 13:30:37 +0000", | |
97 "products": [ | |
98 "com.ubuntu.cloud:server:12.10:amd64", | |
99 "com.ubuntu.cloud:server:13.04:amd64" | |
100 ], | |
101 "format": "products:1.0" | |
102 } | |
103 }, | |
104 "updated": "Wed, 01 May 2013 13:31:26 +0000", | |
105 "format": "index:1.0" | |
106 } | |
107 `}, { | |
108 "/streams/v1/image_metadata.js", ` | |
109 { | |
110 "updated": "Wed, 01 May 2013 13:31:26 +0000", | |
111 "content_id": "com.ubuntu.cloud:released:aws", | |
112 "products": { | |
113 "com.ubuntu.cloud:server:12.10:amd64": { | |
114 "release": "quantal", | |
115 "version": "12.10", | |
116 "arch": "amd64", | |
117 "region": "au-east-1", | |
118 "endpoint": "http://somewhere", | |
119 "versions": { | |
120 "20121218": { | |
121 "region": "au-east-2", | |
122 "endpoint": "http://somewhere-else", | |
123 "items": { | |
124 "usww1pe": { | |
125 "root_store": "ebs", | |
126 "virt": "pv", | |
127 "id": "ami-26745463" | |
128 }, | |
129 "usww2he": { | |
130 "root_store": "ebs", | |
131 "virt": "hvm", | |
132 "id": "ami-442ea674", | |
133 "region": "us-east-1", | |
134 "endpoint": "http://ec2.us-east-1.amazonaws.com" | |
135 }, | |
136 "usww3he": { | |
137 "root_store": "ebs", | |
138 "virt": "hvm", | |
139 "crsn": "uswest3", | |
140 "id": "ami-442ea675" | |
141 } | |
142 }, | |
143 "pubname": "ubuntu-quantal-12.10-amd64-server-20121218", | |
144 "label": "release" | |
145 }, | |
146 "20111111": { | |
147 "items": { | |
148 "usww3pe": { | |
149 "root_store": "ebs", | |
150 "virt": "pv", | |
151 "id": "ami-26745464" | |
152 }, | |
153 "usww2pe": { | |
154 "root_store": "ebs", | |
155 "virt": "pv", | |
156 "id": "ami-442ea684", | |
157 "region": "us-east-1", | |
158 "endpoint": "http://ec2.us-east-1.amazonaws.com" | |
159 } | |
160 }, | |
161 "pubname": "ubuntu-quantal-12.10-amd64-server-20111111", | |
162 "label": "release" | |
163 } | |
164 } | |
165 } | |
166 }, | |
167 "_aliases": { | |
168 "crsn": { | |
169 "uswest3": { | |
170 "region": "us-west-3", | |
171 "endpoint": "http://ec2.us-west-3.amazonaws.com" | |
172 } | |
173 } | |
174 }, | |
175 "format": "products:1.0" | |
176 } | |
177 `}, | |
178 } | |
179 | |
180 func registerSimpleStreamsTests() { | |
181 Suite(&simplestreamsSuite{ | |
182 liveSimplestreamsSuite: liveSimplestreamsSuite{ | |
183 baseURL: "test:", | |
184 validCloudSpec: CloudSpec{"us-east-1", "http://ec2.us-ea st-1.amazonaws.com"}, | |
185 validProdSpec: ProductSpec{"quantal", "amd64", ""}, | |
186 }, | |
187 }) | |
188 } | |
189 | |
190 func registerLiveSimpleStreamsTests(baseURL string, validCloudSpec CloudSpec) { | |
191 Suite(&liveSimplestreamsSuite{ | |
192 baseURL: baseURL, | |
193 validCloudSpec: validCloudSpec, | |
194 validProdSpec: ProductSpec{"precise", "amd64", ""}, | |
195 }) | |
196 } | |
197 | |
198 type simplestreamsSuite struct { | |
199 liveSimplestreamsSuite | |
200 } | |
201 | |
202 type liveSimplestreamsSuite struct { | |
203 coretesting.LoggingSuite | |
204 baseURL string | |
205 validCloudSpec CloudSpec | |
206 validProdSpec ProductSpec | |
207 } | |
208 | |
209 func (s *liveSimplestreamsSuite) SetUpSuite(c *C) { | |
210 s.LoggingSuite.SetUpSuite(c) | |
211 } | |
212 | |
213 func (s *liveSimplestreamsSuite) TearDownSuite(c *C) { | |
214 s.LoggingSuite.TearDownSuite(c) | |
215 } | |
216 | |
217 func (s *simplestreamsSuite) SetUpSuite(c *C) { | |
218 s.liveSimplestreamsSuite.SetUpSuite(c) | |
219 testRoundTripper.Sub = jujutest.NewVirtualRoundTripper(indexData) | |
220 } | |
221 | |
222 func (s *simplestreamsSuite) TearDownSuite(c *C) { | |
223 testRoundTripper.Sub = nil | |
224 s.liveSimplestreamsSuite.TearDownSuite(c) | |
225 } | |
226 | |
227 const ( | |
228 index_v1 = "index:1.0" | |
229 product_v1 = "products:1.0" | |
230 ) | |
231 | |
232 func (s *liveSimplestreamsSuite) TestGetIndex(c *C) { | |
233 indexRef, err := getIndexWithFormat(s.baseURL, defaultIndexPath, index_v 1) | |
234 c.Assert(err, IsNil) | |
235 c.Assert(indexRef.Format, Equals, index_v1) | |
236 c.Assert(indexRef.baseURL, Equals, s.baseURL) | |
237 c.Assert(len(indexRef.Indexes) > 0, Equals, true) | |
238 } | |
239 | |
240 func (s *liveSimplestreamsSuite) TestGetIndexWrongFormat(c *C) { | |
241 _, err := getIndexWithFormat(s.baseURL, defaultIndexPath, "bad") | |
242 c.Assert(err, NotNil) | |
243 } | |
244 | |
245 func (s *liveSimplestreamsSuite) TestGetImageIdsPathExists(c *C) { | |
246 indexRef, err := getIndexWithFormat(s.baseURL, defaultIndexPath, index_v 1) | |
247 c.Assert(err, IsNil) | |
248 path, err := indexRef.getImageIdsPath(&s.validCloudSpec, &s.validProdSpe c) | |
249 c.Assert(err, IsNil) | |
250 c.Assert(path, Not(Equals), "") | |
251 } | |
252 | |
253 func (s *liveSimplestreamsSuite) TestGetImageIdsPathInvalidCloudSpec(c *C) { | |
254 indexRef, err := getIndexWithFormat(s.baseURL, defaultIndexPath, index_v 1) | |
255 c.Assert(err, IsNil) | |
256 spec := CloudSpec{"bad", "spec"} | |
257 _, err = indexRef.getImageIdsPath(&spec, &s.validProdSpec) | |
258 c.Assert(err, NotNil) | |
259 } | |
260 | |
261 func (s *liveSimplestreamsSuite) TestGetImageIdsPathInvalidProductSpec(c *C) { | |
262 indexRef, err := getIndexWithFormat(s.baseURL, defaultIndexPath, index_v 1) | |
263 c.Assert(err, IsNil) | |
264 spec := ProductSpec{"precise", "bad", "spec"} | |
265 _, err = indexRef.getImageIdsPath(&s.validCloudSpec, &spec) | |
266 c.Assert(err, NotNil) | |
267 } | |
268 | |
269 func (s *simplestreamsSuite) TestGetImageIdsPath(c *C) { | |
270 indexRef, err := getIndexWithFormat(s.baseURL, defaultIndexPath, index_v 1) | |
271 c.Assert(err, IsNil) | |
272 path, err := indexRef.getImageIdsPath(&s.validCloudSpec, &s.validProdSpe c) | |
273 c.Assert(err, IsNil) | |
274 c.Assert(path, Equals, "streams/v1/image_metadata.js") | |
275 } | |
276 | |
277 func (s *liveSimplestreamsSuite) assertGetMetadata(c *C) *CloudImageMetadata { | |
278 indexRef, err := getIndexWithFormat(s.baseURL, defaultIndexPath, index_v 1) | |
279 c.Assert(err, IsNil) | |
280 metadata, err := indexRef.getCloudMetadataWithFormat(&s.validCloudSpec, &s.validProdSpec, product_v1) | |
281 c.Assert(err, IsNil) | |
282 c.Assert(metadata.Format, Equals, product_v1) | |
283 c.Assert(len(metadata.Products) > 0, Equals, true) | |
284 return metadata | |
285 } | |
286 | |
287 func (s *liveSimplestreamsSuite) TestGetCloudMetadataWithFormat(c *C) { | |
288 s.assertGetMetadata(c) | |
289 } | |
290 | |
291 func (s *liveSimplestreamsSuite) TestGetDefaultImageIdMetadataExists(c *C) { | |
292 im, err := GetDefaultImageIdMetadata(s.baseURL, &s.validCloudSpec, &s.va lidProdSpec) | |
293 c.Assert(err, IsNil) | |
294 c.Assert(len(im) > 0, Equals, true) | |
295 } | |
296 | |
297 func (s *simplestreamsSuite) assertImageMetadataContents(c *C, im []*ImageMetada ta) { | |
298 c.Assert(len(im), Equals, 2) | |
299 c.Assert(im, DeepEquals, []*ImageMetadata{ | |
300 { | |
301 Id: "ami-442ea674", | |
302 VType: "hvm", | |
303 RegionName: "us-east-1", | |
304 Endpoint: "http://ec2.us-east-1.amazonaws.com", | |
305 Storage: "ebs", | |
306 }, | |
307 { | |
308 Id: "ami-442ea684", | |
309 VType: "pv", | |
310 RegionName: "us-east-1", | |
311 Endpoint: "http://ec2.us-east-1.amazonaws.com", | |
312 Storage: "ebs", | |
313 }, | |
314 }) | |
315 } | |
316 | |
317 func (s *simplestreamsSuite) TestGetDefaultImageIdMetadata(c *C) { | |
318 im, err := GetDefaultImageIdMetadata(s.baseURL, &s.validCloudSpec, &s.va lidProdSpec) | |
319 c.Assert(err, IsNil) | |
320 s.assertImageMetadataContents(c, im) | |
321 } | |
322 | |
323 func (s *simplestreamsSuite) TestGetImageIdMetadata(c *C) { | |
324 im, err := GetImageIdMetadata(s.baseURL, defaultIndexPath, &s.validCloud Spec, &s.validProdSpec) | |
325 c.Assert(err, IsNil) | |
326 s.assertImageMetadataContents(c, im) | |
327 } | |
328 | |
329 func (s *liveSimplestreamsSuite) assertGetImageCollections(c *C, version string) *ImageCollection { | |
330 metadata := s.assertGetMetadata(c) | |
331 metadataCatalog := metadata.Products["com.ubuntu.cloud:server:12.10:amd6 4"] | |
332 ic := metadataCatalog.Images[version] | |
333 return &ic | |
334 } | |
335 | |
336 func (s *simplestreamsSuite) TestMetadataCatalog(c *C) { | |
337 metadata := s.assertGetMetadata(c) | |
338 c.Assert(len(metadata.Products), Equals, 1) | |
339 c.Check(len(metadata.Aliases), Equals, 1) | |
340 metadataCatalog := metadata.Products["com.ubuntu.cloud:server:12.10:amd6 4"] | |
341 c.Check(len(metadataCatalog.Images), Equals, 2) | |
342 c.Check(metadataCatalog.Release, Equals, "quantal") | |
343 c.Check(metadataCatalog.Version, Equals, "12.10") | |
344 c.Check(metadataCatalog.Arch, Equals, "amd64") | |
345 c.Check(metadataCatalog.RegionName, Equals, "au-east-1") | |
346 c.Check(metadataCatalog.Endpoint, Equals, "http://somewhere") | |
347 c.Check(len(metadataCatalog.Images) > 0, Equals, true) | |
348 } | |
349 | |
350 func (s *simplestreamsSuite) TestImageCollection(c *C) { | |
351 ic := s.assertGetImageCollections(c, "20121218") | |
352 c.Check(ic.RegionName, Equals, "au-east-2") | |
353 c.Check(ic.Endpoint, Equals, "http://somewhere-else") | |
354 c.Assert(len(ic.Images) > 0, Equals, true) | |
rog
2013/05/03 14:58:08
im := ic.Images["usww2hw"]
c.Check etc
that way t
wallyworld
2013/05/07 06:35:52
Done.
| |
355 for key, im := range ic.Images { | |
356 if key != "usww2he" { | |
357 continue | |
358 } | |
359 c.Check(im.Id, Equals, "ami-442ea674") | |
360 c.Check(im.Storage, Equals, "ebs") | |
361 c.Check(im.VType, Equals, "hvm") | |
362 c.Check(im.RegionName, Equals, "us-east-1") | |
363 c.Check(im.Endpoint, Equals, "http://ec2.us-east-1.amazonaws.com ") | |
364 } | |
365 } | |
366 | |
367 func (s *simplestreamsSuite) TestImageMetadataDenormalisationFromCollection(c *C ) { | |
368 ic := s.assertGetImageCollections(c, "20121218") | |
369 for key, im := range ic.Images { | |
370 if key != "usww1pe" { | |
371 continue | |
372 } | |
373 c.Check(im.RegionName, Equals, ic.RegionName) | |
374 c.Check(im.Endpoint, Equals, ic.Endpoint) | |
375 } | |
376 } | |
377 | |
378 func (s *simplestreamsSuite) TestImageMetadataDenormalisationFromCatalog(c *C) { | |
379 metadata := s.assertGetMetadata(c) | |
380 metadataCatalog := metadata.Products["com.ubuntu.cloud:server:12.10:amd6 4"] | |
381 ic := metadataCatalog.Images["20121111"] | |
rog
2013/05/03 14:58:08
this test is not testing what you think it is. the
wallyworld
2013/05/07 06:35:52
Done.
| |
382 for key, im := range ic.Images { | |
383 if key != "usww3pe" { | |
384 continue | |
385 } | |
386 c.Check(im.RegionName, Equals, metadataCatalog.RegionName) | |
387 c.Check(im.Endpoint, Equals, metadataCatalog.Endpoint) | |
388 } | |
389 } | |
390 | |
391 func (s *simplestreamsSuite) TestImageMetadataDealiasing(c *C) { | |
392 metadata := s.assertGetMetadata(c) | |
393 metadataCatalog := metadata.Products["com.ubuntu.cloud:server:12.10:amd6 4"] | |
394 ic := metadataCatalog.Images["20121218"] | |
395 for key, im := range ic.Images { | |
396 if key != "usww3he" { | |
rog
2013/05/03 14:58:08
ditto
wallyworld
2013/05/07 06:35:52
Done.
| |
397 continue | |
398 } | |
399 c.Check(im.RegionName, Equals, "us-west-3") | |
400 c.Check(im.Endpoint, Equals, "http://ec2.us-west-3.amazonaws.com ") | |
401 } | |
402 } | |
403 | |
404 type productSpecSuite struct{} | |
405 | |
406 var _ = Suite(&productSpecSuite{}) | |
407 | |
408 func (s *productSpecSuite) TestStringWithDefaultStream(c *C) { | |
409 prodSpec := ProductSpec{"precise", "amd64", ""} | |
410 c.Assert(prodSpec.String(), Equals, "com.ubuntu.cloud:server:12.04:amd64 ") | |
411 } | |
412 | |
413 func (s *productSpecSuite) TestString(c *C) { | |
414 prodSpec := ProductSpec{"precise", "amd64", "daily"} | |
415 c.Assert(prodSpec.String(), Equals, "com.ubuntu.cloud.daily:server:12.04 :amd64") | |
416 } | |
OLD | NEW |