-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathcookbook_test.go
328 lines (284 loc) · 10.3 KB
/
cookbook_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
package chef
import (
"fmt"
"net/http"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
const cookbookListResponseFile = "test/cookbooks_response.json"
const cookbookResponseFile = "test/cookbook.json"
const _IssueUrl = "https://github.com/<insert_org_here>/apache/issues"
const _Name = "apache"
const _Maintainer = "The Authors"
const _MaintainerEmail = "[email protected]"
const _SourceUrl = "https://github.com/<insert_org_here>/apache"
const _License = "All Rights Reserved"
const _Version = "0.1.0"
const _ChefVersion = ">= 15.0"
const _Description = "Installs/Configures apache"
var _Gems = [][]string{[]string{"foobar"}, []string{"aws-sdk-ec2", "~> 1.214.0"}}
func TestGetVersion(t *testing.T) {
setup()
defer teardown()
cbookResp, err := os.ReadFile(cookbookResponseFile)
if err != nil {
t.Error(err)
}
mux.HandleFunc("/cookbooks/foo/_latest", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, string(cbookResp))
})
cookbook, err := client.Cookbooks.GetVersion("foo", "_latest")
assert.Nil(t, err)
if assert.NotNil(t, cookbook) {
assert.Equal(t, "foo", cookbook.CookbookName)
assert.Equal(t, "0.3.0", cookbook.Version)
assert.Equal(t, "foo-0.3.0", cookbook.Name)
assert.Equal(t, "cookbook_version", cookbook.ChefType)
assert.Equal(t, false, cookbook.Frozen)
assert.Equal(t, "Chef::CookbookVersion", cookbook.JsonClass)
assert.Equal(t, 0, len(cookbook.Files))
assert.Equal(t, 0, len(cookbook.Templates))
assert.Equal(t, 0, len(cookbook.Attributes))
assert.Equal(t, 0, len(cookbook.Definitions))
assert.Equal(t, 0, len(cookbook.Libraries))
assert.Equal(t, 0, len(cookbook.Providers))
assert.Equal(t, 0, len(cookbook.Resources))
// Assert Recipes (verify only one field)
assert.Equal(t, 1, len(cookbook.Recipes))
assert.Equal(t, "default.rb", cookbook.Recipes[0].Name)
assert.Equal(t, "recipes/default.rb", cookbook.Recipes[0].Path)
assert.Equal(t, "4e855dcab35b481ee56518db164b501d", cookbook.Recipes[0].Checksum)
assert.Equal(t, "default", cookbook.Recipes[0].Specificity)
// Check partial string just for convenience
assert.Contains(t, cookbook.Recipes[0].Url, "https://localhost:443/bookshelf/organization-")
// Assert RootFiles
assert.Equal(t, 8, len(cookbook.RootFiles))
// Assert CookbookMeta struct
assert.Equal(t, "foo", cookbook.Metadata.Name)
assert.Equal(t, "0.3.0", cookbook.Metadata.Version)
assert.Equal(t, "The Authors", cookbook.Metadata.Maintainer)
assert.Equal(t, "[email protected]", cookbook.Metadata.MaintainerEmail)
assert.Equal(t, "Installs/Configures foo", cookbook.Metadata.Description)
assert.Equal(t, "All Rights Reserved", cookbook.Metadata.License)
// Assert CookbookAccess struct
assert.Equal(t, true, cookbook.Access.Read)
assert.Equal(t, true, cookbook.Access.Create)
assert.Equal(t, true, cookbook.Access.Grant)
assert.Equal(t, true, cookbook.Access.Update)
assert.Equal(t, true, cookbook.Access.Delete)
}
}
func TestCookbookList(t *testing.T) {
setup()
defer teardown()
file, err := os.ReadFile(cookbookListResponseFile)
if err != nil {
t.Error(err)
}
mux.HandleFunc("/cookbooks", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, string(file))
})
data, err := client.Cookbooks.List()
if err != nil {
t.Error(err)
}
if data == nil {
t.Fatal("WTF we should have some data")
}
fmt.Println(data)
_, err = client.Cookbooks.ListAvailableVersions("3")
if err != nil {
t.Error(err)
}
_, err = client.Cookbooks.ListAvailableVersions("0")
if err != nil {
t.Error(err)
}
}
func TestCookbookListAvailableVersions_0(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/cookbooks", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "BAD FUCKING REQUEST", 503)
})
_, err := client.Cookbooks.ListAvailableVersions("2")
if err == nil {
t.Error("We expected this bad request to error", err)
}
}
func TestCookBookDelete(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/cookbooks/good/1.1.1", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "")
})
mux.HandleFunc("/cookbooks/bad/1.1.1", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not Found", 404)
})
err := client.Cookbooks.Delete("bad", "1.1.1")
if err == nil {
t.Error("We expected this bad request to error", err)
}
err = client.Cookbooks.Delete("good", "1.1.1")
if err != nil {
t.Error(err)
}
}
func TestCookBookGet(t *testing.T) {
setup()
defer teardown()
cookbookVerionJSON := `{"url": "http://localhost:4000/cookbooks/apache2/5.1.0", "version": "5.1.0"}`
mux.HandleFunc("/cookbooks/good", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, cookbookVerionJSON)
})
mux.HandleFunc("/cookbooks/bad", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not Found", 404)
})
data, err := client.Cookbooks.Get("good")
if err != nil {
t.Error(err)
}
if data.Version != "5.1.0" {
t.Errorf("We expected '5.1.0' and got '%s'\n", data.Version)
}
_, err = client.Cookbooks.Get("bad")
if err == nil {
t.Error("We expected this bad request to error", err)
}
}
func TestCookBookGetAvailableVersions(t *testing.T) {
setup()
defer teardown()
cookbookVerionsJSON := `
{ "apache2": {
"url": "http://localhost:4000/cookbooks/apache2",
"versions": [
{"url": "http://localhost:4000/cookbooks/apache2/5.1.0",
"version": "5.1.0"},
{"url": "http://localhost:4000/cookbooks/apache2/4.2.0",
"version": "4.2.0"}
]
}}`
mux.HandleFunc("/cookbooks/good", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, cookbookVerionsJSON)
})
mux.HandleFunc("/cookbooks/bad", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not Found", 404)
})
data, err := client.Cookbooks.GetAvailableVersions("good", "3")
if err != nil {
t.Error(err)
}
fmt.Println(data)
}
func TestCookBookListAllRecipes(t *testing.T) {
setup()
defer teardown()
cookbookRecipesJSON := `
[
"apache2",
"apache2::mod_access_compat",
"apache2::mod_actions",
"apache2::mod_alias"
]`
mux.HandleFunc("/cookbooks/_recipes", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, cookbookRecipesJSON)
})
data, err := client.Cookbooks.ListAllRecipes()
if err != nil {
t.Error(err)
}
fmt.Println(data)
}
func TestNewCookbookMeta(t *testing.T) {
data := ` name 'apache'
maintainer 'The Authors'
maintainer_email '[email protected]'
license 'All Rights Reserved'
description 'Installs/Configures apache'
version '0.1.0'
chef_version '>= 15.0'
#issues_url points to the location where issues for this cookbook are
# tracked. A View Issues link will be displayed on this cookbook's page when
# uploaded to a Supermarket.
#
issues_url 'https://github.com/<insert_org_here>/apache/issues'
source_url 'https://github.com/<insert_org_here>/apache'`
md, err := NewMetaData(data)
if err != nil {
t.Error("invalid metadata.rb contain please validate it", err)
}
validateCookbookMetaData(md, t, "TestNewMetaData")
}
func TestNewCookbookMetaFromJson(t *testing.T) {
data := `{"name":"apache","description":"Installs/Configures apache","long_description":"","maintainer":"The Authors","maintainer_email":"[email protected]","license":"All Rights Reserved","platforms":{},"dependencies":{},"providing":null,"recipes":null,"version":"0.1.0","source_url":"https://github.com/\u003cinsert_org_here\u003e/apache","issues_url":"https://github.com/\u003cinsert_org_here\u003e/apache/issues","ChefVersion":"\u003e= 15.0","OhaiVersion":"","gems":null,"eager_load_libraries":false,"privacy":false}`
md, err := NewMetaDataFromJson([]byte(data))
if err != nil {
t.Error("invalid metadata.rb contain please validate it", err)
}
validateCookbookMetaData(md, t, "TestNewMetaDataFromJson")
}
func TestReadCookbookMeta(t *testing.T) {
file, err := os.Create("/tmp/metadata.rb")
if err != nil {
t.Error("unable to create to metadata.rb", err)
}
defer file.Close()
data := ` name 'apache'
maintainer 'The Authors'
maintainer_email '[email protected]'
license 'All Rights Reserved'
description 'Installs/Configures apache'
version '0.1.0'
chef_version '>= 15.0'
#issues_url points to the location where issues for this cookbook are
# tracked. A View Issues link will be displayed on this cookbook's page when
# uploaded to a Supermarket.
#
issues_url 'https://github.com/<insert_org_here>/apache/issues'
source_url 'https://github.com/<insert_org_here>/apache'`
_, err = file.WriteString(data)
if err != nil {
t.Error("error in creating tmp file for metadata.rb", err)
}
md, err := ReadMetaData("/tmp")
if err != nil {
t.Error("error in reading tmp file for metadata.rb", err)
}
validateCookbookMetaData(md, t, "TestReadMetaData")
os.Remove("/tmp/metadata.rb")
}
func TestReadCookbookMeta2(t *testing.T) {
file, err := os.Create("/tmp/metadata.json")
if err != nil {
t.Error("unable to create to metadata.rb", err)
}
defer file.Close()
data := `{"name":"apache","description":"Installs/Configures apache","long_description":"","maintainer":"The Authors","maintainer_email":"[email protected]","license":"All Rights Reserved","platforms":{},"dependencies":{},"providing":null,"recipes":null,"version":"0.1.0","source_url":"https://github.com/\u003cinsert_org_here\u003e/apache","issues_url":"https://github.com/\u003cinsert_org_here\u003e/apache/issues","ChefVersion":"\u003e= 15.0","OhaiVersion":"","gems":[["foobar"], ["aws-sdk-ec2", "~> 1.214.0"]],"eager_load_libraries":false,"privacy":false}`
_, err = file.WriteString(data)
if err != nil {
t.Error("error in creating tmp file for metadata.json", err)
}
md, err := ReadMetaData("/tmp")
if err != nil {
t.Error("error in reading tmp file for metadata.json", err)
}
validateCookbookMetaData(md, t, "TestReadMetaData")
validateCookbookGem(md, t, "TestReadMetaData")
os.Remove("/tmp/metadata.json")
}
func validateCookbookMetaData(md CookbookMeta, t *testing.T, funcName string) {
assert.Equal(t, _Description, md.Description)
assert.Equal(t, _IssueUrl, md.IssueUrl)
assert.Equal(t, _Name, md.Name)
assert.Equal(t, _Maintainer, md.Maintainer)
assert.Equal(t, _MaintainerEmail, md.MaintainerEmail)
assert.Equal(t, _SourceUrl, md.SourceUrl)
assert.Equal(t, _License, md.License)
assert.Equal(t, _Version, md.Version)
assert.Equal(t, _ChefVersion, md.ChefVersion)
}
func validateCookbookGem(md CookbookMeta, t *testing.T, funcName string) {
assert.Equal(t, _Gems, md.Gems)
}