Skip to content

Commit

Permalink
Restore ShipwrightBuild controller unit tests
Browse files Browse the repository at this point in the history
Update the unit tests to account for the new logic that installs cluster
build strategies. Noting that code may need a refactor due the use of
separate client interfaces.
  • Loading branch information
adambkaplan committed Nov 6, 2023
1 parent 0b845cd commit b72656a
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions controllers/shipwrightbuild_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,35 +155,45 @@ func testShipwrightBuildReconcilerReconcile(t *testing.T, targetNamespace string
// rolling out all manifests on the desired namespace, making sure the deployment for Shipwright
// Build Controller is created accordingly
t.Run("rollout-manifests", func(t *testing.T) {
t.Skip("TEMP: Skipping due to fake clients not talking to each other")
ctx := context.TODO()
res, err := r.Reconcile(ctx, req)
g.Expect(err).To(o.BeNil())
g.Expect(res.Requeue).To(o.BeFalse())
// TODO: Code technically uses two different clientsets that don't talk to each other.
// This makes testing brittle and unable to capture the behavior on a real cluster.
// Requeue can return "true" because the tests think the CRD for ClusterBuildStrategies
// do not exist yet.
g.Expect(res.Requeue).To(o.BeTrue(), "checking requeue for Reconcile")
err = c.Get(ctx, deploymentName, &appsv1.Deployment{})
g.Expect(err).To(o.BeNil())
err = c.Get(ctx, namespacedName, b)
g.Expect(err).To(o.BeNil())
g.Expect(b.Status.IsReady()).To(o.BeTrue())
// Likewise, the ShipwrightBuild object will not report itself ready because it is waiting
// for the ClusterBuildStrategy CRD to be created first.
g.Expect(b.Status.IsReady()).To(o.BeFalse(), "checking ShipwrightBuild readiness")
})

t.Run("rollout-manifests-with-images-env-vars", func(t *testing.T) {
t.Skip("TEMP: Skipping due to fake clients not talking to each other")
ctx := context.TODO()
for _, v := range images {
t.Setenv(v.key, v.value)
}
deployment := &appsv1.Deployment{}
res, err := r.Reconcile(ctx, req)
g.Expect(err).To(o.BeNil())
g.Expect(res.Requeue).To(o.BeFalse())
// TODO: Code technically uses two different clientsets that don't talk to each other.
// This makes testing brittle and unable to capture the behavior on a real cluster.
// Requeue can return "true" because the tests think the CRD for ClusterBuildStrategies
// do not exist yet.
g.Expect(res.Requeue).To(o.BeTrue())
err = c.Get(ctx, deploymentName, deployment)
g.Expect(err).To(o.BeNil())
containers := deployment.Spec.Template.Spec.Containers
g.Expect(containers[0].Image).To(o.Equal("ghcr.io/shipwright-io/build/shipwright-build-controller:nightly-2023-05-05-1683263383"))
err = c.Get(ctx, namespacedName, b)
g.Expect(err).To(o.BeNil())
g.Expect(b.Status.IsReady()).To(o.BeTrue())
// Likewise, the ShipwrightBuild object will not report itself ready because it is waiting
// for the ClusterBuildStrategy CRD to be created first.
g.Expect(b.Status.IsReady()).To(o.BeFalse())
})

// rolling back all changes, making sure the main deployment is also not found afterwards
Expand All @@ -195,6 +205,8 @@ func testShipwrightBuildReconcilerReconcile(t *testing.T, targetNamespace string

// setting a deletion timestemp on the build object, it triggers the rollback logic so the
// reconciliation should remove the objects previously deployed

// TODO: Refactor to use owner references so the rollback is handled by Kubernetes itself.
b.SetDeletionTimestamp(&metav1.Time{Time: time.Now()})
err = r.Update(ctx, b, &client.UpdateOptions{})
g.Expect(err).To(o.BeNil())
Expand Down Expand Up @@ -223,9 +235,6 @@ func TestShipwrightBuildReconciler_Reconcile(t *testing.T) {

for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
if tt.testName == "target namespace is informed" {
t.Skip("TEMP: skip due to fake clients not talking to each other")
}
testShipwrightBuildReconcilerReconcile(t, tt.targetNamespace)
})
}
Expand Down

0 comments on commit b72656a

Please sign in to comment.