From e1437c1bc7ba7dab5bacc502da851cdcdee7e12f Mon Sep 17 00:00:00 2001 From: i4k Date: Fri, 27 Sep 2024 21:23:20 +0100 Subject: [PATCH] test: fix windows CI tests. Signed-off-by: i4k --- .../{ci-experimental.yml => ci-windows.yml} | 10 ++---- cmd/terramate/cli/run.go | 2 +- .../cloud/run_script_cloud_preview_test.go | 23 +++++-------- e2etests/core/run_sharing_test.go | 9 ++--- e2etests/core/run_test.go | 34 +++++++++++-------- e2etests/core/script_run_test.go | 11 +++--- e2etests/internal/runner/runner.go | 4 +-- e2etests/internal/runner/setup.go | 8 +++++ makefiles/windows.mk | 2 ++ 9 files changed, 53 insertions(+), 50 deletions(-) rename .github/workflows/{ci-experimental.yml => ci-windows.yml} (83%) diff --git a/.github/workflows/ci-experimental.yml b/.github/workflows/ci-windows.yml similarity index 83% rename from .github/workflows/ci-experimental.yml rename to .github/workflows/ci-windows.yml index abc3e67b87..65a11dc1ad 100644 --- a/.github/workflows/ci-experimental.yml +++ b/.github/workflows/ci-windows.yml @@ -1,15 +1,9 @@ # Copyright 2023 Terramate GmbH # SPDX-License-Identifier: MPL-2.0 -name: ci-experimental +name: ci-windows on: - push: - paths-ignore: - - 'docs/**' - - 'makefiles/**' - - '.github/**' - - '**/*.tm.hcl' - - '.tool-versions' + pull_request: jobs: build_test: diff --git a/cmd/terramate/cli/run.go b/cmd/terramate/cli/run.go index 4544e23d8c..24b2a51746 100644 --- a/cmd/terramate/cli/run.go +++ b/cmd/terramate/cli/run.go @@ -665,7 +665,7 @@ func (c *cli) runAll( var err error if !task.isSuccessExit(result.cmd.ProcessState.ExitCode()) { - err = errors.E(result.err, ErrRunFailed, "running %s (in %s)", result.cmd, run.Stack.Dir) + err = errors.E(result.err, ErrRunFailed, "running %s (in %s)", cmdStr, run.Stack.Dir) errs.Append(err) } diff --git a/e2etests/cloud/run_script_cloud_preview_test.go b/e2etests/cloud/run_script_cloud_preview_test.go index 34bf2bfad5..5f121ebdd1 100644 --- a/e2etests/cloud/run_script_cloud_preview_test.go +++ b/e2etests/cloud/run_script_cloud_preview_test.go @@ -5,7 +5,6 @@ package cloud_test import ( "encoding/json" - "fmt" "net/http" "os" "path/filepath" @@ -72,13 +71,13 @@ func TestScriptRunWithCloudSyncPreview(t *testing.T) { } } `, - fmt.Sprintf(`f:stack/preview.tm: + `f:stack/preview.tm: script "preview" { description = "sync a preview" job { commands = [ - ["%s", "plan", "-out=out.tfplan", "-no-color", "-detailed-exitcode", { + ["terraform", "plan", "-out=out.tfplan", "-no-color", "-detailed-exitcode", { sync_preview = true, terraform_plan_file = "out.tfplan", }], @@ -86,8 +85,6 @@ func TestScriptRunWithCloudSyncPreview(t *testing.T) { } } `, - TerraformTestPath, - ), `f:stack/main.tf: resource "local_file" "foo" { content = "test content" @@ -129,13 +126,13 @@ func TestScriptRunWithCloudSyncPreview(t *testing.T) { } } `, - fmt.Sprintf(`f:stack/preview.tm: + `f:stack/preview.tm: script "preview" { description = "sync a preview" job { commands = [ - ["%s", "plan", "-out=out.tfplan", "-no-color", "-detailed-exitcode", { + ["terraform", "plan", "-out=out.tfplan", "-no-color", "-detailed-exitcode", { sync_preview = true, terraform_plan_file = "out.tfplan", }], @@ -143,8 +140,6 @@ func TestScriptRunWithCloudSyncPreview(t *testing.T) { } } `, - TerraformTestPath, - ), `f:stack/main.tf: resource "local_file" "foo" { content = "test content" @@ -177,7 +172,7 @@ func TestScriptRunWithCloudSyncPreview(t *testing.T) { { ID: "1", Status: "changed", - Cmd: []string{TerraformTestPath, "plan", "-out=out.tfplan", "-no-color", "-detailed-exitcode"}, + Cmd: []string{"terraform", "plan", "-out=out.tfplan", "-no-color", "-detailed-exitcode"}, }, }, ReviewRequest: &cloud.ReviewRequest{ @@ -239,14 +234,14 @@ func TestScriptRunWithCloudSyncPreview(t *testing.T) { } } `, - fmt.Sprintf(`f:stack/preview.tm: + `f:stack/preview.tm: script "preview" { description = "sync a preview" job { commands = [ ["do-not-exist-command"], - ["%s", "plan", "-out=out.tfplan", "-no-color", "-detailed-exitcode", { + ["terraform", "plan", "-out=out.tfplan", "-no-color", "-detailed-exitcode", { sync_preview = true, terraform_plan_file = "out.tfplan", }], @@ -254,8 +249,6 @@ func TestScriptRunWithCloudSyncPreview(t *testing.T) { } } `, - TerraformTestPath, - ), `f:stack/main.tf: resource "local_file" "foo" { content = "test content" @@ -285,7 +278,7 @@ func TestScriptRunWithCloudSyncPreview(t *testing.T) { { ID: "1", Status: "failed", - Cmd: []string{TerraformTestPath, "plan", "-out=out.tfplan", "-no-color", "-detailed-exitcode"}, + Cmd: []string{"terraform", "plan", "-out=out.tfplan", "-no-color", "-detailed-exitcode"}, }, }, ReviewRequest: &cloud.ReviewRequest{ diff --git a/e2etests/core/run_sharing_test.go b/e2etests/core/run_sharing_test.go index eb818d524c..aee4ac36eb 100644 --- a/e2etests/core/run_sharing_test.go +++ b/e2etests/core/run_sharing_test.go @@ -274,7 +274,7 @@ func TestRunSharing(t *testing.T) { Labels("name"), Expr("type", "terraform"), Str("filename", "sharing.tf"), - Command(HelperPath, "exit", "1"), + Command("helper", "exit", "1"), ).String(), "s:s1:id=s1", "f:s1/main.tf:" + Doc( @@ -335,7 +335,7 @@ func TestRunSharing(t *testing.T) { Labels("name"), Expr("type", "terraform"), Str("filename", "sharing.tf"), - Command(HelperPath, "echo", "{}"), + Command("helper", "echo", "{}"), ).String(), "s:s1:id=s1", "f:s1/main.tf:" + Doc( @@ -402,7 +402,7 @@ func TestRunSharing(t *testing.T) { Labels("name"), Expr("type", "terraform"), Str("filename", "sharing.tf"), - Command(HelperPath, "echo", "$error"), + Command("helper", "echo", "$error"), ).String(), "s:s1:id=s1", "f:s1/main.tf:" + Doc( @@ -503,7 +503,8 @@ func TestRunSharing(t *testing.T) { s.BuildTree(layout) tmcli := NewCLI(t, s.RootDir()) tmcli.PrependToPath(filepath.Dir(TerraformTestPath)) - res := tmcli.Run("run", HelperPath, "echo", "hello") + tmcli.PrependToPath(filepath.Dir(HelperPath)) + res := tmcli.Run("run", "helper", "echo", "hello") if res.Status == 0 { // generate safeguard must trigger t.Fatal("run must fail if sharing is not generated") diff --git a/e2etests/core/run_test.go b/e2etests/core/run_test.go index 507451e87e..4d49e12ee9 100644 --- a/e2etests/core/run_test.go +++ b/e2etests/core/run_test.go @@ -918,10 +918,9 @@ func TestCLIRunOrder(t *testing.T) { t.Parallel() copiedLayout := make([]string, len(tc.layout)) copy(copiedLayout, tc.layout) - if runtime.GOOS != "windows" { - copiedLayout = append(copiedLayout, - fmt.Sprintf(`file:script.tm: + copiedLayout = append(copiedLayout, + fmt.Sprintf(`file:script.tm: terramate { config { experiments = ["scripts"] @@ -930,10 +929,13 @@ terramate { script "cmd" { description = "test" job { - command = ["%s", "stack-abs-path", "%s"] + command = ["helper", "stack-abs-path", "${tm_chomp(<<-EOF + %s + EOF + )}"] } -}`, HelperPath, s.RootDir())) - } +}`, s.RootDir())) + s.BuildTree(copiedLayout) wd := s.RootDir() @@ -2611,25 +2613,25 @@ func TestRunOutput(t *testing.T) { for _, tc := range []testcase{ { name: "run without eval", - runArgs: []string{HelperPath, "echo", "hello"}, + runArgs: []string{"helper", "echo", "hello"}, want: RunExpected{ Stderr: "terramate: Entering stack in /stack" + "\n" + - fmt.Sprintf(`terramate: Executing command "%s echo hello"`, HelperPath) + "\n", + `terramate: Executing command "helper echo hello"` + "\n", Stdout: "hello\n", }, }, { name: "run with eval", - runArgs: []string{"--eval", HelperPath, "echo", "${terramate.stack.name}"}, + runArgs: []string{"--eval", "helper", "echo", "${terramate.stack.name}"}, want: RunExpected{ Stderr: "terramate: Entering stack in /stack" + "\n" + - fmt.Sprintf(`terramate: Executing command "%s echo stack"`, HelperPath) + "\n", + `terramate: Executing command "helper echo stack"` + "\n", Stdout: "stack\n", }, }, { name: "run with eval with error", - runArgs: []string{"--eval", HelperPath, "echo", "${terramate.stack.abcabc}"}, + runArgs: []string{"--eval", "helper", "echo", "${terramate.stack.abcabc}"}, want: RunExpected{ Stderr: "Error: unable to evaluate command" + "\n" + `> :1,19-26: eval expression: eval "${terramate.stack.abcabc}": This object does not have an attribute named "abcabc"` + ".\n", @@ -2644,6 +2646,7 @@ func TestRunOutput(t *testing.T) { git := s.Git() git.CommitAll("first commit") cli := NewCLI(t, s.RootDir()) + cli.PrependToPath(filepath.Dir(HelperPath)) AssertRunResult(t, cli.Run(append([]string{"run"}, tc.runArgs...)...), tc.want, @@ -2665,19 +2668,19 @@ func TestRunDryRun(t *testing.T) { for _, tc := range []testcase{ { name: "dryrun without eval", - runArgs: []string{"--dry-run", HelperPath, "echo", "hello"}, + runArgs: []string{"--dry-run", "helper", "echo", "hello"}, want: RunExpected{ Stderr: "terramate: (dry-run) Entering stack in /stack" + "\n" + - fmt.Sprintf(`terramate: (dry-run) Executing command "%s echo hello"`, HelperPath) + "\n", + `terramate: (dry-run) Executing command "helper echo hello"` + "\n", Stdout: "", }, }, { name: "dryrun with eval", - runArgs: []string{"--dry-run", "--eval", HelperPath, "echo", "${terramate.stack.name}"}, + runArgs: []string{"--dry-run", "--eval", "helper", "echo", "${terramate.stack.name}"}, want: RunExpected{ Stderr: "terramate: (dry-run) Entering stack in /stack" + "\n" + - fmt.Sprintf(`terramate: (dry-run) Executing command "%s echo stack"`, HelperPath) + "\n", + `terramate: (dry-run) Executing command "helper echo stack"` + "\n", Stdout: "", }, }, @@ -2688,6 +2691,7 @@ func TestRunDryRun(t *testing.T) { git := s.Git() git.CommitAll("first commit") cli := NewCLI(t, s.RootDir()) + cli.PrependToPath(filepath.Dir(HelperPath)) AssertRunResult(t, cli.Run(append([]string{"run"}, tc.runArgs...)...), tc.want, diff --git a/e2etests/core/script_run_test.go b/e2etests/core/script_run_test.go index 3a06749ccc..3f92df595d 100644 --- a/e2etests/core/script_run_test.go +++ b/e2etests/core/script_run_test.go @@ -228,7 +228,7 @@ func TestScriptRun(t *testing.T) { command = ["echo", "hello1"] } job { - command = ["` + HelperPath + `", "false"] + command = ["helper", "false"] } job { command = ["echo", "hello2"] @@ -242,12 +242,12 @@ func TestScriptRun(t *testing.T) { Status: 1, Stderr: "Script 0 at /stack-a/script.tm:2,5-13,6 having 3 job(s)\n" + "/stack-a (script:0 job:0.0)> echo hello1\n" + - "/stack-a (script:0 job:1.0)> " + HelperPath + " false\n" + + "/stack-a (script:0 job:1.0)> helper false\n" + "/stack-a/stack-b (script:0 job:0.0)> echo hello1\n" + - "/stack-a/stack-b (script:0 job:1.0)> " + HelperPath + " false\n" + + "/stack-a/stack-b (script:0 job:1.0)> helper false\n" + "Error: one or more commands failed\n" + - "> execution failed: running " + HelperPath + " false (in /stack-a): exit status 1\n" + - "> execution failed: running " + HelperPath + " false (in /stack-a/stack-b): exit status 1\n", + "> execution failed: running helper false (in /stack-a): exit status 1\n" + + "> execution failed: running helper false (in /stack-a/stack-b): exit status 1\n", Stdout: "hello1" + "\n" + "hello1" + "\n", }, @@ -489,6 +489,7 @@ func TestScriptRun(t *testing.T) { env = append(env, tc.env...) cli := NewCLI(t, wd, env...) + cli.PrependToPath(filepath.Dir(HelperPath)) args := tc.args args = append(args, tc.runScript...) AssertRunResult(t, cli.RunScript(args...), tc.want) diff --git a/e2etests/internal/runner/runner.go b/e2etests/internal/runner/runner.go index e9e57df088..7fd7a64ef5 100644 --- a/e2etests/internal/runner/runner.go +++ b/e2etests/internal/runner/runner.go @@ -430,8 +430,8 @@ func AssertRunResult(t *testing.T, got RunResult, want RunExpected) { } } } else { - if want.Stderr != got.Stderr { - t.Errorf("stderr mismatch: got %q != want %q", got.Stderr, want.Stderr) + if diff := cmp.Diff(want.Stderr, got.Stderr); diff != "" { + t.Errorf("stderr mismatch (-want +got): %s", diff) } } } diff --git a/e2etests/internal/runner/setup.go b/e2etests/internal/runner/setup.go index fb192b3a12..f6bd113e82 100644 --- a/e2etests/internal/runner/setup.go +++ b/e2etests/internal/runner/setup.go @@ -23,6 +23,10 @@ var TerraformVersion string // TerraformTestPath is the path to the installed terraform binary. var TerraformTestPath string +// TerraformTestPathAsHCL is the path to the installed terraform binary +// but suited for including in an HCL string expression. +var TerraformTestPathAsHCL string + var terraformCleanup func() // HelperPath is the path to the test binary we compiled for test purposes @@ -64,6 +68,10 @@ func Setup(projectRoot string) (err error) { err = errors.E(err, "failed to setup Terraform binary") return } + TerraformTestPathAsHCL = fmt.Sprintf(`${tm_chomp(<<-EOF + %s + EOF + )}`, TerraformTestPath) }) if err == nil { diff --git a/makefiles/windows.mk b/makefiles/windows.mk index 157e2f13f6..968dcd17a2 100644 --- a/makefiles/windows.mk +++ b/makefiles/windows.mk @@ -34,8 +34,10 @@ tempdir=$(shell .\bin\helper.exe tempdir) test: test/helper build set TM_TEST_ROOT_TEMPDIR=$(tempdir) go test -timeout 30m -p 100 ./... + set status=%errorlevel% .\bin\helper.exe rm $(tempdir) .\bin\terramate.exe run -- helper.exe true + exit %status% ## remove build artifacts .PHONY: clean