-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig_test.go
136 lines (110 loc) · 4.62 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
package phphttpd_test
import (
"bytes"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/packit/v2/scribe"
phphttpd "github.com/paketo-buildpacks/php-httpd"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testConfig(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
layerDir string
workingDir string
config phphttpd.Config
)
it.Before(func() {
var err error
layerDir, err = os.MkdirTemp("", "php-httpd-layer")
Expect(err).NotTo(HaveOccurred())
Expect(os.Chmod(layerDir, os.ModePerm)).To(Succeed())
workingDir, err = os.MkdirTemp("", "workingDir")
Expect(err).NotTo(HaveOccurred())
logEmitter := scribe.NewEmitter(bytes.NewBuffer(nil))
config = phphttpd.NewConfig(logEmitter)
})
it.After(func() {
Expect(os.RemoveAll(layerDir)).To(Succeed())
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
it("writes an httpd.conf file into the layer dir", func() {
path, err := config.Write(layerDir, workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal(filepath.Join(layerDir, "httpd.conf")))
Expect(filepath.Join(layerDir, "httpd.conf")).To(BeARegularFile())
contents, err := os.ReadFile(filepath.Join(layerDir, "httpd.conf"))
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(ContainSubstring("ServerAdmin \"admin@localhost\""))
Expect(string(contents)).To(ContainSubstring(fmt.Sprintf("DocumentRoot \"%s/htdocs\"", workingDir)))
Expect(string(contents)).To(ContainSubstring("SetHandler proxy:fcgi://127.0.0.1:9000"))
Expect(string(contents)).To(ContainSubstring("RewriteCond %{HTTPS} !=on"))
Expect(string(contents)).NotTo(ContainSubstring(fmt.Sprintf("IncludeOptional %s/.httpd.conf.d/*.conf", workingDir)))
})
context("there is a user-provided conf file", func() {
it.Before(func() {
Expect(os.MkdirAll(filepath.Join(workingDir, ".httpd.conf.d"), os.ModePerm)).To(Succeed())
Expect(os.WriteFile(filepath.Join(workingDir, ".httpd.conf.d", "user.conf"), nil, os.ModePerm)).To(Succeed())
})
it.After(func() {
Expect(os.RemoveAll(filepath.Join(workingDir, ".httpd.conf.d"))).To(Succeed())
})
it("writes an httpd.conf with the user included conf into layerDir", func() {
path, err := config.Write(layerDir, workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal(filepath.Join(layerDir, "httpd.conf")))
Expect(filepath.Join(layerDir, "httpd.conf")).To(BeARegularFile())
contents, err := os.ReadFile(filepath.Join(layerDir, "httpd.conf"))
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(ContainSubstring(fmt.Sprintf("IncludeOptional %s/.httpd.conf.d/*.conf", workingDir)))
})
})
context("all config env. vars are set", func() {
it.Before(func() {
Expect(os.Setenv("BP_PHP_SERVER_ADMIN", "some-server-admin")).To(Succeed())
Expect(os.Setenv("BP_PHP_ENABLE_HTTPS_REDIRECT", "false")).To(Succeed())
Expect(os.Setenv("BP_PHP_WEB_DIR", "some-web-dir")).To(Succeed())
})
it.After(func() {
Expect(os.Unsetenv("BP_PHP_SERVER_ADMIN")).To(Succeed())
Expect(os.Unsetenv("BP_PHP_ENABLE_HTTPS_REDIRECT")).To(Succeed())
Expect(os.Unsetenv("BP_PHP_WEB_DIR")).To(Succeed())
})
it("writes an httpd.conf that includes the env var values", func() {
path, err := config.Write(layerDir, workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal(filepath.Join(layerDir, "httpd.conf")))
contents, err := os.ReadFile(filepath.Join(layerDir, "httpd.conf"))
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(ContainSubstring("ServerAdmin \"some-server-admin\""))
Expect(string(contents)).To(ContainSubstring(fmt.Sprintf("DocumentRoot \"%s/some-web-dir\"", workingDir)))
Expect(string(contents)).NotTo(ContainSubstring("RewriteCond %{HTTPS} !=on"))
})
})
context("failure cases", func() {
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 := config.Write(layerDir, workingDir)
Expect(err).To(MatchError(ContainSubstring("failed to pase $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(layerDir, "httpd.conf"), nil, 0400)).To(Succeed())
})
it("returns an error", func() {
_, err := config.Write(layerDir, workingDir)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
})
}