File: security_groups_test.go

package info (click to toggle)
golang-github-denverdino-aliyungo 0.0~git20180921.13fa8aa-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,824 kB
  • sloc: xml: 1,359; makefile: 3
file content (121 lines) | stat: -rw-r--r-- 3,348 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package ecs

import (
	"testing"

	"github.com/denverdino/aliyungo/common"
)

func TestSecurityGroups(t *testing.T) {

	client := NewTestClient()
	regions, err := client.DescribeRegions()

	t.Log("regions: ", regions, err)

	for _, region := range regions {

		arg := DescribeSecurityGroupsArgs{
			RegionId: region.RegionId,
		}

		sgs, _, err := client.DescribeSecurityGroups(&arg)
		if err != nil {
			t.Errorf("Failed to DescribeSecurityGroups for region %s: %v", region.RegionId, err)
			continue
		}
		for _, sg := range sgs {
			t.Logf("SecurityGroup: %++v", sg)

			args := DescribeSecurityGroupAttributeArgs{
				SecurityGroupId: sg.SecurityGroupId,
				RegionId:        region.RegionId,
			}
			sga, err := client.DescribeSecurityGroupAttribute(&args)
			if err != nil {
				t.Errorf("Failed to DescribeSecurityGroupAttribute %s: %v", sg.SecurityGroupId, err)
				continue
			}
			t.Logf("SecurityGroup Attribute: %++v", sga)

		}
	}
}

func TestECSSecurityGroupCreationAndDeletion(t *testing.T) {
	client := NewTestClient()
	instance, err := client.DescribeInstanceAttribute(TestInstanceId)
	if err != nil {
		t.Fatalf("Failed to describe instance attribute %s: %v", TestInstanceId, err)
	}
	regionId := instance.RegionId

	_testECSSecurityGroupCreationAndDeletion(t, client, regionId, "")

}

func _testECSSecurityGroupCreationAndDeletion(t *testing.T, client *Client, regionId common.Region, vpcId string) {

	sgName := "test-security-group"
	args := CreateSecurityGroupArgs{
		RegionId:          regionId,
		VpcId:             vpcId,
		SecurityGroupName: sgName,
	}

	sgId, err := client.CreateSecurityGroup(&args)
	if err != nil {
		t.Fatalf("Failed to create security group %s: %v", sgName, err)
	}
	t.Logf("Security group %s is created successfully.", sgId)

	describeArgs := DescribeSecurityGroupAttributeArgs{
		SecurityGroupId: sgId,
		RegionId:        regionId,
	}
	sg, err := client.DescribeSecurityGroupAttribute(&describeArgs)
	if err != nil {
		t.Errorf("Failed to describe security group %s: %v", sgId, err)
	}
	t.Logf("Security group %s: %++v", sgId, sg)

	newName := "test-security-group-new"
	modifyArgs := ModifySecurityGroupAttributeArgs{
		SecurityGroupId:   sgId,
		RegionId:          regionId,
		SecurityGroupName: newName,
	}
	err = client.ModifySecurityGroupAttribute(&modifyArgs)
	if err != nil {
		t.Errorf("Failed to modify security group %s: %v", sgId, err)
	} else {
		sg, err := client.DescribeSecurityGroupAttribute(&describeArgs)
		if err != nil {
			t.Errorf("Failed to describe security group %s: %v", sgId, err)
		}
		t.Logf("Security group %s: %++v", sgId, sg)
		if sg.SecurityGroupName != newName {
			t.Errorf("Failed to modify security group %s with new name %s", sgId, newName)
		}
	}

	err = client.DeleteSecurityGroup(regionId, sgId)

	if err != nil {
		t.Fatalf("Failed to delete security group %s: %v", sgId, err)
	}
	t.Logf("Security group %s is deleted successfully.", sgId)
}

func TestDescribeSecurityGroupAttribute(t *testing.T) {
	client := NewTestClientForDebug()
	args := DescribeSecurityGroupAttributeArgs{
		RegionId:        common.Beijing,
		SecurityGroupId: TestSecurityGroupId,
	}
	attr, err := client.DescribeSecurityGroupAttribute(&args)
	if err != nil {
		t.Fatalf("Failed to describe securitygroup attribute %++v", err)
	}
	t.Logf("Security group attribute is %++v", attr)
}