-
Notifications
You must be signed in to change notification settings - Fork 165
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
HTTP timeouts and proxy #393
Conversation
0c429e5
to
6d2785f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to have some unit tests added to cover this functionality?
6d2785f
to
082fb10
Compare
Yes. Done. |
d432ea8
to
ce3759f
Compare
Hello @pgillich, the PR looks good but I suspect it is a bit over complicated for what we actually need. |
|
||
defer cliclient.TestingReplaceDefaultHTTPClient(mockClient)() | ||
*/ | ||
func TestingReplaceDefaultHTTPClient(mockClient HTTPClienter) func() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move it undert the cliclient_test.go
file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we can. But the compile will be failed, because this function is called from another package, too (cmd/*_test.go
). Go compiler enables importing from *_test.go files
only from same package.
It's possible to create a new package, for example testutil
, which can be imported from everywhere. But it may introduce a new claim: a new package. But anyway, if you mind, I can do it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that creating a new package would be a better approach. What do you think @crobby ?
|
||
defer cliclient.TestingForceClientInsecure()() | ||
*/ | ||
func TestingForceClientInsecure() func() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move it undert the cliclient_test.go
file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same situation to TestingReplaceDefaultHTTPClient
.
func (mc *MasterClient) newManagementClient() error { | ||
options := createClientOpts(mc.UserConfig) | ||
if testingForceClientInsecure { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I would avoid adding conditions for tests into main code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Me too, but the config.ServerConfig does not support the insecure flag.
If you mind, I can extend the config.ServerConfig with the insecure flag, annotated with json:"-"
, to make sure, it won't be loaded from config file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain why we need to set the Insecure
for tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A httptest.Server was started, which supports TLS (client TLS config can be get), but it was unable to use, because github.com/rancher/norman/clientbase.NewAPIClient replaces net/http.Client.Transport (including proxy and TLS config),
so the client TLS config of net/http/httptest.Server will be lost. I cannot understand why NewAPIClient was designed this way.
So insecure must be used with httptest.Server.
Without mocking the network communication, below new/changed functions cannot be tested:
The Originally I didn't want to write unit tests, because I could see, the coverage is low and the source code structure is not ready for writing good unit test coverage (for example: missing Go interfaces). But @crobby asked me to write unit test. So I did it. |
ce3759f
to
dd30156
Compare
dd30156
to
0530814
Compare
I got the point and thanks for writing tests. My concern is that we are introducing several things that can be considered superfluous. What I mean is that we can probably avoid of testing the |
go install github.com/vektra/mockery/[email protected] | ||
mockery |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO we shouldn't bring another mocking tool. If we need mocks we probably have to use what rancher/rancher uses go.uber.org/mock
.
I understand you would like to keep the source code as simple as possible. But automatic tests make the code more complex, in order to introduce clean coding boundaries. This extra cost cannot be avoided and it's higher in Go, comparing to VM-based languages, for example Python and Java. Automatic tests are important for open source projects, because there aren't dedicated manual testers, who make manual tests for new functions and regression tests. The tradeoff between untested features (risk) and use case coverage (cost) is decision of the project (repo) maintainers. When I watched the source code at the first time, I realized, the risk <--> cost decision was low code (use case) coverage, because the boundaries (Go interfaces) were missing for high code coverage. So I skipped the automatic test. But when @crobby asked me to write automatic tests, I did it. There were some new/changed codes, which can be tested by unit tests with low effort. But there were some new/changed codes which need too much effort to write unit tests, because the code should be restructured (boundaries with Go interfaces), before writing unit tests. Instead of the costly restructuring, I introduced function tests, which emulate a Rancher server responses with httptest.Server. The The introduced function tests look superfluous at the first time, but the this investment will return. This kind of tests can be improved to component tests, which start a The decision is in the hands of the maintainers: turning to the function and component tests, or staying at only unit test. Please notice me the unified decision of the maintainers and I will change the mocks to |
It's an extended implementation for #183 .