|
| 1 | +package runtime |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "github.com/docker/docker/api/types" |
| 8 | + "github.com/docker/docker/api/types/container" |
| 9 | + "github.com/docker/docker/client" |
| 10 | + "github.com/docker/docker/pkg/stdcopy" |
| 11 | + "log" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +// DockerExecutor executes the test inside a docker container |
| 17 | +type DockerExecutor struct { |
| 18 | + Image string // Image which is started to execute the test |
| 19 | + Privileged bool // Enable privileged mode for the container |
| 20 | + User string // User defines which user executes the test |
| 21 | +} |
| 22 | + |
| 23 | +// Execute executes the script inside a docker container |
| 24 | +func (e DockerExecutor) Execute(test TestCase) TestResult { |
| 25 | + ctx := context.Background() |
| 26 | + cli, err := client.NewEnvClient() |
| 27 | + if err != nil { |
| 28 | + test.Result.Error = err |
| 29 | + return TestResult{ |
| 30 | + TestCase: test, |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + log.Printf("Pulling image %s\n", e.Image) |
| 35 | + reader, err := cli.ImagePull(ctx, e.Image, types.ImagePullOptions{}) |
| 36 | + if err != nil { |
| 37 | + test.Result.Error = fmt.Errorf("could not pull image '%s' with error: '%s'", e.Image, err) |
| 38 | + return TestResult{ |
| 39 | + TestCase: test, |
| 40 | + } |
| 41 | + } |
| 42 | + buf := bytes.Buffer{} |
| 43 | + buf.ReadFrom(reader) |
| 44 | + log.Printf("Pull log image'%s':\n %s\n", e.Image, buf.String()) |
| 45 | + |
| 46 | + var env []string |
| 47 | + for k, v := range test.Command.Env { |
| 48 | + env = append(env, fmt.Sprintf("%s=%s", k, v)) |
| 49 | + } |
| 50 | + |
| 51 | + log.Printf("Create container %s\n", e.Image) |
| 52 | + resp, err := cli.ContainerCreate(ctx, &container.Config{ |
| 53 | + Image: e.Image, |
| 54 | + WorkingDir: test.Command.Dir, |
| 55 | + Env: env, |
| 56 | + User: e.User, |
| 57 | + Cmd: []string{"/bin/sh", "-c", test.Command.Cmd}, |
| 58 | + Tty: false, |
| 59 | + }, nil, nil, "") |
| 60 | + if err != nil { |
| 61 | + test.Result.Error = fmt.Errorf("could not pull image '%s' with error: '%s'", e.Image, err) |
| 62 | + return TestResult{ |
| 63 | + TestCase: test, |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + log.Printf("Started container %s %s\n", e.Image, resp.ID) |
| 68 | + if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { |
| 69 | + test.Result.Error = fmt.Errorf("could not pull image '%s' with error: '%s'", e.Image, err) |
| 70 | + return TestResult{ |
| 71 | + TestCase: test, |
| 72 | + } |
| 73 | + } |
| 74 | + duration := time.Duration(1 * time.Second) |
| 75 | + defer cli.ContainerStop(ctx, resp.ID, &duration) |
| 76 | + |
| 77 | + status, err := cli.ContainerWait(ctx, resp.ID) |
| 78 | + if err != nil { |
| 79 | + panic(err) |
| 80 | + } |
| 81 | + |
| 82 | + out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true}) |
| 83 | + if err != nil { |
| 84 | + panic(err) |
| 85 | + } |
| 86 | + |
| 87 | + stdout := &bytes.Buffer{} |
| 88 | + stderr := &bytes.Buffer{} |
| 89 | + |
| 90 | + _, err = stdcopy.StdCopy(stdout, stderr, out) |
| 91 | + if err != nil { |
| 92 | + panic(err) |
| 93 | + } |
| 94 | + |
| 95 | + log.Println("title: '"+test.Title+"'", " Command: ", test.Command.Cmd) |
| 96 | + log.Println("title: '"+test.Title+"'", " Directory: ", test.Command.Dir) |
| 97 | + log.Println("title: '"+test.Title+"'", " Env: ", test.Command.Env) |
| 98 | + |
| 99 | + // Write test result |
| 100 | + test.Result = CommandResult{ |
| 101 | + ExitCode: int(status), |
| 102 | + Stdout: strings.TrimSpace(strings.Replace(stdout.String(), "\r\n", "\n", -1)), |
| 103 | + Stderr: strings.TrimSpace(strings.Replace(stderr.String(), "\r\n", "\n", -1)), |
| 104 | + } |
| 105 | + |
| 106 | + log.Println("title: '"+test.Title+"'", " ExitCode: ", test.Result.ExitCode) |
| 107 | + log.Println("title: '"+test.Title+"'", " Stdout: ", test.Result.Stdout) |
| 108 | + log.Println("title: '"+test.Title+"'", " Stderr: ", test.Result.Stderr) |
| 109 | + |
| 110 | + return Validate(test) |
| 111 | +} |
0 commit comments