2017-09-12 05:29:12 +02:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2017-12-26 18:56:44 +02:00
|
|
|
"flag"
|
2018-10-03 14:58:02 +02:00
|
|
|
"fmt"
|
2017-09-13 01:58:02 +02:00
|
|
|
"io/ioutil"
|
2017-09-12 05:29:12 +02:00
|
|
|
"os"
|
2017-09-13 01:58:02 +02:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2018-10-03 14:58:02 +02:00
|
|
|
"regexp"
|
2017-12-26 03:53:44 +02:00
|
|
|
"syscall"
|
2017-12-26 18:56:44 +02:00
|
|
|
"testing"
|
2017-12-26 03:53:44 +02:00
|
|
|
|
2017-12-18 01:00:50 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
2018-09-12 19:18:01 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/pipe"
|
2018-08-15 04:50:20 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/config"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2017-09-13 01:58:02 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-08-15 15:49:28 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-09-12 05:29:12 +02:00
|
|
|
)
|
|
|
|
|
2017-12-26 18:56:44 +02:00
|
|
|
var it = flag.Bool("it", false, "push images to docker hub")
|
|
|
|
var registry = "localhost:5000/"
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2017-12-26 19:24:19 +02:00
|
|
|
flag.Parse()
|
|
|
|
if *it {
|
2017-12-26 18:56:44 +02:00
|
|
|
registry = "docker.io/"
|
|
|
|
}
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
func start(t *testing.T) {
|
2017-12-26 19:41:50 +02:00
|
|
|
if *it {
|
2017-12-26 18:56:44 +02:00
|
|
|
return
|
|
|
|
}
|
2018-02-24 22:59:08 +02:00
|
|
|
if out, err := exec.Command(
|
2017-12-26 18:56:44 +02:00
|
|
|
"docker", "run", "-d", "-p", "5000:5000", "--name", "registry", "registry:2",
|
2018-02-24 22:59:08 +02:00
|
|
|
).CombinedOutput(); err != nil {
|
|
|
|
t.Log("failed to start docker registry", string(out), err)
|
2017-12-26 18:56:44 +02:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 01:00:50 +02:00
|
|
|
func killAndRm(t *testing.T) {
|
2017-12-26 19:41:50 +02:00
|
|
|
if *it {
|
2017-12-26 18:56:44 +02:00
|
|
|
return
|
|
|
|
}
|
2017-12-18 01:00:50 +02:00
|
|
|
t.Log("killing registry")
|
2017-10-07 22:32:17 +02:00
|
|
|
_ = exec.Command("docker", "kill", "registry").Run()
|
|
|
|
_ = exec.Command("docker", "rm", "registry").Run()
|
|
|
|
}
|
|
|
|
|
2017-09-13 01:58:02 +02:00
|
|
|
func TestRunPipe(t *testing.T) {
|
2018-02-16 14:35:44 +02:00
|
|
|
type errChecker func(*testing.T, error)
|
|
|
|
var shouldErr = func(msg string) errChecker {
|
|
|
|
return func(t *testing.T, err error) {
|
2018-08-15 15:49:28 +02:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), msg)
|
2018-02-16 14:35:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var shouldNotErr = func(t *testing.T, err error) {
|
2018-08-15 15:49:28 +02:00
|
|
|
require.NoError(t, err)
|
2018-02-16 14:35:44 +02:00
|
|
|
}
|
2018-10-03 14:58:02 +02:00
|
|
|
type imageLabelFinder func(*testing.T, int, string)
|
|
|
|
var shouldFindImagesWithLabels = func(filters ...string) func(*testing.T, int, string) {
|
|
|
|
return func(t *testing.T, numTags int, image string) {
|
|
|
|
for _, filter := range filters {
|
|
|
|
output, err := exec.Command("docker", "images", "--filter", filter).CombinedOutput()
|
|
|
|
require.NoError(t, err)
|
|
|
|
fmt.Println(string(output))
|
|
|
|
|
|
|
|
matcher := regexp.MustCompile(image)
|
|
|
|
matches := matcher.FindAllStringIndex(string(output), -1)
|
|
|
|
require.Equal(t, numTags, len(matches))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
var noLabels = func(t *testing.T, numTags int, image string) {}
|
2018-02-16 14:35:44 +02:00
|
|
|
|
2017-12-05 15:19:44 +02:00
|
|
|
var table = map[string]struct {
|
2018-10-03 14:58:02 +02:00
|
|
|
dockers []config.Docker
|
|
|
|
publish bool
|
|
|
|
expect []string
|
|
|
|
assertImageLabels imageLabelFinder
|
|
|
|
assertError errChecker
|
2017-12-05 15:19:44 +02:00
|
|
|
}{
|
|
|
|
"valid": {
|
2017-12-26 19:20:25 +02:00
|
|
|
publish: true,
|
2018-05-23 14:30:35 +02:00
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: registry + "goreleaser/test_run_pipe",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "mybin",
|
|
|
|
TagTemplates: []string{
|
|
|
|
"{{.Tag}}-{{.Env.FOO}}",
|
|
|
|
"v{{.Major}}",
|
|
|
|
"v{{.Major}}.{{.Minor}}",
|
2018-10-03 14:58:02 +02:00
|
|
|
"commit-{{.Commit}}",
|
2018-07-09 06:05:18 +02:00
|
|
|
"le-{{.Os}}",
|
2018-05-23 14:30:35 +02:00
|
|
|
"latest",
|
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
BuildFlagTemplates: []string{
|
|
|
|
"--label=org.label-schema.schema-version=1.0",
|
|
|
|
"--label=org.label-schema.version={{.Version}}",
|
|
|
|
"--label=org.label-schema.vcs-ref={{.Commit}}",
|
|
|
|
"--label=org.label-schema.name={{.ProjectName}}",
|
|
|
|
"--build-arg=FRED={{.Tag}}",
|
|
|
|
},
|
2018-05-23 14:30:35 +02:00
|
|
|
Files: []string{
|
|
|
|
"testdata/extra_file.txt",
|
|
|
|
},
|
2017-12-26 18:56:44 +02:00
|
|
|
},
|
2017-12-05 15:19:44 +02:00
|
|
|
},
|
2017-12-26 19:20:25 +02:00
|
|
|
expect: []string{
|
|
|
|
registry + "goreleaser/test_run_pipe:v1.0.0-123",
|
2018-01-19 03:55:26 +02:00
|
|
|
registry + "goreleaser/test_run_pipe:v1",
|
|
|
|
registry + "goreleaser/test_run_pipe:v1.0",
|
2018-10-03 14:58:02 +02:00
|
|
|
registry + "goreleaser/test_run_pipe:commit-a1b2c3d4",
|
2018-07-09 06:05:18 +02:00
|
|
|
registry + "goreleaser/test_run_pipe:le-linux",
|
2017-12-26 19:20:25 +02:00
|
|
|
registry + "goreleaser/test_run_pipe:latest",
|
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
assertImageLabels: shouldFindImagesWithLabels(
|
|
|
|
"label=org.label-schema.schema-version=1.0",
|
|
|
|
"label=org.label-schema.version=1.0.0",
|
|
|
|
"label=org.label-schema.vcs-ref=a1b2c3d4",
|
|
|
|
"label=org.label-schema.name=mybin"),
|
2018-02-16 14:35:44 +02:00
|
|
|
assertError: shouldNotErr,
|
2017-12-05 15:19:44 +02:00
|
|
|
},
|
2018-08-20 23:58:56 +02:00
|
|
|
"multiple images with same extra file": {
|
|
|
|
publish: true,
|
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: registry + "goreleaser/multiplefiles1",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "mybin",
|
|
|
|
TagTemplates: []string{
|
|
|
|
"latest",
|
|
|
|
},
|
|
|
|
Files: []string{
|
|
|
|
"testdata/extra_file.txt",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Image: registry + "goreleaser/multiplefiles2",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "mybin",
|
|
|
|
TagTemplates: []string{
|
|
|
|
"latest",
|
|
|
|
},
|
|
|
|
Files: []string{
|
|
|
|
"testdata/extra_file.txt",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expect: []string{
|
|
|
|
registry + "goreleaser/multiplefiles1:latest",
|
|
|
|
registry + "goreleaser/multiplefiles2:latest",
|
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
assertImageLabels: noLabels,
|
|
|
|
assertError: shouldNotErr,
|
2018-08-20 23:58:56 +02:00
|
|
|
},
|
2018-05-23 14:30:35 +02:00
|
|
|
"multiple images with same dockerfile": {
|
2018-03-24 23:41:24 +02:00
|
|
|
publish: true,
|
2018-05-23 14:30:35 +02:00
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: registry + "goreleaser/test_run_pipe",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "mybin",
|
|
|
|
TagTemplates: []string{"latest"},
|
2018-03-24 23:41:24 +02:00
|
|
|
},
|
2018-05-23 14:30:35 +02:00
|
|
|
{
|
|
|
|
Image: registry + "goreleaser/test_run_pipe2",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "mybin",
|
|
|
|
TagTemplates: []string{"latest"},
|
|
|
|
},
|
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
assertImageLabels: noLabels,
|
2018-05-23 14:30:35 +02:00
|
|
|
expect: []string{
|
|
|
|
registry + "goreleaser/test_run_pipe:latest",
|
|
|
|
registry + "goreleaser/test_run_pipe2:latest",
|
|
|
|
},
|
|
|
|
assertError: shouldNotErr,
|
|
|
|
},
|
|
|
|
"valid_skip_push": {
|
|
|
|
publish: true,
|
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: registry + "goreleaser/test_run_pipe",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "mybin",
|
|
|
|
SkipPush: true,
|
|
|
|
TagTemplates: []string{
|
|
|
|
"{{.Tag}}-{{.Env.FOO}}",
|
|
|
|
"v{{.Major}}",
|
|
|
|
"v{{.Major}}.{{.Minor}}",
|
|
|
|
"latest",
|
|
|
|
},
|
|
|
|
Files: []string{
|
|
|
|
"testdata/extra_file.txt",
|
|
|
|
},
|
2018-03-24 23:41:24 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expect: []string{
|
|
|
|
registry + "goreleaser/test_run_pipe:v1.0.0-123",
|
|
|
|
registry + "goreleaser/test_run_pipe:v1",
|
|
|
|
registry + "goreleaser/test_run_pipe:v1.0",
|
|
|
|
registry + "goreleaser/test_run_pipe:latest",
|
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
assertImageLabels: noLabels,
|
|
|
|
assertError: shouldNotErr,
|
2018-03-24 23:41:24 +02:00
|
|
|
},
|
2017-12-26 19:20:25 +02:00
|
|
|
"valid_no_latest": {
|
|
|
|
publish: true,
|
2018-05-23 14:30:35 +02:00
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: registry + "goreleaser/test_run_pipe",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "mybin",
|
|
|
|
TagTemplates: []string{
|
|
|
|
"{{.Version}}",
|
|
|
|
},
|
|
|
|
Files: []string{
|
|
|
|
"testdata/extra_file.txt",
|
|
|
|
},
|
2017-12-26 19:20:25 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expect: []string{
|
|
|
|
registry + "goreleaser/test_run_pipe:1.0.0",
|
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
assertImageLabels: noLabels,
|
|
|
|
assertError: shouldNotErr,
|
2017-12-26 19:20:25 +02:00
|
|
|
},
|
|
|
|
"valid_dont_publish": {
|
|
|
|
publish: false,
|
2018-05-23 14:30:35 +02:00
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: registry + "goreleaser/test_run_pipe",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "mybin",
|
|
|
|
TagTemplates: []string{
|
|
|
|
"{{.Tag}}-{{.Env.FOO}}",
|
|
|
|
"latest",
|
|
|
|
},
|
|
|
|
Files: []string{
|
|
|
|
"testdata/extra_file.txt",
|
|
|
|
},
|
2017-12-26 19:20:25 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
expect: []string{
|
|
|
|
registry + "goreleaser/test_run_pipe:v1.0.0-123",
|
|
|
|
registry + "goreleaser/test_run_pipe:latest",
|
2017-12-05 15:19:44 +02:00
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
assertImageLabels: noLabels,
|
|
|
|
assertError: shouldNotErr,
|
2017-12-05 15:19:44 +02:00
|
|
|
},
|
2018-10-02 18:02:12 +02:00
|
|
|
"valid build args": {
|
|
|
|
publish: false,
|
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: registry + "goreleaser/test_build_args",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "mybin",
|
|
|
|
TagTemplates: []string{
|
|
|
|
"latest",
|
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
BuildFlagTemplates: []string{
|
2018-10-02 18:02:12 +02:00
|
|
|
"--label=foo=bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expect: []string{
|
|
|
|
registry + "goreleaser/test_build_args:latest",
|
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
assertImageLabels: noLabels,
|
|
|
|
assertError: shouldNotErr,
|
2018-10-02 18:02:12 +02:00
|
|
|
},
|
|
|
|
"bad build args": {
|
|
|
|
publish: false,
|
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: registry + "goreleaser/test_build_args",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "mybin",
|
|
|
|
TagTemplates: []string{
|
|
|
|
"latest",
|
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
BuildFlagTemplates: []string{
|
2018-10-02 18:02:12 +02:00
|
|
|
"--bad-flag",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
assertImageLabels: noLabels,
|
|
|
|
assertError: shouldErr("unknown flag: --bad-flag"),
|
2018-10-02 18:02:12 +02:00
|
|
|
},
|
2017-12-26 19:20:25 +02:00
|
|
|
"bad_dockerfile": {
|
|
|
|
publish: true,
|
2018-05-23 14:30:35 +02:00
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
2018-08-15 15:49:28 +02:00
|
|
|
Image: registry + "goreleaser/bad_dockerfile",
|
2018-05-23 14:30:35 +02:00
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile.bad",
|
|
|
|
Binary: "mybin",
|
|
|
|
TagTemplates: []string{
|
|
|
|
"{{.Version}}",
|
|
|
|
},
|
2018-01-18 21:40:44 +02:00
|
|
|
},
|
2017-12-26 19:20:25 +02:00
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
assertImageLabels: noLabels,
|
|
|
|
assertError: shouldErr("pull access denied for nope, repository does not exist"),
|
2017-12-26 19:20:25 +02:00
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
"tag_template_error": {
|
2017-12-26 19:20:25 +02:00
|
|
|
publish: true,
|
2018-05-23 14:30:35 +02:00
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: registry + "goreleaser/test_run_pipe",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "mybin",
|
|
|
|
TagTemplates: []string{
|
|
|
|
"{{.Tag}",
|
|
|
|
},
|
2018-01-18 21:40:44 +02:00
|
|
|
},
|
2017-12-05 15:19:44 +02:00
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
assertImageLabels: noLabels,
|
|
|
|
assertError: shouldErr(`template: tmpl:1: unexpected "}" in operand`),
|
|
|
|
},
|
|
|
|
"build_flag_template_error": {
|
|
|
|
publish: true,
|
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: registry + "goreleaser/test_run_pipe",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "mybin",
|
|
|
|
TagTemplates: []string{
|
|
|
|
"latest",
|
|
|
|
},
|
|
|
|
BuildFlagTemplates: []string{
|
|
|
|
"--label=tag={{.Tag}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
assertImageLabels: noLabels,
|
|
|
|
assertError: shouldErr(`template: tmpl:1: unexpected "}" in operand`),
|
2017-12-05 15:19:44 +02:00
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
"missing_env_on_tag_template": {
|
2018-01-02 22:44:36 +02:00
|
|
|
publish: true,
|
2018-05-23 14:30:35 +02:00
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: registry + "goreleaser/test_run_pipe",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "mybin",
|
|
|
|
TagTemplates: []string{
|
|
|
|
"{{.Env.NOPE}}",
|
|
|
|
},
|
2018-01-18 21:40:44 +02:00
|
|
|
},
|
2018-01-02 22:44:36 +02:00
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
assertImageLabels: noLabels,
|
2018-10-05 18:00:25 +02:00
|
|
|
assertError: shouldErr(`template: tmpl:1:46: executing "tmpl" at <.Env.NOPE>: map has no entry for key "NOPE"`),
|
2018-10-03 14:58:02 +02:00
|
|
|
},
|
|
|
|
"missing_env_on_build_flag_template": {
|
|
|
|
publish: true,
|
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: registry + "goreleaser/test_run_pipe",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "mybin",
|
|
|
|
TagTemplates: []string{
|
|
|
|
"latest",
|
|
|
|
},
|
|
|
|
BuildFlagTemplates: []string{
|
|
|
|
"--label=nope={{.Env.NOPE}}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
assertImageLabels: noLabels,
|
|
|
|
assertError: shouldErr(`template: tmpl:1:19: executing "tmpl" at <.Env.NOPE>: map has no entry for key "NOPE"`),
|
2018-01-02 22:44:36 +02:00
|
|
|
},
|
2018-10-05 18:00:25 +02:00
|
|
|
"image_has_projectname_template_variable": {
|
|
|
|
publish: true,
|
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: registry + "goreleaser/{{.ProjectName}}",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "mybin",
|
|
|
|
SkipPush: true,
|
|
|
|
TagTemplates: []string{
|
|
|
|
"{{.Tag}}-{{.Env.FOO}}",
|
|
|
|
"v{{.Major}}",
|
|
|
|
"v{{.Major}}.{{.Minor}}",
|
|
|
|
"latest",
|
|
|
|
},
|
|
|
|
Files: []string{
|
|
|
|
"testdata/extra_file.txt",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expect: []string{
|
|
|
|
registry + "goreleaser/mybin:v1.0.0-123",
|
|
|
|
registry + "goreleaser/mybin:v1",
|
|
|
|
registry + "goreleaser/mybin:v1.0",
|
|
|
|
registry + "goreleaser/mybin:latest",
|
|
|
|
},
|
|
|
|
assertImageLabels: noLabels,
|
|
|
|
assertError: shouldNotErr,
|
|
|
|
},
|
2017-12-26 18:56:44 +02:00
|
|
|
"no_permissions": {
|
2017-12-26 19:20:25 +02:00
|
|
|
publish: true,
|
2018-05-23 14:30:35 +02:00
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: "docker.io/nope",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Binary: "mybin",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
TagTemplates: []string{
|
|
|
|
"{{.Tag}}",
|
|
|
|
"latest",
|
|
|
|
},
|
2018-01-18 21:40:44 +02:00
|
|
|
},
|
2017-12-26 18:56:44 +02:00
|
|
|
},
|
2017-12-26 19:20:25 +02:00
|
|
|
expect: []string{
|
|
|
|
"docker.io/nope:latest",
|
|
|
|
"docker.io/nope:v1.0.0",
|
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
assertImageLabels: noLabels,
|
|
|
|
assertError: shouldErr(`requested access to the resource is denied`),
|
2017-12-26 18:56:44 +02:00
|
|
|
},
|
|
|
|
"dockerfile_doesnt_exist": {
|
2017-12-26 19:20:25 +02:00
|
|
|
publish: true,
|
2018-05-23 14:30:35 +02:00
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: "whatever",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Binary: "mybin",
|
|
|
|
Dockerfile: "testdata/Dockerfilezzz",
|
|
|
|
TagTemplates: []string{
|
|
|
|
"{{.Tag}}",
|
|
|
|
},
|
2018-01-18 21:40:44 +02:00
|
|
|
},
|
2017-12-26 18:56:44 +02:00
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
assertImageLabels: noLabels,
|
|
|
|
assertError: shouldErr(`failed to link dockerfile`),
|
2017-12-26 18:56:44 +02:00
|
|
|
},
|
|
|
|
"extra_file_doesnt_exist": {
|
2017-12-26 19:20:25 +02:00
|
|
|
publish: true,
|
2018-05-23 14:30:35 +02:00
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: "whatever",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Binary: "mybin",
|
|
|
|
Files: []string{
|
|
|
|
"testdata/nope.txt",
|
|
|
|
},
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
TagTemplates: []string{
|
|
|
|
"{{.Tag}}",
|
|
|
|
},
|
2018-01-18 21:40:44 +02:00
|
|
|
},
|
2017-12-26 18:56:44 +02:00
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
assertImageLabels: noLabels,
|
|
|
|
assertError: shouldErr(`failed to link extra file 'testdata/nope.txt'`),
|
2017-12-26 18:56:44 +02:00
|
|
|
},
|
|
|
|
"no_matching_binaries": {
|
2017-12-26 19:20:25 +02:00
|
|
|
publish: true,
|
2018-05-23 14:30:35 +02:00
|
|
|
dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: "whatever",
|
|
|
|
Goos: "darwin",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Binary: "mybinnnn",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
},
|
2017-12-26 18:56:44 +02:00
|
|
|
},
|
2018-10-03 14:58:02 +02:00
|
|
|
assertImageLabels: noLabels,
|
|
|
|
assertError: shouldErr(`0 binaries match docker definition: mybinnnn: darwin_amd64_`),
|
2017-12-26 18:56:44 +02:00
|
|
|
},
|
2017-12-05 15:19:44 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 01:00:50 +02:00
|
|
|
killAndRm(t)
|
2017-12-26 18:56:44 +02:00
|
|
|
start(t)
|
2017-12-18 01:00:50 +02:00
|
|
|
defer killAndRm(t)
|
|
|
|
|
2017-12-05 15:19:44 +02:00
|
|
|
for name, docker := range table {
|
2017-12-18 01:00:50 +02:00
|
|
|
t.Run(name, func(tt *testing.T) {
|
2017-12-26 18:56:44 +02:00
|
|
|
folder, err := ioutil.TempDir("", "archivetest")
|
2018-08-15 15:49:28 +02:00
|
|
|
require.NoError(tt, err)
|
2017-12-26 18:56:44 +02:00
|
|
|
var dist = filepath.Join(folder, "dist")
|
2018-08-15 15:49:28 +02:00
|
|
|
require.NoError(tt, os.Mkdir(dist, 0755))
|
|
|
|
require.NoError(tt, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
|
2017-12-26 18:56:44 +02:00
|
|
|
var binPath = filepath.Join(dist, "mybin", "mybin")
|
|
|
|
_, err = os.Create(binPath)
|
2018-08-15 15:49:28 +02:00
|
|
|
require.NoError(tt, err)
|
2017-12-26 18:56:44 +02:00
|
|
|
|
2017-12-29 21:35:22 +02:00
|
|
|
var ctx = context.New(config.Project{
|
|
|
|
ProjectName: "mybin",
|
|
|
|
Dist: dist,
|
2018-05-23 14:30:35 +02:00
|
|
|
Dockers: docker.dockers,
|
2017-12-29 21:35:22 +02:00
|
|
|
})
|
2018-03-01 06:12:58 +02:00
|
|
|
ctx.SkipPublish = !docker.publish
|
2017-12-29 21:35:22 +02:00
|
|
|
ctx.Env = map[string]string{
|
|
|
|
"FOO": "123",
|
|
|
|
}
|
|
|
|
ctx.Version = "1.0.0"
|
|
|
|
ctx.Git = context.GitInfo{
|
|
|
|
CurrentTag: "v1.0.0",
|
2018-10-03 14:58:02 +02:00
|
|
|
Commit: "a1b2c3d4",
|
2017-12-05 15:19:44 +02:00
|
|
|
}
|
2017-12-18 01:00:50 +02:00
|
|
|
for _, os := range []string{"linux", "darwin"} {
|
|
|
|
for _, arch := range []string{"amd64", "386"} {
|
|
|
|
ctx.Artifacts.Add(artifact.Artifact{
|
|
|
|
Name: "mybin",
|
|
|
|
Path: binPath,
|
|
|
|
Goarch: arch,
|
|
|
|
Goos: os,
|
|
|
|
Type: artifact.Binary,
|
|
|
|
Extra: map[string]string{
|
|
|
|
"Binary": "mybin",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2017-12-05 15:19:44 +02:00
|
|
|
}
|
2017-12-26 19:20:25 +02:00
|
|
|
|
|
|
|
// this might fail as the image doesnt exist yet, so lets ignore the error
|
|
|
|
for _, img := range docker.expect {
|
|
|
|
_ = exec.Command("docker", "rmi", img).Run()
|
|
|
|
}
|
|
|
|
|
2018-08-15 15:49:28 +02:00
|
|
|
docker.assertError(tt, Pipe{}.Run(ctx))
|
2018-10-03 14:58:02 +02:00
|
|
|
for _, d := range docker.dockers {
|
|
|
|
docker.assertImageLabels(tt, len(d.TagTemplates), d.Image)
|
|
|
|
}
|
2017-09-15 01:16:49 +02:00
|
|
|
|
2017-12-26 19:20:25 +02:00
|
|
|
// this might should not fail as the image should have been created when
|
|
|
|
// the step ran
|
|
|
|
for _, img := range docker.expect {
|
2017-12-29 21:35:22 +02:00
|
|
|
tt.Log("removing docker image", img)
|
2018-08-15 15:49:28 +02:00
|
|
|
require.NoError(tt, exec.Command("docker", "rmi", img).Run(), "could not delete image %s", img)
|
2017-12-26 19:20:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
2017-09-15 01:16:49 +02:00
|
|
|
}
|
2017-09-13 01:58:02 +02:00
|
|
|
}
|
|
|
|
|
2018-10-02 18:02:12 +02:00
|
|
|
func TestBuildCommand(t *testing.T) {
|
|
|
|
image := "goreleaser/test_build_flag"
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
flags []string
|
|
|
|
expect []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no flags",
|
|
|
|
flags: []string{},
|
|
|
|
expect: []string{"build", "-t", image, "."},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "single flag",
|
|
|
|
flags: []string{"--label=foo"},
|
|
|
|
expect: []string{"build", "-t", image, ".", "--label=foo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple flags",
|
|
|
|
flags: []string{"--label=foo", "--build-arg=bar=baz"},
|
|
|
|
expect: []string{"build", "-t", image, ".", "--label=foo", "--build-arg=bar=baz"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
command := buildCommand(image, tt.flags)
|
|
|
|
assert.Equal(t, tt.expect, command)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 05:29:12 +02:00
|
|
|
func TestDescription(t *testing.T) {
|
2017-12-02 23:53:19 +02:00
|
|
|
assert.NotEmpty(t, Pipe{}.String())
|
2017-09-12 05:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoDockers(t *testing.T) {
|
2018-09-12 19:18:01 +02:00
|
|
|
assert.True(t, pipe.IsSkip(Pipe{}.Run(context.New(config.Project{}))))
|
2017-09-12 05:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoDockerWithoutImageName(t *testing.T) {
|
2018-09-12 19:18:01 +02:00
|
|
|
assert.True(t, pipe.IsSkip(Pipe{}.Run(context.New(config.Project{
|
2017-09-12 05:29:12 +02:00
|
|
|
Dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Goos: "linux",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}))))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerNotInPath(t *testing.T) {
|
|
|
|
var path = os.Getenv("PATH")
|
|
|
|
defer func() {
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, os.Setenv("PATH", path))
|
2017-09-12 05:29:12 +02:00
|
|
|
}()
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, os.Setenv("PATH", ""))
|
2017-09-12 05:29:12 +02:00
|
|
|
var ctx = &context.Context{
|
|
|
|
Version: "1.0.0",
|
|
|
|
Config: config.Project{
|
|
|
|
Dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Image: "a/b",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.EqualError(t, Pipe{}.Run(ctx), ErrNoDocker.Error())
|
2017-09-12 05:29:12 +02:00
|
|
|
}
|
2017-12-03 04:42:52 +02:00
|
|
|
|
|
|
|
func TestDefault(t *testing.T) {
|
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{
|
|
|
|
Builds: []config.Build{
|
|
|
|
{
|
|
|
|
Binary: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Dockers: []config.Docker{
|
2018-08-15 15:49:28 +02:00
|
|
|
{},
|
2017-12-03 04:42:52 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
assert.Len(t, ctx.Config.Dockers, 1)
|
|
|
|
var docker = ctx.Config.Dockers[0]
|
|
|
|
assert.Equal(t, "linux", docker.Goos)
|
|
|
|
assert.Equal(t, "amd64", docker.Goarch)
|
|
|
|
assert.Equal(t, ctx.Config.Builds[0].Binary, docker.Binary)
|
|
|
|
assert.Equal(t, "Dockerfile", docker.Dockerfile)
|
2018-08-15 15:49:28 +02:00
|
|
|
assert.Equal(t, []string{"{{ .Version }}"}, docker.TagTemplates)
|
2018-01-18 21:40:44 +02:00
|
|
|
|
2017-12-03 04:42:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultNoDockers(t *testing.T) {
|
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{
|
|
|
|
Dockers: []config.Docker{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
assert.Empty(t, ctx.Config.Dockers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultSet(t *testing.T) {
|
|
|
|
var ctx = &context.Context{
|
|
|
|
Config: config.Project{
|
|
|
|
Dockers: []config.Docker{
|
|
|
|
{
|
|
|
|
Goos: "windows",
|
|
|
|
Goarch: "i386",
|
|
|
|
Binary: "bar",
|
|
|
|
Dockerfile: "Dockerfile.foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.NoError(t, Pipe{}.Default(ctx))
|
|
|
|
assert.Len(t, ctx.Config.Dockers, 1)
|
|
|
|
var docker = ctx.Config.Dockers[0]
|
|
|
|
assert.Equal(t, "windows", docker.Goos)
|
|
|
|
assert.Equal(t, "i386", docker.Goarch)
|
|
|
|
assert.Equal(t, "bar", docker.Binary)
|
2018-01-18 21:40:44 +02:00
|
|
|
assert.Equal(t, []string{"{{ .Version }}"}, docker.TagTemplates)
|
2017-12-03 04:42:52 +02:00
|
|
|
assert.Equal(t, "Dockerfile.foo", docker.Dockerfile)
|
|
|
|
}
|
2018-10-02 18:02:12 +02:00
|
|
|
|
2017-12-20 23:08:35 +02:00
|
|
|
func TestLinkFile(t *testing.T) {
|
|
|
|
const srcFile = "/tmp/test"
|
|
|
|
const dstFile = "/tmp/linked"
|
|
|
|
err := ioutil.WriteFile(srcFile, []byte("foo"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Cannot setup test file")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
err = link(srcFile, dstFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Failed to link: ", err)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if inode(srcFile) != inode(dstFile) {
|
|
|
|
t.Log("Inodes do not match, destination file is not a link")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
// cleanup
|
|
|
|
os.Remove(srcFile)
|
|
|
|
os.Remove(dstFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLinkDirectory(t *testing.T) {
|
|
|
|
const srcDir = "/tmp/testdir"
|
|
|
|
const testFile = "test"
|
|
|
|
const dstDir = "/tmp/linkedDir"
|
|
|
|
|
|
|
|
os.Mkdir(srcDir, 0755)
|
|
|
|
err := ioutil.WriteFile(srcDir+"/"+testFile, []byte("foo"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Cannot setup test file")
|
|
|
|
t.Fail()
|
|
|
|
}
|
2017-12-24 14:14:55 +02:00
|
|
|
err = link(srcDir, dstDir)
|
2017-12-20 23:08:35 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Log("Failed to link: ", err)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if inode(srcDir+"/"+testFile) != inode(dstDir+"/"+testFile) {
|
|
|
|
t.Log("Inodes do not match, destination file is not a link")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
os.RemoveAll(srcDir)
|
|
|
|
os.RemoveAll(dstDir)
|
|
|
|
}
|
|
|
|
|
2017-12-21 13:11:42 +02:00
|
|
|
func TestLinkTwoLevelDirectory(t *testing.T) {
|
|
|
|
const srcDir = "/tmp/testdir"
|
2017-12-26 03:53:44 +02:00
|
|
|
const srcLevel2 = srcDir + "/level2"
|
2017-12-21 13:11:42 +02:00
|
|
|
const testFile = "test"
|
|
|
|
const dstDir = "/tmp/linkedDir"
|
|
|
|
|
|
|
|
os.Mkdir(srcDir, 0755)
|
|
|
|
os.Mkdir(srcLevel2, 0755)
|
|
|
|
err := ioutil.WriteFile(srcDir+"/"+testFile, []byte("foo"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Cannot setup test file")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(srcLevel2+"/"+testFile, []byte("foo"), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Cannot setup test file")
|
|
|
|
t.Fail()
|
|
|
|
}
|
2017-12-24 14:14:55 +02:00
|
|
|
err = link(srcDir, dstDir)
|
2017-12-21 13:11:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Log("Failed to link: ", err)
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if inode(srcDir+"/"+testFile) != inode(dstDir+"/"+testFile) {
|
|
|
|
t.Log("Inodes do not match")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if inode(srcLevel2+"/"+testFile) != inode(dstDir+"/level2/"+testFile) {
|
|
|
|
t.Log("Inodes do not match")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
// cleanup
|
|
|
|
os.RemoveAll(srcDir)
|
|
|
|
os.RemoveAll(dstDir)
|
|
|
|
}
|
|
|
|
|
2017-12-20 23:08:35 +02:00
|
|
|
func inode(file string) uint64 {
|
|
|
|
fileInfo, err := os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
stat := fileInfo.Sys().(*syscall.Stat_t)
|
|
|
|
return stat.Ino
|
|
|
|
}
|