forked from titansoft-pte-ltd/imagepullsecret-patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_account_test.go
99 lines (93 loc) · 2.47 KB
/
service_account_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
package main
import (
"testing"
corev1 "k8s.io/api/core/v1"
)
var testCasesIncludeImagePullSecret = []struct {
name string
sa *corev1.ServiceAccount
secretName string
expected bool
}{
{
name: "positive one secret",
sa: &corev1.ServiceAccount{
ImagePullSecrets: []corev1.LocalObjectReference{
{Name: "secret-a"}}},
secretName: "secret-a",
expected: true,
},
{
name: "positive two secrets",
sa: &corev1.ServiceAccount{
ImagePullSecrets: []corev1.LocalObjectReference{
{Name: "secret-a"},
{Name: "secret-b"}}},
secretName: "secret-a",
expected: true,
},
{
name: "negative no secret",
sa: &corev1.ServiceAccount{
ImagePullSecrets: []corev1.LocalObjectReference{}},
secretName: "secret-a",
expected: false,
},
{
name: "negative one secret",
sa: &corev1.ServiceAccount{
ImagePullSecrets: []corev1.LocalObjectReference{
{Name: "secret-b"}}},
secretName: "secret-a",
expected: false,
},
}
func TestIncludeImagePullSecret(t *testing.T) {
for _, testCase := range testCasesIncludeImagePullSecret {
actual := includeImagePullSecret(testCase.sa, testCase.secretName)
if actual != testCase.expected {
t.Errorf("includeImagePullSecret(%s) gives %v, expects %v", testCase.name, actual, testCase.expected)
}
}
}
var testCasesGetPatchString = []struct {
name string
sa *corev1.ServiceAccount
secretName string
expected []byte
}{
{
name: "empty",
sa: &corev1.ServiceAccount{
ImagePullSecrets: []corev1.LocalObjectReference{}},
secretName: "secret-a",
expected: []byte(`{"imagePullSecrets":[{"name":"secret-a"}]}`),
},
{
name: "same",
sa: &corev1.ServiceAccount{
ImagePullSecrets: []corev1.LocalObjectReference{
{Name: "secret-a"}}},
secretName: "secret-a",
expected: []byte(`{"imagePullSecrets":[{"name":"secret-a"}]}`),
},
{
name: "different",
sa: &corev1.ServiceAccount{
ImagePullSecrets: []corev1.LocalObjectReference{
{Name: "secret-b"}}},
secretName: "secret-a",
expected: []byte(`{"imagePullSecrets":[{"name":"secret-b"},{"name":"secret-a"}]}`),
},
}
func TestGetPatchString(t *testing.T) {
for _, testCase := range testCasesGetPatchString {
actual, err := getPatchString(testCase.sa, testCase.secretName)
if err != nil {
t.Errorf("getPatchString(%s) has error %v", testCase.name, err)
}
if string(actual) != string(testCase.expected) {
t.Errorf("getPatchString(%s) gives %s, expects %s", testCase.name, actual, testCase.expected)
}
}
}