File: group_test.go

package info (click to toggle)
golang-github-centurylinkcloud-clc-sdk 0.0.2%2Bgit20161004.f62483c-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 236 kB
  • sloc: sh: 18; makefile: 13
file content (188 lines) | stat: -rw-r--r-- 6,558 bytes parent folder | download | duplicates (2)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package group_test

import (
	"encoding/json"
	"fmt"
	"net/url"
	"strings"
	"testing"

	"github.com/CenturyLinkCloud/clc-sdk/api"
	"github.com/CenturyLinkCloud/clc-sdk/group"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
)

func TestCreateGroup(t *testing.T) {
	assert := assert.New(t)

	client := NewMockClient()
	client.On("Post", "http://localhost/v2/groups/test", mock.Anything, mock.Anything).Return(nil)
	service := group.New(client)

	group := group.Group{
		Name:          "new",
		Description:   "my awesome group",
		ParentGroupID: "12345",
	}
	resp, err := service.Create(group)

	assert.Nil(err)
	assert.Equal(group.Name, resp.Name)
	assert.Equal(1, len(resp.Groups))
	assert.Equal(group.ParentGroupID, resp.ParentGroupID())
	client.AssertExpectations(t)
}

func TestGetGroup(t *testing.T) {
	assert := assert.New(t)

	client := NewMockClient()
	client.On("Get", "http://localhost/v2/groups/test/67890", mock.Anything).Return(nil)
	service := group.New(client)

	id := "67890"
	resp, err := service.Get(id)

	assert.Nil(err)
	assert.Equal(id, resp.ID)
	assert.Equal("12345", resp.ParentGroupID())
	assert.Equal(resp.Servers(), []string{})
	client.AssertExpectations(t)
}

func TestUpdateGroup(t *testing.T) {
	assert := assert.New(t)

	client := NewMockClient()
	client.On("Patch", "http://localhost/v2/groups/test/67890", mock.Anything, mock.Anything).Return(nil)
	service := group.New(client)

	id := "67890"
	patches := make([]api.Update, 3)
	patches[0] = group.UpdateName("foobar")
	patches[1] = group.UpdateDescription("mangled")
	patches[2] = group.UpdateParentGroupID("mangled")

	err := service.Update(id, patches...)

	assert.Nil(err)
	client.AssertExpectations(t)
}

func TestDeleteGroup(t *testing.T) {
	assert := assert.New(t)

	client := NewMockClient()
	client.On("Delete", "http://localhost/v2/groups/test/67890", mock.Anything).Return(nil)
	service := group.New(client)

	id := "67890"
	resp, err := service.Delete(id)

	assert.Nil(err)
	assert.Equal("status", resp.Rel)
	assert.NotEmpty(resp.ID)
	client.AssertExpectations(t)
}

func TestArchiveGroup(t *testing.T) {
	assert := assert.New(t)

	client := NewMockClient()
	client.On("Post", "http://localhost/v2/groups/test/67890/archive", "", mock.Anything).Return(nil)
	service := group.New(client)

	id := "67890"
	resp, err := service.Archive(id)

	assert.Nil(err)
	assert.Equal("status", resp.Rel)
	assert.Equal("wa1-12345", resp.ID)
	client.AssertExpectations(t)
}

func TestRestoreGroup(t *testing.T) {
	assert := assert.New(t)

	client := NewMockClient()
	client.On("Post", "http://localhost/v2/groups/test/67890/restore", `{"targetGroupId": "55555"}`, mock.Anything).Return(nil)
	service := group.New(client)

	id := "67890"
	resp, err := service.Restore(id, "55555")

	assert.Nil(err)
	ok, qid := resp.GetStatusID()
	assert.True(ok)
	assert.Equal("wa1-12345", qid)
	client.AssertExpectations(t)
}

// conspicuously missing
//func SetDefaults(t *testing.T) {}
//func SetHorizontalAutoscalePolicy(t *testing.T) {}

func NewMockClient() *MockClient {
	return &MockClient{}
}

type MockClient struct {
	mock.Mock
}

func (m *MockClient) Get(url string, resp interface{}) error {
	json.Unmarshal([]byte(mockGroup), resp)
	args := m.Called(url, resp)
	return args.Error(0)
}

func (m *MockClient) Post(url string, body, resp interface{}) error {
	var canned string
	if strings.HasSuffix(url, "archive") {
		canned = statusResp
	} else if strings.HasSuffix(url, "restore") {
		canned = queuedResp
	} else if strings.HasSuffix(url, "/v2/groups/test") {
		canned = mockGroup
	} else {
		return fmt.Errorf("%v unmocked. add an impl", url)
	}
	json.Unmarshal([]byte(canned), resp)
	args := m.Called(url, body, resp)
	return args.Error(0)
}

func (m *MockClient) Put(url string, body, resp interface{}) error {
	args := m.Called(url, body, resp)
	return args.Error(0)
}

func (m *MockClient) Patch(url string, body, resp interface{}) error {
	args := m.Called(url, body, resp)
	return args.Error(0)
}

func (m *MockClient) Delete(url string, resp interface{}) error {
	json.Unmarshal([]byte(`{"id":"va1-12345","rel":"status","href":"/v2/operations/test/status/va1-12345"}`), resp)
	args := m.Called(url, resp)
	return args.Error(0)
}

func (m *MockClient) Config() *api.Config {
	u, _ := url.Parse("http://localhost/v2")
	return &api.Config{
		User: api.User{
			Username: "test.user",
			Password: "s0s3cur3",
		},
		Alias:   "test",
		BaseURL: u,
	}
}

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":"user@domain.com","modifiedDate":"2014-05-16T23:49:25Z","modifiedBy":"user@domain.com"},"customFields":[]}`

const statusResp = `{"rel":"status", "href":"/v2/operations/acct/status/wa1-12345", "id":"wa1-12345"}`

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"}]}`