File: resource_test.go

package info (click to toggle)
golang-github-akamai-akamaiopen-edgegrid-golang 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,408 kB
  • sloc: sh: 532; makefile: 3
file content (322 lines) | stat: -rw-r--r-- 13,892 bytes parent folder | download
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package configgtm

import (
	"testing"

	"github.com/akamai/AkamaiOPEN-edgegrid-golang/jsonhooks-v1"

	"github.com/stretchr/testify/assert"
	"gopkg.in/h2non/gock.v1"
)

var GtmTestResource = "testResource"

func instantiateResource() *Resource {

	resource := NewResource("dummy")
	resourceData := []byte(`{
                                "aggregationType" : "median",
                                "constrainedProperty" : null,
                                "decayRate" : null,
                                "description" : null,
                                "hostHeader" : null,
                                "leaderString" : null,
                                "leastSquaresDecay" : null,
                                "loadImbalancePercentage" : null,
                                "maxUMultiplicativeIncrement" : null,
                                "name" : "testResource",
                                "resourceInstances" : [ {
                                        "loadObject" : "",
                                        "loadObjectPort" : 0,
                                        "loadServers" : null,
                                        "datacenterId" : 3131,
                                        "useDefaultLoadObject" : false
                                } ],
                                "type" : "Download score",
                                "upperBound" : 0,
                                "links" : [ {
                                        "rel" : "self",
                                        "href" : "https://akaa-32qkzqewderdchot-d3uwbyqc4pqi2c5l.luna-dev.akamaiapis.net/config-gtm/v1/domains/gtmdomtest.akadns.net/resources/testResource"
                                } ]
                       }`)
	jsonhooks.Unmarshal(resourceData, resource)

	return resource

}

// Verify ListResources. Should pass, e.g. no API errors and non nil list.
func TestListResources(t *testing.T) {

	defer gock.Off()

	mock := gock.New("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/config-gtm/v1/domains/" + gtmTestDomain + "/resources")
	mock.
		Get("/config-gtm/v1/domains/"+gtmTestDomain+"/resources").
		HeaderPresent("Authorization").
		Reply(200).
		SetHeader("Content-Type", "application/vnd.config-gtm.v1.3+json;charset=UTF-8").
		BodyString(`{
                        "items" : [ {
                                "aggregationType" : "median",
                                "constrainedProperty" : null,
                                "decayRate" : null,
                                "description" : null,
                                "hostHeader" : null,
                                "leaderString" : null,
                                "leastSquaresDecay" : null,
                                "loadImbalancePercentage" : null,
                                "maxUMultiplicativeIncrement" : null,
                                "name" : "testResource",
                                "resourceInstances" : [ ],
                                "type" : "Download score",
                                "upperBound" : 0,
                                "links" : [ {
                                        "rel" : "self",
                                        "href" : "https://akaa-32qkzqewderdchot-d3uwbyqc4pqi2c5l.luna-dev.akamaiapis.net/config-gtm/v1/domains/gtmdomtest.akadns.net/resources/testResource"
                                } ]
                        } ]
               }`)

	Init(config)
	resourceList, err := ListResources(gtmTestDomain)
	assert.NoError(t, err)
	assert.NotEqual(t, resourceList, nil)

	if len(resourceList) > 0 {
		firstResource := resourceList[0]
		assert.Equal(t, firstResource.Name, GtmTestResource)
	} else {
		t.Fatal("List empty!")
	}
}

// Depends on CreateResource
func TestGetResource(t *testing.T) {

	defer gock.Off()

	mock := gock.New("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/config-gtm/v1/domains/" + gtmTestDomain + "/resources/" + GtmTestResource)
	mock.
		Get("/config-gtm/v1/domains/"+gtmTestDomain+"/resources/"+GtmTestResource).
		HeaderPresent("Authorization").
		Reply(200).
		SetHeader("Content-Type", "application/vnd.config-gtm.v1.3+json;charset=UTF-8").
		BodyString(`{
                        "aggregationType" : "median",
                        "constrainedProperty" : null,
                        "decayRate" : null,
                        "description" : null,
                        "hostHeader" : null,
                        "leaderString" : null,
                        "leastSquaresDecay" : null,
                        "loadImbalancePercentage" : null,
                        "maxUMultiplicativeIncrement" : null,
                        "name" : "testResource",
                        "resourceInstances" : [ ],
                        "type" : "Download score",
                        "upperBound" : 0,
                        "links" : [ {
                                "rel" : "self",
                                "href" : "https://akaa-32qkzqewderdchot-d3uwbyqc4pqi2c5l.luna-dev.akamaiapis.net/config-gtm/v1/domains/gtmdomtest.akadns.net/resources/testResource"
                        } ]
              }`)

	Init(config)

	testResource, err := GetResource(GtmTestResource, gtmTestDomain)
	assert.NoError(t, err)
	assert.IsType(t, &Resource{}, testResource)
	assert.Equal(t, GtmTestResource, testResource.Name)

}

// Verify failed case for GetResource. Should pass, e.g. no API errors and domain not found
func TestGetBadResource(t *testing.T) {

	defer gock.Off()

	mock := gock.New("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/config-gtm/v1/domains/" + gtmTestDomain + "/resources/somebadname")
	mock.
		Get("/config-gtm/v1/domains/"+gtmTestDomain+"/resources/somebadname").
		HeaderPresent("Authorization").
		Reply(404).
		SetHeader("Content-Type", "application/vnd.config-gtm.v1.3+json;charset=UTF-8").
		BodyString(`{
                        }`)

	Init(config)

	_, err := GetResource("somebadname", gtmTestDomain)
	// Shouldn't have found
	assert.Error(t, err)

}

// Test Create resource.
func TestCreateResource(t *testing.T) {

	defer gock.Off()
	mock := gock.New("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/config-gtm/v1/domains/" + gtmTestDomain + "/resources/" + GtmTestResource)
	mock.
		Put("/config-gtm/v1/domains/"+gtmTestDomain+"/resources/"+GtmTestResource).
		HeaderPresent("Authorization").
		Reply(200).
		SetHeader("Content-Type", "application/vnd.config-gtm.v1.3+json;charset=UTF-8").
		BodyString(`{
                        "resource" : {
                                "aggregationType" : "median",
                                "constrainedProperty" : null,
                                "decayRate" : null,
                                "description" : null,
                                "hostHeader" : null,
                                "leaderString" : null,
                                "leastSquaresDecay" : null,
                                "loadImbalancePercentage" : null,
                                "maxUMultiplicativeIncrement" : null,
                                "name" : "testResource",
                                "resourceInstances" : [ {
                                        "loadObject" : "",
                                        "loadObjectPort" : 0,
                                        "loadServers" : null,
                                        "datacenterId" : 3131,
                                        "useDefaultLoadObject" : false
                                } ],
                                "type" : "Download score",
                                "upperBound" : 0,
                                "links" : [ {
                                        "rel" : "self",
                                        "href" : "https://akaa-32qkzqewderdchot-d3uwbyqc4pqi2c5l.luna-dev.akamaiapis.net/config-gtm/v1/domains/gtmdomtest.akadns.net/resources/testResource"
                                } ]
                        },
                        "status" : {
                                "message" : "Change Pending",
                                "changeId" : "5bb9f131-99c8-43ff-afd2-a6ce34db8b95",
                                "propagationStatus" : "PENDING",
                                "propagationStatusDate" : "2019-06-17T17:54:37.383+00:00",
                                "passingValidation" : true,
                                "links" : [ {
                                        "rel" : "self",
                                        "href" : "https://akaa-32qkzqewderdchot-d3uwbyqc4pqi2c5l.luna-dev.akamaiapis.net/config-gtm/v1/domains/gtmdomtest.akadns.net/status/current"
                                } ]
                        }
                }`)

	Init(config)

	testResource := NewResource(GtmTestResource)
	testResource.AggregationType = "median"
	testResource.Type = "Download score"

	// Create a Resource Instance
	var instanceSlice []*ResourceInstance
	instance := testResource.NewResourceInstance(3131)
	instanceSlice = append(instanceSlice, instance)
	testResource.ResourceInstances = instanceSlice

	// do the create

	statresp, err := testResource.Create(gtmTestDomain)
	assert.NoError(t, err)

	assert.IsType(t, &Resource{}, statresp.Resource)
	assert.Equal(t, GtmTestResource, statresp.Resource.Name)

}

func TestUpdateResource(t *testing.T) {

	defer gock.Off()

	mock := gock.New("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/config-gtm/v1/domains/" + gtmTestDomain + "/resources/" + GtmTestResource)
	mock.
		Put("/config-gtm/v1/domains/"+gtmTestDomain+"/resources/"+GtmTestResource).
		HeaderPresent("Authorization").
		Reply(200).
		SetHeader("Content-Type", "application/vnd.config-gtm.v1.3+json;charset=UTF-8").
		BodyString(`{
                        "resource" : {
                                "aggregationType" : "median",
                                "constrainedProperty" : null,
                                "decayRate" : 0.5,
                                "description" : null,
                                "hostHeader" : null,
                                "leaderString" : null,
                                "leastSquaresDecay" : null,
                                "loadImbalancePercentage" : null,
                                "maxUMultiplicativeIncrement" : null,
                                "name" : "testResource",
                                "resourceInstances" : [ {
                                        "loadObject" : "",
                                        "loadObjectPort" : 0,
                                        "loadServers" : null,
                                        "datacenterId" : 3131,
                                        "useDefaultLoadObject" : false
                                } ],
                                "type" : "Download score",
                                "upperBound" : 0,
                                "links" : [ {
                                        "rel" : "self",
                                        "href" : "https://akaa-32qkzqewderdchot-d3uwbyqc4pqi2c5l.luna-dev.akamaiapis.net/config-gtm/v1/domains/gtmdomtest.akadns.net/resources/testResource"
                                } ]
                        },
                        "status" : {
                                "message" : "Change Pending",
                                "changeId" : "5bb9f131-99c8-43ff-afd2-a6ce34db8b95",
                                "propagationStatus" : "PENDING",
                                "propagationStatusDate" : "2019-06-17T17:54:37.383+00:00",
                                "passingValidation" : true,
                                "links" : [ {
                                        "rel" : "self",
                                        "href" : "https://akaa-32qkzqewderdchot-d3uwbyqc4pqi2c5l.luna-dev.akamaiapis.net/config-gtm/v1/domains/gtmdomtest.akadns.net/status/current"
                                } ]
                        }
                }`)

	Init(config)

	testResource := instantiateResource()

	newDecay := 0.5
	testResource.DecayRate = newDecay
	stat, err := testResource.Update(gtmTestDomain)
	assert.NoError(t, err)

	assert.Equal(t, stat.ChangeId, "5bb9f131-99c8-43ff-afd2-a6ce34db8b95")

}

func TestDeleteResource(t *testing.T) {

	defer gock.Off()

	mock := gock.New("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/config-gtm/v1/domains/" + gtmTestDomain + "/resources/" + GtmTestResource)
	mock.
		Delete("/config-gtm/v1/domains/"+gtmTestDomain+"/resources/"+GtmTestResource).
		HeaderPresent("Authorization").
		Reply(200).
		SetHeader("Content-Type", "application/vnd.config-gtm.v1.3+json;charset=UTF-8").
		BodyString(`{
                        "resource" : null,
                        "status" : {
                                "message" : "Change Pending",
                                "changeId" : "9a7a8f84-704b-40de-a903-bcc2728513ac",
                                "propagationStatus" : "PENDING",
                                "propagationStatusDate" : "2019-06-14T19:51:02.273+00:00",
                                "passingValidation" : true,
                                "links" : [ {
                                        "rel" : "self",
                                        "href" : "https://akaa-32qkzqewderdchot-d3uwbyqc4pqi2c5l.luna-dev.akamaiapis.net/config-gtm/v1/domains/gtmdomtest.akadns.net/status/current"
                                 } ]
                        }
               }`)

	Init(config)

	getResource := instantiateResource()
	stat, err := getResource.Delete(gtmTestDomain)
	assert.NoError(t, err)

	assert.Equal(t, "9a7a8f84-704b-40de-a903-bcc2728513ac", stat.ChangeId)

}