-
Notifications
You must be signed in to change notification settings - Fork 59
/
sshd_controller_test.go
354 lines (312 loc) · 11.1 KB
/
sshd_controller_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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package sshd
import (
"context"
"errors"
"reflect"
"testing"
cloudingressv1alpha1 "github.com/openshift/cloud-ingress-operator/pkg/apis/cloudingress/v1alpha1"
mockcc "github.com/openshift/cloud-ingress-operator/pkg/cloudclient/mock_cloudclient"
"github.com/golang/mock/gomock"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
const (
placeholderName string = "placeholderName"
placeholderNamespace string = "placeholderNamespace"
placeholderImage string = "placeholderImage"
)
// tests
func TestSetSSHDStatus(t *testing.T) {
tests := []struct {
Name string
Message string
State cloudingressv1alpha1.SSHDStateType
ExpectError bool
}{
{
Name: "set status",
Message: "working as expected",
State: cloudingressv1alpha1.SSHDStateReady,
},
}
for _, test := range tests {
testClient, testScheme := setUpTestClient(t)
r := &ReconcileSSHD{client: testClient, scheme: testScheme}
r.SetSSHDStatus(cr, test.Message, test.State)
if cr.Status.Message != test.Message {
t.Errorf("test: %s; status was %s, expected %s\n", test.Name, cr.Status.Message, test.Message)
}
if cr.Status.State != test.State {
t.Errorf("test: %s; state was %s, expected %s\n", test.Name, cr.Status.State, test.State)
}
}
}
func TestSetSSHDStatusPending(t *testing.T) {
tests := []struct {
Name string
Message string
ExpectError bool
}{
{
Name: "set status",
Message: "working as expected",
},
}
for _, test := range tests {
testClient, testScheme := setUpTestClient(t)
r := &ReconcileSSHD{client: testClient, scheme: testScheme}
r.SetSSHDStatusPending(cr, test.Message)
if cr.Status.Message != test.Message {
t.Errorf("test: %s; status was %s, expected %s\n", test.Name, cr.Status.Message, test.Message)
}
if cr.Status.State != cloudingressv1alpha1.SSHDStatePending {
t.Errorf("test: %s; state was %s, expected %s\n", test.Name, cr.Status.State, cloudingressv1alpha1.SSHDStatePending)
}
}
}
func TestSetSSHDStatusError(t *testing.T) {
tests := []struct {
Name string
Message string
ExpectError bool
}{
{
Name: "set status",
Message: "working as expected",
},
}
for _, test := range tests {
testClient, testScheme := setUpTestClient(t)
r := &ReconcileSSHD{client: testClient, scheme: testScheme}
r.SetSSHDStatusError(cr, test.Message, errors.New("fake error"))
if cr.Status.Message != test.Message {
t.Errorf("test: %s; status was %s, expected %s\n", test.Name, cr.Status.Message, test.Message)
}
if cr.Status.State != cloudingressv1alpha1.SSHDStateError {
t.Errorf("test: %s; state was %s, expected %s\n", test.Name, cr.Status.State, cloudingressv1alpha1.SSHDStateError)
}
}
}
func TestNewSSHDeployment(t *testing.T) {
var configMapList *corev1.ConfigMapList
var deployment *appsv1.Deployment
const hostKeysName string = "host-keys"
hostKeysSecret, err := newSSHDSecret(placeholderNamespace, hostKeysName)
if err != nil {
t.Fatal("Failed to generate host keys:", err)
}
// Verify SSHD parameters are honored
configMapList = newConfigMapList()
deployment = newSSHDDeployment(cr, configMapList, hostKeysSecret)
if deployment.ObjectMeta.Name != cr.ObjectMeta.Name {
t.Errorf("Deployment has wrong name %q, expected %q",
deployment.ObjectMeta.Name, cr.ObjectMeta.Name)
}
if deployment.ObjectMeta.Namespace != cr.ObjectMeta.Namespace {
t.Errorf("Deployment has wrong namespace %q, expected %q",
deployment.ObjectMeta.Namespace, cr.ObjectMeta.Namespace)
}
if !reflect.DeepEqual(deployment.Spec.Selector.MatchLabels, getMatchLabels(cr)) {
t.Errorf("Deployment has wrong selector %v, expected %v",
deployment.Spec.Selector.MatchLabels, getMatchLabels(cr))
}
if deployment.Spec.Template.ObjectMeta.Name != cr.ObjectMeta.Name {
t.Errorf("Deployment has wrong pod spec name %q, expected %q",
deployment.Spec.Template.ObjectMeta.Name, cr.ObjectMeta.Name)
}
if deployment.Spec.Template.ObjectMeta.Namespace != cr.ObjectMeta.Namespace {
t.Errorf("Deployment has wrong pod spec namespace %q, expected %q",
deployment.Spec.Template.ObjectMeta.Namespace, cr.ObjectMeta.Namespace)
}
if !reflect.DeepEqual(deployment.Spec.Template.ObjectMeta.Labels, getMatchLabels(cr)) {
t.Errorf("Deployment has wrong pod spec labels %v, expected %v",
deployment.Spec.Template.ObjectMeta.Labels, getMatchLabels(cr))
}
if deployment.Spec.Template.Spec.Containers[0].Image != cr.Spec.Image {
t.Errorf("Deployment has wrong container image %q, expected %q",
deployment.Spec.Template.Spec.Containers[0].Image, cr.Spec.Image)
}
// Verify no config maps yields only the host keys volume
if len(deployment.Spec.Template.Spec.Volumes) < 1 {
t.Error("Deployment is missing a volume for host keys")
} else if len(deployment.Spec.Template.Spec.Volumes) > 1 {
t.Errorf("Deployment has unexpected volumes: %v",
deployment.Spec.Template.Spec.Volumes)
} else if deployment.Spec.Template.Spec.Volumes[0].Name != hostKeysName {
t.Errorf("Volume in deployment does not appear to be for host keys: %v",
deployment.Spec.Template.Spec.Volumes[0])
}
if len(deployment.Spec.Template.Spec.Containers[0].VolumeMounts) < 1 {
t.Error("Deployment is missing a volume mount for host keys")
} else if len(deployment.Spec.Template.Spec.Containers[0].VolumeMounts) > 1 {
t.Errorf("Deployment has unexpected volume mounts in container: %v",
deployment.Spec.Template.Spec.Containers[0].VolumeMounts)
} else if deployment.Spec.Template.Spec.Containers[0].VolumeMounts[0].Name != hostKeysName {
t.Errorf("Volume mount in container does not appear to be for host keys: %v",
deployment.Spec.Template.Spec.Containers[0].VolumeMounts[0])
}
// Verify config maps are handled properly
configMapList = newConfigMapList("A", "B")
deployment = newSSHDDeployment(cr, configMapList, hostKeysSecret)
// Plus one volume for the host key secret.
if len(deployment.Spec.Template.Spec.Volumes) != len(configMapList.Items)+1 {
t.Errorf("Volumes are wrong in deployment, found %d, expected %d",
len(deployment.Spec.Template.Spec.Volumes),
len(configMapList.Items))
}
// Plus one volume mount for the host key secret.
if len(deployment.Spec.Template.Spec.Containers[0].VolumeMounts) != len(configMapList.Items)+1 {
t.Errorf("Container's volume mounts are wrong in deployment, found %d, expected %d",
len(deployment.Spec.Template.Spec.Containers[0].VolumeMounts),
len(configMapList.Items))
}
for index, configMap := range configMapList.Items {
volume := &deployment.Spec.Template.Spec.Volumes[index]
if volume.Name != configMap.ObjectMeta.Name {
t.Errorf("Volume %d has wrong name %q, expected %q",
index, volume.Name, configMap.ObjectMeta.Name)
}
if volume.VolumeSource.ConfigMap.LocalObjectReference.Name != configMap.ObjectMeta.Name {
t.Errorf("Volume %d references wrong config map %q, expected %q",
index, volume.VolumeSource.ConfigMap.LocalObjectReference.Name,
configMap.ObjectMeta.Name)
}
volumeMount := &deployment.Spec.Template.Spec.Containers[0].VolumeMounts[index]
if volumeMount.Name != configMap.ObjectMeta.Name {
t.Errorf("Volume mount %d has wrong name %q, expected %q",
index, volumeMount.Name, configMap.ObjectMeta.Name)
}
}
}
func TestNewSSHService(t *testing.T) {
// Verify SSHD parameters are honored
var service *corev1.Service = newSSHDService(cr)
if service.ObjectMeta.Name != cr.ObjectMeta.Name {
t.Errorf("Service has wrong name %q, expected %q",
service.ObjectMeta.Name, cr.ObjectMeta.Name)
}
if service.ObjectMeta.Namespace != cr.ObjectMeta.Namespace {
t.Errorf("Service has wrong namespace %q, expected %q",
service.ObjectMeta.Namespace, cr.ObjectMeta.Namespace)
}
if !reflect.DeepEqual(service.Spec.Selector, getMatchLabels(cr)) {
t.Errorf("Service has wrong selector %v, expected %v",
service.Spec.Selector, getMatchLabels(cr))
}
if !reflect.DeepEqual(service.Spec.LoadBalancerSourceRanges, cr.Spec.AllowedCIDRBlocks) {
t.Errorf("Service has wrong source ranges %v, expected %v",
service.Spec.LoadBalancerSourceRanges, cr.Spec.AllowedCIDRBlocks)
}
}
func TestReconcile(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
testClient, testScheme := setUpTestClient(t)
cloud := mockcc.NewMockCloudClient(ctrl)
cloud.EXPECT().EnsureSSHDNS(context.TODO(), testClient, OfType(reflect.TypeOf(cr).String()), svc)
r := &ReconcileSSHD{
client: testClient,
scheme: testScheme,
cloudClient: cloud,
}
result, err := r.Reconcile(context.TODO(), reconcile.Request{
NamespacedName: types.NamespacedName{
Name: placeholderName,
Namespace: placeholderNamespace,
},
})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if result.Requeue {
t.Errorf("got requeue on %v", result)
}
}
// utils
var cr = &cloudingressv1alpha1.SSHD{
TypeMeta: metav1.TypeMeta{
Kind: "SSHD",
APIVersion: cloudingressv1alpha1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: placeholderName,
Namespace: placeholderNamespace,
Finalizers: []string{
reconcileSSHDFinalizerDNS,
},
},
Spec: cloudingressv1alpha1.SSHDSpec{
AllowedCIDRBlocks: []string{"1.1.1.1", "2.2.2.2"},
Image: placeholderImage,
},
}
var svc = newSSHDService(cr)
func newConfigMap(name string) corev1.ConfigMap {
return corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: corev1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: placeholderNamespace,
Labels: map[string]string{
"api.openshift.com/authorized-keys": name,
},
},
Data: map[string]string{
"authorized_keys": "ssh-rsa R0lCQkVSSVNIIQ==",
},
}
}
func newConfigMapList(names ...string) *corev1.ConfigMapList {
items := []corev1.ConfigMap{}
for _, name := range names {
items = append(items, newConfigMap(name))
}
return &corev1.ConfigMapList{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMapList",
APIVersion: corev1.SchemeGroupVersion.String(),
},
Items: items,
}
}
func setUpTestClient(t *testing.T) (testClient client.Client, s *runtime.Scheme) {
t.Helper()
s = scheme.Scheme
s.AddKnownTypes(cloudingressv1alpha1.SchemeGroupVersion, cr)
secret := &corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: corev1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: placeholderName + "-host-keys",
Namespace: placeholderNamespace,
},
Data: map[string][]byte{
"ssh_host_rsa_key": []byte("somefakedata"),
},
}
testClient = fake.NewClientBuilder().WithScheme(s).WithObjects(secret, svc, cr).Build()
return
}
// set up a gomock matcher to test if something is the right type
type ofType struct{ t string }
func (o *ofType) Matches(x interface{}) bool {
return reflect.TypeOf(x).String() == o.t
}
func (o *ofType) String() string {
return "is of type " + o.t
}
func OfType(t string) gomock.Matcher {
return &ofType{t}
}