forked from dtan4/k8sec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_test.go
158 lines (143 loc) · 3.85 KB
/
list_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
package cmd
import (
"bytes"
"context"
"fmt"
"testing"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestRunList(t *testing.T) {
testcases := map[string]struct {
base64encode bool
args []string
secret *v1.Secret
secrets *v1.SecretList
err error
wantOut string
wantErr error
}{
"no secret arg": {
base64encode: false,
args: []string{},
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: `NAME TYPE KEY VALUE
default-token-12345 kubernetes.io/service-account-token ca.crt "thisiscrt"
default-token-12345 kubernetes.io/service-account-token namespace "test"
default-token-12345 kubernetes.io/service-account-token token "thisistoken"
rails Opaque database-url "postgres://example.com:5432/dbname"
rails Opaque rails-env "production"
`,
wantErr: nil,
},
"no secret arg and error": {
args: []string{},
err: fmt.Errorf("cannot retrieve secret rails"),
wantErr: fmt.Errorf("Failed to retrieve secrets.: cannot retrieve secret rails"),
},
"one secret arg": {
base64encode: false,
args: []string{"rails"},
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: `NAME TYPE KEY VALUE
rails Opaque database-url "postgres://example.com:5432/dbname"
rails Opaque rails-env "production"
`,
wantErr: nil,
},
"one secret arg with --base64 option": {
base64encode: true,
args: []string{"rails"},
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: `NAME TYPE KEY VALUE
rails Opaque database-url cG9zdGdyZXM6Ly9leGFtcGxlLmNvbTo1NDMyL2RibmFtZQ==
rails Opaque rails-env cHJvZHVjdGlvbg==
`,
wantErr: nil,
},
"one secret and error": {
args: []string{"rails"},
err: fmt.Errorf("cannot retrieve secret rails"),
wantErr: fmt.Errorf("Failed to retrieve secrets.: 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 := listOpts{
base64encode: tc.base64encode,
}
err := runList(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())
}
}
})
}
}