-
Notifications
You must be signed in to change notification settings - Fork 3
/
boot_test.go
167 lines (155 loc) · 5.16 KB
/
boot_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
package main
import (
"errors"
"testing"
"time"
"github.com/arschles/assert"
"github.com/teamhephy/registry-token-refresher/pkg/credentials"
"github.com/teamhephy/registry-token-refresher/pkg/k8s"
"k8s.io/kubernetes/pkg/api"
kcl "k8s.io/kubernetes/pkg/client/unversioned"
)
func TestGetDiff(t *testing.T) {
nsList := []api.Namespace{
{ObjectMeta: api.ObjectMeta{Name: "app1"}},
{ObjectMeta: api.ObjectMeta{Name: "app2"}},
}
appList := []string{}
added, appList := getDiff(appList, nsList)
assert.Equal(t, len(added), 2, "number of namespaces added")
assert.Equal(t, added, []string{"app1", "app2"}, "namespaces")
assert.Equal(t, appList, []string{"app1", "app2"}, "namespaces")
assert.Equal(t, len(appList), 2, "number of namespaces added")
nsList = append(nsList, api.Namespace{ObjectMeta: api.ObjectMeta{Name: "app3"}})
added, appList = getDiff(appList, nsList)
assert.Equal(t, len(added), 1, "number of namespaces added")
assert.Equal(t, added, []string{"app3"}, "namespaces")
assert.Equal(t, len(appList), 3, "number of namespaces added")
added, appList = getDiff(appList, nsList)
assert.Equal(t, len(added), 0, "number of namespaces added")
assert.Equal(t, len(appList), 3, "number of namespaces added")
}
func TestTokenRefresherCredsErr(t *testing.T) {
expectedErr := errors.New("get secret error")
credProvider := &credentials.FakeDockerCredProvider{
FnGetCreds: func() (credentials.DockerConfig, error) {
return credentials.DockerConfig{}, expectedErr
},
}
kubeClient := &k8s.FakeSecretsNamespacer{}
appListCh := make(chan []api.Namespace)
tokenRefErrCh := make(chan error)
doneCh := make(chan struct{})
go tokenRefresher(kubeClient, credProvider, appListCh, tokenRefErrCh, doneCh)
timeoutCh := time.After(time.Second * 5)
select {
case err := <-tokenRefErrCh:
assert.Err(t, err, expectedErr)
case <-timeoutCh:
t.Fatalf("expected error but didn't throw error")
}
}
func TestTokenRefresherSecretErr(t *testing.T) {
expectedErr := errors.New("get secret error")
credProvider := &credentials.FakeDockerCredProvider{
FnGetCreds: func() (credentials.DockerConfig, error) {
return credentials.DockerConfig{}, nil
},
FnGetRefTime: func() time.Duration {
return time.Minute * 1
},
}
secretsClient := &k8s.FakeSecrets{
FnCreate: func(*api.Secret) (*api.Secret, error) {
return &api.Secret{}, expectedErr
},
}
kubeClient := &k8s.FakeSecretsNamespacer{
Fn: func(string) kcl.SecretsInterface {
return secretsClient
},
}
appListCh := make(chan []api.Namespace)
tokenRefErrCh := make(chan error)
doneCh := make(chan struct{})
go tokenRefresher(kubeClient, credProvider, appListCh, tokenRefErrCh, doneCh)
appListCh <- []api.Namespace{{ObjectMeta: api.ObjectMeta{Name: "testapp"}}}
timeoutCh := time.After(time.Second * 5)
select {
case err := <-tokenRefErrCh:
assert.Err(t, err, expectedErr)
case <-timeoutCh:
t.Fatalf("expected error but didn't throw error")
}
assert.Equal(t, len(secretsClient.CreateCalls), 1, "number of create secret calls")
}
func TestTokenRefresherCreateSecretSuccess(t *testing.T) {
credProvider := &credentials.FakeDockerCredProvider{
FnGetCreds: func() (credentials.DockerConfig, error) {
return credentials.DockerConfig{}, nil
},
FnGetRefTime: func() time.Duration {
return time.Minute * 1
},
}
secretsClient := &k8s.FakeSecrets{
FnCreate: func(*api.Secret) (*api.Secret, error) {
return &api.Secret{}, nil
},
}
kubeClient := &k8s.FakeSecretsNamespacer{
Fn: func(string) kcl.SecretsInterface {
return secretsClient
},
}
appListCh := make(chan []api.Namespace)
tokenRefErrCh := make(chan error)
doneCh := make(chan struct{})
go tokenRefresher(kubeClient, credProvider, appListCh, tokenRefErrCh, doneCh)
appListCh <- []api.Namespace{{ObjectMeta: api.ObjectMeta{Name: "testapp"}}}
timeoutCh := time.After(time.Second * 5)
select {
case err := <-tokenRefErrCh:
assert.NoErr(t, err)
case <-timeoutCh:
close(doneCh)
}
assert.Equal(t, len(secretsClient.CreateCalls), 1, "number of create secret calls")
}
func TestTokenRefresherUpdateSecretSuccess(t *testing.T) {
credProvider := &credentials.FakeDockerCredProvider{
FnGetCreds: func() (credentials.DockerConfig, error) {
return credentials.DockerConfig{}, nil
},
FnGetRefTime: func() time.Duration {
return time.Second * 2
},
}
secretsClient := &k8s.FakeSecrets{
FnCreate: func(*api.Secret) (*api.Secret, error) {
return &api.Secret{}, nil
},
FnUpdate: func(*api.Secret) (*api.Secret, error) {
return &api.Secret{}, nil
},
}
kubeClient := &k8s.FakeSecretsNamespacer{
Fn: func(string) kcl.SecretsInterface {
return secretsClient
},
}
appListCh := make(chan []api.Namespace)
tokenRefErrCh := make(chan error)
doneCh := make(chan struct{})
go tokenRefresher(kubeClient, credProvider, appListCh, tokenRefErrCh, doneCh)
appListCh <- []api.Namespace{{ObjectMeta: api.ObjectMeta{Name: "testapp"}}}
timeoutCh := time.After(time.Second * 5)
select {
case err := <-tokenRefErrCh:
assert.NoErr(t, err)
case <-timeoutCh:
close(doneCh)
}
assert.Equal(t, len(secretsClient.CreateCalls), 1, "number of create secret calls")
assert.Equal(t, len(secretsClient.UpdateCalls), 2, "number of update secret calls")
}