Skip to content

Commit

Permalink
add testing for buildkit helpers
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
  • Loading branch information
tonistiigi committed Jan 9, 2021
1 parent bf75a2b commit ba0255d
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ jobs:
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
-
name: Test
if: matrix.target == 'buildkit-helper-all'
run: |
docker buildx bake buildkit-test --progress=plain
-
name: Build ${{ matrix.target }}
run: |
Expand Down
11 changes: 10 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,20 @@ WORKDIR /src
RUN --mount=target=. \
TARGETPLATFORM=$TARGETPLATFORM go build -o /go/bin/binfmt ./cmd/binfmt


FROM scratch AS binaries
ARG BINARY_PREFIX
COPY --from=build usr/bin/${BINARY_PREFIX}qemu-* /

FROM --platform=$BUILDPLATFORM tonistiigi/bats-assert AS assert

FROM golang:alpine AS buildkit-test
RUN apk add --no-cache bash bats
WORKDIR /work
COPY --from=assert . .
COPY test .
COPY --from=binaries / /usr/bin
RUN ./run.sh

FROM scratch
COPY --from=binaries / /usr/bin/
COPY --from=binfmt /go/bin/binfmt /usr/bin/binfmt
Expand Down
7 changes: 7 additions & 0 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@ target "buildkit-helper" {
target "buildkit-helper-all" {
inherits = ["buildkit-helper", "all-arch"]
}

target "buildkit-test" {
inherits = ["buildkit-helper"]
target = "buildkit-test"
cache-to = []
tags = []
}
47 changes: 47 additions & 0 deletions test/exec/execargv0.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"errors"
"flag"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)

func main() {
if err := run(); err != nil {
log.Printf("%+v", err)
os.Exit(1)
}
}

func run() error {
flag.Parse()
if len(flag.Args()) < 2 {
return errors.New("at least 2 arguments required")
}
cmd := &exec.Cmd{
Args: flag.Args()[1:],
}
argv0 := flag.Arg(0)
if filepath.IsAbs(argv0) {
cmd.Path = argv0
} else if strings.HasPrefix(argv0, "./") {
p, err := filepath.Abs(argv0)
if err != nil {
return err
}
cmd.Path = p
} else {
p, err := exec.LookPath(argv0)
if err != nil {
return err
}
cmd.Path = p
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
11 changes: 11 additions & 0 deletions test/print/printargs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"fmt"
"os"
"strings"
)

func main() {
fmt.Println(strings.Join(os.Args, " "))
}
26 changes: 26 additions & 0 deletions test/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env sh

go build print/printargs.go
go build exec/execargv0.go

echo "testing $(uname -m)"
./test.bats

crossArch=arm64
crossEmulator=aarch64

if [ "$(uname -m)" = "aarch64" ]; then
crossArch="amd64"
crossEmulator="x86_64"
fi

GOARCH=$crossArch go build print/printargs.go
GOARCH=$crossArch go build exec/execargv0.go

if ./printargs >/dev/null 2>/dev/nulll; then
echo "can't test emulator because $crossEmulator emulator is installed in the kernel"
exit 1
fi

echo "testing $crossEmulator"
BINFMT_EMULATOR=$crossEmulator ./test.bats
1 change: 1 addition & 0 deletions test/shebang.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#!./printargs
1 change: 1 addition & 0 deletions test/shebang2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#!./printargs arg
1 change: 1 addition & 0 deletions test/shebang3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#!/work/printargs
1 change: 1 addition & 0 deletions test/shebang4.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#!/work/shebang3.sh
87 changes: 87 additions & 0 deletions test/test.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env bats

load "assert"

exec0() {
if [ -z "$BINFMT_EMULATOR" ]; then
run ./execargv0 "$@"
else
run buildkit-qemu-$BINFMT_EMULATOR ./execargv0 "$@"
fi
}

execdirect() {
if [ -z "$BINFMT_EMULATOR" ]; then
run "$@"
else
run buildkit-qemu-$BINFMT_EMULATOR "$@"
fi
}


@test "exec-single" {
exec0 ./printargs foo bar1 bar2
assert_success
assert_output "foo bar1 bar2"
}

@test "exec-multi" {
exec0 ./execargv0 ./printargs ./printargs baz
assert_success
assert_output "baz"
}

@test "exec-multi-abs" {
exec0 ./execargv0 $(pwd)/printargs $(pwd)/printargs baz
assert_success
assert_output "baz"
}

@test "exec-multi-path" {
cp $(pwd)/printargs /usr/bin/test-printargs
exec0 test-printargs test-printargs abc
assert_success
assert_output "test-printargs abc"
}

@test "exec-direct" {
execdirect test-printargs foo bar1
assert_success
assert_output "test-printargs foo bar1"
}

@test "exec-direct-abs" {
execdirect $(pwd)/printargs foo bar1
assert_success
assert_output "$(pwd)/printargs foo bar1"
}

@test "shebang" {
exec0 ./shebang.sh arg1 arg2
assert_success
assert_output "./printargs $(pwd)/shebang.sh arg2"
}

@test "shebang-arg" {
exec0 ./shebang2.sh arg1 arg2
assert_success
assert_output "./printargs arg $(pwd)/shebang2.sh arg2"
}

@test "shebang-abs" {
exec0 ./shebang3.sh arg1 arg2
assert_success
assert_output "/work/printargs $(pwd)/shebang3.sh arg2"
}

@test "shebang-multi" {
exec0 ./shebang4.sh arg1 arg2
assert_success
assert_output "/work/printargs $(pwd)/shebang3.sh $(pwd)/shebang4.sh arg2"
}

@test "shebang-direct" {
execdirect ./shebang.sh foo bar1
assert_success
assert_output "./printargs ./shebang.sh foo bar1"
}

0 comments on commit ba0255d

Please sign in to comment.