5
5
"context"
6
6
"encoding/json"
7
7
"fmt"
8
- "io"
9
8
"os/exec"
10
9
"regexp"
11
10
"strings"
@@ -18,25 +17,15 @@ import (
18
17
"golang.org/x/xerrors"
19
18
)
20
19
21
- var (
22
- _ runnable = & ContainerRunner {}
23
- _ runnable = & HostRunner {}
24
- )
25
-
26
- type runnable interface {
27
- Run (ctx context.Context , command string ) * Assertable
28
- RunCmd (cmd * exec.Cmd ) * Assertable
29
- io.Closer
30
- }
31
-
32
20
// ContainerConfig describes the ContainerRunner configuration schema for initializing a testing environment
33
21
type ContainerConfig struct {
34
22
Name string
35
23
Image string
36
24
BindMounts map [string ]string
37
25
}
38
26
39
- func mountArgs (m map [string ]string ) (args []string ) {
27
+ func mountArgs (m map [string ]string ) []string {
28
+ args := make ([]string , 0 , len (m ))
40
29
for src , dest := range m {
41
30
args = append (args , "--mount" , fmt .Sprintf ("type=bind,source=%s,target=%s" , src , dest ))
42
31
}
@@ -54,7 +43,6 @@ func preflightChecks() error {
54
43
// ContainerRunner specifies a runtime container for performing command tests
55
44
type ContainerRunner struct {
56
45
name string
57
- ctx context.Context
58
46
}
59
47
60
48
// NewContainerRunner starts a new docker container for executing command tests
@@ -83,13 +71,15 @@ func NewContainerRunner(ctx context.Context, config *ContainerConfig) (*Containe
83
71
84
72
return & ContainerRunner {
85
73
name : config .Name ,
86
- ctx : ctx ,
87
74
}, nil
88
75
}
89
76
90
77
// Close kills and removes the command execution testing container
91
78
func (r * ContainerRunner ) Close () error {
92
- cmd := exec .CommandContext (r .ctx ,
79
+ ctx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
80
+ defer cancel ()
81
+
82
+ cmd := exec .CommandContext (ctx ,
93
83
"sh" , "-c" , strings .Join ([]string {
94
84
"docker" , "kill" , r .name , "&&" ,
95
85
"docker" , "rm" , r .name ,
0 commit comments