OLD | NEW |
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 amzec2 "launchpad.net/goamz/ec2" | 7 amzec2 "launchpad.net/goamz/ec2" |
8 . "launchpad.net/gocheck" | 8 . "launchpad.net/gocheck" |
9 "launchpad.net/juju/go/environs" | 9 "launchpad.net/juju/go/environs" |
10 "launchpad.net/juju/go/environs/ec2" | 10 "launchpad.net/juju/go/environs/ec2" |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 // Create a same-named group for the second instance | 101 // Create a same-named group for the second instance |
102 // before starting it, to check that it's reused correctly. | 102 // before starting it, to check that it's reused correctly. |
103 oldMachineGroup := createGroup(c, ec2conn, groups[2].Name, "old machine
group") | 103 oldMachineGroup := createGroup(c, ec2conn, groups[2].Name, "old machine
group") |
104 | 104 |
105 inst1, err := t.Env.StartInstance(99, jujutest.InvalidStateInfo) | 105 inst1, err := t.Env.StartInstance(99, jujutest.InvalidStateInfo) |
106 c.Assert(err, IsNil) | 106 c.Assert(err, IsNil) |
107 defer t.Env.StopInstances([]environs.Instance{inst1}) | 107 defer t.Env.StopInstances([]environs.Instance{inst1}) |
108 | 108 |
109 groupsResp, err := ec2conn.SecurityGroups(groups, nil) | 109 groupsResp, err := ec2conn.SecurityGroups(groups, nil) |
110 c.Assert(err, IsNil) | 110 c.Assert(err, IsNil) |
111 » c.Assert(len(groupsResp.Groups), Equals, len(groups)) | 111 » c.Assert(groupsResp.Groups, HasLen, len(groups)) |
112 | 112 |
113 // For each group, check that it exists and record its id. | 113 // For each group, check that it exists and record its id. |
114 for i, group := range groups { | 114 for i, group := range groups { |
115 found := false | 115 found := false |
116 for _, g := range groupsResp.Groups { | 116 for _, g := range groupsResp.Groups { |
117 if g.Name == group.Name { | 117 if g.Name == group.Name { |
118 groups[i].Id = g.Id | 118 groups[i].Id = g.Id |
119 info[i] = g | 119 info[i] = g |
120 found = true | 120 found = true |
121 break | 121 break |
122 } | 122 } |
123 } | 123 } |
124 if !found { | 124 if !found { |
125 c.Fatalf("group %q not found", group.Name) | 125 c.Fatalf("group %q not found", group.Name) |
126 } | 126 } |
127 } | 127 } |
128 | 128 |
129 // The old juju group should have been reused. | 129 // The old juju group should have been reused. |
130 c.Check(groups[0].Id, Equals, oldJujuGroup.Id) | 130 c.Check(groups[0].Id, Equals, oldJujuGroup.Id) |
131 | 131 |
132 // Check that it authorizes the correct ports and there | 132 // Check that it authorizes the correct ports and there |
133 // are no extra permissions (in particular we are checking | 133 // are no extra permissions (in particular we are checking |
134 // that the unneeded permission that we added earlier | 134 // that the unneeded permission that we added earlier |
135 // has been deleted). | 135 // has been deleted). |
136 perms := info[0].IPPerms | 136 perms := info[0].IPPerms |
137 » c.Assert(len(perms), Equals, 2, Bug("got security groups %#v", perms)) | 137 » c.Assert(perms, HasLen, 2) |
138 checkPortAllowed(c, perms, 22) | 138 checkPortAllowed(c, perms, 22) |
139 checkPortAllowed(c, perms, 2181) | 139 checkPortAllowed(c, perms, 2181) |
140 | 140 |
141 // The old machine group should have been reused also. | 141 // The old machine group should have been reused also. |
142 c.Check(groups[2].Id, Equals, oldMachineGroup.Id) | 142 c.Check(groups[2].Id, Equals, oldMachineGroup.Id) |
143 | 143 |
144 // Check that each instance is part of the correct groups. | 144 // Check that each instance is part of the correct groups. |
145 resp, err := ec2conn.Instances([]string{inst0.Id(), inst1.Id()}, nil) | 145 resp, err := ec2conn.Instances([]string{inst0.Id(), inst1.Id()}, nil) |
146 c.Assert(err, IsNil) | 146 c.Assert(err, IsNil) |
147 » c.Assert(len(resp.Reservations), Equals, 2, Bug("reservations %#v", resp
.Reservations)) | 147 » c.Assert(resp.Reservations, HasLen, 2) |
148 for _, r := range resp.Reservations { | 148 for _, r := range resp.Reservations { |
149 » » c.Assert(len(r.Instances), Equals, 1) | 149 » » c.Assert(r.Instances, HasLen, 1) |
150 // each instance must be part of the general juju group. | 150 // each instance must be part of the general juju group. |
151 » » msg := Bug("reservation %#v", r) | 151 » » msg := Commentf("reservation %#v", r) |
152 c.Assert(hasSecurityGroup(r, groups[0]), Equals, true, msg) | 152 c.Assert(hasSecurityGroup(r, groups[0]), Equals, true, msg) |
153 inst := r.Instances[0] | 153 inst := r.Instances[0] |
154 switch inst.InstanceId { | 154 switch inst.InstanceId { |
155 case inst0.Id(): | 155 case inst0.Id(): |
156 c.Assert(hasSecurityGroup(r, groups[1]), Equals, true, m
sg) | 156 c.Assert(hasSecurityGroup(r, groups[1]), Equals, true, m
sg) |
157 c.Assert(hasSecurityGroup(r, groups[2]), Equals, false,
msg) | 157 c.Assert(hasSecurityGroup(r, groups[2]), Equals, false,
msg) |
158 case inst1.Id(): | 158 case inst1.Id(): |
159 c.Assert(hasSecurityGroup(r, groups[2]), Equals, true, m
sg) | 159 c.Assert(hasSecurityGroup(r, groups[2]), Equals, true, m
sg) |
160 c.Assert(hasSecurityGroup(r, groups[1]), Equals, false,
msg) | 160 c.Assert(hasSecurityGroup(r, groups[1]), Equals, false,
msg) |
161 default: | 161 default: |
162 c.Errorf("unknown instance found: %v", inst) | 162 c.Errorf("unknown instance found: %v", inst) |
163 } | 163 } |
164 } | 164 } |
165 } | 165 } |
166 | 166 |
167 func checkPortAllowed(c *C, perms []amzec2.IPPerm, port int) { | 167 func checkPortAllowed(c *C, perms []amzec2.IPPerm, port int) { |
168 for _, perm := range perms { | 168 for _, perm := range perms { |
169 if perm.FromPort == port { | 169 if perm.FromPort == port { |
170 c.Check(perm.Protocol, Equals, "tcp") | 170 c.Check(perm.Protocol, Equals, "tcp") |
171 c.Check(perm.ToPort, Equals, port) | 171 c.Check(perm.ToPort, Equals, port) |
172 » » » c.Check(perm.SourceIPs, Equals, []string{"0.0.0.0/0"}) | 172 » » » c.Check(perm.SourceIPs, DeepEquals, []string{"0.0.0.0/0"
}) |
173 » » » c.Check(len(perm.SourceGroups), Equals, 0) | 173 » » » c.Check(perm.SourceGroups, HasLen, 0) |
174 return | 174 return |
175 } | 175 } |
176 } | 176 } |
177 c.Errorf("ip port permission not found for %d in %#v", port, perms) | 177 c.Errorf("ip port permission not found for %d in %#v", port, perms) |
178 } | 178 } |
179 | 179 |
180 func (t *LiveTests) TestStopInstances(c *C) { | 180 func (t *LiveTests) TestStopInstances(c *C) { |
181 // It would be nice if this test was in jujutest, but | 181 // It would be nice if this test was in jujutest, but |
182 // there's no way for jujutest to fabricate a valid-looking | 182 // there's no way for jujutest to fabricate a valid-looking |
183 // instance id. | 183 // instance id. |
184 inst0, err := t.Env.StartInstance(40, jujutest.InvalidStateInfo) | 184 inst0, err := t.Env.StartInstance(40, jujutest.InvalidStateInfo) |
185 c.Assert(err, IsNil) | 185 c.Assert(err, IsNil) |
186 | 186 |
187 inst1 := ec2.FabricateInstance(inst0, "i-aaaaa") | 187 inst1 := ec2.FabricateInstance(inst0, "i-aaaaa") |
188 | 188 |
189 inst2, err := t.Env.StartInstance(41, jujutest.InvalidStateInfo) | 189 inst2, err := t.Env.StartInstance(41, jujutest.InvalidStateInfo) |
190 c.Assert(err, IsNil) | 190 c.Assert(err, IsNil) |
191 | 191 |
192 err = t.Env.StopInstances([]environs.Instance{inst0, inst1, inst2}) | 192 err = t.Env.StopInstances([]environs.Instance{inst0, inst1, inst2}) |
193 c.Check(err, IsNil) | 193 c.Check(err, IsNil) |
194 | 194 |
195 insts, err := t.Env.Instances([]string{inst0.Id(), inst2.Id()}) | 195 insts, err := t.Env.Instances([]string{inst0.Id(), inst2.Id()}) |
196 c.Check(err, Equals, environs.ErrMissingInstance) | 196 c.Check(err, Equals, environs.ErrMissingInstance) |
197 » c.Check(len(insts), Equals, 0) | 197 » c.Check(insts, HasLen, 0) |
198 } | 198 } |
199 | 199 |
200 // createGroup creates a new EC2 group and returns it. If it already exists, | 200 // createGroup creates a new EC2 group and returns it. If it already exists, |
201 // it revokes all its permissions and returns the existing group. | 201 // it revokes all its permissions and returns the existing group. |
202 func createGroup(c *C, ec2conn *amzec2.EC2, name, descr string) amzec2.SecurityG
roup { | 202 func createGroup(c *C, ec2conn *amzec2.EC2, name, descr string) amzec2.SecurityG
roup { |
203 resp, err := ec2conn.CreateSecurityGroup(name, descr) | 203 resp, err := ec2conn.CreateSecurityGroup(name, descr) |
204 if err == nil { | 204 if err == nil { |
205 return resp.SecurityGroup | 205 return resp.SecurityGroup |
206 } | 206 } |
207 if err.(*amzec2.Error).Code != "InvalidGroup.Duplicate" { | 207 if err.(*amzec2.Error).Code != "InvalidGroup.Duplicate" { |
(...skipping 11 matching lines...) Expand all Loading... |
219 } | 219 } |
220 | 220 |
221 func hasSecurityGroup(r amzec2.Reservation, g amzec2.SecurityGroup) bool { | 221 func hasSecurityGroup(r amzec2.Reservation, g amzec2.SecurityGroup) bool { |
222 for _, rg := range r.SecurityGroups { | 222 for _, rg := range r.SecurityGroups { |
223 if rg.Id == g.Id { | 223 if rg.Id == g.Id { |
224 return true | 224 return true |
225 } | 225 } |
226 } | 226 } |
227 return false | 227 return false |
228 } | 228 } |
OLD | NEW |