-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmulticlusterconfigmap_webhook_test.go
202 lines (184 loc) · 7.85 KB
/
multiclusterconfigmap_webhook_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
// Copyright (c) 2021, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
package webhooks
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
v1alpha12 "github.com/verrazzano/verrazzano/application-operator/apis/clusters/v1alpha1"
"github.com/verrazzano/verrazzano/application-operator/constants"
"github.com/verrazzano/verrazzano/platform-operator/apis/clusters/v1alpha1"
admissionv1beta1 "k8s.io/api/admission/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
// newMultiClusterConfigmapValidator creates a new MultiClusterConfigmapValidator
func newMultiClusterConfigmapValidator() MultiClusterConfigmapValidator {
scheme := newScheme()
decoder, _ := admission.NewDecoder(scheme)
cli := fake.NewFakeClientWithScheme(scheme)
v := MultiClusterConfigmapValidator{client: cli, decoder: decoder}
return v
}
// TestValidationFailureForMultiClusterConfigMapCreationWithoutTargetClusters tests preventing the creation
// of a MultiClusterConfigMap resources that is missing Placement information.
// GIVEN a call to validate a MultiClusterConfigMap resource
// WHEN the MultiClusterConfigMap resource is missing Placement information
// THEN the validation should fail.
func TestValidationFailureForMultiClusterConfigMapCreationWithoutTargetClusters(t *testing.T) {
asrt := assert.New(t)
v := newMultiClusterConfigmapValidator()
p := v1alpha12.MultiClusterConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "test-mcconfigmap-name",
Namespace: constants.VerrazzanoMultiClusterNamespace,
},
Spec: v1alpha12.MultiClusterConfigMapSpec{},
}
req := newAdmissionRequest(admissionv1beta1.Create, p)
res := v.Handle(context.TODO(), req)
asrt.False(res.Allowed, "Expected multi-cluster configmap validation to fail due to missing placement information.")
asrt.Contains(res.Result.Reason, "target cluster")
req = newAdmissionRequest(admissionv1beta1.Update, p)
res = v.Handle(context.TODO(), req)
asrt.False(res.Allowed, "Expected multi-cluster configmap validation to fail due to missing placement information.")
asrt.Contains(res.Result.Reason, "target cluster")
}
// TestValidationFailureForMultiClusterConfigMapCreationTargetingMissingManagedCluster tests preventing the creation
// of a MultiClusterConfigMap resources that references a non-existent managed cluster.
// GIVEN a call to validate a MultiClusterConfigMap resource
// WHEN the MultiClusterConfigMap resource references a VerrazzanoManagedCluster that does not exist
// THEN the validation should fail.
func TestValidationFailureForMultiClusterConfigMapCreationTargetingMissingManagedCluster(t *testing.T) {
asrt := assert.New(t)
v := newMultiClusterConfigmapValidator()
p := v1alpha12.MultiClusterConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "test-mcconfigmap-name",
Namespace: constants.VerrazzanoMultiClusterNamespace,
},
Spec: v1alpha12.MultiClusterConfigMapSpec{
Placement: v1alpha12.Placement{
Clusters: []v1alpha12.Cluster{{Name: "invalid-cluster-name"}},
},
},
}
req := newAdmissionRequest(admissionv1beta1.Create, p)
res := v.Handle(context.TODO(), req)
asrt.False(res.Allowed, "Expected multi-cluster configmap validation to fail due to missing placement information.")
asrt.Contains(res.Result.Reason, "invalid-cluster-name")
req = newAdmissionRequest(admissionv1beta1.Update, p)
res = v.Handle(context.TODO(), req)
asrt.False(res.Allowed, "Expected multi-cluster configmap validation to fail due to missing placement information.")
asrt.Contains(res.Result.Reason, "invalid-cluster-name")
}
// TestValidationSuccessForMultiClusterConfigMapCreationTargetingExistingManagedCluster tests allowing the creation
// of a MultiClusterConfigMap resources that references an existent managed cluster.
// GIVEN a call to validate a MultiClusterConfigMap resource
// WHEN the MultiClusterConfigMap resource references a VerrazzanoManagedCluster that does exist
// THEN the validation should pass.
func TestValidationSuccessForMultiClusterConfigMapCreationTargetingExistingManagedCluster(t *testing.T) {
asrt := assert.New(t)
v := newMultiClusterConfigmapValidator()
c := v1alpha1.VerrazzanoManagedCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "valid-cluster-name",
Namespace: constants.VerrazzanoMultiClusterNamespace,
},
Spec: v1alpha1.VerrazzanoManagedClusterSpec{
CASecret: "test-secret",
ManagedClusterManifestSecret: "test-cluster-manifest-secret",
ServiceAccount: "test-service-account",
},
}
p := v1alpha12.MultiClusterConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "test-mcconfigmap-name",
Namespace: "application-ns",
},
Spec: v1alpha12.MultiClusterConfigMapSpec{
Placement: v1alpha12.Placement{
Clusters: []v1alpha12.Cluster{{Name: "valid-cluster-name"}},
},
},
}
vp := v1alpha12.VerrazzanoProject{
ObjectMeta: metav1.ObjectMeta{
Name: "test-verrazzanoproject-name",
Namespace: constants.VerrazzanoMultiClusterNamespace,
},
Spec: v1alpha12.VerrazzanoProjectSpec{
Template: v1alpha12.ProjectTemplate{
Namespaces: []v1alpha12.NamespaceTemplate{
{
Metadata: metav1.ObjectMeta{
Name: "application-ns",
},
},
},
},
},
}
asrt.NoError(v.client.Create(context.TODO(), &c))
asrt.NoError(v.client.Create(context.TODO(), &vp))
req := newAdmissionRequest(admissionv1beta1.Create, p)
res := v.Handle(context.TODO(), req)
asrt.True(res.Allowed, "Expected multi-cluster configmap create validation to succeed.")
req = newAdmissionRequest(admissionv1beta1.Update, p)
res = v.Handle(context.TODO(), req)
asrt.True(res.Allowed, "Expected multi-cluster configmap update validation to succeed.")
}
// TestValidationSuccessForMultiClusterConfigMapCreationWithoutTargetClustersOnManagedCluster tests allowing the creation
// of a MultiClusterConfigMap resources that is missing target cluster information when on managed cluster.
// GIVEN a call to validate a MultiClusterConfigMap resource
// WHEN the MultiClusterConfigMap resource is missing Placement information
// AND the validation is being done on a managed cluster
// THEN the validation should succeed.
func TestValidationSuccessForMultiClusterConfigMapCreationWithoutTargetClustersOnManagedCluster(t *testing.T) {
asrt := assert.New(t)
v := newMultiClusterConfigmapValidator()
s := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: constants.MCRegistrationSecret,
Namespace: constants.VerrazzanoSystemNamespace,
},
}
p := v1alpha12.MultiClusterConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "test-mcconfigmap-name",
Namespace: "application-ns",
},
Spec: v1alpha12.MultiClusterConfigMapSpec{
Placement: v1alpha12.Placement{
Clusters: []v1alpha12.Cluster{{Name: "invalid-cluster-name"}},
},
},
}
vp := v1alpha12.VerrazzanoProject{
ObjectMeta: metav1.ObjectMeta{
Name: "test-verrazzanoproject-name",
Namespace: constants.VerrazzanoMultiClusterNamespace,
},
Spec: v1alpha12.VerrazzanoProjectSpec{
Template: v1alpha12.ProjectTemplate{
Namespaces: []v1alpha12.NamespaceTemplate{
{
Metadata: metav1.ObjectMeta{
Name: "application-ns",
},
},
},
},
},
}
asrt.NoError(v.client.Create(context.TODO(), &s))
asrt.NoError(v.client.Create(context.TODO(), &vp))
req := newAdmissionRequest(admissionv1beta1.Create, p)
res := v.Handle(context.TODO(), req)
asrt.True(res.Allowed, "Expected multi-cluster configmap validation to succeed with missing placement information on managed cluster.")
req = newAdmissionRequest(admissionv1beta1.Update, p)
res = v.Handle(context.TODO(), req)
asrt.True(res.Allowed, "Expected multi-cluster configmap validation to succeed with missing placement information on managed cluster.")
}