github.com/sergiusens/goreleaser@v0.34.3-0.20171009111917-ae6f7c157c5c/pipeline/docker/docker_test.go (about) 1 package docker 2 3 import ( 4 "io/ioutil" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "testing" 9 10 "github.com/apex/log" 11 12 "github.com/goreleaser/goreleaser/config" 13 "github.com/goreleaser/goreleaser/context" 14 "github.com/goreleaser/goreleaser/pipeline" 15 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func killAndRm() { 20 log.Info("killing registry") 21 _ = exec.Command("docker", "kill", "registry").Run() 22 _ = exec.Command("docker", "rm", "registry").Run() 23 } 24 25 func TestMain(m *testing.M) { 26 killAndRm() 27 if err := exec.Command( 28 "docker", "run", "-d", "-p", "5000:5000", "--name", "registry", "registry:2", 29 ).Run(); err != nil { 30 log.WithError(err).Fatal("failed to start docker registry") 31 } 32 defer killAndRm() 33 os.Exit(m.Run()) 34 } 35 36 func TestRunPipe(t *testing.T) { 37 folder, err := ioutil.TempDir("", "archivetest") 38 assert.NoError(t, err) 39 var dist = filepath.Join(folder, "dist") 40 assert.NoError(t, os.Mkdir(dist, 0755)) 41 assert.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755)) 42 var binPath = filepath.Join(dist, "mybin", "mybin") 43 _, err = os.Create(binPath) 44 assert.NoError(t, err) 45 var images = []string{ 46 "localhost:5000/goreleaser/test_run_pipe:1.0.0", 47 "localhost:5000/goreleaser/test_run_pipe:latest", 48 } 49 // this might fail as the image doesnt exist yet, so lets ignore the error 50 for _, img := range images { 51 _ = exec.Command("docker", "rmi", img).Run() 52 } 53 var ctx = &context.Context{ 54 Version: "1.0.0", 55 Publish: true, 56 Config: config.Project{ 57 ProjectName: "mybin", 58 Dist: dist, 59 Dockers: []config.Docker{ 60 { 61 Image: "localhost:5000/goreleaser/test_run_pipe", 62 Goos: "linux", 63 Goarch: "amd64", 64 Dockerfile: "testdata/Dockerfile", 65 Binary: "mybin", 66 Latest: true, 67 }, 68 { 69 Image: "localhost:5000/goreleaser/test_run_pipe_nope", 70 Goos: "linux", 71 Goarch: "amd64", 72 Dockerfile: "testdata/Dockerfile", 73 Binary: "otherbin", 74 }, 75 }, 76 }, 77 } 78 for _, plat := range []string{"linuxamd64", "linux386", "darwinamd64"} { 79 ctx.AddBinary(plat, "mybin", "mybin", binPath) 80 } 81 assert.NoError(t, Pipe{}.Run(ctx)) 82 83 // this might should not fail as the image should have been created when 84 // the step ran 85 for _, img := range images { 86 assert.NoError(t, exec.Command("docker", "rmi", img).Run()) 87 } 88 89 // the test_run_pipe_nope image should not have been created, so deleting 90 // it should fail 91 assert.Error(t, 92 exec.Command( 93 "docker", "rmi", "localhost:5000/goreleaser/test_run_pipe_nope:1.0.0", 94 ).Run(), 95 ) 96 } 97 98 func TestDescription(t *testing.T) { 99 assert.NotEmpty(t, Pipe{}.Description()) 100 } 101 102 func TestNoDockers(t *testing.T) { 103 assert.True(t, pipeline.IsSkip(Pipe{}.Run(context.New(config.Project{})))) 104 } 105 106 func TestNoDockerWithoutImageName(t *testing.T) { 107 assert.True(t, pipeline.IsSkip(Pipe{}.Run(context.New(config.Project{ 108 Dockers: []config.Docker{ 109 { 110 Goos: "linux", 111 }, 112 }, 113 })))) 114 } 115 116 func TestDockerNotInPath(t *testing.T) { 117 var path = os.Getenv("PATH") 118 defer func() { 119 assert.NoError(t, os.Setenv("PATH", path)) 120 }() 121 assert.NoError(t, os.Setenv("PATH", "")) 122 var ctx = &context.Context{ 123 Version: "1.0.0", 124 Config: config.Project{ 125 Dockers: []config.Docker{ 126 { 127 Image: "a/b", 128 }, 129 }, 130 }, 131 } 132 assert.EqualError(t, Pipe{}.Run(ctx), ErrNoDocker.Error()) 133 }