diff --git a/acceptance/analyzer_test.go b/acceptance/analyzer_test.go index db56c56eb..5e50660cc 100644 --- a/acceptance/analyzer_test.go +++ b/acceptance/analyzer_test.go @@ -34,7 +34,6 @@ var ( noAuthRegistry *ih.DockerRegistry registry *ih.DockerRegistry registryNetwork string - vh VariableHelper ) func TestAnalyzer(t *testing.T) { @@ -44,8 +43,6 @@ func TestAnalyzer(t *testing.T) { h.AssertNil(t, err) daemonOS = info.OSType - vh = VariableHelper{OS: daemonOS} - // Setup registry dockerConfigDir, err := ioutil.TempDir("", "test.docker.config.dir") @@ -79,7 +76,7 @@ func TestAnalyzer(t *testing.T) { h.DockerBuild(t, analyzeImage, analyzeDockerContext, - h.WithFlags("-f", filepath.Join(analyzeDockerContext, vh.Dockerfilename())), + h.WithFlags("-f", filepath.Join(analyzeDockerContext, dockerfileName)), ) defer h.DockerImageRemove(t, analyzeImage) @@ -111,7 +108,7 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { cmd := exec.Command( "docker", "run", "--rm", analyzeImage, - vh.CtrPath(analyzerPath), + ctrPath(analyzerPath), ) output, err := cmd.CombinedOutput() @@ -125,7 +122,10 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { it("warns", func() { output := h.DockerRun(t, analyzeImage, - h.WithArgs(vh.CtrPath(analyzerPath), "some-image"), + h.WithArgs( + ctrPath(analyzerPath), + "some-image", + ), ) expected := "Not restoring cached layer metadata, no cache flag specified." @@ -151,13 +151,13 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { it("uses the provided group path", func() { h.DockerSeedRunAndCopy(t, containerName, - cacheFixtureDir, vh.CtrPath("/cache"), - copyDir, vh.CtrPath("/layers"), + cacheFixtureDir, ctrPath("/cache"), + copyDir, ctrPath("/layers"), analyzeImage, h.WithArgs( - vh.CtrPath(analyzerPath), - "-cache-dir", vh.CtrPath("/cache"), - "-group", vh.CtrPath("/layers/other-group.toml"), + ctrPath(analyzerPath), + "-cache-dir", ctrPath("/cache"), + "-group", ctrPath("/layers/other-group.toml"), "some-image", ), ) @@ -172,11 +172,11 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { h.DockerRunAndCopy(t, containerName, copyDir, - vh.CtrPath("/some-dir/some-analyzed.toml"), + ctrPath("/some-dir/some-analyzed.toml"), analyzeImage, h.WithArgs( - vh.CtrPath(analyzerPath), - "-analyzed", vh.CtrPath("/some-dir/some-analyzed.toml"), + ctrPath(analyzerPath), + "-analyzed", ctrPath("/some-dir/some-analyzed.toml"), "some-image", ), ) @@ -190,10 +190,14 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { h.DockerRunAndCopy(t, containerName, copyDir, - vh.CtrPath("/layers/analyzed.toml"), + ctrPath("/layers/analyzed.toml"), analyzeImage, - h.WithFlags(vh.DockerSocketMount()...), - h.WithArgs(vh.CtrPath(analyzerPath), "-daemon", "some-image"), + h.WithFlags(dockerSocketMount...), + h.WithArgs( + ctrPath(analyzerPath), + "-daemon", + "some-image", + ), ) assertAnalyzedMetadata(t, filepath.Join(copyDir, "analyzed.toml")) @@ -210,7 +214,7 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { "docker", "build", "-t", appImage, - "--build-arg", "fromImage="+vh.ContainerBaseImage(), + "--build-arg", "fromImage="+containerBaseImage, "--build-arg", "metadata="+metadata, filepath.Join("testdata", "analyzer", "app-image"), ) @@ -225,11 +229,11 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { output := h.DockerRunAndCopy(t, containerName, copyDir, - vh.CtrPath("/layers"), + ctrPath("/layers"), analyzeImage, - h.WithFlags(vh.DockerSocketMount()...), + h.WithFlags(dockerSocketMount...), h.WithArgs( - vh.CtrPath(analyzerPath), + ctrPath(analyzerPath), "-daemon", appImage, ), @@ -243,11 +247,11 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { output := h.DockerRunAndCopy(t, containerName, copyDir, - vh.CtrPath("/layers"), + ctrPath("/layers"), analyzeImage, - h.WithFlags(vh.DockerSocketMount()...), + h.WithFlags(dockerSocketMount...), h.WithArgs( - vh.CtrPath(analyzerPath), + ctrPath(analyzerPath), "-daemon", "-skip-layers", appImage, @@ -273,7 +277,7 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { "docker", "build", "-t", cacheImage, - "--build-arg", "fromImage="+vh.ContainerBaseImage(), + "--build-arg", "fromImage="+containerBaseImage, "--build-arg", "metadata="+metadata, filepath.Join("testdata", "analyzer", "cache-image"), ) @@ -288,11 +292,11 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { h.DockerRunAndCopy(t, containerName, copyDir, - vh.CtrPath("/layers"), + ctrPath("/layers"), analyzeImage, - h.WithFlags(vh.DockerSocketMount()...), + h.WithFlags(dockerSocketMount...), h.WithArgs( - vh.CtrPath(analyzerPath), + ctrPath(analyzerPath), "-daemon", "-cache-image", cacheImage, "some-image", @@ -314,7 +318,7 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { t, "some-cache-image-"+h.RandString(10), filepath.Join("testdata", "analyzer", "cache-image"), - "--build-arg", "fromImage="+vh.ContainerBaseImage(), + "--build-arg", "fromImage="+containerBaseImage, "--build-arg", "metadata="+metadata, ) }) @@ -331,12 +335,12 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { "/layers", analyzeImage, h.WithFlags(append( - vh.DockerSocketMount(), + dockerSocketMount, "--env", "CNB_REGISTRY_AUTH="+cacheAuthConfig, "--network", registryNetwork, )...), h.WithArgs( - vh.CtrPath(analyzerPath), + ctrPath(analyzerPath), "-daemon", "-cache-image", authRegCacheImage, "some-image", @@ -352,14 +356,14 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { output := h.DockerRunAndCopy(t, containerName, copyDir, - vh.CtrPath("/layers"), + ctrPath("/layers"), analyzeImage, h.WithFlags( "--env", "DOCKER_CONFIG=/docker-config", "--network", registryNetwork, ), h.WithArgs( - vh.CtrPath(analyzerPath), + ctrPath(analyzerPath), "-cache-image", authRegCacheImage, "some-image", @@ -382,7 +386,7 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { t, imageName, filepath.Join("testdata", "analyzer", "cache-image"), - "--build-arg", "fromImage="+vh.ContainerBaseImage(), + "--build-arg", "fromImage="+containerBaseImage, "--build-arg", "metadata="+metadata, ) @@ -397,11 +401,11 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { output := h.DockerRunAndCopy(t, containerName, copyDir, - vh.CtrPath("/layers"), + ctrPath("/layers"), analyzeImage, h.WithFlags("--network", registryNetwork), h.WithArgs( - vh.CtrPath(analyzerPath), + ctrPath(analyzerPath), "-cache-image", noAuthRegCacheImage, "some-image", @@ -418,14 +422,14 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { it("restores cache metadata", func() { output := h.DockerSeedRunAndCopy(t, containerName, - cacheFixtureDir, vh.CtrPath("/cache"), - copyDir, vh.CtrPath("/layers"), + cacheFixtureDir, ctrPath("/cache"), + copyDir, ctrPath("/layers"), analyzeImage, - h.WithFlags(vh.DockerSocketMount()...), + h.WithFlags(dockerSocketMount...), h.WithArgs( - vh.CtrPath(analyzerPath), + ctrPath(analyzerPath), "-daemon", - "-cache-dir", vh.CtrPath("/cache"), + "-cache-dir", ctrPath("/cache"), "some-image", ), ) @@ -443,7 +447,7 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { output := h.DockerRun(t, analyzeImage, h.WithFlags(append( - vh.DockerSocketMount(), + dockerSocketMount, "--volume", cacheVolume+":/cache", )...), h.WithBash( @@ -467,7 +471,7 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { output := h.DockerRun(t, analyzeImage, h.WithFlags(append( - vh.DockerSocketMount(), + dockerSocketMount, "--volume", cacheVolume+":/cache", )...), h.WithBash( @@ -489,9 +493,12 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { h.DockerRunAndCopy(t, containerName, copyDir, - vh.CtrPath("/layers/analyzed.toml"), + ctrPath("/layers/analyzed.toml"), analyzeImage, - h.WithArgs(vh.CtrPath(analyzerPath), "some-image"), + h.WithArgs( + ctrPath(analyzerPath), + "some-image", + ), ) assertAnalyzedMetadata(t, filepath.Join(copyDir, "analyzed.toml")) @@ -507,7 +514,7 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { t, "some-app-image-"+h.RandString(10), filepath.Join("testdata", "analyzer", "app-image"), - "--build-arg", "fromImage="+vh.ContainerBaseImage(), + "--build-arg", "fromImage="+containerBaseImage, "--build-arg", "metadata="+metadata, ) }) @@ -521,14 +528,14 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { output := h.DockerRunAndCopy(t, containerName, copyDir, - vh.CtrPath("/layers"), + ctrPath("/layers"), analyzeImage, h.WithFlags( "--env", "CNB_REGISTRY_AUTH="+appAuthConfig, "--network", registryNetwork, ), h.WithArgs( - vh.CtrPath(analyzerPath), + ctrPath(analyzerPath), authRegAppImage, ), ) @@ -542,14 +549,14 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { output := h.DockerRunAndCopy(t, containerName, copyDir, - vh.CtrPath("/layers"), + ctrPath("/layers"), analyzeImage, h.WithFlags( "--env", "DOCKER_CONFIG=/docker-config", "--network", registryNetwork, ), h.WithArgs( - vh.CtrPath(analyzerPath), + ctrPath(analyzerPath), authRegAppImage, ), ) @@ -563,14 +570,14 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { output := h.DockerRunAndCopy(t, containerName, copyDir, - vh.CtrPath("/layers"), + ctrPath("/layers"), analyzeImage, h.WithFlags( "--network", registryNetwork, "--env", "CNB_REGISTRY_AUTH="+appAuthConfig, ), h.WithArgs( - vh.CtrPath(analyzerPath), + ctrPath(analyzerPath), "-skip-layers", authRegAppImage, ), @@ -593,7 +600,7 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { t, imageName, filepath.Join("testdata", "analyzer", "app-image"), - "--build-arg", "fromImage="+vh.ContainerBaseImage(), + "--build-arg", "fromImage="+containerBaseImage, "--build-arg", "metadata="+metadata, ) @@ -608,11 +615,11 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { output := h.DockerRunAndCopy(t, containerName, copyDir, - vh.CtrPath("/layers"), + ctrPath("/layers"), analyzeImage, h.WithFlags("--network", registryNetwork), h.WithArgs( - vh.CtrPath(analyzerPath), + ctrPath(analyzerPath), noAuthRegAppImage, ), ) @@ -625,11 +632,11 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { output := h.DockerRunAndCopy(t, containerName, copyDir, - vh.CtrPath("/layers"), + ctrPath("/layers"), analyzeImage, h.WithFlags("--network", registryNetwork), h.WithArgs( - vh.CtrPath(analyzerPath), + ctrPath(analyzerPath), "-skip-layers", noAuthRegAppImage, ), @@ -653,7 +660,7 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { t, "some-cache-image-"+h.RandString(10), filepath.Join("testdata", "analyzer", "cache-image"), - "--build-arg", "fromImage="+vh.ContainerBaseImage(), + "--build-arg", "fromImage="+containerBaseImage, "--build-arg", "metadata="+metadata, ) }) @@ -667,14 +674,14 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { output := h.DockerRunAndCopy(t, containerName, copyDir, - vh.CtrPath("/layers"), + ctrPath("/layers"), analyzeImage, h.WithFlags( "--env", "CNB_REGISTRY_AUTH="+cacheAuthConfig, "--network", registryNetwork, ), h.WithArgs( - vh.CtrPath(analyzerPath), + ctrPath(analyzerPath), "-cache-image", authRegCacheImage, "some-image", ), @@ -689,14 +696,14 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { output := h.DockerRunAndCopy(t, containerName, copyDir, - vh.CtrPath("/layers"), + ctrPath("/layers"), analyzeImage, h.WithFlags( "--env", "DOCKER_CONFIG=/docker-config", "--network", registryNetwork, ), h.WithArgs( - vh.CtrPath(analyzerPath), + ctrPath(analyzerPath), "-cache-image", authRegCacheImage, "some-image", @@ -719,7 +726,7 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { t, imageName, filepath.Join("testdata", "analyzer", "cache-image"), - "--build-arg", "fromImage="+vh.ContainerBaseImage(), + "--build-arg", "fromImage="+containerBaseImage, "--build-arg", "metadata="+metadata, ) @@ -734,11 +741,11 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { output := h.DockerRunAndCopy(t, containerName, copyDir, - vh.CtrPath("/layers"), + ctrPath("/layers"), analyzeImage, h.WithFlags("--network", registryNetwork), h.WithArgs( - vh.CtrPath(analyzerPath), + ctrPath(analyzerPath), "-cache-image", noAuthRegCacheImage, "some-image", ), @@ -753,12 +760,12 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { it("restores cache metadata", func() { output := h.DockerSeedRunAndCopy(t, containerName, - cacheFixtureDir, vh.CtrPath("/cache"), - copyDir, vh.CtrPath("/layers"), + cacheFixtureDir, ctrPath("/cache"), + copyDir, ctrPath("/layers"), analyzeImage, h.WithArgs( - vh.CtrPath(analyzerPath), - "-cache-dir", vh.CtrPath("/cache"), + ctrPath(analyzerPath), + "-cache-dir", ctrPath("/cache"), "some-image", ), ) @@ -778,22 +785,22 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { // The working directory is set to /layers in the Dockerfile h.DockerSeedRunAndCopy(t, containerName, - cacheFixtureDir, vh.CtrPath("/cache"), - otherLayersDir, vh.CtrPath("/other-layers"), + cacheFixtureDir, ctrPath("/cache"), + otherLayersDir, ctrPath("/other-layers"), analyzeImage, h.WithFlags( "--env", "CNB_PLATFORM_API=0.4", ), h.WithArgs( - vh.CtrPath(analyzerPath), - "-layers", vh.CtrPath("/other-layers"), - "-cache-dir", vh.CtrPath("/cache"), // use a cache so that we can observe the effect of group.toml on /some-other-layers (since we don't have a previous image) + ctrPath(analyzerPath), + "-layers", ctrPath("/other-layers"), + "-cache-dir", ctrPath("/cache"), // use a cache so that we can observe the effect of group.toml on /some-other-layers (since we don't have a previous image) "some-image", ), ) h.AssertPathExists(t, filepath.Join(otherLayersDir, "some-buildpack-id")) - h.DockerCopyOut(t, containerName, vh.CtrPath("/layers"), layersDir) + h.DockerCopyOut(t, containerName, ctrPath("/layers"), layersDir) assertAnalyzedMetadata(t, filepath.Join(layersDir, "analyzed.toml")) }) }) @@ -804,16 +811,16 @@ func testAnalyzer(t *testing.T, when spec.G, it spec.S) { it("uses the group path at the layers path and writes analyzed.toml at the layers path", func() { h.DockerSeedRunAndCopy(t, containerName, - cacheFixtureDir, vh.CtrPath("/cache"), - copyDir, vh.CtrPath("/some-other-layers"), + cacheFixtureDir, ctrPath("/cache"), + copyDir, ctrPath("/some-other-layers"), analyzeImage, h.WithFlags( "--env", "CNB_PLATFORM_API=0.5", ), h.WithArgs( - vh.CtrPath(analyzerPath), - "-layers", vh.CtrPath("/some-other-layers"), - "-cache-dir", vh.CtrPath("/cache"), // use a cache so that we can observe the effect of group.toml on /some-other-layers (since we don't have a previous image) + ctrPath(analyzerPath), + "-layers", ctrPath("/some-other-layers"), + "-cache-dir", ctrPath("/cache"), // use a cache so that we can observe the effect of group.toml on /some-other-layers (since we don't have a previous image) "some-image", ), ) diff --git a/acceptance/launcher_test.go b/acceptance/launcher_test.go index 7bc48cc10..67f58379c 100644 --- a/acceptance/launcher_test.go +++ b/acceptance/launcher_test.go @@ -26,8 +26,6 @@ func TestLauncher(t *testing.T) { h.AssertNil(t, err) daemonOS = info.OSType - vh = VariableHelper{OS: daemonOS} - launchDockerContext = filepath.Join("testdata", "launcher") if daemonOS == "windows" { launcherBinaryDir = filepath.Join("testdata", "launcher", "windows", "container", "cnb", "lifecycle") @@ -37,7 +35,7 @@ func TestLauncher(t *testing.T) { h.MakeAndCopyLauncher(t, daemonOS, launcherBinaryDir) - h.DockerBuild(t, launchImage, launchDockerContext, h.WithFlags("-f", filepath.Join(launchDockerContext, vh.Dockerfilename()))) + h.DockerBuild(t, launchImage, launchDockerContext, h.WithFlags("-f", filepath.Join(launchDockerContext, dockerfileName))) defer h.DockerImageRemove(t, launchImage) spec.Run(t, "acceptance", testLauncher, spec.Parallel(), spec.Report(report.Terminal{})) @@ -51,10 +49,10 @@ func testLauncher(t *testing.T, when spec.G, it spec.S) { "--env=VAR_FROM_EXEC_D=orig-val", launchImage, "exec.d-checker") - helper := "helper" + vh.Exe() - execDHelper := vh.CtrPath("/", "layers", vh.ExecDBpDir(), "some_layer", "exec.d", helper) - execDCheckerHelper := vh.CtrPath("/", "layers", vh.ExecDBpDir(), "some_layer", "exec.d", "exec.d-checker", helper) - workDir := vh.CtrPath("/", "workspace") + helper := "helper" + exe + execDHelper := ctrPath("/layers", execDBpDir, "some_layer/exec.d", helper) + execDCheckerHelper := ctrPath("/layers", execDBpDir, "some_layer/exec.d/exec.d-checker", helper) + workDir := ctrPath("/workspace") expected := fmt.Sprintf("%s was executed\n", execDHelper) expected += fmt.Sprintf("Exec.d Working Dir: %s\n", workDir) diff --git a/acceptance/variables.go b/acceptance/variables.go deleted file mode 100644 index 04ada892c..000000000 --- a/acceptance/variables.go +++ /dev/null @@ -1,77 +0,0 @@ -package acceptance - -import ( - "path" - "strings" -) - -type VariableHelper struct { - OS string -} - -func (h *VariableHelper) DummyCommand() []string { - if h.OS == "windows" { - return []string{"cmd", "/c", "exit 0"} - } - return []string{"true"} -} - -func (h *VariableHelper) DockerSocketMount() []string { - if h.OS == "windows" { - return []string{ - "--mount", `type=npipe,source=\\.\pipe\docker_engine,target=\\.\pipe\docker_engine`, - "--user", "ContainerAdministrator", - } - } - return []string{ - "--mount", "type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock", - "--user", "0", - } -} - -func (h *VariableHelper) ContainerBaseImage() string { - if h.OS == "windows" { - return "mcr.microsoft.com/windows/nanoserver:1809" - } - return "ubuntu:bionic" -} - -func (h *VariableHelper) VolumeHelperImage() string { - if h.OS == "windows" { - return "mcr.microsoft.com/windows/nanoserver:1809" - } - return "busybox" -} - -func (h *VariableHelper) CtrPath(unixPathParts ...string) string { - unixPath := path.Join(unixPathParts...) - if h.OS == "windows" { - windowsPath := strings.ReplaceAll(unixPath, "/", `\`) - if strings.HasPrefix(windowsPath, `\`) { - return "c:" + windowsPath - } - return windowsPath - } - return unixPath -} - -func (h *VariableHelper) Dockerfilename() string { - if h.OS == "windows" { - return "Dockerfile.windows" - } - return "Dockerfile" -} - -func (h *VariableHelper) ExecDBpDir() string { - if h.OS == "windows" { - return "0.6_buildpack" - } - return "0.5_buildpack" -} - -func (h *VariableHelper) Exe() string { - if h.OS == "windows" { - return ".exe" - } - return "" -} diff --git a/acceptance/variables_unix.go b/acceptance/variables_unix.go new file mode 100644 index 000000000..8ba7dcc2d --- /dev/null +++ b/acceptance/variables_unix.go @@ -0,0 +1,21 @@ +//+build linux darwin + +package acceptance + +import "path" + +const ( + dockerfileName = "Dockerfile" + exe = "" + execDBpDir = "0.5_buildpack" + containerBaseImage = "busybox" +) + +var dockerSocketMount = []string{ + "--mount", "type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock", + "--user", "0", +} + +func ctrPath(unixPathParts ...string) string { + return path.Join(unixPathParts...) +} diff --git a/acceptance/variables_windows.go b/acceptance/variables_windows.go new file mode 100644 index 000000000..47330f298 --- /dev/null +++ b/acceptance/variables_windows.go @@ -0,0 +1,29 @@ +package acceptance + +import ( + "path" + "path/filepath" + "strings" +) + +const ( + dockerfileName = "Dockerfile.windows" + exe = ".exe" + execDBpDir = "0.6_buildpack" + containerBaseImage = "mcr.microsoft.com/windows/nanoserver:1809" +) + +var dockerSocketMount = []string{ + "--mount", `type=npipe,source=\\.\pipe\docker_engine,target=\\.\pipe\docker_engine`, + "--user", "ContainerAdministrator", +} + +//ctrPath equivalent to path.Join but converts to Windows slashes and drive prefix when needed +func ctrPath(unixPathParts ...string) string { + unixPath := path.Join(unixPathParts...) + windowsPath := filepath.FromSlash(unixPath) + if strings.HasPrefix(windowsPath, `\`) { + return "c:" + windowsPath + } + return windowsPath +}