internal/commands/server: prevent test panic on timeout (#5249)

Previously, if one of these tests failed to start their servers before
the timeout, there was a risk that the goroutine started to run
the server would invoke t.Errorf after the test itself had concluded.
Replace t.Errorf with fmt.Printf to avoid the panic, and include
the test name for debugging.
This commit is contained in:
Johan Brandhorst-Satzkorn authored and GitHub committed 2024-11-22 09:57:41 -08:00
1 parent 4f9f281398
commit 10370edf59
5 files changed
+13 -17

No files matched your search

@@ -115,7 +115,7 @@ func TestReloadControllerDatabase(t *testing.T) {
exitCode := cmd.Run(args)
if exitCode != 0 {
output := cmd.UI.(*cli.MockUi).ErrorWriter.String() + cmd.UI.(*cli.MockUi).OutputWriter.String()
t.Errorf("got a non-zero exit status: %s", output)
fmt.Printf("%s: got a non-zero exit status: %s", t.Name(), output)
}
}()
@@ -241,7 +241,6 @@ func TestReloadControllerDatabase_InvalidNewDatabaseState(t *testing.T) {
cfgHcl := fmt.Sprintf(dbSwapConfig, urlA, controllerKey, workerAuthKey, recoveryKey)
require.NoError(t, os.WriteFile(td+"/config.hcl", []byte(cfgHcl), 0o644))
errCh := make(chan error, 1)
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
@@ -251,15 +250,12 @@ func TestReloadControllerDatabase_InvalidNewDatabaseState(t *testing.T) {
exitCode := cmd.Run(args)
if exitCode != 0 {
output := cmd.UI.(*cli.MockUi).ErrorWriter.String() + cmd.UI.(*cli.MockUi).OutputWriter.String()
errCh <- fmt.Errorf("got a non-zero exit status: %s", output)
close(errCh)
fmt.Printf("%s: got a non-zero exit status: %s", t.Name(), output)
}
}()
// Wait until things are up and running (or timeout).
select {
case err := <-errCh:
t.Fatal(err)
case <-cmd.startedCh:
case <-time.After(15 * time.Second):
t.Fatal("timeout")