forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 114
/
client_alert_resource_test.go
125 lines (111 loc) · 3.58 KB
/
client_alert_resource_test.go
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
122
123
124
125
package sls
import (
"encoding/json"
"os"
"testing"
"github.com/stretchr/testify/suite"
)
func TestUser(t *testing.T) {
suite.Run(t, new(UserTestSuite))
}
type UserTestSuite struct {
suite.Suite
endpoint string
accessKeyID string
accessKeySecret string
client Client
userId string
userName string
resourceName string
}
func (s *UserTestSuite) SetupSuite() {
s.endpoint = "cn-heyuan.log.aliyuncs.com"
s.accessKeyID = os.Getenv("LOG_TEST_ACCESS_KEY_ID")
s.accessKeySecret = os.Getenv("LOG_TEST_ACCESS_KEY_SECRET")
s.client.AccessKeyID = s.accessKeyID
s.client.AccessKeySecret = s.accessKeySecret
s.client.Endpoint = s.endpoint
s.resourceName = ResourceNameUser
s.userId = "user_id_1"
s.userName = "user name 1"
_ = s.client.DeleteResourceRecord(s.resourceName, s.userId)
}
func (s *UserTestSuite) TearDownSuite() {
_ = s.client.DeleteResourceRecord(s.resourceName, s.userId)
}
func (s *UserTestSuite) createUser() error {
customUser := new(ResourceRecord)
user := new(ResourceUser)
user.UserId = s.userId
user.UserName = s.userName
user.Phone = "13888888888"
user.CountryCode = "86"
user.Enabled = true
user.VoiceEnabled = true
user.SmsEnabled = true
customUser.Id = s.userId
customUser.Tag = s.userName
customUser.Value = JsonMarshal(user)
return s.client.CreateResourceRecord(s.resourceName, customUser)
}
func (s *UserTestSuite) TestClient_CreateUser() {
err := s.createUser()
s.Require().Nil(err)
err = s.client.DeleteResourceRecord(s.resourceName, s.userId)
s.Require().Nil(err)
}
func (s *UserTestSuite) TestClient_UpdateUser() {
err := s.createUser()
s.Require().Nil(err)
resourceRecord, err := s.client.GetResourceRecord(s.resourceName, s.userId)
s.Require().Nil(err)
user := new(ResourceUser)
err = json.Unmarshal([]byte(resourceRecord.Value), user)
s.Require().Nil(err)
user.UserName = "new name"
resourceRecord.Value = JsonMarshal(user)
err = s.client.UpdateResourceRecord(s.resourceName, resourceRecord)
s.Require().Nil(err)
resourceRecord, err = s.client.GetResourceRecord(s.resourceName, s.userId)
s.Require().Nil(err)
nUser := new(ResourceUser)
err = json.Unmarshal([]byte(resourceRecord.Value), nUser)
s.Require().Nil(err)
s.Require().Equal(`new name`, nUser.UserName, "update resourceRecord failed")
err = s.client.DeleteResourceRecord(s.resourceName, s.userId)
s.Require().Nil(err)
}
func (s *UserTestSuite) TestClient_DeleteUser() {
err := s.createUser()
s.Require().Nil(err)
_, err = s.client.GetResourceRecord(s.resourceName, s.userId)
s.Require().Nil(err)
err = s.client.DeleteResourceRecord(s.resourceName, s.userId)
s.Require().Nil(err)
_, err = s.client.GetResourceRecord(s.resourceName, s.userId)
s.Require().NotNil(err)
}
func (s *UserTestSuite) TestClient_GetUser() {
err := s.createUser()
s.Require().Nil(err)
getUserRecord, err := s.client.GetResourceRecord(s.resourceName, s.userId)
s.Require().Nil(err)
user := new(ResourceUser)
err = json.Unmarshal([]byte(getUserRecord.Value), user)
s.Require().Nil(err)
s.Require().Equal(user.UserId, s.userId)
err = s.client.DeleteResourceRecord(s.resourceName, s.userId)
s.Require().Nil(err)
}
func (s *UserTestSuite) TestClient_ListUser() {
err := s.createUser()
s.Require().Nil(err)
resourceRecords, count, total, err := s.client.ListResourceRecord(s.resourceName, 0, 100)
s.Require().Nil(err)
if total < 1 || count < 1 {
s.Require().Fail("list resourceRecord failed")
}
s.Require().Equal(count, len(resourceRecords), "there should be more than one resourceRecord")
err = s.client.DeleteResourceRecord(s.resourceName, s.userId)
s.Require().Nil(err)
}