Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 package ec2_test | 1 package ec2_test |
2 | 2 |
3 import ( | 3 import ( |
4 "crypto/rand" | 4 "crypto/rand" |
5 "fmt" | 5 "fmt" |
6 "io" | 6 "io" |
7 "io/ioutil" | 7 "io/ioutil" |
8 amzec2 "launchpad.net/goamz/ec2" | 8 amzec2 "launchpad.net/goamz/ec2" |
9 . "launchpad.net/gocheck" | 9 . "launchpad.net/gocheck" |
10 "launchpad.net/juju-core/environs" | 10 "launchpad.net/juju-core/environs" |
11 "launchpad.net/juju-core/environs/ec2" | 11 "launchpad.net/juju-core/environs/ec2" |
12 "launchpad.net/juju-core/environs/jujutest" | 12 "launchpad.net/juju-core/environs/jujutest" |
13 "launchpad.net/juju-core/juju/testing" | 13 "launchpad.net/juju-core/juju/testing" |
14 "launchpad.net/juju-core/state" | |
14 coretesting "launchpad.net/juju-core/testing" | 15 coretesting "launchpad.net/juju-core/testing" |
15 "strings" | 16 "strings" |
16 ) | 17 ) |
17 | 18 |
18 // uniqueName is generated afresh for every test run, so that | 19 // uniqueName is generated afresh for every test run, so that |
19 // we are not polluted by previous test state. | 20 // we are not polluted by previous test state. |
20 var uniqueName = randomName() | 21 var uniqueName = randomName() |
21 | 22 |
22 func randomName() string { | 23 func randomName() string { |
23 buf := make([]byte, 8) | 24 buf := make([]byte, 8) |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 } | 91 } |
91 | 92 |
92 func (t *LiveTests) TearDownTest(c *C) { | 93 func (t *LiveTests) TearDownTest(c *C) { |
93 t.LiveTests.TearDownTest(c) | 94 t.LiveTests.TearDownTest(c) |
94 t.LoggingSuite.TearDownTest(c) | 95 t.LoggingSuite.TearDownTest(c) |
95 } | 96 } |
96 | 97 |
97 // TODO(niemeyer): Looks like many of those tests should be moved to jujutest.Li veTests. | 98 // TODO(niemeyer): Looks like many of those tests should be moved to jujutest.Li veTests. |
98 | 99 |
99 func (t *LiveTests) TestInstanceDNSName(c *C) { | 100 func (t *LiveTests) TestInstanceDNSName(c *C) { |
100 » inst, err := t.Env.StartInstance(30, testing.InvalidStateInfo(30), nil) | 101 » inst, err := t.Env.StartInstance("30", testing.InvalidStateInfo("30"), n il) |
101 c.Assert(err, IsNil) | 102 c.Assert(err, IsNil) |
102 defer t.Env.StopInstances([]environs.Instance{inst}) | 103 defer t.Env.StopInstances([]environs.Instance{inst}) |
103 dns, err := inst.WaitDNSName() | 104 dns, err := inst.WaitDNSName() |
104 // TODO(niemeyer): This assert sometimes fails with "no instances found" | 105 // TODO(niemeyer): This assert sometimes fails with "no instances found" |
105 c.Assert(err, IsNil) | 106 c.Assert(err, IsNil) |
106 c.Assert(dns, Not(Equals), "") | 107 c.Assert(dns, Not(Equals), "") |
107 | 108 |
108 » insts, err := t.Env.Instances([]string{inst.Id()}) | 109 » insts, err := t.Env.Instances([]state.InstanceId{inst.Id()}) |
109 c.Assert(err, IsNil) | 110 c.Assert(err, IsNil) |
110 c.Assert(len(insts), Equals, 1) | 111 c.Assert(len(insts), Equals, 1) |
111 | 112 |
112 ec2inst := ec2.InstanceEC2(insts[0]) | 113 ec2inst := ec2.InstanceEC2(insts[0]) |
113 c.Assert(ec2inst.DNSName, Equals, dns) | 114 c.Assert(ec2inst.DNSName, Equals, dns) |
114 } | 115 } |
115 | 116 |
116 func (t *LiveTests) TestInstanceGroups(c *C) { | 117 func (t *LiveTests) TestInstanceGroups(c *C) { |
117 ec2conn := ec2.EnvironEC2(t.Env) | 118 ec2conn := ec2.EnvironEC2(t.Env) |
118 | 119 |
119 groups := amzec2.SecurityGroupNames( | 120 groups := amzec2.SecurityGroupNames( |
120 ec2.JujuGroupName(t.Env), | 121 ec2.JujuGroupName(t.Env), |
121 » » ec2.MachineGroupName(t.Env, 98), | 122 » » ec2.MachineGroupName(t.Env, "98"), |
122 » » ec2.MachineGroupName(t.Env, 99), | 123 » » ec2.MachineGroupName(t.Env, "99"), |
123 ) | 124 ) |
124 info := make([]amzec2.SecurityGroupInfo, len(groups)) | 125 info := make([]amzec2.SecurityGroupInfo, len(groups)) |
125 | 126 |
126 // Create a group with the same name as the juju group | 127 // Create a group with the same name as the juju group |
127 // but with different permissions, to check that it's deleted | 128 // but with different permissions, to check that it's deleted |
128 // and recreated correctly. | 129 // and recreated correctly. |
129 oldJujuGroup := createGroup(c, ec2conn, groups[0].Name, "old juju group" ) | 130 oldJujuGroup := createGroup(c, ec2conn, groups[0].Name, "old juju group" ) |
130 | 131 |
131 // Add two permissions: one is required and should be left alone; | 132 // Add two permissions: one is required and should be left alone; |
132 // the other is not and should be deleted. | 133 // the other is not and should be deleted. |
133 // N.B. this is unfortunately sensitive to the actual set of permissions used. | 134 // N.B. this is unfortunately sensitive to the actual set of permissions used. |
134 _, err := ec2conn.AuthorizeSecurityGroup(oldJujuGroup, | 135 _, err := ec2conn.AuthorizeSecurityGroup(oldJujuGroup, |
135 []amzec2.IPPerm{ | 136 []amzec2.IPPerm{ |
136 { | 137 { |
137 Protocol: "tcp", | 138 Protocol: "tcp", |
138 FromPort: 22, | 139 FromPort: 22, |
139 ToPort: 22, | 140 ToPort: 22, |
140 SourceIPs: []string{"0.0.0.0/0"}, | 141 SourceIPs: []string{"0.0.0.0/0"}, |
141 }, | 142 }, |
142 { | 143 { |
143 Protocol: "udp", | 144 Protocol: "udp", |
144 FromPort: 4321, | 145 FromPort: 4321, |
145 ToPort: 4322, | 146 ToPort: 4322, |
146 SourceIPs: []string{"3.4.5.6/32"}, | 147 SourceIPs: []string{"3.4.5.6/32"}, |
147 }, | 148 }, |
148 }) | 149 }) |
149 c.Assert(err, IsNil) | 150 c.Assert(err, IsNil) |
150 | 151 |
151 » inst0, err := t.Env.StartInstance(98, testing.InvalidStateInfo(98), nil) | 152 » inst0, err := t.Env.StartInstance("98", testing.InvalidStateInfo("98"), nil) |
152 c.Assert(err, IsNil) | 153 c.Assert(err, IsNil) |
153 defer t.Env.StopInstances([]environs.Instance{inst0}) | 154 defer t.Env.StopInstances([]environs.Instance{inst0}) |
154 | 155 |
155 // Create a same-named group for the second instance | 156 // Create a same-named group for the second instance |
156 // before starting it, to check that it's reused correctly. | 157 // before starting it, to check that it's reused correctly. |
157 oldMachineGroup := createGroup(c, ec2conn, groups[2].Name, "old machine group") | 158 oldMachineGroup := createGroup(c, ec2conn, groups[2].Name, "old machine group") |
158 | 159 |
159 » inst1, err := t.Env.StartInstance(99, testing.InvalidStateInfo(99), nil) | 160 » inst1, err := t.Env.StartInstance("99", testing.InvalidStateInfo("99"), nil) |
160 c.Assert(err, IsNil) | 161 c.Assert(err, IsNil) |
161 defer t.Env.StopInstances([]environs.Instance{inst1}) | 162 defer t.Env.StopInstances([]environs.Instance{inst1}) |
162 | 163 |
163 groupsResp, err := ec2conn.SecurityGroups(groups, nil) | 164 groupsResp, err := ec2conn.SecurityGroups(groups, nil) |
164 c.Assert(err, IsNil) | 165 c.Assert(err, IsNil) |
165 c.Assert(groupsResp.Groups, HasLen, len(groups)) | 166 c.Assert(groupsResp.Groups, HasLen, len(groups)) |
166 | 167 |
167 // For each group, check that it exists and record its id. | 168 // For each group, check that it exists and record its id. |
168 for i, group := range groups { | 169 for i, group := range groups { |
169 found := false | 170 found := false |
(...skipping 12 matching lines...) Expand all Loading... | |
182 | 183 |
183 // The old juju group should have been reused. | 184 // The old juju group should have been reused. |
184 c.Check(groups[0].Id, Equals, oldJujuGroup.Id) | 185 c.Check(groups[0].Id, Equals, oldJujuGroup.Id) |
185 | 186 |
186 // Check that it authorizes the correct ports and there | 187 // Check that it authorizes the correct ports and there |
187 // are no extra permissions (in particular we are checking | 188 // are no extra permissions (in particular we are checking |
188 // that the unneeded permission that we added earlier | 189 // that the unneeded permission that we added earlier |
189 // has been deleted). | 190 // has been deleted). |
190 perms := info[0].IPPerms | 191 perms := info[0].IPPerms |
191 c.Assert(perms, HasLen, 5) | 192 c.Assert(perms, HasLen, 5) |
192 » checkPortAllowed(c, perms, 22) | 193 » checkPortAllowed(c, perms, 22) // SSH |
193 » checkPortAllowed(c, perms, ec2.MgoPort) | 194 » checkPortAllowed(c, perms, 37017) // MongoDB |
niemeyer
2012/11/28 15:58:37
37017 should be tested explicitly, otherwise we're
rog
2012/11/28 18:08:05
Done.
niemeyer
2012/11/28 18:37:51
Apparently not.
| |
194 checkSecurityGroupAllowed(c, perms, groups[0]) | 195 checkSecurityGroupAllowed(c, perms, groups[0]) |
195 | 196 |
196 // The old machine group should have been reused also. | 197 // The old machine group should have been reused also. |
197 c.Check(groups[2].Id, Equals, oldMachineGroup.Id) | 198 c.Check(groups[2].Id, Equals, oldMachineGroup.Id) |
198 | 199 |
199 // Check that each instance is part of the correct groups. | 200 // Check that each instance is part of the correct groups. |
200 » resp, err := ec2conn.Instances([]string{inst0.Id(), inst1.Id()}, nil) | 201 » resp, err := ec2conn.Instances([]string{string(inst0.Id()), string(inst1 .Id())}, nil) |
201 c.Assert(err, IsNil) | 202 c.Assert(err, IsNil) |
202 c.Assert(resp.Reservations, HasLen, 2) | 203 c.Assert(resp.Reservations, HasLen, 2) |
203 for _, r := range resp.Reservations { | 204 for _, r := range resp.Reservations { |
204 c.Assert(r.Instances, HasLen, 1) | 205 c.Assert(r.Instances, HasLen, 1) |
205 // each instance must be part of the general juju group. | 206 // each instance must be part of the general juju group. |
206 msg := Commentf("reservation %#v", r) | 207 msg := Commentf("reservation %#v", r) |
207 c.Assert(hasSecurityGroup(r, groups[0]), Equals, true, msg) | 208 c.Assert(hasSecurityGroup(r, groups[0]), Equals, true, msg) |
208 inst := r.Instances[0] | 209 inst := r.Instances[0] |
209 » » switch inst.InstanceId { | 210 » » switch state.InstanceId(inst.InstanceId) { |
210 case inst0.Id(): | 211 case inst0.Id(): |
211 c.Assert(hasSecurityGroup(r, groups[1]), Equals, true, m sg) | 212 c.Assert(hasSecurityGroup(r, groups[1]), Equals, true, m sg) |
212 c.Assert(hasSecurityGroup(r, groups[2]), Equals, false, msg) | 213 c.Assert(hasSecurityGroup(r, groups[2]), Equals, false, msg) |
213 case inst1.Id(): | 214 case inst1.Id(): |
214 c.Assert(hasSecurityGroup(r, groups[2]), Equals, true, m sg) | 215 c.Assert(hasSecurityGroup(r, groups[2]), Equals, true, m sg) |
215 c.Assert(hasSecurityGroup(r, groups[1]), Equals, false, msg) | 216 c.Assert(hasSecurityGroup(r, groups[1]), Equals, false, msg) |
216 default: | 217 default: |
217 c.Errorf("unknown instance found: %v", inst) | 218 c.Errorf("unknown instance found: %v", inst) |
218 } | 219 } |
219 } | 220 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
280 } | 281 } |
281 if len(protos) > 0 { | 282 if len(protos) > 0 { |
282 c.Errorf("%d security group permission not found for %#v in %#v" , len(protos), g, perms) | 283 c.Errorf("%d security group permission not found for %#v in %#v" , len(protos), g, perms) |
283 } | 284 } |
284 } | 285 } |
285 | 286 |
286 func (t *LiveTests) TestStopInstances(c *C) { | 287 func (t *LiveTests) TestStopInstances(c *C) { |
287 // It would be nice if this test was in jujutest, but | 288 // It would be nice if this test was in jujutest, but |
288 // there's no way for jujutest to fabricate a valid-looking | 289 // there's no way for jujutest to fabricate a valid-looking |
289 // instance id. | 290 // instance id. |
290 » inst0, err := t.Env.StartInstance(40, testing.InvalidStateInfo(40), nil) | 291 » inst0, err := t.Env.StartInstance("40", testing.InvalidStateInfo("40"), nil) |
291 c.Assert(err, IsNil) | 292 c.Assert(err, IsNil) |
292 | 293 |
293 inst1 := ec2.FabricateInstance(inst0, "i-aaaaaaaa") | 294 inst1 := ec2.FabricateInstance(inst0, "i-aaaaaaaa") |
294 | 295 |
295 » inst2, err := t.Env.StartInstance(41, testing.InvalidStateInfo(41), nil) | 296 » inst2, err := t.Env.StartInstance("41", testing.InvalidStateInfo("41"), nil) |
296 c.Assert(err, IsNil) | 297 c.Assert(err, IsNil) |
297 | 298 |
298 err = t.Env.StopInstances([]environs.Instance{inst0, inst1, inst2}) | 299 err = t.Env.StopInstances([]environs.Instance{inst0, inst1, inst2}) |
299 c.Check(err, IsNil) | 300 c.Check(err, IsNil) |
300 | 301 |
301 var insts []environs.Instance | 302 var insts []environs.Instance |
302 | 303 |
303 // We need the retry logic here because we are waiting | 304 // We need the retry logic here because we are waiting |
304 // for Instances to return an error, and it will not retry | 305 // for Instances to return an error, and it will not retry |
305 // if it succeeds. | 306 // if it succeeds. |
306 gone := false | 307 gone := false |
307 for a := ec2.ShortAttempt.Start(); a.Next(); { | 308 for a := ec2.ShortAttempt.Start(); a.Next(); { |
308 » » insts, err = t.Env.Instances([]string{inst0.Id(), inst2.Id()}) | 309 » » insts, err = t.Env.Instances([]state.InstanceId{inst0.Id(), inst 2.Id()}) |
309 if err == environs.ErrPartialInstances { | 310 if err == environs.ErrPartialInstances { |
310 // instances not gone yet. | 311 // instances not gone yet. |
311 continue | 312 continue |
312 } | 313 } |
313 if err == environs.ErrNoInstances { | 314 if err == environs.ErrNoInstances { |
314 gone = true | 315 gone = true |
315 break | 316 break |
316 } | 317 } |
317 c.Fatalf("error getting instances: %v", err) | 318 c.Fatalf("error getting instances: %v", err) |
318 } | 319 } |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
390 } | 391 } |
391 | 392 |
392 func hasSecurityGroup(r amzec2.Reservation, g amzec2.SecurityGroup) bool { | 393 func hasSecurityGroup(r amzec2.Reservation, g amzec2.SecurityGroup) bool { |
393 for _, rg := range r.SecurityGroups { | 394 for _, rg := range r.SecurityGroups { |
394 if rg.Id == g.Id { | 395 if rg.Id == g.Id { |
395 return true | 396 return true |
396 } | 397 } |
397 } | 398 } |
398 return false | 399 return false |
399 } | 400 } |
LEFT | RIGHT |