|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + corev1 "k8s.io/api/core/v1" |
| 7 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 8 | +) |
| 9 | + |
| 10 | +func TestBuildCLIServerDeployment(t *testing.T) { |
| 11 | + const ( |
| 12 | + testNamespace = "openshift-adp" |
| 13 | + testImage = "quay.io/konveyor/oadp-cli-binaries:oadp-1.3" |
| 14 | + ) |
| 15 | + |
| 16 | + deployment := buildCLIServerDeployment(testNamespace, testImage) |
| 17 | + |
| 18 | + if deployment.Namespace != testNamespace { |
| 19 | + t.Errorf("expected namespace %q, got %q", testNamespace, deployment.Namespace) |
| 20 | + } |
| 21 | + if deployment.Name != cliServerDeploymentName { |
| 22 | + t.Errorf("expected name %q, got %q", cliServerDeploymentName, deployment.Name) |
| 23 | + } |
| 24 | + |
| 25 | + podSpec := deployment.Spec.Template.Spec |
| 26 | + |
| 27 | + if podSpec.ServiceAccountName != cliServerServiceAccountName { |
| 28 | + t.Errorf("expected serviceAccountName %q, got %q", cliServerServiceAccountName, podSpec.ServiceAccountName) |
| 29 | + } |
| 30 | + if podSpec.AutomountServiceAccountToken == nil || *podSpec.AutomountServiceAccountToken { |
| 31 | + t.Error("expected AutomountServiceAccountToken to be false") |
| 32 | + } |
| 33 | + |
| 34 | + if len(podSpec.Containers) == 0 { |
| 35 | + t.Fatal("expected at least one container") |
| 36 | + } |
| 37 | + container := podSpec.Containers[0] |
| 38 | + |
| 39 | + if container.Image != testImage { |
| 40 | + t.Errorf("expected image %q, got %q", testImage, container.Image) |
| 41 | + } |
| 42 | + |
| 43 | + if container.ReadinessProbe == nil { |
| 44 | + t.Fatal("expected ReadinessProbe to be set") |
| 45 | + } |
| 46 | + if container.ReadinessProbe.HTTPGet == nil { |
| 47 | + t.Fatal("expected ReadinessProbe to use HTTPGet") |
| 48 | + } |
| 49 | + if container.ReadinessProbe.HTTPGet.Path != "/" { |
| 50 | + t.Errorf("expected ReadinessProbe path \"/\", got %q", container.ReadinessProbe.HTTPGet.Path) |
| 51 | + } |
| 52 | + if container.ReadinessProbe.HTTPGet.Port != intstr.FromString("http") { |
| 53 | + t.Errorf("expected ReadinessProbe port \"http\", got %v", container.ReadinessProbe.HTTPGet.Port) |
| 54 | + } |
| 55 | + |
| 56 | + if container.LivenessProbe == nil { |
| 57 | + t.Fatal("expected LivenessProbe to be set") |
| 58 | + } |
| 59 | + if container.LivenessProbe.HTTPGet == nil { |
| 60 | + t.Fatal("expected LivenessProbe to use HTTPGet") |
| 61 | + } |
| 62 | + if container.LivenessProbe.HTTPGet.Path != "/" { |
| 63 | + t.Errorf("expected LivenessProbe path \"/\", got %q", container.LivenessProbe.HTTPGet.Path) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestBuildCLIServerServiceAccount(t *testing.T) { |
| 68 | + const testNamespace = "openshift-adp" |
| 69 | + |
| 70 | + sa := buildCLIServerServiceAccount(testNamespace) |
| 71 | + |
| 72 | + if sa.Name != cliServerServiceAccountName { |
| 73 | + t.Errorf("expected name %q, got %q", cliServerServiceAccountName, sa.Name) |
| 74 | + } |
| 75 | + if sa.Namespace != testNamespace { |
| 76 | + t.Errorf("expected namespace %q, got %q", testNamespace, sa.Namespace) |
| 77 | + } |
| 78 | + if sa.AutomountServiceAccountToken == nil || *sa.AutomountServiceAccountToken { |
| 79 | + t.Error("expected AutomountServiceAccountToken to be false") |
| 80 | + } |
| 81 | + if sa.Labels[managedByLabel] != operatorName { |
| 82 | + t.Errorf("expected label %q=%q, got %q", managedByLabel, operatorName, sa.Labels[managedByLabel]) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestBuildCLIServerDeployment_SecurityContext(t *testing.T) { |
| 87 | + deployment := buildCLIServerDeployment("openshift-adp", "test-image") |
| 88 | + podSpec := deployment.Spec.Template.Spec |
| 89 | + |
| 90 | + if podSpec.SecurityContext == nil || podSpec.SecurityContext.RunAsNonRoot == nil || !*podSpec.SecurityContext.RunAsNonRoot { |
| 91 | + t.Error("expected RunAsNonRoot to be true") |
| 92 | + } |
| 93 | + |
| 94 | + container := podSpec.Containers[0] |
| 95 | + if container.SecurityContext == nil { |
| 96 | + t.Fatal("expected container SecurityContext to be set") |
| 97 | + } |
| 98 | + if container.SecurityContext.AllowPrivilegeEscalation == nil || *container.SecurityContext.AllowPrivilegeEscalation { |
| 99 | + t.Error("expected AllowPrivilegeEscalation to be false") |
| 100 | + } |
| 101 | + if container.SecurityContext.ReadOnlyRootFilesystem == nil || !*container.SecurityContext.ReadOnlyRootFilesystem { |
| 102 | + t.Error("expected ReadOnlyRootFilesystem to be true") |
| 103 | + } |
| 104 | + |
| 105 | + dropped := false |
| 106 | + for _, cap := range container.SecurityContext.Capabilities.Drop { |
| 107 | + if cap == corev1.Capability("ALL") { |
| 108 | + dropped = true |
| 109 | + } |
| 110 | + } |
| 111 | + if !dropped { |
| 112 | + t.Error("expected ALL capabilities to be dropped") |
| 113 | + } |
| 114 | +} |
0 commit comments