-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlocal_cloudenv_test.go
241 lines (223 loc) · 6.08 KB
/
local_cloudenv_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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package cloudenv_test
import (
. "github.com/cloudfoundry-community/gautocloud/cloudenv"
"bytes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"os"
"path"
)
var yamlServices = []byte(`
app_name: "myapp"
services:
- name: myelephantsql
tags: [postgresql, service]
credentials:
uri: postgres://seilbmbd:[email protected]:5432/seilbmbd
- name: mysendgrid
tags: [smtp, service]
credentials:
hostname: smtp.sendgrid.net
username: QvsXMbJ3rK
password: HCHMOYluTv
`)
var jsonServices = []byte(`
{
"app_name": "myapp",
"services": [
{
"name": "myelephantsql",
"tags": ["postgresql", "service"],
"credentials":
{
"uri": "postgres://seilbmbd:[email protected]:5432/seilbmbd"
}
},
{
"name": "mysendgrid",
"tags": ["smtp", "service"],
"credentials":
{
"hostname": "smtp.sendgrid.net",
"username": "QvsXMbJ3rK",
"password": "HCHMOYluTv"
}
}
]
}
`)
var tomlServices = []byte(`
app_name = "myapp"
[[services]]
name = "myelephantsql"
tags = [ "postgresql", "service" ]
[services.credentials]
uri = "postgres://seilbmbd:[email protected]:5432/seilbmbd"
[[services]]
name = "mysendgrid"
tags = [ "smtp", "service" ]
[services.credentials]
hostname = "smtp.sendgrid.net"
username = "QvsXMbJ3rK"
password = "HCHMOYluTv"
`)
var hclServices = []byte(`
services {
name = "myelephantsql"
tags = ["postgresql", "service"]
credentials {
uri = "postgres://seilbmbd:[email protected]:5432/seilbmbd"
}
}
services {
name = "mysendgrid"
tags = ["smtp", "service"]
credentials {
hostname = "smtp.sendgrid.net"
username = "QvsXMbJ3rK"
password = "HCHMOYluTv"
}
}
`)
type FormatService struct {
Type string
Content []byte
}
func defaultTest(cloudEnv CloudEnv) {
Context("GetServicesFromTags", func() {
It("should give correct service(s)", func() {
services := cloudEnv.GetServicesFromTags([]string{"service"})
Expect(services).Should(HaveLen(2))
})
It("should give correct service(s) when tag have regex", func() {
services := cloudEnv.GetServicesFromTags([]string{"postgres.*"})
Expect(services).Should(HaveLen(1))
})
It("should give correct service(s) when have mulitple tag", func() {
services := cloudEnv.GetServicesFromTags([]string{"postgresql", "smtp"})
Expect(services).Should(HaveLen(2))
})
})
Context("GetServicesFromName", func() {
It("should give correct service(s)", func() {
services := cloudEnv.GetServicesFromName(".*my.*")
Expect(services).Should(HaveLen(2))
services = cloudEnv.GetServicesFromName(".*sql.*")
Expect(services).Should(HaveLen(1))
})
})
Context("GetAppInfo", func() {
It("should return informations about instance of the running application", func() {
appInfo := cloudEnv.GetAppInfo()
Expect(appInfo.Id).ShouldNot(BeEmpty())
Expect(appInfo.Name).Should(Or(Equal("myapp"), Equal("<unknown>")))
Expect(appInfo.Properties).Should(HaveLen(0))
})
})
}
var _ = Describe("LocalCloudenv", func() {
AfterEach(func() {
os.Unsetenv(LOCAL_ENV_KEY)
os.Unsetenv(LOCAL_CONFIG_ENV_KEY)
})
formatServices := []FormatService{
{
Type: "yaml",
Content: yamlServices,
},
{
Type: "json",
Content: jsonServices,
},
{
Type: "toml",
Content: tomlServices,
},
{
Type: "hcl",
Content: hclServices,
},
}
for _, formatService := range formatServices {
Describe("When config file is a "+formatService.Type+" file", func() {
var cloudEnv CloudEnv
cloudEnv = NewLocalCloudEnvFromReader(bytes.NewBuffer(formatService.Content), formatService.Type)
defaultTest(cloudEnv)
})
}
Describe("When cloud file path is provided", func() {
dir, err := os.Getwd()
if err != nil {
Fail(err.Error())
}
fixturePath := path.Join(dir, "test-fixtures")
configPath := path.Join(fixturePath, "services.json")
err = os.Setenv(LOCAL_ENV_KEY, configPath)
if err != nil {
Fail(err.Error())
}
cloudEnv := NewLocalCloudEnv()
err = cloudEnv.Load()
if err != nil {
Fail(err.Error())
}
defaultTest(cloudEnv)
})
Describe("When config file path is provided", func() {
dir, err := os.Getwd()
if err != nil {
Fail(err.Error())
}
fixturePath := path.Join(dir, "test-fixtures")
configPath := path.Join(fixturePath, "config.yaml")
err = os.Setenv(LOCAL_CONFIG_ENV_KEY, configPath)
if err != nil {
Fail(err.Error())
}
cloudEnv := NewLocalCloudEnv()
err = cloudEnv.Load()
if err != nil {
Fail(err.Error())
}
expectService := func(service Service) {
creds := service.Credentials
Expect(creds).Should(HaveKey("foo"))
Expect(creds["foo"]).Should(Equal("bar"))
Expect(creds).Should(HaveKey("config"))
config := creds["config"].(map[string]interface{})
Expect(config).Should(HaveKey("key"))
Expect(config["key"]).Should(Equal("value"))
Expect(config).Should(HaveKey("slice"))
slice := config["slice"].([]interface{})
Expect(slice[0]).Should(Equal("value1"))
Expect(slice[1]).Should(Equal("value2"))
}
Context("GetServicesFromTags", func() {
It("should give correct config service", func() {
services := cloudEnv.GetServicesFromTags([]string{"config"})
Expect(services).Should(HaveLen(1))
expectService(services[0])
})
It("should give correct config service when tag have regex", func() {
services := cloudEnv.GetServicesFromTags([]string{"conf.*"})
Expect(services).Should(HaveLen(1))
expectService(services[0])
})
})
Context("GetServicesFromName", func() {
It("should give correct config service", func() {
services := cloudEnv.GetServicesFromName(".*conf.*")
Expect(services).Should(HaveLen(1))
})
})
Context("GetAppInfo", func() {
It("should return informations about instance of the running application", func() {
appInfo := cloudEnv.GetAppInfo()
Expect(appInfo.Id).ShouldNot(BeEmpty())
Expect(appInfo.Port).Should(Equal(0))
Expect(appInfo.Name).Should(Or(Equal("myapp"), Equal("<unknown>")))
Expect(appInfo.Properties).Should(HaveLen(0))
})
})
})
})