Skip to content

Activator unit tests#1689

Merged
josephburnett merged 13 commits intoknative:masterfrom
tanzeeb:activator-unit-tests
Aug 10, 2018
Merged

Activator unit tests#1689
josephburnett merged 13 commits intoknative:masterfrom
tanzeeb:activator-unit-tests

Conversation

@tanzeeb
Copy link
Copy Markdown
Contributor

@tanzeeb tanzeeb commented Jul 25, 2018

Refactored retryRoundTripper and activationHandler into multiple
components to make testing simpler.

Fixes #1231

(Continuation of #1585)

@google-prow-robot google-prow-robot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Jul 25, 2018
@tanzeeb
Copy link
Copy Markdown
Contributor Author

tanzeeb commented Jul 25, 2018

/assign @josephburnett @akyyy @mdemirhan

Comment thread cmd/activator/handlers.go Outdated
}
}

func (h uploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
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 you define the method on the pointer rather than the value type?
func (h *uploadHandler) ServeHTTP(...)

Comment thread cmd/activator/handlers_test.go Outdated
}
}

func (fa fakeActivator) ActiveEndpoint(namespace, name string) (activator.Endpoint, activator.Status, error) {
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.

Define the func on the pointer rather than the value type.

Comment thread cmd/activator/round_trippers.go Outdated
var baseTransport http.RoundTripper = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
var transport http.RoundTripper = http.DefaultTransport
if r.ProtoMajor == 2 {
transport = h2cutil.DefaultTransport
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.

My preference would be getting rid of this func and providing http1 and http2 transports as inputs to the rountripper. That way, we will get better coverage (i.e. the logic to select rounttripper is also covered) and also the tests wouldn't have to know the internals of rounttripper as much.

See: https://github.com/mdemirhan/serving/blob/2bf10f2fca8e56ea174b3259287f62bc2385f26c/pkg/activator/httptransport.go#L33 as an example.

And corresponding fake transport: https://github.com/mdemirhan/serving/blob/2bf10f2fca8e56ea174b3259287f62bc2385f26c/pkg/activator/httptransport_test.go#L38

And table tests:
https://github.com/mdemirhan/serving/blob/2bf10f2fca8e56ea174b3259287f62bc2385f26c/pkg/activator/httptransport_test.go#L69

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 feels a little weird to have RetryRoundTripper be concerned with transport protocol selection. If there was better test coverage, would you be OK with keeping this logic separate?

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.

Yes, that sounds good to me. Thanks!

Comment thread cmd/activator/round_trippers.go Outdated
if resp.StatusCode == status {
resp.Body.Close()

return nil, fmt.Errorf("Filtering %d", status)
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.

Converting an HTTP response to error doesn't seem right. This will definitely confuse people who are debugging this.

Instead of making this two roundtrippers that are chained together, can you simply get a func as an input: something like type ShouldRetryFunc func(*http.Response) (bool) and removing the statusFilterRoundTripper in favor of this func.

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.

Yeah, that makes sense. I'll take a stab at this.

Comment thread cmd/activator/round_trippers.go Outdated
}

// TODO: add metrics for number of tries and the response code.
if resp != nil {
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 you change this so that we also log error cases? It seems weird that we log happy path but not unhappy path. Something like:

	if resp != nil {
		rrt.logger.Infof("Activation finished after %d attempt(s). Response code: %d", i, resp.StatusCode)
	} else {
		rrt.logger.Errorf("Activation failed after %d attempts. Last error: %v", i, err)
	}

Comment thread cmd/activator/round_trippers_test.go Outdated
status int
err error
}{
{"filtered status", goodRT, 502, errors.New("Filtering 502")},
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.

Test cases here seem to be too limited. Can we add tests to cover all the cases listed below?

https://github.com/mdemirhan/serving/blob/2bf10f2fca8e56ea174b3259287f62bc2385f26c/pkg/activator/httptransport_test.go#L81

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.

Sure, I'll add test cases for http1/http2. I think everything else is covered in other, separate tests?

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.

Actually there are a few more. List of test cases to add so far:

  • http1/http2
  • nil response body

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.

Is the nil response body check actually necessary? From the source, Response Body is guaranteed to be non-nil:

https://github.com/golang/go/blob/master/src/net/http/response.go#L57-L62

@google-prow-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: tanzeeb
To fully approve this pull request, please assign additional approvers.
We suggest the following additional approver: mattmoor

If they are not already assigned, you can assign the PR to them by writing /assign @mattmoor in a comment when ready.

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

The pull request process is described 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

@tanzeeb
Copy link
Copy Markdown
Contributor Author

tanzeeb commented Aug 2, 2018

Thanks for the feedback @mdemirhan. I've made some updates to the RetryRoundTripper and removed StatusFilterRoundTripper. The components have also been moved out of cmd/activator -- pkg/activator for ActivationHandler, and cmd/util for all others.

@nader-ziada nader-ziada force-pushed the activator-unit-tests branch from f57f317 to 41287ba Compare August 9, 2018 17:55
@knative-prow-robot knative-prow-robot removed the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Aug 9, 2018
@knative-prow-robot knative-prow-robot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Aug 9, 2018
@knative-metrics-robot
Copy link
Copy Markdown

The following is the coverage report on pkg/.
Say /test pull-knative-serving-go-coverage to run the coverage report again

File Old Coverage New Coverage Delta
pkg/activator/util/retryer.go Do not exist 100.0%
pkg/activator/util/rewinder.go Do not exist 90.0%
pkg/activator/util/transports.go Do not exist 100.0%
pkg/activator/handler/enforce_length_handler.go Do not exist 100.0%
pkg/activator/handler/handler.go Do not exist 100.0%
pkg/activator/handler/reporting_handler.go Do not exist 100.0%

@nader-ziada
Copy link
Copy Markdown
Member

@mdemirhan @josephburnett

Please take another look - just rebased this PR while @tanzeeb is out on PTO.

/cc @dprotaso

Copy link
Copy Markdown
Member

@dprotaso dprotaso left a comment

Choose a reason for hiding this comment

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

I helped wrap this up so I approve ;)


// NewLinearRetryer will return a retryer that retries `action` up to
// `maxRetries` times with `interval` delay between retries
func NewLinearRetryer(interval time.Duration, maxRetries int) Retryer {
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.

@markusthoemmes is adding exponential backoff in #1814 and I've suggested to @pivotal-sukhil-suresh to reuse that code in #1665 (review). This would be a good way to do that reuse, with an ExponentialRetrier.

So there are three changes in-flight in this space. I'm not sure what order they should land in. Any suggestions?

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.

I'd suggest to land this one first. Then I can base my PR on top of it (I felt bad because I had no tests anyway) and then @pivotal-sukhil-suresh can base off of mine to gain the ability of having an exponential backoff. WDYT @josephburnett

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.

That would be fine.

Copy link
Copy Markdown
Contributor

@josephburnett josephburnett left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Aug 10, 2018
@knative-prow-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dprotaso, josephburnett, tanzeeb

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

The pull request process is described 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

@knative-prow-robot knative-prow-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 10, 2018
@josephburnett
Copy link
Copy Markdown
Contributor

@mdemirhan is on leave. It looks like all his review comments have been addressed.

@nader-ziada
Copy link
Copy Markdown
Member

Thanks @josephburnett, will look at #1665 with @pivotal-sukhil-suresh after @markusthoemmes PR is merged in.

tanzeeb added a commit to tanzeeb/serving that referenced this pull request Aug 15, 2018
Was originally fixed in knative#1733, but accidentally reverted in knative#1689
@tanzeeb tanzeeb mentioned this pull request Aug 15, 2018
@trisberg trisberg mentioned this pull request Aug 16, 2018
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. lgtm Indicates that a PR is ready to be merged. 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.

10 participants