Skip to content

OSD-10308 added unit test for ensure forwarding rule - #255

Closed
ritmun wants to merge 8 commits into
openshift:masterfrom
ritmun:OSD-10308
Closed

ritmun wants to merge 8 commits into
openshift:masterfrom
ritmun:OSD-10308

Conversation

@ritmun

@ritmun ritmun commented Apr 9, 2022

Copy link
Copy Markdown
Contributor

Maintenance sprint PR. Added unit test to cover recently added GCP code.
Jira: https://issues.redhat.com/browse/OSD-10308

@openshift-ci
openshift-ci Bot requested review from fahlmant and robotmaxtron April 9, 2022 02:08
@openshift-ci

openshift-ci Bot commented Apr 9, 2022

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: ritmun
To complete the pull request process, please assign sam-nguyen7 after the PR has been reviewed.
You can assign the PR to them by writing /assign @sam-nguyen7 in a comment when ready.

The full list of commands accepted by this bot can be found here.

Details 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

@codecov

codecov Bot commented Apr 9, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #255 (8004bc5) into master (3bee4a2) will decrease coverage by 2.23%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #255      +/-   ##
==========================================
- Coverage   41.93%   39.70%   -2.24%     
==========================================
  Files          33       32       -1     
  Lines        2592     2297     -295     
==========================================
- Hits         1087      912     -175     
+ Misses       1427     1315     -112     
+ Partials       78       70       -8     
Impacted Files Coverage Δ
pkg/cloudclient/gcp/private.go 10.09% <100.00%> (+2.63%) ⬆️
...g/cloudclient/mock_cloudclient/mock_cloudclient.go 0.00% <0.00%> (-22.59%) ⬇️
...ublishingstrategy/publishingstrategy_controller.go 71.75% <0.00%> (-3.09%) ⬇️
pkg/testutils/testutils.go 83.68% <0.00%> (ø)
pkg/cloudclient/cloudclient.go 33.33% <0.00%> (ø)
.../cloudingress/v1alpha1/publishingstrategy_types.go 100.00% <0.00%> (ø)
pkg/controller/add_sshd.go
pkg/controller/sshd/sshd_controller.go
pkg/apis/cloudingress/v1alpha1/sshd_types.go
pkg/ingresscontroller/addtoscheme.go 100.00% <0.00%> (ø)
... and 6 more

Comment thread go.mod Outdated
github.com/openshift/cluster-api-provider-gcp v0.0.0
github.com/openshift/machine-api-operator v0.2.1-0.20200226185612-9b0170a1ba07
github.com/openshift/operator-custom-metrics v0.4.2
github.com/openshift/origin v0.0.0-20160503220234-8f127d736703

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's pulling this in?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed this by go mod tidy

Comment thread pkg/cloudclient/gcp/gcp_test.go Resolved
@ritmun
ritmun requested a review from boranx April 11, 2022 19:55
@ritmun
ritmun requested a review from fahlmant April 12, 2022 15:51
@fahlmant

Copy link
Copy Markdown
Contributor

/retest

@ritmun

ritmun commented Apr 12, 2022

Copy link
Copy Markdown
Contributor Author

/cc @lnguyen1401 @abyrne55

@openshift-ci
openshift-ci Bot requested review from abyrne55 and lnguyen1401 April 12, 2022 17:44
@ritmun

ritmun commented Apr 12, 2022

Copy link
Copy Markdown
Contributor Author

/cc @anispate

@openshift-ci
openshift-ci Bot requested a review from anispate April 12, 2022 18:15
rhapiLbIP := ingressList[0].IP
// ensure forwarding rule exists in GCP for service
err := gc.ensureGCPForwardingRuleForExtIP(rhapiLbIP)
err := gc.ensureGCPForwardingRuleForExtIP(rhapiLbIP, getForwardingRuleList)

@fahlmant fahlmant Apr 13, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can ensureGCPForwardingRuleForExtIp call getForwardingRuleList directly given they're in the same package?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This follows higher order function mocking. It's one of the ways of mocking a function so that we cover precisely the function we're testing. The higher order function is private and is kept extremely simple to isolate the external dependency. https://medium.com/@tahasevim231/mocking-with-high-order-functions-in-go-b76d5a3103d2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking at this again, I'm not convinced getForwadingRuleList needs to be its own function at this point. Its only ever called once and its not a method of the gcp Client but instead takes it as an argument so I don't see much value in it being a function that's only available in this package as it current stands

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's for mocking the unit test correctly. This is a common method of unit testing. It's increasing coverage, so it's good for the code overall.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This changes the ergonomics of the code and makes it fairy complicated going forward but with no functional change, except to make mocking tests simpler. We have other functions with a similar pattern that would also all need to be refactored to this pattern if we want to test them as well.

IMO, we should instead abstract out the GCP API client from the gc Client to an interface with the list of required methods. That way we can pass in a mocked or "real" implementation to the gc Client struct.

@openshift-ci openshift-ci Bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 14, 2022
@openshift-ci openshift-ci Bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 6, 2022
@openshift-ci

openshift-ci Bot commented Jul 6, 2022

Copy link
Copy Markdown
Contributor

@ritmun: all tests passed!

Full PR test history. Your PR dashboard.

Details

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. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants