Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions controllers/controllers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,20 +405,6 @@ func getDefaultHetznerBareMetalMachineSpec() infrav1.HetznerBareMetalMachineSpec
}
}

func isPresentAndFalseWithReason(key types.NamespacedName, getter conditions.Getter, condition clusterv1.ConditionType, reason string) bool {
err := testEnv.Get(ctx, key, getter)
if err != nil {
return false
}

if !conditions.Has(getter, condition) {
return false
}
objectCondition := conditions.Get(getter, condition)
return objectCondition.Status == corev1.ConditionFalse &&
objectCondition.Reason == reason
}

func isPresentAndTrue(key types.NamespacedName, getter conditions.Getter, condition clusterv1.ConditionType) bool {
err := testEnv.Get(ctx, key, getter)
if err != nil {
Expand Down
40 changes: 31 additions & 9 deletions controllers/hcloudmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
infrav1 "github.com/syself/cluster-api-provider-hetzner/api/v1beta1"
hcloudclient "github.com/syself/cluster-api-provider-hetzner/pkg/services/hcloud/client"
"github.com/syself/cluster-api-provider-hetzner/pkg/utils"
"github.com/syself/cluster-api-provider-hetzner/test/helpers"
)

func TestIgnoreInsignificantHCloudMachineStatusUpdates(t *testing.T) {
Expand Down Expand Up @@ -513,9 +514,16 @@ var _ = Describe("HCloudMachineReconciler", func() {
})

It("checks that ImageNotFound is visible in conditions if image does not exist", func() {
Eventually(func() bool {
return isPresentAndFalseWithReason(key, hcloudMachine, infrav1.ServerCreateSucceededCondition, infrav1.ImageNotFoundReason)
}, timeout, interval).Should(BeTrue())
Eventually(func() error {
return helpers.ConditionFalseWithReasonAtKey(
ctx,
testEnv,
key,
hcloudMachine,
infrav1.ServerCreateSucceededCondition,
infrav1.ImageNotFoundReason,
)
}, timeout, interval).Should(Succeed())
})
})
})
Expand Down Expand Up @@ -620,9 +628,16 @@ var _ = Describe("HCloudMachineReconciler", func() {
})

It("should show the expected reason for server not created", func() {
Eventually(func() bool {
return isPresentAndFalseWithReason(key, hcloudMachine, infrav1.ServerCreateSucceededCondition, infrav1.InstanceHasNonExistingPlacementGroupReason)
}, timeout).Should(BeTrue())
Eventually(func() error {
return helpers.ConditionFalseWithReasonAtKey(
ctx,
testEnv,
key,
hcloudMachine,
infrav1.ServerCreateSucceededCondition,
infrav1.InstanceHasNonExistingPlacementGroupReason,
)
}, timeout).Should(Succeed())
})
})

Expand Down Expand Up @@ -795,9 +810,16 @@ var _ = Describe("Hetzner secret", func() {
hetznerSecret = secretFunc()
Expect(testEnv.Create(ctx, hetznerSecret)).To(Succeed())

Eventually(func() bool {
return isPresentAndFalseWithReason(key, hcloudMachine, infrav1.HCloudTokenAvailableCondition, expectedReason)
}, timeout, interval).Should(BeTrue())
Eventually(func() error {
return helpers.ConditionFalseWithReasonAtKey(
ctx,
testEnv,
key,
hcloudMachine,
infrav1.HCloudTokenAvailableCondition,
expectedReason,
)
}, timeout, interval).Should(Succeed())
Expect(testEnv.Cleanup(ctx, hetznerSecret)).To(Succeed())
},
Entry("no Hetzner secret/wrong reference", func() *corev1.Secret {
Expand Down
41 changes: 29 additions & 12 deletions controllers/hcloudremediation_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/syself/cluster-api-provider-hetzner/pkg/scope"
hcloudutil "github.com/syself/cluster-api-provider-hetzner/pkg/services/hcloud/util"
"github.com/syself/cluster-api-provider-hetzner/pkg/utils"
"github.com/syself/cluster-api-provider-hetzner/test/helpers"
)

var _ = Describe("HCloudRemediationReconciler", func() {
Expand Down Expand Up @@ -236,10 +237,14 @@ var _ = Describe("HCloudRemediationReconciler", func() {
if hcloudRemediation.Status.Phase != infrav1.PhaseDeleting {
return fmt.Errorf("hcloudRemediation.Status.Phase is not infrav1.PhaseDeleting")
}
if !isPresentAndFalseWithReason(capiMachineKey, capiMachine, clusterv1.MachineOwnerRemediatedCondition, clusterv1.WaitingForRemediationReason) {
return fmt.Errorf("MachineOwnerRemediatedCondition not set")
}
return nil
return helpers.ConditionFalseWithReasonAtKey(
ctx,
testEnv,
capiMachineKey,
capiMachine,
clusterv1.MachineOwnerRemediatedCondition,
clusterv1.WaitingForRemediationReason,
)
}, timeout).Should(Succeed())
})

Expand Down Expand Up @@ -320,15 +325,25 @@ var _ = Describe("HCloudRemediationReconciler", func() {
Expect(hcloudRemediationPatchHelper.Patch(ctx, hcloudRemediation)).NotTo(HaveOccurred())

By("checking if hcloudRemediation is in deleting phase and capiMachine has MachineOwnerRemediatedCondition")
Eventually(func() bool {
Eventually(func() error {
if err := testEnv.Get(ctx, hcloudRemediationkey, hcloudRemediation); err != nil {
return false
return err
}

testEnv.GetLogger().Info("status of hcloudRemediation", "status", hcloudRemediation.Status.Phase)
return hcloudRemediation.Status.Phase == infrav1.PhaseDeleting &&
isPresentAndFalseWithReason(capiMachineKey, capiMachine, clusterv1.MachineOwnerRemediatedCondition, clusterv1.WaitingForRemediationReason)
}, timeout).Should(BeTrue())
if hcloudRemediation.Status.Phase != infrav1.PhaseDeleting {
return fmt.Errorf("hcloudRemediation.Status.Phase is %q", hcloudRemediation.Status.Phase)
}

return helpers.ConditionFalseWithReasonAtKey(
ctx,
testEnv,
capiMachineKey,
capiMachine,
clusterv1.MachineOwnerRemediatedCondition,
clusterv1.WaitingForRemediationReason,
)
}, timeout).Should(Succeed())
})
It("should set RemediationSkippedCondition when HCloudMachine has irrecoverable server creation failure", func() {
By("waiting for HCloudMachine to be fully provisioned")
Expand Down Expand Up @@ -359,14 +374,16 @@ var _ = Describe("HCloudRemediationReconciler", func() {
Expect(testEnv.Create(ctx, hcloudRemediation)).To(Succeed())

By("checking that RemediationSkippedCondition is set with IrrecoverableServerCreateFailureReason")
Eventually(func() bool {
return isPresentAndFalseWithReason(
Eventually(func() error {
return helpers.ConditionFalseWithReasonAtKey(
ctx,
testEnv,
hcloudRemediationkey,
hcloudRemediation,
infrav1.RemediationSkippedCondition,
infrav1.IrrecoverableServerCreateFailureReason,
)
}, timeout).Should(BeTrue())
}, timeout).Should(Succeed())
})

It("should delete machine if SetErrorAndRemediate() was called", func() {
Expand Down
78 changes: 60 additions & 18 deletions controllers/hetznerbaremetalhost_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,9 +756,16 @@ var _ = Describe("HetznerBareMetalHostReconciler - missing secrets", func() {
})

It("gives an error", func() {
Eventually(func() bool {
return isPresentAndFalseWithReason(key, host, infrav1.CredentialsAvailableCondition, infrav1.RescueSSHSecretMissingReason)
}, timeout).Should(BeTrue())
Eventually(func() error {
return helpers.ConditionFalseWithReasonAtKey(
ctx,
testEnv,
key,
host,
infrav1.CredentialsAvailableCondition,
infrav1.RescueSSHSecretMissingReason,
)
}, timeout).Should(Succeed())
})

It("gives the right error if secret if rescue-ssh is invalid", func() {
Expand All @@ -778,9 +785,16 @@ var _ = Describe("HetznerBareMetalHostReconciler - missing secrets", func() {
Expect(testEnv.Delete(ctx, rescueSSHSecret)).To(Succeed())
}()

Eventually(func() bool {
return isPresentAndFalseWithReason(key, host, infrav1.CredentialsAvailableCondition, infrav1.SSHCredentialsInSecretInvalidReason)
}, timeout).Should(BeTrue())
Eventually(func() error {
return helpers.ConditionFalseWithReasonAtKey(
ctx,
testEnv,
key,
host,
infrav1.CredentialsAvailableCondition,
infrav1.SSHCredentialsInSecretInvalidReason,
)
}, timeout).Should(Succeed())
})
})

Expand Down Expand Up @@ -808,9 +822,16 @@ var _ = Describe("HetznerBareMetalHostReconciler - missing secrets", func() {
})

It("gives the right error if secret is missing", func() {
Eventually(func() bool {
return isPresentAndFalseWithReason(key, host, infrav1.CredentialsAvailableCondition, infrav1.OSSSHSecretMissingReason)
}, timeout).Should(BeTrue())
Eventually(func() error {
return helpers.ConditionFalseWithReasonAtKey(
ctx,
testEnv,
key,
host,
infrav1.CredentialsAvailableCondition,
infrav1.OSSSHSecretMissingReason,
)
}, timeout).Should(Succeed())
})

It("gives the right error if secret is invalid", func() {
Expand All @@ -830,9 +851,16 @@ var _ = Describe("HetznerBareMetalHostReconciler - missing secrets", func() {
Expect(testEnv.Delete(ctx, osSSHSecret)).To(Succeed())
}()

Eventually(func() bool {
return isPresentAndFalseWithReason(key, host, infrav1.CredentialsAvailableCondition, infrav1.SSHCredentialsInSecretInvalidReason)
}, timeout).Should(BeTrue())
Eventually(func() error {
return helpers.ConditionFalseWithReasonAtKey(
ctx,
testEnv,
key,
host,
infrav1.CredentialsAvailableCondition,
infrav1.SSHCredentialsInSecretInvalidReason,
)
}, timeout).Should(Succeed())
})
})

Expand Down Expand Up @@ -876,9 +904,16 @@ var _ = Describe("HetznerBareMetalHostReconciler - missing secrets", func() {

It("should set CredentialsAvailable condition to false if Robot API returned unauthorized", func() {
By("making the Robot client return an unauthorized error")
Eventually(func() bool {
return isPresentAndFalseWithReason(key, host, infrav1.RobotCredentialsAvailableCondition, infrav1.RobotCredentialsInvalidReason)
}, timeout).Should(BeTrue())
Eventually(func() error {
return helpers.ConditionFalseWithReasonAtKey(
ctx,
testEnv,
key,
host,
infrav1.RobotCredentialsAvailableCondition,
infrav1.RobotCredentialsInvalidReason,
)
}, timeout).Should(Succeed())

Expect(robotClient.AssertExpectations(GinkgoT())).To(BeTrue())
})
Expand Down Expand Up @@ -919,9 +954,16 @@ var _ = Describe("HetznerBareMetalHostReconciler - missing secrets", func() {
})

It("sets RobotCredentialsAvailable to false if robot-user is empty", func() {
Eventually(func() bool {
return isPresentAndFalseWithReason(key, host, infrav1.RobotCredentialsAvailableCondition, infrav1.RobotCredentialsInvalidReason)
}, timeout).Should(BeTrue())
Eventually(func() error {
return helpers.ConditionFalseWithReasonAtKey(
ctx,
testEnv,
key,
host,
infrav1.RobotCredentialsAvailableCondition,
infrav1.RobotCredentialsInvalidReason,
)
}, timeout).Should(Succeed())
})
})
})
Expand Down
39 changes: 30 additions & 9 deletions controllers/hetznerbaremetalmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,16 @@ var _ = Describe("HetznerBareMetalMachineReconciler", func() {
})

It("sets bootstrap condition on false if no bootstrap available", func() {
Eventually(func() bool {
return isPresentAndFalseWithReason(key, bmMachine, infrav1.BootstrapReadyCondition, infrav1.BootstrapNotReadyReason)
}, timeout, time.Second).Should(BeTrue())
Eventually(func() error {
return helpers.ConditionFalseWithReasonAtKey(
ctx,
testEnv,
key,
bmMachine,
infrav1.BootstrapReadyCondition,
infrav1.BootstrapNotReadyReason,
)
}, timeout, time.Second).Should(Succeed())
})

It("sets bootstrap condition on true if bootstrap available", func() {
Expand Down Expand Up @@ -763,9 +770,16 @@ var _ = Describe("HetznerBareMetalMachineReconciler", func() {
})

It("creates the bare metal machine and sets condition that no host is available", func() {
Eventually(func() bool {
return isPresentAndFalseWithReason(key, bmMachine, infrav1.HostAssociateSucceededCondition, infrav1.NoAvailableHostReason)
}, timeout).Should(BeTrue())
Eventually(func() error {
return helpers.ConditionFalseWithReasonAtKey(
ctx,
testEnv,
key,
bmMachine,
infrav1.HostAssociateSucceededCondition,
infrav1.NoAvailableHostReason,
)
}, timeout).Should(Succeed())
})

It("checks the hetznerBareMetalMachine status pending phase", func() {
Expand Down Expand Up @@ -864,9 +878,16 @@ var _ = Describe("HetznerBareMetalMachineReconciler", func() {
By("Deleting the host, expect HostReady condition is set to false")
Expect(testEnv.Delete(ctx, host)).To(Succeed())

Eventually(func() bool {
return isPresentAndFalseWithReason(bmmKey, bmMachine, infrav1.HostReadyCondition, infrav1.HostNotFoundReason)
}, timeout).Should(BeTrue())
Eventually(func() error {
return helpers.ConditionFalseWithReasonAtKey(
ctx,
testEnv,
bmmKey,
bmMachine,
infrav1.HostReadyCondition,
infrav1.HostNotFoundReason,
)
}, timeout).Should(Succeed())

By("ensuring remediate machine annotation is set on CAPI machine")
Expect(testEnv.Get(ctx, client.ObjectKeyFromObject(capiMachine), capiMachine)).To(Succeed())
Expand Down
Loading
Loading