-
-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathenumerate_test.go
More file actions
232 lines (201 loc) · 7.72 KB
/
Copy pathenumerate_test.go
File metadata and controls
232 lines (201 loc) · 7.72 KB
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
package secret
import (
"errors"
"testing"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/secrets"
)
// declaringSection returns a resolved component section declaring a single store-backed secret,
// so secrets.ExtractDeclarations reports one declaration for it.
func declaringSection(name string) map[string]any {
return map[string]any{
"secrets": map[string]any{
"vars": map[string]any{
name: map[string]any{"store": "app-secrets"},
},
},
}
}
// sopsSection returns a component section wiring a SOPS provider (resolving to file) and one
// instance-scoped SOPS-backed secret. Used to exercise the write-time collision guard.
func sopsSection(file string) map[string]any {
const (
provider = "v"
secretName = "K"
)
return map[string]any{
"secrets": map[string]any{
"providers": map[string]any{
provider: map[string]any{"kind": "sops/age", "spec": map[string]any{"file": file}},
},
"vars": map[string]any{
secretName: map[string]any{"sops": provider},
},
},
}
}
// TestCollectSecretScopeEntries proves the describe-stacks traversal keeps only secret-declaring
// instances, skips non-map nodes, and returns entries sorted by stack then component.
func TestCollectSecretScopeEntries(t *testing.T) {
stacksMap := map[string]any{
"prod": map[string]any{
cfg.ComponentsSectionName: map[string]any{
"terraform": map[string]any{
"web": declaringSection("WEB_KEY"),
"api": declaringSection("API_KEY"),
// A component with no secrets is excluded.
"empty": map[string]any{"vars": map[string]any{"x": 1}},
},
},
},
"dev": map[string]any{
cfg.ComponentsSectionName: map[string]any{
"terraform": map[string]any{
"vpc": declaringSection("VPC_KEY"),
},
},
},
// Non-map stack value is skipped without panicking.
"broken": "not-a-map",
}
entries := collectSecretScopeEntries(stacksMap, "")
require.Len(t, entries, 3)
// Sorted by stack, then component: dev/vpc, prod/api, prod/web.
assert.Equal(t, "dev", entries[0].Stack)
assert.Equal(t, "vpc", entries[0].Component)
assert.Equal(t, "prod", entries[1].Stack)
assert.Equal(t, "api", entries[1].Component)
assert.Equal(t, "prod", entries[2].Stack)
assert.Equal(t, "web", entries[2].Component)
}
// TestCollectSecretScopeEntries_ComponentFilter narrows the result to a single component.
func TestCollectSecretScopeEntries_ComponentFilter(t *testing.T) {
stacksMap := map[string]any{
"prod": map[string]any{
cfg.ComponentsSectionName: map[string]any{
"terraform": map[string]any{
"web": declaringSection("WEB_KEY"),
"api": declaringSection("API_KEY"),
},
},
},
}
entries := collectSecretScopeEntries(stacksMap, "api")
require.Len(t, entries, 1)
assert.Equal(t, "api", entries[0].Component)
}
func TestCollectSecretScopeEntries_SortsSharedNameByComponentType(t *testing.T) {
stacksMap := map[string]any{
"dev": map[string]any{
cfg.ComponentsSectionName: map[string]any{
"terraform": map[string]any{"example-service": declaringSection("SHARED_TOKEN")},
"helm": map[string]any{"example-service": declaringSection("SHARED_TOKEN")},
},
},
}
entries := collectSecretScopeEntries(stacksMap, "")
require.Len(t, entries, 2)
assert.Equal(t, []string{"helm", "terraform"}, []string{entries[0].ComponentType, entries[1].ComponentType})
}
// TestSecretEntriesInStack covers the per-stack edge cases: a missing components section, a
// non-map component-type node, and a section that is not a map.
func TestSecretEntriesInStack(t *testing.T) {
t.Run("missing components section", func(t *testing.T) {
assert.Nil(t, secretEntriesInStack("prod", map[string]any{}, ""))
})
t.Run("non-map nodes are skipped", func(t *testing.T) {
stackMap := map[string]any{
cfg.ComponentsSectionName: map[string]any{
"terraform": "not-a-map", // type node is not a map
"helmfile": map[string]any{"chart": "not-a-map"},
"packer": map[string]any{"ami": declaringSection("AMI_KEY")},
},
}
entries := secretEntriesInStack("prod", stackMap, "")
require.Len(t, entries, 1)
assert.Equal(t, "ami", entries[0].Component)
})
}
// TestDistinct proves distinct de-duplicates, drops empty values, and sorts.
func TestDistinct(t *testing.T) {
entries := []scopeEntry{
{Stack: "prod", Component: "web"},
{Stack: "dev", Component: "api"},
{Stack: "prod", Component: "api"}, // duplicate stack "prod"
{Stack: "", Component: ""}, // empty values dropped
}
stacks := distinct(entries, func(e scopeEntry) string { return e.Stack })
assert.Equal(t, []string{"dev", "prod"}, stacks)
components := distinct(entries, func(e scopeEntry) string { return e.Component })
assert.Equal(t, []string{"api", "web"}, components)
}
// TestStackCompletion exercises the shell-completion seam for --stack in both the success and
// error directions.
func TestStackCompletion(t *testing.T) {
t.Run("returns distinct stacks", func(t *testing.T) {
overrideEnumerateScopes(t, []scopeEntry{
{Stack: "prod", Component: "api"},
{Stack: "prod", Component: "web"},
{Stack: "dev", Component: "api"},
}, nil)
got, directive := stackCompletion(&cobra.Command{}, nil, "")
assert.Equal(t, []string{"dev", "prod"}, got)
assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive)
})
t.Run("enumerate error yields no completions", func(t *testing.T) {
overrideEnumerateScopes(t, nil, errors.New("boom"))
got, directive := stackCompletion(&cobra.Command{}, nil, "")
assert.Nil(t, got)
assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive)
})
}
// TestComponentCompletion exercises the shell-completion seam for --component, including that it
// reads the currently-selected stack from viper.
func TestComponentCompletion(t *testing.T) {
t.Run("returns distinct components", func(t *testing.T) {
viper.Reset()
t.Cleanup(viper.Reset)
viper.Set("stack", "prod")
overrideEnumerateScopes(t, []scopeEntry{
{Stack: "prod", Component: "web"},
{Stack: "prod", Component: "api"},
}, nil)
got, directive := componentCompletion(&cobra.Command{}, nil, "")
assert.Equal(t, []string{"api", "web"}, got)
assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive)
})
t.Run("enumerate error yields no completions", func(t *testing.T) {
overrideEnumerateScopes(t, nil, errors.New("boom"))
got, directive := componentCompletion(&cobra.Command{}, nil, "")
assert.Nil(t, got)
assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive)
})
}
// TestCheckStackSopsCollisions covers the write-time SOPS collision guard: the no-collision path,
// a real instance-level collision (two components sharing one non-discriminating file), and the
// error pass-through from enumeration.
func TestCheckStackSopsCollisions(t *testing.T) {
t.Run("distinct files pass", func(t *testing.T) {
overrideEnumerateScopes(t, []scopeEntry{
{Stack: "prod", Component: "api", Section: sopsSection("secrets/prod.api.enc.yaml")},
{Stack: "prod", Component: "web", Section: sopsSection("secrets/prod.web.enc.yaml")},
}, nil)
require.NoError(t, checkStackSopsCollisions("prod"))
})
t.Run("shared instance file collides", func(t *testing.T) {
overrideEnumerateScopes(t, []scopeEntry{
{Stack: "prod", Component: "api", Section: sopsSection("secrets/shared.enc.yaml")},
{Stack: "prod", Component: "web", Section: sopsSection("secrets/shared.enc.yaml")},
}, nil)
require.ErrorIs(t, checkStackSopsCollisions("prod"), secrets.ErrSopsCollision)
})
t.Run("enumerate error is returned", func(t *testing.T) {
sentinel := errors.New("enumerate failed")
overrideEnumerateScopes(t, nil, sentinel)
require.ErrorIs(t, checkStackSopsCollisions("prod"), sentinel)
})
}