From d5f4243c9efe3970ccf0c6227c27bb2c03f02a31 Mon Sep 17 00:00:00 2001 From: Hamid Ghaf <83242695+hghaf099@users.noreply.github.com> Date: Tue, 19 Sep 2023 08:40:10 -0700 Subject: [PATCH] fix: Fail in goroutine after tests have completed (#23158) * fix panic: Fail in goroutine after TestProxy_Config_ReloadTls has completed * fix proxy test * feedback * track the command output code and stdout/err --- command/agent_test.go | 45 ++++++++++++++++++++++++++++--------------- command/proxy_test.go | 12 ++++++++---- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/command/agent_test.go b/command/agent_test.go index 3cc5d6446d..d3868d43a1 100644 --- a/command/agent_test.go +++ b/command/agent_test.go @@ -375,14 +375,14 @@ listener "tcp" { cmd.client = serverClient cmd.startedCh = make(chan struct{}) + var output string + var code int wg := &sync.WaitGroup{} wg.Add(1) go func() { - code := cmd.Run([]string{"-config", configPath}) + code = cmd.Run([]string{"-config", configPath}) if code != 0 { - t.Errorf("non-zero return code when running agent: %d", code) - t.Logf("STDOUT from agent:\n%s", ui.OutputWriter.String()) - t.Logf("STDERR from agent:\n%s", ui.ErrorWriter.String()) + output = ui.ErrorWriter.String() + ui.OutputWriter.String() } wg.Done() }() @@ -397,6 +397,9 @@ listener "tcp" { defer func() { cmd.ShutdownCh <- struct{}{} wg.Wait() + if code != 0 { + t.Fatalf("got a non-zero exit status: %d, stdout/stderr: %s", code, output) + } }() //---------------------------------------------------- @@ -2612,14 +2615,14 @@ listener "tcp" { cmd.client = serverClient cmd.startedCh = make(chan struct{}) + var output string + var code int wg := &sync.WaitGroup{} wg.Add(1) go func() { - code := cmd.Run([]string{"-config", configPath}) + code = cmd.Run([]string{"-config", configPath}) if code != 0 { - t.Errorf("non-zero return code when running agent: %d", code) - t.Logf("STDOUT from agent:\n%s", ui.OutputWriter.String()) - t.Logf("STDERR from agent:\n%s", ui.ErrorWriter.String()) + output = ui.ErrorWriter.String() + ui.OutputWriter.String() } wg.Done() }() @@ -2634,6 +2637,9 @@ listener "tcp" { defer func() { cmd.ShutdownCh <- struct{}{} wg.Wait() + if code != 0 { + t.Fatalf("got a non-zero exit status: %d, stdout/stderr: %s", code, output) + } }() conf := api.DefaultConfig() @@ -2910,12 +2916,13 @@ func TestAgent_Config_ReloadTls(t *testing.T) { logger := logging.NewVaultLogger(hclog.Trace) ui, cmd := testAgentCommand(t, logger) + var output string + var code int wg.Add(1) args := []string{"-config", configFile.Name()} go func() { - if code := cmd.Run(args); code != 0 { - output := ui.ErrorWriter.String() + ui.OutputWriter.String() - t.Errorf("got a non-zero exit status: %s", output) + if code = cmd.Run(args); code != 0 { + output = ui.ErrorWriter.String() + ui.OutputWriter.String() } wg.Done() }() @@ -2982,8 +2989,11 @@ func TestAgent_Config_ReloadTls(t *testing.T) { // Shut down cmd.ShutdownCh <- struct{}{} - wg.Wait() + + if code != 0 { + t.Fatalf("got a non-zero exit status: %d, stdout/stderr: %s", code, output) + } } // TestAgent_NonTLSListener_SIGHUP tests giving a SIGHUP signal to a listener @@ -3027,12 +3037,13 @@ vault { cmd.startedCh = make(chan struct{}) + var output string + var code int wg := &sync.WaitGroup{} wg.Add(1) go func() { - if code := cmd.Run([]string{"-config", configPath}); code != 0 { - output := ui.ErrorWriter.String() + ui.OutputWriter.String() - t.Errorf("got a non-zero exit status: %s", output) + if code = cmd.Run([]string{"-config", configPath}); code != 0 { + output = ui.ErrorWriter.String() + ui.OutputWriter.String() } wg.Done() }() @@ -3053,6 +3064,10 @@ vault { close(cmd.ShutdownCh) wg.Wait() + + if code != 0 { + t.Fatalf("got a non-zero exit status: %d, stdout/stderr: %s", code, output) + } } // Get a randomly assigned port and then free it again before returning it. diff --git a/command/proxy_test.go b/command/proxy_test.go index 32a6c7d289..4720e1909f 100644 --- a/command/proxy_test.go +++ b/command/proxy_test.go @@ -1171,12 +1171,13 @@ func TestProxy_Config_ReloadTls(t *testing.T) { logger := logging.NewVaultLogger(hclog.Trace) ui, cmd := testProxyCommand(t, logger) + var output string + var code int wg.Add(1) args := []string{"-config", configFile.Name()} go func() { - if code := cmd.Run(args); code != 0 { - output := ui.ErrorWriter.String() + ui.OutputWriter.String() - t.Errorf("got a non-zero exit status: %s", output) + if code = cmd.Run(args); code != 0 { + output = ui.ErrorWriter.String() + ui.OutputWriter.String() } wg.Done() }() @@ -1243,6 +1244,9 @@ func TestProxy_Config_ReloadTls(t *testing.T) { // Shut down cmd.ShutdownCh <- struct{}{} - wg.Wait() + + if code != 0 { + t.Fatalf("got a non-zero exit status: %d, stdout/stderr: %s", code, output) + } }