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

Increase test coverage for groups/ #2873

Merged
merged 8 commits into from Nov 2, 2021

Conversation

MadhavJivrajani
Copy link
Contributor

@MadhavJivrajani MadhavJivrajani commented Oct 4, 2021

This PR adds fake clients to test out code in groups/

TODO:

  • Implement fake client
  • Write unit tests
    • Admin service
    • Group service
    • Reconcile

Fixes #420
/area groups

@k8s-ci-robot
Copy link
Contributor

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

@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. area/groups Google Groups management, code in groups/ 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. area/access Define who has access to what via IAM bindings, role bindings, policy, etc. labels Oct 4, 2021
@k8s-ci-robot k8s-ci-robot added the sig/k8s-infra Categorizes an issue or PR as relevant to SIG K8s Infra. label Oct 4, 2021
@MadhavJivrajani
Copy link
Contributor Author

/test all

@MadhavJivrajani
Copy link
Contributor Author

/assign @spiffxp
I'm hoping to make progress on this this week - in case you have early feedback, please lmk! :)

@MadhavJivrajani MadhavJivrajani force-pushed the groups-test-coverage branch 4 times, most recently from 6b8a5ce to 94abbfe Compare October 12, 2021 06:29
@MadhavJivrajani
Copy link
Contributor Author

MadhavJivrajani commented Oct 12, 2021

@spiffxp @dims while writing tests - I'm a little confused regarding the logic around creating/updating groups:

k8s.io/groups/service.go

Lines 187 to 209 in 75c0411

if group.Name != "" && grp.Name != group.Name ||
group.Description != "" && grp.Description != group.Description {
if !config.ConfirmChanges {
log.Printf("dry-run: would update group name/description %q\n", group.EmailId)
} else {
log.Printf("Trying to update group: %q\n", group.EmailId)
g := admin.Group{
Email: group.EmailId,
}
if group.Name != "" {
g.Name = group.Name
}
if group.Description != "" {
g.Description = group.Description
}
g4, err := as.client.UpdateGroup(group.EmailId, &g)
if err != nil {
return fmt.Errorf("unable to update group %q: %w", group.EmailId, err)
}
log.Printf("> Successfully updated group %s\n", g4.Email)
}
}
}

Here, in the top most if condition itself it is checked that Name and Description are non-empty and then again in the if condition it is checked. Is the intent of the top most if condition, i.e.:
if group.Name != "" && grp.Name != group.Name ||

to make sure that updates do not clear names and descriptions of the google groups?

If that is the intent then, the code above this will also need to be changed:

k8s.io/groups/service.go

Lines 168 to 181 in 75c0411

g := admin.Group{
Email: group.EmailId,
}
if group.Name != "" {
g.Name = group.Name
}
if group.Description != "" {
g.Description = group.Description
}
g4, err := as.client.InsertGroup(&g)
if err != nil {
return fmt.Errorf("unable to add new group %q: %w", group.EmailId, err)
}
log.Printf("> Successfully created group %s\n", g4.Email)

since there is a possibility of groups with empty names and descriptions being created.

@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 Oct 13, 2021
@MadhavJivrajani
Copy link
Contributor Author

/test all

@MadhavJivrajani MadhavJivrajani marked this pull request as ready for review October 13, 2021 07:15
@MadhavJivrajani
Copy link
Contributor Author

/test ?

@k8s-ci-robot
Copy link
Contributor

@MadhavJivrajani: The following commands are available to trigger required jobs:

  • /test pull-k8sio-backup
  • /test pull-k8sio-cip
  • /test pull-k8sio-file-promo
  • /test pull-k8sio-groups-test
  • /test pull-k8sio-terraform-kubernetes-public
  • /test pull-k8sio-terraform-prow-build
  • /test pull-k8sio-terraform-prow-build-trusted
  • /test pull-k8sio-terraform-public-pii
  • /test pull-k8sio-verify

Use /test all to run the following jobs that were automatically triggered:

  • pull-k8sio-groups-test
  • pull-k8sio-verify

In response to this:

/test ?

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 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 Oct 14, 2021
@MadhavJivrajani MadhavJivrajani changed the title [WIP] Increase test coverage for groups/ Increase test coverage for groups/ Oct 14, 2021
@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 Oct 14, 2021
@MadhavJivrajani
Copy link
Contributor Author

MadhavJivrajani commented Oct 14, 2021

@spiffxp the PR's ready for review, I've added the required tests, PTAL :)

Signed-off-by: Madhav Jivrajani <madhav.jiv@gmail.com>
- Stub out clientErrCheckFunc for testing so that returned errors
  from fake client can be checked easily.
- Add methods to create admin and group services by taking in custom
  clients and clientErrCheckFunc

Signed-off-by: Madhav Jivrajani <madhav.jiv@gmail.com>
Signed-off-by: Madhav Jivrajani <madhav.jiv@gmail.com>
Signed-off-by: Madhav Jivrajani <madhav.jiv@gmail.com>
Signed-off-by: Madhav Jivrajani <madhav.jiv@gmail.com>
Copy link
Member

@spiffxp spiffxp left a comment

Choose a reason for hiding this comment

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

I've got some style / corner-case nits. Overall though I think this is great coverage.

groups/fake/fake_client.go Outdated Show resolved Hide resolved
Comment on lines +28 to +32
// Groups is a mapping from groupKey to *admin.Group
Groups map[string]*admin.Group
// Members is a mapping from groupKey -> members of that group
// Members of a group are a mapping from memberKey -> *admin.Member
Members map[string]map[string]*admin.Member
Copy link
Member

Choose a reason for hiding this comment

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

map[string]map[string] feels off. Makes me wonder if we're missing a type that holds both the Group and its Members

groups/fake/fake_client.go Outdated Show resolved Hide resolved
groups/reconcile_test.go Outdated Show resolved Hide resolved
groups/service_test.go Outdated Show resolved Hide resolved
Comment on lines +379 to +381
"ReconcileMembers": "true",
},
Members: []string{}, // member removed
Copy link
Member

Choose a reason for hiding this comment

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

Do we have a test case that confirms ReconcileMembers: false behavior for a member? Like proof that attempting to remove a member won't work if this setting is false

"k8s.io/k8s.io/groups/fake"
)

func augmentAdminServiceFakeClient(fakeClient *fake.FakeAdminServiceClient) *fake.FakeAdminServiceClient {
Copy link
Member

Choose a reason for hiding this comment

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

Ah, here's where the fixtures are hidden. I feel like it would be better to put these closer to the reconcile tests so I don't have to keep bouncing back and forth here. I don't have a suggestion off the top of my head though.

FWIW I also wonder if it'd be better to pass this data in via the constructor, then less code needs to be aware it's dealing with / mutating a fake

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about providing a constructor that returns an augmented fake client?

groups/service_test.go Outdated Show resolved Hide resolved
groups/service_test.go Show resolved Hide resolved
groups/service.go Outdated Show resolved Hide resolved
Signed-off-by: Madhav Jivrajani <madhav.jiv@gmail.com>
Signed-off-by: Madhav Jivrajani <madhav.jiv@gmail.com>
Signed-off-by: Madhav Jivrajani <madhav.jiv@gmail.com>
Comment on lines +222 to +232
// desired state is not nescessarily the same as expected state.
// We might need the two of them to differ in cases where we want
// to test out functionality where our desired state warrants for
// a change but in actuality this change should not occur - which
// is signified via expected state. shouldConsiderExpectedState is
// what differentiates what we should consider when comparing results.
// If false - desired is same as expected. This is done because in
// most cases these two are same and we can avoid clutter.
shouldConsiderExpectedState bool
desiredState []GoogleGroup
expectedState []GoogleGroup
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@spiffxp I've added a test for testing out reconciliation for ReconcileMembers setting set to "false". In order to do so, we would need another dimension called expectedState to verify if things worked correctly for such a case. However, adding expectedState to all test cases resulted in a lot of bloat - which is why I went with this approach. Let me know if this is okay or if I should change it.

@MadhavJivrajani
Copy link
Contributor Author

@spiffxp I've addressed most comments, PTAL

Copy link
Member

@spiffxp spiffxp left a comment

Choose a reason for hiding this comment

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

I think the test data could be pruned down a bit still but this has been hanging out enough, improvements can be done as followup

/approve
/lgtm

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: MadhavJivrajani, spiffxp

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 Nov 2, 2021
@k8s-ci-robot k8s-ci-robot merged commit bdc301b into kubernetes:main Nov 2, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.23 milestone Nov 2, 2021
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. area/access Define who has access to what via IAM bindings, role bindings, policy, etc. area/groups Google Groups management, code in groups/ 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. sig/k8s-infra Categorizes an issue or PR as relevant to SIG K8s Infra. 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.

groups: needs unit tests
3 participants