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 test for DescribeInstances #277

Merged
merged 1 commit into from
Sep 30, 2021

Conversation

nckturner
Copy link
Contributor

@nckturner nckturner commented Sep 22, 2021

  • Add test for DescribeInstances (the lower level wrapper around the API call).
  • Use interface in awsSdkEC2 to allow for mock.
  • Check len(request.InstanceIds) == 0 to account for both empty structs and nil pointers.

See #274 for the change and #269 for some context.

What type of PR is this?

/kind cleanup

What this PR does / why we need it:
Add a test and make a comparison more resilient.

Does this PR introduce a user-facing change?:

NONE

@k8s-ci-robot k8s-ci-robot added kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. release-note-none Denotes a PR that doesn't merit a release note. 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 Sep 22, 2021
@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 22, 2021
@nckturner nckturner requested review from justinsb and removed request for justinsb September 22, 2021 17:21
@wongma7
Copy link
Contributor

wongma7 commented Sep 22, 2021

/home/prow/go/src/k8s.io/cloud-provider-aws/pkg/providers/v1/aws_test.go:3756:6: don't use underscores in Go names; func expectDescribeInstances_EmptyInput should be expectDescribeInstancesEmptyInput

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 25, 2021
tests := []struct {
name string
input *ec2.DescribeInstancesInput
expect func(interface{})
Copy link
Contributor

Choose a reason for hiding this comment

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

why is it interface{}? what is expect supposed to do? what would a failure look like? if in every test case I tell DescribeInstances what to take as input and what to return, what is being tested?

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 think I'm missing an AssertExpectations call in the test (I was a little confused as to whether I needed it while writing the test, because the test does fail if DescribeInstances is not called as expected). Let me see if I can determine if its needed and clear up the logic.

Copy link
Contributor

Choose a reason for hiding this comment

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

Documentation for On: https://pkg.go.dev/github.com/stretchr/testify/mock#Call.On
so I am telling it what to return when given so and so input. And I think ultimately I have to assert that this call happened at all by doing https://pkg.go.dev/github.com/stretchr/testify/mock#Mock.AssertExpectations

(new to this package, slightly more familiar with gomock)

Copy link
Contributor

Choose a reason for hiding this comment

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

we dont need Assert since Called basically does it for us . https://pkg.go.dev/github.com/stretchr/testify/mock?utm_source=godoc#Mock.Called

Copy link
Contributor Author

@nckturner nckturner Sep 29, 2021

Choose a reason for hiding this comment

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

Right, that's the reason that I didn't have an explicit call to AssertExpectations -- because I was seeing tests fail if I called the expected functions with unexpected arguments. That being said, I think we do want an explicit call to AssertExpectations because of the missing case. If there is an assert, and a missing call, it will only be caught by the explicit AssertExpectations, whereas Called will catch calls with improper arguments.

* Add test for DescribeInstances
* Use interface in awsSdkEC2 to allow for mock
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 29, 2021
@wongma7
Copy link
Contributor

wongma7 commented Sep 30, 2021

/lgtm

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: nckturner, wongma7

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

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. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. 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.

3 participants