github.com/toplink-cn/moby@v0.0.0-20240305205811-460b4aebdf81/integration-cli/fixtures_linux_daemon_test.go (about)

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