|
| 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 | +funcTestServer(t*testing.T) { |
| 20 | +t.Parallel() |
| 21 | + |
| 22 | +varroot 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 | +iferr!=nil { |
| 37 | +panic(err) |
| 38 | +} |
| 39 | +resp,err:=http.DefaultClient.Do(req) |
| 40 | +iferr!=nil { |
| 41 | +t.Log("/healthz not ready yet") |
| 42 | +returnfalse |
| 43 | +} |
| 44 | +deferresp.Body.Close() |
| 45 | +bs,err:=io.ReadAll(resp.Body) |
| 46 | +iferr!=nil { |
| 47 | +panic(err) |
| 48 | +} |
| 49 | +returnassert.Equal(t,"OK",string(bs)) |
| 50 | +},testutil.WaitShort,testutil.IntervalMedium) |
| 51 | +waiter.Cancel() |
| 52 | +} |