-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocker_helpers_test.go
More file actions
227 lines (207 loc) · 6.68 KB
/
docker_helpers_test.go
File metadata and controls
227 lines (207 loc) · 6.68 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"testing"
"time"
)
var (
RUN_SECS = uint(60)
DURATION_SET = false
NETWORK_HOST = false
)
func init() {
s := os.Getenv("TEST_RUN_SECS")
if s != "" {
i, err := strconv.Atoi(s)
if err != nil {
panic("Invalid value for env var TEST_RUN_SECS")
}
RUN_SECS = uint(i)
DURATION_SET = true
}
network_host, ok := os.LookupEnv("NETWORK_HOST")
if ok && network_host != "OFF" {
NETWORK_HOST = true
}
}
type DockerTestConfig struct {
folder string
jsonFilePath string
dockerfilePath string
}
func findDockerConfigs(rootDir string, t *testing.T, scenarioRegexp string) ([]DockerTestConfig, error) {
var configs []DockerTestConfig
var folder, jsonFilePath, dockerfilePath string
testPathRegexp := regexp.MustCompile(scenarioRegexp)
err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !testPathRegexp.MatchString(filepath.Dir(path)) {
return nil
}
// check if the file is a JSON file or a Dockerfile
if filepath.Base(path) == "expected_profile.json" {
jsonFilePath = path
} else if filepath.Base(path) == "Dockerfile" {
dockerfilePath = path
} else {
// skip files that are not JSON or Dockerfiles
return nil
}
// if we have both a JSON file and a Dockerfile, create a Config instance
if jsonFilePath != "" && dockerfilePath != "" {
if filepath.Dir(jsonFilePath) != filepath.Dir(dockerfilePath) {
t.Errorf("miss matching file structure in %s", filepath.Dir(jsonFilePath))
return nil
}
folder = filepath.Dir(jsonFilePath)
configs = append(configs, DockerTestConfig{folder, jsonFilePath, dockerfilePath})
jsonFilePath = ""
dockerfilePath = ""
}
return nil
})
if err != nil {
return nil, err
}
return configs, nil
}
// Find the base image being used within a dockerfile
func extractBaseImage(dockerfilePath string) (string, error) {
file, err := os.Open(dockerfilePath)
if err != nil {
return "", err
}
defer file.Close()
scanner := bufio.NewScanner(file)
lineCount := 0
for scanner.Scan() && lineCount < 10 {
line := scanner.Text()
matches := regexp.MustCompile(`ARG BASE_IMAGE="(.+?)"`).FindStringSubmatch(line)
if len(matches) > 1 {
return matches[1], nil
}
lineCount++
}
if err := scanner.Err(); err != nil {
return "", err
}
return "", nil
}
// buildBaseImages collects and builds all base images referenced by the given configs.
func buildBaseImages(t *testing.T, configs []DockerTestConfig) {
baseImageNames := map[string]bool{}
for _, config := range configs {
baseImage, err := extractBaseImage(config.dockerfilePath)
if err != nil {
t.Fatalf("Error extracting base image from %s: %v", config.dockerfilePath, err)
}
if baseImage != "" {
baseImageNames[baseImage] = true
}
}
for baseImageName := range baseImageNames {
t.Log("Building base image:", baseImageName)
buildBaseImage("./", baseImageName, t)
}
}
func buildBaseImage(rootDir string, baseImageName string, t *testing.T) {
baseImageDir := filepath.Join(rootDir, "base_images")
dockerfileName := "Dockerfile." + strings.TrimPrefix(baseImageName, "prof-")
dockerfilePath := filepath.Join(baseImageDir, dockerfileName)
if _, err := os.Stat(dockerfilePath); os.IsNotExist(err) {
t.Fatalf("Required base Dockerfile %s not found!", dockerfilePath)
return
}
tag := baseImageName
args := []string{"build", "-t", tag, "-f", dockerfilePath}
if u := os.Getenv("DDTRACE_INSTALL_URL"); u != "" {
args = append(args, "--build-arg", "DDTRACE_INSTALL_URL="+u)
}
args = append(args, rootDir)
buildCmd := exec.Command("docker", args...)
buildCmd.Stdout = os.Stdout
buildCmd.Stderr = os.Stderr
err := buildCmd.Run()
if err != nil {
t.Fatalf("Error building base image %s: %v", tag, err)
}
t.Logf("Built base image with tag: %s", tag)
}
// returns the tag for built docker app
func buildTestApp(t *testing.T, config DockerTestConfig) string {
// we could use the docker client, though that makes it harder to do command lines manually
now_time := time.Now()
// Following arg helps forces to rerun steps after the arg (allows reinstallation of recent profiler) --build-arg CACHE_DATE=$(date +%Y-%m-%d_%H:%M:%S)
args := []string{"build", "-f", config.dockerfilePath, "--build-arg", now_time.Format("2006-01-02_15:04:05"), "-t", "test-app"}
if u := os.Getenv("DDTRACE_INSTALL_URL"); u != "" {
args = append(args, "--build-arg", "DDTRACE_INSTALL_URL="+u)
}
args = append(args, ".")
cmd := exec.Command("docker", args...)
out, err := cmd.CombinedOutput()
if err != nil {
t.Logf("%s", err)
t.Fatalf("Error building %s - %s", config.folder, out)
}
return string("test-app")
}
// runTestAppSafe runs a docker container for the given scenario and returns the
// output directory. It returns an error instead of calling t.Fatalf, making it
// safe to call from goroutines.
func runTestAppSafe(dockerTag string, folder string) (string, error) {
currentPath, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("getwd: %w", err)
}
profilePath := currentPath + "/data"
timestamp := time.Now().Format("20060102-150405")
tmpdir, err := os.MkdirTemp(profilePath, filepath.Base(folder)+"-"+timestamp+"-*")
if err != nil {
return "", fmt.Errorf("mkdtemp: %w", err)
}
mountOption := tmpdir + ":/app/data:rw"
userID := os.Getuid()
groupID := os.Getgid()
userOption := fmt.Sprintf("%d:%d", userID, groupID)
var args []string
if strings.Contains(folder, "full_host") {
args = []string{"run", "-v", mountOption, "--pid=host", "--privileged", "--security-opt", "seccomp=unconfined"}
args = append(args, "--cap-add=SYS_ADMIN", "--cap-add=SYS_PTRACE", "--cap-add=SYS_RESOURCE")
args = append(args, "-v", "/sys/kernel/debug:/sys/kernel/debug:ro")
args = append(args, "-v", "/sys/kernel/tracing:/sys/kernel/tracing:ro")
} else {
args = []string{"run", "-v", mountOption, "-u", userOption, "--security-opt", "seccomp=unconfined"}
}
if DURATION_SET {
args = append(args, "-e", "EXECUTION_TIME_SEC="+fmt.Sprint(RUN_SECS))
}
if NETWORK_HOST {
args = append(args, "--network=host")
}
if !strings.Contains(folder, "full_host") {
args = append(args, "-e", "DD_SERVICE=prof-correctness-"+strings.Split(folder, "/")[1])
}
args = append(args, dockerTag+":latest")
cmd := exec.Command("docker", args...)
out, err := cmd.CombinedOutput()
if err != nil {
_ = os.WriteFile(tmpdir+"/output.txt", out, 0644) // best-effort
return tmpdir, fmt.Errorf("docker run failed: %w\noutput: %s", err, out)
}
if writeErr := os.WriteFile(tmpdir+"/output.txt", out, 0644); writeErr != nil {
return tmpdir, fmt.Errorf("write output: %w", writeErr)
}
return tmpdir, nil
}