Skip to content

Commit

Permalink
🌱Change approach to use testEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
yrs147 committed Jun 24, 2024
1 parent 180a275 commit f25cd7b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 32 deletions.
47 changes: 25 additions & 22 deletions controllers/hcloudmachinetemplate_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,8 @@ var _ = Describe("HCloudMachineTemplateReconciler", func() {
})
Context("HCloudMachineTemplate Webhook Validation", func() {
var (
hcloudMachineTemplate *infrav1.HCloudMachineTemplate
oldHCloudMachineTemplate *infrav1.HCloudMachineTemplate
testNs *corev1.Namespace
hcloudMachineTemplateHook *infrav1.HCloudMachineTemplateWebhook
hcloudMachineTemplate *infrav1.HCloudMachineTemplate
testNs *corev1.Namespace
)
BeforeEach(func() {
var err error
Expand All @@ -272,32 +270,37 @@ var _ = Describe("HCloudMachineTemplateReconciler", func() {
},
},
}
oldHCloudMachineTemplate = hcloudMachineTemplate.DeepCopy()
hcloudMachineTemplateHook = &infrav1.HCloudMachineTemplateWebhook{}
Expect(testEnv.Client.Create(ctx, hcloudMachineTemplate)).To(Succeed())
key = client.ObjectKey{Namespace: testNs.Name, Name: "hcloud-validation-machine"}
Eventually(func() error {
return testEnv.Client.Get(ctx, key, hcloudMachineTemplate)
}, timeout, time.Second).Should(BeNil())
})
AfterEach(func() {
Expect(testEnv.Cleanup(ctx, testNs, hcloudMachineTemplate)).To(Succeed())
})
It("should allow valid HCloudMachineTemplate creation", func() {
warnings, err := hcloudMachineTemplateHook.ValidateCreate(ctx, hcloudMachineTemplate)
Expect(warnings).To(BeNil())
Expect(err).To(BeNil())
})

It("should prevent updating immutable fields", func() {
newHCloudMachineTemplate := hcloudMachineTemplate.DeepCopy()
newHCloudMachineTemplate.Spec.Template.Spec.Type = "cpx32"
newHCloudMachineTemplate.Spec.Template.Spec.ImageName = "fedora-control-plane"
Expect(testEnv.Get(ctx, key, machineTemplate)).To(Succeed())

warnings, err := hcloudMachineTemplateHook.ValidateUpdate(ctx, oldHCloudMachineTemplate, newHCloudMachineTemplate)
Expect(warnings).To(BeNil())
Expect(err).To(HaveOccurred())
})
hcloudMachineTemplate.Spec = infrav1.HCloudMachineTemplateSpec{
Template: infrav1.HCloudMachineTemplateResource{
Spec: infrav1.HCloudMachineSpec{
Type: "cpx32",
},
},
}
Expect(testEnv.Client.Update(ctx, hcloudMachineTemplate)).ToNot(Succeed())

hcloudMachineTemplate.Spec = infrav1.HCloudMachineTemplateSpec{
Template: infrav1.HCloudMachineTemplateResource{
Spec: infrav1.HCloudMachineSpec{
ImageName: "fedora-control-plane",
},
},
}
Expect(testEnv.Client.Update(ctx, hcloudMachineTemplate)).ToNot(Succeed())

It("should allow valid HCloudMachineTemplate deletion", func() {
warnings, err := hcloudMachineTemplateHook.ValidateDelete(ctx, hcloudMachineTemplate)
Expect(warnings).To(BeNil())
Expect(err).To(BeNil())
})
})

Expand Down
74 changes: 64 additions & 10 deletions controllers/hetznerbaremetalmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,18 +750,16 @@ var _ = Describe("HetznerBareMetalMachineReconciler", func() {

Context("validate update", func() {
var (
hbmmt *infrav1.HetznerBareMetalMachineTemplate
oldhbmmt *infrav1.HetznerBareMetalMachineTemplate
testNs *corev1.Namespace
hbmmtwebhook *infrav1.HetznerBareMetalMachineTemplateWebhook
hbmmt *infrav1.HetznerBareMetalMachineTemplate
testNs *corev1.Namespace
)
BeforeEach(func() {
var err error
testNs, err = testEnv.CreateNamespace(ctx, "hcloudmachine-validation")
Expect(err).NotTo(HaveOccurred())
hbmmt = &infrav1.HetznerBareMetalMachineTemplate{
ObjectMeta: metav1.ObjectMeta{
Name: bmMachineName,
Name: "test-machine",
Namespace: testNs.Name,
Labels: map[string]string{
clusterv1.ClusterNameLabel: capiCluster.Name,
Expand All @@ -773,17 +771,73 @@ var _ = Describe("HetznerBareMetalMachineReconciler", func() {
},
},
}
hbmmtwebhook = &infrav1.HetznerBareMetalMachineTemplateWebhook{}
Expect(testEnv.Client.Create(ctx, hbmmt)).To(Succeed())
key = client.ObjectKey{Namespace: testNs.Name, Name: hbmmt.Name}
Eventually(func() error {
return testEnv.Client.Get(ctx, key, hbmmt)
}, timeout, time.Second).Should(BeNil())

})
AfterEach(func() {
Expect(testEnv.Cleanup(ctx, testNs, hbmmt)).To(Succeed())
})
It("should validate update", func() {
newhbmmt := hbmmt.DeepCopy()
warnings, err := hbmmtwebhook.ValidateUpdate(ctx, oldhbmmt, newhbmmt)
Expect(warnings).To(BeNil())
Expect(err).To(HaveOccurred())
Expect(testEnv.Get(ctx, key, hbmmt)).To(Succeed())
hbmmt.Spec = infrav1.HetznerBareMetalMachineTemplateSpec{
Template: infrav1.HetznerBareMetalMachineTemplateResource{
Spec: infrav1.HetznerBareMetalMachineSpec{
InstallImage: infrav1.InstallImage{
Image: infrav1.Image{
Name: "ubuntu-20.04",
URL: "https://example.com/ubuntu-20.04.tar.gz",
},
},
},
},
}
Expect(testEnv.Client.Update(ctx, hbmmt)).ToNot(Succeed())

Expect(testEnv.Get(ctx, key, hbmmt)).To(Succeed())
hbmmt.Spec = infrav1.HetznerBareMetalMachineTemplateSpec{
Template: infrav1.HetznerBareMetalMachineTemplateResource{
Spec: infrav1.HetznerBareMetalMachineSpec{
SSHSpec: infrav1.SSHSpec{
SecretRef: infrav1.SSHSecretRef{
Name: "ssh-secret-new",
Key: infrav1.SSHSecretKeyRef{
Name: "ssh-key-name-new",
PublicKey: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC",
PrivateKey: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC",
},
},
PortAfterInstallImage: 2222,
PortAfterCloudInit: 2222,
},
},
},
}
Expect(testEnv.Client.Update(ctx, hbmmt)).ToNot(Succeed())

Expect(testEnv.Get(ctx, key, hbmmt)).To(Succeed())
hbmmt.Spec = infrav1.HetznerBareMetalMachineTemplateSpec{
Template: infrav1.HetznerBareMetalMachineTemplateResource{
Spec: infrav1.HetznerBareMetalMachineSpec{
HostSelector: infrav1.HostSelector{
MatchLabels: map[string]string{
"key3": "value3",
},
MatchExpressions: []infrav1.HostSelectorRequirement{
{
Key: "key4",
Operator: selection.In,
Values: []string{"value4"},
},
},
},
},
},
}
Expect(testEnv.Client.Update(ctx, hbmmt)).ToNot(Succeed())
})

})
Expand Down

0 comments on commit f25cd7b

Please sign in to comment.