Add/improve unit tests for agentendpoint/exec_task.go#933
Add/improve unit tests for agentendpoint/exec_task.go#933ganochenkodg wants to merge 8 commits intoGoogleCloudPlatform:masterfrom
Conversation
|
This is draft PR, do not review it right now please. |
|
Hi @ganochenkodg. Thanks for your PR. I'm waiting for a GoogleCloudPlatform member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
63267c8 to
3d8c42c
Compare
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: ganochenkodg The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
905b95c to
a7a5ea6
Compare
iliatsuprik
left a comment
There was a problem hiding this comment.
I've got 79% via go test -coverprofile=coverage.out ./agentendpoint && go tool cover -html=coverage.out. Could you check if some code branches can be covered
agentendpoint/exec_task_test.go
Outdated
| t.Run(tt.name, func(t *testing.T) { | ||
| run = tt.mockRun | ||
| gotCode, err := executeCommand(ctx, "test-path", nil) | ||
| if (err != nil) != tt.wantErr { |
There was a problem hiding this comment.
could you check exact error text when possible
| } | ||
| } | ||
|
|
||
| func TestExecuteCommand(t *testing.T) { |
There was a problem hiding this comment.
could you add a docstring to the test with a short description what it's doing
There was a problem hiding this comment.
Sure, added for both TestExecuteCommand and TestGCS funcs
| }, | ||
| wantCode: 0, | ||
| wantErr: false, | ||
| }, |
There was a problem hiding this comment.
here we validate errors in result, but do not validate a success testcase
agentendpoint/exec_task_test.go
Outdated
| defer ts.Close() | ||
|
|
||
| os.Setenv("STORAGE_EMULATOR_HOST", ts.URL) | ||
| defer os.Unsetenv("STORAGE_EMULATOR_HOST") |
There was a problem hiding this comment.
Unset env will remove STORAGE_EMULATOR_HOST value, it's better to restore the initial value which existed before the test started (to avoid flaky tests). I propose to use a function similar to https://github.com/GoogleCloudPlatform/osconfig/pull/925/changes#diff-783605524de2c2d44922219ca97cd7cbee5b401a62efdf763bc00083728780c6R1322-R1341
There was a problem hiding this comment.
Good idea, use this function now
|
/gcbrun |
|
/gcbrun |
agentendpoint/exec_task_test.go
Outdated
| w.WriteHeader(http.StatusOK) | ||
| w.Write([]byte(testContent)) | ||
| }, | ||
| wantErr: true, |
There was a problem hiding this comment.
Is there anything blocking us from verifying an actual error instead of just it's presence ?
agentendpoint/exec_task_test.go
Outdated
| ts := httptest.NewServer(tt.handler) | ||
| defer ts.Close() | ||
|
|
||
| rollback := OverrideEnv(t, "STORAGE_EMULATOR_HOST", ts.URL) |
agentendpoint/exec_task_test.go
Outdated
| } | ||
| if err == nil { | ||
| defer os.Remove(localPath) | ||
| content, err := os.ReadFile(localPath) |
There was a problem hiding this comment.
I suppose util functions from https://github.com/GoogleCloudPlatform/osconfig/pull/934/changes#diff-b794709cfa990f89da0485b8f4526aa8c890463db3dce888ee66cfaccff93d18 should came handy
agentendpoint/exec_task_test.go
Outdated
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("%s: executeCommand() error = %v, wantErr %v", tt.name, err, tt.wantErr) | ||
| } | ||
| if err != nil && tt.wantErrMsg != "" && !strings.Contains(err.Error(), tt.wantErrMsg) { |
There was a problem hiding this comment.
Same about errors and result check https://github.com/GoogleCloudPlatform/osconfig/pull/934/changes#diff-b794709cfa990f89da0485b8f4526aa8c890463db3dce888ee66cfaccff93d18 should be handy
With this PR better test coverage for ./agentendpoint/exec_task.go was reached:
github.com/GoogleCloudPlatform/osconfig/agentendpoint/exec_task.go:66: getGCSObject 0.0% -> 92.9%
github.com/GoogleCloudPlatform/osconfig/agentendpoint/exec_task.go:89: executeCommand 63.6% -> 100.0%
github.com/GoogleCloudPlatform/osconfig/agentendpoint/exec_task.go:236: RunExecStep 0.0% -> 100.0%
Average test coverage for this file 51.88% -> 90.01%