|
| 1 | +package util |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + . "github.com/onsi/gomega" |
| 8 | + |
| 9 | + appsv1 "k8s.io/api/apps/v1" |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | +) |
| 12 | + |
| 13 | +// initDeployment creates a base deployment for testing. When existing is non-empty, |
| 14 | +// it populates volumes, init containers, volume mounts, and env vars using the |
| 15 | +// string as a naming prefix. When empty, those fields are left uninitialized. |
| 16 | +func initDeployment(existing string) *appsv1.Deployment { |
| 17 | + dep := &appsv1.Deployment{ |
| 18 | + Spec: appsv1.DeploymentSpec{ |
| 19 | + Template: corev1.PodTemplateSpec{ |
| 20 | + Spec: corev1.PodSpec{ |
| 21 | + Containers: []corev1.Container{ |
| 22 | + {Name: "main"}, |
| 23 | + }, |
| 24 | + }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + } |
| 28 | + if existing != "" { |
| 29 | + dep.Spec.Template.Spec.Volumes = []corev1.Volume{ |
| 30 | + {Name: existing + "-volume"}, |
| 31 | + } |
| 32 | + dep.Spec.Template.Spec.InitContainers = []corev1.Container{ |
| 33 | + {Name: existing + "-init"}, |
| 34 | + } |
| 35 | + dep.Spec.Template.Spec.Containers[0].VolumeMounts = []corev1.VolumeMount{ |
| 36 | + {Name: existing + "-mount", MountPath: "/data"}, |
| 37 | + } |
| 38 | + dep.Spec.Template.Spec.Containers[0].Env = []corev1.EnvVar{ |
| 39 | + {Name: strings.ToUpper(existing) + "_VAR", Value: "value"}, |
| 40 | + } |
| 41 | + } |
| 42 | + return dep |
| 43 | +} |
| 44 | + |
| 45 | +func TestDeploymentAddAWSCABundleVolume(t *testing.T) { |
| 46 | + testCases := []struct { |
| 47 | + name string |
| 48 | + trustBundleConfigMap *corev1.LocalObjectReference |
| 49 | + existing string |
| 50 | + initContainerImage string |
| 51 | + }{ |
| 52 | + { |
| 53 | + name: "When a trust bundle ConfigMap is provided it should add volumes, init container, volume mount, and AWS_CA_BUNDLE env var", |
| 54 | + trustBundleConfigMap: &corev1.LocalObjectReference{Name: "my-trust-bundle"}, |
| 55 | + existing: "", |
| 56 | + initContainerImage: "registry.example.com/cpo:latest", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "When the deployment already has existing resources it should append without removing them", |
| 60 | + trustBundleConfigMap: &corev1.LocalObjectReference{Name: "custom-ca"}, |
| 61 | + existing: "existing", |
| 62 | + initContainerImage: "registry.example.com/cpo:v2", |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tc := range testCases { |
| 67 | + t.Run(tc.name, func(t *testing.T) { |
| 68 | + g := NewGomegaWithT(t) |
| 69 | + |
| 70 | + deployment := initDeployment(tc.existing) |
| 71 | + existingVolumeCount := len(deployment.Spec.Template.Spec.Volumes) |
| 72 | + existingInitContainerCount := len(deployment.Spec.Template.Spec.InitContainers) |
| 73 | + existingVolumeMountCount := len(deployment.Spec.Template.Spec.Containers[0].VolumeMounts) |
| 74 | + existingEnvCount := len(deployment.Spec.Template.Spec.Containers[0].Env) |
| 75 | + |
| 76 | + DeploymentAddAWSCABundleVolume(tc.trustBundleConfigMap, deployment, tc.initContainerImage) |
| 77 | + |
| 78 | + spec := deployment.Spec.Template.Spec |
| 79 | + |
| 80 | + // It should add exactly two new volumes (user-ca-bundle and aws-ca-bundle). |
| 81 | + g.Expect(spec.Volumes).To(HaveLen(existingVolumeCount + 2)) |
| 82 | + |
| 83 | + // Verify user-ca-bundle volume references the ConfigMap. |
| 84 | + var userCAVolume *corev1.Volume |
| 85 | + for i := range spec.Volumes { |
| 86 | + if spec.Volumes[i].Name == "user-ca-bundle" { |
| 87 | + userCAVolume = &spec.Volumes[i] |
| 88 | + break |
| 89 | + } |
| 90 | + } |
| 91 | + g.Expect(userCAVolume).NotTo(BeNil(), "user-ca-bundle volume should exist") |
| 92 | + g.Expect(userCAVolume.VolumeSource.ConfigMap).NotTo(BeNil()) |
| 93 | + g.Expect(userCAVolume.VolumeSource.ConfigMap.LocalObjectReference.Name).To(Equal(tc.trustBundleConfigMap.Name)) |
| 94 | + g.Expect(userCAVolume.VolumeSource.ConfigMap.Items).To(ConsistOf( |
| 95 | + corev1.KeyToPath{Key: "ca-bundle.crt", Path: "user-ca-bundle.pem"}, |
| 96 | + )) |
| 97 | + |
| 98 | + // Verify aws-ca-bundle volume is an EmptyDir. |
| 99 | + var combinedCAVolume *corev1.Volume |
| 100 | + for i := range spec.Volumes { |
| 101 | + if spec.Volumes[i].Name == "aws-ca-bundle" { |
| 102 | + combinedCAVolume = &spec.Volumes[i] |
| 103 | + break |
| 104 | + } |
| 105 | + } |
| 106 | + g.Expect(combinedCAVolume).NotTo(BeNil(), "aws-ca-bundle volume should exist") |
| 107 | + g.Expect(combinedCAVolume.VolumeSource.EmptyDir).NotTo(BeNil()) |
| 108 | + |
| 109 | + // It should add exactly one init container. |
| 110 | + g.Expect(spec.InitContainers).To(HaveLen(existingInitContainerCount + 1)) |
| 111 | + |
| 112 | + initContainer := spec.InitContainers[len(spec.InitContainers)-1] |
| 113 | + g.Expect(initContainer.Name).To(Equal("setup-aws-ca-bundle")) |
| 114 | + g.Expect(initContainer.Image).To(Equal(tc.initContainerImage)) |
| 115 | + g.Expect(initContainer.Command).To(Equal([]string{ |
| 116 | + "/bin/sh", "-c", |
| 117 | + "cat /etc/pki/tls/certs/ca-bundle.crt /user-ca/user-ca-bundle.pem > /etc/pki/ca-trust/extracted/hypershift/combined-ca-bundle.pem", |
| 118 | + })) |
| 119 | + g.Expect(initContainer.VolumeMounts).To(ConsistOf( |
| 120 | + corev1.VolumeMount{Name: "user-ca-bundle", MountPath: "/user-ca", ReadOnly: true}, |
| 121 | + corev1.VolumeMount{Name: "aws-ca-bundle", MountPath: "/etc/pki/ca-trust/extracted/hypershift"}, |
| 122 | + )) |
| 123 | + |
| 124 | + // It should add exactly one volume mount to the main container. |
| 125 | + g.Expect(spec.Containers[0].VolumeMounts).To(HaveLen(existingVolumeMountCount + 1)) |
| 126 | + addedMount := spec.Containers[0].VolumeMounts[len(spec.Containers[0].VolumeMounts)-1] |
| 127 | + g.Expect(addedMount.Name).To(Equal("aws-ca-bundle")) |
| 128 | + g.Expect(addedMount.MountPath).To(Equal("/etc/pki/ca-trust/extracted/hypershift")) |
| 129 | + g.Expect(addedMount.ReadOnly).To(BeTrue()) |
| 130 | + |
| 131 | + // It should set AWS_CA_BUNDLE env var on the main container. |
| 132 | + g.Expect(spec.Containers[0].Env).To(HaveLen(existingEnvCount + 1)) |
| 133 | + addedEnv := spec.Containers[0].Env[len(spec.Containers[0].Env)-1] |
| 134 | + g.Expect(addedEnv.Name).To(Equal("AWS_CA_BUNDLE")) |
| 135 | + g.Expect(addedEnv.Value).To(Equal("/etc/pki/ca-trust/extracted/hypershift/combined-ca-bundle.pem")) |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
0 commit comments