forked from go-chef/chef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookbook_artifacts_download_test.go
110 lines (97 loc) · 2.93 KB
/
cookbook_artifacts_download_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
package chef
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCBADownloadThatDoesNotExist(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/cookbook_artifacts/seven_zip/xyz", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not Found", 404)
})
err := client.CookbookArtifacts.DownloadTo("seven_zip", "xyz", "")
if assert.NotNil(t, err) {
assert.Contains(t, err.Error(), "404")
}
}
func TestCBADownloadTo(t *testing.T) {
setup()
defer teardown()
mockedCBAResponseFile := cbaData()
tempDir, err := ioutil.TempDir("", "seven_zip-cookbook")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(tempDir) // clean up
mux.HandleFunc("/cookbook_artifacts/seven_zip/0e1fed3b56aa5e84205e330d92aca22d8704a014", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, string(mockedCBAResponseFile))
})
mux.HandleFunc("/bookshelf/seven_zip/metadata_rb", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "name 'foo'")
})
mux.HandleFunc("/bookshelf/seven_zip/default_rb", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "log 'this is a resource'")
})
err = client.CookbookArtifacts.DownloadTo("seven_zip", "0e1fed3b56aa5e84205e330d92aca22d8704a014", tempDir)
assert.Nil(t, err)
var (
cookbookPath = path.Join(tempDir, "seven_zip-0e1fed3b56aa5e84205e")
metadataPath = path.Join(cookbookPath, "metadata.rb")
recipesPath = path.Join(cookbookPath, "recipes")
defaultPath = path.Join(recipesPath, "default.rb")
)
assert.DirExistsf(t, cookbookPath, "the cookbook directory should exist")
assert.DirExistsf(t, recipesPath, "the recipes directory should exist")
if assert.FileExistsf(t, metadataPath, "a metadata.rb file should exist") {
metadataBytes, err := ioutil.ReadFile(metadataPath)
assert.Nil(t, err)
assert.Equal(t, "name 'foo'", string(metadataBytes))
}
if assert.FileExistsf(t, defaultPath, "the default.rb recipes should exist") {
recipeBytes, err := ioutil.ReadFile(defaultPath)
assert.Nil(t, err)
assert.Equal(t, "log 'this is a resource'", string(recipeBytes))
}
}
func cbaData() string {
return `
{
"version": "3.1.2",
"name": "seven_zip",
"identifier": "0e1fed3b56aa5e84205e330d92aca22d8704a014",
"frozen?": false,
"chef_type": "cookbook_version",
"root_files": [
{
"name": "metadata.rb",
"path": "metadata.rb",
"checksum": "6607f3131919e82dc4ba4c026fcfee9f",
"specificity": "default",
"url": "` + server.URL + `/bookshelf/seven_zip/metadata_rb"
}
],
"attributes": [],
"definitions": [],
"files": [],
"libraries": [],
"providers": [],
"recipes": [
{
"name": "default.rb",
"path": "recipes/default.rb",
"checksum": "8e751ed8663cb9b97499956b6a20b0de",
"specificity": "default",
"url": "` + server.URL + `/bookshelf/seven_zip/default_rb"
}
],
"resources": [],
"templates": [],
"metadata": {},
"access": {}
} `
}