Skip to content
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

Validate utils Package and Taint Functionality #61

Merged
merged 6 commits into from
Jul 6, 2023

Conversation

razo7
Copy link
Member

@razo7 razo7 commented Jun 29, 2023

  • Add one unit test for taint functionality on control-plane node with adding and removing FAR NoExecute taint
  • Use worker label from Commons rather than declaring it in FAR's repoistory
  • Run make go-verify
  • Create new suite test and UT file for utils package, then move out the 3 UT for Utils functionality to utils_test, and update makefile to test the new directory.

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Jun 29, 2023

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@razo7
Copy link
Member Author

razo7 commented Jun 29, 2023

Tests will fail since the PR uses common repo which depends on newer Kubernetes API, thus #56 should be merged, and then after rebasing the PR could be tested.

@mshitrit
Copy link
Member

mshitrit commented Jul 2, 2023

I think at the moment go.mod/go.sum/vendor aren't in sync.
This can be resolved by go mod tidy/vendor.
However once done it will uncover a more serious issue which I've also encountered in a separate PR.

By("having one control-plane-role taint")
taintedNode := &corev1.Node{}
Expect(k8sClient.Get(context.Background(), nodeKey, taintedNode)).To(Succeed())
Expect(utils.TaintExists(taintedNode.Spec.Taints, &controlPlaneRoleTaint)).To(BeTrue())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like a test for the utils package to me, so it should be there IMHO

razo7 added 3 commits July 3, 2023 16:43
Add UT to check whether append and remove taint functions append and remove properly taints while the other taints remain
Use common for k8s node labels
Add missing and remove unused modules, make vendored copy of dependencies, and verify dependencies have expected content
@razo7 razo7 changed the title Unit Test Taint Functionality Validate utils Package and Taint Functionality Jul 3, 2023
Comment on lines 38 to 62
// used for making new node object for test and have a unique resourceVersion
// GetNode returns a node object with the name nodeName based on the nodeType input
func GetNode(nodeType, nodeName string) *corev1.Node {
if nodeType == "control-plane" {
return &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: nodeName,
},
Spec: corev1.NodeSpec{
Taints: []corev1.Taint{
{
Key: medik8sLabels.ControlPlaneRole,
Effect: corev1.TaintEffectNoExecute,
},
},
},
}
} else {
return &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: nodeName,
},
}
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes more sense to move this function which is used for unit-testing to another file. Maybe under test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even having two local function for getting the node object? One in fenceagentsremediation_controller_test.go , and one in utils_test.go

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I would put it into pkg/utils/nodes_test.go

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then controllers/fenceagentsremediation_controller_test.go could not find the GetNode function from a test code pkg/utils/nodes_test.go.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah crap, I always forget about this limitation...
then I would just leave it here 🤷🏼‍♂️

@razo7
Copy link
Member Author

razo7 commented Jul 3, 2023

/test 4.13-openshift-e2e

@@ -0,0 +1,99 @@
/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filename: you can skip "utils", it's redundant (there should be one suite per package)

@@ -0,0 +1,78 @@
package utils
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would name this file nodes_test.go, so you can have separate test files when you add new util files later on.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But nodes_test.go wouldn't be interfered as the test file for nodes.go, while this file shares a test for the taint file as well? ATM it has tests for two utils files, taint.go, and nodes.go
Going with your direction maybe it would make more sense to move Context("Taint functioninality test",... logic to taint_test.go, and have nodes_test.go for the other tests that utils.go has.
WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, ideally every xyz.go file should have its own xyz_test.go file. But I won't block on this, for these few methods / tests

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me :)

Comment on lines 38 to 62
// used for making new node object for test and have a unique resourceVersion
// GetNode returns a node object with the name nodeName based on the nodeType input
func GetNode(nodeType, nodeName string) *corev1.Node {
if nodeType == "control-plane" {
return &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: nodeName,
},
Spec: corev1.NodeSpec{
Taints: []corev1.Taint{
{
Key: medik8sLabels.ControlPlaneRole,
Effect: corev1.TaintEffectNoExecute,
},
},
},
}
} else {
return &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: nodeName,
},
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I would put it into pkg/utils/nodes_test.go

@@ -132,7 +114,7 @@ var _ = Describe("FAR Controller", func() {

When("creating valid FAR CR", func() {
BeforeEach(func() {
node = getNode(node01)
node = utils.GetNode("", node01)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC node is always initialized to the same value.
If this is indeed the case I suggest:

  • initialize node value on var declaration
  • move the node creation to a share BeforeEach block

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC node is always initialized to the same value.

Correct, but if it is initialized once then the second test would fail as the instance might be deleted or already created while the second test tries to create it (an error due race condition). To overcome it we initialize an independent instance of the node (same spec and name, but different resource version) in a different BeforeEach block.

Context("FAR CR and Node Names Validity test", func() {
BeforeEach(func() {
node = GetNode("", node01)
DeferCleanup(k8sClient.Delete, context.Background(), node)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: defer should be after the Create

By("having one control-plane-role taint")
taintedNode := &corev1.Node{}
Expect(k8sClient.Get(context.Background(), nodeKey, taintedNode)).To(Succeed())
Expect(TaintExists(taintedNode.Spec.Taints, &controlPlaneRoleTaint)).To(BeTrue())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this is redundant since we added this taint in the test code

These test are unrelated to controller package, thus they should reside under utils directory
@razo7
Copy link
Member Author

razo7 commented Jul 4, 2023

/test 4.13-openshift-e2e

Every xyz.go file should have its own xyz_test.go file, and not a general one as utils_test.go
@razo7
Copy link
Member Author

razo7 commented Jul 5, 2023

/test 4.13-openshift-e2e

@razo7
Copy link
Member Author

razo7 commented Jul 5, 2023

/retry

@razo7
Copy link
Member Author

razo7 commented Jul 5, 2023

/retest

@razo7
Copy link
Member Author

razo7 commented Jul 5, 2023

I didn't though that /retest will trigger all the jobs, since the PR is still considered as draft... But now I know :D

@razo7
Copy link
Member Author

razo7 commented Jul 5, 2023

/retest

Copy link
Member

@slintes slintes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two nits, other than that lgtm

@@ -33,3 +34,29 @@ func IsNodeNameValid(r client.Reader, nodeName string) (bool, error) {
}
return true, nil
}

// used for making new node object for test and have a unique resourceVersion
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments should start with the function name, in the 1st line of the comment 😉


// used for making new node object for test and have a unique resourceVersion
// GetNode returns a node object with the name nodeName based on the nodeType input
func GetNode(nodeType, nodeName string) *corev1.Node {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO nodeRole wold be a better name than nodeType?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree

Var name and function declaration should begin in the first line of comments
@razo7
Copy link
Member Author

razo7 commented Jul 6, 2023

/test 4.13-openshift-e2e

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Jul 6, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: razo7, slintes

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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@razo7 razo7 marked this pull request as ready for review July 6, 2023 13:19
@openshift-ci openshift-ci bot requested review from mshitrit and slintes July 6, 2023 13:19
@openshift-merge-robot openshift-merge-robot merged commit a50d9f6 into medik8s:main Jul 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants