Debian Codegraph @

group/group_test.go

raw symbols SCIP
1 package group_test
2
3 import (
4 "encoding/json"
5 "fmt"
6 "net/url"
7 "strings"
8 "testing"
9
10 "github.com/CenturyLinkCloud/clc-sdk/api"
11 "github.com/CenturyLinkCloud/clc-sdk/group"
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/mock"
14 )
15
16 func TestCreateGroup(t *testing.T) {
17 assert := assert.New(t)
18
19 client := NewMockClient()
20 client.On("Post", "http://localhost/v2/groups/test", mock.Anything, mock.Anything).Return(nil)
21 service := group.New(client)
22
23 group := group.Group{
24 Name: "new",
25 Description: "my awesome group",
26 ParentGroupID: "12345",
27 }
28 resp, err := service.Create(group)
29
30 assert.Nil(err)
31 assert.Equal(group.Name, resp.Name)
32 assert.Equal(1, len(resp.Groups))
33 assert.Equal(group.ParentGroupID, resp.ParentGroupID())
34 client.AssertExpectations(t)
35 }
36
37 func TestGetGroup(t *testing.T) {
38 assert := assert.New(t)
39
40 client := NewMockClient()
41 client.On("Get", "http://localhost/v2/groups/test/67890", mock.Anything).Return(nil)
42 service := group.New(client)
43
44 id := "67890"
45 resp, err := service.Get(id)
46
47 assert.Nil(err)
48 assert.Equal(id, resp.ID)
49 assert.Equal("12345", resp.ParentGroupID())
50 assert.Equal(resp.Servers(), []string{})
51 client.AssertExpectations(t)
52 }
53
54 func TestUpdateGroup(t *testing.T) {
55 assert := assert.New(t)
56
57 client := NewMockClient()
58 client.On("Patch", "http://localhost/v2/groups/test/67890", mock.Anything, mock.Anything).Return(nil)
59 service := group.New(client)
60
61 id := "67890"
62 patches := make([]api.Update, 3)
63 patches[0] = group.UpdateName("foobar")
64 patches[1] = group.UpdateDescription("mangled")
65 patches[2] = group.UpdateParentGroupID("mangled")
66
67 err := service.Update(id, patches...)
68
69 assert.Nil(err)
70 client.AssertExpectations(t)
71 }
72
73 func TestDeleteGroup(t *testing.T) {
74 assert := assert.New(t)
75
76 client := NewMockClient()
77 client.On("Delete", "http://localhost/v2/groups/test/67890", mock.Anything).Return(nil)
78 service := group.New(client)
79
80 id := "67890"
81 resp, err := service.Delete(id)
82
83 assert.Nil(err)
84 assert.Equal("status", resp.Rel)
85 assert.NotEmpty(resp.ID)
86 client.AssertExpectations(t)
87 }
88
89 func TestArchiveGroup(t *testing.T) {
90 assert := assert.New(t)
91
92 client := NewMockClient()
93 client.On("Post", "http://localhost/v2/groups/test/67890/archive", "", mock.Anything).Return(nil)
94 service := group.New(client)
95
96 id := "67890"
97 resp, err := service.Archive(id)
98
99 assert.Nil(err)
100 assert.Equal("status", resp.Rel)
101 assert.Equal("wa1-12345", resp.ID)
102 client.AssertExpectations(t)
103 }
104
105 func TestRestoreGroup(t *testing.T) {
106 assert := assert.New(t)
107
108 client := NewMockClient()
109 client.On("Post", "http://localhost/v2/groups/test/67890/restore", `{"targetGroupId": "55555"}`, mock.Anything).Return(nil)
110 service := group.New(client)
111
112 id := "67890"
113 resp, err := service.Restore(id, "55555")
114
115 assert.Nil(err)
116 ok, qid := resp.GetStatusID()
117 assert.True(ok)
118 assert.Equal("wa1-12345", qid)
119 client.AssertExpectations(t)
120 }
121
122 // conspicuously missing
123 //func SetDefaults(t *testing.T) {}
124 //func SetHorizontalAutoscalePolicy(t *testing.T) {}
125
126 func NewMockClient() *MockClient {
127 return &MockClient{}
128 }
129
130 type MockClient struct {
131 mock.Mock
132 }
133
134 func (m *MockClient) Get(url string, resp interface{}) error {
135 json.Unmarshal([]byte(mockGroup), resp)
136 args := m.Called(url, resp)
137 return args.Error(0)
138 }
139
140 func (m *MockClient) Post(url string, body, resp interface{}) error {
141 var canned string
142 if strings.HasSuffix(url, "archive") {
143 canned = statusResp
144 } else if strings.HasSuffix(url, "restore") {
145 canned = queuedResp
146 } else if strings.HasSuffix(url, "/v2/groups/test") {
147 canned = mockGroup
148 } else {
149 return fmt.Errorf("%v unmocked. add an impl", url)
150 }
151 json.Unmarshal([]byte(canned), resp)
152 args := m.Called(url, body, resp)
153 return args.Error(0)
154 }
155
156 func (m *MockClient) Put(url string, body, resp interface{}) error {
157 args := m.Called(url, body, resp)
158 return args.Error(0)
159 }
160
161 func (m *MockClient) Patch(url string, body, resp interface{}) error {
162 args := m.Called(url, body, resp)
163 return args.Error(0)
164 }
165
166 func (m *MockClient) Delete(url string, resp interface{}) error {
167 json.Unmarshal([]byte(`{"id":"va1-12345","rel":"status","href":"/v2/operations/test/status/va1-12345"}`), resp)
168 args := m.Called(url, resp)
169 return args.Error(0)
170 }
171
172 func (m *MockClient) Config() *api.Config {
173 u, _ := url.Parse("http://localhost/v2")
174 return &api.Config{
175 User: api.User{
176 Username: "test.user",
177 Password: "s0s3cur3",
178 },
179 Alias: "test",
180 BaseURL: u,
181 }
182 }
183
184 const mockGroup = `{"id":"67890","name":"new","description":"my awesome group","locationId":"WA1","type":"default","status":"active","groups":[{"id":"12345","name":"Parent Group Name","description":"The parent group.","locationId":"WA1","type":"default","status":"active","serversCount":0,"groups":[],"links":[{"rel":"createGroup","href":"/v2/groups/acct","verbs":["POST"]},{"rel":"createServer","href":"/v2/servers/acct","verbs":["POST"]},{"rel":"self","href":"/v2/groups/acct/12345","verbs":["GET","PATCH","DELETE"]},{"rel":"parentGroup","href":"/v2/groups/acct/12345","id":"12345"},{"rel":"defaults","href":"/v2/groups/acct/12345/defaults","verbs":["GET","POST"]},{"rel":"billing","href":"/v2/groups/acct/12345/billing"},{"rel":"archiveGroupAction","href":"/v2/groups/acct/12345/archive"},{"rel":"statistics","href":"/v2/groups/acct/12345/statistics"},{"rel":"upcomingScheduledActivities","href":"/v2/groups/acct/12345/upcomingScheduledActivities"},{"rel":"horizontalAutoscalePolicyMapping","href":"/v2/groups/acct/12345/horizontalAutoscalePolicy","verbs":["GET","PUT","DELETE"]},{"rel":"scheduledActivities","href":"/v2/groups/acct/12345/scheduledActivities","verbs":["GET","POST"]}]}],"links":[{"rel":"self","href":"/v2/groups/acct/67890"},{"rel":"parentGroup","href":"/v2/groups/acct/12345","id":"12345"},{"rel":"billing","href":"/v2/groups/acct/67890/billing"},{"rel":"archiveGroupAction","href":"/v2/groups/acct/67890/archive"},{"rel":"statistics","href":"/v2/groups/acct/67890/statistics"},{"rel":"scheduledActivities","href":"/v2/groups/acct/67890/scheduledActivities"}],"changeInfo":{"createdDate":"2012-12-17T01:17:17Z","createdBy":"[email protected]","modifiedDate":"2014-05-16T23:49:25Z","modifiedBy":"[email protected]"},"customFields":[]}`
185
186 const statusResp = `{"rel":"status", "href":"/v2/operations/acct/status/wa1-12345", "id":"wa1-12345"}`
187
188 const queuedResp = `{"isQueued":true, "links":[{"rel":"self", "href":"/v2/groups/acct/67890?AccountAlias=ALIAS&identifier=2a5c0b9662cf4fc8bf6180f139facdc0"}, {"rel":"status", "href":"/v2/operations/acct/status/wa1-12345", "id":"wa1-12345"}]}`