From 137b6277379bbca7a209d7739cc508f9d2b95fb3 Mon Sep 17 00:00:00 2001 From: Blaine Gardner Date: Mon, 14 Apr 2025 12:19:37 -0600 Subject: [PATCH] test: update ExecuteCommandWithTimeout unit test Update the unit test for ExecuteCommandWithTimeout(). This unit failed a CI run. This seems to have been a race condition where the previous `cat` command was returning quickly enough that it was available to the `select` statment at the same time as the timeout. Resolve this issue by replacing the `cat` command with `sleep`, which will be guaranteed to take longer to return than the timeout, ensuring this race doesn't happen. While working on the test, I also notice that the 30 second timeouts for other tests would be too long in the event the commands hang for any reason, so lower these to help ensure the CI won't time out due to unforeseen events. Also add a missing unit test to ensure error returns (via `false` command) without timeouts are properly returned. Signed-off-by: Blaine Gardner --- pkg/util/exec/exec_test.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/pkg/util/exec/exec_test.go b/pkg/util/exec/exec_test.go index aed52d7a5..06ba82335 100644 --- a/pkg/util/exec/exec_test.go +++ b/pkg/util/exec/exec_test.go @@ -147,7 +147,7 @@ func TestExecuteCommandWithTimeout(t *testing.T) { { name: "test stdin", args: args{ - timeout: 30 * time.Second, + timeout: 2 * time.Second, command: "cat", stdin: &testString, arg: []string{}, @@ -158,7 +158,7 @@ func TestExecuteCommandWithTimeout(t *testing.T) { { name: "test nil stdin", args: args{ - timeout: 30 * time.Second, + timeout: 2 * time.Second, command: "echo", stdin: nil, arg: []string{testString}, @@ -166,13 +166,24 @@ func TestExecuteCommandWithTimeout(t *testing.T) { want: testString, wantErr: false, }, + { + name: "test err return", + args: args{ + timeout: 2 * time.Second, + command: "false", + stdin: nil, + arg: []string{}, + }, + want: "", + wantErr: true, + }, { name: "test timeout", args: args{ - timeout: 0 * time.Second, - command: "cat", + timeout: 5 * time.Millisecond, + command: "sleep", stdin: &testString, - arg: []string{}, + arg: []string{"2"}, }, want: "", wantErr: true,