From 7ffed9a175df232faa993839f06f2536507f69e7 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Wed, 28 Feb 2024 17:23:51 +0000 Subject: [PATCH 1/2] chore: add test for enterprise server cli --- enterprise/cli/server_test.go | 62 +++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 enterprise/cli/server_test.go diff --git a/enterprise/cli/server_test.go b/enterprise/cli/server_test.go new file mode 100644 index 0000000000000..9eb37749d7a17 --- /dev/null +++ b/enterprise/cli/server_test.go @@ -0,0 +1,62 @@ +package cli_test + +import ( + "fmt" + "io" + "net" + "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() + + randomPort := func(t *testing.T) int { + random, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + _ = random.Close() + tcpAddr, valid := random.Addr().(*net.TCPAddr) + require.True(t, valid) + return tcpAddr.Port + } + + 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() +} From 8a6fa649ad7d184f39815810bc90f8cfb3bee72f Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Thu, 29 Feb 2024 10:03:08 +0000 Subject: [PATCH 2/2] use the pre-existing randomPort() function --- enterprise/cli/server_test.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/enterprise/cli/server_test.go b/enterprise/cli/server_test.go index 9eb37749d7a17..b70d6eab3ce98 100644 --- a/enterprise/cli/server_test.go +++ b/enterprise/cli/server_test.go @@ -3,7 +3,6 @@ package cli_test import ( "fmt" "io" - "net" "net/http" "testing" @@ -20,15 +19,6 @@ import ( func TestServer(t *testing.T) { t.Parallel() - randomPort := func(t *testing.T) int { - random, err := net.Listen("tcp", "127.0.0.1:0") - require.NoError(t, err) - _ = random.Close() - tcpAddr, valid := random.Addr().(*net.TCPAddr) - require.True(t, valid) - return tcpAddr.Port - } - var root cli.RootCmd cmd, err := root.Command(root.EnterpriseSubcommands()) require.NoError(t, err)