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 mock client and 2 tests for networking package #935

Merged
merged 1 commit into from
Jul 22, 2021

Conversation

macaptain
Copy link
Contributor

@macaptain macaptain commented Jul 12, 2021

Refactors the Service in the networking package to use a client with CRUD functions for manipulating OpenStack resources. This interface is combined with mockgen code generation to make the package unit testable.

Two tests are added to illustrate how writing tests for the package will work. The list / filter networks and subnets functions are also slightly simplified.

What this PR does / why we need it:

A refactor of the code to allow unit tests to be written for code that uses gophercloud to interface with OpenStack.

Which issue(s) this PR fixes:
Goes some way to fixing #843.

Special notes for your reviewer:

If you like this change, my plan is to move some of the 'ports' logic to the networking package and write unit tests for it, and then apply the same changes, adding mock clients for the Service in the compute package.

I tried a few things out for making the code unit testable, including using gomega's ghttp server for mocking (too much JSON), or trying to write smaller interfaces (ended up being a lot of boiler plate and manual writing of mocks), and ended up settling on this. It's a similar pattern to what's used in cluster-api-provider-aws (see instance_test.go and the generated mocks: https://github.com/kubernetes-sigs/cluster-api-provider-aws/tree/main/pkg/cloud/services/ec2), as well as using the gomega library for matching (as recommended in the Cluster API book section on Testing.)

TODOs:

  • squashed commits
  • if necessary:
    • includes documentation
    • adds unit tests

/hold

@k8s-ci-robot k8s-ci-robot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Jul 12, 2021
@k8s-ci-robot
Copy link
Contributor

Hi @macaptain. Thanks for your PR.

I'm waiting for a kubernetes-sigs 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 needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jul 12, 2021
@jichenjc
Copy link
Contributor

/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 Jul 13, 2021
@macaptain macaptain force-pushed the feat-add-unit-tests branch from 1d35770 to ca8ccdb Compare July 13, 2021 06:44
Copy link
Contributor

@lentzi90 lentzi90 left a comment

Choose a reason for hiding this comment

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

This looks great!

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 14, 2021
@macaptain macaptain force-pushed the feat-add-unit-tests branch from ca8ccdb to e10d1bf Compare July 16, 2021 06:42
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 16, 2021
@macaptain
Copy link
Contributor Author

This PR is two commits to make it a bit easier to review. After rebasing for the metrics changes in #932, I decided to move other metrics into the Client implementation as well for consistency in a second commit. I can squash the commits before merging if desired.

@jichenjc
Copy link
Contributor

/lgtm

I think it's great PR , @hidekazuna can you take a look so that we can merge it soon (to avoid various conflict)
and if any followup needed we can anyway do it later on

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 19, 2021
return ports.Create(c.serviceClient, opts).Extract()
}

func (c networkClient) DeletePort(id string) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest we consider add metrics for those kind of API call as well , but no need to be in this PR as this is a pure refactory

@macaptain macaptain force-pushed the feat-add-unit-tests branch from 5ca1033 to 300cf9c Compare July 21, 2021 12:11
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 21, 2021
@jichenjc
Copy link
Contributor

/lgtm

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

Can we update like following?

networking/
├── client.go
├── floatingip.go
├── floatingip_test.go
├── mock_networking
│ ├── client.go -> rename to client_mock.go
│ ├── doc.go
│ └── mock_client -> delete.
│ └── client.go -> delete.
.......

Because CAPZ is the following.

subnets/
├── client.go
├── mock_subnets
│ ├── client_mock.go
│ ├── doc.go
│ └── subnets_mock.go
├── subnets.go
└── subnets_test.go

Refactors the Service in the networking package to use a client with
CRUD functions for manipulating OpenStack resources. This interface is
combined with mockgen code generation to make the package unit testable.

Two tests are added to illustrate how writing tests for the package will
work.

Metrics recording is also refactored to Client. It makes sense to take
the metrics at the site where the OpenStack call is made, and also
tidies up the 'business logic' functions when metrics recording is the
responsibility of the network client implementation.
@macaptain macaptain force-pushed the feat-add-unit-tests branch from 300cf9c to efe8353 Compare July 22, 2021 06:47
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 22, 2021
@macaptain
Copy link
Contributor Author

Can we update like following?

Yes, of course. I've updated the folder structure and filenames to match. The mock package is called mock_networking and the generated file is client_mock.go, and mock_client and mock_client.go have been removed.

@jichenjc
Copy link
Contributor

/lgtm

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

@macaptain Thanks updating.

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: hidekazuna, lentzi90, macaptain

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 Jul 22, 2021
@hidekazuna
Copy link
Contributor

/hold cancel

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. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. 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.

5 participants