gopkg.in/docker/docker.v20@v20.10.27/integration-cli/fixtures_linux_daemon_test.go (about)

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