-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdump_test.go
289 lines (259 loc) · 6.18 KB
/
dump_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
package cmd
import (
"bytes"
"context"
"fmt"
"io"
"os"
"path/filepath"
"testing"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestRunDump(t *testing.T) {
testcases := map[string]struct {
args []string
filename string
noquotes bool
secret *v1.Secret
secrets *v1.SecretList
err error
wantOut string
wantErr error
}{
"no secret arg": {
args: []string{},
filename: "",
noquotes: false,
secrets: &v1.SecretList{
Items: []v1.Secret{
{
ObjectMeta: metav1.ObjectMeta{
Name: "default-token-12345",
},
Data: map[string][]byte{
"ca.crt": []byte("thisiscrt"),
"namespace": []byte("test"),
"token": []byte("thisistoken"),
},
Type: v1.SecretTypeServiceAccountToken,
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "rails",
},
Data: map[string][]byte{
"rails-env": []byte("production"),
"database-url": []byte("postgres://example.com:5432/dbname"),
},
Type: v1.SecretTypeOpaque,
},
},
},
err: nil,
wantOut: `ca.crt="thisiscrt"
database-url="postgres://example.com:5432/dbname"
namespace="test"
rails-env="production"
token="thisistoken"
`,
wantErr: nil,
},
"no secret arg and error": {
args: []string{},
filename: "",
noquotes: false,
err: fmt.Errorf("cannot retrieve secret rails"),
wantErr: fmt.Errorf("Failed to list secret.: cannot retrieve secret rails"),
},
"one secret arg": {
args: []string{"rails"},
filename: "",
noquotes: false,
secret: &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "rails",
},
Data: map[string][]byte{
"rails-env": []byte("production"),
"database-url": []byte("postgres://example.com:5432/dbname"),
},
Type: v1.SecretTypeOpaque,
},
err: nil,
wantOut: `database-url="postgres://example.com:5432/dbname"
rails-env="production"
`,
wantErr: nil,
},
"dump with no quotes": {
args: []string{},
filename: "",
noquotes: true,
secrets: &v1.SecretList{
Items: []v1.Secret{
{
ObjectMeta: metav1.ObjectMeta{
Name: "default-token-12345",
},
Data: map[string][]byte{
"ca.crt": []byte("thisiscrt"),
"namespace": []byte("test"),
"token": []byte("thisistoken"),
},
Type: v1.SecretTypeServiceAccountToken,
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "rails",
},
Data: map[string][]byte{
"rails-env": []byte("production"),
"database-url": []byte("postgres://example.com:5432/dbname"),
},
Type: v1.SecretTypeOpaque,
},
},
},
err: nil,
wantOut: `ca.crt=thisiscrt
database-url=postgres://example.com:5432/dbname
namespace=test
rails-env=production
token=thisistoken
`,
wantErr: nil,
},
"one secret and error": {
args: []string{"rails"},
filename: "",
noquotes: false,
err: fmt.Errorf("cannot retrieve secret rails"),
wantErr: fmt.Errorf("Failed to get secret. name=rails: cannot retrieve secret rails"),
},
}
namespace := "test"
for name, tc := range testcases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
k8sclient := &fakeClient{
getSecretResponse: tc.secret,
listSecretsResponse: tc.secrets,
err: tc.err,
}
var out bytes.Buffer
opts := dumpOpts{
filename: tc.filename,
noquotes: tc.noquotes,
}
err := runDump(context.Background(), k8sclient, namespace, tc.args, &out, &opts)
if tc.wantErr != nil {
if err == nil {
t.Fatalf("want error %q, got no error", tc.wantErr.Error())
}
if err.Error() != tc.wantErr.Error() {
t.Fatalf("want error %q, got %q", tc.wantErr.Error(), err.Error())
}
} else {
if err != nil {
t.Fatalf("want no error, got %q", err.Error())
}
if out.String() != tc.wantOut {
t.Logf("want:\n%s", tc.wantOut)
t.Logf("got:\n%s", out.String())
t.Fatalf("want %q, got %q", tc.wantOut, out.String())
}
}
})
}
}
func TestRunDump_dumpToFile(t *testing.T) {
testcases := map[string]struct {
args []string
filename string
noquotes bool
secret *v1.Secret
secrets *v1.SecretList
wantOut string
wantBody string
}{
"dump to file": {
args: []string{},
filename: ".env",
noquotes: false,
secrets: &v1.SecretList{
Items: []v1.Secret{
{
ObjectMeta: metav1.ObjectMeta{
Name: "default-token-12345",
},
Data: map[string][]byte{
"ca.crt": []byte("thisiscrt"),
"namespace": []byte("test"),
"token": []byte("thisistoken"),
},
Type: v1.SecretTypeServiceAccountToken,
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "rails",
},
Data: map[string][]byte{
"rails-env": []byte("production"),
"database-url": []byte("postgres://example.com:5432/dbname"),
},
Type: v1.SecretTypeOpaque,
},
},
},
wantOut: "",
wantBody: `ca.crt="thisiscrt"
database-url="postgres://example.com:5432/dbname"
namespace="test"
rails-env="production"
token="thisistoken"
`,
},
}
namespace := "test"
for name, tc := range testcases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
k8sclient := &fakeClient{
getSecretResponse: tc.secret,
listSecretsResponse: tc.secrets,
}
var out bytes.Buffer
filename := filepath.Join(t.TempDir(), tc.filename)
opts := dumpOpts{
filename: filename,
noquotes: tc.noquotes,
}
err := runDump(context.Background(), k8sclient, namespace, tc.args, &out, &opts)
if err != nil {
t.Fatalf("want no error, got %q", err.Error())
}
if out.String() != tc.wantOut {
t.Logf("want:\n%s", tc.wantOut)
t.Logf("got:\n%s", out.String())
t.Fatalf("want %q, got %q", tc.wantOut, out.String())
}
if _, err := os.Stat(filename); err != nil {
t.Fatalf("want file %q but not found", filename)
}
f, err := os.Open(filename)
if err != nil {
t.Fatal(err)
}
b, err := io.ReadAll(f)
if err != nil {
t.Fatal(err)
}
if string(b) != tc.wantBody {
t.Fatalf("want %q, got %q", tc.wantBody, string(b))
}
})
}
}