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
|
package ram
import (
"encoding/json"
"os"
"strconv"
"testing"
"time"
)
/*
Please also set account id in env so that roles could be created test
AccessKeyId=YourAccessKeyId AccessKeySecret=YourAccessKeySecret AccountId=111111111 go test -v -run=Role
*/
var (
accountId = os.Getenv("AccountId")
roleName = strconv.FormatInt(time.Now().Unix(), 10)
princpal = AssumeRolePolicyPrincpal{RAM: []string{"acs:ram::" + accountId + ":root"}}
policyDocument = AssumeRolePolicyDocument{
Statement: []AssumeRolePolicyItem{
AssumeRolePolicyItem{Action: "sts:AssumeRole", Effect: "Allow", Principal: princpal},
},
Version: "1"}
newPolicyDocument = AssumeRolePolicyDocument{
Statement: []AssumeRolePolicyItem{
AssumeRolePolicyItem{Action: "sts:AssumeRole", Effect: "Deny", Principal: princpal},
},
Version: "1"}
role = RoleRequest{
RoleName: roleName,
AssumeRolePolicyDocument: getAssumeRolePolicyDocumentStr(),
Description: "this is a role for unit test purpose",
}
updateRoleRequest = UpdateRoleRequest{
RoleName: roleName,
NewAssumeRolePolicyDocument: getNewAssumeRolePolicyDocumentStr(),
}
roleQuery = RoleQueryRequest{RoleName: roleName}
)
func getAssumeRolePolicyDocumentStr() string {
b, _ := json.Marshal(policyDocument)
return string(b)
}
func getNewAssumeRolePolicyDocumentStr() string {
b, _ := json.Marshal(newPolicyDocument)
return string(b)
}
func TestCreateRole(t *testing.T) {
client := NewTestClient()
resp, err := client.CreateRole(role)
if err != nil {
t.Errorf("Failed to CreateRole %v", err)
}
t.Logf("pass CreateRole %v", resp)
}
func TestGetRole(t *testing.T) {
client := NewTestClient()
resp, err := client.GetRole(roleQuery)
if err != nil {
t.Errorf("Failed to GetRole %v", err)
}
t.Logf("pass GetRole %v", resp)
}
func TestUpdateRole(t *testing.T) {
client := NewTestClient()
resp, err := client.UpdateRole(updateRoleRequest)
if err != nil {
t.Errorf("Failed to UpdateRole %v", err)
}
t.Logf("pass UpdateRole %v", resp)
}
func TestListRoles(t *testing.T) {
client := NewTestClient()
resp, err := client.ListRoles()
if err != nil {
t.Errorf("Failed to ListRoles %v", err)
}
t.Logf("pass ListRoles %v", resp)
}
func TestDeleteRole(t *testing.T) {
client := NewTestClient()
resp, err := client.DeleteRole(RoleQueryRequest{RoleName: roleName})
if err != nil {
t.Errorf("Failed to DeleteRole %v", err)
}
t.Logf("pass DeleteRole %v", resp)
}
|