OLD | NEW |
1 package ec2_test | 1 package ec2_test |
2 | 2 |
3 import ( | 3 import ( |
4 "fmt" | 4 "fmt" |
5 "launchpad.net/goamz/aws" | 5 "launchpad.net/goamz/aws" |
6 "launchpad.net/goamz/ec2" | 6 "launchpad.net/goamz/ec2" |
7 "launchpad.net/goamz/ec2/ec2test" | 7 "launchpad.net/goamz/ec2/ec2test" |
8 . "launchpad.net/gocheck" | 8 . "launchpad.net/gocheck" |
| 9 "regexp" |
9 "sort" | 10 "sort" |
10 ) | 11 ) |
11 | 12 |
12 // LocalServer represents a local ec2test fake server. | 13 // LocalServer represents a local ec2test fake server. |
13 type LocalServer struct { | 14 type LocalServer struct { |
14 auth aws.Auth | 15 auth aws.Auth |
15 region aws.Region | 16 region aws.Region |
16 srv *ec2test.Server | 17 srv *ec2test.Server |
17 } | 18 } |
18 | 19 |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 c.Assert(err, NotNil) | 153 c.Assert(err, NotNil) |
153 c.Check(err.(*ec2.Error).Code, Equals, "InvalidPermission.Malformed") | 154 c.Check(err.(*ec2.Error).Code, Equals, "InvalidPermission.Malformed") |
154 | 155 |
155 // Check that AuthorizeSecurityGroup adds the correct authorizations. | 156 // Check that AuthorizeSecurityGroup adds the correct authorizations. |
156 _, err = s.ec2.AuthorizeSecurityGroup(g0, []ec2.IPPerm{{ | 157 _, err = s.ec2.AuthorizeSecurityGroup(g0, []ec2.IPPerm{{ |
157 Protocol: "tcp", | 158 Protocol: "tcp", |
158 FromPort: 2000, | 159 FromPort: 2000, |
159 ToPort: 2001, | 160 ToPort: 2001, |
160 SourceIPs: []string{"127.0.0.0/24"}, | 161 SourceIPs: []string{"127.0.0.0/24"}, |
161 SourceGroups: []ec2.UserSecurityGroup{{ | 162 SourceGroups: []ec2.UserSecurityGroup{{ |
162 OwnerId: ownerId, | |
163 Name: g1.Name, | 163 Name: g1.Name, |
164 }, { | 164 }, { |
165 Id: g0.Id, | 165 Id: g0.Id, |
166 }}, | 166 }}, |
167 }, { | 167 }, { |
168 Protocol: "tcp", | 168 Protocol: "tcp", |
169 FromPort: 2000, | 169 FromPort: 2000, |
170 ToPort: 2001, | 170 ToPort: 2001, |
171 SourceIPs: []string{"200.1.1.34/32"}, | 171 SourceIPs: []string{"200.1.1.34/32"}, |
172 }}) | 172 }}) |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 _, err = s.ec2.AuthorizeSecurityGroup(ec2.SecurityGroup{Name: name}, per
ms[0:2]) | 271 _, err = s.ec2.AuthorizeSecurityGroup(ec2.SecurityGroup{Name: name}, per
ms[0:2]) |
272 c.Assert(err, ErrorMatches, `.*\(InvalidPermission.Duplicate\)`) | 272 c.Assert(err, ErrorMatches, `.*\(InvalidPermission.Duplicate\)`) |
273 } | 273 } |
274 | 274 |
275 type filterSpec struct { | 275 type filterSpec struct { |
276 name string | 276 name string |
277 values []string | 277 values []string |
278 } | 278 } |
279 | 279 |
280 func (s *ServerTests) TestInstanceFiltering(c *C) { | 280 func (s *ServerTests) TestInstanceFiltering(c *C) { |
281 » groupResp, err := s.ec2.CreateSecurityGroup(uniqueName(s.ec2, "testgroup
1"), "testgroup one description") | 281 » groupResp, err := s.ec2.CreateSecurityGroup(sessionName("testgroup1"), "
testgroup one description") |
282 c.Assert(err, IsNil) | 282 c.Assert(err, IsNil) |
283 group1 := groupResp.SecurityGroup | 283 group1 := groupResp.SecurityGroup |
284 defer s.ec2.DeleteSecurityGroup(group1) | 284 defer s.ec2.DeleteSecurityGroup(group1) |
285 | 285 |
286 » groupResp, err = s.ec2.CreateSecurityGroup(uniqueName(s.ec2, "testgroup2
"), "testgroup two description") | 286 » groupResp, err = s.ec2.CreateSecurityGroup(sessionName("testgroup2"), "t
estgroup two description") |
287 c.Assert(err, IsNil) | 287 c.Assert(err, IsNil) |
288 group2 := groupResp.SecurityGroup | 288 group2 := groupResp.SecurityGroup |
289 defer s.ec2.DeleteSecurityGroup(group2) | 289 defer s.ec2.DeleteSecurityGroup(group2) |
290 | 290 |
291 insts := make([]*ec2.Instance, 3) | 291 insts := make([]*ec2.Instance, 3) |
292 inst, err := s.ec2.RunInstances(&ec2.RunInstances{ | 292 inst, err := s.ec2.RunInstances(&ec2.RunInstances{ |
293 MinCount: 2, | 293 MinCount: 2, |
294 ImageId: imageId, | 294 ImageId: imageId, |
295 InstanceType: "t1.micro", | 295 InstanceType: "t1.micro", |
296 SecurityGroups: []ec2.SecurityGroup{group1}, | 296 SecurityGroups: []ec2.SecurityGroup{group1}, |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 func namesOnly(gs []ec2.SecurityGroup) []ec2.SecurityGroup { | 429 func namesOnly(gs []ec2.SecurityGroup) []ec2.SecurityGroup { |
430 for i := range gs { | 430 for i := range gs { |
431 gs[i].Id = "" | 431 gs[i].Id = "" |
432 } | 432 } |
433 return gs | 433 return gs |
434 } | 434 } |
435 | 435 |
436 func (s *ServerTests) TestGroupFiltering(c *C) { | 436 func (s *ServerTests) TestGroupFiltering(c *C) { |
437 g := make([]ec2.SecurityGroup, 4) | 437 g := make([]ec2.SecurityGroup, 4) |
438 for i := range g[0:3] { | 438 for i := range g[0:3] { |
439 » » resp, err := s.ec2.CreateSecurityGroup(uniqueName(s.ec2, fmt.Spr
intf("testgroup%d", i)), fmt.Sprintf("testdescription%d", i)) | 439 » » resp, err := s.ec2.CreateSecurityGroup(sessionName(fmt.Sprintf("
testgroup%d", i)), fmt.Sprintf("testdescription%d", i)) |
440 c.Assert(err, IsNil) | 440 c.Assert(err, IsNil) |
441 g[i] = resp.SecurityGroup | 441 g[i] = resp.SecurityGroup |
442 c.Logf("group %d: %v", i, g[i]) | 442 c.Logf("group %d: %v", i, g[i]) |
443 defer s.ec2.DeleteSecurityGroup(g[i]) | 443 defer s.ec2.DeleteSecurityGroup(g[i]) |
444 } | 444 } |
445 // Get the default group. | 445 // Get the default group. |
446 resp, err := s.ec2.SecurityGroups([]ec2.SecurityGroup{{Name: "default"}}
, nil) | 446 resp, err := s.ec2.SecurityGroups([]ec2.SecurityGroup{{Name: "default"}}
, nil) |
447 c.Assert(err, IsNil) | 447 c.Assert(err, IsNil) |
448 g[3] = resp.Groups[0].SecurityGroup | 448 g[3] = resp.Groups[0].SecurityGroup |
449 | 449 |
(...skipping 27 matching lines...) Expand all Loading... |
477 gs = append(gs, g[index]) | 477 gs = append(gs, g[index]) |
478 } | 478 } |
479 return | 479 return |
480 } | 480 } |
481 | 481 |
482 type groupTest struct { | 482 type groupTest struct { |
483 about string | 483 about string |
484 groups []ec2.SecurityGroup // groupIds argument to SecurityG
roups method. | 484 groups []ec2.SecurityGroup // groupIds argument to SecurityG
roups method. |
485 filters []filterSpec // filters argument to SecurityGr
oups method. | 485 filters []filterSpec // filters argument to SecurityGr
oups method. |
486 results []ec2.SecurityGroup // set of expected result groups. | 486 results []ec2.SecurityGroup // set of expected result groups. |
487 » » allowExtra bool // results may be incomplete. | 487 » » allowExtra bool // specified results may be incom
plete. |
488 err string // expected error. | 488 err string // expected error. |
489 } | 489 } |
490 filterCheck := func(name, val string, gs []ec2.SecurityGroup) groupTest
{ | 490 filterCheck := func(name, val string, gs []ec2.SecurityGroup) groupTest
{ |
491 return groupTest{ | 491 return groupTest{ |
492 about: "filter check " + name, | 492 about: "filter check " + name, |
493 filters: []filterSpec{{name, []string{val}}}, | 493 filters: []filterSpec{{name, []string{val}}}, |
494 results: gs, | 494 results: gs, |
| 495 allowExtra: true, |
495 } | 496 } |
496 } | 497 } |
497 tests := []groupTest{ | 498 tests := []groupTest{ |
498 { | 499 { |
499 about: "check that SecurityGroups returns all group
s", | 500 about: "check that SecurityGroups returns all group
s", |
500 » » » groups: groups(0, 1, 2), | 501 » » » results: groups(0, 1, 2, 3), |
501 allowExtra: true, | 502 allowExtra: true, |
502 }, { | 503 }, { |
503 about: "check that specifying two group ids returns th
em", | 504 about: "check that specifying two group ids returns th
em", |
504 groups: idsOnly(groups(0, 2)), | 505 groups: idsOnly(groups(0, 2)), |
505 results: groups(0, 2), | 506 results: groups(0, 2), |
506 }, { | 507 }, { |
507 about: "check that specifying names only works", | 508 about: "check that specifying names only works", |
508 groups: namesOnly(groups(0, 2)), | 509 groups: namesOnly(groups(0, 2)), |
509 results: groups(0, 2), | 510 results: groups(0, 2), |
510 }, { | 511 }, { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 continue | 556 continue |
556 } | 557 } |
557 c.Assert(err, IsNil) | 558 c.Assert(err, IsNil) |
558 groups := make(map[string]*ec2.SecurityGroup) | 559 groups := make(map[string]*ec2.SecurityGroup) |
559 for j := range resp.Groups { | 560 for j := range resp.Groups { |
560 group := &resp.Groups[j].SecurityGroup | 561 group := &resp.Groups[j].SecurityGroup |
561 c.Check(groups[group.Id], IsNil, Commentf("duplicate gro
up id: %q", group.Id)) | 562 c.Check(groups[group.Id], IsNil, Commentf("duplicate gro
up id: %q", group.Id)) |
562 | 563 |
563 groups[group.Id] = group | 564 groups[group.Id] = group |
564 } | 565 } |
565 » » if !t.allowExtra { | 566 » » // If extra groups may be returned, eliminate all groups that |
566 » » » c.Check(groups, HasLen, len(t.results)) | 567 » » // we did not create in this session apart from the default grou
p. |
| 568 » » if t.allowExtra { |
| 569 » » » namePat := regexp.MustCompile(sessionName("testgroup[0-9
]")) |
| 570 » » » for id, g := range groups { |
| 571 » » » » if g.Name != "default" && !namePat.MatchString(g
.Name) { |
| 572 » » » » » delete(groups, id) |
| 573 » » » » } |
| 574 » » » } |
567 } | 575 } |
| 576 c.Check(groups, HasLen, len(t.results)) |
568 for j, g := range t.results { | 577 for j, g := range t.results { |
569 rg := groups[g.Id] | 578 rg := groups[g.Id] |
570 c.Assert(rg, NotNil, Commentf("group %d (%v) not found;
got %#v", j, g, groups)) | 579 c.Assert(rg, NotNil, Commentf("group %d (%v) not found;
got %#v", j, g, groups)) |
571 c.Check(rg.Name, Equals, g.Name, Commentf("group %d (%v)
", j, g)) | 580 c.Check(rg.Name, Equals, g.Name, Commentf("group %d (%v)
", j, g)) |
572 } | 581 } |
573 } | 582 } |
574 } | 583 } |
OLD | NEW |