buildkit/client/client_test.go

133 lines
2.8 KiB
Go
Raw Normal View History

package client
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"syscall"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tonistiigi/buildkit_poc/client/llb"
)
var clientAddressStandalone string
var clientAddressContainerd string
func TestMain(m *testing.M) {
if testing.Short() {
os.Exit(m.Run())
}
cleanup, err := setupStandalone()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer cleanup()
cleanup, err = setupContainerd()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer cleanup()
os.Exit(m.Run())
}
func runContainerd() (string, func(), error) {
var buf = bytes.NewBuffer(nil)
tmpdir, err := ioutil.TempDir("", "containerd")
if err != nil {
return "", nil, err
}
defer os.RemoveAll(tmpdir)
address := filepath.Join(tmpdir, "containerd.sock")
args := append([]string{}, "containerd", "--root", tmpdir, "--root", filepath.Join(tmpdir, "state"), "--address", address)
cmd := exec.Command(args[0], args[1:]...)
cmd.Stderr = buf
if err := cmd.Start(); err != nil {
return "", nil, err
}
return address, func() {
// tear down the daemon and resources created
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
fmt.Fprintln(os.Stderr, err)
}
if _, err := cmd.Process.Wait(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
os.RemoveAll(tmpdir)
}, nil
}
func runBuildd(args []string) (string, func(), error) {
var buf = bytes.NewBuffer(nil)
tmpdir, err := ioutil.TempDir("", "buildd")
if err != nil {
return "", nil, err
}
defer os.RemoveAll(tmpdir)
address := filepath.Join(tmpdir, "buildd.sock")
args = append(args, "--root", tmpdir, "--socket", address, "--debug")
cmd := exec.Command(args[0], args[1:]...)
cmd.Stderr = buf
if err := cmd.Start(); err != nil {
return "", nil, err
}
return address, func() {
// tear down the daemon and resources created
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
fmt.Fprintln(os.Stderr, err)
}
if _, err := cmd.Process.Wait(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
os.RemoveAll(tmpdir)
}, nil
}
func testCallDiskUsage(t *testing.T, address string) {
c, err := New(address)
assert.Nil(t, err)
_, err = c.DiskUsage(context.TODO())
assert.Nil(t, err)
}
func testBuildMultiMount(t *testing.T, address string) {
t.Parallel()
c, err := New(address)
assert.Nil(t, err)
alpine := llb.Image("docker.io/library/alpine:latest")
ls := alpine.Run(llb.Shlex("/bin/ls -l"))
busybox := llb.Image("docker.io/library/busybox:latest")
cp := ls.Run(llb.Shlex("/bin/cp -a /busybox/etc/passwd baz"))
cp.AddMount("/busybox", busybox)
dt, err := cp.Marshal()
assert.Nil(t, err)
buf := bytes.NewBuffer(nil)
err = llb.WriteTo(dt, buf)
assert.Nil(t, err)
err = c.Solve(context.TODO(), buf, nil)
assert.Nil(t, err)
}