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

Test refactor in preparation for change authz header initialization #1127

Merged
merged 3 commits into from
Aug 21, 2019

Conversation

absoludity
Copy link
Contributor

@absoludity absoludity commented Aug 20, 2019

Since the client will be able to pass an AppRepository name instead of the actual Auth object, I'll need to initialise the authHeader during InitNetClient when the custom resource will be fetched (as we already fetch the CustomCA there too).

In preparation for that, I want to move the existing code which fetches the auth header from the GetChart call to InitNetClient, so both will be consistent - that'll be the next PR. This PR just does a little preparation, refactoring the GetChart tests so that I can add the auth check cleanly in the next PR.

A few notes inline.

Ref #1110

}

func (f *fakeHTTPClient) Do(h *http.Request) (*http.Response, error) {
// Record the request for later test assertions.
f.requests = append(f.requests, h)
if f.userAgent != "" && h.Header.Get("User-Agent") != f.userAgent {
return nil, fmt.Errorf("Wrong user agent: %s", h.Header.Get("User-Agent"))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we're recording the requests now - and can assert on the actual requests in the context of the test, we could simplify this fake Do implementation, removing the test assertions like this one? That is, rather than the test not actually doing any assertion (other than that an error was not returned), we can assert the user-agent was set as expected in the context of the test, rather than in this fake code (which has no access to the t.Testing to call t.Errorf etc). See what you think - I won't do it in this PR, but just a thought.

Copy link
Contributor

Choose a reason for hiding this comment

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

that sounds good to me, the only reason for doing the assertion here is that we couldn't do it in the test. If we can I am happy to do it.

@@ -420,15 +424,13 @@ func (f *fakeHTTPClient) Do(h *http.Request) (*http.Response, error) {
// Return fake chart index (not customizable per repo)
body, err := json.Marshal(*f.index)
if err != nil {
fmt.Printf("Error! %v", err)
return nil, err
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doesn't seem right to be printing to stdout and continuing on with a 200 response in this case, so verified that tests still pass if I return an error here.

}
return &http.Response{StatusCode: 200, Body: ioutil.NopCloser(bytes.NewReader(body))}, nil
}
}
for _, chartURL := range f.chartURLs {
if h.URL.String() == chartURL {
// Simulate download time
time.Sleep(100 * time.Millisecond)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Couldn't see any reason why we'd need to slow down our unit tests like this? I don't think this fake is being used for anything other than the unit-tests?

Copy link
Contributor

Choose a reason for hiding this comment

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

TBH I don't remember the reason for this.

chartURLs: chartURLs,
index: index,
userAgent: userAgent,
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just switched to labeling the composite literal fields to avoid adding nil on the end for the requests (which aren't required for initialization)

Copy link
Contributor

Choose a reason for hiding this comment

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

I was unaware of that change of behavior, can you add a comment? If you initialize it like this request is an empty slice 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.

Yep, from https://golang.org/doc/effective_go.html#composite_literals "However, by labeling the elements explicitly as field:value pairs, the initializers can appear in any order, with the missing ones left as their respective zero values."

There's no change of behaviour here - I added the requests slice in this PR (and it should get a nil value on initialization). I just don't like specifying nil in an initialization without a label as you then need to scroll to find out what it was being initialized to nil to see if it's relevant, whereas with a labelled initialization, I can assume all the relevant info is there.

Copy link
Contributor

@andresmgot andresmgot left a comment

Choose a reason for hiding this comment

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

LGTM

}

func (f *fakeHTTPClient) Do(h *http.Request) (*http.Response, error) {
// Record the request for later test assertions.
f.requests = append(f.requests, h)
if f.userAgent != "" && h.Header.Get("User-Agent") != f.userAgent {
return nil, fmt.Errorf("Wrong user agent: %s", h.Header.Get("User-Agent"))
Copy link
Contributor

Choose a reason for hiding this comment

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

that sounds good to me, the only reason for doing the assertion here is that we couldn't do it in the test. If we can I am happy to do it.

}
return &http.Response{StatusCode: 200, Body: ioutil.NopCloser(bytes.NewReader(body))}, nil
}
}
for _, chartURL := range f.chartURLs {
if h.URL.String() == chartURL {
// Simulate download time
time.Sleep(100 * time.Millisecond)
Copy link
Contributor

Choose a reason for hiding this comment

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

TBH I don't remember the reason for this.

chartURLs: chartURLs,
index: index,
userAgent: userAgent,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

I was unaware of that change of behavior, can you add a comment? If you initialize it like this request is an empty slice instead?

@absoludity absoludity merged commit 08958c7 into master Aug 21, 2019
@absoludity absoludity deleted the tiller-authz-header-during-initialization branch August 21, 2019 01:10
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