|
| 1 | +package buildstrategy |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + o "github.com/onsi/gomega" |
| 8 | + v1beta1 "github.com/shipwright-io/build/pkg/apis/build/v1beta1" |
| 9 | + "github.com/shipwright-io/operator/api/v1alpha1" |
| 10 | + commonctrl "github.com/shipwright-io/operator/controllers/common" |
| 11 | + appsv1 "k8s.io/api/apps/v1" |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 14 | + crdclientv1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + "k8s.io/apimachinery/pkg/runtime" |
| 17 | + "k8s.io/apimachinery/pkg/types" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 20 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 21 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 22 | +) |
| 23 | + |
| 24 | +// bootstrapBuildStrategyReconciler start up a new instance of BuildStrategyReconciler which is |
| 25 | +// ready to interact with Manifestival, returning the Manifestival instance and the client. |
| 26 | +func bootstrapBuildStrategyReconciler( |
| 27 | + t *testing.T, |
| 28 | + b *v1alpha1.ShipwrightBuild, |
| 29 | + tcrds []*crdv1.CustomResourceDefinition, |
| 30 | +) (client.Client, *crdclientv1.Clientset, *BuildStrategyReconciler) { |
| 31 | + g := o.NewGomegaWithT(t) |
| 32 | + |
| 33 | + s := runtime.NewScheme() |
| 34 | + s.AddKnownTypes(corev1.SchemeGroupVersion, &corev1.Namespace{}) |
| 35 | + s.AddKnownTypes(appsv1.SchemeGroupVersion, &appsv1.Deployment{}) |
| 36 | + s.AddKnownTypes(v1beta1.SchemeGroupVersion, &v1beta1.ClusterBuildStrategy{}) |
| 37 | + s.AddKnownTypes(v1beta1.SchemeGroupVersion, &v1beta1.BuildStrategy{}) |
| 38 | + s.AddKnownTypes(v1alpha1.GroupVersion, &v1alpha1.ShipwrightBuild{}) |
| 39 | + |
| 40 | + logger := zap.New() |
| 41 | + |
| 42 | + // create fake webhook deployment(prerequisite of build strategy installation) |
| 43 | + webhookdep := &appsv1.Deployment{ |
| 44 | + ObjectMeta: metav1.ObjectMeta{Namespace: b.Spec.TargetNamespace, Name: "shp-build-webhook"}, |
| 45 | + Status: appsv1.DeploymentStatus{ |
| 46 | + Conditions: []appsv1.DeploymentCondition{{ |
| 47 | + Type: appsv1.DeploymentAvailable, |
| 48 | + Status: corev1.ConditionTrue, |
| 49 | + }}, |
| 50 | + }, |
| 51 | + } |
| 52 | + c := fake.NewClientBuilder().WithScheme(s).WithObjects(b, webhookdep).WithStatusSubresource(b, webhookdep).Build() |
| 53 | + |
| 54 | + var crdClient *crdclientv1.Clientset |
| 55 | + if len(tcrds) > 0 { |
| 56 | + objs := []runtime.Object{} |
| 57 | + for _, obj := range tcrds { |
| 58 | + objs = append(objs, obj) |
| 59 | + } |
| 60 | + crdClient = crdclientv1.NewSimpleClientset(objs...) |
| 61 | + } else { |
| 62 | + crdClient = crdclientv1.NewSimpleClientset() |
| 63 | + } |
| 64 | + |
| 65 | + r := &BuildStrategyReconciler{CRDClient: crdClient.ApiextensionsV1(), Client: c, Scheme: s, Logger: logger} |
| 66 | + |
| 67 | + if b.Spec.TargetNamespace != "" { |
| 68 | + t.Logf("Creating test namespace '%s'", b.Spec.TargetNamespace) |
| 69 | + t.Run("create-test-namespace", func(t *testing.T) { |
| 70 | + err := c.Create( |
| 71 | + context.TODO(), |
| 72 | + &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: b.Spec.TargetNamespace}}, |
| 73 | + &client.CreateOptions{}, |
| 74 | + ) |
| 75 | + g.Expect(err).To(o.BeNil()) |
| 76 | + }) |
| 77 | + } |
| 78 | + |
| 79 | + return c, crdClient, r |
| 80 | +} |
| 81 | + |
| 82 | +// testBuildStrategyReconcilerReconcile simulates the reconciliation process for rolling out and |
| 83 | +// rolling back manifests in the informed target namespace name. |
| 84 | +func testBuildStrategyReconcilerReconcile(t *testing.T, targetNamespace string) { |
| 85 | + g := o.NewGomegaWithT(t) |
| 86 | + |
| 87 | + namespacedName := types.NamespacedName{Namespace: "default", Name: "name"} |
| 88 | + clsName := types.NamespacedName{ |
| 89 | + Name: "kaniko", |
| 90 | + } |
| 91 | + |
| 92 | + req := reconcile.Request{NamespacedName: namespacedName} |
| 93 | + |
| 94 | + b := &v1alpha1.ShipwrightBuild{ |
| 95 | + ObjectMeta: metav1.ObjectMeta{ |
| 96 | + Name: namespacedName.Name, |
| 97 | + Namespace: namespacedName.Namespace, |
| 98 | + }, |
| 99 | + Spec: v1alpha1.ShipwrightBuildSpec{ |
| 100 | + TargetNamespace: targetNamespace, |
| 101 | + }, |
| 102 | + } |
| 103 | + crd1 := &crdv1.CustomResourceDefinition{} |
| 104 | + crd1.Name = "clusterbuildstrategies.shipwright.io" |
| 105 | + crd2 := &crdv1.CustomResourceDefinition{} |
| 106 | + crd2.Name = "buildstrategies.shipwright.io" |
| 107 | + crds := []*crdv1.CustomResourceDefinition{crd1, crd2} |
| 108 | + _, _, r := bootstrapBuildStrategyReconciler(t, b, crds) |
| 109 | + |
| 110 | + t.Logf("Deploying BuildStrategy Controller against '%s' namespace", targetNamespace) |
| 111 | + |
| 112 | + // rolling out all manifests on the desired namespace, making sure the build cluster strategies are created |
| 113 | + t.Run("rollout-manifests", func(t *testing.T) { |
| 114 | + ctx := context.TODO() |
| 115 | + res, err := r.Reconcile(ctx, req) |
| 116 | + g.Expect(err).To(o.BeNil()) |
| 117 | + g.Expect(res.Requeue).To(o.BeFalse()) |
| 118 | + err = r.Get(ctx, clsName, &v1beta1.ClusterBuildStrategy{}) |
| 119 | + g.Expect(err).To(o.BeNil()) |
| 120 | + }) |
| 121 | + |
| 122 | + // rolling back all changes, making sure the build cluster strategies are also not found afterwards |
| 123 | + t.Run("rollback-manifests", func(t *testing.T) { |
| 124 | + ctx := context.TODO() |
| 125 | + |
| 126 | + err := r.Get(ctx, namespacedName, b) |
| 127 | + g.Expect(err).To(o.BeNil()) |
| 128 | + |
| 129 | + /*b.SetDeletionTimestamp(&metav1.Time{Time: time.Now()}) |
| 130 | + err = r.Update(ctx, b, &client.UpdateOptions{Raw: &metav1.UpdateOptions{}}) |
| 131 | + g.Expect(err).To(o.BeNil()) |
| 132 | +
|
| 133 | + res, err := r.Reconcile(ctx, req) |
| 134 | + g.Expect(err).To(o.BeNil()) |
| 135 | + g.Expect(res.Requeue).To(o.BeFalse()) |
| 136 | +
|
| 137 | + err = r.Get(ctx, clsName, &v1beta1.ClusterBuildStrategy{}) |
| 138 | + g.Expect(errors.IsNotFound(err)).To(o.BeTrue())*/ |
| 139 | + }) |
| 140 | +} |
| 141 | + |
| 142 | +// TestBuildStrategyReconciler_Reconcile runs rollout/rollback tests against different namespaces. |
| 143 | +func TestBuildStrategyReconciler_Reconcile(t *testing.T) { |
| 144 | + tests := []struct { |
| 145 | + testName string |
| 146 | + targetNamespace string |
| 147 | + }{{ |
| 148 | + testName: "target namespace is informed", |
| 149 | + targetNamespace: "namespace", |
| 150 | + }, { |
| 151 | + testName: "target namespace is not informed", |
| 152 | + targetNamespace: commonctrl.DefaultTargetNamespace, |
| 153 | + }} |
| 154 | + |
| 155 | + for _, tt := range tests { |
| 156 | + t.Run(tt.testName, func(t *testing.T) { |
| 157 | + testBuildStrategyReconcilerReconcile(t, tt.targetNamespace) |
| 158 | + }) |
| 159 | + } |
| 160 | +} |
0 commit comments