forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathclient_resource_test.go
134 lines (121 loc) · 3.61 KB
/
client_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
126
127
128
129
130
131
132
133
134
package sls
import (
"fmt"
"github.com/stretchr/testify/suite"
"os"
"testing"
"time"
)
func TestResource(t *testing.T) {
suite.Run(t, new(ResourceTestSuite))
}
type ResourceTestSuite struct {
suite.Suite
endpoint string
accessKeyID string
accessKeySecret string
client Client
resourceName string
}
func (s *ResourceTestSuite) 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())
}
func (s *ResourceTestSuite) TearDownTest() {
}
func (s *ResourceTestSuite) createResource() error {
rs := &ResourceSchema{
Schema: []*ResourceSchemaItem{
{
Column: "col1",
Desc: "col1 desc",
ExtInfo: map[string]string{},
Required: true,
Type: "string",
},
{
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 *ResourceTestSuite) TestClient_CreateResource() {
err := s.createResource()
s.Require().Nil(err)
err = s.client.DeleteResource(s.resourceName)
s.Require().Nil(err)
}
func (s *ResourceTestSuite) TestClient_UpdateResource() {
err := s.createResource()
s.Require().Nil(err)
resource, err := s.client.GetResource(s.resourceName)
s.Require().Nil(err)
rs := new(ResourceSchema)
err = rs.FromJsonString(resource.Schema)
s.Require().Nil(err)
rs.Schema[0].Desc = "new desc"
resource.Schema = rs.ToString()
err = s.client.UpdateResource(resource)
s.Require().Nil(err)
resource, err = s.client.GetResource(s.resourceName)
s.Require().Nil(err)
nrs := new(ResourceSchema)
err = nrs.FromJsonString(resource.Schema)
s.Require().Nil(err)
s.Require().Equal("new desc", rs.Schema[0].Desc, "update resource failed")
err = s.client.DeleteResource(s.resourceName)
s.Require().Nil(err)
}
func (s *ResourceTestSuite) TestClient_DeleteResource() {
err := s.createResource()
s.Require().Nil(err)
_, err = s.client.GetResource(s.resourceName)
s.Require().Nil(err)
err = s.client.DeleteResource(s.resourceName)
s.Require().Nil(err)
_, err = s.client.GetResource(s.resourceName)
s.Require().NotNil(err)
}
func (s *ResourceTestSuite) TestClient_GetResource() {
err := s.createResource()
s.Require().Nil(err)
getResource, err := s.client.GetResource(s.resourceName)
s.Require().Nil(err)
s.Require().Equal(getResource.Name, s.resourceName)
rs := new(ResourceSchema)
err = rs.FromJsonString(getResource.Schema)
s.Require().Nil(err)
s.Require().Equal(len(rs.Schema), 2)
s.Require().Equal(rs.Schema[0].Desc, "col1 desc")
err = s.client.DeleteResource(s.resourceName)
s.Require().Nil(err)
}
func (s *ResourceTestSuite) TestClient_ListResource() {
err := s.createResource()
s.Require().Nil(err)
resources, count, total, err := s.client.ListResource(ResourceTypeUserDefine, s.resourceName, 0, 100)
s.Require().Nil(err)
if total != 1 || count != 1 {
s.Require().Fail("list resource failed")
}
s.Require().Equal(1, len(resources), "there should be only one resource")
resource := resources[0]
s.Require().Equal(s.resourceName, resource.Name, "list resource failed")
err = s.client.DeleteResource(s.resourceName)
s.Require().Nil(err)
}