forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathclient_resource_record_test.go
139 lines (124 loc) · 4.25 KB
/
client_resource_record_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package sls
import (
"fmt"
"os"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
func TestResourceRecord(t *testing.T) {
suite.Run(t, new(ResourceRecordTestSuite))
}
type ResourceRecordTestSuite struct {
suite.Suite
endpoint string
accessKeyID string
accessKeySecret string
client Client
recordId string
tagName string
resourceName string
}
func (s *ResourceRecordTestSuite) 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 = fmt.Sprintf("user.test_resource_%d", time.Now().Unix())
s.recordId = "test_record_1"
s.tagName = "test record"
_ = s.client.DeleteResource(s.resourceName)
_ = s.createResource()
}
func (s *ResourceRecordTestSuite) createResource() error {
rs := &ResourceSchema{
Schema: []*ResourceSchemaItem{
&ResourceSchemaItem{
Column: "col1",
Desc: "col1 desc",
ExtInfo: map[string]string{},
Required: true,
Type: "string",
},
&ResourceSchemaItem{
Column: "col2",
Desc: "col2 desc",
ExtInfo: "optional",
Required: true,
Type: "string",
},
},
}
customResource := new(Resource)
customResource.Type = ResourceTypeUserDefine
customResource.Name = s.resourceName
customResource.Schema = rs.ToString()
customResource.Description = "user test resource 1 descc"
return s.client.CreateResource(customResource)
}
func (s *ResourceRecordTestSuite) TearDownSuite() {
err := s.client.DeleteResource(s.resourceName)
s.Require().Nil(err)
}
func (s *ResourceRecordTestSuite) createResourceRecord() error {
customResourceRecord := new(ResourceRecord)
customResourceRecord.Id = s.recordId
customResourceRecord.Tag = s.tagName
customResourceRecord.Value = `{"col1": "sls", "col2": "tag"}`
return s.client.CreateResourceRecord(s.resourceName, customResourceRecord)
}
func (s *ResourceRecordTestSuite) TestClient_CreateResourceRecord() {
err := s.createResourceRecord()
s.Require().Nil(err)
err = s.client.DeleteResourceRecord(s.resourceName, s.recordId)
s.Require().Nil(err)
}
func (s *ResourceRecordTestSuite) TestClient_UpdateResourceRecord() {
err := s.createResourceRecord()
s.Require().Nil(err)
resourceRecord, err := s.client.GetResourceRecord(s.resourceName, s.recordId)
s.Require().Nil(err)
resourceRecord.Value = `{"col1": "new sls", "col2": "new tag"}`
err = s.client.UpdateResourceRecord(s.resourceName, resourceRecord)
s.Require().Nil(err)
resourceRecord, err = s.client.GetResourceRecord(s.resourceName, s.recordId)
s.Require().Nil(err)
s.Require().Equal(`{"col1": "new sls", "col2": "new tag"}`, resourceRecord.Value, "update resourceRecord failed")
err = s.client.DeleteResourceRecord(s.resourceName, s.recordId)
s.Require().Nil(err)
}
func (s *ResourceRecordTestSuite) TestClient_DeleteResourceRecord() {
err := s.createResourceRecord()
s.Require().Nil(err)
_, err = s.client.GetResourceRecord(s.resourceName, s.recordId)
s.Require().Nil(err)
err = s.client.DeleteResourceRecord(s.resourceName, s.recordId)
s.Require().Nil(err)
_, err = s.client.GetResourceRecord(s.resourceName, s.recordId)
s.Require().NotNil(err)
}
func (s *ResourceRecordTestSuite) TestClient_GetResourceRecord() {
err := s.createResourceRecord()
s.Require().Nil(err)
getResourceRecord, err := s.client.GetResourceRecord(s.resourceName, s.recordId)
s.Require().Nil(err)
s.Require().Equal(getResourceRecord.Id, s.recordId)
err = s.client.DeleteResourceRecord(s.resourceName, s.recordId)
s.Require().Nil(err)
}
func (s *ResourceRecordTestSuite) TestClient_ListResourceRecord() {
err := s.createResourceRecord()
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(1, len(resourceRecords), "there should be only one resourceRecord")
resourceRecord := resourceRecords[0]
s.Require().Equal(s.recordId, resourceRecord.Id, "list resourceRecord failed")
err = s.client.DeleteResourceRecord(s.resourceName, s.recordId)
s.Require().Nil(err)
}