forked from Nitro/nginx-discovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
168 lines (142 loc) · 4.29 KB
/
main_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
package main
import (
"bytes"
"io/ioutil"
"os"
"testing"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/jarcoal/httpmock.v1"
)
const (
ValidResponse = `
{
"Services": {
"foo": [
{
"ID": "afc06fd44f8f",
"Name": "foo",
"Image": "gonitro/foo",
"Created": "2017-08-04T18:50:09Z",
"Hostname": "bede",
"Ports": [
{
"Type": "tcp",
"Port": 26858,
"ServicePort": 10101,
"IP": "10.10.10.10"
}
],
"Updated": "2017-08-14T15:30:23.451172154Z",
"ProxyMode": "http",
"Status": 0
},
{
"ID": "0123456789",
"Name": "foo",
"Image": "gonitro/foo",
"Created": "2017-08-04T18:50:09Z",
"Hostname": "tolkien",
"Ports": [
{
"Type": "tcp",
"Port": 42042,
"ServicePort": 10101,
"IP": "10.10.10.20"
}
],
"Updated": "2017-08-14T15:30:23.451172154Z",
"ProxyMode": "http",
"Status": 1
}
]
},
"ClusterName": "foobar"
}`
)
func Test_WriteTemplate(t *testing.T) {
Convey("WriteTemplate()", t, func() {
config := Config{
TemplateFile: "templates/nginx.conf.tmpl",
}
servers := []string{
"bocaccio:12345",
"bede:3456",
}
Convey("Can write a file with the right servers", func() {
buf := bytes.NewBuffer(make([]byte, 65535))
err := WriteTemplate(&config, servers, buf)
So(err, ShouldBeNil)
So(buf.String(), ShouldContainSubstring, "server bocaccio:12345")
So(buf.String(), ShouldContainSubstring, "server bede:3456")
})
Convey("Raises an error when the template is bad", func() {
buf := bytes.NewBuffer(make([]byte, 65535))
config.TemplateFile = "OMGTheresNoWayThisExists.OMG.OMG"
err := WriteTemplate(&config, servers, buf)
So(err, ShouldNotBeNil)
})
})
}
func Test_UpdateNginx(t *testing.T) {
Convey("UpdateNginx()", t, func() {
previousServers := []string{}
outFile, err := ioutil.TempFile("", "UpdateNginx")
So(err, ShouldBeNil)
pidFile, err := ioutil.TempFile("", "NginxPID")
So(err, ShouldBeNil)
config := Config{
TemplateFile: "templates/nginx.conf.tmpl",
SidecarAddress: "beowulf:31337",
FollowService: "foo",
FollowPort: 10101,
NginxConf: outFile.Name(),
NginxPID: pidFile.Name(),
}
servers := []string{
"10.10.10.10:26858",
}
httpmock.RegisterResponder("GET", "http://beowulf:31337/services/foo.json",
httpmock.NewStringResponder(
200, ValidResponse,
),
)
httpmock.Activate()
Reset(func() {
httpmock.Reset()
os.Remove(outFile.Name())
})
Convey("Writes a template when the servers changed", func() {
newServers, err := innerUpdate(&config, previousServers)
stat, _ := outFile.Stat()
So(err, ShouldBeNil)
So(stat.Size(), ShouldBeGreaterThan, 0)
So(newServers, ShouldResemble, servers)
})
Convey("Does not write a template when the servers are the same", func() {
newServers, err := innerUpdate(&config, servers)
stat, _ := outFile.Stat()
So(err, ShouldBeNil)
So(stat.Size(), ShouldEqual, 0)
So(newServers, ShouldResemble, servers)
})
Convey("Bubbles up errors in validation", func() {
config.ValidateCommand = "false"
_, err := innerUpdate(&config, previousServers)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "Unable to validate")
})
Convey("Bubbles up errors when restarting", func() {
config.ValidateCommand = "true"
config.UpdateCommand = "false"
_, err := innerUpdate(&config, previousServers)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "Unable to reload")
})
Convey("Does not add non-healthy servers", func() {
config.TemplateFile = ""
newServers, err := innerUpdate(&config, servers)
So(err, ShouldBeNil)
So(newServers, ShouldResemble, servers)
})
})
}