github.com/triarius/goreleaser@v1.12.5/internal/pipe/docker/docker_test.go (about)

     1  package docker
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/caarlos0/log"
    13  	"github.com/triarius/goreleaser/internal/artifact"
    14  	"github.com/triarius/goreleaser/internal/pipe"
    15  	"github.com/triarius/goreleaser/internal/testlib"
    16  	"github.com/triarius/goreleaser/pkg/config"
    17  	"github.com/triarius/goreleaser/pkg/context"
    18  	"github.com/stretchr/testify/require"
    19  )
    20  
    21  var (
    22  	it              = flag.Bool("it", false, "push images to docker hub")
    23  	debug           = flag.Bool("debug", false, "enable debug logs")
    24  	registryPort    = "5050"
    25  	registry        = fmt.Sprintf("localhost:%s/", registryPort)
    26  	altRegistryPort = "5051"
    27  	altRegistry     = fmt.Sprintf("localhost:%s/", altRegistryPort)
    28  )
    29  
    30  func TestMain(m *testing.M) {
    31  	flag.Parse()
    32  	if *it {
    33  		registry = "docker.io/"
    34  	}
    35  	if *debug {
    36  		log.SetLevel(log.DebugLevel)
    37  	}
    38  	os.Exit(m.Run())
    39  }
    40  
    41  func start(t *testing.T) {
    42  	t.Helper()
    43  	if *it {
    44  		return
    45  	}
    46  	t.Log("starting registries")
    47  	for _, line := range []string{
    48  		fmt.Sprintf("run -d -p %s:5000 --name registry registry:2", registryPort),
    49  		fmt.Sprintf("run -d -p %s:5000 --name alt_registry registry:2", altRegistryPort),
    50  	} {
    51  		if out, err := exec.Command("docker", strings.Fields(line)...).CombinedOutput(); err != nil {
    52  			t.Log("failed to start docker registry", string(out), err)
    53  			t.FailNow()
    54  		}
    55  	}
    56  }
    57  
    58  func killAndRm(t *testing.T) {
    59  	t.Helper()
    60  	if *it {
    61  		return
    62  	}
    63  	t.Log("killing registries")
    64  	for _, registry := range []string{"registry", "alt_registry"} {
    65  		_ = exec.Command("docker", "rm", "--force", registry).Run()
    66  	}
    67  }
    68  
    69  // TODO: this test is too big... split in smaller tests? Mainly the manifest ones...
    70  func TestRunPipe(t *testing.T) {
    71  	testlib.CheckPath(t, "docker")
    72  	type errChecker func(*testing.T, error)
    73  	shouldErr := func(msg string) errChecker {
    74  		return func(t *testing.T, err error) {
    75  			t.Helper()
    76  			require.Error(t, err)
    77  			require.Contains(t, err.Error(), msg)
    78  		}
    79  	}
    80  	shouldNotErr := func(t *testing.T, err error) {
    81  		t.Helper()
    82  		require.NoError(t, err)
    83  	}
    84  	shouldTemplateErr := func(t *testing.T, err error) {
    85  		t.Helper()
    86  		testlib.RequireTemplateError(t, err)
    87  	}
    88  	type imageLabelFinder func(*testing.T, string)
    89  	shouldFindImagesWithLabels := func(image string, filters ...string) func(*testing.T, string) {
    90  		return func(t *testing.T, use string) {
    91  			t.Helper()
    92  			for _, filter := range filters {
    93  				cmd := exec.Command("docker", "images", "-q", "--filter", "reference=*/"+image, "--filter", filter)
    94  				// t.Log("running", cmd)
    95  				output, err := cmd.CombinedOutput()
    96  				require.NoError(t, err, string(output))
    97  				uniqueIDs := map[string]string{}
    98  				for _, id := range strings.Split(strings.TrimSpace(string(output)), "\n") {
    99  					uniqueIDs[id] = id
   100  				}
   101  				require.Equal(t, 1, len(uniqueIDs))
   102  			}
   103  		}
   104  	}
   105  	noLabels := func(t *testing.T, _ string) {
   106  		t.Helper()
   107  	}
   108  
   109  	table := map[string]struct {
   110  		dockers             []config.Docker
   111  		manifests           []config.DockerManifest
   112  		env                 map[string]string
   113  		expect              []string
   114  		assertImageLabels   imageLabelFinder
   115  		assertError         errChecker
   116  		pubAssertError      errChecker
   117  		manifestAssertError errChecker
   118  		extraPrepare        func(t *testing.T, ctx *context.Context)
   119  	}{
   120  		"multiarch": {
   121  			dockers: []config.Docker{
   122  				{
   123  					ImageTemplates:     []string{registry + "goreleaser/test_multiarch:test-amd64"},
   124  					Goos:               "linux",
   125  					Goarch:             "amd64",
   126  					Dockerfile:         "testdata/Dockerfile.arch",
   127  					BuildFlagTemplates: []string{"--build-arg", "ARCH=amd64"},
   128  				},
   129  				{
   130  					ImageTemplates:     []string{registry + "goreleaser/test_multiarch:test-arm64v8"},
   131  					Goos:               "linux",
   132  					Goarch:             "arm64",
   133  					Dockerfile:         "testdata/Dockerfile.arch",
   134  					BuildFlagTemplates: []string{"--build-arg", "ARCH=arm64v8"},
   135  				},
   136  			},
   137  			manifests: []config.DockerManifest{
   138  				{
   139  					NameTemplate: registry + "goreleaser/test_multiarch:test",
   140  					ImageTemplates: []string{
   141  						registry + "goreleaser/test_multiarch:test-amd64",
   142  						registry + "goreleaser/test_multiarch:test-arm64v8",
   143  					},
   144  				},
   145  			},
   146  			expect: []string{
   147  				registry + "goreleaser/test_multiarch:test-amd64",
   148  				registry + "goreleaser/test_multiarch:test-arm64v8",
   149  			},
   150  			assertError:         shouldNotErr,
   151  			pubAssertError:      shouldNotErr,
   152  			manifestAssertError: shouldNotErr,
   153  			assertImageLabels:   noLabels,
   154  		},
   155  		"manifest autoskip no prerelease": {
   156  			dockers: []config.Docker{
   157  				{
   158  					ImageTemplates: []string{registry + "goreleaser/test_manifestskip:test-amd64"},
   159  					Goos:           "linux",
   160  					Goarch:         "amd64",
   161  					Dockerfile:     "testdata/Dockerfile",
   162  				},
   163  			},
   164  			manifests: []config.DockerManifest{
   165  				{
   166  					NameTemplate: registry + "goreleaser/test_manifestskip:test",
   167  					ImageTemplates: []string{
   168  						registry + "goreleaser/test_manifestskip:test-amd64",
   169  					},
   170  					SkipPush: "auto",
   171  				},
   172  			},
   173  			expect: []string{
   174  				registry + "goreleaser/test_manifestskip:test-amd64",
   175  			},
   176  			assertError:         shouldNotErr,
   177  			pubAssertError:      shouldNotErr,
   178  			manifestAssertError: shouldNotErr,
   179  			assertImageLabels:   noLabels,
   180  		},
   181  		"manifest autoskip prerelease": {
   182  			dockers: []config.Docker{
   183  				{
   184  					ImageTemplates: []string{registry + "goreleaser/test_manifestskip-prerelease:test-amd64"},
   185  					Goos:           "linux",
   186  					Goarch:         "amd64",
   187  					Dockerfile:     "testdata/Dockerfile",
   188  				},
   189  			},
   190  			manifests: []config.DockerManifest{
   191  				{
   192  					NameTemplate: registry + "goreleaser/test_manifestskip-prerelease:test",
   193  					ImageTemplates: []string{
   194  						registry + "goreleaser/test_manifestskip-prerelease:test-amd64",
   195  					},
   196  					SkipPush: "auto",
   197  				},
   198  			},
   199  			expect: []string{
   200  				registry + "goreleaser/test_manifestskip-prerelease:test-amd64",
   201  			},
   202  			assertError:         shouldNotErr,
   203  			pubAssertError:      shouldNotErr,
   204  			manifestAssertError: testlib.AssertSkipped,
   205  			assertImageLabels:   noLabels,
   206  			extraPrepare: func(t *testing.T, ctx *context.Context) {
   207  				t.Helper()
   208  				ctx.Semver.Prerelease = "beta"
   209  			},
   210  		},
   211  		"manifest skip": {
   212  			dockers: []config.Docker{
   213  				{
   214  					ImageTemplates: []string{registry + "goreleaser/test_manifestskip-true:test-amd64"},
   215  					Goos:           "linux",
   216  					Goarch:         "amd64",
   217  					Dockerfile:     "testdata/Dockerfile",
   218  				},
   219  			},
   220  			manifests: []config.DockerManifest{
   221  				{
   222  					NameTemplate: registry + "goreleaser/test_manifestskip-true:test",
   223  					ImageTemplates: []string{
   224  						registry + "goreleaser/test_manifestskip-true:test-amd64",
   225  					},
   226  					SkipPush: "true",
   227  				},
   228  			},
   229  			expect: []string{
   230  				registry + "goreleaser/test_manifestskip-true:test-amd64",
   231  			},
   232  			assertError:         shouldNotErr,
   233  			pubAssertError:      shouldNotErr,
   234  			manifestAssertError: testlib.AssertSkipped,
   235  			assertImageLabels:   noLabels,
   236  		},
   237  		"multiarch with previous existing manifest": {
   238  			dockers: []config.Docker{
   239  				{
   240  					ImageTemplates:     []string{registry + "goreleaser/test_multiarch:2test-amd64"},
   241  					Goos:               "linux",
   242  					Goarch:             "amd64",
   243  					Dockerfile:         "testdata/Dockerfile.arch",
   244  					BuildFlagTemplates: []string{"--build-arg", "ARCH=amd64"},
   245  				},
   246  				{
   247  					ImageTemplates:     []string{registry + "goreleaser/test_multiarch:2test-arm64v8"},
   248  					Goos:               "linux",
   249  					Goarch:             "arm64",
   250  					Dockerfile:         "testdata/Dockerfile.arch",
   251  					BuildFlagTemplates: []string{"--build-arg", "ARCH=arm64v8"},
   252  				},
   253  			},
   254  			manifests: []config.DockerManifest{
   255  				{
   256  					NameTemplate: registry + "goreleaser/test_multiarch:2test",
   257  					ImageTemplates: []string{
   258  						registry + "goreleaser/test_multiarch:2test-amd64",
   259  						registry + "goreleaser/test_multiarch:2test-arm64v8",
   260  					},
   261  				},
   262  			},
   263  			expect: []string{
   264  				registry + "goreleaser/test_multiarch:2test-amd64",
   265  				registry + "goreleaser/test_multiarch:2test-arm64v8",
   266  			},
   267  			assertError:         shouldNotErr,
   268  			pubAssertError:      shouldNotErr,
   269  			manifestAssertError: shouldNotErr,
   270  			assertImageLabels:   noLabels,
   271  			extraPrepare: func(t *testing.T, ctx *context.Context) {
   272  				t.Helper()
   273  				_ = exec.Command(fmt.Sprintf("docker manifest rm %sgoreleaser/test_multiarch:2test ", registry)).Run()
   274  				for _, cmd := range []string{
   275  					fmt.Sprintf("docker build -t %sgoreleaser/dummy:v1 --platform linux/amd64 -f testdata/Dockerfile.dummy .", registry),
   276  					fmt.Sprintf("docker push %sgoreleaser/dummy:v1", registry),
   277  					fmt.Sprintf("docker manifest create %sgoreleaser/test_multiarch:2test --amend %sgoreleaser/dummy:v1 --insecure", registry, registry),
   278  				} {
   279  					// t.Log("running", cmd)
   280  					parts := strings.Fields(cmd)
   281  					out, err := exec.CommandContext(ctx, parts[0], parts[1:]...).CombinedOutput()
   282  					require.NoError(t, err, cmd+": "+string(out))
   283  				}
   284  			},
   285  		},
   286  		"multiarch image not found": {
   287  			dockers: []config.Docker{
   288  				{
   289  					ImageTemplates:     []string{registry + "goreleaser/test_multiarch_fail:latest-arm64v8"},
   290  					Goos:               "linux",
   291  					Goarch:             "arm64",
   292  					Dockerfile:         "testdata/Dockerfile.arch",
   293  					BuildFlagTemplates: []string{"--build-arg", "ARCH=arm64v8"},
   294  				},
   295  			},
   296  			manifests: []config.DockerManifest{
   297  				{
   298  					NameTemplate:   registry + "goreleaser/test_multiarch_fail:test",
   299  					ImageTemplates: []string{registry + "goreleaser/test_multiarch_fail:latest-amd64"},
   300  				},
   301  			},
   302  			expect:              []string{registry + "goreleaser/test_multiarch_fail:latest-arm64v8"},
   303  			assertError:         shouldNotErr,
   304  			pubAssertError:      shouldNotErr,
   305  			manifestAssertError: shouldErr("failed to create localhost:5050/goreleaser/test_multiarch_fail:test"),
   306  			assertImageLabels:   noLabels,
   307  		},
   308  		"multiarch manifest template error": {
   309  			dockers: []config.Docker{
   310  				{
   311  					ImageTemplates: []string{registry + "goreleaser/test_multiarch_manifest_tmpl_error"},
   312  					Goos:           "linux",
   313  					Goarch:         "arm64",
   314  					Dockerfile:     "testdata/Dockerfile",
   315  				},
   316  			},
   317  			manifests: []config.DockerManifest{
   318  				{
   319  					NameTemplate:   registry + "goreleaser/test_multiarch_manifest_tmpl_error:{{ .Goos }",
   320  					ImageTemplates: []string{registry + "goreleaser/test_multiarch_manifest_tmpl_error"},
   321  				},
   322  			},
   323  			expect:              []string{registry + "goreleaser/test_multiarch_manifest_tmpl_error"},
   324  			assertError:         shouldNotErr,
   325  			pubAssertError:      shouldNotErr,
   326  			manifestAssertError: shouldTemplateErr,
   327  			assertImageLabels:   noLabels,
   328  		},
   329  		"multiarch image template error": {
   330  			dockers: []config.Docker{
   331  				{
   332  					ImageTemplates: []string{registry + "goreleaser/test_multiarch_img_tmpl_error"},
   333  					Goos:           "linux",
   334  					Goarch:         "arm64",
   335  					Dockerfile:     "testdata/Dockerfile",
   336  				},
   337  			},
   338  			manifests: []config.DockerManifest{
   339  				{
   340  					NameTemplate:   registry + "goreleaser/test_multiarch_img_tmpl_error",
   341  					ImageTemplates: []string{registry + "goreleaser/test_multiarch_img_tmpl_error:{{ .Goos }"},
   342  				},
   343  			},
   344  			expect:              []string{registry + "goreleaser/test_multiarch_img_tmpl_error"},
   345  			assertError:         shouldNotErr,
   346  			pubAssertError:      shouldNotErr,
   347  			manifestAssertError: shouldTemplateErr,
   348  			assertImageLabels:   noLabels,
   349  		},
   350  		"multiarch missing manifest name": {
   351  			dockers: []config.Docker{
   352  				{
   353  					ImageTemplates: []string{registry + "goreleaser/test_multiarch_no_manifest_name"},
   354  					Goos:           "linux",
   355  					Goarch:         "arm64",
   356  					Dockerfile:     "testdata/Dockerfile",
   357  				},
   358  			},
   359  			manifests: []config.DockerManifest{
   360  				{
   361  					NameTemplate:   "  ",
   362  					ImageTemplates: []string{registry + "goreleaser/test_multiarch_no_manifest_name"},
   363  				},
   364  			},
   365  			expect:              []string{registry + "goreleaser/test_multiarch_no_manifest_name"},
   366  			assertError:         shouldNotErr,
   367  			pubAssertError:      shouldNotErr,
   368  			manifestAssertError: testlib.AssertSkipped,
   369  			assertImageLabels:   noLabels,
   370  		},
   371  		"multiarch missing images": {
   372  			dockers: []config.Docker{
   373  				{
   374  					ImageTemplates: []string{registry + "goreleaser/test_multiarch_no_manifest_images"},
   375  					Dockerfile:     "testdata/Dockerfile",
   376  					Goos:           "linux",
   377  					Goarch:         "arm64",
   378  				},
   379  			},
   380  			manifests: []config.DockerManifest{
   381  				{
   382  					NameTemplate:   "ignored",
   383  					ImageTemplates: []string{" ", "   ", ""},
   384  				},
   385  			},
   386  			expect:              []string{registry + "goreleaser/test_multiarch_no_manifest_images"},
   387  			assertError:         shouldNotErr,
   388  			pubAssertError:      shouldNotErr,
   389  			manifestAssertError: testlib.AssertSkipped,
   390  			assertImageLabels:   noLabels,
   391  		},
   392  		"valid": {
   393  			env: map[string]string{
   394  				"FOO": "123",
   395  			},
   396  			dockers: []config.Docker{
   397  				{
   398  					ImageTemplates: []string{
   399  						registry + "goreleaser/test_run_pipe:{{.Tag}}-{{.Env.FOO}}",
   400  						registry + "goreleaser/test_run_pipe:v{{.Major}}",
   401  						registry + "goreleaser/test_run_pipe:v{{.Major}}.{{.Minor}}",
   402  						registry + "goreleaser/test_run_pipe:commit-{{.Commit}}",
   403  						registry + "goreleaser/test_run_pipe:latest",
   404  						altRegistry + "goreleaser/test_run_pipe:{{.Tag}}-{{.Env.FOO}}",
   405  						altRegistry + "goreleaser/test_run_pipe:v{{.Major}}",
   406  						altRegistry + "goreleaser/test_run_pipe:v{{.Major}}.{{.Minor}}",
   407  						altRegistry + "goreleaser/test_run_pipe:commit-{{.Commit}}",
   408  						altRegistry + "goreleaser/test_run_pipe:latest",
   409  					},
   410  					Goos:       "linux",
   411  					Goarch:     "amd64",
   412  					Dockerfile: "testdata/Dockerfile",
   413  					BuildFlagTemplates: []string{
   414  						"--label=org.label-schema.schema-version=1.0",
   415  						"--label=org.label-schema.version={{.Version}}",
   416  						"--label=org.label-schema.vcs-ref={{.Commit}}",
   417  						"--label=org.label-schema.name={{.ProjectName}}",
   418  						"--build-arg=FRED={{.Tag}}",
   419  					},
   420  					Files: []string{
   421  						"testdata/extra_file.txt",
   422  					},
   423  				},
   424  			},
   425  			expect: []string{
   426  				registry + "goreleaser/test_run_pipe:v1.0.0-123",
   427  				registry + "goreleaser/test_run_pipe:v1",
   428  				registry + "goreleaser/test_run_pipe:v1.0",
   429  				registry + "goreleaser/test_run_pipe:commit-a1b2c3d4",
   430  				registry + "goreleaser/test_run_pipe:latest",
   431  				altRegistry + "goreleaser/test_run_pipe:v1.0.0-123",
   432  				altRegistry + "goreleaser/test_run_pipe:v1",
   433  				altRegistry + "goreleaser/test_run_pipe:v1.0",
   434  				altRegistry + "goreleaser/test_run_pipe:commit-a1b2c3d4",
   435  				altRegistry + "goreleaser/test_run_pipe:latest",
   436  			},
   437  			assertImageLabels: shouldFindImagesWithLabels(
   438  				"goreleaser/test_run_pipe",
   439  				"label=org.label-schema.schema-version=1.0",
   440  				"label=org.label-schema.version=1.0.0",
   441  				"label=org.label-schema.vcs-ref=a1b2c3d4",
   442  				"label=org.label-schema.name=mybin",
   443  			),
   444  			assertError:         shouldNotErr,
   445  			pubAssertError:      shouldNotErr,
   446  			manifestAssertError: shouldNotErr,
   447  		},
   448  		"templated-dockerfile": {
   449  			env: map[string]string{
   450  				"Dockerfile": "testdata/Dockerfile",
   451  			},
   452  			dockers: []config.Docker{
   453  				{
   454  					ImageTemplates: []string{
   455  						registry + "goreleaser/templated_dockerfile:v1",
   456  					},
   457  					Goos:       "linux",
   458  					Goarch:     "amd64",
   459  					Dockerfile: "{{ .Env.Dockerfile }}",
   460  					Files: []string{
   461  						"testdata/extra_file.txt",
   462  					},
   463  				},
   464  			},
   465  			expect: []string{
   466  				registry + "goreleaser/templated_dockerfile:v1",
   467  			},
   468  			assertError:         shouldNotErr,
   469  			assertImageLabels:   noLabels,
   470  			pubAssertError:      shouldNotErr,
   471  			manifestAssertError: shouldNotErr,
   472  		},
   473  		"templated-dockerfile-invalid": {
   474  			dockers: []config.Docker{
   475  				{
   476  					ImageTemplates: []string{
   477  						registry + "goreleaser/invalid-templated-dockerfile:v1",
   478  					},
   479  					Goos:       "linux",
   480  					Goarch:     "amd64",
   481  					Dockerfile: "{{ .Env.Dockerfile }}",
   482  				},
   483  			},
   484  			expect:              []string{},
   485  			assertError:         shouldTemplateErr,
   486  			assertImageLabels:   noLabels,
   487  			pubAssertError:      shouldNotErr,
   488  			manifestAssertError: shouldNotErr,
   489  		},
   490  		"image template with env": {
   491  			env: map[string]string{
   492  				"FOO": "test_run_pipe_template",
   493  			},
   494  			dockers: []config.Docker{
   495  				{
   496  					ImageTemplates: []string{
   497  						registry + "goreleaser/{{.Env.FOO}}:{{.Tag}}",
   498  					},
   499  					Goos:       "linux",
   500  					Goarch:     "amd64",
   501  					Dockerfile: "testdata/Dockerfile",
   502  				},
   503  			},
   504  			expect: []string{
   505  				registry + "goreleaser/test_run_pipe_template:v1.0.0",
   506  			},
   507  			assertImageLabels:   noLabels,
   508  			assertError:         shouldNotErr,
   509  			pubAssertError:      shouldNotErr,
   510  			manifestAssertError: shouldNotErr,
   511  		},
   512  		"image template uppercase": {
   513  			env: map[string]string{
   514  				"FOO": "test_run_pipe_template_UPPERCASE",
   515  			},
   516  			dockers: []config.Docker{
   517  				{
   518  					ImageTemplates: []string{
   519  						registry + "goreleaser/{{.Env.FOO}}:{{.Tag}}",
   520  					},
   521  					Goos:       "linux",
   522  					Goarch:     "amd64",
   523  					Dockerfile: "testdata/Dockerfile",
   524  				},
   525  			},
   526  			expect:              []string{},
   527  			assertImageLabels:   noLabels,
   528  			assertError:         shouldErr(`failed to build localhost:5050/goreleaser/test_run_pipe_template_UPPERCASE:v1.0.0`),
   529  			pubAssertError:      shouldNotErr,
   530  			manifestAssertError: shouldNotErr,
   531  		},
   532  		"empty image tag": {
   533  			dockers: []config.Docker{
   534  				{
   535  					ImageTemplates: []string{
   536  						"",
   537  						registry + "goreleaser/empty_tag:latest",
   538  					},
   539  					Goos:       "linux",
   540  					Goarch:     "amd64",
   541  					Dockerfile: "testdata/Dockerfile",
   542  				},
   543  			},
   544  			expect: []string{
   545  				registry + "goreleaser/empty_tag:latest",
   546  			},
   547  			assertImageLabels:   noLabels,
   548  			assertError:         shouldNotErr,
   549  			pubAssertError:      shouldNotErr,
   550  			manifestAssertError: shouldNotErr,
   551  		},
   552  		"no image tags": {
   553  			dockers: []config.Docker{
   554  				{
   555  					ImageTemplates: []string{
   556  						"",
   557  					},
   558  					Goos:       "linux",
   559  					Goarch:     "amd64",
   560  					Dockerfile: "testdata/Dockerfile",
   561  				},
   562  			},
   563  			expect:              []string{},
   564  			assertImageLabels:   noLabels,
   565  			assertError:         shouldErr("no image templates found"),
   566  			pubAssertError:      shouldNotErr,
   567  			manifestAssertError: shouldNotErr,
   568  		},
   569  		"valid with ids": {
   570  			dockers: []config.Docker{
   571  				{
   572  					ImageTemplates: []string{
   573  						registry + "goreleaser/test_run_pipe_build:latest",
   574  					},
   575  					Goos:       "linux",
   576  					Goarch:     "amd64",
   577  					Dockerfile: "testdata/Dockerfile",
   578  					IDs:        []string{"mybin"},
   579  				},
   580  			},
   581  			expect: []string{
   582  				registry + "goreleaser/test_run_pipe_build:latest",
   583  			},
   584  			assertImageLabels:   noLabels,
   585  			assertError:         shouldNotErr,
   586  			pubAssertError:      shouldNotErr,
   587  			manifestAssertError: shouldNotErr,
   588  		},
   589  		"multiple images with same extra file": {
   590  			dockers: []config.Docker{
   591  				{
   592  					ImageTemplates: []string{
   593  						registry + "goreleaser/multiplefiles1:latest",
   594  					},
   595  					Goos:       "linux",
   596  					Goarch:     "amd64",
   597  					Dockerfile: "testdata/Dockerfile",
   598  					Files:      []string{"testdata/extra_file.txt"},
   599  				},
   600  				{
   601  					ImageTemplates: []string{
   602  						registry + "goreleaser/multiplefiles2:latest",
   603  					},
   604  					Goos:       "linux",
   605  					Goarch:     "amd64",
   606  					Dockerfile: "testdata/Dockerfile",
   607  					Files:      []string{"testdata/extra_file.txt"},
   608  				},
   609  			},
   610  			expect: []string{
   611  				registry + "goreleaser/multiplefiles1:latest",
   612  				registry + "goreleaser/multiplefiles2:latest",
   613  			},
   614  			assertImageLabels:   noLabels,
   615  			assertError:         shouldNotErr,
   616  			pubAssertError:      shouldNotErr,
   617  			manifestAssertError: shouldNotErr,
   618  		},
   619  		"multiple images with same dockerfile": {
   620  			dockers: []config.Docker{
   621  				{
   622  					ImageTemplates: []string{
   623  						registry + "goreleaser/test_run_pipe:latest",
   624  					},
   625  					Goos:       "linux",
   626  					Goarch:     "amd64",
   627  					Dockerfile: "testdata/Dockerfile",
   628  				},
   629  				{
   630  					ImageTemplates: []string{
   631  						registry + "goreleaser/test_run_pipe2:latest",
   632  					},
   633  					Goos:       "linux",
   634  					Goarch:     "amd64",
   635  					Dockerfile: "testdata/Dockerfile",
   636  				},
   637  			},
   638  			assertImageLabels: noLabels,
   639  			expect: []string{
   640  				registry + "goreleaser/test_run_pipe:latest",
   641  				registry + "goreleaser/test_run_pipe2:latest",
   642  			},
   643  			assertError:         shouldNotErr,
   644  			pubAssertError:      shouldNotErr,
   645  			manifestAssertError: shouldNotErr,
   646  		},
   647  		"valid_skip_push": {
   648  			dockers: []config.Docker{
   649  				{
   650  					ImageTemplates: []string{
   651  						registry + "goreleaser/test_run_pipe:latest",
   652  					},
   653  					Goos:       "linux",
   654  					Goarch:     "amd64",
   655  					Dockerfile: "testdata/Dockerfile",
   656  					SkipPush:   "true",
   657  				},
   658  			},
   659  			expect: []string{
   660  				registry + "goreleaser/test_run_pipe:latest",
   661  			},
   662  			assertImageLabels:   noLabels,
   663  			assertError:         shouldNotErr,
   664  			pubAssertError:      testlib.AssertSkipped,
   665  			manifestAssertError: shouldNotErr,
   666  		},
   667  		"one_img_error_with_skip_push": {
   668  			dockers: []config.Docker{
   669  				{
   670  					ImageTemplates: []string{
   671  						registry + "goreleaser/one_img_error_with_skip_push:true",
   672  					},
   673  					Goos:       "linux",
   674  					Goarch:     "amd64",
   675  					Dockerfile: "testdata/Dockerfile.true",
   676  					SkipPush:   "true",
   677  				},
   678  				{
   679  					ImageTemplates: []string{
   680  						registry + "goreleaser/one_img_error_with_skip_push:false",
   681  					},
   682  					Goos:       "linux",
   683  					Goarch:     "amd64",
   684  					Dockerfile: "testdata/Dockerfile.false",
   685  					SkipPush:   "true",
   686  				},
   687  			},
   688  			expect: []string{
   689  				registry + "goreleaser/one_img_error_with_skip_push:true",
   690  			},
   691  			assertImageLabels: noLabels,
   692  			assertError:       shouldErr("failed to build localhost:5050/goreleaser/one_img_error_with_skip_push:false"),
   693  		},
   694  		"valid_no_latest": {
   695  			dockers: []config.Docker{
   696  				{
   697  					ImageTemplates: []string{
   698  						registry + "goreleaser/test_run_pipe:{{.Version}}",
   699  					},
   700  					Goos:       "linux",
   701  					Goarch:     "amd64",
   702  					Dockerfile: "testdata/Dockerfile",
   703  				},
   704  			},
   705  			expect: []string{
   706  				registry + "goreleaser/test_run_pipe:1.0.0",
   707  			},
   708  			assertImageLabels:   noLabels,
   709  			assertError:         shouldNotErr,
   710  			pubAssertError:      shouldNotErr,
   711  			manifestAssertError: shouldNotErr,
   712  		},
   713  		"valid build args": {
   714  			dockers: []config.Docker{
   715  				{
   716  					ImageTemplates: []string{
   717  						registry + "goreleaser/test_build_args:latest",
   718  					},
   719  					Goos:       "linux",
   720  					Goarch:     "amd64",
   721  					Dockerfile: "testdata/Dockerfile",
   722  					BuildFlagTemplates: []string{
   723  						"--label=foo=bar",
   724  					},
   725  				},
   726  			},
   727  			expect: []string{
   728  				registry + "goreleaser/test_build_args:latest",
   729  			},
   730  			assertImageLabels:   noLabels,
   731  			assertError:         shouldNotErr,
   732  			pubAssertError:      shouldNotErr,
   733  			manifestAssertError: shouldNotErr,
   734  		},
   735  		"bad build args": {
   736  			dockers: []config.Docker{
   737  				{
   738  					ImageTemplates: []string{
   739  						registry + "goreleaser/test_build_args:latest",
   740  					},
   741  					Goos:       "linux",
   742  					Goarch:     "amd64",
   743  					Dockerfile: "testdata/Dockerfile",
   744  					BuildFlagTemplates: []string{
   745  						"--bad-flag",
   746  					},
   747  				},
   748  			},
   749  			assertImageLabels: noLabels,
   750  			assertError:       shouldErr("failed to build localhost:5050/goreleaser/test_build_args:latest"),
   751  		},
   752  		"bad_dockerfile": {
   753  			dockers: []config.Docker{
   754  				{
   755  					ImageTemplates: []string{
   756  						registry + "goreleaser/bad_dockerfile:latest",
   757  					},
   758  					Goos:       "linux",
   759  					Goarch:     "amd64",
   760  					Dockerfile: "testdata/Dockerfile.bad",
   761  				},
   762  			},
   763  			assertImageLabels: noLabels,
   764  			assertError:       shouldErr("failed to build localhost:5050/goreleaser/bad_dockerfile:latest"),
   765  		},
   766  		"tag_template_error": {
   767  			dockers: []config.Docker{
   768  				{
   769  					ImageTemplates: []string{
   770  						registry + "goreleaser/test_run_pipe:{{.Tag}",
   771  					},
   772  					Goos:       "linux",
   773  					Goarch:     "amd64",
   774  					Dockerfile: "testdata/Dockerfile",
   775  				},
   776  			},
   777  			assertImageLabels: noLabels,
   778  			assertError:       shouldTemplateErr,
   779  		},
   780  		"build_flag_template_error": {
   781  			dockers: []config.Docker{
   782  				{
   783  					ImageTemplates: []string{
   784  						registry + "goreleaser/test_run_pipe:latest",
   785  					},
   786  					Goos:       "linux",
   787  					Goarch:     "amd64",
   788  					Dockerfile: "testdata/Dockerfile",
   789  					BuildFlagTemplates: []string{
   790  						"--label=tag={{.Tag}",
   791  					},
   792  				},
   793  			},
   794  			assertImageLabels: noLabels,
   795  			assertError:       shouldTemplateErr,
   796  		},
   797  		"missing_env_on_tag_template": {
   798  			dockers: []config.Docker{
   799  				{
   800  					ImageTemplates: []string{
   801  						registry + "goreleaser/test_run_pipe:{{.Env.NOPE}}",
   802  					},
   803  					Goos:       "linux",
   804  					Goarch:     "amd64",
   805  					Dockerfile: "testdata/Dockerfile",
   806  				},
   807  			},
   808  			assertImageLabels: noLabels,
   809  			assertError:       shouldTemplateErr,
   810  		},
   811  		"missing_env_on_build_flag_template": {
   812  			dockers: []config.Docker{
   813  				{
   814  					ImageTemplates: []string{
   815  						registry + "goreleaser/test_run_pipe:latest",
   816  					},
   817  					Goos:       "linux",
   818  					Goarch:     "amd64",
   819  					Dockerfile: "testdata/Dockerfile",
   820  					BuildFlagTemplates: []string{
   821  						"--label=nope={{.Env.NOPE}}",
   822  					},
   823  				},
   824  			},
   825  			assertImageLabels: noLabels,
   826  			assertError:       shouldTemplateErr,
   827  		},
   828  		"image_has_projectname_template_variable": {
   829  			dockers: []config.Docker{
   830  				{
   831  					ImageTemplates: []string{
   832  						registry + "goreleaser/{{.ProjectName}}:{{.Tag}}-{{.Env.FOO}}",
   833  						registry + "goreleaser/{{.ProjectName}}:latest",
   834  					},
   835  					Goos:       "linux",
   836  					Goarch:     "amd64",
   837  					Dockerfile: "testdata/Dockerfile",
   838  					SkipPush:   "true",
   839  				},
   840  			},
   841  			env: map[string]string{
   842  				"FOO": "123",
   843  			},
   844  			expect: []string{
   845  				registry + "goreleaser/mybin:v1.0.0-123",
   846  				registry + "goreleaser/mybin:latest",
   847  			},
   848  			assertImageLabels:   noLabels,
   849  			assertError:         shouldNotErr,
   850  			pubAssertError:      testlib.AssertSkipped,
   851  			manifestAssertError: shouldNotErr,
   852  		},
   853  		"no_permissions": {
   854  			dockers: []config.Docker{
   855  				{
   856  					ImageTemplates: []string{"docker.io/nope:latest"},
   857  					Goos:           "linux",
   858  					Goarch:         "amd64",
   859  					Dockerfile:     "testdata/Dockerfile",
   860  				},
   861  			},
   862  			expect: []string{
   863  				"docker.io/nope:latest",
   864  			},
   865  			assertImageLabels:   noLabels,
   866  			assertError:         shouldNotErr,
   867  			pubAssertError:      shouldErr(`failed to push docker.io/nope:latest`),
   868  			manifestAssertError: shouldNotErr,
   869  		},
   870  		"dockerfile_doesnt_exist": {
   871  			dockers: []config.Docker{
   872  				{
   873  					ImageTemplates: []string{"whatever:latest"},
   874  					Goos:           "linux",
   875  					Goarch:         "amd64",
   876  					Dockerfile:     "testdata/Dockerfilezzz",
   877  				},
   878  			},
   879  			assertImageLabels: noLabels,
   880  			assertError:       shouldErr(`failed to copy dockerfile`),
   881  		},
   882  		"extra_file_doesnt_exist": {
   883  			dockers: []config.Docker{
   884  				{
   885  					ImageTemplates: []string{"whatever:latest"},
   886  					Goos:           "linux",
   887  					Goarch:         "amd64",
   888  					Dockerfile:     "testdata/Dockerfile",
   889  					Files: []string{
   890  						"testdata/nope.txt",
   891  					},
   892  				},
   893  			},
   894  			assertImageLabels: noLabels,
   895  			assertError:       shouldErr(`failed to copy extra file 'testdata/nope.txt'`),
   896  		},
   897  		"binary doesnt exist": {
   898  			dockers: []config.Docker{
   899  				{
   900  					ImageTemplates: []string{"whatever:latest"},
   901  					Goos:           "linux",
   902  					Goarch:         "amd64",
   903  					Dockerfile:     "testdata/Dockerfile",
   904  					IDs:            []string{"nope"},
   905  				},
   906  			},
   907  			assertImageLabels: noLabels,
   908  			assertError:       shouldErr(`failed to copy wont-exist`),
   909  			extraPrepare: func(t *testing.T, ctx *context.Context) {
   910  				t.Helper()
   911  				ctx.Artifacts.Add(&artifact.Artifact{
   912  					Name:   "wont-exist",
   913  					Path:   "wont-exist",
   914  					Goarch: "amd64",
   915  					Goos:   "linux",
   916  					Type:   artifact.Binary,
   917  					Extra: map[string]interface{}{
   918  						artifact.ExtraID: "nope",
   919  					},
   920  				})
   921  			},
   922  		},
   923  		"multiple_ids": {
   924  			dockers: []config.Docker{
   925  				{
   926  					ImageTemplates: []string{registry + "goreleaser/multiple:latest"},
   927  					Goos:           "darwin",
   928  					Goarch:         "amd64",
   929  					IDs:            []string{"mybin", "anotherbin"},
   930  					Dockerfile:     "testdata/Dockerfile.multiple",
   931  				},
   932  			},
   933  			assertImageLabels:   noLabels,
   934  			assertError:         shouldNotErr,
   935  			pubAssertError:      shouldNotErr,
   936  			manifestAssertError: shouldNotErr,
   937  			expect: []string{
   938  				registry + "goreleaser/multiple:latest",
   939  			},
   940  		},
   941  		"nfpm and multiple binaries": {
   942  			dockers: []config.Docker{
   943  				{
   944  					ImageTemplates: []string{registry + "goreleaser/nfpm:latest"},
   945  					Goos:           "linux",
   946  					Goarch:         "amd64",
   947  					IDs:            []string{"mybin", "anotherbin"},
   948  					Dockerfile:     "testdata/Dockerfile.nfpm",
   949  				},
   950  			},
   951  			assertImageLabels:   noLabels,
   952  			assertError:         shouldNotErr,
   953  			pubAssertError:      shouldNotErr,
   954  			manifestAssertError: shouldNotErr,
   955  			expect: []string{
   956  				registry + "goreleaser/nfpm:latest",
   957  			},
   958  		},
   959  		"nfpm and multiple binaries on arm64": {
   960  			dockers: []config.Docker{
   961  				{
   962  					ImageTemplates: []string{registry + "goreleaser/nfpm_arm:latest"},
   963  					Goos:           "linux",
   964  					Goarch:         "arm64",
   965  					IDs:            []string{"mybin", "anotherbin"},
   966  					Dockerfile:     "testdata/Dockerfile.nfpm",
   967  				},
   968  			},
   969  			assertImageLabels:   noLabels,
   970  			assertError:         shouldNotErr,
   971  			pubAssertError:      shouldNotErr,
   972  			manifestAssertError: shouldNotErr,
   973  			expect: []string{
   974  				registry + "goreleaser/nfpm_arm:latest",
   975  			},
   976  		},
   977  	}
   978  
   979  	killAndRm(t)
   980  	start(t)
   981  	defer killAndRm(t)
   982  
   983  	for name, docker := range table {
   984  		for imager := range imagers {
   985  			t.Run(name+" on "+imager, func(t *testing.T) {
   986  				folder := t.TempDir()
   987  				dist := filepath.Join(folder, "dist")
   988  				require.NoError(t, os.Mkdir(dist, 0o755))
   989  				require.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0o755))
   990  				f, err := os.Create(filepath.Join(dist, "mybin", "mybin"))
   991  				require.NoError(t, err)
   992  				require.NoError(t, f.Close())
   993  				f, err = os.Create(filepath.Join(dist, "mybin", "anotherbin"))
   994  				require.NoError(t, err)
   995  				require.NoError(t, f.Close())
   996  				f, err = os.Create(filepath.Join(dist, "mynfpm.apk"))
   997  				require.NoError(t, err)
   998  				require.NoError(t, f.Close())
   999  				for _, arch := range []string{"amd64", "386", "arm64"} {
  1000  					f, err = os.Create(filepath.Join(dist, fmt.Sprintf("mybin_%s.apk", arch)))
  1001  					require.NoError(t, err)
  1002  					require.NoError(t, f.Close())
  1003  				}
  1004  
  1005  				ctx := context.New(config.Project{
  1006  					ProjectName:     "mybin",
  1007  					Dist:            dist,
  1008  					Dockers:         docker.dockers,
  1009  					DockerManifests: docker.manifests,
  1010  				})
  1011  				ctx.Parallelism = 1
  1012  				ctx.Env = docker.env
  1013  				ctx.Version = "1.0.0"
  1014  				ctx.Git = context.GitInfo{
  1015  					CurrentTag: "v1.0.0",
  1016  					Commit:     "a1b2c3d4",
  1017  				}
  1018  				ctx.Semver = context.Semver{
  1019  					Major: 1,
  1020  					Minor: 0,
  1021  					Patch: 0,
  1022  				}
  1023  				for _, os := range []string{"linux", "darwin"} {
  1024  					for _, arch := range []string{"amd64", "386", "arm64"} {
  1025  						for _, bin := range []string{"mybin", "anotherbin"} {
  1026  							ctx.Artifacts.Add(&artifact.Artifact{
  1027  								Name:   bin,
  1028  								Path:   filepath.Join(dist, "mybin", bin),
  1029  								Goarch: arch,
  1030  								Goos:   os,
  1031  								Type:   artifact.Binary,
  1032  								Extra: map[string]interface{}{
  1033  									artifact.ExtraID: bin,
  1034  								},
  1035  							})
  1036  						}
  1037  					}
  1038  				}
  1039  				for _, arch := range []string{"amd64", "386", "arm64"} {
  1040  					name := fmt.Sprintf("mybin_%s.apk", arch)
  1041  					ctx.Artifacts.Add(&artifact.Artifact{
  1042  						Name:   name,
  1043  						Path:   filepath.Join(dist, name),
  1044  						Goarch: arch,
  1045  						Goos:   "linux",
  1046  						Type:   artifact.LinuxPackage,
  1047  						Extra: map[string]interface{}{
  1048  							artifact.ExtraID: "mybin",
  1049  						},
  1050  					})
  1051  				}
  1052  
  1053  				if docker.extraPrepare != nil {
  1054  					docker.extraPrepare(t, ctx)
  1055  				}
  1056  
  1057  				rmi := func(img string) error {
  1058  					return exec.Command("docker", "rmi", "--force", img).Run()
  1059  				}
  1060  
  1061  				// this might fail as the image doesnt exist yet, so lets ignore the error
  1062  				for _, img := range docker.expect {
  1063  					_ = rmi(img)
  1064  				}
  1065  
  1066  				for i := range ctx.Config.Dockers {
  1067  					docker := &ctx.Config.Dockers[i]
  1068  					docker.Use = imager
  1069  					docker.PushFlags = []string{}
  1070  				}
  1071  				for i := range ctx.Config.DockerManifests {
  1072  					manifest := &ctx.Config.DockerManifests[i]
  1073  					manifest.Use = useDocker
  1074  					manifest.PushFlags = []string{"--insecure"}
  1075  					manifest.CreateFlags = []string{"--insecure"}
  1076  				}
  1077  				err = Pipe{}.Run(ctx)
  1078  				docker.assertError(t, err)
  1079  				if err == nil {
  1080  					docker.pubAssertError(t, Pipe{}.Publish(ctx))
  1081  					docker.manifestAssertError(t, ManifestPipe{}.Publish(ctx))
  1082  				}
  1083  
  1084  				for _, d := range docker.dockers {
  1085  					docker.assertImageLabels(t, d.Use)
  1086  				}
  1087  
  1088  				// this might should not fail as the image should have been created when
  1089  				// the step ran
  1090  				for _, img := range docker.expect {
  1091  					// t.Log("removing docker image", img)
  1092  					require.NoError(t, rmi(img), "could not delete image %s", img)
  1093  				}
  1094  			})
  1095  		}
  1096  	}
  1097  }
  1098  
  1099  func TestBuildCommand(t *testing.T) {
  1100  	images := []string{"goreleaser/test_build_flag", "goreleaser/test_multiple_tags"}
  1101  	tests := []struct {
  1102  		name   string
  1103  		flags  []string
  1104  		buildx bool
  1105  		expect []string
  1106  	}{
  1107  		{
  1108  			name:   "no flags",
  1109  			flags:  []string{},
  1110  			expect: []string{"build", ".", "-t", images[0], "-t", images[1]},
  1111  		},
  1112  		{
  1113  			name:   "single flag",
  1114  			flags:  []string{"--label=foo"},
  1115  			expect: []string{"build", ".", "-t", images[0], "-t", images[1], "--label=foo"},
  1116  		},
  1117  		{
  1118  			name:   "multiple flags",
  1119  			flags:  []string{"--label=foo", "--build-arg=bar=baz"},
  1120  			expect: []string{"build", ".", "-t", images[0], "-t", images[1], "--label=foo", "--build-arg=bar=baz"},
  1121  		},
  1122  		{
  1123  			name:   "buildx",
  1124  			buildx: true,
  1125  			flags:  []string{"--label=foo", "--build-arg=bar=baz"},
  1126  			expect: []string{"buildx", "--builder", "default", "build", ".", "--load", "-t", images[0], "-t", images[1], "--label=foo", "--build-arg=bar=baz"},
  1127  		},
  1128  	}
  1129  	for _, tt := range tests {
  1130  		t.Run(tt.name, func(t *testing.T) {
  1131  			imager := dockerImager{
  1132  				buildx: tt.buildx,
  1133  			}
  1134  			require.Equal(t, tt.expect, imager.buildCommand(images, tt.flags))
  1135  		})
  1136  	}
  1137  }
  1138  
  1139  func TestDescription(t *testing.T) {
  1140  	require.NotEmpty(t, Pipe{}.String())
  1141  }
  1142  
  1143  func TestNoDockerWithoutImageName(t *testing.T) {
  1144  	testlib.AssertSkipped(t, Pipe{}.Run(context.New(config.Project{
  1145  		Dockers: []config.Docker{
  1146  			{
  1147  				Goos: "linux",
  1148  			},
  1149  		},
  1150  	})))
  1151  }
  1152  
  1153  func TestDefault(t *testing.T) {
  1154  	ctx := &context.Context{
  1155  		Config: config.Project{
  1156  			Dockers: []config.Docker{
  1157  				{
  1158  					IDs: []string{"aa"},
  1159  				},
  1160  				{
  1161  					Use: useBuildx,
  1162  				},
  1163  			},
  1164  			DockerManifests: []config.DockerManifest{
  1165  				{},
  1166  				{
  1167  					Use: useDocker,
  1168  				},
  1169  			},
  1170  		},
  1171  	}
  1172  	require.NoError(t, Pipe{}.Default(ctx))
  1173  	require.Len(t, ctx.Config.Dockers, 2)
  1174  	docker := ctx.Config.Dockers[0]
  1175  	require.Equal(t, "linux", docker.Goos)
  1176  	require.Equal(t, "amd64", docker.Goarch)
  1177  	require.Equal(t, []string{"aa"}, docker.IDs)
  1178  	require.Equal(t, useDocker, docker.Use)
  1179  	docker = ctx.Config.Dockers[1]
  1180  	require.Equal(t, useBuildx, docker.Use)
  1181  
  1182  	require.NoError(t, ManifestPipe{}.Default(ctx))
  1183  	require.Len(t, ctx.Config.DockerManifests, 2)
  1184  	require.Equal(t, useDocker, ctx.Config.DockerManifests[0].Use)
  1185  	require.Equal(t, useDocker, ctx.Config.DockerManifests[1].Use)
  1186  }
  1187  
  1188  func TestDefaultDuplicateID(t *testing.T) {
  1189  	ctx := &context.Context{
  1190  		Config: config.Project{
  1191  			Dockers: []config.Docker{
  1192  				{ID: "foo"},
  1193  				{ /* empty */ },
  1194  				{ID: "bar"},
  1195  				{ID: "foo"},
  1196  			},
  1197  			DockerManifests: []config.DockerManifest{
  1198  				{ID: "bar"},
  1199  				{ /* empty */ },
  1200  				{ID: "bar"},
  1201  				{ID: "foo"},
  1202  			},
  1203  		},
  1204  	}
  1205  	require.EqualError(t, Pipe{}.Default(ctx), "found 2 dockers with the ID 'foo', please fix your config")
  1206  	require.EqualError(t, ManifestPipe{}.Default(ctx), "found 2 docker_manifests with the ID 'bar', please fix your config")
  1207  }
  1208  
  1209  func TestDefaultInvalidUse(t *testing.T) {
  1210  	ctx := &context.Context{
  1211  		Config: config.Project{
  1212  			Dockers: []config.Docker{
  1213  				{
  1214  					Use: "something",
  1215  				},
  1216  			},
  1217  			DockerManifests: []config.DockerManifest{
  1218  				{
  1219  					Use: "something",
  1220  				},
  1221  			},
  1222  		},
  1223  	}
  1224  	err := Pipe{}.Default(ctx)
  1225  	require.Error(t, err)
  1226  	require.True(t, strings.HasPrefix(err.Error(), `docker: invalid use: something, valid options are`))
  1227  
  1228  	err = ManifestPipe{}.Default(ctx)
  1229  	require.Error(t, err)
  1230  	require.True(t, strings.HasPrefix(err.Error(), `docker manifest: invalid use: something, valid options are`))
  1231  }
  1232  
  1233  func TestDefaultDockerfile(t *testing.T) {
  1234  	ctx := &context.Context{
  1235  		Config: config.Project{
  1236  			Builds: []config.Build{
  1237  				{},
  1238  			},
  1239  			Dockers: []config.Docker{
  1240  				{},
  1241  				{},
  1242  			},
  1243  		},
  1244  	}
  1245  	require.NoError(t, Pipe{}.Default(ctx))
  1246  	require.Len(t, ctx.Config.Dockers, 2)
  1247  	require.Equal(t, "Dockerfile", ctx.Config.Dockers[0].Dockerfile)
  1248  	require.Equal(t, "Dockerfile", ctx.Config.Dockers[1].Dockerfile)
  1249  }
  1250  
  1251  func TestDraftRelease(t *testing.T) {
  1252  	ctx := context.New(
  1253  		config.Project{
  1254  			Release: config.Release{
  1255  				Draft: true,
  1256  			},
  1257  		},
  1258  	)
  1259  
  1260  	require.False(t, pipe.IsSkip(Pipe{}.Publish(ctx)))
  1261  }
  1262  
  1263  func TestDefaultNoDockers(t *testing.T) {
  1264  	ctx := &context.Context{
  1265  		Config: config.Project{
  1266  			Dockers: []config.Docker{},
  1267  		},
  1268  	}
  1269  	require.NoError(t, Pipe{}.Default(ctx))
  1270  	require.Empty(t, ctx.Config.Dockers)
  1271  }
  1272  
  1273  func TestDefaultFilesDot(t *testing.T) {
  1274  	ctx := &context.Context{
  1275  		Config: config.Project{
  1276  			Dist: "/tmp/distt",
  1277  			Dockers: []config.Docker{
  1278  				{
  1279  					Files: []string{"./lala", "./lolsob", "."},
  1280  				},
  1281  			},
  1282  		},
  1283  	}
  1284  	require.EqualError(t, Pipe{}.Default(ctx), `invalid docker.files: can't be . or inside dist folder: .`)
  1285  }
  1286  
  1287  func TestDefaultFilesDis(t *testing.T) {
  1288  	ctx := &context.Context{
  1289  		Config: config.Project{
  1290  			Dist: "/tmp/dist",
  1291  			Dockers: []config.Docker{
  1292  				{
  1293  					Files: []string{"./fooo", "/tmp/dist/asdasd/asd", "./bar"},
  1294  				},
  1295  			},
  1296  		},
  1297  	}
  1298  	require.EqualError(t, Pipe{}.Default(ctx), `invalid docker.files: can't be . or inside dist folder: /tmp/dist/asdasd/asd`)
  1299  }
  1300  
  1301  func TestDefaultSet(t *testing.T) {
  1302  	ctx := &context.Context{
  1303  		Config: config.Project{
  1304  			Dockers: []config.Docker{
  1305  				{
  1306  					IDs:        []string{"foo"},
  1307  					Goos:       "windows",
  1308  					Goarch:     "i386",
  1309  					Dockerfile: "Dockerfile.foo",
  1310  				},
  1311  			},
  1312  		},
  1313  	}
  1314  	require.NoError(t, Pipe{}.Default(ctx))
  1315  	require.Len(t, ctx.Config.Dockers, 1)
  1316  	docker := ctx.Config.Dockers[0]
  1317  	require.Equal(t, "windows", docker.Goos)
  1318  	require.Equal(t, "i386", docker.Goarch)
  1319  	require.Equal(t, []string{"foo"}, docker.IDs)
  1320  	require.Equal(t, "Dockerfile.foo", docker.Dockerfile)
  1321  }
  1322  
  1323  func Test_processImageTemplates(t *testing.T) {
  1324  	ctx := &context.Context{
  1325  		Config: config.Project{
  1326  			Builds: []config.Build{
  1327  				{
  1328  					ID: "default",
  1329  				},
  1330  			},
  1331  			Dockers: []config.Docker{
  1332  				{
  1333  					Dockerfile: "Dockerfile.foo",
  1334  					ImageTemplates: []string{
  1335  						"user/image:{{.Tag}}",
  1336  						"gcr.io/image:{{.Tag}}-{{.Env.FOO}}",
  1337  						"gcr.io/image:v{{.Major}}.{{.Minor}}",
  1338  					},
  1339  					SkipPush: "true",
  1340  				},
  1341  			},
  1342  		},
  1343  	}
  1344  
  1345  	ctx.Env = map[string]string{
  1346  		"FOO": "123",
  1347  	}
  1348  	ctx.Version = "1.0.0"
  1349  	ctx.Git = context.GitInfo{
  1350  		CurrentTag: "v1.0.0",
  1351  		Commit:     "a1b2c3d4",
  1352  	}
  1353  	ctx.Semver = context.Semver{
  1354  		Major: 1,
  1355  		Minor: 0,
  1356  		Patch: 0,
  1357  	}
  1358  
  1359  	require.NoError(t, Pipe{}.Default(ctx))
  1360  	require.Len(t, ctx.Config.Dockers, 1)
  1361  
  1362  	docker := ctx.Config.Dockers[0]
  1363  	require.Equal(t, "Dockerfile.foo", docker.Dockerfile)
  1364  
  1365  	images, err := processImageTemplates(ctx, docker)
  1366  	require.NoError(t, err)
  1367  	require.Equal(t, []string{
  1368  		"user/image:v1.0.0",
  1369  		"gcr.io/image:v1.0.0-123",
  1370  		"gcr.io/image:v1.0",
  1371  	}, images)
  1372  }
  1373  
  1374  func TestSkip(t *testing.T) {
  1375  	t.Run("image", func(t *testing.T) {
  1376  		t.Run("skip", func(t *testing.T) {
  1377  			require.True(t, Pipe{}.Skip(context.New(config.Project{})))
  1378  		})
  1379  
  1380  		t.Run("skip docker", func(t *testing.T) {
  1381  			ctx := context.New(config.Project{
  1382  				Dockers: []config.Docker{{}},
  1383  			})
  1384  			ctx.SkipDocker = true
  1385  			require.True(t, Pipe{}.Skip(ctx))
  1386  		})
  1387  
  1388  		t.Run("dont skip", func(t *testing.T) {
  1389  			ctx := context.New(config.Project{
  1390  				Dockers: []config.Docker{{}},
  1391  			})
  1392  			require.False(t, Pipe{}.Skip(ctx))
  1393  		})
  1394  	})
  1395  
  1396  	t.Run("manifest", func(t *testing.T) {
  1397  		t.Run("skip", func(t *testing.T) {
  1398  			require.True(t, ManifestPipe{}.Skip(context.New(config.Project{})))
  1399  		})
  1400  
  1401  		t.Run("skip docker", func(t *testing.T) {
  1402  			ctx := context.New(config.Project{
  1403  				DockerManifests: []config.DockerManifest{{}},
  1404  			})
  1405  			ctx.SkipDocker = true
  1406  			require.True(t, ManifestPipe{}.Skip(ctx))
  1407  		})
  1408  
  1409  		t.Run("dont skip", func(t *testing.T) {
  1410  			ctx := context.New(config.Project{
  1411  				DockerManifests: []config.DockerManifest{{}},
  1412  			})
  1413  			require.False(t, ManifestPipe{}.Skip(ctx))
  1414  		})
  1415  	})
  1416  }