|
| 1 | +package resources |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + corev1 "k8s.io/api/core/v1" |
| 7 | + |
| 8 | + paperclipv1alpha1 "github.com/paperclipinc/paperclip-operator/api/v1alpha1" |
| 9 | +) |
| 10 | + |
| 11 | +func findVolume(volumes []corev1.Volume, name string) *corev1.Volume { |
| 12 | + for i := range volumes { |
| 13 | + if volumes[i].Name == name { |
| 14 | + return &volumes[i] |
| 15 | + } |
| 16 | + } |
| 17 | + return nil |
| 18 | +} |
| 19 | + |
| 20 | +func findMount(mounts []corev1.VolumeMount, name string) *corev1.VolumeMount { |
| 21 | + for i := range mounts { |
| 22 | + if mounts[i].Name == name { |
| 23 | + return &mounts[i] |
| 24 | + } |
| 25 | + } |
| 26 | + return nil |
| 27 | +} |
| 28 | + |
| 29 | +func TestBuildStatefulSetNoBrandingByDefault(t *testing.T) { |
| 30 | + instance := newTestInstance("my-paperclip") |
| 31 | + sts := BuildStatefulSet(instance, nil) |
| 32 | + |
| 33 | + if v := findVolume(sts.Spec.Template.Spec.Volumes, BrandVolumeName); v != nil { |
| 34 | + t.Errorf("expected no brand volume when branding unset, got %+v", v) |
| 35 | + } |
| 36 | + |
| 37 | + container := sts.Spec.Template.Spec.Containers[0] |
| 38 | + if m := findMount(container.VolumeMounts, BrandVolumeName); m != nil { |
| 39 | + t.Errorf("expected no brand volume mount when branding unset, got %+v", m) |
| 40 | + } |
| 41 | + for _, env := range container.Env { |
| 42 | + if env.Name == EnvBrandDir { |
| 43 | + t.Errorf("expected no %s env when branding unset", EnvBrandDir) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestBuildStatefulSetBrandingWiring(t *testing.T) { |
| 49 | + instance := newTestInstance("my-paperclip") |
| 50 | + instance.Spec.Branding = &paperclipv1alpha1.BrandingSpec{ |
| 51 | + CSSConfigMapRef: &corev1.LocalObjectReference{Name: "paperclip-brand-css"}, |
| 52 | + } |
| 53 | + sts := BuildStatefulSet(instance, nil) |
| 54 | + |
| 55 | + vol := findVolume(sts.Spec.Template.Spec.Volumes, BrandVolumeName) |
| 56 | + if vol == nil { |
| 57 | + t.Fatalf("expected a %q volume", BrandVolumeName) |
| 58 | + } |
| 59 | + if vol.ConfigMap == nil { |
| 60 | + t.Fatalf("expected brand volume to be backed by a ConfigMap, got %+v", vol.VolumeSource) |
| 61 | + } |
| 62 | + if vol.ConfigMap.Name != "paperclip-brand-css" { |
| 63 | + t.Errorf("expected brand ConfigMap name paperclip-brand-css, got %q", vol.ConfigMap.Name) |
| 64 | + } |
| 65 | + |
| 66 | + container := sts.Spec.Template.Spec.Containers[0] |
| 67 | + mount := findMount(container.VolumeMounts, BrandVolumeName) |
| 68 | + if mount == nil { |
| 69 | + t.Fatalf("expected a %q volume mount on the main container", BrandVolumeName) |
| 70 | + } |
| 71 | + if mount.MountPath != BrandMountPath { |
| 72 | + t.Errorf("expected brand mount path %q, got %q", BrandMountPath, mount.MountPath) |
| 73 | + } |
| 74 | + if !mount.ReadOnly { |
| 75 | + t.Error("expected the brand mount to be read-only") |
| 76 | + } |
| 77 | + |
| 78 | + var brandDir string |
| 79 | + for _, env := range container.Env { |
| 80 | + if env.Name == EnvBrandDir { |
| 81 | + brandDir = env.Value |
| 82 | + } |
| 83 | + } |
| 84 | + if brandDir != BrandMountPath { |
| 85 | + t.Errorf("expected %s=%s, got %q", EnvBrandDir, BrandMountPath, brandDir) |
| 86 | + } |
| 87 | +} |
0 commit comments