| 1 | package ecs |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/denverdino/aliyungo/common" |
| 7 | ) |
| 8 | |
| 9 | func TestSecurityGroups(t *testing.T) { |
| 10 | |
| 11 | client := NewTestClient() |
| 12 | regions, err := client.DescribeRegions() |
| 13 | |
| 14 | t.Log("regions: ", regions, err) |
| 15 | |
| 16 | for _, region := range regions { |
| 17 | |
| 18 | arg := DescribeSecurityGroupsArgs{ |
| 19 | RegionId: region.RegionId, |
| 20 | } |
| 21 | |
| 22 | sgs, _, err := client.DescribeSecurityGroups(&arg) |
| 23 | if err != nil { |
| 24 | t.Errorf("Failed to DescribeSecurityGroups for region %s: %v", region.RegionId, err) |
| 25 | continue |
| 26 | } |
| 27 | for _, sg := range sgs { |
| 28 | t.Logf("SecurityGroup: %++v", sg) |
| 29 | |
| 30 | args := DescribeSecurityGroupAttributeArgs{ |
| 31 | SecurityGroupId: sg.SecurityGroupId, |
| 32 | RegionId: region.RegionId, |
| 33 | } |
| 34 | sga, err := client.DescribeSecurityGroupAttribute(&args) |
| 35 | if err != nil { |
| 36 | t.Errorf("Failed to DescribeSecurityGroupAttribute %s: %v", sg.SecurityGroupId, err) |
| 37 | continue |
| 38 | } |
| 39 | t.Logf("SecurityGroup Attribute: %++v", sga) |
| 40 | |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestECSSecurityGroupCreationAndDeletion(t *testing.T) { |
| 46 | client := NewTestClient() |
| 47 | instance, err := client.DescribeInstanceAttribute(TestInstanceId) |
| 48 | if err != nil { |
| 49 | t.Fatalf("Failed to describe instance attribute %s: %v", TestInstanceId, err) |
| 50 | } |
| 51 | regionId := instance.RegionId |
| 52 | |
| 53 | _testECSSecurityGroupCreationAndDeletion(t, client, regionId, "") |
| 54 | |
| 55 | } |
| 56 | |
| 57 | func _testECSSecurityGroupCreationAndDeletion(t *testing.T, client *Client, regionId common.Region, vpcId string) { |
| 58 | |
| 59 | sgName := "test-security-group" |
| 60 | args := CreateSecurityGroupArgs{ |
| 61 | RegionId: regionId, |
| 62 | VpcId: vpcId, |
| 63 | SecurityGroupName: sgName, |
| 64 | } |
| 65 | |
| 66 | sgId, err := client.CreateSecurityGroup(&args) |
| 67 | if err != nil { |
| 68 | t.Fatalf("Failed to create security group %s: %v", sgName, err) |
| 69 | } |
| 70 | t.Logf("Security group %s is created successfully.", sgId) |
| 71 | |
| 72 | describeArgs := DescribeSecurityGroupAttributeArgs{ |
| 73 | SecurityGroupId: sgId, |
| 74 | RegionId: regionId, |
| 75 | } |
| 76 | sg, err := client.DescribeSecurityGroupAttribute(&describeArgs) |
| 77 | if err != nil { |
| 78 | t.Errorf("Failed to describe security group %s: %v", sgId, err) |
| 79 | } |
| 80 | t.Logf("Security group %s: %++v", sgId, sg) |
| 81 | |
| 82 | newName := "test-security-group-new" |
| 83 | modifyArgs := ModifySecurityGroupAttributeArgs{ |
| 84 | SecurityGroupId: sgId, |
| 85 | RegionId: regionId, |
| 86 | SecurityGroupName: newName, |
| 87 | } |
| 88 | err = client.ModifySecurityGroupAttribute(&modifyArgs) |
| 89 | if err != nil { |
| 90 | t.Errorf("Failed to modify security group %s: %v", sgId, err) |
| 91 | } else { |
| 92 | sg, err := client.DescribeSecurityGroupAttribute(&describeArgs) |
| 93 | if err != nil { |
| 94 | t.Errorf("Failed to describe security group %s: %v", sgId, err) |
| 95 | } |
| 96 | t.Logf("Security group %s: %++v", sgId, sg) |
| 97 | if sg.SecurityGroupName != newName { |
| 98 | t.Errorf("Failed to modify security group %s with new name %s", sgId, newName) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | err = client.DeleteSecurityGroup(regionId, sgId) |
| 103 | |
| 104 | if err != nil { |
| 105 | t.Fatalf("Failed to delete security group %s: %v", sgId, err) |
| 106 | } |
| 107 | t.Logf("Security group %s is deleted successfully.", sgId) |
| 108 | } |
| 109 | |
| 110 | func TestDescribeSecurityGroupAttribute(t *testing.T) { |
| 111 | client := NewTestClientForDebug() |
| 112 | args := DescribeSecurityGroupAttributeArgs{ |
| 113 | RegionId: common.Beijing, |
| 114 | SecurityGroupId: TestSecurityGroupId, |
| 115 | } |
| 116 | attr, err := client.DescribeSecurityGroupAttribute(&args) |
| 117 | if err != nil { |
| 118 | t.Fatalf("Failed to describe securitygroup attribute %++v", err) |
| 119 | } |
| 120 | t.Logf("Security group attribute is %++v", attr) |
| 121 | } |