forked from paketo-buildpacks/nginx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration_test.go
137 lines (120 loc) · 4.68 KB
/
configuration_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
package nginx_test
import (
"errors"
"testing"
"github.com/initializ-buildpacks/nginx"
"github.com/initializ-buildpacks/nginx/fakes"
"github.com/paketo-buildpacks/packit/v2/servicebindings"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testConfiguration(t *testing.T, context spec.G, it spec.S) {
var Expect = NewWithT(t).Expect
context("LoadConfiguration", func() {
var bindingsResolver *fakes.BindingsResolver
it.Before(func() {
bindingsResolver = &fakes.BindingsResolver{}
bindingsResolver.ResolveOneCall.Returns.Binding = servicebindings.Binding{
Name: "first",
Type: "htpasswd",
Path: "/path/to/binding/",
Entries: map[string]*servicebindings.Entry{
".htpasswd": servicebindings.NewEntry("/path/to/binding/.htpasswd"),
},
}
})
it("loads the buildpack configuration", func() {
config, err := nginx.LoadConfiguration([]string{
"BP_NGINX_CONF_LOCATION=some-conf-location",
"BP_NGINX_VERSION=some-nginx-version",
"BP_LIVE_RELOAD_ENABLED=true",
"BP_WEB_SERVER=some-web-server",
"BP_WEB_SERVER_FORCE_HTTPS=true",
"BP_WEB_SERVER_ENABLE_PUSH_STATE=true",
"BP_WEB_SERVER_ROOT=some-root",
"BP_WEB_SERVER_LOCATION_PATH=some-location-path",
"BP_NGINX_STUB_STATUS_PORT=8083",
}, bindingsResolver, "some-platform-path")
Expect(err).NotTo(HaveOccurred())
Expect(config).To(Equal(nginx.Configuration{
NGINXConfLocation: "some-conf-location",
NGINXVersion: "some-nginx-version",
LiveReloadEnabled: true,
WebServer: "some-web-server",
WebServerForceHTTPS: true,
WebServerEnablePushState: true,
WebServerRoot: "some-root",
WebServerLocationPath: "some-location-path",
NGINXStubStatusPort: "8083",
}))
})
context("when no BP_NGINX_CONF_LOCATION is set", func() {
it("assigns a default", func() {
config, err := nginx.LoadConfiguration(nil, bindingsResolver, "some-platform-path")
Expect(err).NotTo(HaveOccurred())
Expect(config.NGINXConfLocation).To(Equal("./nginx.conf"))
})
})
context("when no BP_WEB_SERVER_ROOT is set", func() {
it("assigns a default", func() {
config, err := nginx.LoadConfiguration(nil, bindingsResolver, "some-platform-path")
Expect(err).NotTo(HaveOccurred())
Expect(config.WebServerRoot).To(Equal("./public"))
})
})
context("when BP_WEB_SERVER=nginx", func() {
context("when a .htpasswd service binding is provided", func() {
it("loads the binding path", func() {
config, err := nginx.LoadConfiguration([]string{"BP_WEB_SERVER=nginx"}, bindingsResolver, "some-platform-path")
Expect(err).NotTo(HaveOccurred())
Expect(config.BasicAuthFile).To(Equal("/path/to/binding/.htpasswd"))
})
})
context("when a .htpasswd service binding is NOT provided", func() {
it.Before(func() {
bindingsResolver.ResolveOneCall.Returns.Error = errors.New("expected exactly 1")
})
it("does not load the binding path", func() {
config, err := nginx.LoadConfiguration([]string{"BP_WEB_SERVER=nginx"}, bindingsResolver, "some-platform-path")
Expect(err).NotTo(HaveOccurred())
Expect(config.BasicAuthFile).To(Equal(""))
})
})
})
context("failure cases", func() {
context("when the environment cannot be parsed", func() {
it("returns an error", func() {
_, err := nginx.LoadConfiguration([]string{
"this is not a parseable environment variable",
}, bindingsResolver, "some-platform-path")
Expect(err).To(MatchError("failed to parse environment variables: items in environ must have format key=value"))
})
})
context("when resolving the .htpasswd service binding fails", func() {
it.Before(func() {
bindingsResolver.ResolveOneCall.Returns.Error = errors.New("some bindings error")
})
it("returns an error", func() {
_, err := nginx.LoadConfiguration([]string{"BP_WEB_SERVER=nginx"}, bindingsResolver, "some-platform-path")
Expect(err).To(MatchError(ContainSubstring("some bindings error")))
})
})
context("when the .htpasswd service binding is malformed", func() {
it.Before(func() {
bindingsResolver.ResolveOneCall.Returns.Binding = servicebindings.Binding{
Name: "first",
Type: "htpasswd",
Path: "/path/to/binding/",
Entries: map[string]*servicebindings.Entry{
"some-irrelevant-file": servicebindings.NewEntry("some-irrelevant-path"),
},
}
})
it("returns an error", func() {
_, err := nginx.LoadConfiguration([]string{"BP_WEB_SERVER=nginx"}, bindingsResolver, "some-platform-path")
Expect(err).To(MatchError("binding of type 'htpasswd' does not contain required entry '.htpasswd'"))
})
})
})
})
}