Skip to content

Commit

Permalink
🌱Add test for hcloudmachine webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
yrs147 committed May 27, 2024
1 parent b50d40d commit 1e32559
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions controllers/hcloudmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,55 @@ var _ = Describe("HCloudMachine validation", func() {
})
})

var _ = Describe("HcloudMachine Webhook Validation", func(){
var(
hcloudMachine *infrav1.HCloudMachine
oldHCloudMachine *infrav1.HCloudMachine
testNs *corev1.Namespace
)
BeforeEach(func () {
var err error
testNs, err = testEnv.CreateNamespace(ctx, "hcloudmachine-validation")
Expect(err).NotTo(HaveOccurred())
hcloudMachine = &infrav1.HCloudMachine{
ObjectMeta: metav1.ObjectMeta{
Name: "hcloud-validation-machine",
Namespace: testNs.Name,
},
Spec: infrav1.HCloudMachineSpec{
Type: "cpx31",
ImageName: "ubuntu",
},
}
oldHCloudMachine = hcloudMachine.DeepCopy()
oldHCloudMachine.Spec.Type = "cpx21"
oldHCloudMachine.Spec.ImageName = "ubuntu-18.04"
})
AfterEach(func() {
Expect(testEnv.Cleanup(ctx, testNs, hcloudMachine)).To(Succeed())
})
It("should allow valid HCloudMachine creation", func(){
warnings, err := hcloudMachine.ValidateCreate()
Expect(warnings).To(BeNil())
Expect(err).To(BeNil())
})
It("should prevent updating immutable fields",func () {
newHCloudMachine := hcloudMachine.DeepCopy()
newHCloudMachine.Spec.Type = "cpx32"
newHCloudMachine.Spec.ImageName = "fedora-control-plane"

warnings,err := newHCloudMachine.ValidateUpdate(oldHCloudMachine)
Expect(warnings).To(BeNil())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("field is immutable"))
})
It("should allow valid HCloudMachine deleteion",func () {
warnings,err := hcloudMachine.ValidateDelete()
Expect(warnings).To(BeNil())
Expect(err).To(BeNil())
})
})

var _ = Describe("IgnoreHetznerClusterConditionUpdates Predicate", func() {
var (
predicate predicate.Predicate
Expand Down

0 comments on commit 1e32559

Please sign in to comment.