|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + cfg "github.com/cloudposse/atmos/pkg/config" |
| 13 | + "github.com/cloudposse/atmos/pkg/schema" |
| 14 | + "github.com/cloudposse/atmos/tests/testhelpers" |
| 15 | +) |
| 16 | + |
| 17 | +// TestCustomCommandContainerBuildPassesWithBlockToDocker reproduces |
| 18 | +// https://github.com/cloudposse/atmos/issues/2876 end to end: a |
| 19 | +// `.atmos.d/commands.yaml`-style custom command with a `type: container, |
| 20 | +// action: build` step and a `with:` block must invoke docker with the full |
| 21 | +// configured Buildx engine, builder, cache, tags, dockerfile, and context -- |
| 22 | +// not silently fall back to `docker build -f Dockerfile .`. |
| 23 | +// |
| 24 | +// This writes real config to disk, loads it through cfg.InitCliConfig (the |
| 25 | +// same production config-loading path `atmos` itself uses), registers it via |
| 26 | +// processCustomCommands, and invokes the resulting custom command |
| 27 | +// through RootCmd.Execute() exactly as a user would from the shell. A fake, |
| 28 | +// logging `docker` executable on PATH (testhelpers.InstallFakeContainerRuntime) |
| 29 | +// captures the real argv Atmos emits, so this exercises the real command |
| 30 | +// executor rather than manually constructing schema.Task/WorkflowStep/ |
| 31 | +// ContainerBuildStep values in Go. |
| 32 | +func TestCustomCommandContainerBuildPassesWithBlockToDocker(t *testing.T) { |
| 33 | + _ = NewTestKit(t) |
| 34 | + |
| 35 | + tempDir := t.TempDir() |
| 36 | + appDir := filepath.Join(tempDir, "app") |
| 37 | + require.NoError(t, os.MkdirAll(appDir, 0o755)) |
| 38 | + require.NoError(t, os.WriteFile(filepath.Join(appDir, "Dockerfile"), []byte("FROM scratch\n"), 0o644)) |
| 39 | + |
| 40 | + atmosYAML := ` |
| 41 | +base_path: "." |
| 42 | +commands: |
| 43 | + - name: test-container-build-with-block |
| 44 | + description: Build the application image |
| 45 | + steps: |
| 46 | + - name: build |
| 47 | + type: container |
| 48 | + action: build |
| 49 | + provider: docker |
| 50 | + with: |
| 51 | + engine: buildx |
| 52 | + context: app |
| 53 | + dockerfile: Dockerfile |
| 54 | + tags: |
| 55 | + - "example.invalid/demo:sha-test" |
| 56 | + driver: |
| 57 | + name: atmos-native-ci |
| 58 | + provider: docker-container |
| 59 | + opts: |
| 60 | + image: mirror.gcr.io/moby/buildkit:buildx-stable-1 |
| 61 | + cache: |
| 62 | + from: |
| 63 | + - type: registry |
| 64 | + ref: "example.invalid/demo:buildcache" |
| 65 | + to: |
| 66 | + - type: registry |
| 67 | + ref: "example.invalid/demo:buildcache" |
| 68 | + mode: max |
| 69 | +` |
| 70 | + require.NoError(t, os.WriteFile(filepath.Join(tempDir, "atmos.yaml"), []byte(atmosYAML), 0o644)) |
| 71 | + |
| 72 | + t.Setenv("ATMOS_CLI_CONFIG_PATH", tempDir) |
| 73 | + t.Setenv("ATMOS_BASE_PATH", tempDir) |
| 74 | + t.Chdir(tempDir) |
| 75 | + |
| 76 | + argsPath := filepath.Join(t.TempDir(), "docker-args.log") |
| 77 | + t.Setenv("ATMOS_FAKE_RUNTIME_ARGS_FILE", argsPath) |
| 78 | + testhelpers.InstallFakeContainerRuntime(t, testhelpers.FakeContainerRuntimeSpec{ |
| 79 | + Name: "docker", |
| 80 | + Mode: testhelpers.FakeContainerRuntimeStep, |
| 81 | + }) |
| 82 | + |
| 83 | + atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, false) |
| 84 | + require.NoError(t, err) |
| 85 | + |
| 86 | + require.NoError(t, processCustomCommands(atmosConfig, atmosConfig.Commands, RootCmd)) |
| 87 | + |
| 88 | + RootCmd.SetArgs([]string{"test-container-build-with-block"}) |
| 89 | + require.NoError(t, RootCmd.Execute()) |
| 90 | + |
| 91 | + content, err := os.ReadFile(argsPath) |
| 92 | + require.NoError(t, err, "the fake docker executable must have been invoked at least once") |
| 93 | + lines := strings.Split(strings.TrimSpace(string(content)), "\n") |
| 94 | + |
| 95 | + var buildLine string |
| 96 | + for _, line := range lines { |
| 97 | + fields := strings.Split(line, "\t") |
| 98 | + if len(fields) > 1 && fields[0] == "buildx" && fields[1] == "build" { |
| 99 | + buildLine = line |
| 100 | + break |
| 101 | + } |
| 102 | + } |
| 103 | + require.NotEmpty(t, buildLine, |
| 104 | + "expected a `docker buildx build ...` invocation; got invocations: %v", lines) |
| 105 | + |
| 106 | + fields := strings.Split(buildLine, "\t") |
| 107 | + assert.Contains(t, fields, "--builder", "configured Buildx driver must be applied") |
| 108 | + assert.Contains(t, fields, "atmos-native-ci") |
| 109 | + assert.Contains(t, fields, "--cache-from", "configured registry cache-from must be applied") |
| 110 | + assert.Contains(t, fields, "--cache-to", "configured registry cache-to must be applied") |
| 111 | + assert.Contains(t, fields, "-t", "configured tag must be applied") |
| 112 | + assert.Contains(t, fields, "example.invalid/demo:sha-test") |
| 113 | + assert.Contains(t, fields, "-f", "configured Dockerfile must be applied") |
| 114 | + assert.Contains(t, fields, "Dockerfile") |
| 115 | + assert.Contains(t, fields, "app", "configured context must be applied") |
| 116 | + |
| 117 | + // The exact bug report's symptom: Atmos must not fall back to a bare, |
| 118 | + // unconfigured `docker build -f Dockerfile .`. |
| 119 | + for _, line := range lines { |
| 120 | + assert.NotEqual(t, "build\t-f\tDockerfile\t.", line, |
| 121 | + "must not silently fall back to a bare, unconfigured docker build") |
| 122 | + } |
| 123 | +} |
0 commit comments