Skip to content

Commit

Permalink
Add test for the condition
Browse files Browse the repository at this point in the history
Signed-off-by: Aniruddha Basak <[email protected]>
  • Loading branch information
aniruddha2000 committed Oct 4, 2023
1 parent 61e03ad commit 6ba1459
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
63 changes: 62 additions & 1 deletion controllers/hcloudmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ var _ = Describe("HCloudMachineReconciler", func() {
}, timeout).Should(BeTrue())
})

FIt("creates the HCloud machine in Hetzner", func() {
It("creates the HCloud machine in Hetzner", func() {
By("checking that no servers exist")

Eventually(func() bool {
Expand Down Expand Up @@ -254,6 +254,67 @@ var _ = Describe("HCloudMachineReconciler", func() {
}, timeout, time.Second).Should(BeTrue())
})
})

Context("correct server", func() {
BeforeEach(func() {
hcloudMachine.Spec.ImageName = "fedora-control-plane-2"

Expect(testEnv.Create(ctx, hcloudMachine)).To(Succeed())
Expect(testEnv.Create(ctx, hetznerCluster)).To(Succeed())
})

AfterEach(func() {
Expect(testEnv.Cleanup(ctx, hcloudMachine, hetznerCluster)).To(Succeed())
})

It("creates the HCloud machine in Hetzner", func() {
By("checking that no servers exist")

Eventually(func() bool {
servers, err := hcloudClient.ListServers(ctx, hcloud.ServerListOpts{
ListOpts: hcloud.ListOpts{
LabelSelector: utils.LabelsToLabelSelector(map[string]string{hetznerCluster.ClusterTagKey(): "owned"}),
},
})
if err != nil {
return false
}

return len(servers) == 0
}, timeout, time.Second).Should(BeTrue())

By("checking that bootstrap condition is not ready")

Eventually(func() bool {
return isPresentAndFalseWithReason(key, hcloudMachine, infrav1.BootstrapReadyCondition, infrav1.BootstrapNotReadyReason)
}, timeout, time.Second).Should(BeTrue())

By("setting the bootstrap data")

ph, err := patch.NewHelper(capiMachine, testEnv)
Expect(err).ShouldNot(HaveOccurred())

capiMachine.Spec.Bootstrap = clusterv1.Bootstrap{
DataSecretName: ptr.To("bootstrap-secret"),
}

Eventually(func() error {
return ph.Patch(ctx, capiMachine, patch.WithStatusObservedGeneration{})
}, timeout, time.Second).Should(BeNil())

By("checking that bootstrap condition is ready")

Eventually(func() bool {
return isPresentAndTrue(key, hcloudMachine, infrav1.BootstrapReadyCondition)
}, timeout, time.Second).Should(BeTrue())

By("checking HCloudMachine has ImageNotFoundReason")

Eventually(func() bool {
return isPresentAndFalseWithReason(key, hcloudMachine, infrav1.ServerCreateSucceededCondition, infrav1.ImageNotFoundReason)
}, timeout, time.Second).Should(BeTrue())
})
})
})

Context("various specs", func() {
Expand Down
4 changes: 4 additions & 0 deletions pkg/services/hcloud/client/fake/hcloud_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,10 @@ func (c *cacheHCloudClient) DeleteServiceFromLoadBalancer(_ context.Context, lb
}

func (c *cacheHCloudClient) ListImages(_ context.Context, opts hcloud.ImageListOpts) ([]*hcloud.Image, error) {
if opts.Name != "" {
return nil, nil
}

labels, err := utils.LabelSelectorToLabels(opts.LabelSelector)
if err != nil {
return nil, fmt.Errorf("failed to convert label selector to labels: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/hcloud/client/fake/hcloud_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ var _ = Describe("Load balancer", func() {
var listOpts hcloud.ImageListOpts
client := factory.NewClient("")
BeforeEach(func() {
listOpts.LabelSelector = labelSelector
listOpts.LabelSelector = "caph-image-name==fedora-control-plane"
})
It("lists at least one image", func() {
resp, err := client.ListImages(ctx, listOpts)
Expand Down

0 comments on commit 6ba1459

Please sign in to comment.