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

🌱 Add composable matchers to internal/testtypes #5259

Merged
merged 1 commit into from
Sep 23, 2021

Conversation

killianmuldoon
Copy link
Contributor

This commit adds composable matching objects to the internal
testtypes package. These types can be used for advanced comparison
between objects in the Cluster API unit and integration tests.
A simple integration test is included to show the basic working of
the matchers.

Fixes #5157

Note: This is a duplicate / update of #5157 - the branch name was not correct ahead for merge history.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 17, 2021
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Sep 17, 2021
@killianmuldoon killianmuldoon changed the title Add composable matchers to internal/testtypes 🌱 Add composable matchers to internal/testtypes Sep 17, 2021
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 17, 2021
@killianmuldoon
Copy link
Contributor Author

/retest

@@ -21,9 +21,8 @@ import (

"github.com/pkg/errors"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
"sigs.k8s.io/cluster-api/internal/testtypes"
. "sigs.k8s.io/cluster-api/internal/testtypes"
Copy link
Member

Choose a reason for hiding this comment

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

Moved from the old PR:

@sbueringer sbueringer 4 days ago Member
I'm not a fan of dot imports to be honest :/ (because of the "magic-factor")
I understand why we want it for matchers, I'm not sure if we should also use it for our builder funcs.

@killianmuldoon killianmuldoon 3 days ago Author Member
Yeah - it's a compromise here from having one package with the two different functions. We > are using . imports across our testing for Gomega. Do you have strong feelings about this? I can definitely see both sides.

Copy link
Member

Choose a reason for hiding this comment

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

@killianmuldoon It's fine for me to keep it, if that's the consensus. Overall I would like to be very careful with dot imports. Gomega is basically the only one I ever saw in Go and I think that's a good thing because it makes the code less readable.

An option could be to have the matchers and builders in separate packages and only import the matchers with a dot import (like the gomega matchers).

I would like to hear opinions from @vincepri and @fabriziopandini on that, though.

Copy link
Member

Choose a reason for hiding this comment

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

I'm personally ok with this dot import given that it is our own "additional layer" on top of gomega/testing and it makes tests more readable. However, no problem to accept whatever we agree on.

Copy link
Member

Choose a reason for hiding this comment

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

I definitely agree with having the dot imports for the matchers. It's more the builder funcs and everything else we currently have and will add in the future in the testtypes package.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I definitely agree there's a danger here - dot-by-default isn't something we want for the entire testtypes package I don't think. We could have testtypes/matchers below it - other than worries about too many small packages is there any issue with doing it that way and only doing dot import on matchers?

Copy link
Member

@sbueringer sbueringer Sep 20, 2021

Choose a reason for hiding this comment

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

I don't have a concern about a separate package, only wondering if a slightly different structure would make more sense:

internal
  testtypes
  testmatchers

Or

internal
  test
    types
    matchers

If I understood correctly the matchers are an extension to gomega matchers for CAPI/Kubernetes so they are not really specific to testtypes they are just also part of our "test library" (so I'm mostly challenging if they should really be a sub-package of testtypes)

@@ -20,7 +20,7 @@ import (
"strings"
"testing"

"sigs.k8s.io/cluster-api/internal/testtypes"
. "sigs.k8s.io/cluster-api/internal/testtypes"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

@@ -19,11 +19,8 @@ package topology
import (
"testing"

"github.com/google/go-cmp/cmp/cmpopts"
. "sigs.k8s.io/cluster-api/internal/testtypes"

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

@@ -21,9 +21,8 @@ import (
"regexp"
"testing"

"sigs.k8s.io/cluster-api/internal/testtypes"
. "sigs.k8s.io/cluster-api/internal/testtypes"

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

@@ -21,9 +21,8 @@ import (

"github.com/pkg/errors"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
"sigs.k8s.io/cluster-api/internal/testtypes"
. "sigs.k8s.io/cluster-api/internal/testtypes"
Copy link
Member

Choose a reason for hiding this comment

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

I'm personally ok with this dot import given that it is our own "additional layer" on top of gomega/testing and it makes tests more readable. However, no problem to accept whatever we agree on.

)

// This code is a modified version of the mergePatch code from the controllers/topology/internal/mergepatch pkg.
// TODO: Create a shared patchdiff library that can be shared for both patching and matching.
Copy link
Member

Choose a reason for hiding this comment

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

Replicating my comment from the old PR:

Are we sure we want a separate package for that? Sometimes a little bit copying is better then a new package/dependency

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replicating my response 🙃 (sorry I didn't notice the bad branch name earlier!)

Copying is definitely the right way for now - but having this code in multiple places doesn't seem like the right solution in the long term as its usage might grow - especially if mergepatch goes out of internal. I think having an issue for this is a good idea, and we can decide down the line if we want to actually execute on it.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry looked at the wrong place for the response and didn't see it :/

Might be a good idea to open an issue for it. Not sure how the usual "process" is. TODO in the code or issue or both

Copy link
Contributor Author

@killianmuldoon killianmuldoon Sep 20, 2021

Choose a reason for hiding this comment

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

I think maybe we leave the TODO out of the code and open an issue - I always feel like a TODO should focus on something more short term, whereas this might not be resolved for a while. An issue (at least with the Kubernetes bots keeping us honest) forces us to make a decision on how to proceed on a regular basis.

I'll definitely open an issue as soon as we've decided the exact form to merge this in - and I'll take out the TODO if noone has any objections.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 20, 2021
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 23, 2021
@killianmuldoon
Copy link
Contributor Author

killianmuldoon commented Sep 23, 2021

With the new import style we have builder.NewInfrastructureClusterBuilder() which is repetitive and not as nice as it should be. I think we could transform this (and all the other builders) to builder.InfrastructureCluster and it would read much better - @sbueringer @fabriziopandini WDYT?

@killianmuldoon
Copy link
Contributor Author

killianmuldoon commented Sep 23, 2021

I've added a commit with the updated builder names - let me know if it looks like the right approach.

The only issue I see is with blocks like:

CRDs: []apiextensionsv1.CustomResourceDefinition{ *builder.GenericBootstrapConfigCRD.DeepCopy(), *builder.GenericBootstrapConfigTemplateCRD.DeepCopy(), *builder.GenericControlPlaneCRD.DeepCopy(), *builder.GenericControlPlaneTemplateCRD.DeepCopy(), *builder.GenericInfrastructureMachineCRD.DeepCopy(), *builder.GenericInfrastructureMachineTemplateCRD.DeepCopy(), *builder.GenericInfrastructureClusterCRD.DeepCopy(), *builder.GenericInfrastructureClusterTemplateCRD.DeepCopy(), *builder.GenericRemediationCRD.DeepCopy(), *builder.GenericRemediationTemplateCRD.DeepCopy(), },

@fabriziopandini
Copy link
Member

lgtm apart a small nit.
I like how this is shaping out and I think we can have a follow up iteration on CRDs

@sbueringer
Copy link
Member

Looks great!
/lgtm
from my side

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 23, 2021
This commit adds composable matching objects to the internal
testtypes package. These types can be used for advanced comparison
between objects in the Cluster API unit and integration tests.
A simple integration test is included to show the basic working of
the matchers.
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 23, 2021
@killianmuldoon
Copy link
Contributor Author

@fabriziopandini I think I've hit all the nits now and I've squashed the commits.

@fabriziopandini
Copy link
Member

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 23, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: fabriziopandini

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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 23, 2021
@sbueringer
Copy link
Member

/lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create custom matchers for internal/testtypes
4 participants