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
|
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/edwarnicke/exechelper"
)
type Volume struct {
hostDir string
containerDir string
isDefaultWorkDir bool
}
type Container struct {
suite *HstSuite
isOptional bool
name string
image string
extraRunningArgs string
volumes map[string]Volume
envVars map[string]string
vppInstance *VppInstance
}
func NewContainer(yamlInput ContainerConfig) (*Container, error) {
containerName := yamlInput["name"].(string)
if len(containerName) == 0 {
err := fmt.Errorf("container name must not be blank")
return nil, err
}
var container = new(Container)
container.volumes = make(map[string]Volume)
container.envVars = make(map[string]string)
container.name = containerName
if image, ok := yamlInput["image"]; ok {
container.image = image.(string)
} else {
container.image = "hs-test/vpp"
}
if args, ok := yamlInput["extra-args"]; ok {
container.extraRunningArgs = args.(string)
} else {
container.extraRunningArgs = ""
}
if isOptional, ok := yamlInput["is-optional"]; ok {
container.isOptional = isOptional.(bool)
} else {
container.isOptional = false
}
if _, ok := yamlInput["volumes"]; ok {
r := strings.NewReplacer("$HST_DIR", workDir)
for _, volu := range yamlInput["volumes"].([]interface{}) {
volumeMap := volu.(ContainerConfig)
hostDir := r.Replace(volumeMap["host-dir"].(string))
containerDir := volumeMap["container-dir"].(string)
isDefaultWorkDir := false
if isDefault, ok := volumeMap["is-default-work-dir"]; ok {
isDefaultWorkDir = isDefault.(bool)
}
container.addVolume(hostDir, containerDir, isDefaultWorkDir)
}
}
if _, ok := yamlInput["vars"]; ok {
for _, envVar := range yamlInput["vars"].([]interface{}) {
envVarMap := envVar.(ContainerConfig)
name := envVarMap["name"].(string)
value := envVarMap["value"].(string)
container.addEnvVar(name, value)
}
}
return container, nil
}
func (c *Container) Suite() *HstSuite {
return c.suite
}
func (c *Container) getWorkDirVolume() (res Volume, exists bool) {
for _, v := range c.volumes {
if v.isDefaultWorkDir {
res = v
exists = true
return
}
}
return
}
func (c *Container) GetHostWorkDir() (res string) {
if v, ok := c.getWorkDirVolume(); ok {
res = v.hostDir
}
return
}
func (c *Container) GetContainerWorkDir() (res string) {
if v, ok := c.getWorkDirVolume(); ok {
res = v.containerDir
}
return
}
func (c *Container) getRunCommand() string {
syncPath := fmt.Sprintf(" -v %s:/tmp/sync", c.getSyncPath())
cmd := "docker run --cap-add=all -d --privileged --network host --rm"
cmd += syncPath
cmd += c.getVolumesAsCliOption()
cmd += c.getEnvVarsAsCliOption()
cmd += " --name " + c.name + " " + c.image + " " + c.extraRunningArgs
return cmd
}
func (c *Container) run() error {
if c.name == "" {
return fmt.Errorf("run container failed: name is blank")
}
exechelper.Run(fmt.Sprintf("mkdir -p /tmp/%s/sync", c.name))
cmd := c.getRunCommand()
err := exechelper.Run(cmd)
if err != nil {
return fmt.Errorf("container run failed: %s", err)
}
return nil
}
func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) {
var volume Volume
volume.hostDir = hostDir
volume.containerDir = containerDir
volume.isDefaultWorkDir = isDefaultWorkDir
c.volumes[hostDir] = volume
}
func (c *Container) getVolumeByHostDir(hostDir string) Volume {
return c.volumes[hostDir]
}
func (c *Container) getVolumesAsCliOption() string {
cliOption := ""
if len(c.volumes) > 0 {
for _, volume := range c.volumes {
cliOption += fmt.Sprintf(" -v %s:%s", volume.hostDir, volume.containerDir)
}
}
return cliOption
}
func (c *Container) getWorkDirAsCliOption() string {
if _, ok := c.getWorkDirVolume(); ok {
return fmt.Sprintf(" --workdir=\"%s\"", c.GetContainerWorkDir())
}
return ""
}
func (c *Container) addEnvVar(name string, value string) {
c.envVars[name] = value
}
func (c *Container) getEnvVarsAsCliOption() string {
cliOption := ""
if len(c.envVars) == 0 {
return cliOption
}
for name, value := range c.envVars {
cliOption += fmt.Sprintf(" -e %s=%s", name, value)
}
return cliOption
}
func (c *Container) getSyncPath() string {
return fmt.Sprintf("/tmp/%s/sync", c.name)
}
func (c *Container) newVppInstance(additionalConfig ...Stanza) (*VppInstance, error) {
vppConfig := new(VppConfig)
vppConfig.CliSocketFilePath = defaultCliSocketFilePath
if len(additionalConfig) > 0 {
vppConfig.additionalConfig = additionalConfig[0]
}
vpp := new(VppInstance)
vpp.container = c
vpp.config = vppConfig
c.vppInstance = vpp
return vpp, nil
}
func (c *Container) copy(sourceFileName string, targetFileName string) error {
cmd := exec.Command("docker", "cp", sourceFileName, c.name+":"+targetFileName)
return cmd.Run()
}
func (c *Container) createFile(destFileName string, content string) error {
f, err := os.CreateTemp("/tmp", "hst-config")
if err != nil {
return err
}
defer os.Remove(f.Name())
if _, err := f.Write([]byte(content)); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
c.copy(f.Name(), destFileName)
return nil
}
/*
* Executes in detached mode so that the started application can continue to run
* without blocking execution of test
*/
func (c *Container) execServer(command string, arguments ...any) {
serverCommand := fmt.Sprintf(command, arguments...)
containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() +
" " + c.name + " " + serverCommand
c.Suite().log(containerExecCommand)
c.Suite().assertNil(exechelper.Run(containerExecCommand))
}
func (c *Container) exec(command string, arguments ...any) string {
cliCommand := fmt.Sprintf(command, arguments...)
containerExecCommand := "docker exec" + c.getEnvVarsAsCliOption() +
" " + c.name + " " + cliCommand
c.Suite().log(containerExecCommand)
byteOutput, err := exechelper.CombinedOutput(containerExecCommand)
c.Suite().assertNil(err)
return string(byteOutput)
}
func (c *Container) execAction(args string) (string, error) {
syncFile := c.getSyncPath() + "/rc"
os.Remove(syncFile)
workDir := c.getWorkDirAsCliOption()
cmd := fmt.Sprintf("docker exec -d %s %s hs-test %s",
workDir,
c.name,
args)
err := exechelper.Run(cmd)
if err != nil {
return "", err
}
res, err := waitForSyncFile(syncFile)
if err != nil {
return "", fmt.Errorf("failed to read sync file while executing 'hs-test %s': %v", args, err)
}
o := res.StdOutput + res.ErrOutput
if res.Code != 0 {
return o, fmt.Errorf("cmd resulted in non-zero value %d: %s", res.Code, res.Desc)
}
return o, err
}
func (c *Container) stop() error {
if c.vppInstance != nil && c.vppInstance.apiChannel != nil {
c.vppInstance.disconnect()
}
c.vppInstance = nil
return exechelper.Run("docker stop " + c.name + " -t 0")
}
|