2017-09-12 05:29:12 +02:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
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"
|
2017-09-12 05:29:12 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/goreleaser/goreleaser/config"
|
|
|
|
"github.com/goreleaser/goreleaser/context"
|
2017-12-18 01:00:50 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/artifact"
|
2017-09-12 05:29:12 +02:00
|
|
|
"github.com/goreleaser/goreleaser/pipeline"
|
2017-09-13 01:58:02 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-12-20 23:08:35 +02:00
|
|
|
"syscall"
|
2017-09-12 05:29:12 +02:00
|
|
|
)
|
|
|
|
|
2017-12-18 01:00:50 +02:00
|
|
|
func killAndRm(t *testing.T) {
|
|
|
|
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) {
|
|
|
|
folder, err := ioutil.TempDir("", "archivetest")
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, err)
|
2017-09-13 01:58:02 +02:00
|
|
|
var dist = filepath.Join(folder, "dist")
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, os.Mkdir(dist, 0755))
|
|
|
|
assert.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755))
|
2017-09-13 01:58:02 +02:00
|
|
|
var binPath = filepath.Join(dist, "mybin", "mybin")
|
|
|
|
_, err = os.Create(binPath)
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, err)
|
2017-12-05 15:19:44 +02:00
|
|
|
|
|
|
|
var table = map[string]struct {
|
|
|
|
docker config.Docker
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
"valid": {
|
|
|
|
docker: config.Docker{
|
|
|
|
Image: "localhost:5000/goreleaser/test_run_pipe",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "mybin",
|
|
|
|
Latest: true,
|
2017-12-06 01:06:37 +02:00
|
|
|
TagTemplate: "{{.Tag}}-{{.Env.FOO}}",
|
2017-12-05 15:19:44 +02:00
|
|
|
},
|
|
|
|
err: "",
|
|
|
|
},
|
|
|
|
"invalid": {
|
|
|
|
docker: config.Docker{
|
|
|
|
Image: "localhost:5000/goreleaser/test_run_pipe_nope",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "otherbin",
|
|
|
|
TagTemplate: "{{.Version}}",
|
|
|
|
},
|
|
|
|
err: "",
|
|
|
|
},
|
|
|
|
"template_error": {
|
|
|
|
docker: config.Docker{
|
|
|
|
Image: "localhost:5000/goreleaser/test_run_pipe_template_error",
|
|
|
|
Goos: "linux",
|
|
|
|
Goarch: "amd64",
|
|
|
|
Dockerfile: "testdata/Dockerfile",
|
|
|
|
Binary: "mybin",
|
|
|
|
Latest: true,
|
|
|
|
TagTemplate: "{{.Tag}",
|
|
|
|
},
|
2017-12-05 15:35:30 +02:00
|
|
|
err: `template: tag:1: unexpected "}" in operand`,
|
2017-12-05 15:19:44 +02:00
|
|
|
},
|
|
|
|
}
|
2017-09-15 01:16:49 +02:00
|
|
|
var images = []string{
|
2017-12-06 01:06:37 +02:00
|
|
|
"localhost:5000/goreleaser/test_run_pipe:v1.0.0-123",
|
2017-10-07 22:32:17 +02:00
|
|
|
"localhost:5000/goreleaser/test_run_pipe:latest",
|
2017-09-15 01:16:49 +02:00
|
|
|
}
|
2017-09-13 01:58:02 +02:00
|
|
|
// this might fail as the image doesnt exist yet, so lets ignore the error
|
2017-09-15 01:16:49 +02:00
|
|
|
for _, img := range images {
|
|
|
|
_ = exec.Command("docker", "rmi", img).Run()
|
|
|
|
}
|
2017-12-05 15:19:44 +02:00
|
|
|
|
2017-12-18 01:00:50 +02:00
|
|
|
killAndRm(t)
|
|
|
|
if err := exec.Command(
|
|
|
|
"docker", "run", "-d", "-p", "5000:5000", "--name", "registry", "registry:2",
|
|
|
|
).Run(); err != nil {
|
|
|
|
t.Log("failed to start docker registry", err)
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
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-05 15:19:44 +02:00
|
|
|
var ctx = &context.Context{
|
2017-12-18 01:00:50 +02:00
|
|
|
Version: "1.0.0",
|
|
|
|
Publish: true,
|
|
|
|
Artifacts: artifact.New(),
|
2017-12-05 15:19:44 +02:00
|
|
|
Git: context.GitInfo{
|
|
|
|
CurrentTag: "v1.0.0",
|
2017-09-13 01:58:02 +02:00
|
|
|
},
|
2017-12-05 15:19:44 +02:00
|
|
|
Config: config.Project{
|
|
|
|
ProjectName: "mybin",
|
|
|
|
Dist: dist,
|
|
|
|
Dockers: []config.Docker{
|
|
|
|
docker.docker,
|
|
|
|
},
|
2017-09-13 01:58:02 +02:00
|
|
|
},
|
2017-12-06 01:06:37 +02:00
|
|
|
Env: map[string]string{"FOO": "123"},
|
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-05 15:35:30 +02:00
|
|
|
if docker.err == "" {
|
2017-12-18 01:00:50 +02:00
|
|
|
assert.NoError(tt, Pipe{}.Run(ctx))
|
2017-12-05 15:35:30 +02:00
|
|
|
} else {
|
2017-12-18 01:00:50 +02:00
|
|
|
assert.EqualError(tt, Pipe{}.Run(ctx), docker.err)
|
2017-12-05 15:19:44 +02:00
|
|
|
}
|
|
|
|
})
|
2017-09-13 01:58:02 +02:00
|
|
|
}
|
2017-09-15 01:16:49 +02:00
|
|
|
|
2017-09-13 01:58:02 +02:00
|
|
|
// this might should not fail as the image should have been created when
|
|
|
|
// the step ran
|
2017-09-15 01:16:49 +02:00
|
|
|
for _, img := range images {
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.NoError(t, exec.Command("docker", "rmi", img).Run())
|
2017-09-15 01:16:49 +02:00
|
|
|
}
|
2017-09-13 01:58:02 +02:00
|
|
|
// the test_run_pipe_nope image should not have been created, so deleting
|
|
|
|
// it should fail
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.Error(t,
|
2017-10-07 22:32:17 +02:00
|
|
|
exec.Command(
|
|
|
|
"docker", "rmi", "localhost:5000/goreleaser/test_run_pipe_nope:1.0.0",
|
|
|
|
).Run(),
|
2017-09-13 01:58:02 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.True(t, pipeline.IsSkip(Pipe{}.Run(context.New(config.Project{}))))
|
2017-09-12 05:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoDockerWithoutImageName(t *testing.T) {
|
2017-09-27 00:24:49 +02:00
|
|
|
assert.True(t, pipeline.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{
|
|
|
|
{
|
|
|
|
Latest: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
2017-12-05 15:19:44 +02:00
|
|
|
assert.Equal(t, "{{ .Version }}", docker.TagTemplate)
|
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)
|
2017-12-05 15:19:44 +02:00
|
|
|
assert.Equal(t, "{{ .Version }}", docker.TagTemplate)
|
2017-12-03 04:42:52 +02:00
|
|
|
assert.Equal(t, "Dockerfile.foo", docker.Dockerfile)
|
|
|
|
}
|
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"
|
|
|
|
const srcLevel2 = srcDir+"/level2"
|
|
|
|
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
|
|
|
|
}
|