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