|
5 | 5 | "testing"
|
6 | 6 |
|
7 | 7 | "github.com/onsi/gomega"
|
| 8 | + "github.com/securesign/operator/api/v1alpha1" |
8 | 9 | "github.com/securesign/operator/internal/controller/annotations"
|
9 | 10 | "github.com/securesign/operator/internal/controller/common/utils"
|
10 | 11 | "github.com/securesign/operator/internal/controller/common/utils/kubernetes"
|
@@ -63,3 +64,76 @@ func TestEnsureTrustedCAFromAnnotations(t *testing.T) {
|
63 | 64 |
|
64 | 65 | })
|
65 | 66 | }
|
| 67 | + |
| 68 | +func TestEnsureTLS(t *testing.T) { |
| 69 | + gomega.RegisterTestingT(t) |
| 70 | + t.Run("update existing object", func(t *testing.T) { |
| 71 | + |
| 72 | + ctx := context.TODO() |
| 73 | + c := testAction.FakeClientBuilder(). |
| 74 | + WithObjects(&v1.Deployment{ |
| 75 | + ObjectMeta: v2.ObjectMeta{Name: name, Namespace: "default"}, |
| 76 | + Spec: v1.DeploymentSpec{ |
| 77 | + Template: v3.PodTemplateSpec{ |
| 78 | + Spec: v3.PodSpec{ |
| 79 | + Containers: []v3.Container{ |
| 80 | + {Name: name, Image: "test"}, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }). |
| 86 | + Build() |
| 87 | + |
| 88 | + result, err := kubernetes.CreateOrUpdate(ctx, c, |
| 89 | + &v1.Deployment{ObjectMeta: v2.ObjectMeta{Name: name, Namespace: "default"}}, |
| 90 | + TLS(v1alpha1.TLS{ |
| 91 | + PrivateKeyRef: &v1alpha1.SecretKeySelector{ |
| 92 | + LocalObjectReference: v1alpha1.LocalObjectReference{ |
| 93 | + Name: "testSecret", |
| 94 | + }, |
| 95 | + Key: "key", |
| 96 | + }, |
| 97 | + CertRef: &v1alpha1.SecretKeySelector{ |
| 98 | + LocalObjectReference: v1alpha1.LocalObjectReference{ |
| 99 | + Name: "testSecret", |
| 100 | + }, |
| 101 | + Key: "cert", |
| 102 | + }, |
| 103 | + }), |
| 104 | + ) |
| 105 | + gomega.Expect(err).ToNot(gomega.HaveOccurred()) |
| 106 | + |
| 107 | + gomega.Expect(result).To(gomega.Equal(controllerutil.OperationResultUpdated)) |
| 108 | + |
| 109 | + existing := &v1.Deployment{} |
| 110 | + gomega.Expect(c.Get(ctx, client.ObjectKey{Namespace: "default", Name: name}, existing)).To(gomega.Succeed()) |
| 111 | + |
| 112 | + gomega.Expect(existing.Spec.Template.Spec.Containers[0].VolumeMounts).To(gomega.HaveLen(1)) |
| 113 | + gomega.Expect(existing.Spec.Template.Spec.Containers[0].VolumeMounts[0].Name).To(gomega.Equal(TLSVolumeName)) |
| 114 | + gomega.Expect(existing.Spec.Template.Spec.Containers[0].VolumeMounts[0].MountPath).To(gomega.Equal("/var/run/secrets/tas")) |
| 115 | + |
| 116 | + gomega.Expect(existing.Spec.Template.Spec.Volumes).To(gomega.HaveLen(1)) |
| 117 | + gomega.Expect(existing.Spec.Template.Spec.Volumes[0].Name).To(gomega.Equal(TLSVolumeName)) |
| 118 | + gomega.Expect(existing.Spec.Template.Spec.Volumes[0].Projected.Sources).To(gomega.HaveLen(2)) |
| 119 | + gomega.Expect(existing.Spec.Template.Spec.Volumes[0].Projected.Sources).To(gomega.ContainElements( |
| 120 | + gomega.And( |
| 121 | + gomega.WithTransform(func(s v3.VolumeProjection) string { |
| 122 | + return s.Secret.Name |
| 123 | + }, gomega.Equal("testSecret")), |
| 124 | + gomega.WithTransform(func(s v3.VolumeProjection) string { |
| 125 | + return s.Secret.Items[0].Key |
| 126 | + }, gomega.Equal("key")), |
| 127 | + ), |
| 128 | + gomega.And( |
| 129 | + gomega.WithTransform(func(s v3.VolumeProjection) string { |
| 130 | + return s.Secret.Name |
| 131 | + }, gomega.Equal("testSecret")), |
| 132 | + gomega.WithTransform(func(s v3.VolumeProjection) string { |
| 133 | + return s.Secret.Items[0].Key |
| 134 | + }, gomega.Equal("cert")), |
| 135 | + ), |
| 136 | + )) |
| 137 | + |
| 138 | + }) |
| 139 | +} |
0 commit comments