Skip to content

Commit 7097658

Browse files
committed
add more unit tests
Signed-off-by: odubajDT <[email protected]>
1 parent da7f93b commit 7097658

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

webhooks/common_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,13 @@ func Test_NewFeatureFlagSourceSpec(t *testing.T) {
178178

179179
require.Equal(t, expected, NewFeatureFlagSourceSpec(env))
180180
}
181+
182+
func Test_shouldUseRPC(t *testing.T) {
183+
require.True(t, shouldUseRPC(map[string]string{
184+
fmt.Sprintf("%s/%s", common.OpenFeatureAnnotationPrefix, common.FeatureFlagSourceAnnotation): "value",
185+
}))
186+
187+
require.False(t, shouldUseRPC(map[string]string{
188+
fmt.Sprintf("%s/%s", common.OpenFeatureAnnotationPrefix, common.FeatureFlagInProcessSourceAnnotation): "value",
189+
}))
190+
}

webhooks/pod_webhook_test.go

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ import (
2929
)
3030

3131
const (
32-
mutatePodNamespace = "test-mutate-pod"
33-
defaultPodServiceAccountName = "test-pod-service-account"
34-
featureFlagSourceName = "test-feature-flag-source"
32+
mutatePodNamespace = "test-mutate-pod"
33+
defaultPodServiceAccountName = "test-pod-service-account"
34+
featureFlagSourceName = "test-feature-flag-source"
35+
featureFlagInProcessSourceName = "test-feature-flag-in-process-source"
3536
)
3637

3738
func TestPodMutator_BackfillPermissions(t *testing.T) {
@@ -258,6 +259,22 @@ func TestPodMutator_Handle(t *testing.T) {
258259
goodAnnotatedPod, err := json.Marshal(antPod)
259260
require.Nil(t, err)
260261

262+
inProcessPod := corev1.Pod{
263+
ObjectMeta: metav1.ObjectMeta{
264+
Name: "myAnnotatedPod",
265+
Namespace: mutatePodNamespace,
266+
Annotations: map[string]string{
267+
fmt.Sprintf("%s/%s", common.OpenFeatureAnnotationPrefix, common.EnabledAnnotation): "true",
268+
fmt.Sprintf("%s/%s", common.OpenFeatureAnnotationPrefix, common.FeatureFlagInProcessSourceAnnotation): fmt.Sprintf("%s/%s", mutatePodNamespace, featureFlagInProcessSourceName),
269+
},
270+
OwnerReferences: []metav1.OwnerReference{{UID: "123"}},
271+
},
272+
Spec: corev1.PodSpec{ServiceAccountName: defaultPodServiceAccountName},
273+
}
274+
275+
goodInProcessAnnotatedPod, err := json.Marshal(inProcessPod)
276+
require.Nil(t, err)
277+
261278
tests := []struct {
262279
name string
263280
mutator *PodMutator
@@ -402,7 +419,7 @@ func TestPodMutator_Handle(t *testing.T) {
402419
wantCode: http.StatusNotFound,
403420
},
404421
{
405-
name: "happy path: request pod annotated configured for env var",
422+
name: "happy path rpc: request pod annotated configured for env var",
406423
mutator: &PodMutator{
407424
Client: NewClient(true,
408425
&antPod,
@@ -448,6 +465,64 @@ func TestPodMutator_Handle(t *testing.T) {
448465
},
449466
allow: true,
450467
},
468+
{
469+
name: "happy path in-process: request pod annotated configured for env var",
470+
mutator: &PodMutator{
471+
Client: NewClient(true,
472+
&inProcessPod,
473+
&corev1.ServiceAccount{
474+
ObjectMeta: metav1.ObjectMeta{
475+
Name: defaultPodServiceAccountName,
476+
Namespace: mutatePodNamespace,
477+
},
478+
},
479+
&rbac.ClusterRoleBinding{
480+
ObjectMeta: metav1.ObjectMeta{Name: common.ClusterRoleBindingName},
481+
Subjects: nil,
482+
RoleRef: rbac.RoleRef{},
483+
},
484+
&api.FeatureFlagInProcessSource{
485+
ObjectMeta: metav1.ObjectMeta{
486+
Name: featureFlagInProcessSourceName,
487+
Namespace: mutatePodNamespace,
488+
},
489+
Spec: api.FeatureFlagInProcessSourceSpec{
490+
EnvVars: []corev1.EnvVar{
491+
{
492+
Name: "env1",
493+
Value: "val1",
494+
},
495+
{
496+
Name: "env2",
497+
Value: "val2",
498+
},
499+
},
500+
},
501+
},
502+
),
503+
decoder: decoder,
504+
Log: testr.New(t),
505+
},
506+
req: admission.Request{
507+
AdmissionRequest: admissionv1.AdmissionRequest{
508+
UID: "123",
509+
Object: runtime.RawExtension{
510+
Raw: goodInProcessAnnotatedPod,
511+
Object: &inProcessPod,
512+
},
513+
},
514+
},
515+
setup: func(mockInjector *flagdinjectorfake.MockFlagdContainerInjector) {
516+
mockInjector.EXPECT().
517+
InjectFlagd(
518+
gomock.Any(),
519+
gomock.Any(),
520+
gomock.Any(),
521+
gomock.Any(),
522+
).Return(nil).Times(0)
523+
},
524+
allow: true,
525+
},
451526
{
452527
name: "wrong request",
453528
mutator: &PodMutator{

0 commit comments

Comments
 (0)