Skip to content

Commit

Permalink
Fix tests hanging
Browse files Browse the repository at this point in the history
  • Loading branch information
szh committed Mar 30, 2023
1 parent bc8a780 commit 4be26f3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/clients/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestAuthenticatedConjurClientForCommand(t *testing.T) {

client, err := AuthenticatedConjurClientForCommand(cmd)

// Note: This fails when run inside the dev container because it has a valid config
assert.Nil(t, client)
assert.Error(t, err)
})
Expand Down
15 changes: 10 additions & 5 deletions pkg/cmd/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/creack/pty"
"github.com/hinshun/vt10x"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

// promptResponse is a prompt-response pair representing an expected prompt from stdout and
Expand Down Expand Up @@ -69,15 +70,19 @@ func executeCommandForTestWithPromptResponses(
}
}

// Expect string that never gets output by command, this forces go-expect
// to keep reading the command output to the buffer. Set the read timeout
// to assume command has finished when there is no output for 0.5 seconds.
_, _ = c.Expect(expect.String("FINISHED"), expect.WithTimeout(500*time.Millisecond))
// Wait until EOF gets printed to the virtual console, which happens in the code below
// when the command finishes executing. This forces the virtual console to
// continue reading input until the command finishes. Set a timeout
// to ensure that the virtual console doesn't wait forever.
_, err := c.Expect(expect.String("TEST_COMMAND_EXITED"), expect.WithTimeout(10*time.Second))
// If the timeout is reached, we should fail the test.
assert.NoError(t, err)
}()

// Execute command with args.
err := cmd.Execute()

// Print EOF to virtual console to signal that the command has finished executing.
cmd.Print("TEST_COMMAND_EXITED")
// Wait for virtual console to finish.
<-donec

Expand Down

0 comments on commit 4be26f3

Please sign in to comment.