|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/go-logr/logr" |
| 8 | + . "github.com/onsi/ginkgo/v2" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | + console "github.com/pluralsh/console-client-go" |
| 11 | + "github.com/pluralsh/deployment-operator/api/v1alpha1" |
| 12 | + "github.com/pluralsh/deployment-operator/pkg/client" |
| 13 | + "github.com/pluralsh/deployment-operator/pkg/test/common" |
| 14 | + "github.com/pluralsh/deployment-operator/pkg/test/mocks" |
| 15 | + "github.com/samber/lo" |
| 16 | + "github.com/stretchr/testify/mock" |
| 17 | + batchv1 "k8s.io/api/batch/v1" |
| 18 | + corev1 "k8s.io/api/core/v1" |
| 19 | + "k8s.io/apimachinery/pkg/api/errors" |
| 20 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 21 | + "k8s.io/apimachinery/pkg/types" |
| 22 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 23 | +) |
| 24 | + |
| 25 | +var _ = Describe("PipelineGate Controller", Ordered, func() { |
| 26 | + Context("When reconciling a resource", func() { |
| 27 | + const ( |
| 28 | + gateName = "gate-test" |
| 29 | + namespace = "default" |
| 30 | + id = "123" |
| 31 | + raw = `{"backoffLimit":4,"template":{"metadata":{"namespace":"default","creationTimestamp":null},"spec":{"containers":[{"name":"pi","image":"perl:5.34.0","command":["perl","-Mbignum=bpi","-wle","print bpi(2000)"],"resources":{}}],"restartPolicy":"Never"}}}` |
| 32 | + ) |
| 33 | + |
| 34 | + gateCache := client.NewCache[console.PipelineGateFragment](time.Second, func(id string) (*console.PipelineGateFragment, error) { |
| 35 | + return &console.PipelineGateFragment{ |
| 36 | + ID: id, |
| 37 | + Name: "test", |
| 38 | + Spec: &console.GateSpecFragment{ |
| 39 | + Job: &console.JobSpecFragment{ |
| 40 | + Namespace: namespace, |
| 41 | + Raw: lo.ToPtr(raw), |
| 42 | + }, |
| 43 | + }, |
| 44 | + Status: nil, |
| 45 | + }, nil |
| 46 | + }) |
| 47 | + |
| 48 | + ctx := context.Background() |
| 49 | + gateNamespacedName := types.NamespacedName{Name: gateName, Namespace: namespace} |
| 50 | + pipelineGate := &v1alpha1.PipelineGate{} |
| 51 | + |
| 52 | + BeforeAll(func() { |
| 53 | + By("Creating pipeline gate") |
| 54 | + err := kClient.Get(ctx, gateNamespacedName, pipelineGate) |
| 55 | + if err != nil && errors.IsNotFound(err) { |
| 56 | + resource := &v1alpha1.PipelineGate{ |
| 57 | + ObjectMeta: metav1.ObjectMeta{ |
| 58 | + Name: gateName, |
| 59 | + Namespace: namespace, |
| 60 | + }, |
| 61 | + Spec: v1alpha1.PipelineGateSpec{ |
| 62 | + ID: id, |
| 63 | + Name: "test", |
| 64 | + Type: v1alpha1.GateType(console.GateTypeJob), |
| 65 | + GateSpec: &v1alpha1.GateSpec{ |
| 66 | + JobSpec: &batchv1.JobSpec{ |
| 67 | + Template: corev1.PodTemplateSpec{ |
| 68 | + ObjectMeta: metav1.ObjectMeta{}, |
| 69 | + Spec: corev1.PodSpec{ |
| 70 | + Containers: []corev1.Container{ |
| 71 | + { |
| 72 | + Name: "image1", |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | + Expect(kClient.Create(ctx, resource)).To(Succeed()) |
| 82 | + } |
| 83 | + |
| 84 | + }) |
| 85 | + |
| 86 | + It("should set state pending", func() { |
| 87 | + fakeConsoleClient := mocks.NewClientMock(mocks.TestingT) |
| 88 | + fakeConsoleClient.On("UpdateGate", mock.Anything, mock.Anything).Return(nil) |
| 89 | + reconciler := &PipelineGateReconciler{ |
| 90 | + Client: kClient, |
| 91 | + ConsoleClient: fakeConsoleClient, |
| 92 | + GateCache: gateCache, |
| 93 | + Scheme: kClient.Scheme(), |
| 94 | + Log: logr.Logger{}, |
| 95 | + } |
| 96 | + _, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: gateNamespacedName}) |
| 97 | + Expect(err).NotTo(HaveOccurred()) |
| 98 | + |
| 99 | + existingGate := &v1alpha1.PipelineGate{} |
| 100 | + Expect(kClient.Get(ctx, gateNamespacedName, existingGate)).NotTo(HaveOccurred()) |
| 101 | + Expect(*existingGate.Status.State).Should(Equal(v1alpha1.GateState(console.GateStatePending))) |
| 102 | + |
| 103 | + }) |
| 104 | + |
| 105 | + It("should reconcile Pending Gate", func() { |
| 106 | + fakeConsoleClient := mocks.NewClientMock(mocks.TestingT) |
| 107 | + fakeConsoleClient.On("UpdateGate", mock.Anything, mock.Anything).Return(nil) |
| 108 | + reconciler := &PipelineGateReconciler{ |
| 109 | + Client: kClient, |
| 110 | + ConsoleClient: fakeConsoleClient, |
| 111 | + GateCache: gateCache, |
| 112 | + Scheme: kClient.Scheme(), |
| 113 | + Log: logr.Logger{}, |
| 114 | + } |
| 115 | + _, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: gateNamespacedName}) |
| 116 | + Expect(err).NotTo(HaveOccurred()) |
| 117 | + |
| 118 | + existingGate := &v1alpha1.PipelineGate{} |
| 119 | + Expect(kClient.Get(ctx, gateNamespacedName, existingGate)).NotTo(HaveOccurred()) |
| 120 | + Expect(*existingGate.Status.State).Should(Equal(v1alpha1.GateState(console.GateStateRunning))) |
| 121 | + existingJob := &batchv1.Job{} |
| 122 | + Expect(kClient.Get(ctx, gateNamespacedName, existingJob)).NotTo(HaveOccurred()) |
| 123 | + }) |
| 124 | + |
| 125 | + It("should open Gate", func() { |
| 126 | + fakeConsoleClient := mocks.NewClientMock(mocks.TestingT) |
| 127 | + fakeConsoleClient.On("UpdateGate", mock.Anything, mock.Anything).Return(nil) |
| 128 | + reconciler := &PipelineGateReconciler{ |
| 129 | + Client: kClient, |
| 130 | + ConsoleClient: fakeConsoleClient, |
| 131 | + GateCache: gateCache, |
| 132 | + Scheme: kClient.Scheme(), |
| 133 | + Log: logr.Logger{}, |
| 134 | + } |
| 135 | + |
| 136 | + existingJob := &batchv1.Job{} |
| 137 | + Expect(kClient.Get(ctx, gateNamespacedName, existingJob)).NotTo(HaveOccurred()) |
| 138 | + |
| 139 | + Expect(common.MaybePatch(kClient, existingJob, |
| 140 | + func(p *batchv1.Job) { |
| 141 | + p.Status.Conditions = []batchv1.JobCondition{ |
| 142 | + { |
| 143 | + Type: batchv1.JobComplete, |
| 144 | + Status: corev1.ConditionTrue, |
| 145 | + }, |
| 146 | + } |
| 147 | + })).To(Succeed()) |
| 148 | + |
| 149 | + _, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: gateNamespacedName}) |
| 150 | + Expect(err).NotTo(HaveOccurred()) |
| 151 | + |
| 152 | + existingGate := &v1alpha1.PipelineGate{} |
| 153 | + Expect(kClient.Get(ctx, gateNamespacedName, existingGate)).NotTo(HaveOccurred()) |
| 154 | + Expect(*existingGate.Status.State).Should(Equal(v1alpha1.GateState(console.GateStateOpen))) |
| 155 | + |
| 156 | + Expect(kClient.Delete(ctx, existingGate)).To(Succeed()) |
| 157 | + Expect(kClient.Delete(ctx, existingJob)).To(Succeed()) |
| 158 | + }) |
| 159 | + }) |
| 160 | +}) |
0 commit comments