|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. |
| 2 | +// |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2024 Datadog, Inc. |
| 4 | + |
| 5 | +package v1alpha1 |
| 6 | + |
| 7 | +// Integration tests for CRD-level CEL validation rules on TemporalWorkerDeployment. |
| 8 | +// |
| 9 | +// These tests hit a real kube-apiserver (via envtest) so they verify that the |
| 10 | +// x-kubernetes-validations blocks in the generated CRD manifest are syntactically |
| 11 | +// valid and semantically correct. The webhook Go code is NOT involved here — we are |
| 12 | +// testing what the API server enforces regardless of whether the webhook is enabled. |
| 13 | + |
| 14 | +import ( |
| 15 | + "strings" |
| 16 | + "time" |
| 17 | + |
| 18 | + . "github.com/onsi/ginkgo/v2" |
| 19 | + . "github.com/onsi/gomega" |
| 20 | + corev1 "k8s.io/api/core/v1" |
| 21 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | +) |
| 23 | + |
| 24 | +var _ = Describe("TemporalWorkerDeployment CRD CEL validation", func() { |
| 25 | + var ns string |
| 26 | + |
| 27 | + BeforeEach(func() { |
| 28 | + ns = makeTestNamespace("twd-cel") |
| 29 | + }) |
| 30 | + |
| 31 | + // baseTWD returns a minimal valid TWD in the given namespace. |
| 32 | + baseTWD := func(name string) *TemporalWorkerDeployment { |
| 33 | + return &TemporalWorkerDeployment{ |
| 34 | + ObjectMeta: metav1.ObjectMeta{ |
| 35 | + Name: name, |
| 36 | + Namespace: ns, |
| 37 | + }, |
| 38 | + Spec: TemporalWorkerDeploymentSpec{ |
| 39 | + Template: corev1.PodTemplateSpec{ |
| 40 | + Spec: corev1.PodSpec{ |
| 41 | + Containers: []corev1.Container{{Name: "worker", Image: "worker:latest"}}, |
| 42 | + }, |
| 43 | + }, |
| 44 | + RolloutStrategy: RolloutStrategy{Strategy: UpdateAllAtOnce}, |
| 45 | + WorkerOptions: WorkerOptions{ |
| 46 | + TemporalConnectionRef: TemporalConnectionReference{Name: "my-connection"}, |
| 47 | + TemporalNamespace: "default", |
| 48 | + }, |
| 49 | + }, |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + It("accepts a valid TWD", func() { |
| 54 | + Expect(k8sClient.Create(ctx, baseTWD("valid-worker"))).To(Succeed()) |
| 55 | + }) |
| 56 | + |
| 57 | + It("rejects name longer than 63 characters", func() { |
| 58 | + twd := baseTWD(strings.Repeat("a", 64)) |
| 59 | + err := k8sClient.Create(ctx, twd) |
| 60 | + Expect(err).To(HaveOccurred()) |
| 61 | + Expect(err.Error()).To(ContainSubstring("name cannot be more than 63 characters")) |
| 62 | + }) |
| 63 | + |
| 64 | + It("rejects Progressive strategy with no steps", func() { |
| 65 | + twd := baseTWD("prog-no-steps") |
| 66 | + twd.Spec.RolloutStrategy = RolloutStrategy{Strategy: UpdateProgressive} |
| 67 | + err := k8sClient.Create(ctx, twd) |
| 68 | + Expect(err).To(HaveOccurred()) |
| 69 | + Expect(err.Error()).To(ContainSubstring("steps are required for Progressive rollout")) |
| 70 | + }) |
| 71 | + |
| 72 | + It("rejects more than 20 Progressive steps", func() { |
| 73 | + steps := make([]RolloutStep, 21) |
| 74 | + for i := range steps { |
| 75 | + steps[i] = RolloutStep{ |
| 76 | + RampPercentage: i + 1, |
| 77 | + PauseDuration: metav1.Duration{Duration: time.Minute}, |
| 78 | + } |
| 79 | + } |
| 80 | + twd := baseTWD("prog-too-many-steps") |
| 81 | + twd.Spec.RolloutStrategy = RolloutStrategy{Strategy: UpdateProgressive, Steps: steps} |
| 82 | + err := k8sClient.Create(ctx, twd) |
| 83 | + Expect(err).To(HaveOccurred()) |
| 84 | + Expect(err.Error()).To(ContainSubstring("Too many")) |
| 85 | + }) |
| 86 | + |
| 87 | + It("rejects a Progressive step with pauseDuration less than 30s", func() { |
| 88 | + twd := baseTWD("short-pause") |
| 89 | + twd.Spec.RolloutStrategy = RolloutStrategy{ |
| 90 | + Strategy: UpdateProgressive, |
| 91 | + Steps: []RolloutStep{ |
| 92 | + {RampPercentage: 50, PauseDuration: metav1.Duration{Duration: 10 * time.Second}}, |
| 93 | + }, |
| 94 | + } |
| 95 | + err := k8sClient.Create(ctx, twd) |
| 96 | + Expect(err).To(HaveOccurred()) |
| 97 | + Expect(err.Error()).To(ContainSubstring("pause duration must be at least 30s")) |
| 98 | + }) |
| 99 | + |
| 100 | + It("rejects gate.inputFrom with both configMapKeyRef and secretKeyRef set", func() { |
| 101 | + twd := baseTWD("bad-gate-inputfrom") |
| 102 | + twd.Spec.RolloutStrategy = RolloutStrategy{ |
| 103 | + Strategy: UpdateAllAtOnce, |
| 104 | + Gate: &GateWorkflowConfig{ |
| 105 | + WorkflowType: "my-gate", |
| 106 | + InputFrom: &GateInputSource{ |
| 107 | + ConfigMapKeyRef: &corev1.ConfigMapKeySelector{ |
| 108 | + LocalObjectReference: corev1.LocalObjectReference{Name: "my-cm"}, |
| 109 | + Key: "key", |
| 110 | + }, |
| 111 | + SecretKeyRef: &corev1.SecretKeySelector{ |
| 112 | + LocalObjectReference: corev1.LocalObjectReference{Name: "my-secret"}, |
| 113 | + Key: "key", |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + } |
| 118 | + err := k8sClient.Create(ctx, twd) |
| 119 | + Expect(err).To(HaveOccurred()) |
| 120 | + Expect(err.Error()).To(ContainSubstring("exactly one of configMapKeyRef or secretKeyRef must be set")) |
| 121 | + }) |
| 122 | +}) |
0 commit comments