forked from konflux-ci/konflux-build-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrunner_container.go
More file actions
430 lines (368 loc) · 11.6 KB
/
runner_container.go
File metadata and controls
430 lines (368 loc) · 11.6 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package integration_tests_framework
import (
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"strings"
cliWrappers "github.com/konflux-ci/konflux-build-cli/pkg/cliwrappers"
l "github.com/konflux-ci/konflux-build-cli/pkg/logger"
)
const (
ResultsPathInContainer = "/tmp/"
)
type ContainerStatus int
const (
ContainerStatus_NotStarted ContainerStatus = iota
ContainerStatus_Running
ContainerStatus_Deleted
)
type TestRunnerContainer struct {
ReplaceEntrypoint bool
name string
image string
workdir string
privileged bool
env map[string]string
volumes map[string]string
ports map[string]string
networks []string
results map[string]string
executor cliWrappers.CliExecutorInterface
containerStatus ContainerStatus
}
// ContainerOption is a functional-style option for configuring TestRunnerContainer.
type ContainerOption func(*TestRunnerContainer)
func NewTestRunnerContainer(name, image string, opts ...ContainerOption) *TestRunnerContainer {
container := &TestRunnerContainer{
ReplaceEntrypoint: true,
executor: cliWrappers.NewCliExecutor(),
name: name,
image: image,
env: make(map[string]string),
volumes: make(map[string]string),
ports: make(map[string]string),
results: make(map[string]string),
}
for _, opt := range opts {
opt(container)
}
return container
}
// NewBuildCliRunnerContainer creates NewTestRunnerContainer
// with additional settings for running the Build CLI.
func NewBuildCliRunnerContainer(name, image string, opts ...ContainerOption) *TestRunnerContainer {
container := NewTestRunnerContainer(name, image, opts...)
container.AddVolumeWithOptions(GetCliBinPath(), path.Join("/usr/bin/", KonfluxBuildCli), "z")
container.AddNetwork("host")
if Debug {
container.AddPort("2345", "2345")
}
container.AddEnv("KBC_LOG_LEVEL", "debug")
// On macOS, containers run in a Linux VM; overlay storage driver
// doesn't work reliably with host volume mounts through the VM
if runtime.GOOS == "darwin" {
container.AddEnv("STORAGE_DRIVER", "vfs")
}
return container
}
func (c *TestRunnerContainer) ensureContainerNotStarted() {
if c.containerStatus != ContainerStatus_NotStarted {
panic("the operation can be done only before the container is started")
}
}
func (c *TestRunnerContainer) ensureContainerRunning() {
if c.containerStatus != ContainerStatus_Running {
panic("the operation can be done only on running container")
}
}
func (c *TestRunnerContainer) SetWorkdir(workdir string) {
c.ensureContainerNotStarted()
c.workdir = workdir
}
func WithWorkdir(workdir string) ContainerOption {
return func(c *TestRunnerContainer) {
c.SetWorkdir(workdir)
}
}
func (c *TestRunnerContainer) AddEnv(key, value string) {
c.ensureContainerNotStarted()
c.env[key] = value
}
func WithEnv(key, value string) ContainerOption {
return func(c *TestRunnerContainer) {
c.AddEnv(key, value)
}
}
func (c *TestRunnerContainer) AddVolume(hostPath, containerPath string) {
c.ensureContainerNotStarted()
c.volumes[hostPath] = containerPath
}
func WithVolume(hostPath, containerPath string) ContainerOption {
return func(c *TestRunnerContainer) {
c.AddVolume(hostPath, containerPath)
}
}
func (c *TestRunnerContainer) AddVolumeWithOptions(hostPath, containerPath, mountOptions string) {
c.ensureContainerNotStarted()
c.volumes[hostPath] = containerPath + ":" + mountOptions
}
func WithVolumeWithOptions(hostPath, containerPath, mountOptions string) ContainerOption {
return func(c *TestRunnerContainer) {
c.AddVolumeWithOptions(hostPath, containerPath, mountOptions)
}
}
func (c *TestRunnerContainer) AddPort(hostPort, containerPort string) {
c.ensureContainerNotStarted()
c.ports[hostPort] = containerPort
}
func WithPort(hostPort, containerPort string) ContainerOption {
return func(c *TestRunnerContainer) {
c.AddPort(hostPort, containerPort)
}
}
func (c *TestRunnerContainer) AddNetwork(networkName string) {
c.ensureContainerNotStarted()
c.networks = append(c.networks, networkName)
}
func WithNetwork(networkName string) ContainerOption {
return func(c *TestRunnerContainer) {
c.AddNetwork(networkName)
}
}
// ContainerExists checks for container with the same name.
func (c *TestRunnerContainer) ContainerExists(isRunning bool) (bool, error) {
args := []string{"ps", "-q"}
if !isRunning {
args = append(args, "-a")
}
args = append(args, "-f", "name="+c.name)
stdout, stderr, _, err := c.executor.Execute(containerTool, args...)
if err != nil {
l.Logger.Infof("[stdout]:\n%s\n", stdout)
l.Logger.Infof("[stderr]:\n%s\n", stderr)
return false, err
}
return len(stdout) > 0, nil
}
func (c *TestRunnerContainer) ensureDoesNotExist() error {
existRunning, err := c.ContainerExists(true)
if err != nil {
return err
}
if existRunning {
return c.Delete()
}
existStopped, err := c.ContainerExists(false)
if err != nil {
return err
}
if existStopped {
return c.Delete()
}
return nil
}
func (c *TestRunnerContainer) Start() error {
if err := c.ensureDoesNotExist(); err != nil {
return err
}
args := []string{"run", "--detach", "--name", c.name}
for name, value := range c.env {
args = append(args, "-e", name+"="+value)
}
for hostPath, containerPath := range c.volumes {
args = append(args, "-v", hostPath+":"+containerPath)
}
for hostPort, containerPort := range c.ports {
args = append(args, "-p", hostPort+":"+containerPort)
}
for _, network := range c.networks {
args = append(args, "--network", network)
}
if c.workdir != "" {
args = append(args, "--workdir", c.workdir)
}
if c.privileged {
args = append(args, "--privileged")
}
if c.ReplaceEntrypoint {
args = append(args, "--entrypoint", "sleep", c.image, "infinity")
} else {
args = append(args, c.image)
}
stdout, stderr, _, err := c.executor.Execute(containerTool, args...)
if err != nil {
l.Logger.Infof("[stdout]:\n%s\n", stdout)
l.Logger.Infof("[stderr]:\n%s\n", stderr)
}
c.containerStatus = ContainerStatus_Running
return err
}
// Start the container while injecting the certificates and credentials required to access
// the image registry.
//
// Note that the method may fail to start the container but may also fail *after*
// starting the container. Use the DeleteIfExists() method for cleanup to handle either case.
func (c *TestRunnerContainer) StartWithRegistryIntegration(imageRegistry ImageRegistry) error {
if imageRegistry.IsLocal() {
c.AddVolumeWithOptions(imageRegistry.GetCaCertPath(), "/etc/pki/tls/certs/ca-custom-bundle.crt", "z")
}
err := c.Start()
if err != nil {
return err
}
login, password := imageRegistry.GetCredentials()
return c.InjectDockerAuth(imageRegistry.GetRegistryDomain(), login, password)
}
func (c *TestRunnerContainer) Delete() error {
args := []string{"rm", "-f", c.name}
if containerTool != "docker" {
// By default, podman sends SIGTERM first and, if the container doesn't stop within
// 10 seconds, sends SIGKILL. Docker always sends SIGKILL right away and doesn't have
// a --time flag. Do the same thing for podman, for consistency and to speed up tests.
args = append(args, "--time=0")
}
stdout, stderr, _, err := c.executor.Execute(containerTool, args...)
if err == nil {
c.containerStatus = ContainerStatus_Deleted
} else {
l.Logger.Infof("[stdout]:\n%s\n", stdout)
l.Logger.Infof("[stderr]:\n%s\n", stderr)
}
return err
}
func (c *TestRunnerContainer) DeleteIfExists() error {
if c.containerStatus != ContainerStatus_Running {
return nil
}
return c.Delete()
}
func (c *TestRunnerContainer) CopyFileIntoContainer(hostPath, containerPath string) error {
c.ensureContainerRunning()
stdout, stderr, _, err := c.executor.Execute(containerTool, "cp", hostPath, c.name+":"+containerPath)
if err != nil {
l.Logger.Infof("[stdout]:\n%s\n", stdout)
l.Logger.Infof("[stderr]:\n%s\n", stderr)
}
return err
}
// GetFileContent reads file inside the container.
func (c *TestRunnerContainer) GetFileContent(path string) (string, error) {
c.ensureContainerRunning()
stdout, stderr, _, err := c.executor.Execute(containerTool, "exec", c.name, "cat", path)
if err != nil {
l.Logger.Infof("[stdout]:\n%s\n", stdout)
l.Logger.Infof("[stderr]:\n%s\n", stderr)
if strings.Contains(stderr, "No such file or directory") {
return "", fmt.Errorf("no such file or directory: '%s'", path)
}
return "", err
}
return stdout, nil
}
func (c *TestRunnerContainer) ExecuteBuildCli(args ...string) error {
if Debug {
return c.debugBuildCli(args...)
}
return c.ExecuteCommand(KonfluxBuildCli, args...)
}
// ExecuteCommandWithOutput executes a command in the container and returns
// stdout, stderr, and error.
func (c *TestRunnerContainer) ExecuteCommandWithOutput(command string, args ...string) (string, string, error) {
c.ensureContainerRunning()
execArgs := []string{"exec", "-t", c.name}
execArgs = append(execArgs, command)
execArgs = append(execArgs, args...)
stdout, stderr, _, err := c.executor.ExecuteWithOutput(containerTool, execArgs...)
if err != nil {
l.Logger.Infof("[stdout]:\n%s\n", stdout)
l.Logger.Infof("[stderr]:\n%s\n", stderr)
}
return stdout, stderr, err
}
func (c *TestRunnerContainer) ExecuteCommand(command string, args ...string) error {
_, _, err := c.ExecuteCommandWithOutput(command, args...)
return err
}
func (c *TestRunnerContainer) debugBuildCli(cliArgs ...string) error {
c.ensureContainerRunning()
dlvPath, err := getDlvPath()
if err != nil {
return err
}
err = c.CopyFileIntoContainer(dlvPath, "/usr/bin/")
if err != nil {
return err
}
execArgs := []string{"exec", "-t", c.name}
execArgs = append(execArgs, "dlv", "--listen=0.0.0.0:2345", "--headless=true", "--log=true", "--api-version=2", "exec", "/usr/bin/"+KonfluxBuildCli)
if len(cliArgs) > 0 {
execArgs = append(execArgs, "--")
execArgs = append(execArgs, cliArgs...)
}
stdout, stderr, _, err := c.executor.ExecuteWithOutput(containerTool, execArgs...)
if err != nil {
l.Logger.Infof("[stdout]:\n%s\n", stdout)
l.Logger.Infof("[stderr]:\n%s\n", stderr)
}
return err
}
func getDlvPath() (string, error) {
goPath, isSet := os.LookupEnv("GOPATH")
if !isSet {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
goPath = path.Join(homeDir, "go")
}
dlvPath := path.Join(goPath, "bin", "dlv")
if !FileExists(dlvPath) {
return "", fmt.Errorf("dlv is not found")
}
return dlvPath, nil
}
// GetTaskResultValue returns result file content from container.
func (c *TestRunnerContainer) GetTaskResultValue(resultFilePath string) (string, error) {
resultValue, err := c.GetFileContent(resultFilePath)
if err != nil {
if strings.Contains(strings.ToLower(err.Error()), "no such file or directory") {
return "", fmt.Errorf("result file '%s' is not created", resultFilePath)
}
return "", err
}
return resultValue, nil
}
func (c *TestRunnerContainer) GetHomeDir() (string, error) {
execCmd := []string{"exec", "-t", c.name, "bash", "-c", "echo -n $HOME"}
homeDir, _, _, err := c.executor.Execute(containerTool, execCmd...)
if err != nil {
return "", err
}
return homeDir, nil
}
func (c *TestRunnerContainer) InjectDockerAuth(registry, login, password string) error {
c.ensureContainerRunning()
authContent, err := GenerateDockerAuthContent(registry, login, password)
if err != nil {
return err
}
filePath, err := SaveToTempFile(authContent)
if err != nil {
return err
}
defer func() { os.Remove(filePath) }()
homeDir, err := c.GetHomeDir()
if err != nil {
return err
}
dockerDir := filepath.Join(homeDir, ".docker")
if err := c.ExecuteCommand("mkdir", "-p", dockerDir); err != nil {
return err
}
if err := c.CopyFileIntoContainer(filePath, path.Join(dockerDir, "config.json")); err != nil {
return err
}
return nil
}