-
Notifications
You must be signed in to change notification settings - Fork 1
/
options_test.go
330 lines (240 loc) · 7.53 KB
/
options_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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package sad_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
testutils "github.com/jswny/sad/internal"
"github.com/jswny/sad"
)
func TestOptionsMergeNoEmptyValues(t *testing.T) {
expectedOpts := testutils.GetTestOpts()
optsToMerge := testutils.GetTestOpts()
optsToMergeInto := sad.Options{}
err := testutils.CloneOptions(&expectedOpts, &optsToMergeInto)
if err != nil {
t.Fatalf("Error cloning expected options into options to merge into: %s", err)
}
optsToMergeInto.Merge(&optsToMerge)
testutils.CompareOpts(expectedOpts, optsToMergeInto, t)
}
func TestOptionsMergeEmptyValues(t *testing.T) {
expectedOpts := testutils.GetTestOpts()
optsToMerge := sad.Options{}
optsToMergeInto := sad.Options{}
err := testutils.CloneOptions(&expectedOpts, &optsToMergeInto)
if err != nil {
t.Fatalf("Error cloning expected options into options to merge into: %s", err)
}
optsToMergeInto.Merge(&optsToMerge)
testutils.CompareOpts(expectedOpts, optsToMergeInto, t)
}
func TestOptionsMergeSomeEmptyValues(t *testing.T) {
expectedOpts := testutils.GetTestOpts()
optsToMerge := sad.Options{}
optsToMergeInto := sad.Options{}
expectedOpts.Username = ""
expectedOpts.RootDir = ""
err := testutils.CloneOptions(&expectedOpts, &optsToMergeInto)
if err != nil {
t.Fatalf("Error cloning expected options into options to merge into: %s", err)
}
optsToMergeInto.Merge(&optsToMerge)
expectedOpts.Username = optsToMerge.Username
expectedOpts.RootDir = optsToMerge.RootDir
testutils.CompareOpts(expectedOpts, optsToMergeInto, t)
}
func TestOptionsMergeDefaults(t *testing.T) {
expectedOpts := testutils.GetTestOpts()
opts := sad.Options{}
expectedOpts.Channel = ""
expectedOpts.RootDir = ""
err := testutils.CloneOptions(&expectedOpts, &opts)
if err != nil {
t.Fatalf("Error cloning expected options into options: %s", err)
}
opts.MergeDefaults()
expectedOpts.Channel = "beta"
expectedOpts.RootDir = "/"
testutils.CompareOpts(expectedOpts, opts, t)
}
func TestOptionsVerifyValid(t *testing.T) {
opts := testutils.GetTestOpts()
err := opts.Verify()
if err != nil {
t.Errorf("Error verifying options: %s", err)
}
}
func TestOptionsVerifyInvalid(t *testing.T) {
opts := testutils.GetTestOpts()
opts.Username = ""
err := opts.Verify()
if err == nil {
t.Errorf("No error verifying options")
}
if !strings.ContainsAny(err.Error(), "username is <empty>") {
t.Errorf("Error message doesn't contain username error")
}
}
func TestOptionsFromStrings(t *testing.T) {
testOpts := testutils.GetTestOpts()
stringTestOpts := testutils.StringOptions{}
stringTestOpts.FromOptions(&testOpts)
registry := stringTestOpts.Registry
image := stringTestOpts.Image
digest := stringTestOpts.Digest
server := stringTestOpts.Server
username := stringTestOpts.Username
rootDir := stringTestOpts.RootDir
privateKey := stringTestOpts.PrivateKey
channel := stringTestOpts.Channel
envVars := stringTestOpts.EnvVars
debug := stringTestOpts.Debug
opts := sad.Options{}
err := opts.FromStrings(registry, image, digest, server, username, rootDir, privateKey, channel, envVars, debug)
if err != nil {
t.Fatalf("Error getting options from test options strings: %s", err)
}
testutils.CompareOpts(testOpts, opts, t)
}
func TestOptionsFromJSON(t *testing.T) {
testOpts := testutils.GetTestOpts()
testOptsData, err := json.Marshal(testOpts)
if err != nil {
t.Fatalf("Error marshaling test options: %s", err)
}
tempFile, err := ioutil.TempFile(".", ".sad.json.test.")
if err != nil {
t.Fatalf("Error creating temp file: %s", err)
}
defer os.Remove(tempFile.Name())
if err := ioutil.WriteFile(tempFile.Name(), testOptsData, 0644); err != nil {
t.Fatalf("Error writing to temp file: %s", err)
}
opts := sad.Options{}
if err := opts.FromJSON(tempFile.Name()); err != nil {
t.Fatalf("Error getting options from file: %s", err)
}
testutils.CompareOpts(testOpts, opts, t)
}
func TestOptionsFromJSONEmptyValues(t *testing.T) {
testOpts := sad.Options{}
testOptsData, err := json.Marshal(testOpts)
if err != nil {
t.Fatalf("Error marshaling test options: %s", err)
}
tempFile, err := ioutil.TempFile(".", ".sad.json.test.")
if err != nil {
t.Fatalf("Error creating temp file: %s", err)
}
defer os.Remove(tempFile.Name())
if err := ioutil.WriteFile(tempFile.Name(), testOptsData, 0644); err != nil {
t.Fatalf("Error writing to temp file: %s", err)
}
opts := sad.Options{}
if err := opts.FromJSON(tempFile.Name()); err != nil {
t.Fatalf("Error getting options from file: %s", err)
}
testutils.CompareOpts(testOpts, opts, t)
}
func TestOptionsFromEnv(t *testing.T) {
testOpts := testutils.GetTestOpts()
stringTestOpts := testutils.StringOptions{}
stringTestOpts.FromOptions(&testOpts)
stringTestOpts.SetEnv()
defer stringTestOpts.UnsetEnv()
opts := sad.Options{}
err := opts.FromEnv()
if err != nil {
t.Fatalf("Error getting options from environment: %s", err)
}
testutils.CompareOpts(testOpts, opts, t)
}
func TestOptionsFromEnvEmptyValues(t *testing.T) {
testOpts := sad.Options{}
opts := sad.Options{}
err := opts.FromEnv()
if err != nil {
t.Fatalf("Error getting options from environment: %s", err)
}
testutils.CompareOpts(testOpts, opts, t)
}
func TestGetDeploymentName(t *testing.T) {
opts := sad.Options{
Image: "user/foo",
Channel: "beta/123",
}
deploymentName, err := opts.GetDeploymentName()
if err != nil {
t.Fatalf("Error getting deployment name: %s", err)
}
expected := "user-foo-beta-123"
testutils.CompareStrings("full name", expected, deploymentName, t)
}
func TestGetImageSpecifierNoRegistry(t *testing.T) {
opts := sad.Options{
Image: "user/foo",
Digest: "sha256:abc123",
}
deploymentName := opts.GetImageSpecifier()
expected := "user/foo@sha256:abc123"
testutils.CompareStrings("image specifier", expected, deploymentName, t)
}
func TestGetImageSpecifierRegistry(t *testing.T) {
opts := sad.Options{
Registry: "registry.io",
Image: "user/foo",
Digest: "sha256:abc123",
}
deploymentName := opts.GetImageSpecifier()
expected := "registry.io/user/foo@sha256:abc123"
testutils.CompareStrings("image specifier", expected, deploymentName, t)
}
func TestGetDeploymentEnvValues(t *testing.T) {
opts := sad.Options{
EnvVars: []string{
"foo",
"bar",
},
}
content := "test"
prefix := sad.DeploymentEnvVarPrefix
testutils.SetEnvVarsConstant(opts.EnvVars, prefix, content)
defer testutils.UnsetEnvVars(opts.EnvVars, prefix)
envMap, err := opts.GetDeploymentEnvValues()
if err != nil {
t.Fatalf("Error getting referenced environment variables: %s", err)
}
for _, variableName := range opts.EnvVars {
variableValue := envMap[variableName]
name := fmt.Sprintf("environment variable %s value", variableName)
testutils.CompareStrings(name, content, variableValue, t)
}
}
func TestGetEnvValuesBlank(t *testing.T) {
opts := sad.Options{
EnvVars: []string{
"foo",
"bar",
},
}
content := "test"
prefix := sad.DeploymentEnvVarPrefix
testutils.SetEnvVarsConstant(opts.EnvVars, prefix, content)
toUnset := opts.EnvVars[0]
os.Unsetenv(prefix + toUnset)
defer testutils.UnsetEnvVars(opts.EnvVars, prefix)
envMap, err := opts.GetDeploymentEnvValues()
if err == nil {
t.Errorf("Expected error getting referenced environment variables but got: %s", err)
}
containsVarName := strings.Contains(err.Error(), toUnset)
if !containsVarName {
t.Errorf("Expected error getting referenced environment variable %s but got: %s", toUnset, err)
}
if envMap != nil {
t.Errorf("Expected nil returned environment variables but got: %s", envMap)
}
}