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 tests for KubeadmControlPlane create workflow #1925

Merged
merged 1 commit into from
Dec 30, 2019

Conversation

detiber
Copy link
Member

@detiber detiber commented Dec 18, 2019

What this PR does / why we need it:

  • Adds tests for the KubeadmControlPlane create workflow
  • Addresses bugs that were found while adding testing.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Related to #1756

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Dec 18, 2019
@k8s-ci-robot k8s-ci-robot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Dec 18, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: detiber

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 Dec 18, 2019
@@ -162,6 +162,9 @@ func GenerateSecretWithOwner(clusterName types.NamespacedName, data []byte, owne
ObjectMeta: metav1.ObjectMeta{
Name: secret.Name(clusterName.Name, secret.Kubeconfig),
Namespace: clusterName.Namespace,
Labels: map[string]string{
clusterv1.ClusterLabelName: clusterName.Name,
},
Copy link
Member Author

Choose a reason for hiding this comment

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

Fixes missing cluster-name label on generated kubeconfig secrets

Comment on lines 50 to 92
type fakeClientWrapper struct {
fakeClient client.Client
}

func NewFakeClientWrapperWithScheme(clientScheme *runtime.Scheme, initObjs ...runtime.Object) client.Client {
fakeClient := fake.NewFakeClientWithScheme(clientScheme, initObjs...)
return &fakeClientWrapper{fakeClient: fakeClient}
}

func (f fakeClientWrapper) Create(ctx context.Context, obj runtime.Object, opts ...client.CreateOption) error {
u, ok := obj.(*unstructured.Unstructured)
if ok {
if u.GetName() == "" && u.GetGenerateName() != "" {
u.SetName(u.GetGenerateName() + util.RandomString(6))
}
if u.GroupVersionKind().Kind == "GenericMachine" && u.GroupVersionKind().GroupVersion() == externalv1.GroupVersion {
gm := &externalv1.GenericMachine{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), gm); err == nil {
return f.fakeClient.Create(ctx, gm, opts...)
}
}
return f.fakeClient.Create(ctx, u, opts...)
}

return f.fakeClient.Create(ctx, obj, opts...)
}

func (f fakeClientWrapper) Update(ctx context.Context, obj runtime.Object, opts ...client.UpdateOption) error {
return f.fakeClient.Update(ctx, obj, opts...)
}

func (f fakeClientWrapper) Delete(ctx context.Context, obj runtime.Object, opts ...client.DeleteOption) error {
return f.fakeClient.Delete(ctx, obj, opts...)
}

func (f fakeClientWrapper) DeleteAllOf(ctx context.Context, obj runtime.Object, opts ...client.DeleteAllOfOption) error {
return f.fakeClient.DeleteAllOf(ctx, obj, opts...)
}

func (f fakeClientWrapper) Patch(ctx context.Context, obj runtime.Object, patch client.Patch, opts ...client.PatchOption) error {
return f.fakeClient.Patch(ctx, obj, patch, opts...)
}

func (f fakeClientWrapper) List(ctx context.Context, list runtime.Object, opts ...client.ListOption) error {
return f.fakeClient.List(ctx, list, opts...)
}

func (f fakeClientWrapper) Get(ctx context.Context, key client.ObjectKey, obj runtime.Object) error {
return f.fakeClient.Get(ctx, key, obj)
}

func (f fakeClientWrapper) Status() client.StatusWriter {
return f.fakeClient.Status()
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Ugly workaround for fake client not populating Name when GenerateName is used as well as handling of resources created as unstructured and then subsequently accessed as a typed list.

@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Dec 18, 2019
} else {
g.Expect(kubeconfigSecret.Labels).To(gomega.Equal(existingKubeconfigSecret.Labels))
g.Expect(kubeconfigSecret.Data).To(gomega.Equal(existingKubeconfigSecret.Data))
g.Expect(kubeconfigSecret.OwnerReferences).NotTo(gomega.ContainElement(*metav1.NewControllerRef(kcp, controlplanev1.GroupVersion.WithKind("KubeadmControlPlane"))))
Copy link
Member

@neolit123 neolit123 Dec 18, 2019

Choose a reason for hiding this comment

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

do these have sufficient output indicating what fields are problematic without annotations:
g.Expect(...., "some annotation")?

we had such a general problem in k/k/test, but in this case it might not be needed.

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 haven't had much issue with them in the past, mostly because they point directly to the line that caused the error, we can always add annotations later if it starts to become an issue, though.

@k8s-ci-robot k8s-ci-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Dec 19, 2019
@k8s-ci-robot k8s-ci-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Dec 19, 2019
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 20, 2019
Copy link
Contributor

@chuckha chuckha left a comment

Choose a reason for hiding this comment

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

comment around test refactor into one test testing one thing (e.g. one TestFunction for each error condition we want to test)

gist around how to remove the generic resources: https://gist.github.com/chuckha/b01acc137b8b5cd8c0daa28e0bb10636

controllers/external/util_test.go Outdated Show resolved Hide resolved
@detiber
Copy link
Member Author

detiber commented Dec 23, 2019

First breakout PR: #1959

@detiber detiber force-pushed the kcpCreateTests branch 3 times, most recently from f069641 to 0b26064 Compare December 23, 2019 21:49
@detiber
Copy link
Member Author

detiber commented Dec 23, 2019

Second breakout PR: #1961

@detiber detiber force-pushed the kcpCreateTests branch 3 times, most recently from def6e81 to 71ee133 Compare December 26, 2019 20:49
@detiber detiber changed the title [WIP] 🐛 Add tests for KubeadmControlPlane create workflow 🐛 Add tests for KubeadmControlPlane create workflow Dec 26, 2019
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 26, 2019
Copy link
Contributor

@chuckha chuckha left a comment

Choose a reason for hiding this comment

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

Just one comment. This looks great! I'm so happy we were able to avoid the generic machines

- Add additional generators for kubeadmcontrolplane reconciliation testing
Copy link
Contributor

@chuckha chuckha left a comment

Choose a reason for hiding this comment

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

/lgtm

amped on the code coverage here, really nice job :)

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 30, 2019
@k8s-ci-robot k8s-ci-robot merged commit b3526e1 into kubernetes-sigs:master Dec 30, 2019
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/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants