|
| 1 | +package cli_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/coder/coder/v2/cli/clitest" |
| 13 | + "github.com/coder/coder/v2/enterprise/cli" |
| 14 | + "github.com/coder/coder/v2/testutil" |
| 15 | +) |
| 16 | + |
| 17 | +// TestServer runs the enterprise server command |
| 18 | +// and waits for /healthz to return "OK". |
| 19 | +func TestServer(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + var root cli.RootCmd |
| 23 | + cmd, err := root.Command(root.EnterpriseSubcommands()) |
| 24 | + require.NoError(t, err) |
| 25 | + port := randomPort(t) |
| 26 | + inv, _ := clitest.NewWithCommand(t, cmd, |
| 27 | + "server", |
| 28 | + "--in-memory", |
| 29 | + "--http-address", fmt.Sprintf(":%d", port), |
| 30 | + "--access-url", "http://example.com", |
| 31 | + ) |
| 32 | + waiter := clitest.StartWithWaiter(t, inv) |
| 33 | + require.Eventually(t, func() bool { |
| 34 | + reqCtx := testutil.Context(t, testutil.IntervalMedium) |
| 35 | + req, err := http.NewRequestWithContext(reqCtx, http.MethodGet, fmt.Sprintf("http://localhost:%d/healthz", port), nil) |
| 36 | + if err != nil { |
| 37 | + panic(err) |
| 38 | + } |
| 39 | + resp, err := http.DefaultClient.Do(req) |
| 40 | + if err != nil { |
| 41 | + t.Log("/healthz not ready yet") |
| 42 | + return false |
| 43 | + } |
| 44 | + defer resp.Body.Close() |
| 45 | + bs, err := io.ReadAll(resp.Body) |
| 46 | + if err != nil { |
| 47 | + panic(err) |
| 48 | + } |
| 49 | + return assert.Equal(t, "OK", string(bs)) |
| 50 | + }, testutil.WaitShort, testutil.IntervalMedium) |
| 51 | + waiter.Cancel() |
| 52 | +} |
0 commit comments