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

Refactor GCEClient: wrap compute.Service in an interface for mocking … #73

Merged
merged 1 commit into from
Apr 30, 2018

Conversation

spew
Copy link
Contributor

@spew spew commented Apr 16, 2018

…GCP compute

This change creates a ComputeService implementation which has a runtime
implementation that wraps the compute.Service. The MachineActuator is
changed to make use of the ComputeService through a new interface named
GCEClientComputeService. This will enable creating tests that mock GCP Compute
Service calls to control MachineActuator behavior.

What this PR does / why we need it:
It is difficult to test changes that interact with GCP without manually running integration tests. This change will enable developers to write unit tests that mock GCP and test that their code correctly handles various scenarios.

Release note:

NONE

@kubernetes/kube-deploy-reviewers

@spew spew requested a review from krousey April 16, 2018 23:01
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Apr 16, 2018
@krousey
Copy link
Contributor

krousey commented Apr 17, 2018

/test all

@spew
Copy link
Contributor Author

spew commented Apr 17, 2018

/retest

@k8s-ci-robot
Copy link
Contributor

@spew: you can't request testing unless you are a kubernetes or kubernetes-sigs member.

In response to this:

/retest

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.

@krousey
Copy link
Contributor

krousey commented Apr 17, 2018

/retest

}

func NewComputeService(client *http.Client) (*ComputeService, error) {
return newComputeService(client)
Copy link
Contributor

Choose a reason for hiding this comment

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

You should just move the body of newComputeService here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed, this was an artifact of the previous interface approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note this is another good thing about not locating the interface in computeservice.go.

}
mux.Handle("/compute/v1/projects/projectName/global/images/imageName", handler(nil, &responseImage))
image, err := client.ImagesGet("projectName", "imageName")
assert.Nil(t, err)
Copy link
Contributor

@krousey krousey Apr 17, 2018

Choose a reason for hiding this comment

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

If you're going to use an assertion library, at least provide useful failure messages. This is hardly helpful:

func TestFoo(t *testing.T) {                                                                                                                                                                              
    x := 1                                                                                                                                                                                                
    y := 2                                                                                                                                                                                                
    assert.Equal(t, x, y)                                                                                                                                                                                        
} 
go test                                                                                                                                                                                                   
--- FAIL: TestFoo (0.00s)                                                                                                                                                                                 
        Error Trace:                                                                                                                                                                                      
        Error:          Not equal: 1 (expected)                                                                                                                                                           
                                != 2 (actual)

Now for a more opinionated comment, please don't use assertion libraries if you can. This a request, not a mandate. I would state my reasons, but the Go team sums them up pretty neatly: https://golang.org/doc/faq#testing_framework.

Copy link
Contributor Author

@spew spew Apr 17, 2018

Choose a reason for hiding this comment

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

I think the go testing library does support letting the test finish and then all failed assertions will print which is one of the main points talked about in that FAQ. I sort of understand the anti-assertion library sentiment. I think one good thing about using the library is that the error messages are consistent and easy to scan & read. However, as you pointed out, that's not very useful if the failure messages aren't populated in which case there is a bit of a custom message anyway.

I was looking through some of the k8s.io libraries and it seems that there is a mix of using the assert library and implementing the checks. I'll go with what you suggested and rework these tests to not use assert since it seems that is somewhat the norm.

@spew
Copy link
Contributor Author

spew commented Apr 17, 2018

Updated with the following changes:

  1. Removed the 'newComputeService' method and moved the related code into NewComputeService
  2. Removed usage of the assertion library in tests and simply check the various scenarios.

@spew
Copy link
Contributor Author

spew commented Apr 27, 2018

Been out -- rebased on top of the latest commits so this can be reviewed again.

"net/url"
)

type ComputeService struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you provide documentation for all the public types?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updating with documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated with some minimal documentation that I thought was appropriate, happy to change or add more if you have something different in mind.

@@ -0,0 +1,156 @@
package clients_test
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we testing our code here? Or are we testing the GCE client code?

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 is possible that we call the wrong underlying GCE client method or that we do not properly hit the specified endpoint. We are testing that each ComputeService method calls the correct underlying GCE client method at the endpoint that we specified when creating the ComputeService.

…GCP compute

This change creates a ComputeService implementation which has a runtime
implementation that wraps the compute.Service. The MachineActuator is
changed to make use of the ComputeService through a new interface named
GCEClientComputeService. This will enable creating tests that mock GCP Compute
Service calls to control MachineActuator behavior.
@krousey
Copy link
Contributor

krousey commented Apr 30, 2018

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 30, 2018
@krousey
Copy link
Contributor

krousey commented Apr 30, 2018

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: krousey, spew

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 Apr 30, 2018
@k8s-ci-robot k8s-ci-robot merged commit 79317f9 into kubernetes-sigs:master Apr 30, 2018
csrwng pushed a commit to csrwng/cluster-api that referenced this pull request Sep 27, 2019
….2-cluster-api-0.1.0

Rebase openshift 4.2 cluster api 0.1.0
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/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants