This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
buildpack_yml_parser_test.go
188 lines (155 loc) · 4.78 KB
/
buildpack_yml_parser_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
package staticfile_test
import (
"os"
"path/filepath"
"testing"
"github.com/paketo-community/staticfile"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testBuildpackYAMLParser(t *testing.T, when spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
buildpackYMLParser staticfile.BuildpackYMLParser
path string
)
it.Before(func() {
buildpackYMLParser = staticfile.NewBuildpackYMLParser()
})
when("Parse", func() {
when("buildpack.yml exists", func() {
it.Before(func() {
file, err := os.CreateTemp("", "buildpack.yml")
Expect(err).NotTo(HaveOccurred())
defer file.Close()
path = file.Name()
err = os.WriteFile(path, []byte(`---
staticfile:
nginx:
root: some-root-dir
host_dot_files: true
location_include: some-location
directory: true
ssi: true
pushstate: true
http_strict_transport_security: true
http_strict_transport_security_include_subdomains: true
http_strict_transport_security_preload: true
force_https: true
basic_auth: true
status_codes:
some-staus: some-code
`), os.ModePerm)
Expect(err).NotTo(HaveOccurred())
})
it("parses", func() {
configData, err := buildpackYMLParser.Parse(path)
Expect(err).NotTo(HaveOccurred())
Expect(configData.Nginx.RootDir).To(Equal("some-root-dir"))
Expect(configData.Nginx.HostDotFiles).To(BeTrue())
Expect(configData.Nginx.LocationInclude).To(Equal("some-location"))
Expect(configData.Nginx.DirectoryIndex).To(BeTrue())
Expect(configData.Nginx.SSI).To(BeTrue())
Expect(configData.Nginx.PushState).To(BeTrue())
Expect(configData.Nginx.HSTSIncludeSubDomains).To(BeTrue())
Expect(configData.Nginx.HSTSPreload).To(BeTrue())
Expect(configData.Nginx.ForceHTTPS).To(BeTrue())
Expect(configData.Nginx.BasicAuth).To(BeTrue())
Expect(configData.Nginx.StatusCodes).To(Equal(map[string]string{
"some-staus": "some-code",
}))
})
when("the root dir is not specified", func() {
it.Before(func() {
err := os.WriteFile(path, []byte(`---
staticfile:
nginx: {}
`), os.ModePerm)
Expect(err).NotTo(HaveOccurred())
})
it("sets RootDir to 'public'", func() {
configData, err := buildpackYMLParser.Parse(path)
Expect(err).NotTo(HaveOccurred())
Expect(configData.Nginx.RootDir).To(Equal("public"))
})
})
})
})
when("ValidConfig", func() {
when("the buildpack.yml indicates the user wants to generate an nginx config", func() {
it.Before(func() {
file, err := os.CreateTemp("", "buildpack.yml")
Expect(err).NotTo(HaveOccurred())
defer file.Close()
path = file.Name()
err = os.WriteFile(path, []byte(`---
staticfile:
nginx: {}
`), os.ModePerm)
Expect(err).NotTo(HaveOccurred())
})
it("returns true", func() {
valid, err := buildpackYMLParser.ValidConfig(path)
Expect(err).NotTo(HaveOccurred())
Expect(valid).To(BeTrue())
})
})
when("the buildpack.yml indicates the user does not want to generate an nginx config", func() {
it.Before(func() {
file, err := os.CreateTemp("", "buildpack.yml")
Expect(err).NotTo(HaveOccurred())
defer file.Close()
path = file.Name()
err = os.WriteFile(path, []byte(`---
staticfile:
`), os.ModePerm)
Expect(err).NotTo(HaveOccurred())
})
it("returns false", func() {
valid, err := buildpackYMLParser.ValidConfig(path)
Expect(err).NotTo(HaveOccurred())
Expect(valid).To(BeFalse())
})
})
when("buildpack.yml does not exist", func() {
it("returns false", func() {
path = filepath.Join("does-not-exist", "buildpack.yml")
valid, err := buildpackYMLParser.ValidConfig(path)
Expect(err).NotTo(HaveOccurred())
Expect(valid).To(BeFalse())
})
})
})
when("error cases", func() {
when("buildpack.yml is unable to be opened", func() {
var workingDir string
it.Before(func() {
var err error
workingDir, err = os.MkdirTemp("", "workingDir")
Expect(err).NotTo(HaveOccurred())
path = filepath.Join(workingDir, "buildpack.yml")
err = os.WriteFile(path, []byte(``), os.ModePerm)
Expect(err).NotTo(HaveOccurred())
Expect(os.Chmod(workingDir, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(workingDir, os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
_, err := buildpackYMLParser.Parse(path)
Expect(err).To(MatchError(ContainSubstring("unable to open buildpack.yml: ")))
})
})
when("buildpack.yml is malformed", func() {
it.Before(func() {
file, err := os.CreateTemp("", "buildpack.yml")
Expect(err).NotTo(HaveOccurred())
path = file.Name()
})
it("returns an error", func() {
_, err := buildpackYMLParser.Parse(path)
Expect(err).To(MatchError(ContainSubstring("unable to parse buildpack.yml: ")))
})
})
})
}