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(#6) #22

Merged
merged 2 commits into from
Oct 29, 2019
Merged

Conversation

pratyushprakash
Copy link
Contributor

@pratyushprakash pratyushprakash commented Oct 17, 2019

  • Refactor Run to take in ReaperContext

  • Refactor podreaper command line to build a ReaperContext

@pratyushprakash pratyushprakash requested a review from a team as a code owner October 17, 2019 17:13
@CLAassistant
Copy link

CLAassistant commented Oct 17, 2019

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Oct 17, 2019

Codecov Report

Merging #22 into master will increase coverage by 2.03%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #22      +/-   ##
==========================================
+ Coverage   53.38%   55.42%   +2.03%     
==========================================
  Files           4        4              
  Lines         826      821       -5     
==========================================
+ Hits          441      455      +14     
+ Misses        338      317      -21     
- Partials       47       49       +2
Impacted Files Coverage Δ
pkg/reaper/podreaper/podreaper.go 76.66% <100%> (+14.26%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ca05516...52fc249. Read the comment docs.

@tekenstam
Copy link
Member

tekenstam commented Oct 17, 2019

Thanks @pratyushprakash! Related to #6.

@eytan-avisror
Copy link
Collaborator

Hi @pratyushprakash
Thanks.
I don't really understand the changes you are suggesting.
Can you provide an explanation on why we should change this?
Essentially it seems you moved some of the mocking code from the test files to the actual pod_reaper code.. how does this improve the coverage?

Copy link
Collaborator

@eytan-avisror eytan-avisror left a comment

Choose a reason for hiding this comment

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

If the goal is to make Run() testable, we should instantiate the clients at a higher level and pass in an interface to Run() (or a struct of interfaces)
Then in the test code we can simply pass fake/mock clients instead of real ones

@@ -29,7 +32,7 @@ import (
var log = logrus.New()

// Run is the main runner function for pod-reaper, and will initialize and start the pod-reaper
func Run(args *Args) error {
func Run(args *Args, fakePods ...[]FakePod) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should not make this change, adding faking at this level is not a correct refactor in my opinion.
Run should simply take in kubernetes.Interface and a fake client could be passed in at a higher level

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem here is that a Reaper Context is built in the Run function. Could we maybe pass the context in its entirety instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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 agree that we should keep mocks only in testing. How would we bypass validateArguments in this scenario.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think a refactor should move both the validate and client instantiation out to the cli package that runs.
We can then pass some struct like ReaperContext into Run() -> however, this is a big change and should probably be part a separate PR.
If you want to do this, we can discuss further about how to re-design this.. but essentially you can look at the design of https://github.com/keikoproj/lifecycle-manager/blob/master/cmd/serve.go vs. https://github.com/keikoproj/lifecycle-manager/blob/master/pkg/service/server.go#L33 - in this project we do exactly that for testability and it just makes testing much better as far as clients/mocking/faking goes.

@@ -41,6 +44,11 @@ func Run(args *Args) error {
return err
}

if len(fakePods) > 0 && args.K8sConfigPath == "fake" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same for this, all mocking/faking should happen within the test 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.

👍

@@ -217,3 +225,81 @@ func (ctx *ReaperContext) validateArguments(args *Args) error {

return nil
}

func loadFakeAPI(ctx *ReaperContext) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need this moved to podreaper.go it's the same package, we can keep this code under podreaper_test.go

}
}

func createFakePods(pods []FakePod, ctx *ReaperContext) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need this moved to podreaper.go it's the same package, we can keep this code under podreaper_test.go

@@ -40,3 +42,15 @@ type Args struct {
SoftReap bool
DryRun bool
}

// FakePod is the pod struct used for testing
type FakePod struct {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need this moved to types.go it's the same package, we can keep this code under podreaper_test.go

* Refactor `Run` to take in `ReaperContext`

* Refactor podreaper command line to build a `ReaperContext`
@pratyushprakash
Copy link
Contributor Author

@eytan-avisror Could you take a look at this again?

@eytan-avisror
Copy link
Collaborator

Thanks @pratyushprakash this change looks much better.
I will do some tests and approve shortly if no issues

@eytan-avisror eytan-avisror merged commit 0b5d58b into keikoproj:master Oct 29, 2019
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.

None yet

4 participants