-
Notifications
You must be signed in to change notification settings - Fork 257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐛issue-1737: Add unit tests for openstackmachine_webhook #2068
Conversation
Hi @MykolaRodin. Thanks for your PR. I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
✅ Deploy Preview for kubernetes-sigs-cluster-api-openstack ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
@@ -50,7 +50,7 @@ var _ = Describe("OpenStackMachine API validations", func() { | |||
Expect(k8sClient.Create(ctx, defaultMachine())).To(Succeed(), "OpenStackMachine creation should succeed") | |||
}) | |||
|
|||
It("should only allow the providerID to be set once", func() { | |||
It("should only allow the providerID to be set once and identityRef to be set several times", func() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please can you add a separate test for identityRef instead of adding it to this one? Unlike the e2e tests these tests are pretty fast to initialise, so it's ok to run lots of them. It's simpler to understand and maintain if they each do one thing only.
@@ -63,6 +63,14 @@ var _ = Describe("OpenStackMachine API validations", func() { | |||
By("Modifying the providerID") | |||
machine.Spec.ProviderID = ptr.To("bar") | |||
Expect(k8sClient.Update(ctx, machine)).NotTo(Succeed(), "Updating providerID should fail") | |||
|
|||
By("Setting the identityRef") | |||
machine.Spec.IdentityRef = ptr.To(infrav1.OpenStackIdentityReference{Name: "foo", CloudName: "GCP"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incidentally, it's not important to this test, except for readability, but CloudName
here refers to an entry for an OpenStack cloud in clouds.yaml
. So it's going to be something like production
, staging
, etc. In practise people don't tend to push a clouds.yaml with more than 1 cloud in it so this is academic, but we still support it.
|
||
// By("Modifying the identityRef") | ||
// machine.Spec.IdentityRef = ptr.To(infrav1.OpenStackIdentityReference{Name: "bar", CloudName: "AWS"}) | ||
// Expect(k8sClient.Update(ctx, machine)).To(Succeed(), "Updating identityRef should succeed") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it this one that fails?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have not uncommented it yet because it fails before.
If I change providerID
and identityRef
in different tests, it works fine. If I change both values withing the same test, if fails. I updated cluster-api-provider-openstack/pkg/webhooks/openstackmachine_webhook.go
- ValidateUpdate()
a bit to print old and new objects on failure. Here the results of the failure are:
Message: "admission webhook \"validation.openstackmachine.infrastructure.cluster.x-k8s.io\" denied the request: OpenStackMachine.infrastructure.cluster.x-k8s.io \"machine-vjrtn\" is invalid: spec: Forbidden: cannot be modified
old:map[flavor: image:map[filter:map[name:test-image]] providerID:foo]
new:map[flavor: image:map[filter:map[name:test-image]] providerID:bar]"
Please clarify if it is expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, it looks like I got the reason for the failure when updating both providerID
and identityRef
. The test failed because updating providerID
for the second time failed. Once I removed it, the test with updating providerID
once and updating identityRef
twice succeeded. Please clarify whether this test with updating both providerID
and identityRef
should be kept or it should be deleted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, of course! The local object still contains the dirty ProviderID from the previous test.
No, it's not worth keeping this test.
/ok-to-test |
/retest |
HI @mdbooth, I have removed the test with updating |
@@ -65,6 +65,21 @@ var _ = Describe("OpenStackMachine API validations", func() { | |||
Expect(k8sClient.Update(ctx, machine)).NotTo(Succeed(), "Updating providerID should fail") | |||
}) | |||
|
|||
It("should only allow the identityRef to be set several times", func() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
It("should only allow the identityRef to be set several times", func() { | |
It("should allow the identityRef to be set several times", func() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -96,6 +96,7 @@ func (*openStackMachineWebhook) ValidateUpdate(_ context.Context, oldObjRaw, new | |||
delete(newOpenStackMachineSpec, "providerID") | |||
} | |||
|
|||
// instanceID is present in v1alpha6 but is deprecated in v1beta1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the problem of writing these webhooks with Unstructured
instead of proper types. The code below is a no-op because instanceID
can never be set. If we were using proper types it would have been a compile error and we'd have noticed.
This can be deleted. Probably best in a separate PR, though, or at least a separate commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// instanceID is present in v1alpha6 but is deprecated in v1beta1
was removed
wantErr: false, | ||
}, | ||
{ | ||
name: "OpenStackMachine.Spec.RootVolume and OpenStackMachine.Spec.AdditionalBlockDevices with non-root user on create", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name: "OpenStackMachine.Spec.RootVolume and OpenStackMachine.Spec.AdditionalBlockDevices with non-root user on create", | |
name: "OpenStackMachine.Spec.RootVolume and OpenStackMachine.Spec.AdditionalBlockDevices with non-root device name on create", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
wantErr: false, | ||
}, | ||
{ | ||
name: "OpenStackMachine.Spec.RootVolume and OpenStackMachine.Spec.AdditionalBlockDevices with root user on create", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name: "OpenStackMachine.Spec.RootVolume and OpenStackMachine.Spec.AdditionalBlockDevices with root user on create", | |
name: "OpenStackMachine.Spec.RootVolume and OpenStackMachine.Spec.AdditionalBlockDevices with root device name on create", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests are good. Ideally they'd be implemented in the apivalidations suite instead, though, because in the medium term I actually want to delete this webhook entirely and implement these validations in the apiserver directly. When we do that, these tests will no longer pass.
However, if we implement these tests in apivalidations instead we can completely change the implementation of the validations while ensuring that the exact same test suite continues to pass.
If you are able, my preference would be to move them to apivalidations. If not, though, I'll take these anyway. Apart from anything else they document the test cases that need to be implemented in apivalidations when we change the validations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the review. I have updated my PR in accordance with your code review remarks. On the other hand, I am sorry I cannot get what you mean under moving the tests to apivalidations
. The e2e test I added has already been placed in cluster-api-provider-openstack/test/e2e/suites/apivalidations
folder. The unit tests I added are placed in the file in the same folder as cluster-api-provider-openstack/pkg/webhooks/openstackmachine_webhook.go
file. I cannot find other apivalidations
folder and cannot see what I cad additionally change in the paths.
If it is possible, I would like to merge this PR as is. If you provide me with the additional clarification related to moving the tests to apivalidations
, I am ready to do it in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please clarify if I should have added several e2e tests instead of the unit tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My preference was that they be e2e tests instead. However these tests improve the current state of our testing and I'll gladly merge unit tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mdbooth,
It looks like the PR should still be modified to be taken into account and be reviewed. I removed the unit tests that I added and substituted them with e2e tests. I hope it is a bit closer to your preferences. Please review it and let me know if I am still missing something.
/approve These are great, thanks! If you wanted to convert the unit tests to be e2e tests instead that would also be great, but I'm happy for these to merge as-is. |
FYI, I added:
to the PR description so the issue will be automatically closed when this PR merges. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great, thanks. I have already approved. Lets wait for an lgtm from another reviewer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
Hi @mdbooth , The tests failed with this error:
I am sorry, I am not quite sure what it actually means. |
It is an e2e test that failed. They are a bit flaky sometimes unfortunately. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: EmilienM, mdbooth The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/hold waiting for the CI job |
/hold cancel |
What this PR does / why we need it:
It extends tests for
openstackmachine_webhook
Fixes: #1737