@@ -4,13 +4,15 @@ import (
4
4
"encoding/json"
5
5
"testing"
6
6
7
+ "github.com/jetstack/preflight/pkg/testutil"
8
+ "github.com/stretchr/testify/assert"
9
+ "github.com/stretchr/testify/require"
7
10
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
8
11
)
9
12
10
13
func TestSelect (t * testing.T ) {
11
- // secret objects
12
- secretResource := & unstructured.Unstructured {
13
- Object : map [string ]interface {}{
14
+ t .Run ("secret" , run_TestSelect (
15
+ map [string ]interface {}{
14
16
"apiVersion" : "v1" ,
15
17
"kind" : "Secret" ,
16
18
"metadata" : map [string ]interface {}{
@@ -19,46 +21,52 @@ func TestSelect(t *testing.T) {
19
21
"annotations" : map [string ]string {
20
22
"kubectl.kubernetes.io/last-applied-configuration" : "secret" ,
21
23
},
24
+ "labels" : map [string ]string {
25
+ "foo" : "bar" ,
26
+ },
22
27
},
23
28
"type" : "kubernetes.io/tls" ,
24
29
"data" : map [string ]interface {}{
25
30
"tls.crt" : "cert data" ,
26
31
"tls.key" : "secret" ,
27
32
},
28
33
},
29
- }
30
-
31
- secretFieldsToSelect := []string {
32
- "apiVersion" ,
33
- "kind" ,
34
- "metadata.name" ,
35
- "metadata.namespace" ,
36
- "type" ,
37
- "/data/tls.crt" ,
38
- }
34
+ SecretSelectedFields ,
35
+ map [string ]interface {}{
36
+ "apiVersion" : "v1" ,
37
+ "kind" : "Secret" ,
38
+ "metadata" : map [string ]interface {}{
39
+ "name" : "example" ,
40
+ "namespace" : "example" ,
41
+ "annotations" : map [string ]interface {}{
42
+ // The "last-applied-configuration" isn't ignored in
43
+ // "Select". "Redact" removes it.
44
+ "kubectl.kubernetes.io/last-applied-configuration" : "secret" ,
45
+ },
46
+ "labels" : map [string ]interface {}{
47
+ "foo" : "bar" ,
48
+ },
49
+ },
50
+ "type" : "kubernetes.io/tls" ,
51
+ "data" : map [string ]interface {}{
52
+ // The "tls.key" is ignored.
53
+ "tls.crt" : "cert data" ,
54
+ },
55
+ },
56
+ ))
39
57
40
- secretExpectedJSON := `{
41
- "apiVersion": "v1",
42
- "data": {
43
- "tls.crt": "cert data"
44
- },
45
- "kind": "Secret",
46
- "metadata": {
47
- "name": "example",
48
- "namespace": "example"
49
- },
50
- "type": "kubernetes.io/tls"
51
- }`
52
- // route objects
53
- routeResource := & unstructured.Unstructured {
54
- Object : map [string ]interface {}{
58
+ t .Run ("route" , run_TestSelect (
59
+ map [string ]interface {}{
55
60
"apiVersion" : "v1" ,
56
61
"kind" : "Route" ,
57
62
"metadata" : map [string ]interface {}{
58
63
"name" : "example" ,
59
64
"annotations" : map [string ]string {
60
65
"kubectl.kubernetes.io/last-applied-configuration" : "secret" ,
61
66
},
67
+ "labels" : map [string ]string {
68
+ "foo" : "bar" ,
69
+ },
62
70
},
63
71
"spec" : map [string ]interface {}{
64
72
"host" : "www.example.com" ,
@@ -74,68 +82,44 @@ func TestSelect(t *testing.T) {
74
82
"destinationCACertificate" : "destinationCaCert data" ,
75
83
},
76
84
},
85
+ }, RouteSelectedFields ,
86
+ map [string ]interface {}{
87
+ "apiVersion" : "v1" ,
88
+ "kind" : "Route" ,
89
+ "metadata" : map [string ]interface {}{
90
+ "name" : "example" ,
91
+ "annotations" : map [string ]interface {}{
92
+ // The "last-applied-configuration" isn't ignored in
93
+ // "Select". "Redact" removes it.
94
+ "kubectl.kubernetes.io/last-applied-configuration" : "secret" ,
95
+ },
96
+ },
97
+ "spec" : map [string ]interface {}{
98
+ "host" : "www.example.com" ,
99
+ "to" : map [string ]interface {}{
100
+ "kind" : "Service" ,
101
+ "name" : "frontend" ,
102
+ },
103
+ "tls" : map [string ]interface {}{
104
+ "termination" : "reencrypt" ,
105
+ // The "key" field is ignored.
106
+ "certificate" : "cert data" ,
107
+ "caCertificate" : "caCert data" ,
108
+ "destinationCACertificate" : "destinationCaCert data" ,
109
+ },
110
+ },
77
111
},
78
- }
79
-
80
- routeFieldsToSelect := []string {
81
- "apiVersion" ,
82
- "kind" ,
83
- "metadata.name" ,
84
- "spec.host" ,
85
- "spec.to.kind" ,
86
- "spec.to.name" ,
87
- "spec.tls.termination" ,
88
- "spec.tls.certificate" ,
89
- "spec.tls.caCertificate" ,
90
- "spec.tls.destinationCACertificate" ,
91
- }
92
-
93
- routeExpectedJSON := `{
94
- "apiVersion": "v1",
95
- "kind": "Route",
96
- "metadata": {
97
- "name": "example"
98
- },
99
- "spec": {
100
- "host": "www.example.com",
101
- "tls": {
102
- "caCertificate": "caCert data",
103
- "certificate": "cert data",
104
- "destinationCACertificate": "destinationCaCert data",
105
- "termination": "reencrypt"
106
- },
107
- "to": {
108
- "kind": "Service",
109
- "name": "frontend"
110
- }
111
- }
112
- }`
113
-
114
- tests := map [string ]struct {
115
- resource * unstructured.Unstructured
116
- fieldsToSelect []string
117
- expectedJSON string
118
- }{
119
- "secret" : {secretResource , secretFieldsToSelect , secretExpectedJSON },
120
- "route" : {routeResource , routeFieldsToSelect , routeExpectedJSON },
121
- }
122
-
123
- for name , test := range tests {
124
- err := Select (test .fieldsToSelect , test .resource )
125
- if err != nil {
126
- t .Fatalf ("unexpected error: %s" , err )
127
- }
112
+ ))
113
+ }
128
114
129
- bytes , err := json .MarshalIndent (test .resource , "" , " " )
130
- if err != nil {
131
- t .Fatalf ("unexpected error: %s" , err )
132
- }
115
+ func run_TestSelect (given map [string ]interface {}, givenSelect []string , expect map [string ]interface {}) func (* testing.T ) {
116
+ return func (t * testing.T ) {
117
+ t .Helper ()
118
+ givenPtr := unstructured.Unstructured {Object : given }
119
+ err := Select (givenSelect , & givenPtr )
120
+ require .NoError (t , err )
133
121
134
- t .Run (name , func (t * testing.T ) {
135
- if string (bytes ) != test .expectedJSON {
136
- t .Fatalf ("unexpected JSON: \n got \n %s\n want\n %s" , string (bytes ), test .expectedJSON )
137
- }
138
- })
122
+ assert .Equal (t , expect , givenPtr .Object )
139
123
}
140
124
}
141
125
@@ -153,21 +137,15 @@ func TestSelectMissingSelectedField(t *testing.T) {
153
137
}
154
138
155
139
err := Select (fieldsToSelect , resource )
156
- if err != nil {
157
- t .Fatalf ("unexpected error: %s" , err )
158
- }
159
-
140
+ require .NoError (t , err )
160
141
bytes , err := json .MarshalIndent (resource , "" , " " )
161
- if err != nil {
162
- t .Fatalf ("unexpected error: %s" , err )
163
- }
142
+ require .NoError (t , err )
164
143
165
- expectedJSON := `{
166
- "kind": "Secret"
167
- }`
168
- if string (bytes ) != expectedJSON {
169
- t .Fatalf ("unexpected JSON: \n got \n %s\n want\n %s" , string (bytes ), expectedJSON )
170
- }
144
+ expectedJSON := testutil .Undent (`
145
+ {
146
+ "kind": "Secret"
147
+ }` )
148
+ assert .Equal (t , expectedJSON , string (bytes ))
171
149
}
172
150
173
151
func TestRedactSecret (t * testing.T ) {
@@ -198,30 +176,25 @@ func TestRedactSecret(t *testing.T) {
198
176
}
199
177
200
178
err := Redact (fieldsToRedact , resource )
201
- if err != nil {
202
- t .Fatalf ("unexpected error: %s" , err )
203
- }
179
+ require .NoError (t , err )
204
180
205
181
bytes , err := json .MarshalIndent (resource , "" , " " )
206
- if err != nil {
207
- t .Fatalf ("unexpected error: %s" , err )
208
- }
209
- expectedJSON := `{
210
- "apiVersion": "v1",
211
- "data": {
212
- "tls.crt": "cert data"
213
- },
214
- "kind": "Secret",
215
- "metadata": {
216
- "annotations": {},
217
- "name": "example",
218
- "namespace": "example"
219
- },
220
- "type": "kubernetes.io/tls"
221
- }`
222
- if string (bytes ) != expectedJSON {
223
- t .Fatalf ("unexpected JSON: \n got \n %s\n want\n %s" , string (bytes ), expectedJSON )
224
- }
182
+ require .NoError (t , err )
183
+ expectedJSON := testutil .Undent (`
184
+ {
185
+ "apiVersion": "v1",
186
+ "data": {
187
+ "tls.crt": "cert data"
188
+ },
189
+ "kind": "Secret",
190
+ "metadata": {
191
+ "annotations": {},
192
+ "name": "example",
193
+ "namespace": "example"
194
+ },
195
+ "type": "kubernetes.io/tls"
196
+ }` )
197
+ assert .Equal (t , expectedJSON , string (bytes ))
225
198
}
226
199
227
200
func TestRedactPod (t * testing.T ) {
@@ -245,28 +218,23 @@ func TestRedactPod(t *testing.T) {
245
218
}
246
219
247
220
err := Redact (fieldsToRedact , resource )
248
- if err != nil {
249
- t .Fatalf ("unexpected error: %s" , err )
250
- }
221
+ require .NoError (t , err )
251
222
252
223
bytes , err := json .MarshalIndent (resource , "" , " " )
253
- if err != nil {
254
- t .Fatalf ("unexpected error: %s" , err )
255
- }
256
- expectedJSON := `{
257
- "apiVersion": "v1",
258
- "kind": "Pod",
259
- "metadata": {
260
- "name": "example",
261
- "namespace": "example"
262
- },
263
- "spec": {
264
- "serviceAccountName": "example"
265
- }
266
- }`
267
- if string (bytes ) != expectedJSON {
268
- t .Fatalf ("unexpected JSON: \n got \n %s\n want\n %s" , string (bytes ), expectedJSON )
269
- }
224
+ require .NoError (t , err )
225
+ expectedJSON := testutil .Undent (`
226
+ {
227
+ "apiVersion": "v1",
228
+ "kind": "Pod",
229
+ "metadata": {
230
+ "name": "example",
231
+ "namespace": "example"
232
+ },
233
+ "spec": {
234
+ "serviceAccountName": "example"
235
+ }
236
+ }` )
237
+ assert .Equal (t , expectedJSON , string (bytes ))
270
238
}
271
239
272
240
func TestRedactMissingField (t * testing.T ) {
@@ -282,19 +250,13 @@ func TestRedactMissingField(t *testing.T) {
282
250
}
283
251
284
252
err := Redact (fieldsToRedact , resource )
285
- if err != nil {
286
- t .Fatalf ("unexpected error: %s" , err )
287
- }
288
-
253
+ require .NoError (t , err )
289
254
bytes , err := json .MarshalIndent (resource , "" , " " )
290
- if err != nil {
291
- t .Fatalf ("unexpected error: %s" , err )
292
- }
255
+ require .NoError (t , err )
293
256
294
- expectedJSON := `{
295
- "kind": "Secret"
296
- }`
297
- if string (bytes ) != expectedJSON {
298
- t .Fatalf ("unexpected JSON: \n got \n %s\n want\n %s" , string (bytes ), expectedJSON )
299
- }
257
+ expectedJSON := testutil .Undent (`
258
+ {
259
+ "kind": "Secret"
260
+ }` )
261
+ assert .Equal (t , expectedJSON , string (bytes ))
300
262
}
0 commit comments