Skip to content

Commit

Permalink
buildx integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
reingart committed Jan 20, 2025
1 parent fd74681 commit c608380
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
53 changes: 53 additions & 0 deletions integration/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func TestBuild(t *testing.T) {
description: "docker build",
dir: "testdata/build",
},
{
setup: setupBuildX,
description: "docker buildx",
args: []string{"--config", "config", "--verbosity=trace"},
dir: "testdata/buildx",
},
{
description: "git tagger",
dir: "testdata/tagPolicy",
Expand Down Expand Up @@ -131,6 +137,7 @@ func TestBuildWithWithPlatform(t *testing.T) {
dir string
args []string
image string
setup func(t *testing.T, workdir string)
expectedPlatforms []v1.Platform
}{
{
Expand All @@ -145,11 +152,28 @@ func TestBuildWithWithPlatform(t *testing.T) {
args: []string{"--platform", "linux/arm64"},
expectedPlatforms: []v1.Platform{{OS: "linux", Architecture: "arm64"}},
},
{
setup: setupBuildX,
description: "docker buildx linux/amd64",
dir: "testdata/buildx",
args: []string{"--platform", "linux/amd64", "--cache-artifacts=false", "--config", "config", "--verbosity=trace"},
expectedPlatforms: []v1.Platform{{OS: "linux", Architecture: "amd64"}},
},
{
setup: setupBuildX,
description: "docker buildx linux/arm64",
dir: "testdata/buildx",
args: []string{"--platform", "linux/arm64", "--cache-artifacts=false", "--config", "config", "--verbosity=trace"},
expectedPlatforms: []v1.Platform{{OS: "linux", Architecture: "arm64"}},
},
}

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
MarkIntegrationTest(t, CanRunWithoutGcp)
if test.setup != nil {
test.setup(t, test.dir)
}
tmpfile := testutil.TempFile(t, "", []byte{})
args := append(test.args, "--file-output", tmpfile)
skaffold.Build(args...).InDir(test.dir).RunOrFail(t)
Expand Down Expand Up @@ -314,6 +338,35 @@ func setupGitRepo(t *testing.T, dir string) {
}
}

// setupBuildX sets up a docker buildx builder using buildkit
func setupBuildX(t *testing.T, dir string) {
t.Cleanup(func() {
dockerArgs := [][]string{
{"buildx", "uninstall"},
{"buildx", "rm", "buildkit"},
}
for _, args := range dockerArgs {
cmd := exec.Command("docker", args...)
if buf, err := util.RunCmdOut(context.Background(), cmd); err != nil {
t.Log(string(buf))
t.Fatal(err)
}
}
})

dockerArgs := [][]string{
{"buildx", "install"},
{"buildx", "create", "--driver", "docker-container", "--name", "buildkit"},
}
for _, args := range dockerArgs {
cmd := exec.Command("docker", args...)
if buf, err := util.RunCmdOut(context.Background(), cmd); err != nil {
t.Log(string(buf))
t.Fatal(err)
}
}
}

// nowInChicago returns the dateTime string as generated by the dateTime tagger
func nowInChicago() string {
loc, _ := tz.LoadLocation("America/Chicago")
Expand Down
12 changes: 12 additions & 0 deletions integration/testdata/buildx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# syntax=docker/dockerfile:1

FROM golang:1.23-alpine AS builder

COPY main.go .
RUN go build -o /app main.go

FROM alpine:3

COPY --from=builder /app .

CMD ["/app"]
9 changes: 9 additions & 0 deletions integration/testdata/buildx/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
global:
cache-tag: cache
buildx-builder: buildkit
survey:
last-prompted: "2025-01-19T21:32:47Z"
collect-metrics: true
update:
last-prompted: "2025-01-19T23:10:47Z"
kubeContexts: []
14 changes: 14 additions & 0 deletions integration/testdata/buildx/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"fmt"
"time"
)

func main() {
for counter := 0; ; counter++ {
fmt.Println("Hello world!", counter)

time.Sleep(time.Second * 1)
}
}
12 changes: 12 additions & 0 deletions integration/testdata/buildx/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: skaffold/v4beta12
kind: Config
build:
artifacts:
- image: my-app
docker:
dockerfile: Dockerfile
local:
useBuildkit: true
useDockerCLI: true
tryImportMissing: true
push: false
2 changes: 1 addition & 1 deletion pkg/skaffold/build/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func parseBuildxMetadataFile(ctx context.Context, filename string) (string, erro
if err == nil {
// avoid panic: interface conversion: interface {} is nil, not string (if keys don't exists)
var digest string
if value := metadata["containerimage.digest"]; value != nil {
if value := metadata["containerimage.config.digest"]; value != nil {
digest = value.(string)
}
var name string
Expand Down

0 comments on commit c608380

Please sign in to comment.