Skip to content

Add/improve unit tests for agentendpoint/exec_task.go#933

Open
ganochenkodg wants to merge 8 commits intoGoogleCloudPlatform:masterfrom
ganochenkodg:agentendpoint-exectask-unittests
Open

Add/improve unit tests for agentendpoint/exec_task.go#933
ganochenkodg wants to merge 8 commits intoGoogleCloudPlatform:masterfrom
ganochenkodg:agentendpoint-exectask-unittests

Conversation

@ganochenkodg
Copy link
Copy Markdown

@ganochenkodg ganochenkodg commented Mar 19, 2026

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%

@ganochenkodg ganochenkodg marked this pull request as draft March 19, 2026 16:53
@ganochenkodg
Copy link
Copy Markdown
Author

This is draft PR, do not review it right now please.

@google-oss-prow
Copy link
Copy Markdown

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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions 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.

@google-oss-prow
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: ganochenkodg
Once this PR has been reviewed and has the lgtm label, please assign savija-tv for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found 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

@ganochenkodg ganochenkodg force-pushed the agentendpoint-exectask-unittests branch from 905b95c to a7a5ea6 Compare March 19, 2026 17:04
Copy link
Copy Markdown
Contributor

@iliatsuprik iliatsuprik left a comment

Choose a reason for hiding this comment

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

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

t.Run(tt.name, func(t *testing.T) {
run = tt.mockRun
gotCode, err := executeCommand(ctx, "test-path", nil)
if (err != nil) != tt.wantErr {
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.

could you check exact error text when possible

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

added

}
}

func TestExecuteCommand(t *testing.T) {
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.

could you add a docstring to the test with a short description what it's doing

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Sure, added for both TestExecuteCommand and TestGCS funcs

},
wantCode: 0,
wantErr: false,
},
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.

here we validate errors in result, but do not validate a success testcase

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added Success case

defer ts.Close()

os.Setenv("STORAGE_EMULATOR_HOST", ts.URL)
defer os.Unsetenv("STORAGE_EMULATOR_HOST")
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.

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good idea, use this function now

@burov
Copy link
Copy Markdown
Member

burov commented Mar 23, 2026

/gcbrun

@burov
Copy link
Copy Markdown
Member

burov commented Mar 25, 2026

/gcbrun

w.WriteHeader(http.StatusOK)
w.Write([]byte(testContent))
},
wantErr: true,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there anything blocking us from verifying an actual error instead of just it's presence ?

ts := httptest.NewServer(tt.handler)
defer ts.Close()

rollback := OverrideEnv(t, "STORAGE_EMULATOR_HOST", ts.URL)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fix format please.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

gofmt? fixed now

}
if err == nil {
defer os.Remove(localPath)
content, err := os.ReadFile(localPath)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yep, using them now

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) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@ganochenkodg ganochenkodg marked this pull request as ready for review March 27, 2026 01:45
@google-oss-prow google-oss-prow bot requested a review from savija-tv March 27, 2026 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants