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

Refactor (controller_tests): build the test case objects with all the information #1125

Closed

Conversation

hshitomi
Copy link

@hshitomi hshitomi commented Dec 5, 2023

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

Remove the conditional statements that customize the input test case object based on the different test scenarios in the function of running a test.

The function to build the test cases should build the test case objects with all the information that the function of running a test needs so that it makes the test code easier to read and understand.

Which issue(s) this PR fixes:

Fixes #376

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

NONE

Remove the conditional statements that customize the input test
case object based on the different test scenarios in the function
of running a test.

The function to build the test cases should build the test case
objects with all the information that the function of running a
test needs so that it makes the test code easier to read and
understand.

Signed-off-by: Hidehisa Shitomi <hidehisa.shtm@gmail.com>
@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. labels Dec 5, 2023
Copy link

linux-foundation-easycla bot commented Dec 5, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Dec 5, 2023
@k8s-ci-robot
Copy link
Contributor

Welcome @hshitomi!

It looks like this is your first PR to kubernetes-csi/external-provisioner 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-csi/external-provisioner has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Dec 5, 2023
@k8s-ci-robot
Copy link
Contributor

Hi @hshitomi. Thanks for your PR.

I'm waiting for a kubernetes-csi member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

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/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Dec 5, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: hshitomi
Once this PR has been reviewed and has the lgtm label, please assign pohly for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found 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

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Dec 5, 2023
@hshitomi
Copy link
Author

hshitomi commented Dec 5, 2023

/hold

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Dec 5, 2023
@hshitomi
Copy link
Author

hshitomi commented Dec 5, 2023

/cc @mauriciopoppe @msau42

@hshitomi
Copy link
Author

hshitomi commented Dec 5, 2023

@mauriciopoppe Let me start our conversation before I completely refactor the code.

My current focus is on the provisioning test cases. I have removed the if statements from runProvisionTest(). Could you please take a look? I want to confirm that I am on the right track.

Since I rewrote the test cases using method chaining (in provisionTestcase()), a lot of code has changed. However, as you can see, the core part of the change is not big and is around provisioningTestcaseOption struct, TestProvision() function and TestShouldProvision() function.

For now, I am using the method chaining rather than the functional option pattern because it seems a bit simpler to me. I know that the method to build the test case objects with all the information does not matter much. But if you prefer to use the functional option pattern, please let me know.

Thank you in advance for your time and guidance. Looking forward to your feedback.

@mauriciopoppe
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Dec 5, 2023
@@ -1090,8 +1198,8 @@ func provisionTestcases() (int64, map[string]provisioningTestcase) {
}
vacName := "test-vac"
return requestedBytes, map[string]provisioningTestcase{
"normal provision": {
volOpts: controller.ProvisionOptions{
"normal provision": *provisionTestcaseBuilder().volOpts(
Copy link
Member

Choose a reason for hiding this comment

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

I think the builder pattern should be applied to things we want to build in an object to then test it externally. With this implementation both the things to build and the expectations are part of the same object.

Maybe provisioningTestCase shouldn't have setters for expectations.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for your feedback. Let me confirm your suggestion.

We should create another function to set the expectations. The function is to set the following elements of the provisioningTestcase struct, which is the expectation of the test result.

	expectedPVSpec            *pvSpec
	expectedMockReturn        func(t *testing.T, out *csi.CreateVolumeResponse, controllerServer *driver.MockControllerServer, createVolumeError error)
	expectErr                 bool
	expectState               controller.ProvisioningState
	expectSelectedNode        string // a specific selected-node of the PVC in the apiserver after the test, same as before if empty
	expectNoProvision         bool   // if true, then ShouldProvision should return false

At the same time, the provisionTestcase() function should be used to build the test case, which means to set the following elements of the provisioningTestcase struct.

	capacity                  int64 // if zero, default capacity, otherwise available bytes
	volOpts                   controller.ProvisionOptions
	clientSetObjects          []runtime.Object
	createVolumeError         error
	withExtraMetadata         bool
	deploymentNode            string // fake distributed provisioning with this node as host
	immediateBinding          bool   // enable immediate binding support for distributed provisioning
	controllerPublishReadOnly bool
	featureGates              map[featuregate.Feature]bool
	pluginCapabilities        func() (rpc.PluginCapabilitySet, rpc.ControllerCapabilitySet)

Did I understand your suggestion properly?

Copy link
Author

Choose a reason for hiding this comment

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

I split the first three test cases into builder (buildProvisionTestcase()) and expectation setter (setProvisionTestcaseExpectation()). Is this what you intended?

Or is the intention to further divide the provisioningTestcase struct into two, as shown below, and rewrite related parts based on that?

type provisioningTestcase struct {
	capacity                  int64 // if zero, default capacity, otherwise available bytes
	volOpts                   controller.ProvisionOptions
	clientSetObjects          []runtime.Object
	createVolumeError         error
	withExtraMetadata         bool
	deploymentNode            string // fake distributed provisioning with this node as host
	immediateBinding          bool   // enable immediate binding support for distributed provisioning
	controllerPublishReadOnly bool
	featureGates              map[featuregate.Feature]bool
	pluginCapabilities        func() (rpc.PluginCapabilitySet, rpc.ControllerCapabilitySet)
}

type provisioningTestcaseExpectation struct {
	expectedPVSpec            *pvSpec
	expectedMockReturn        func(t *testing.T, out *csi.CreateVolumeResponse, controllerServer *driver.MockControllerServer, createVolumeError error)
	expectErr                 bool
	expectState               controller.ProvisioningState
	expectSelectedNode        string // a specific selected-node of the PVC in the apiserver after the test, same as before if empty
	expectNoProvision         bool   // if true, then ShouldProvision should return false
}

@@ -1090,8 +1198,8 @@ func provisionTestcases() (int64, map[string]provisioningTestcase) {
}
vacName := "test-vac"
return requestedBytes, map[string]provisioningTestcase{
"normal provision": {
volOpts: controller.ProvisionOptions{
"normal provision": *provisionTestcaseBuilder().volOpts(
Copy link
Member

Choose a reason for hiding this comment

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

I think it became a little bit harder to read with chaining, compare:

makeProvisionTestcaseBuilder().volOpts(
  ...
).foo(
  ...
).bar(
  ...
).build()

vs

makeProvisionTestcaseBuilder(
  withFoo(),
  withBar(),
  withVolOpts()
).build()

Copy link
Author

Choose a reason for hiding this comment

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

OK. Let me implement it using the functional option pattern.

Copy link
Author

@hshitomi hshitomi Dec 6, 2023

Choose a reason for hiding this comment

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

I wrote the first three test cases with the functional option pattern.

As you said, the test case setup part in buildProvisionTestcase() has become a bit easier to read (we do not need the last build() method in case of the functional option pattern). On the other hand, the functions for the functional option pattern seem to be a bit harder to read than the methods for method chaining.

func (tco *provisioningTestcaseOption) volOpts(op controller.ProvisionOptions) *provisioningTestcaseOption {
	tco.tc.volOpts = op
	return tco
}

vs.

func withVolOpts(op controller.ProvisionOptions) provisionTestcaseOption {
	return func(tc *provisioningTestcase) *provisioningTestcase {
		tc.volOpts = op
		return tc
	}
}

That is why I chose the method chaining. However, I guess the simplicity of the test case setup part might be more important. So, I should use the functional option pattern for the rest of the test cases.

What do you think?

- Change to use the functional option patter for the first 3 test cases
- Split the first 3 test cases into builder and expectation setter

Signed-off-by: Hidehisa Shitomi <hidehisa.shtm@gmail.com>
@hshitomi
Copy link
Author

@mauriciopoppe @msau42 I'm sure you're busy, but I'm looking forward to your feedback. If possible, could you please let me know when you can get to the review?

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle stale
  • Close this PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 9, 2024
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle rotten
  • Close this PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label May 9, 2024
@k8s-ci-robot k8s-ci-robot added the lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. label May 9, 2024
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Reopen this PR with /reopen
  • Mark this PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

@k8s-ci-robot
Copy link
Contributor

@k8s-triage-robot: Closed this PR.

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Reopen this PR with /reopen
  • Mark this PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor controller_tests to use builder pattern
4 participants