Skip to content

Commit

Permalink
Make tests reliable
Browse files Browse the repository at this point in the history
  • Loading branch information
Gchbg committed May 13, 2024
1 parent 1b7f0d7 commit aba8295
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 174 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ manifests: ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefin
@go run sigs.k8s.io/controller-tools/cmd/controller-gen rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases

.PHONY: generate
generate: ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
generate: manifests fmt vet ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
@go run sigs.k8s.io/controller-tools/cmd/controller-gen object:headerFile=".reuse/boilerplate.go.txt" paths="./..."
@internal/tools/generate.sh

Expand All @@ -49,8 +49,8 @@ vet: ## Run go vet against code.
@go vet ./...

.PHONY: test
test: manifests generate fmt vet ## Run tests.
@go run github.com/onsi/ginkgo/v2/ginkgo -r --race --randomize-suites --keep-going --randomize-all --repeat=1
test: ## Run tests.
@go run github.com/onsi/ginkgo/v2/ginkgo -r --race --randomize-suites --keep-going --randomize-all --repeat=11

.PHONY: lint
lint: ## Run golangci-lint linter & yamllint.
Expand All @@ -67,7 +67,7 @@ checklicense: ## Check that every file has a license header present.
##@ Build

.PHONY: build
build: manifests generate fmt vet ## Build manager binary.
build: generate ## Build manager binary.
@go build -o metal cmd/main.go

.PHONY: docker-build
Expand Down
60 changes: 33 additions & 27 deletions internal/controller/machineclaim_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
package controller

import (
"time"

"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
. "sigs.k8s.io/controller-runtime/pkg/envtest/komega"

metalv1alpha1 "github.com/ironcore-dev/metal/api/v1alpha1"
Expand All @@ -25,7 +28,36 @@ var _ = Describe("MachineClaim Controller", Serial, func() {
},
}
Expect(k8sClient.Create(ctx, ns)).To(Succeed())
DeferCleanup(k8sClient.Delete, ns)
DeferCleanup(func(ctx SpecContext) {
Expect(k8sClient.Delete(ctx, ns)).To(Succeed())
})

Eventually(ObjectList(&metalv1alpha1.MachineList{})).Should(HaveField("Items", HaveLen(0)))
Eventually(ObjectList(&metalv1alpha1.MachineClaimList{}, &client.ListOptions{
Namespace: ns.Name,
})).Should(HaveField("Items", HaveLen(0)))

DeferCleanup(func(ctx SpecContext) {
Eventually(ctx, func(g Gomega, ctx SpecContext) {
var machines metalv1alpha1.MachineList
g.Expect(ObjectList(&machines)()).To(SatisfyAll())
if len(machines.Items) > 0 {
g.Expect(k8sClient.DeleteAllOf(ctx, &machines.Items[0])).To(Succeed())
}
var claims metalv1alpha1.MachineClaimList
g.Expect(ObjectList(&claims)()).To(SatisfyAll())
if len(claims.Items) > 0 {
g.Expect(k8sClient.DeleteAllOf(ctx, &claims.Items[0], &client.DeleteAllOfOptions{
ListOptions: client.ListOptions{
Namespace: ns.Name,
},
})).To(Succeed())
}

g.Expect(ObjectList(&machines)()).To(HaveField("Items", BeEmpty()))
g.Expect(ObjectList(&claims)()).To(HaveField("Items", BeEmpty()))
}, time.Second*3).Should(Succeed())
})
})

It("should claim a Machine by ref", func(ctx SpecContext) {
Expand All @@ -42,10 +74,6 @@ var _ = Describe("MachineClaim Controller", Serial, func() {
},
}
Expect(k8sClient.Create(ctx, machine)).To(Succeed())
DeferCleanup(func(ctx SpecContext) {
Expect(k8sClient.Delete(ctx, machine)).To(Succeed())
Eventually(Get(machine)).Should(Satisfy(errors.IsNotFound))
})

By("Patching Machine state to Ready")
Eventually(UpdateStatus(machine, func() {
Expand Down Expand Up @@ -112,10 +140,6 @@ var _ = Describe("MachineClaim Controller", Serial, func() {
},
}
Expect(k8sClient.Create(ctx, machine)).To(Succeed())
DeferCleanup(func(ctx SpecContext) {
Expect(k8sClient.Delete(ctx, machine)).To(Succeed())
Eventually(Get(machine)).Should(Satisfy(errors.IsNotFound))
})

By("Patching Machine state to Ready")
Eventually(UpdateStatus(machine, func() {
Expand Down Expand Up @@ -190,12 +214,6 @@ var _ = Describe("MachineClaim Controller", Serial, func() {
HaveField("Finalizers", ContainElement(MachineClaimFinalizer)),
HaveField("Status.Phase", metalv1alpha1.MachineClaimPhaseUnbound),
))

By("Deleting the MachineClaim")
Expect(k8sClient.Delete(ctx, claim)).To(Succeed())

By("Expecting MachineClaim to be removed")
Eventually(Get(claim)).Should(Satisfy(errors.IsNotFound))
})

It("should not claim a Machine with no matching selector", func(ctx SpecContext) {
Expand All @@ -215,10 +233,6 @@ var _ = Describe("MachineClaim Controller", Serial, func() {
},
}
Expect(k8sClient.Create(ctx, machine)).To(Succeed())
DeferCleanup(func(ctx SpecContext) {
Expect(k8sClient.Delete(ctx, machine)).To(Succeed())
Eventually(Get(machine)).Should(Satisfy(errors.IsNotFound))
})

By("Patching Machine state to Ready")
Eventually(UpdateStatus(machine, func() {
Expand Down Expand Up @@ -276,10 +290,6 @@ var _ = Describe("MachineClaim Controller", Serial, func() {
},
}
Expect(k8sClient.Create(ctx, machine)).To(Succeed())
DeferCleanup(func(ctx SpecContext) {
Expect(k8sClient.Delete(ctx, machine)).To(Succeed())
Eventually(Get(machine)).Should(Satisfy(errors.IsNotFound))
})

By("Patching Machine state to Error")
Eventually(UpdateStatus(machine, func() {
Expand All @@ -301,10 +311,6 @@ var _ = Describe("MachineClaim Controller", Serial, func() {
},
}
Expect(k8sClient.Create(ctx, claim)).To(Succeed())
DeferCleanup(func(ctx SpecContext) {
Expect(k8sClient.Delete(ctx, claim)).To(Succeed())
Eventually(Get(claim)).Should(Satisfy(errors.IsNotFound))
})

By("Expecting finalizer and phase to be correct on the MachineClaim")
Eventually(Object(claim)).Should(SatisfyAll(
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/oob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func (r *OOBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(fmt.Errorf("cannot get OOB: %w", err))
}
log.Info(ctx, "OOB", "object", oob)

if !oob.DeletionTimestamp.IsZero() {
return ctrl.Result{}, r.finalize(ctx, &oob)
Expand Down Expand Up @@ -415,7 +416,7 @@ func (r *OOBReconciler) runPhase(ctx context.Context, oob *metalv1alpha1.OOB, ph
func (r *OOBReconciler) setCondition(ctx context.Context, oob *metalv1alpha1.OOB, apply *metalv1alpha1apply.OOBApplyConfiguration, status *metalv1alpha1apply.OOBStatusApplyConfiguration, state metalv1alpha1.OOBState, cond metav1.Condition) (context.Context, *metalv1alpha1apply.OOBApplyConfiguration, *metalv1alpha1apply.OOBStatusApplyConfiguration, error) {
conds, mod := ssa.SetCondition(oob.Status.Conditions, cond)
if oob.Status.State != state || mod {
log.Debug(ctx, "Setting condition", "reason", cond.Reason)
log.Debug(ctx, "Setting condition", "type", cond.Type, "status", cond.Status, "reason", cond.Reason)
if status == nil {
applyst, err := metalv1alpha1apply.ExtractOOBStatus(oob, OOBFieldManager)
if err != nil {
Expand Down
Loading

0 comments on commit aba8295

Please sign in to comment.