From 7058e7493961d49c5cb11e88ffbb94ef6f94e8f2 Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Mon, 2 Sep 2024 17:47:45 +1000 Subject: [PATCH] feat: run tests on different port This should stop tests from interferring with ftl dev fixes #2577 --- internal/exec/exec.go | 9 +++++++++ internal/integration/actions.go | 14 ++++++++------ internal/integration/harness.go | 11 +++++++---- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/internal/exec/exec.go b/internal/exec/exec.go index 003e8fbdee..27a8ac70c4 100644 --- a/internal/exec/exec.go +++ b/internal/exec/exec.go @@ -22,6 +22,15 @@ func LookPath(exe string) (string, error) { return path, err } +func CaptureWithEnv(ctx context.Context, dir, exe string, env []string, args ...string) ([]byte, error) { + cmd := Command(ctx, log.Debug, dir, exe, args...) + cmd.Env = append(cmd.Env, env...) + cmd.Stdout = nil + cmd.Stderr = nil + out, err := cmd.CombinedOutput() + return out, err +} + func Capture(ctx context.Context, dir, exe string, args ...string) ([]byte, error) { cmd := Command(ctx, log.Debug, dir, exe, args...) cmd.Stdout = nil diff --git a/internal/integration/actions.go b/internal/integration/actions.go index 73f67b9d68..8d6998060d 100644 --- a/internal/integration/actions.go +++ b/internal/integration/actions.go @@ -160,7 +160,9 @@ func DebugShell() Action { func Exec(cmd string, args ...string) Action { return func(t testing.TB, ic TestContext) { Infof("Executing (in %s): %s %s", ic.workDir, cmd, shellquote.Join(args...)) - err := ftlexec.Command(ic, log.Debug, ic.workDir, cmd, args...).RunStderrError(ic) + command := ftlexec.Command(ic, log.Debug, ic.workDir, cmd, args...) + command.Env = append(command.Env, "FTL_ENDPOINT=http://127.0.0.1:"+TestPort) + err := command.RunStderrError(ic) assert.NoError(t, err) } } @@ -170,7 +172,7 @@ func Exec(cmd string, args ...string) Action { func ExecWithExpectedOutput(want string, cmd string, args ...string) Action { return func(t testing.TB, ic TestContext) { Infof("Executing: %s %s", cmd, shellquote.Join(args...)) - output, err := ftlexec.Capture(ic, ic.workDir, cmd, args...) + output, err := ftlexec.CaptureWithEnv(ic, ic.workDir, cmd, []string{"FTL_ENDPOINT=http://127.0.0.1:" + TestPort}, args...) assert.NoError(t, err) assert.Equal(t, output, []byte(want)) } @@ -181,7 +183,7 @@ func ExecWithExpectedOutput(want string, cmd string, args ...string) Action { func ExecWithExpectedError(want string, cmd string, args ...string) Action { return func(t testing.TB, ic TestContext) { Infof("Executing: %s %s", cmd, shellquote.Join(args...)) - output, err := ftlexec.Capture(ic, ic.workDir, cmd, args...) + output, err := ftlexec.CaptureWithEnv(ic, ic.workDir, cmd, []string{"FTL_ENDPOINT=http://127.0.0.1:" + TestPort}, args...) assert.Error(t, err) assert.Contains(t, string(output), want) } @@ -193,7 +195,7 @@ func ExecWithExpectedError(want string, cmd string, args ...string) Action { func ExecWithOutput(cmd string, args []string, capture func(output string)) Action { return func(t testing.TB, ic TestContext) { Infof("Executing: %s %s", cmd, shellquote.Join(args...)) - output, err := ftlexec.Capture(ic, ic.workDir, cmd, args...) + output, err := ftlexec.CaptureWithEnv(ic, ic.workDir, cmd, []string{"FTL_ENDPOINT=http://127.0.0.1:" + TestPort}, args...) assert.NoError(t, err, "%s", string(output)) capture(string(output)) } @@ -220,7 +222,7 @@ func ExpectError(action Action, expectedErrorMsg ...string) Action { // Deploy a module from the working directory and wait for it to become available. func Deploy(module string) Action { return Chain( - Exec("ftl", "deploy", module), + Exec("ftl", "deploy", "--endpoint", "http://127.0.0.1:"+TestPort, module), Wait(module), ) } @@ -521,7 +523,7 @@ func JsonData(t testing.TB, body interface{}) []byte { func HttpCall(method string, path string, headers map[string][]string, body []byte, onResponse func(t testing.TB, resp *HTTPResponse)) Action { return func(t testing.TB, ic TestContext) { Infof("HTTP %s %s", method, path) - baseURL, err := url.Parse(fmt.Sprintf("http://localhost:8891")) + baseURL, err := url.Parse(fmt.Sprintf("http://localhost:" + TestIngressPort)) assert.NoError(t, err) u, err := baseURL.Parse(path) diff --git a/internal/integration/harness.go b/internal/integration/harness.go index af379d8348..471b36f312 100644 --- a/internal/integration/harness.go +++ b/internal/integration/harness.go @@ -29,6 +29,9 @@ import ( "github.com/TBD54566975/ftl/internal/rpc" ) +const TestPort = "9892" +const TestIngressPort = "9891" + func integrationTestTimeout() time.Duration { timeout := optional.Zero(os.Getenv("FTL_INTEGRATION_TEST_TIMEOUT")).Default("5s") d, err := time.ParseDuration(timeout) @@ -174,16 +177,16 @@ func run(t *testing.T, actionsOrOptions ...ActionOrOption) { t.Run(language, func(t *testing.T) { tmpDir := initWorkDir(t, cwd, opts) - verbs := rpc.Dial(ftlv1connect.NewVerbServiceClient, "http://localhost:8892", log.Debug) + verbs := rpc.Dial(ftlv1connect.NewVerbServiceClient, "http://localhost:"+TestPort, log.Debug) var controller ftlv1connect.ControllerServiceClient var console pbconsoleconnect.ConsoleServiceClient if opts.startController { - controller = rpc.Dial(ftlv1connect.NewControllerServiceClient, "http://localhost:8892", log.Debug) - console = rpc.Dial(pbconsoleconnect.NewConsoleServiceClient, "http://localhost:8892", log.Debug) + controller = rpc.Dial(ftlv1connect.NewControllerServiceClient, "http://localhost:"+TestPort, log.Debug) + console = rpc.Dial(pbconsoleconnect.NewConsoleServiceClient, "http://localhost:"+TestPort, log.Debug) Infof("Starting ftl cluster") - ctx = startProcess(ctx, t, filepath.Join(binDir, "ftl"), "serve", "--recreate") + ctx = startProcess(ctx, t, filepath.Join(binDir, "ftl"), "serve", "--recreate", "--bind", "http://127.0.0.1:"+TestIngressPort) } testData := filepath.Join(cwd, "testdata", language)