-
Notifications
You must be signed in to change notification settings - Fork 4
/
config_test.go
201 lines (163 loc) · 7.35 KB
/
config_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
package phpnginx_test
import (
"bytes"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/packit/v2/scribe"
phpnginx "github.com/paketo-buildpacks/php-nginx"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testConfig(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
workingDir string
nginxConfigWriter phpnginx.NginxConfigWriter
nginxFpmConfigWriter phpnginx.NginxFpmConfigWriter
)
it.Before(func() {
var err error
workingDir, err = os.MkdirTemp("", "workingDir")
Expect(err).NotTo(HaveOccurred())
Expect(os.MkdirAll(filepath.Join(workingDir, ".php.fpm.bp"), os.ModePerm)).To(Succeed())
logEmitter := scribe.NewEmitter(bytes.NewBuffer(nil))
nginxConfigWriter = phpnginx.NewNginxConfigWriter(logEmitter)
nginxFpmConfigWriter = phpnginx.NewFpmNginxConfigWriter(logEmitter)
})
it.After(func() {
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
context("nginx Config writer", func() {
it("writes an nginx.conf file into the working dir", func() {
path, err := nginxConfigWriter.Write(workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal(filepath.Join(workingDir, "nginx.conf")))
Expect(path).To(BeARegularFile())
info, err := os.Stat(path)
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode().String()).To(Equal("-rw-rw----"))
contents, err := os.ReadFile(path)
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(ContainSubstring(fmt.Sprintf("%s/htdocs;", workingDir)))
Expect(string(contents)).To(ContainSubstring(`listen {{env "PORT"}} default_server;`))
Expect(string(contents)).To(ContainSubstring("map $http_x_forwarded_proto $redirect_to_https"))
Expect(string(contents)).To(ContainSubstring("server unix:/tmp/php-fpm.socket;"))
Expect(string(contents)).NotTo(ContainSubstring(fmt.Sprintf("include %s/.nginx.conf.d/*-server.conf", workingDir)))
Expect(string(contents)).NotTo(ContainSubstring(fmt.Sprintf("include %s/.nginx.conf.d/*-http.conf", workingDir)))
})
context("there are user-provided conf files", func() {
it.Before(func() {
Expect(os.MkdirAll(filepath.Join(workingDir, ".nginx.conf.d"), os.ModePerm)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workingDir, ".nginx.conf.d", "some-server.conf"), nil, 0600)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workingDir, ".nginx.conf.d", "some-http.conf"), nil, 0600)).To(Succeed())
})
it.After(func() {
Expect(os.RemoveAll(filepath.Join(workingDir, ".nginx.conf.d"))).To(Succeed())
})
it("writes an nginx.conf with the user included configurations into workingDir", func() {
path, err := nginxConfigWriter.Write(workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal(filepath.Join(workingDir, "nginx.conf")))
Expect(filepath.Join(workingDir, "nginx.conf")).To(BeARegularFile())
contents, err := os.ReadFile(filepath.Join(workingDir, "nginx.conf"))
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(ContainSubstring(fmt.Sprintf("include %s/.nginx.conf.d/*-server.conf", workingDir)))
Expect(string(contents)).To(ContainSubstring(fmt.Sprintf("include %s/.nginx.conf.d/*-http.conf", workingDir)))
})
it("sets the permissions on those files to be group-writable", func() {
_, err := nginxConfigWriter.Write(workingDir)
Expect(err).NotTo(HaveOccurred())
info, err := os.Stat(filepath.Join(workingDir, ".nginx.conf.d", "some-server.conf"))
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode().String()).To(Equal("-rw-rw----"))
info, err = os.Stat(filepath.Join(workingDir, ".nginx.conf.d", "some-http.conf"))
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode().String()).To(Equal("-rw-rw----"))
})
})
context("all config env. vars are set", func() {
it.Before(func() {
Expect(os.Setenv("BP_PHP_WEB_DIR", "some-web-dir")).To(Succeed())
Expect(os.Setenv("BP_PHP_NGINX_ENABLE_HTTPS", "true")).To(Succeed())
Expect(os.Setenv("BP_PHP_ENABLE_HTTPS_REDIRECT", "false")).To(Succeed())
})
it.After(func() {
Expect(os.Unsetenv("BP_PHP_ENABLE_HTTPS_REDIRECT")).To(Succeed())
Expect(os.Unsetenv("BP_PHP_NGINX_ENABLE_HTTPS")).To(Succeed())
Expect(os.Unsetenv("BP_PHP_WEB_DIR")).To(Succeed())
})
it("writes an nginx.conf that includes the environment variable values", func() {
path, err := nginxConfigWriter.Write(workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal(filepath.Join(workingDir, "nginx.conf")))
contents, err := os.ReadFile(filepath.Join(workingDir, "nginx.conf"))
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(ContainSubstring(fmt.Sprintf("%s/some-web-dir;", workingDir)))
Expect(string(contents)).To(ContainSubstring(`listen {{env "PORT"}} ssl default_server;`))
Expect(string(contents)).NotTo(ContainSubstring("map $http_x_forwarded_proto $redirect_to_https"))
})
})
context("failure cases", func() {
context("when the BP_PHP_NGINX_ENABLE_HTTPS value cannot be parsed into a bool", func() {
it.Before(func() {
Expect(os.Setenv("BP_PHP_NGINX_ENABLE_HTTPS", "blah")).To(Succeed())
})
it.After(func() {
Expect(os.Unsetenv("BP_PHP_NGINX_ENABLE_HTTPS")).To(Succeed())
})
it("returns an error", func() {
_, err := nginxConfigWriter.Write(workingDir)
Expect(err).To(MatchError(ContainSubstring("failed to parse $BP_PHP_NGINX_ENABLE_HTTPS into boolean:")))
})
})
context("when the BP_PHP_ENABLE_HTTPS_REDIRECT value cannot be parsed into a bool", func() {
it.Before(func() {
Expect(os.Setenv("BP_PHP_ENABLE_HTTPS_REDIRECT", "blah")).To(Succeed())
})
it.After(func() {
Expect(os.Unsetenv("BP_PHP_ENABLE_HTTPS_REDIRECT")).To(Succeed())
})
it("returns an error", func() {
_, err := nginxConfigWriter.Write(workingDir)
Expect(err).To(MatchError(ContainSubstring("failed to parse $BP_PHP_ENABLE_HTTPS_REDIRECT into boolean:")))
})
})
context("when conf file can't be opened for writing", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "nginx.conf"), nil, 0400)).To(Succeed())
})
it("returns an error", func() {
_, err := nginxConfigWriter.Write(workingDir)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
})
})
context("nginx-fpm config writer", func() {
it("writes an nginx-fpm.conf file into the working dir", func() {
path, err := nginxFpmConfigWriter.Write(workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal(filepath.Join(workingDir, ".php.fpm.bp", "nginx-fpm.conf")))
Expect(filepath.Join(workingDir, ".php.fpm.bp", "nginx-fpm.conf")).To(BeARegularFile())
info, err := os.Stat(path)
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode().String()).To(Equal("-rw-r-----"))
contents, err := os.ReadFile(path)
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(ContainSubstring("listen = /tmp/php-fpm.socket"))
})
context("failure cases", func() {
context("when conf file can't be opened for writing", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, ".php.fpm.bp", "nginx-fpm.conf"), nil, 0400)).To(Succeed())
})
it("returns an error", func() {
_, err := nginxFpmConfigWriter.Write(workingDir)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
})
})
}