Skip to content

Commit

Permalink
Add test for hetzner baremetal remediation controller
Browse files Browse the repository at this point in the history
Signed-off-by: Aniruddha Basak <aniruddha.basak@syself.com>
  • Loading branch information
aniruddha2000 committed Oct 8, 2023
1 parent 5210043 commit 5226d1d
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 7 deletions.
17 changes: 10 additions & 7 deletions controllers/hcloudremediation_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ package controllers
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
infrav1 "github.com/syself/cluster-api-provider-hetzner/api/v1beta1"
"github.com/syself/cluster-api-provider-hetzner/pkg/utils"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"

infrav1 "github.com/syself/cluster-api-provider-hetzner/api/v1beta1"
"github.com/syself/cluster-api-provider-hetzner/pkg/utils"
)

var _ = Describe("HCloudRemediationReconciler", func() {
var (
hcloudRemediation *infrav1.HCloudRemediation
hcloudMachine *infrav1.HCloudMachine
hetznerCluster *infrav1.HetznerCluster
capiMachine *clusterv1.Machine
capiCluster *clusterv1.Cluster
testNs *corev1.Namespace
key client.ObjectKey

capiMachine *clusterv1.Machine
capiCluster *clusterv1.Cluster

testNs *corev1.Namespace
key client.ObjectKey
)

BeforeEach(func() {
Expand Down Expand Up @@ -145,7 +148,7 @@ var _ = Describe("HCloudRemediationReconciler", func() {
})

Context("Basic test", func() {
FIt("creates the hcloud remediation successfully", func() {
It("creates the hcloudRemediation successfully", func() {
Eventually(func() bool {
if err := testEnv.Get(ctx, key, hcloudRemediation); err != nil {
return false
Expand Down
170 changes: 170 additions & 0 deletions controllers/hetznerbaremetalremediation_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package controllers

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"

infrav1 "github.com/syself/cluster-api-provider-hetzner/api/v1beta1"
"github.com/syself/cluster-api-provider-hetzner/pkg/utils"
)

var _ = Describe("HetznerBareMetalRemediationReconciler", func() {
var (
hetznerBareMetalRemediation *infrav1.HetznerBareMetalRemediation
hcloudMachine *infrav1.HCloudMachine
hetznerCluster *infrav1.HetznerCluster

capiMachine *clusterv1.Machine
capiCluster *clusterv1.Cluster

hetznerSecret *corev1.Secret
bootstrapSecret *corev1.Secret

testNs *corev1.Namespace
key client.ObjectKey
)

BeforeEach(func() {
var err error
testNs, err = testEnv.CreateNamespace(ctx, "hcloudmachinetemplate-reconciler")
Expect(err).NotTo(HaveOccurred())

hetznerSecret = getDefaultHetznerSecret(testNs.Name)
Expect(testEnv.Create(ctx, hetznerSecret)).To(Succeed())

bootstrapSecret = getDefaultBootstrapSecret(testNs.Name)
Expect(testEnv.Create(ctx, bootstrapSecret)).To(Succeed())

capiCluster = &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test1-",
Namespace: testNs.Name,
Finalizers: []string{clusterv1.ClusterFinalizer},
},
Spec: clusterv1.ClusterSpec{
InfrastructureRef: &corev1.ObjectReference{
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1",
Kind: "HetznerCluster",
Name: "hetzner-test1",
Namespace: testNs.Name,
},
},
}
Expect(testEnv.Create(ctx, capiCluster)).To(Succeed())

hcloudMachineName := utils.GenerateName(nil, "hcloud-machine-")
capiMachineName := utils.GenerateName(nil, "capi-machine-")

capiMachine = &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: capiMachineName,
Namespace: testNs.Name,
Finalizers: []string{clusterv1.MachineFinalizer},
Labels: map[string]string{
clusterv1.ClusterNameLabel: capiCluster.Name,
},
},
Spec: clusterv1.MachineSpec{
ClusterName: capiCluster.Name,
InfrastructureRef: corev1.ObjectReference{
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1",
Kind: "HCloudMachine",
Name: hcloudMachineName,
},
FailureDomain: &defaultFailureDomain,
Bootstrap: clusterv1.Bootstrap{
DataSecretName: ptr.To("bootstrap-secret"),
},
},
}
Expect(testEnv.Create(ctx, capiMachine)).To(Succeed())

hetznerCluster = &infrav1.HetznerCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "hetzner-test1",
Namespace: testNs.Name,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: capiCluster.Name,
UID: capiCluster.UID,
},
},
},
Spec: getDefaultHetznerClusterSpec(),
}
Expect(testEnv.Create(ctx, hetznerCluster)).To(Succeed())

hcloudMachine = &infrav1.HCloudMachine{
ObjectMeta: metav1.ObjectMeta{
Name: hcloudMachineName,
Namespace: testNs.Name,
Labels: map[string]string{
clusterv1.ClusterNameLabel: capiCluster.Name,
clusterv1.MachineControlPlaneNameLabel: "",
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Machine",
Name: capiMachine.Name,
UID: capiMachine.UID,
},
},
},
Spec: infrav1.HCloudMachineSpec{
ImageName: "fedora-control-plane",
Type: "cpx31",
PlacementGroupName: &defaultPlacementGroupName,
},
}
Expect(testEnv.Create(ctx, hcloudMachine)).To(Succeed())

hetznerBareMetalRemediation = &infrav1.HetznerBareMetalRemediation{
ObjectMeta: metav1.ObjectMeta{
Name: "hetzner-baremetal-remediation",
Namespace: testNs.Name,
OwnerReferences: []metav1.OwnerReference{
{
Kind: "Machine",
APIVersion: clusterv1.GroupVersion.String(),
Name: capiMachine.Name,
UID: capiMachine.UID,
},
},
},
Spec: infrav1.HetznerBareMetalRemediationSpec{
Strategy: &infrav1.RemediationStrategy{
Type: "Reboot",
RetryLimit: 1,
Timeout: &metav1.Duration{},
},
},
}
Expect(testEnv.Create(ctx, hetznerBareMetalRemediation)).To(Succeed())

key = client.ObjectKey{Namespace: testNs.Name, Name: "hetzner-baremetal-remediation"}
})

AfterEach(func() {
Expect(testEnv.Cleanup(ctx, testNs, hetznerBareMetalRemediation, hcloudMachine, hetznerCluster,
hetznerSecret, bootstrapSecret, capiMachine, capiCluster)).To(Succeed())
})

Context("Basic test", func() {
It("creates the hetznerBareMetalRemediation successfully", func() {
Eventually(func() bool {
if err := testEnv.Get(ctx, key, hetznerBareMetalRemediation); err != nil {
return false
}
return true
}, timeout).Should(BeTrue())
})
})
})

0 comments on commit 5226d1d

Please sign in to comment.