github.com/devdivbcp/moby@v17.12.0-ce-rc1.0.20200726071732-2d4bfdc789ad+incompatible/integration-cli/fixtures_linux_daemon_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "os/exec" 8 "path/filepath" 9 "runtime" 10 "strings" 11 "testing" 12 13 "github.com/docker/docker/internal/test/fixtures/load" 14 "gotest.tools/assert" 15 ) 16 17 type testingT interface { 18 logT 19 Fatalf(string, ...interface{}) 20 } 21 22 type logT interface { 23 Logf(string, ...interface{}) 24 } 25 26 func ensureSyscallTest(c *testing.T) { 27 defer testEnv.ProtectImage(c, "syscall-test:latest") 28 29 // If the image already exists, there's nothing left to do. 30 if testEnv.HasExistingImage(c, "syscall-test:latest") { 31 return 32 } 33 34 // if no match, must build in docker, which is significantly slower 35 // (slower mostly because of the vfs graphdriver) 36 if testEnv.OSType != runtime.GOOS { 37 ensureSyscallTestBuild(c) 38 return 39 } 40 41 tmp, err := ioutil.TempDir("", "syscall-test-build") 42 assert.NilError(c, err, "couldn't create temp dir") 43 defer os.RemoveAll(tmp) 44 45 gcc, err := exec.LookPath("gcc") 46 assert.NilError(c, err, "could not find gcc") 47 48 tests := []string{"userns", "ns", "acct", "setuid", "setgid", "socket", "raw"} 49 for _, test := range tests { 50 out, err := exec.Command(gcc, "-g", "-Wall", "-static", fmt.Sprintf("../contrib/syscall-test/%s.c", test), "-o", fmt.Sprintf("%s/%s-test", tmp, test)).CombinedOutput() 51 assert.NilError(c, err, string(out)) 52 } 53 54 if runtime.GOOS == "linux" && runtime.GOARCH == "amd64" { 55 out, err := exec.Command(gcc, "-s", "-m32", "-nostdlib", "-static", "../contrib/syscall-test/exit32.s", "-o", tmp+"/"+"exit32-test").CombinedOutput() 56 assert.NilError(c, err, string(out)) 57 } 58 59 dockerFile := filepath.Join(tmp, "Dockerfile") 60 content := []byte(` 61 FROM debian:jessie 62 COPY . /usr/bin/ 63 `) 64 err = ioutil.WriteFile(dockerFile, content, 600) 65 assert.NilError(c, err) 66 67 var buildArgs []string 68 if arg := os.Getenv("DOCKER_BUILD_ARGS"); strings.TrimSpace(arg) != "" { 69 buildArgs = strings.Split(arg, " ") 70 } 71 buildArgs = append(buildArgs, []string{"-q", "-t", "syscall-test", tmp}...) 72 buildArgs = append([]string{"build"}, buildArgs...) 73 dockerCmd(c, buildArgs...) 74 } 75 76 func ensureSyscallTestBuild(c *testing.T) { 77 err := load.FrozenImagesLinux(testEnv.APIClient(), "buildpack-deps:jessie") 78 assert.NilError(c, err) 79 80 var buildArgs []string 81 if arg := os.Getenv("DOCKER_BUILD_ARGS"); strings.TrimSpace(arg) != "" { 82 buildArgs = strings.Split(arg, " ") 83 } 84 buildArgs = append(buildArgs, []string{"-q", "-t", "syscall-test", "../contrib/syscall-test"}...) 85 buildArgs = append([]string{"build"}, buildArgs...) 86 dockerCmd(c, buildArgs...) 87 } 88 89 func ensureNNPTest(c *testing.T) { 90 defer testEnv.ProtectImage(c, "nnp-test:latest") 91 92 // If the image already exists, there's nothing left to do. 93 if testEnv.HasExistingImage(c, "nnp-test:latest") { 94 return 95 } 96 97 // if no match, must build in docker, which is significantly slower 98 // (slower mostly because of the vfs graphdriver) 99 if testEnv.OSType != runtime.GOOS { 100 ensureNNPTestBuild(c) 101 return 102 } 103 104 tmp, err := ioutil.TempDir("", "docker-nnp-test") 105 assert.NilError(c, err) 106 107 gcc, err := exec.LookPath("gcc") 108 assert.NilError(c, err, "could not find gcc") 109 110 out, err := exec.Command(gcc, "-g", "-Wall", "-static", "../contrib/nnp-test/nnp-test.c", "-o", filepath.Join(tmp, "nnp-test")).CombinedOutput() 111 assert.NilError(c, err, string(out)) 112 113 dockerfile := filepath.Join(tmp, "Dockerfile") 114 content := ` 115 FROM debian:jessie 116 COPY . /usr/bin 117 RUN chmod +s /usr/bin/nnp-test 118 ` 119 err = ioutil.WriteFile(dockerfile, []byte(content), 600) 120 assert.NilError(c, err, "could not write Dockerfile for nnp-test image") 121 122 var buildArgs []string 123 if arg := os.Getenv("DOCKER_BUILD_ARGS"); strings.TrimSpace(arg) != "" { 124 buildArgs = strings.Split(arg, " ") 125 } 126 buildArgs = append(buildArgs, []string{"-q", "-t", "nnp-test", tmp}...) 127 buildArgs = append([]string{"build"}, buildArgs...) 128 dockerCmd(c, buildArgs...) 129 } 130 131 func ensureNNPTestBuild(c *testing.T) { 132 err := load.FrozenImagesLinux(testEnv.APIClient(), "buildpack-deps:jessie") 133 assert.NilError(c, err) 134 135 var buildArgs []string 136 if arg := os.Getenv("DOCKER_BUILD_ARGS"); strings.TrimSpace(arg) != "" { 137 buildArgs = strings.Split(arg, " ") 138 } 139 buildArgs = append(buildArgs, []string{"-q", "-t", "npp-test", "../contrib/nnp-test"}...) 140 buildArgs = append([]string{"build"}, buildArgs...) 141 dockerCmd(c, buildArgs...) 142 }