chore: add test for enterprise server cli (#12353)

This commit is contained in:
Cian Johnston 2024-02-29 10:25:50 +00:00 committed by GitHub
parent b17fcd9cff
commit 2bf3c72948
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,52 @@
package cli_test
import (
"fmt"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/enterprise/cli"
"github.com/coder/coder/v2/testutil"
)
// TestServer runs the enterprise server command
// and waits for /healthz to return "OK".
func TestServer(t *testing.T) {
t.Parallel()
var root cli.RootCmd
cmd, err := root.Command(root.EnterpriseSubcommands())
require.NoError(t, err)
port := randomPort(t)
inv, _ := clitest.NewWithCommand(t, cmd,
"server",
"--in-memory",
"--http-address", fmt.Sprintf(":%d", port),
"--access-url", "http://example.com",
)
waiter := clitest.StartWithWaiter(t, inv)
require.Eventually(t, func() bool {
reqCtx := testutil.Context(t, testutil.IntervalMedium)
req, err := http.NewRequestWithContext(reqCtx, http.MethodGet, fmt.Sprintf("http://localhost:%d/healthz", port), nil)
if err != nil {
panic(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Log("/healthz not ready yet")
return false
}
defer resp.Body.Close()
bs, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
return assert.Equal(t, "OK", string(bs))
}, testutil.WaitShort, testutil.IntervalMedium)
waiter.Cancel()
}