-
Notifications
You must be signed in to change notification settings - Fork 2
/
validate_test.go
139 lines (120 loc) · 3.39 KB
/
validate_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
package main
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDependency_Required(t *testing.T) {
d := dependency{
dependencyInner: dependencyInner{
Regions: []string{"us-east-1"},
},
}
assert.True(t, d.Required("test", "us-east-1"))
assert.False(t, d.Required("test", "us-west-2"))
d = dependency{}
assert.True(t, d.Required("test", "us-east-1"))
assert.True(t, d.Required("test", "us-west-2"))
d = dependency{
dependencyInner: dependencyInner{
Products: []string{"test"},
},
}
assert.True(t, d.Required("test", "us-east-1"))
assert.False(t, d.Required("prod", "us-east-1"))
d = dependency{
dependencyInner: dependencyInner{
Regions: []string{"us-east-1"},
Products: []string{"test"},
},
}
assert.False(t, d.Required("prod", "us-east-1"))
assert.False(t, d.Required("test", "us-west-2"))
assert.True(t, d.Required("test", "us-east-1"))
}
func TestValidate(t *testing.T) { //nolint:funlen
s := filepath.Join(t.TempDir(), "service.json")
require.NoError(t, os.WriteFile(s, []byte(`{
"dependencies": {
"env_vars": {
"required": [
"FOO",
{
"key": "BAR",
"regions": ["us-east-1"]
},
{
"key":"BAZ"
},
{
"key": "FIZZ",
"products": ["test"]
}
],
"optional": [
"QUX",
{
"key": "FOOBAR",
"regions": ["eu-central-1"]
},
{
"key":"FOOBAZ"
}
]
}
}
}`), 0o600))
l, log := testLogger()
c := &Config{ServiceDefinition: s, Region: "us-east-1"}
e := NewEnvMap()
err := validate(context.TODO(), c, e, l)
require.ErrorIs(t, err, ErrMissingEnvVars)
assert.Contains(
t,
log.String(),
`"ERROR","msg":"Missing required environment variables","env_vars":["FOO","BAR","BAZ"]`,
)
assert.Contains(t, log.String(), `"WARN","msg":"Missing optional environment variables","env_vars":["QUX","FOOBAZ"]`)
// Skips validation
c.SkipValidation = true
require.NoError(t, validate(context.TODO(), c, e, l))
c.SkipValidation = false
// Skips validation in test environment
c.Environment = "test"
require.NoError(t, validate(context.TODO(), c, e, l))
c.Environment = "dev"
// Empty env vars should be considered missing
e.Add("FOO", "")
t.Setenv("BAR", "")
log.Reset()
err = validate(context.TODO(), c, e, l)
require.ErrorIs(t, err, ErrMissingEnvVars)
assert.Contains(t, log.String(), `Missing required environment variables","env_vars":["FOO","BAR"`)
// Set all required env vars
c.Region = "eu-central-1"
e.Add("FOO", "foo")
t.Setenv("BAZ", "baz")
log.Reset()
err = validate(context.TODO(), c, e, l)
require.NoError(t, err)
assert.NotContains(t, log.String(), "Missing required environment variables")
assert.Contains(t, log.String(), "Missing optional environment variables")
// Set all optional env vars
e.Add("QUX", "qux")
e.Add("FOOBAR", "foobar")
t.Setenv("FOOBAZ", "foobaz")
log.Reset()
err = validate(context.TODO(), c, e, l)
require.NoError(t, err)
assert.NotContains(t, log.String(), "Missing required environment variables")
assert.NotContains(t, log.String(), "Missing optional environment variables")
// Missing required env vars for product
log.Reset()
c.Product = "test"
err = validate(context.TODO(), c, e, l)
require.ErrorIs(t, err, ErrMissingEnvVars)
assert.Contains(t, log.String(), `Missing required environment variables","env_vars":["FIZZ"]`)
}