Debian Codegraph @

ram/role_test.go

raw symbols SCIP
1 package ram
2
3 import (
4 "encoding/json"
5 "os"
6 "strconv"
7 "testing"
8 "time"
9 )
10
11 /*
12 Please also set account id in env so that roles could be created test
13 AccessKeyId=YourAccessKeyId AccessKeySecret=YourAccessKeySecret AccountId=111111111 go test -v -run=Role
14 */
15 var (
16 accountId = os.Getenv("AccountId")
17 roleName = strconv.FormatInt(time.Now().Unix(), 10)
18
19 princpal = AssumeRolePolicyPrincpal{RAM: []string{"acs:ram::" + accountId + ":root"}}
20
21 policyDocument = AssumeRolePolicyDocument{
22 Statement: []AssumeRolePolicyItem{
23 AssumeRolePolicyItem{Action: "sts:AssumeRole", Effect: "Allow", Principal: princpal},
24 },
25 Version: "1"}
26
27 newPolicyDocument = AssumeRolePolicyDocument{
28 Statement: []AssumeRolePolicyItem{
29 AssumeRolePolicyItem{Action: "sts:AssumeRole", Effect: "Deny", Principal: princpal},
30 },
31 Version: "1"}
32
33 role = RoleRequest{
34 RoleName: roleName,
35 AssumeRolePolicyDocument: getAssumeRolePolicyDocumentStr(),
36 Description: "this is a role for unit test purpose",
37 }
38
39 updateRoleRequest = UpdateRoleRequest{
40 RoleName: roleName,
41 NewAssumeRolePolicyDocument: getNewAssumeRolePolicyDocumentStr(),
42 }
43
44 roleQuery = RoleQueryRequest{RoleName: roleName}
45 )
46
47 func getAssumeRolePolicyDocumentStr() string {
48 b, _ := json.Marshal(policyDocument)
49 return string(b)
50 }
51
52 func getNewAssumeRolePolicyDocumentStr() string {
53 b, _ := json.Marshal(newPolicyDocument)
54 return string(b)
55 }
56
57 func TestCreateRole(t *testing.T) {
58 client := NewTestClient()
59 resp, err := client.CreateRole(role)
60 if err != nil {
61 t.Errorf("Failed to CreateRole %v", err)
62 }
63 t.Logf("pass CreateRole %v", resp)
64 }
65
66 func TestGetRole(t *testing.T) {
67 client := NewTestClient()
68 resp, err := client.GetRole(roleQuery)
69 if err != nil {
70 t.Errorf("Failed to GetRole %v", err)
71 }
72 t.Logf("pass GetRole %v", resp)
73 }
74
75 func TestUpdateRole(t *testing.T) {
76 client := NewTestClient()
77 resp, err := client.UpdateRole(updateRoleRequest)
78 if err != nil {
79 t.Errorf("Failed to UpdateRole %v", err)
80 }
81 t.Logf("pass UpdateRole %v", resp)
82 }
83
84 func TestListRoles(t *testing.T) {
85 client := NewTestClient()
86 resp, err := client.ListRoles()
87 if err != nil {
88 t.Errorf("Failed to ListRoles %v", err)
89 }
90 t.Logf("pass ListRoles %v", resp)
91 }
92
93 func TestDeleteRole(t *testing.T) {
94 client := NewTestClient()
95 resp, err := client.DeleteRole(RoleQueryRequest{RoleName: roleName})
96 if err != nil {
97 t.Errorf("Failed to DeleteRole %v", err)
98 }
99 t.Logf("pass DeleteRole %v", resp)
100 }