func (s *S) TestDescribeSecurityGroupsExampleWithFilter(c *C) { testServer.Response(200, nil, DescribeSecurityGroupsExample) filter := compute.NewFilter() filter.Add("ip-permission.protocol", "tcp") filter.Add("ip-permission.from-port", "22") filter.Add("ip-permission.to-port", "22") filter.Add("ip-permission.group-name", "app_server_group", "database_group") _, err := s.compute.SecurityGroups(nil, filter) req := testServer.WaitRequest() c.Assert(req.Form["Action"], DeepEquals, []string{"DescribeSecurityGroups"}) c.Assert(req.Form["Filter.1.Name"], DeepEquals, []string{"ip-permission.from-port"}) c.Assert(req.Form["Filter.1.Value.1"], DeepEquals, []string{"22"}) c.Assert(req.Form["Filter.2.Name"], DeepEquals, []string{"ip-permission.group-name"}) c.Assert(req.Form["Filter.2.Value.1"], DeepEquals, []string{"app_server_group"}) c.Assert(req.Form["Filter.2.Value.2"], DeepEquals, []string{"database_group"}) c.Assert(req.Form["Filter.3.Name"], DeepEquals, []string{"ip-permission.protocol"}) c.Assert(req.Form["Filter.3.Value.1"], DeepEquals, []string{"tcp"}) c.Assert(req.Form["Filter.4.Name"], DeepEquals, []string{"ip-permission.to-port"}) c.Assert(req.Form["Filter.4.Value.1"], DeepEquals, []string{"22"}) c.Assert(err, IsNil) }
func (s *S) TestDescribeInstancesExample2(c *C) { testServer.Response(200, nil, DescribeInstancesExample2) filter := compute.NewFilter() filter.Add("key1", "value1") filter.Add("key2", "value2", "value3") resp, err := s.compute.DescribeInstances([]string{"i-1", "i-2"}, filter) req := testServer.WaitRequest() c.Assert(req.Form["Action"], DeepEquals, []string{"DescribeInstances"}) c.Assert(req.Form["InstanceId.1"], DeepEquals, []string{"i-1"}) c.Assert(req.Form["InstanceId.2"], DeepEquals, []string{"i-2"}) c.Assert(req.Form["Filter.1.Name"], DeepEquals, []string{"key1"}) c.Assert(req.Form["Filter.1.Value.1"], DeepEquals, []string{"value1"}) c.Assert(req.Form["Filter.1.Value.2"], IsNil) c.Assert(req.Form["Filter.2.Name"], DeepEquals, []string{"key2"}) c.Assert(req.Form["Filter.2.Value.1"], DeepEquals, []string{"value2"}) c.Assert(req.Form["Filter.2.Value.2"], DeepEquals, []string{"value3"}) c.Assert(err, IsNil) c.Assert(resp.RequestId, Equals, "59dbff89-35bd-4eac-99ed-be587EXAMPLE") c.Assert(resp.Reservations, HasLen, 1) r0 := resp.Reservations[0] r0i := r0.Instances[0] c.Assert(r0i.State.Code, Equals, 16) c.Assert(r0i.State.Name, Equals, "running") }
func (s *S) TestDescribeInstancesExample1(c *C) { testServer.Response(200, nil, DescribeInstancesExample1) filter := compute.NewFilter() filter.Add("key1", "value1") filter.Add("key2", "value2", "value3") resp, err := s.compute.DescribeInstances([]string{"i-1", "i-2"}, nil) req := testServer.WaitRequest() c.Assert(req.Form["Action"], DeepEquals, []string{"DescribeInstances"}) c.Assert(req.Form["InstanceId.1"], DeepEquals, []string{"i-1"}) c.Assert(req.Form["InstanceId.2"], DeepEquals, []string{"i-2"}) c.Assert(err, IsNil) c.Assert(resp.RequestId, Equals, "98e3c9a4-848c-4d6d-8e8a-b1bdEXAMPLE") c.Assert(resp.Reservations, HasLen, 2) r0 := resp.Reservations[0] c.Assert(r0.ReservationId, Equals, "r-b27e30d9") c.Assert(r0.OwnerId, Equals, "999988887777") c.Assert(r0.RequesterId, Equals, "854251627541") c.Assert(r0.SecurityGroups, DeepEquals, []compute.SecurityGroup{{Name: "default", Id: "sg-67ad940e"}}) c.Assert(r0.Instances, HasLen, 1) r0i := r0.Instances[0] c.Assert(r0i.InstanceId, Equals, "c5cd56af") }
func (s *S) TestDescribeImagesExample(c *C) { testServer.Response(200, nil, DescribeImagesExample) filter := compute.NewFilter() filter.Add("key1", "value1") filter.Add("key2", "value2", "value3") resp, err := s.compute.Images([]string{"ami-1", "ami-2"}, filter) req := testServer.WaitRequest() c.Assert(req.Form["Action"], DeepEquals, []string{"DescribeImages"}) c.Assert(req.Form["ImageId.1"], DeepEquals, []string{"ami-1"}) c.Assert(req.Form["ImageId.2"], DeepEquals, []string{"ami-2"}) c.Assert(req.Form["Filter.1.Name"], DeepEquals, []string{"key1"}) c.Assert(req.Form["Filter.1.Value.1"], DeepEquals, []string{"value1"}) c.Assert(req.Form["Filter.1.Value.2"], IsNil) c.Assert(req.Form["Filter.2.Name"], DeepEquals, []string{"key2"}) c.Assert(req.Form["Filter.2.Value.1"], DeepEquals, []string{"value2"}) c.Assert(req.Form["Filter.2.Value.2"], DeepEquals, []string{"value3"}) c.Assert(err, IsNil) c.Assert(resp.RequestId, Equals, "4a4a27a2-2e7c-475d-b35b-ca822EXAMPLE") c.Assert(resp.Images, HasLen, 1) i0 := resp.Images[0] c.Assert(i0.Id, Equals, "29") c.Assert(i0.Type, Equals, "machine") c.Assert(i0.Name, Equals, "CentOS 6.4 64bit Plain") c.Assert(i0.Description, Equals, "") c.Assert(i0.Location, Equals, "") c.Assert(i0.State, Equals, "available") c.Assert(i0.OwnerId, Equals, "niftycloud") c.Assert(i0.OwnerAlias, Equals, "ニフティ株式会社") c.Assert(i0.Architecture, Equals, "x86_64") c.Assert(i0.RootDeviceType, Equals, "disk") c.Assert(i0.RootDeviceName, Equals, "") testServer.Response(200, nil, DescribeImagesExample) resp2, err := s.compute.ImagesByOwners([]string{"1", "2"}, []string{"niftycloud", "id2"}, filter) req2 := testServer.WaitRequest() c.Assert(req2.Form["Action"], DeepEquals, []string{"DescribeImages"}) c.Assert(req2.Form["ImageId.1"], DeepEquals, []string{"1"}) c.Assert(req2.Form["ImageId.2"], DeepEquals, []string{"2"}) c.Assert(req2.Form["Owner.1"], DeepEquals, []string{"niftycloud"}) c.Assert(req2.Form["Owner.2"], DeepEquals, []string{"id2"}) c.Assert(req2.Form["Filter.1.Name"], DeepEquals, []string{"key1"}) c.Assert(req2.Form["Filter.1.Value.1"], DeepEquals, []string{"value1"}) c.Assert(req2.Form["Filter.1.Value.2"], IsNil) c.Assert(req2.Form["Filter.2.Name"], DeepEquals, []string{"key2"}) c.Assert(req2.Form["Filter.2.Value.1"], DeepEquals, []string{"value2"}) c.Assert(req2.Form["Filter.2.Value.2"], DeepEquals, []string{"value3"}) c.Assert(err, IsNil) c.Assert(resp2.RequestId, Equals, "4a4a27a2-2e7c-475d-b35b-ca822EXAMPLE") c.Assert(resp2.Images, HasLen, 1) i1 := resp2.Images[0] c.Assert(i1.Id, Equals, "29") c.Assert(i1.Type, Equals, "machine") c.Assert(i1.Name, Equals, "CentOS 6.4 64bit Plain") c.Assert(i1.Description, Equals, "") c.Assert(i1.Location, Equals, "") c.Assert(i1.State, Equals, "available") c.Assert(i1.OwnerId, Equals, "niftycloud") c.Assert(i1.OwnerAlias, Equals, "ニフティ株式会社") c.Assert(i1.Architecture, Equals, "x86_64") c.Assert(i1.RootDeviceType, Equals, "disk") c.Assert(i1.RootDeviceName, Equals, "") }
func (s *ServerTests) TestGroupFiltering(c *C) { g := make([]compute.SecurityGroup, 4) for i := range g { resp, err := s.compute.CreateSecurityGroup(compute.SecurityGroup{Name: sessionName(fmt.Sprintf("testgroup%d", i)), Description: fmt.Sprintf("testdescription%d", i)}) c.Assert(err, IsNil) g[i] = resp.SecurityGroup c.Logf("group %d: %v", i, g[i]) defer s.compute.DeleteSecurityGroup(g[i]) } perms := [][]compute.IPPerm{ {{ Protocol: "tcp", FromPort: 100, ToPort: 200, SourceIPs: []string{"1.2.3.4/32"}, }}, {{ Protocol: "tcp", FromPort: 200, ToPort: 300, SourceGroups: []compute.UserSecurityGroup{{Id: g[1].Id}}, }}, {{ Protocol: "udp", FromPort: 200, ToPort: 400, SourceGroups: []compute.UserSecurityGroup{{Id: g[1].Id}}, }}, } for i, ps := range perms { _, err := s.compute.AuthorizeSecurityGroup(g[i], ps) c.Assert(err, IsNil) } groups := func(indices ...int) (gs []compute.SecurityGroup) { for _, index := range indices { gs = append(gs, g[index]) } return } type groupTest struct { about string groups []compute.SecurityGroup // groupIds argument to SecurityGroups method. filters []filterSpec // filters argument to SecurityGroups method. results []compute.SecurityGroup // set of expected result groups. allowExtra bool // specified results may be incomplete. err string // expected error. } filterCheck := func(name, val string, gs []compute.SecurityGroup) groupTest { return groupTest{ about: "filter check " + name, filters: []filterSpec{{name, []string{val}}}, results: gs, allowExtra: true, } } tests := []groupTest{ { about: "check that SecurityGroups returns all groups", results: groups(0, 1, 2, 3), allowExtra: true, }, { about: "check that specifying two group ids returns them", groups: idsOnly(groups(0, 2)), results: groups(0, 2), }, { about: "check that specifying names only works", groups: namesOnly(groups(0, 2)), results: groups(0, 2), }, { about: "check that specifying a non-existent group id gives an error", groups: append(groups(0), compute.SecurityGroup{Id: "sg-eeeeeeeee"}), err: `.*\(InvalidGroup\.NotFound\)`, }, { about: "check that a filter allowed two groups returns both of them", filters: []filterSpec{ {"group-id", []string{g[0].Id, g[2].Id}}, }, results: groups(0, 2), }, { about: "check that the previous filter works when specifying a list of ids", groups: groups(1, 2), filters: []filterSpec{ {"group-id", []string{g[0].Id, g[2].Id}}, }, results: groups(2), }, { about: "check that a filter allowing no groups returns none", filters: []filterSpec{ {"group-id", []string{"sg-eeeeeeeee"}}, }, }, filterCheck("description", "testdescription1", groups(1)), filterCheck("group-name", g[2].Name, groups(2)), filterCheck("ip-permission.cidr", "1.2.3.4/32", groups(0)), filterCheck("ip-permission.group-name", g[1].Name, groups(1, 2)), filterCheck("ip-permission.protocol", "udp", groups(2)), filterCheck("ip-permission.from-port", "200", groups(1, 2)), filterCheck("ip-permission.to-port", "200", groups(0)), // TODO owner-id } for i, t := range tests { c.Logf("%d. %s", i, t.about) var f *compute.Filter if t.filters != nil { f = compute.NewFilter() for _, spec := range t.filters { f.Add(spec.name, spec.values...) } } resp, err := s.compute.SecurityGroups(t.groups, f) if t.err != "" { c.Check(err, ErrorMatches, t.err) continue } c.Assert(err, IsNil) groups := make(map[string]*compute.SecurityGroup) for j := range resp.Groups { group := &resp.Groups[j].SecurityGroup c.Check(groups[group.Id], IsNil, Commentf("duplicate group id: %q", group.Id)) groups[group.Id] = group } // If extra groups may be returned, eliminate all groups that // we did not create in this session apart from the default group. if t.allowExtra { namePat := regexp.MustCompile(sessionName("testgroup[0-9]")) for id, g := range groups { if !namePat.MatchString(g.Name) { delete(groups, id) } } } c.Check(groups, HasLen, len(t.results)) for j, g := range t.results { rg := groups[g.Id] c.Assert(rg, NotNil, Commentf("group %d (%v) not found; got %#v", j, g, groups)) c.Check(rg.Name, Equals, g.Name, Commentf("group %d (%v)", j, g)) } } }
func (s *ServerTests) TestIPPerms(c *C) { g0 := s.makeTestGroup(c, "goamz-test0", "computetest group 0") defer s.compute.DeleteSecurityGroup(g0) g1 := s.makeTestGroup(c, "goamz-test1", "computetest group 1") defer s.compute.DeleteSecurityGroup(g1) resp, err := s.compute.SecurityGroups([]compute.SecurityGroup{g0, g1}, nil) c.Assert(err, IsNil) c.Assert(resp.Groups, HasLen, 2) c.Assert(resp.Groups[0].IPPerms, HasLen, 0) c.Assert(resp.Groups[1].IPPerms, HasLen, 0) ownerId := resp.Groups[0].OwnerId // test some invalid parameters // TODO more _, err = s.compute.AuthorizeSecurityGroup(g0, []compute.IPPerm{{ Protocol: "tcp", FromPort: 0, ToPort: 1024, SourceIPs: []string{"z127.0.0.1/24"}, }}) c.Assert(err, NotNil) c.Check(err.(*compute.Error).Code, Equals, "InvalidPermission.Malformed") // Check that AuthorizeSecurityGroup adds the correct authorizations. _, err = s.compute.AuthorizeSecurityGroup(g0, []compute.IPPerm{{ Protocol: "tcp", FromPort: 2000, ToPort: 2001, SourceIPs: []string{"127.0.0.0/24"}, SourceGroups: []compute.UserSecurityGroup{{ Name: g1.Name, }, { Id: g0.Id, }}, }, { Protocol: "tcp", FromPort: 2000, ToPort: 2001, SourceIPs: []string{"200.1.1.34/32"}, }}) c.Assert(err, IsNil) resp, err = s.compute.SecurityGroups([]compute.SecurityGroup{g0}, nil) c.Assert(err, IsNil) c.Assert(resp.Groups, HasLen, 1) c.Assert(resp.Groups[0].IPPerms, HasLen, 1) perm := resp.Groups[0].IPPerms[0] srcg := perm.SourceGroups c.Assert(srcg, HasLen, 2) // Normalize so we don't care about returned order. if srcg[0].Name == g1.Name { srcg[0], srcg[1] = srcg[1], srcg[0] } c.Check(srcg[0].Name, Equals, g0.Name) c.Check(srcg[0].Id, Equals, g0.Id) c.Check(srcg[0].OwnerId, Equals, ownerId) c.Check(srcg[1].Name, Equals, g1.Name) c.Check(srcg[1].Id, Equals, g1.Id) c.Check(srcg[1].OwnerId, Equals, ownerId) sort.Strings(perm.SourceIPs) c.Check(perm.SourceIPs, DeepEquals, []string{"127.0.0.0/24", "200.1.1.34/32"}) // Check that we can't delete g1 (because g0 is using it) _, err = s.compute.DeleteSecurityGroup(g1) c.Assert(err, NotNil) c.Check(err.(*compute.Error).Code, Equals, "InvalidGroup.InUse") _, err = s.compute.RevokeSecurityGroup(g0, []compute.IPPerm{{ Protocol: "tcp", FromPort: 2000, ToPort: 2001, SourceGroups: []compute.UserSecurityGroup{{Id: g1.Id}}, }, { Protocol: "tcp", FromPort: 2000, ToPort: 2001, SourceIPs: []string{"200.1.1.34/32"}, }}) c.Assert(err, IsNil) resp, err = s.compute.SecurityGroups([]compute.SecurityGroup{g0}, nil) c.Assert(err, IsNil) c.Assert(resp.Groups, HasLen, 1) c.Assert(resp.Groups[0].IPPerms, HasLen, 1) perm = resp.Groups[0].IPPerms[0] srcg = perm.SourceGroups c.Assert(srcg, HasLen, 1) c.Check(srcg[0].Name, Equals, g0.Name) c.Check(srcg[0].Id, Equals, g0.Id) c.Check(srcg[0].OwnerId, Equals, ownerId) c.Check(perm.SourceIPs, DeepEquals, []string{"127.0.0.0/24"}) // We should be able to delete g1 now because we've removed its only use. _, err = s.compute.DeleteSecurityGroup(g1) c.Assert(err, IsNil) _, err = s.compute.DeleteSecurityGroup(g0) c.Assert(err, IsNil) f := compute.NewFilter() f.Add("group-id", g0.Id, g1.Id) resp, err = s.compute.SecurityGroups(nil, f) c.Assert(err, IsNil) c.Assert(resp.Groups, HasLen, 0) }