-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathappconfig_defaulter_test.go
240 lines (215 loc) · 8.97 KB
/
appconfig_defaulter_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
// Copyright (c) 2020, 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"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"testing"
"github.com/crossplane/oam-kubernetes-runtime/apis/core"
oamv1 "github.com/crossplane/oam-kubernetes-runtime/apis/core/v1alpha2"
"github.com/golang/mock/gomock"
certapiv1alpha2 "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha2"
"github.com/stretchr/testify/assert"
"github.com/verrazzano/verrazzano/application-operator/mocks"
istiofake "istio.io/client-go/pkg/clientset/versioned/fake"
admissionv1beta1 "k8s.io/api/admission/v1beta1"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
k8sschema "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/fake"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/yaml"
)
func decoder() *admission.Decoder {
scheme := runtime.NewScheme()
core.AddToScheme(scheme)
decoder, err := admission.NewDecoder(scheme)
if err != nil {
log.Error(err, "error new decoder")
}
return decoder
}
// TestAppConfigDefaulterHandleError tests handling an invalid appconfig admission.Request
// GIVEN a AppConfigDefaulter and an appconfig admission.Request
// WHEN Handle is called with an invalid admission.Request containing no content
// THEN Handle should return an error with http.StatusBadRequest
func TestAppConfigDefaulterHandleError(t *testing.T) {
decoder := decoder()
defaulter := &AppConfigWebhook{}
defaulter.InjectDecoder(decoder)
req := admission.Request{}
res := defaulter.Handle(context.TODO(), req)
assert.False(t, res.Allowed)
assert.Equal(t, int32(http.StatusBadRequest), res.Result.Code)
}
// TestAppConfigDefaulterHandle tests handling an appconfig admission.Request
// GIVEN a AppConfigDefaulter and an appconfig admission.Request
// WHEN Handle is called with an admission.Request containing appconfig
// THEN Handle should return a patch response
func TestAppConfigDefaulterHandle(t *testing.T) {
decoder := decoder()
defaulter := &AppConfigWebhook{}
defaulter.InjectDecoder(decoder)
req := admission.Request{}
req.Object = runtime.RawExtension{Raw: readYaml2Json(t, "hello-conf.yaml")}
res := defaulter.Handle(context.TODO(), req)
assert.True(t, res.Allowed)
assert.NotEqual(t, 0, len(res.Patches))
}
// TestAppConfigWebhookHandleDelete tests handling an appconfig Delete admission.Request
// GIVEN a AppConfigWebhook and an appconfig Delete admission.Request
// WHEN Handle is called with an admission.Request containing appconfig
// THEN Handle should return an Allowed response with no patch
func TestAppConfigWebhookHandleDelete(t *testing.T) {
testAppConfigWebhookHandleDelete(t, true, true, false)
}
// TestAppConfigWebhookHandleDeleteDryRun tests handling a dry run appconfig Delete admission.Request
// GIVEN a AppConfigWebhook and an appconfig Delete admission.Request
// WHEN Handle is called with an admission.Request containing appconfig and set for a dry run
// THEN Handle should return an Allowed response with no patch
func TestAppConfigWebhookHandleDeleteDryRun(t *testing.T) {
testAppConfigWebhookHandleDelete(t, true, true, true)
}
// TestAppConfigWebhookHandleDeleteCertNotFound tests handling an appconfig Delete admission.Request where the app config cert is not found
// GIVEN a AppConfigWebhook and an appconfig Delete admission.Request
// WHEN Handle is called with an admission.Request containing appconfig and the cert is not found
// THEN Handle should return an Allowed response with no patch
func TestAppConfigWebhookHandleDeleteCertNotFound(t *testing.T) {
testAppConfigWebhookHandleDelete(t, false, true, false)
}
// TestAppConfigWebhookHandleDeleteSecretNotFound tests handling an appconfig Delete admission.Request where the app config secret is not found
// GIVEN a AppConfigWebhook and an appconfig Delete admission.Request
// WHEN Handle is called with an admission.Request containing appconfig and the secret is not found
// THEN Handle should return an Allowed response with no patch
func TestAppConfigWebhookHandleDeleteSecretNotFound(t *testing.T) {
testAppConfigWebhookHandleDelete(t, true, false, false)
}
// TestAppConfigDefaulterHandleMarshalError tests handling an appconfig with json marshal error
// GIVEN a AppConfigDefaulter with mock appconfigMarshalFunc
// WHEN Handle is called with an admission.Request containing appconfig
// THEN Handle should return error with http.StatusInternalServerError
func TestAppConfigDefaulterHandleMarshalError(t *testing.T) {
decoder := decoder()
defaulter := &AppConfigWebhook{}
defaulter.InjectDecoder(decoder)
req := admission.Request{}
req.Object = runtime.RawExtension{Raw: readYaml2Json(t, "hello-conf.yaml")}
appconfigMarshalFunc = func(v interface{}) ([]byte, error) {
return nil, fmt.Errorf("json marshal error")
}
res := defaulter.Handle(context.TODO(), req)
assert.False(t, res.Allowed)
assert.Equal(t, int32(http.StatusInternalServerError), res.Result.Code)
}
type mockErrorDefaulter struct {
}
func (*mockErrorDefaulter) Default(appConfig *oamv1.ApplicationConfiguration, dryRun bool) error {
return fmt.Errorf("mockErrorDefaulter error")
}
func (*mockErrorDefaulter) Cleanup(appConfig *oamv1.ApplicationConfiguration, dryRun bool) error {
return nil
}
// TestAppConfigDefaulterHandleDefaultError tests handling a defaulter error
// GIVEN a AppConfigDefaulter with mock defaulter
// WHEN Handle is called with an admission.Request containing appconfig
// THEN Handle should return error with http.StatusInternalServerError
func TestAppConfigDefaulterHandleDefaultError(t *testing.T) {
decoder := decoder()
defaulter := &AppConfigWebhook{Defaulters: []AppConfigDefaulter{&mockErrorDefaulter{}}}
defaulter.InjectDecoder(decoder)
req := admission.Request{}
req.Object = runtime.RawExtension{Raw: readYaml2Json(t, "hello-conf.yaml")}
res := defaulter.Handle(context.TODO(), req)
assert.False(t, res.Allowed)
assert.Equal(t, int32(http.StatusInternalServerError), res.Result.Code)
}
func testAppConfigWebhookHandleDelete(t *testing.T, certFound, secretFound, dryRun bool) {
const istioSystem = "istio-system"
const certName = "default-hello-app-cert"
const secretName = "default-hello-app-cert-secret"
mocker := gomock.NewController(t)
mockClient := mocks.NewMockClient(mocker)
if !dryRun {
// get cert
mockClient.EXPECT().
Get(gomock.Any(), types.NamespacedName{Namespace: istioSystem, Name: certName}, gomock.Not(gomock.Nil())).
DoAndReturn(func(ctx context.Context, name types.NamespacedName, cert *certapiv1alpha2.Certificate) error {
if certFound {
cert.Namespace = istioSystem
cert.Name = certName
return nil
}
return k8serrors.NewNotFound(k8sschema.GroupResource{}, certName)
})
// delete cert
if certFound {
mockClient.EXPECT().
Delete(gomock.Any(), gomock.Not(gomock.Nil()), gomock.Any()).
DoAndReturn(func(ctx context.Context, cert *certapiv1alpha2.Certificate, opt *client.DeleteOptions) error {
assert.Equal(t, istioSystem, cert.Namespace)
assert.Equal(t, certName, cert.Name)
return nil
})
}
// get secret
mockClient.EXPECT().
Get(gomock.Any(), types.NamespacedName{Namespace: istioSystem, Name: secretName}, gomock.Not(gomock.Nil())).
DoAndReturn(func(ctx context.Context, name types.NamespacedName, sec *corev1.Secret) error {
if secretFound {
sec.Namespace = istioSystem
sec.Name = secretName
return nil
}
return k8serrors.NewNotFound(k8sschema.GroupResource{}, secretName)
})
// delete secret
if secretFound {
mockClient.EXPECT().
Delete(gomock.Any(), gomock.Not(gomock.Nil()), gomock.Any()).
DoAndReturn(func(ctx context.Context, sec *corev1.Secret, opt *client.DeleteOptions) error {
assert.Equal(t, istioSystem, sec.Namespace)
assert.Equal(t, secretName, sec.Name)
return nil
})
}
// list projects
mockClient.EXPECT().
List(gomock.Any(), gomock.Not(gomock.Nil()), gomock.Any())
}
decoder := decoder()
webhook := &AppConfigWebhook{
Client: mockClient,
KubeClient: fake.NewSimpleClientset(),
IstioClient: istiofake.NewSimpleClientset(),
}
webhook.InjectDecoder(decoder)
req := admission.Request{
AdmissionRequest: admissionv1beta1.AdmissionRequest{Operation: admissionv1beta1.Delete},
}
req.OldObject = runtime.RawExtension{Raw: readYaml2Json(t, "hello-conf.yaml")}
if dryRun {
dryRun := true
req.DryRun = &dryRun
}
res := webhook.Handle(context.TODO(), req)
assert.True(t, res.Allowed)
assert.Equal(t, 0, len(res.Patches))
}
func readYaml2Json(t *testing.T, path string) []byte {
filename, _ := filepath.Abs(fmt.Sprintf("testdata/%s", path))
yamlBytes, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatalf("Error reading %v: %v", path, err)
}
jsonBytes, err := yaml.YAMLToJSON(yamlBytes)
if err != nil {
log.Error(err, "Error json marshal")
}
return jsonBytes
}