From 670322b2982a93c6ead3fa73449c24b106c8de71 Mon Sep 17 00:00:00 2001 From: bstrausser Date: Tue, 12 Mar 2024 10:13:30 -0400 Subject: [PATCH 1/4] Add exit code sugar method --- wait/exec.go | 7 +++++++ wait/exec_test.go | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/wait/exec.go b/wait/exec.go index e0ffc89758..f3032923b6 100644 --- a/wait/exec.go +++ b/wait/exec.go @@ -45,6 +45,13 @@ func (ws *ExecStrategy) WithStartupTimeout(startupTimeout time.Duration) *ExecSt return ws } +func (ws *ExecStrategy) WithExitCode(exitCode int) *ExecStrategy { + + return ws.WithExitCodeMatcher(func(actualCode int) bool { + return actualCode == exitCode + }) +} + func (ws *ExecStrategy) WithExitCodeMatcher(exitCodeMatcher func(exitCode int) bool) *ExecStrategy { ws.ExitCodeMatcher = exitCodeMatcher return ws diff --git a/wait/exec_test.go b/wait/exec_test.go index b1c31f1b6e..25f585c1c3 100644 --- a/wait/exec_test.go +++ b/wait/exec_test.go @@ -156,6 +156,27 @@ func TestExecStrategyWaitUntilReady_CustomExitCode(t *testing.T) { } } +func TestExecStrategyWaitUntilReadyWithSugaryExitCode(t *testing.T) { + target := mockExecTarget{ + exitCode: 10, + } + wg := wait.NewExecStrategy([]string{"true"}).WithExitCode(10) + // Default is 60. Let's shorten that + wg.WithStartupTimeout(time.Second*2) + err := wg.WaitUntilReady(context.Background(), target) + if err != nil { + t.Fatal(err) + } + + //Ensure we aren't spuriously returning on any code + wg = wait.NewExecStrategy([]string{"true"}).WithExitCode(0) + wg.WithStartupTimeout(time.Second*2) + err = wg.WaitUntilReady(context.Background(), target) + if err == nil { + t.Fatalf("Expected strategy to timeout out") + } +} + func TestExecStrategyWaitUntilReady_CustomResponseMatcher(t *testing.T) { // waitForExecExitCodeResponse { dockerReq := testcontainers.ContainerRequest{ From aebbb02599f69f6dc7932d7bd4cd5e72f93d6a2a Mon Sep 17 00:00:00 2001 From: bstrausser Date: Tue, 12 Mar 2024 11:55:20 -0400 Subject: [PATCH 2/4] Fix lint errors --- wait/exec.go | 1 - wait/exec_test.go | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/wait/exec.go b/wait/exec.go index f3032923b6..2e341dd3e0 100644 --- a/wait/exec.go +++ b/wait/exec.go @@ -46,7 +46,6 @@ func (ws *ExecStrategy) WithStartupTimeout(startupTimeout time.Duration) *ExecSt } func (ws *ExecStrategy) WithExitCode(exitCode int) *ExecStrategy { - return ws.WithExitCodeMatcher(func(actualCode int) bool { return actualCode == exitCode }) diff --git a/wait/exec_test.go b/wait/exec_test.go index 25f585c1c3..d7d1f0269c 100644 --- a/wait/exec_test.go +++ b/wait/exec_test.go @@ -162,15 +162,15 @@ func TestExecStrategyWaitUntilReadyWithSugaryExitCode(t *testing.T) { } wg := wait.NewExecStrategy([]string{"true"}).WithExitCode(10) // Default is 60. Let's shorten that - wg.WithStartupTimeout(time.Second*2) + wg.WithStartupTimeout(time.Second * 2) err := wg.WaitUntilReady(context.Background(), target) if err != nil { t.Fatal(err) } - //Ensure we aren't spuriously returning on any code + // Ensure we aren't spuriously returning on any code wg = wait.NewExecStrategy([]string{"true"}).WithExitCode(0) - wg.WithStartupTimeout(time.Second*2) + wg.WithStartupTimeout(time.Second * 2) err = wg.WaitUntilReady(context.Background(), target) if err == nil { t.Fatalf("Expected strategy to timeout out") From 6497678d86d5b5061caf1b0df1d93e72e9759c67 Mon Sep 17 00:00:00 2001 From: bstrausser Date: Mon, 18 Mar 2024 11:10:51 -0400 Subject: [PATCH 3/4] Tweak test name --- wait/exec_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wait/exec_test.go b/wait/exec_test.go index d7d1f0269c..9e979fa463 100644 --- a/wait/exec_test.go +++ b/wait/exec_test.go @@ -156,7 +156,7 @@ func TestExecStrategyWaitUntilReady_CustomExitCode(t *testing.T) { } } -func TestExecStrategyWaitUntilReadyWithSugaryExitCode(t *testing.T) { +func TestExecStrategyWaitUntilReadyWithSugaryExitCode_MatchesAndTimeout(t *testing.T) { target := mockExecTarget{ exitCode: 10, } From feba2a939aa12b143c65a7a20bba6d318ef1a582 Mon Sep 17 00:00:00 2001 From: bstrausser Date: Mon, 25 Mar 2024 12:17:03 -0400 Subject: [PATCH 4/4] Rename test --- wait/exec_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wait/exec_test.go b/wait/exec_test.go index 9e979fa463..132933018f 100644 --- a/wait/exec_test.go +++ b/wait/exec_test.go @@ -156,7 +156,7 @@ func TestExecStrategyWaitUntilReady_CustomExitCode(t *testing.T) { } } -func TestExecStrategyWaitUntilReadyWithSugaryExitCode_MatchesAndTimeout(t *testing.T) { +func TestExecStrategyWaitUntilReady_withExitCode(t *testing.T) { target := mockExecTarget{ exitCode: 10, }