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

chore: Refactor test helpers into reusable ktest library #398

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

justinsb
Copy link
Contributor

This will avoid a circular (module) dependency between
mockkubeapiserver and kdp itself.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: justinsb

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 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. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 15, 2024
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 15, 2024
@justinsb
Copy link
Contributor Author

Sadly it looks like I can't start using this / remove the old pkg/test directories in the same PR.

I feel like I'm using modules "wrong", I guess we should be using a separate repo. Even if we were using a separate repo, we would have the same sequence of PRs, I guess.

@justinsb
Copy link
Contributor Author

(This is breaking down #383 into its required commits)

@@ -41,6 +41,7 @@ echo "kubectl version is $(kubectl version --client)"
rm -f go.work go.work.sum
go work init .
go work use applylib
go work use ktest
Copy link
Member

Choose a reason for hiding this comment

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

This, even more, suggests to me that the workspace file should really be checked into the repo (assuming, which I think is true but haven't double-checked, that all referenced modules live within the same repo). Doing that would, I believe, help with making local development easier, so that one can just clone the repo and have it work both in an editor and with other tooling (e.g. go test) without having to first figure out how to make the modules reference each-other in sane ways.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, my only concern is that I haven't seen it elsewhere, but given the pain of not having it checked in you have persuaded me and I think it's worth doing. If we find something unexpected, then we can revert it. I'll do a separate PR I guess where we do all this in one (that's the hope, anyway!)

Comment on lines 31 to 32
if err != nil {
panic("failed to read request body")
Copy link
Member

Choose a reason for hiding this comment

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

Why not return an error value here, since the signature allows it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call - done!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great call. Done

}
}

func resetTimestamp(body string) string {
Copy link
Member

Choose a reason for hiding this comment

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

This method replaces LastTransitionTime on conditions, but does not care about other timestamps in the payload (e.g. metadata.creationTimestamp). Should it?

}

type RequestLog struct {
// We have seen dropped logs from concurrent requests
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
// We have seen dropped logs from concurrent requests
// Avoid risking dropped logs from concurrent requests

The previous comment makes it a little ambiguous if we've seen dropped logs without this mutex (i.e. the comment explains why we have it) or if we've seen it despite the mutext (implying there might be a concurrency bug somewhere that merits more investigation at some point).

This suggestion is assuming the former is the right interpretation.

for i := range l.entries {
request := &l.entries[i].Request
u := request.URL
r, err := regexp.Compile(find)
Copy link
Member

Choose a reason for hiding this comment

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

This should probably be done once, outside the loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, done

u := request.URL
r, err := regexp.Compile(find)
if err != nil {
klog.Fatalf("failed to compile regex %q: %v", find, err)
Copy link
Member

Choose a reason for hiding this comment

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

Can we make this return the error instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It makes it harder to use, but I changed this method to accept a t *testing.T as its first parameter; this shows it should be used from test code, and then we can easily call t.Fatalf (which is probably equivalent to a panic anyway, but now we're guarding it to be used from test code)

This will avoid a circular (module) dependency between
mockkubeapiserver and kdp itself.
@justinsb
Copy link
Contributor Author

justinsb commented Jul 2, 2024

Cleaned this up, but let's try adding go.work in #383

@justinsb
Copy link
Contributor Author

justinsb commented Jul 2, 2024

/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 Jul 2, 2024
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. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. 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.

None yet

3 participants