-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaterials_test.go
331 lines (264 loc) · 13.7 KB
/
materials_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
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
323
324
325
326
327
328
329
330
331
package gocd_test
import (
_ "embed"
"net/http"
"testing"
"github.com/nikhilsbhat/gocd-sdk-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
//go:embed internal/fixtures/materials.json
var materialsJSON string
//go:embed internal/fixtures/materials_usage.json
var materialUsageJSON string
func Test_client_GetMaterials(t *testing.T) {
correctArtifactHeader := map[string]string{"Accept": gocd.HeaderVersionZero}
t.Run("should be able to fetch all available materials present in GoCD successfully", func(t *testing.T) {
server := mockServer([]byte(materialsJSON), http.StatusOK,
correctArtifactHeader, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
expected := []gocd.Material{
{
CanTriggerUpdate: true,
Config: gocd.MaterialConfig{
Type: "plugin",
Fingerprint: "6bb6a100069baa6968671aa9f6c8ce4f50d9f6b14607e1820bb92b824d26e482",
Attributes: gocd.Attribute{
Ref: "b054d0aa-4704-4369-b54c-7ebdd3394210",
AutoUpdate: true,
Origin: map[string]string{
"type": "config_repo",
"id": "sample-repo",
},
},
},
Messages: []map[string]string{
{
"description": "Did not find 'scm' plugin with id 'git-path'. Looks like plugin is missing",
"level": "ERROR",
"message": "Modification check failed for material: WARNING! Plugin missing. " +
"[url=https://github.com/TWChennai/gocd-git-path-sample.git, path=integration, shallow_clone=true]\nAffected pipelines are both.",
},
},
},
{
CanTriggerUpdate: true,
Config: gocd.MaterialConfig{
Type: "git",
Fingerprint: "ae0cc647060aae14b29e252c9a912a8980a1eef592f6cbfb72a66b5467f93d8e",
Attributes: gocd.Attribute{
URL: "https://github.com/nikhilsbhat/helm-drift.git",
Branch: "main",
},
},
Messages: []map[string]string{},
},
}
actual, err := client.GetMaterials()
require.NoError(t, err)
assert.Equal(t, expected, actual)
})
t.Run("should error out while fetching all available materials present in GoCD due to wrong headers", func(t *testing.T) {
server := mockServer([]byte(materialsJSON), http.StatusOK,
map[string]string{"Accept": gocd.HeaderVersionThree}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
var expected []gocd.Material
actual, err := client.GetMaterials()
require.EqualError(t, err, "got 404 from GoCD while making GET call for "+server.URL+
"/api/internal/materials\nwith BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
assert.Equal(t, expected, actual)
})
t.Run("should error out while fetching all available materials present in GoCD due to missing headers", func(t *testing.T) {
server := mockServer([]byte(materialsJSON), http.StatusOK,
nil, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
var expected []gocd.Material
actual, err := client.GetMaterials()
require.EqualError(t, err, "got 404 from GoCD while making GET call for "+server.URL+
"/api/internal/materials\nwith BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
assert.Equal(t, expected, actual)
})
t.Run("should error out while fetching all available materials from GoCD as server returned malformed response", func(t *testing.T) {
server := mockServer([]byte("materialsJSON"), http.StatusOK, correctArtifactHeader,
false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
var expected []gocd.Material
actual, err := client.GetMaterials()
require.EqualError(t, err, "reading response body errored with: invalid character 'm' looking for beginning of value")
assert.Equal(t, expected, actual)
})
t.Run("should error out while fetching all available materials present in GoCD as server is not reachable", func(t *testing.T) {
client := gocd.NewClient("http://localhost:8156/go", auth, "info", nil)
client.SetRetryCount(1)
client.SetRetryWaitTime(1)
var expected []gocd.Material
actual, err := client.GetMaterials()
require.EqualError(t, err, "call made to get all available materials errored with: "+
"Get \"http://localhost:8156/go/api/internal/materials\": dial tcp [::1]:8156: connect: connection refused")
assert.Equal(t, expected, actual)
})
}
func Test_client_GetMaterialUsage(t *testing.T) {
correctArtifactHeader := map[string]string{"Accept": gocd.HeaderVersionZero}
t.Run("should be able to fetch usage of a material present in GoCD successfully", func(t *testing.T) {
server := mockServer([]byte(materialUsageJSON), http.StatusOK,
correctArtifactHeader, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
expected := []string{
"pipeline_1",
"pipeline_2",
"pipeline_3",
"pipeline_4",
"pipeline_5",
"pipeline_6",
"pipeline_7",
}
actual, err := client.GetMaterialUsage("2faa648612e02becba2b6809fb375f1810acec79498fe50908cebdc5ba0a0a5b")
require.NoError(t, err)
assert.Equal(t, expected, actual)
})
t.Run("should error out while fetching material usage from GoCD due to wrong headers", func(t *testing.T) {
server := mockServer([]byte(materialsJSON), http.StatusOK,
map[string]string{"Accept": gocd.HeaderVersionThree}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.GetMaterialUsage("2faa648612e02becba2b6809fb375f1810acec79498fe50908cebdc5ba0a0a5b")
require.EqualError(t, err, "got 404 from GoCD while making GET call for "+server.URL+
"/api/internal/materials/2faa648612e02becba2b6809fb375f1810acec79498fe50908cebdc5ba0a0a5b/usages\nwith "+
"BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
assert.Nil(t, actual)
})
t.Run("should error out while fetching material usage from GoCD due to missing headers", func(t *testing.T) {
server := mockServer([]byte(materialsJSON), http.StatusOK,
nil, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.GetMaterialUsage("2faa648612e02becba2b6809fb375f1810acec79498fe50908cebdc5ba0a0a5b")
require.EqualError(t, err, "got 404 from GoCD while making GET call for "+server.URL+
"/api/internal/materials/2faa648612e02becba2b6809fb375f1810acec79498fe50908cebdc5ba0a0a5b/usages\nwith "+
"BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
assert.Nil(t, actual)
})
t.Run("should error out while fetching material usage from GoCD as server returned malformed response", func(t *testing.T) {
server := mockServer([]byte("materialsJSON"), http.StatusOK, correctArtifactHeader,
false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.GetMaterialUsage("2faa648612e02becba2b6809fb375f1810acec79498fe50908cebdc5ba0a0a5b")
require.EqualError(t, err, "reading response body errored with: invalid character 'm' looking for beginning of value")
assert.Nil(t, actual)
})
t.Run("should error out while fetching material usage from GoCD as server is not reachable", func(t *testing.T) {
client := gocd.NewClient("http://localhost:8156/go", auth, "info", nil)
client.SetRetryCount(1)
client.SetRetryWaitTime(1)
actual, err := client.GetMaterialUsage("2faa648612e02becba2b6809fb375f1810acec79498fe50908cebdc5ba0a0a5b")
require.EqualError(t, err, "call made to get material usage '2faa648612e02becba2b6809fb375f1810acec79498fe50908cebdc5ba0a0a5b' errored with: "+
"Get \"http://localhost:8156/go/api/internal/materials/2faa648612e02becba2b6809fb375f1810acec79498fe50908cebdc5ba0a0a5b/usages\": "+
"dial tcp [::1]:8156: connect: connection refused")
assert.Nil(t, actual)
})
}
func Test_client_NotifyMaterial(t *testing.T) {
material := gocd.Material{
Type: "git",
RepoURL: "https://github.com/nikhilsbhat/helm-images",
}
correctArtifactHeader := map[string]string{"Accept": gocd.HeaderVersionTwo, "Content-Type": gocd.ContentJSON}
t.Run("should be able to notify the material present in GoCD successfully", func(t *testing.T) {
server := mockServer([]byte(`{"message": "The material is now scheduled for an update. Please check relevant pipeline(s) for status."}`), http.StatusAccepted,
correctArtifactHeader, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.NotifyMaterial(material)
require.NoError(t, err)
assert.Equal(t, "The material is now scheduled for an update. Please check relevant pipeline(s) for status.", actual)
})
t.Run("should error out while notifying the material present in GoCD due to wrong headers", func(t *testing.T) {
server := mockServer([]byte(materialsJSON), http.StatusOK,
map[string]string{"Accept": gocd.HeaderVersionThree}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.NotifyMaterial(material)
require.EqualError(t, err, "got 404 from GoCD while making POST call for "+server.URL+
"/api/admin/materials/git/notify\nwith "+
"BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
assert.Equal(t, "", actual)
})
t.Run("should error out while notifying the material present in GoCD due to missing headers", func(t *testing.T) {
server := mockServer([]byte(materialsJSON), http.StatusOK,
nil, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.NotifyMaterial(material)
require.EqualError(t, err, "got 404 from GoCD while making POST call for "+server.URL+
"/api/admin/materials/git/notify\nwith "+
"BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
assert.Equal(t, "", actual)
})
t.Run("should error out while notifying the material present in GoCD as server returned malformed response", func(t *testing.T) {
server := mockServer([]byte(`{"message": "The material is now scheduled for an update.Please check relevant pipeline(s) for status."`),
http.StatusAccepted, correctArtifactHeader,
false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.NotifyMaterial(material)
require.EqualError(t, err, "reading response body errored with: unexpected end of JSON input")
assert.Equal(t, "", actual)
})
t.Run("should error out while fetching notifying the material present in GoCD as server is not reachable", func(t *testing.T) {
client := gocd.NewClient("http://localhost:8156/go", auth, "info", nil)
client.SetRetryCount(1)
client.SetRetryWaitTime(1)
actual, err := client.NotifyMaterial(material)
require.EqualError(t, err, "call made to notify material 'https://github.com/nikhilsbhat/helm-images' of type git errored with: "+
"Post \"http://localhost:8156/go/api/admin/materials/git/notify\": "+
"dial tcp [::1]:8156: connect: connection refused")
assert.Equal(t, "", actual)
})
}
func Test_client_MaterialTriggerUpdate(t *testing.T) {
materialID := "5fc2198707d4e5b7dfa8cc5c6e398b9ea4bcb17d3aa54f0146ccb361cf03bbd4" //nolint:gosec
correctArtifactHeader := map[string]string{"Accept": gocd.HeaderVersionZero}
t.Run("should be able to trigger material update present in GoCD successfully", func(t *testing.T) {
server := mockServer([]byte(`{"message" : "OK"}`), http.StatusCreated,
correctArtifactHeader, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
expected := map[string]string{"message": "OK"}
actual, err := client.MaterialTriggerUpdate(materialID)
require.NoError(t, err)
assert.Equal(t, expected, actual)
})
t.Run("should error out while triggering material update present in GoCD due to wrong headers", func(t *testing.T) {
server := mockServer([]byte(materialsJSON), http.StatusOK,
map[string]string{"Accept": gocd.HeaderVersionThree}, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.MaterialTriggerUpdate(materialID)
require.EqualError(t, err, "got 404 from GoCD while making POST call for "+server.URL+
"/api/internal/materials/5fc2198707d4e5b7dfa8cc5c6e398b9ea4bcb17d3aa54f0146ccb361cf03bbd4/trigger_update\nwith "+
"BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
assert.Nil(t, actual)
})
t.Run("should error out while triggering material update present in GoCD due to missing headers", func(t *testing.T) {
server := mockServer([]byte(materialsJSON), http.StatusOK,
nil, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.MaterialTriggerUpdate(materialID)
require.EqualError(t, err, "got 404 from GoCD while making POST call for "+server.URL+
"/api/internal/materials/5fc2198707d4e5b7dfa8cc5c6e398b9ea4bcb17d3aa54f0146ccb361cf03bbd4/trigger_update\nwith "+
"BODY:<html>\n<body>\n\t<h2>404 Not found</h2>\n</body>\n\n</html>")
assert.Nil(t, actual)
})
t.Run("should error out while triggering material update present in GoCD as server returned malformed response", func(t *testing.T) {
server := mockServer([]byte(`{"message" : "OK"`),
http.StatusCreated, correctArtifactHeader,
false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)
actual, err := client.MaterialTriggerUpdate(materialID)
require.EqualError(t, err, "reading response body errored with: unexpected end of JSON input")
assert.Nil(t, actual)
})
t.Run("should error out while triggering material update material present in GoCD as server is not reachable", func(t *testing.T) {
client := gocd.NewClient("http://localhost:8156/go", auth, "info", nil)
client.SetRetryCount(1)
client.SetRetryWaitTime(1)
actual, err := client.MaterialTriggerUpdate(materialID)
require.EqualError(t, err, "call made to trigger update '5fc2198707d4e5b7dfa8cc5c6e398b9ea4bcb17d3aa54f0146ccb361cf03bbd4' errored with: "+
"Post \"http://localhost:8156/go/api/internal/materials/5fc2198707d4e5b7dfa8cc5c6e398b9ea4bcb17d3aa54f0146ccb361cf03bbd4/trigger_update\": "+
"dial tcp [::1]:8156: connect: connection refused")
assert.Nil(t, actual)
})
}