Merge lp:~gz/goamz/tests_amazon_region into lp:goamz
- tests_amazon_region
- Merge into trunk
Proposed by
Martin Packman
Status: | Needs review |
---|---|
Proposed branch: | lp:~gz/goamz/tests_amazon_region |
Merge into: | lp:goamz |
Diff against target: |
501 lines (+311/-29) 7 files modified
ec2/ec2_test.go (+7/-7) ec2/ec2i_test.go (+17/-7) ec2/ec2t_test.go (+21/-9) ec2/ec2test/server.go (+1/-1) testutil/streams.go (+91/-0) testutil/streams_test.go (+150/-0) testutil/suite.go (+24/-5) |
To merge this branch: | bzr merge lp:~gz/goamz/tests_amazon_region |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
goamz maintainers | Pending | ||
Review via email: mp+192269@code.launchpad.net |
Commit message
Description of the change
Enable running live tests against any EC2 region
There are two stages:
* Making the test -amazon flag take a region name
* Looking up real amis with simplestreams and using those in tests
Perhaps leaving the -amazon flag as an enabling bool, and setting a new
flag just for selecting a region that defaults to us-east-1 would be
better.
Both the simplestreams code and required test changes are pretty ugly,
a bigger rewrite would help to clean up the tests.
To post a comment you must log in.
lp:~gz/goamz/tests_amazon_region
updated
- 46. By Martin Packman
-
go fmt
Revision history for this message
Martin Packman (gz) wrote : | # |
Please take a look.
Unmerged revisions
- 46. By Martin Packman
-
go fmt
- 45. By Martin Packman
-
Get real ami values for live tests from published simplestreams data
- 44. By Martin Packman
-
Change -amazon test flag to accept region value for testing against other regions
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'ec2/ec2_test.go' | |||
2 | --- ec2/ec2_test.go 2013-10-04 17:05:49 +0000 | |||
3 | +++ ec2/ec2_test.go 2013-10-24 00:16:37 +0000 | |||
4 | @@ -96,14 +96,14 @@ | |||
5 | 96 | DisableAPITermination: true, | 96 | DisableAPITermination: true, |
6 | 97 | ShutdownBehavior: "terminate", | 97 | ShutdownBehavior: "terminate", |
7 | 98 | PrivateIPAddress: "10.0.0.25", | 98 | PrivateIPAddress: "10.0.0.25", |
14 | 99 | BlockDeviceMappings: []ec2.BlockDeviceMapping{{ | 99 | BlockDeviceMappings: []ec2.BlockDeviceMapping{{ |
15 | 100 | DeviceName: "device-name", | 100 | DeviceName: "device-name", |
16 | 101 | VirtualName: "virtual-name", | 101 | VirtualName: "virtual-name", |
17 | 102 | SnapshotId: "snapshot-id", | 102 | SnapshotId: "snapshot-id", |
18 | 103 | VolumeType: "volume-type", | 103 | VolumeType: "volume-type", |
19 | 104 | VolumeSize: 10, | 104 | VolumeSize: 10, |
20 | 105 | DeleteOnTermination: true, | 105 | DeleteOnTermination: true, |
22 | 106 | IOPS: 1000, | 106 | IOPS: 1000, |
23 | 107 | }}, | 107 | }}, |
24 | 108 | } | 108 | } |
25 | 109 | resp, err := s.ec2.RunInstances(&options) | 109 | resp, err := s.ec2.RunInstances(&options) |
26 | 110 | 110 | ||
27 | === modified file 'ec2/ec2i_test.go' | |||
28 | --- ec2/ec2i_test.go 2013-01-31 14:52:05 +0000 | |||
29 | +++ ec2/ec2i_test.go 2013-10-24 00:16:37 +0000 | |||
30 | @@ -32,27 +32,35 @@ | |||
31 | 32 | } | 32 | } |
32 | 33 | 33 | ||
33 | 34 | func (s *AmazonClientSuite) SetUpSuite(c *C) { | 34 | func (s *AmazonClientSuite) SetUpSuite(c *C) { |
35 | 35 | if !testutil.Amazon { | 35 | if testutil.AmazonRegion == nil { |
36 | 36 | c.Skip("AmazonClientSuite tests not enabled") | 36 | c.Skip("AmazonClientSuite tests not enabled") |
37 | 37 | } | 37 | } |
38 | 38 | s.srv.SetUp(c) | 38 | s.srv.SetUp(c) |
40 | 39 | s.ec2 = ec2.New(s.srv.auth, aws.USEast) | 39 | s.ec2 = ec2.New(s.srv.auth, *testutil.AmazonRegion) |
41 | 40 | s.GetImage = func(version, rootStore string) (string, error) { | ||
42 | 41 | return testutil.GetImageForRegion(s.ec2.Region, version, rootStore) | ||
43 | 42 | } | ||
44 | 40 | } | 43 | } |
45 | 41 | 44 | ||
46 | 45 | type imageGetter func(string, string) (string, error) | ||
47 | 46 | |||
48 | 42 | // ClientTests defines integration tests designed to test the client. | 47 | // ClientTests defines integration tests designed to test the client. |
49 | 43 | // It is not used as a test suite in itself, but embedded within | 48 | // It is not used as a test suite in itself, but embedded within |
50 | 44 | // another type. | 49 | // another type. |
51 | 45 | type ClientTests struct { | 50 | type ClientTests struct { |
53 | 46 | ec2 *ec2.EC2 | 51 | ec2 *ec2.EC2 |
54 | 52 | GetImage imageGetter | ||
55 | 47 | } | 53 | } |
56 | 48 | 54 | ||
57 | 49 | var imageId = "ami-ccf405a5" // Ubuntu Maverick, i386, EBS store | ||
58 | 50 | |||
59 | 51 | // Cost: 0.00 USD | 55 | // Cost: 0.00 USD |
60 | 52 | func (s *ClientTests) TestRunInstancesError(c *C) { | 56 | func (s *ClientTests) TestRunInstancesError(c *C) { |
61 | 57 | // Get an image id that used instance store, which can't be used on | ||
62 | 58 | // t1.micro machines, results in 400 error from RunInstances. | ||
63 | 59 | imageId, err := s.GetImage("12.04", "instance") | ||
64 | 60 | c.Assert(err, IsNil) | ||
65 | 53 | options := ec2.RunInstances{ | 61 | options := ec2.RunInstances{ |
68 | 54 | ImageId: "ami-a6f504cf", // Ubuntu Maverick, i386, instance store | 62 | ImageId: imageId, |
69 | 55 | InstanceType: "t1.micro", // Doesn't work with micro, results in 400. | 63 | InstanceType: "t1.micro", |
70 | 56 | } | 64 | } |
71 | 57 | 65 | ||
72 | 58 | resp, err := s.ec2.RunInstances(&options) | 66 | resp, err := s.ec2.RunInstances(&options) |
73 | @@ -70,6 +78,8 @@ | |||
74 | 70 | 78 | ||
75 | 71 | // Cost: 0.02 USD | 79 | // Cost: 0.02 USD |
76 | 72 | func (s *ClientTests) TestRunAndTerminate(c *C) { | 80 | func (s *ClientTests) TestRunAndTerminate(c *C) { |
77 | 81 | imageId, err := s.GetImage("12.04", "ebs") | ||
78 | 82 | c.Assert(err, IsNil) | ||
79 | 73 | options := ec2.RunInstances{ | 83 | options := ec2.RunInstances{ |
80 | 74 | ImageId: imageId, | 84 | ImageId: imageId, |
81 | 75 | InstanceType: "t1.micro", | 85 | InstanceType: "t1.micro", |
82 | 76 | 86 | ||
83 | === modified file 'ec2/ec2t_test.go' | |||
84 | --- ec2/ec2t_test.go 2013-10-02 21:34:26 +0000 | |||
85 | +++ ec2/ec2t_test.go 2013-10-24 00:16:37 +0000 | |||
86 | @@ -46,6 +46,11 @@ | |||
87 | 46 | s.srv.SetUp(c) | 46 | s.srv.SetUp(c) |
88 | 47 | s.ServerTests.ec2 = ec2.New(s.srv.auth, s.srv.region) | 47 | s.ServerTests.ec2 = ec2.New(s.srv.auth, s.srv.region) |
89 | 48 | s.clientTests.ec2 = ec2.New(s.srv.auth, s.srv.region) | 48 | s.clientTests.ec2 = ec2.New(s.srv.auth, s.srv.region) |
90 | 49 | getFakeImage := func(version, rootStore string) (string, error) { | ||
91 | 50 | return fmt.Sprintf("ami-fake-%v-%v", version, rootStore), nil | ||
92 | 51 | } | ||
93 | 52 | s.ServerTests.GetImage = getFakeImage | ||
94 | 53 | s.clientTests.GetImage = getFakeImage | ||
95 | 49 | } | 54 | } |
96 | 50 | 55 | ||
97 | 51 | func (s *LocalServerSuite) TestRunAndTerminate(c *C) { | 56 | func (s *LocalServerSuite) TestRunAndTerminate(c *C) { |
98 | @@ -64,7 +69,7 @@ | |||
99 | 64 | data[i] = byte(i) | 69 | data[i] = byte(i) |
100 | 65 | } | 70 | } |
101 | 66 | inst, err := s.ec2.RunInstances(&ec2.RunInstances{ | 71 | inst, err := s.ec2.RunInstances(&ec2.RunInstances{ |
103 | 67 | ImageId: imageId, | 72 | ImageId: "ami-fakeimage", |
104 | 68 | InstanceType: "t1.micro", | 73 | InstanceType: "t1.micro", |
105 | 69 | UserData: data, | 74 | UserData: data, |
106 | 70 | }) | 75 | }) |
107 | @@ -82,7 +87,7 @@ | |||
108 | 82 | 87 | ||
109 | 83 | func (s *LocalServerSuite) TestInstanceInfo(c *C) { | 88 | func (s *LocalServerSuite) TestInstanceInfo(c *C) { |
110 | 84 | list, err := s.ec2.RunInstances(&ec2.RunInstances{ | 89 | list, err := s.ec2.RunInstances(&ec2.RunInstances{ |
112 | 85 | ImageId: imageId, | 90 | ImageId: "ami-fakeimage", |
113 | 86 | InstanceType: "t1.micro", | 91 | InstanceType: "t1.micro", |
114 | 87 | }) | 92 | }) |
115 | 88 | c.Assert(err, IsNil) | 93 | c.Assert(err, IsNil) |
116 | @@ -113,11 +118,14 @@ | |||
117 | 113 | var _ = Suite(&AmazonServerSuite{}) | 118 | var _ = Suite(&AmazonServerSuite{}) |
118 | 114 | 119 | ||
119 | 115 | func (s *AmazonServerSuite) SetUpSuite(c *C) { | 120 | func (s *AmazonServerSuite) SetUpSuite(c *C) { |
121 | 116 | if !testutil.Amazon { | 121 | if testutil.AmazonRegion == nil { |
122 | 117 | c.Skip("AmazonServerSuite tests not enabled") | 122 | c.Skip("AmazonServerSuite tests not enabled") |
123 | 118 | } | 123 | } |
124 | 119 | s.srv.SetUp(c) | 124 | s.srv.SetUp(c) |
126 | 120 | s.ServerTests.ec2 = ec2.New(s.srv.auth, aws.USEast) | 125 | s.ServerTests.ec2 = ec2.New(s.srv.auth, *testutil.AmazonRegion) |
127 | 126 | s.GetImage = func(version string, rootStore string) (string, error) { | ||
128 | 127 | return testutil.GetImageForRegion(s.ec2.Region, version, rootStore) | ||
129 | 128 | } | ||
130 | 121 | } | 129 | } |
131 | 122 | 130 | ||
132 | 123 | // ServerTests defines a set of tests designed to test | 131 | // ServerTests defines a set of tests designed to test |
133 | @@ -125,7 +133,8 @@ | |||
134 | 125 | // It is not used as a test suite in itself, but embedded within | 133 | // It is not used as a test suite in itself, but embedded within |
135 | 126 | // another type. | 134 | // another type. |
136 | 127 | type ServerTests struct { | 135 | type ServerTests struct { |
138 | 128 | ec2 *ec2.EC2 | 136 | ec2 *ec2.EC2 |
139 | 137 | GetImage imageGetter | ||
140 | 129 | } | 138 | } |
141 | 130 | 139 | ||
142 | 131 | func terminateInstances(c *C, e *ec2.EC2, insts []*ec2.Instance) { | 140 | func terminateInstances(c *C, e *ec2.EC2, insts []*ec2.Instance) { |
143 | @@ -313,10 +322,12 @@ | |||
144 | 313 | group2 := groupResp.SecurityGroup | 322 | group2 := groupResp.SecurityGroup |
145 | 314 | defer s.ec2.DeleteSecurityGroup(group2) | 323 | defer s.ec2.DeleteSecurityGroup(group2) |
146 | 315 | 324 | ||
147 | 325 | imageId1, err := s.GetImage("12.04", "ebs") | ||
148 | 326 | c.Assert(err, IsNil) | ||
149 | 316 | insts := make([]*ec2.Instance, 3) | 327 | insts := make([]*ec2.Instance, 3) |
150 | 317 | inst, err := s.ec2.RunInstances(&ec2.RunInstances{ | 328 | inst, err := s.ec2.RunInstances(&ec2.RunInstances{ |
151 | 318 | MinCount: 2, | 329 | MinCount: 2, |
153 | 319 | ImageId: imageId, | 330 | ImageId: imageId1, |
154 | 320 | InstanceType: "t1.micro", | 331 | InstanceType: "t1.micro", |
155 | 321 | SecurityGroups: []ec2.SecurityGroup{group1}, | 332 | SecurityGroups: []ec2.SecurityGroup{group1}, |
156 | 322 | }) | 333 | }) |
157 | @@ -325,7 +336,8 @@ | |||
158 | 325 | insts[1] = &inst.Instances[1] | 336 | insts[1] = &inst.Instances[1] |
159 | 326 | defer terminateInstances(c, s.ec2, insts) | 337 | defer terminateInstances(c, s.ec2, insts) |
160 | 327 | 338 | ||
162 | 328 | imageId2 := "ami-e358958a" // Natty server, i386, EBS store | 339 | imageId2, err := s.GetImage("13.10", "ebs") |
163 | 340 | c.Assert(err, IsNil) | ||
164 | 329 | inst, err = s.ec2.RunInstances(&ec2.RunInstances{ | 341 | inst, err = s.ec2.RunInstances(&ec2.RunInstances{ |
165 | 330 | ImageId: imageId2, | 342 | ImageId: imageId2, |
166 | 331 | InstanceType: "t1.micro", | 343 | InstanceType: "t1.micro", |
167 | @@ -405,14 +417,14 @@ | |||
168 | 405 | }, { | 417 | }, { |
169 | 406 | about: "check that filtering on image id works", | 418 | about: "check that filtering on image id works", |
170 | 407 | filters: []filterSpec{ | 419 | filters: []filterSpec{ |
172 | 408 | {"image-id", []string{imageId}}, | 420 | {"image-id", []string{imageId1}}, |
173 | 409 | }, | 421 | }, |
174 | 410 | resultIds: ids(0, 1), | 422 | resultIds: ids(0, 1), |
175 | 411 | allowExtra: true, | 423 | allowExtra: true, |
176 | 412 | }, { | 424 | }, { |
177 | 413 | about: "combination filters 1", | 425 | about: "combination filters 1", |
178 | 414 | filters: []filterSpec{ | 426 | filters: []filterSpec{ |
180 | 415 | {"image-id", []string{imageId, imageId2}}, | 427 | {"image-id", []string{imageId1, imageId2}}, |
181 | 416 | {"group-name", []string{group1.Name}}, | 428 | {"group-name", []string{group1.Name}}, |
182 | 417 | }, | 429 | }, |
183 | 418 | resultIds: ids(0, 1), | 430 | resultIds: ids(0, 1), |
184 | 419 | 431 | ||
185 | === modified file 'ec2/ec2test/server.go' | |||
186 | --- ec2/ec2test/server.go 2013-10-03 12:10:33 +0000 | |||
187 | +++ ec2/ec2test/server.go 2013-10-24 00:16:37 +0000 | |||
188 | @@ -66,7 +66,7 @@ | |||
189 | 66 | 66 | ||
190 | 67 | // instance holds a simulated ec2 instance | 67 | // instance holds a simulated ec2 instance |
191 | 68 | type Instance struct { | 68 | type Instance struct { |
193 | 69 | seq int | 69 | seq int |
194 | 70 | // UserData holds the data that was passed to the RunInstances request | 70 | // UserData holds the data that was passed to the RunInstances request |
195 | 71 | // when the instance was started. | 71 | // when the instance was started. |
196 | 72 | UserData []byte | 72 | UserData []byte |
197 | 73 | 73 | ||
198 | === added file 'testutil/streams.go' | |||
199 | --- testutil/streams.go 1970-01-01 00:00:00 +0000 | |||
200 | +++ testutil/streams.go 2013-10-24 00:16:37 +0000 | |||
201 | @@ -0,0 +1,91 @@ | |||
202 | 1 | package testutil | ||
203 | 2 | |||
204 | 3 | import ( | ||
205 | 4 | "encoding/json" | ||
206 | 5 | "fmt" | ||
207 | 6 | "io/ioutil" | ||
208 | 7 | "launchpad.net/goamz/aws" | ||
209 | 8 | "net/http" | ||
210 | 9 | ) | ||
211 | 10 | |||
212 | 11 | var streamsURL = "http://cloud-images.ubuntu.com/releases/streams/v1/com.ubuntu.cloud:released:aws.json" | ||
213 | 12 | |||
214 | 13 | func fetchStreamsData(result interface{}) error { | ||
215 | 14 | response, err := http.Get(streamsURL) | ||
216 | 15 | if err != nil { | ||
217 | 16 | return fmt.Errorf("failed to fetch streams data: %v", err) | ||
218 | 17 | } | ||
219 | 18 | defer response.Body.Close() | ||
220 | 19 | contents, err := ioutil.ReadAll(response.Body) | ||
221 | 20 | if err != nil { | ||
222 | 21 | return fmt.Errorf("failed to read streams data: %v", err) | ||
223 | 22 | } | ||
224 | 23 | if response.StatusCode != 200 { | ||
225 | 24 | return fmt.Errorf("http error fetching streams data: %q", contents) | ||
226 | 25 | } | ||
227 | 26 | err = json.Unmarshal(contents, &result) | ||
228 | 27 | if err != nil { | ||
229 | 28 | return fmt.Errorf("failed to parse streams data: %v", err) | ||
230 | 29 | } | ||
231 | 30 | return nil | ||
232 | 31 | } | ||
233 | 32 | |||
234 | 33 | // TODO(gz): These structs are based on those in lp:juju-core simplestreams | ||
235 | 34 | // code at environs/simplestreams/simplestreams.go which if split out into a | ||
236 | 35 | // seperate module could be reused here instead. | ||
237 | 36 | |||
238 | 37 | type CloudMetadata struct { | ||
239 | 38 | Products map[string]MetadataCatalog `json:"products"` | ||
240 | 39 | DataType string `json:"datatype"` | ||
241 | 40 | } | ||
242 | 41 | |||
243 | 42 | type MetadataCatalog struct { | ||
244 | 43 | Series string `json:"release,omitempty"` | ||
245 | 44 | Version string `json:"version,omitempty"` | ||
246 | 45 | Arch string `json:"arch,omitempty"` | ||
247 | 46 | |||
248 | 47 | Versions map[string]ItemCollection `json:"versions"` | ||
249 | 48 | } | ||
250 | 49 | |||
251 | 50 | type ItemCollection struct { | ||
252 | 51 | Items map[string]Item `json:"items"` | ||
253 | 52 | } | ||
254 | 53 | |||
255 | 54 | type Item struct { | ||
256 | 55 | RootStore string `json:"root_store"` | ||
257 | 56 | Virt string `json:"virt"` | ||
258 | 57 | CRSN string `json:"crsn"` | ||
259 | 58 | Id string `json:"id"` | ||
260 | 59 | } | ||
261 | 60 | |||
262 | 61 | var streamsData *CloudMetadata | ||
263 | 62 | |||
264 | 63 | // GetImageForRegion selects an ami string id from the published simplestreams | ||
265 | 64 | // data matching the region, version, and root store values given. | ||
266 | 65 | func GetImageForRegion(region aws.Region, version string, rootStore string) (string, error) { | ||
267 | 66 | if streamsData == nil { | ||
268 | 67 | err := fetchStreamsData(&streamsData) | ||
269 | 68 | if err != nil { | ||
270 | 69 | return "", err | ||
271 | 70 | } | ||
272 | 71 | } | ||
273 | 72 | for productKey, _ := range streamsData.Products { | ||
274 | 73 | product := streamsData.Products[productKey] | ||
275 | 74 | if version != product.Version { | ||
276 | 75 | continue | ||
277 | 76 | } | ||
278 | 77 | for versionsKey, _ := range product.Versions { | ||
279 | 78 | itemCollection := product.Versions[versionsKey] | ||
280 | 79 | for itemKey, _ := range itemCollection.Items { | ||
281 | 80 | item := itemCollection.Items[itemKey] | ||
282 | 81 | if item.Virt == "hvm" || item.RootStore != rootStore { | ||
283 | 82 | continue | ||
284 | 83 | } | ||
285 | 84 | if item.CRSN == region.Name { | ||
286 | 85 | return item.Id, nil | ||
287 | 86 | } | ||
288 | 87 | } | ||
289 | 88 | } | ||
290 | 89 | } | ||
291 | 90 | return "", fmt.Errorf("no image found for region:%v version:%v root_store:%v", region.Name, version, rootStore) | ||
292 | 91 | } | ||
293 | 0 | 92 | ||
294 | === added file 'testutil/streams_test.go' | |||
295 | --- testutil/streams_test.go 1970-01-01 00:00:00 +0000 | |||
296 | +++ testutil/streams_test.go 2013-10-24 00:16:37 +0000 | |||
297 | @@ -0,0 +1,150 @@ | |||
298 | 1 | package testutil | ||
299 | 2 | |||
300 | 3 | import ( | ||
301 | 4 | "encoding/json" | ||
302 | 5 | "testing" | ||
303 | 6 | |||
304 | 7 | gc "launchpad.net/gocheck" | ||
305 | 8 | |||
306 | 9 | "launchpad.net/goamz/aws" | ||
307 | 10 | ) | ||
308 | 11 | |||
309 | 12 | func Test(t *testing.T) { | ||
310 | 13 | gc.TestingT(t) | ||
311 | 14 | } | ||
312 | 15 | |||
313 | 16 | func init() { | ||
314 | 17 | gc.Suite(&StreamsServerSuite{NewHTTPServer()}) | ||
315 | 18 | gc.Suite(&StreamsSuite{}) | ||
316 | 19 | } | ||
317 | 20 | |||
318 | 21 | type StreamsSuite struct{} | ||
319 | 22 | |||
320 | 23 | type StreamsServerSuite struct { | ||
321 | 24 | Server *HTTPServer | ||
322 | 25 | } | ||
323 | 26 | |||
324 | 27 | func (s *StreamsServerSuite) SetUpSuite(c *gc.C) { | ||
325 | 28 | s.Server.Start() | ||
326 | 29 | } | ||
327 | 30 | |||
328 | 31 | func (s *StreamsServerSuite) TearDownTest(c *gc.C) { | ||
329 | 32 | s.Server.Flush() | ||
330 | 33 | } | ||
331 | 34 | |||
332 | 35 | func (s *StreamsServerSuite) TearDownSuite(c *gc.C) { | ||
333 | 36 | s.Server.Stop() | ||
334 | 37 | } | ||
335 | 38 | |||
336 | 39 | func (s *StreamsServerSuite) patchStreamsURL() func() { | ||
337 | 40 | origStreamsURL := streamsURL | ||
338 | 41 | streamsURL = s.Server.URL | ||
339 | 42 | return func() { | ||
340 | 43 | streamsURL = origStreamsURL | ||
341 | 44 | } | ||
342 | 45 | } | ||
343 | 46 | |||
344 | 47 | func (s *StreamsServerSuite) TestFetchStreams(c *gc.C) { | ||
345 | 48 | s.Server.Response(200, nil, "{}") | ||
346 | 49 | |||
347 | 50 | defer s.patchStreamsURL()() | ||
348 | 51 | |||
349 | 52 | result := make(map[string]interface{}) | ||
350 | 53 | err := fetchStreamsData(&result) | ||
351 | 54 | c.Assert(err, gc.IsNil) | ||
352 | 55 | c.Check(result, gc.DeepEquals, map[string]interface{}{}) | ||
353 | 56 | } | ||
354 | 57 | |||
355 | 58 | func (s *StreamsServerSuite) TestFetchStreamsServerError(c *gc.C) { | ||
356 | 59 | s.Server.Response(500, nil, "Server error") | ||
357 | 60 | |||
358 | 61 | defer s.patchStreamsURL()() | ||
359 | 62 | |||
360 | 63 | result := make(map[string]interface{}) | ||
361 | 64 | err := fetchStreamsData(&result) | ||
362 | 65 | c.Assert(err, gc.NotNil) | ||
363 | 66 | c.Assert(err, gc.ErrorMatches, "http error.*Server error.*") | ||
364 | 67 | } | ||
365 | 68 | |||
366 | 69 | func (s *StreamsServerSuite) TestFetchStreamsBadJSON(c *gc.C) { | ||
367 | 70 | s.Server.Response(200, nil, "--junk-- {}") | ||
368 | 71 | |||
369 | 72 | defer s.patchStreamsURL()() | ||
370 | 73 | |||
371 | 74 | result := make(map[string]interface{}) | ||
372 | 75 | err := fetchStreamsData(&result) | ||
373 | 76 | c.Assert(err, gc.NotNil) | ||
374 | 77 | c.Assert(err, gc.ErrorMatches, "failed to parse streams data: .*") | ||
375 | 78 | } | ||
376 | 79 | |||
377 | 80 | func (s *StreamsSuite) patchStreamsData(data string) func() { | ||
378 | 81 | cloudData := CloudMetadata{} | ||
379 | 82 | err := json.Unmarshal([]byte(data), &cloudData) | ||
380 | 83 | if err != nil { | ||
381 | 84 | panic(err) | ||
382 | 85 | } | ||
383 | 86 | streamsData = &cloudData | ||
384 | 87 | return func() { | ||
385 | 88 | streamsData = nil | ||
386 | 89 | } | ||
387 | 90 | } | ||
388 | 91 | |||
389 | 92 | var sampleStreamsData = `{ | ||
390 | 93 | "updated": "Sun, 20 Oct 2013 13:48:35 +0000", | ||
391 | 94 | "format": "products:1.0", | ||
392 | 95 | "datatype": "image-ids", | ||
393 | 96 | "products": { | ||
394 | 97 | "com.ubuntu.cloud:server:13.10:amd64": { | ||
395 | 98 | "release": "saucy", | ||
396 | 99 | "version": "13.10", | ||
397 | 100 | "arch": "amd64", | ||
398 | 101 | "versions": { | ||
399 | 102 | "20130808": { | ||
400 | 103 | "items": { | ||
401 | 104 | "usww1pe": { | ||
402 | 105 | "root_store": "ebs", | ||
403 | 106 | "virt": "pv", | ||
404 | 107 | "crsn": "us-west-1", | ||
405 | 108 | "id": "ami-88ffd4cd" | ||
406 | 109 | }, | ||
407 | 110 | "usww2he": { | ||
408 | 111 | "root_store": "ebs", | ||
409 | 112 | "virt": "hvm", | ||
410 | 113 | "crsn": "us-west-2", | ||
411 | 114 | "id": "ami-89f964b9" | ||
412 | 115 | }, | ||
413 | 116 | "apne1pe": { | ||
414 | 117 | "root_store": "ebs", | ||
415 | 118 | "virt": "pv", | ||
416 | 119 | "crsn": "ap-northeast-1", | ||
417 | 120 | "id": "ami-1139a810" | ||
418 | 121 | }, | ||
419 | 122 | "usee1pi": { | ||
420 | 123 | "root_store": "instance", | ||
421 | 124 | "virt": "pv", | ||
422 | 125 | "crsn": "us-east-1", | ||
423 | 126 | "id": "ami-eaafe883" | ||
424 | 127 | } | ||
425 | 128 | } | ||
426 | 129 | } | ||
427 | 130 | } | ||
428 | 131 | } | ||
429 | 132 | } | ||
430 | 133 | } | ||
431 | 134 | ` | ||
432 | 135 | |||
433 | 136 | func (s *StreamsSuite) TestFetchStreamsUSEast(c *gc.C) { | ||
434 | 137 | defer s.patchStreamsData(sampleStreamsData)() | ||
435 | 138 | |||
436 | 139 | imageId, err := GetImageForRegion(aws.USEast, "13.10", "ebs") | ||
437 | 140 | c.Assert(err, gc.IsNil) | ||
438 | 141 | c.Assert(imageId, gc.Equals, "ami-eaafe883") | ||
439 | 142 | } | ||
440 | 143 | |||
441 | 144 | func (s *StreamsSuite) TestFetchStreamsBadJSON(c *gc.C) { | ||
442 | 145 | defer s.patchStreamsData("{}")() | ||
443 | 146 | |||
444 | 147 | _, err := GetImageForRegion(aws.USEast, "13.10", "ebs") | ||
445 | 148 | c.Assert(err, gc.NotNil) | ||
446 | 149 | c.Assert(err, gc.ErrorMatches, "no image found for region.*") | ||
447 | 150 | } | ||
448 | 0 | 151 | ||
449 | === modified file 'testutil/suite.go' | |||
450 | --- testutil/suite.go 2013-01-31 14:52:05 +0000 | |||
451 | +++ testutil/suite.go 2013-10-24 00:16:37 +0000 | |||
452 | @@ -1,17 +1,36 @@ | |||
453 | 1 | package testutil | 1 | package testutil |
454 | 2 | 2 | ||
455 | 3 | import ( | 3 | import ( |
456 | 4 | "errors" | ||
457 | 4 | "flag" | 5 | "flag" |
458 | 5 | "launchpad.net/goamz/aws" | 6 | "launchpad.net/goamz/aws" |
459 | 6 | . "launchpad.net/gocheck" | 7 | . "launchpad.net/gocheck" |
460 | 7 | ) | 8 | ) |
461 | 8 | 9 | ||
465 | 9 | // Amazon must be used by all tested packages to determine whether to | 10 | // AmazonRegion must be used by all tested packages to determine which region |
466 | 10 | // run functional tests against the real AWS servers. | 11 | // to use when runnong functional tests against the real AWS servers, and |
467 | 11 | var Amazon bool | 12 | // disable those tests if nil. |
468 | 13 | var AmazonRegion *aws.Region | ||
469 | 14 | |||
470 | 15 | type amazonRegionValue struct { | ||
471 | 16 | flag.Value | ||
472 | 17 | } | ||
473 | 18 | |||
474 | 19 | func (v *amazonRegionValue) String() string { | ||
475 | 20 | return "REGION" | ||
476 | 21 | } | ||
477 | 22 | |||
478 | 23 | func (v *amazonRegionValue) Set(val string) error { | ||
479 | 24 | region, ok := aws.Regions[val] | ||
480 | 25 | if !ok { | ||
481 | 26 | return errors.New("not an AWS region") | ||
482 | 27 | } | ||
483 | 28 | AmazonRegion = ®ion | ||
484 | 29 | return nil | ||
485 | 30 | } | ||
486 | 12 | 31 | ||
487 | 13 | func init() { | 32 | func init() { |
489 | 14 | flag.BoolVar(&Amazon, "amazon", false, "Enable tests against amazon server") | 33 | flag.Var(&amazonRegionValue{}, "amazon", "Region to enable tests against amazon server") |
490 | 15 | } | 34 | } |
491 | 16 | 35 | ||
492 | 17 | type LiveSuite struct { | 36 | type LiveSuite struct { |
493 | @@ -19,7 +38,7 @@ | |||
494 | 19 | } | 38 | } |
495 | 20 | 39 | ||
496 | 21 | func (s *LiveSuite) SetUpSuite(c *C) { | 40 | func (s *LiveSuite) SetUpSuite(c *C) { |
498 | 22 | if !Amazon { | 41 | if AmazonRegion == nil { |
499 | 23 | c.Skip("amazon tests not enabled (-amazon flag)") | 42 | c.Skip("amazon tests not enabled (-amazon flag)") |
500 | 24 | } | 43 | } |
501 | 25 | auth, err := aws.EnvAuth() | 44 | auth, err := aws.EnvAuth() |
Reviewers: mp+192269_ code.launchpad. net,
Message:
Please take a look.
Description:
Enable running live tests against any EC2 region
There are two stages:
* Making the test -amazon flag take a region name
* Looking up real amis with simplestreams and using those in tests
Perhaps leaving the -amazon flag as an enabling bool, and setting a new
flag just for selecting a region that defaults to us-east-1 would be
better.
Both the simplestreams code and required test changes are pretty ugly,
a bigger rewrite would help to clean up the tests.
https:/ /code.launchpad .net/~gz/ goamz/tests_ amazon_ region/ +merge/ 192269
(do not edit description out of merge proposal)
Please review this at https:/ /codereview. appspot. com/14930049/
Affected files (+304, -19 lines): streams_ test.go
A [revision details]
M ec2/ec2i_test.go
M ec2/ec2t_test.go
A testutil/streams.go
A testutil/
M testutil/suite.go