blob: fd3aa47b21bf75f51a1c7c3e73f80bc10618b979 [file] [log] [blame]
Maros Ondrejicka11a03e92022-12-01 09:56:37 +01001package main
2
3import (
4 "fmt"
Maros Ondrejickadb823ed2022-12-14 16:30:04 +01005 "os"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +01006 "os/exec"
Maros Ondrejickadb823ed2022-12-14 16:30:04 +01007 "strings"
Maros Ondrejicka85396a52023-02-28 12:49:43 +01008 "text/template"
Maros Ondrejicka11a03e92022-12-01 09:56:37 +01009
10 "github.com/edwarnicke/exechelper"
11)
12
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010013const (
14 logDir string = "/tmp/hs-test/"
15)
16
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010017var (
18 workDir, _ = os.Getwd()
19)
20
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +010021type Volume struct {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010022 hostDir string
23 containerDir string
24 isDefaultWorkDir bool
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +010025}
26
27type Container struct {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010028 suite *HstSuite
Filip Tehlar3f951432023-01-13 21:33:43 +010029 isOptional bool
Filip Tehlarb41b0af2023-03-20 12:39:20 +010030 runDetached bool
Filip Tehlar3f951432023-01-13 21:33:43 +010031 name string
32 image string
Filip Tehlar3f951432023-01-13 21:33:43 +010033 extraRunningArgs string
34 volumes map[string]Volume
35 envVars map[string]string
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010036 vppInstance *VppInstance
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010037}
38
Maros Ondrejickae7625d02023-02-28 16:55:01 +010039func newContainer(yamlInput ContainerConfig) (*Container, error) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010040 containerName := yamlInput["name"].(string)
41 if len(containerName) == 0 {
42 err := fmt.Errorf("container name must not be blank")
43 return nil, err
44 }
45
46 var container = new(Container)
47 container.volumes = make(map[string]Volume)
48 container.envVars = make(map[string]string)
49 container.name = containerName
50
51 if image, ok := yamlInput["image"]; ok {
52 container.image = image.(string)
53 } else {
54 container.image = "hs-test/vpp"
55 }
56
Filip Tehlar3f951432023-01-13 21:33:43 +010057 if args, ok := yamlInput["extra-args"]; ok {
58 container.extraRunningArgs = args.(string)
59 } else {
60 container.extraRunningArgs = ""
61 }
62
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010063 if isOptional, ok := yamlInput["is-optional"]; ok {
64 container.isOptional = isOptional.(bool)
65 } else {
66 container.isOptional = false
67 }
68
Filip Tehlarb41b0af2023-03-20 12:39:20 +010069 if runDetached, ok := yamlInput["run-detached"]; ok {
70 container.runDetached = runDetached.(bool)
71 } else {
72 container.runDetached = true
73 }
74
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010075 if _, ok := yamlInput["volumes"]; ok {
Filip Tehlarf3ee2b62023-01-09 12:07:09 +010076 r := strings.NewReplacer("$HST_DIR", workDir)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010077 for _, volu := range yamlInput["volumes"].([]interface{}) {
78 volumeMap := volu.(ContainerConfig)
79 hostDir := r.Replace(volumeMap["host-dir"].(string))
80 containerDir := volumeMap["container-dir"].(string)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010081 isDefaultWorkDir := false
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010082
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010083 if isDefault, ok := volumeMap["is-default-work-dir"]; ok {
84 isDefaultWorkDir = isDefault.(bool)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010085 }
86
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010087 container.addVolume(hostDir, containerDir, isDefaultWorkDir)
88
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010089 }
90 }
91
92 if _, ok := yamlInput["vars"]; ok {
93 for _, envVar := range yamlInput["vars"].([]interface{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010094 envVarMap := envVar.(ContainerConfig)
95 name := envVarMap["name"].(string)
96 value := envVarMap["value"].(string)
97 container.addEnvVar(name, value)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010098 }
99 }
100 return container, nil
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100101}
102
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100103func (c *Container) getWorkDirVolume() (res Volume, exists bool) {
104 for _, v := range c.volumes {
105 if v.isDefaultWorkDir {
106 res = v
107 exists = true
108 return
109 }
110 }
111 return
112}
113
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100114func (c *Container) getHostWorkDir() (res string) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100115 if v, ok := c.getWorkDirVolume(); ok {
116 res = v.hostDir
117 }
118 return
119}
120
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100121func (c *Container) getContainerWorkDir() (res string) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100122 if v, ok := c.getWorkDirVolume(); ok {
123 res = v.containerDir
124 }
125 return
126}
127
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100128func (c *Container) getContainerArguments() string {
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100129 args := "--ulimit nofile=90000:90000 --cap-add=all --privileged --network host --rm"
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100130 args += c.getVolumesAsCliOption()
131 args += c.getEnvVarsAsCliOption()
132 args += " --name " + c.name + " " + c.image
Filip Tehlar9abba112023-03-07 10:13:19 +0100133 args += " " + c.extraRunningArgs
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100134 return args
135}
136
Filip Tehlar9abba112023-03-07 10:13:19 +0100137func (c *Container) create() error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100138 cmd := "docker create " + c.getContainerArguments()
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100139 c.suite.log(cmd)
Filip Tehlar9abba112023-03-07 10:13:19 +0100140 return exechelper.Run(cmd)
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100141}
142
Filip Tehlar9abba112023-03-07 10:13:19 +0100143func (c *Container) start() error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100144 cmd := "docker start " + c.name
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100145 c.suite.log(cmd)
Filip Tehlar9abba112023-03-07 10:13:19 +0100146 return exechelper.Run(cmd)
Maros Ondrejicka87531802022-12-19 20:35:27 +0100147}
148
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100149func (c *Container) prepareCommand() (string, error) {
Maros Ondrejicka87531802022-12-19 20:35:27 +0100150 if c.name == "" {
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100151 return "", fmt.Errorf("run container failed: name is blank")
Maros Ondrejicka87531802022-12-19 20:35:27 +0100152 }
153
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100154 cmd := "docker run "
155 if c.runDetached {
156 cmd += " -d"
157 }
158 cmd += " " + c.getContainerArguments()
159
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100160 c.suite.log(cmd)
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100161 return cmd, nil
162}
163
164func (c *Container) combinedOutput() (string, error) {
165 cmd, err := c.prepareCommand()
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100166 if err != nil {
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100167 return "", err
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100168 }
169
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100170 byteOutput, err := exechelper.CombinedOutput(cmd)
171 return string(byteOutput), err
172}
173
174func (c *Container) run() error {
175 cmd, err := c.prepareCommand()
176 if err != nil {
177 return err
178 }
179
180 return exechelper.Run(cmd)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100181}
182
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100183func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100184 var volume Volume
185 volume.hostDir = hostDir
186 volume.containerDir = containerDir
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100187 volume.isDefaultWorkDir = isDefaultWorkDir
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100188 c.volumes[hostDir] = volume
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100189}
190
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100191func (c *Container) getVolumesAsCliOption() string {
192 cliOption := ""
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100193
194 if len(c.volumes) > 0 {
195 for _, volume := range c.volumes {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100196 cliOption += fmt.Sprintf(" -v %s:%s", volume.hostDir, volume.containerDir)
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100197 }
198 }
199
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100200 return cliOption
201}
202
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100203func (c *Container) addEnvVar(name string, value string) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100204 c.envVars[name] = value
205}
206
207func (c *Container) getEnvVarsAsCliOption() string {
208 cliOption := ""
209 if len(c.envVars) == 0 {
210 return cliOption
211 }
212
213 for name, value := range c.envVars {
214 cliOption += fmt.Sprintf(" -e %s=%s", name, value)
215 }
216 return cliOption
217}
218
Filip Tehlar608d0062023-04-28 10:29:47 +0200219func (c *Container) newVppInstance(cpus []int, additionalConfigs ...Stanza) (*VppInstance, error) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100220 vpp := new(VppInstance)
221 vpp.container = c
Filip Tehlar608d0062023-04-28 10:29:47 +0200222 vpp.cpus = cpus
223 vpp.additionalConfig = append(vpp.additionalConfig, additionalConfigs...)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100224 c.vppInstance = vpp
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100225 return vpp, nil
226}
227
228func (c *Container) copy(sourceFileName string, targetFileName string) error {
229 cmd := exec.Command("docker", "cp", sourceFileName, c.name+":"+targetFileName)
230 return cmd.Run()
231}
232
233func (c *Container) createFile(destFileName string, content string) error {
234 f, err := os.CreateTemp("/tmp", "hst-config")
235 if err != nil {
236 return err
237 }
238 defer os.Remove(f.Name())
239
240 if _, err := f.Write([]byte(content)); err != nil {
241 return err
242 }
243 if err := f.Close(); err != nil {
244 return err
245 }
246 c.copy(f.Name(), destFileName)
247 return nil
248}
249
250/*
251 * Executes in detached mode so that the started application can continue to run
252 * without blocking execution of test
253 */
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100254func (c *Container) execServer(command string, arguments ...any) {
255 serverCommand := fmt.Sprintf(command, arguments...)
256 containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() +
257 " " + c.name + " " + serverCommand
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100258 c.suite.T().Helper()
259 c.suite.log(containerExecCommand)
260 c.suite.assertNil(exechelper.Run(containerExecCommand))
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100261}
262
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100263func (c *Container) exec(command string, arguments ...any) string {
264 cliCommand := fmt.Sprintf(command, arguments...)
265 containerExecCommand := "docker exec" + c.getEnvVarsAsCliOption() +
266 " " + c.name + " " + cliCommand
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100267 c.suite.T().Helper()
268 c.suite.log(containerExecCommand)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100269 byteOutput, err := exechelper.CombinedOutput(containerExecCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100270 c.suite.assertNil(err)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100271 return string(byteOutput)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100272}
273
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100274func (c *Container) getLogDirPath() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100275 testId := c.suite.getTestId()
276 testName := c.suite.T().Name()
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100277 logDirPath := logDir + testName + "/" + testId + "/"
278
279 cmd := exec.Command("mkdir", "-p", logDirPath)
280 if err := cmd.Run(); err != nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100281 c.suite.T().Fatalf("mkdir error: %v", err)
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100282 }
283
284 return logDirPath
285}
286
287func (c *Container) saveLogs() {
288 cmd := exec.Command("docker", "inspect", "--format='{{.State.Status}}'", c.name)
289 if output, _ := cmd.CombinedOutput(); !strings.Contains(string(output), "running") {
290 return
291 }
292
293 testLogFilePath := c.getLogDirPath() + "container-" + c.name + ".log"
294
295 cmd = exec.Command("docker", "logs", "--details", "-t", c.name)
296 output, err := cmd.CombinedOutput()
297 if err != nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100298 c.suite.T().Fatalf("fetching logs error: %v", err)
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100299 }
300
301 f, err := os.Create(testLogFilePath)
302 if err != nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100303 c.suite.T().Fatalf("file create error: %v", err)
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100304 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100305 fmt.Fprint(f, string(output))
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100306 f.Close()
307}
308
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100309func (c *Container) log() string {
310 cmd := "docker logs " + c.name
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100311 c.suite.log(cmd)
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100312 o, err := exechelper.CombinedOutput(cmd)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100313 c.suite.assertNil(err)
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100314 return string(o)
315}
316
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100317func (c *Container) stop() error {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100318 if c.vppInstance != nil && c.vppInstance.apiChannel != nil {
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100319 c.vppInstance.saveLogs()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100320 c.vppInstance.disconnect()
321 }
322 c.vppInstance = nil
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100323 c.saveLogs()
Florin Corasf4fe0162023-01-17 13:02:51 -0800324 return exechelper.Run("docker stop " + c.name + " -t 0")
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100325}
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100326
327func (c *Container) createConfig(targetConfigName string, templateName string, values any) {
328 template := template.Must(template.ParseFiles(templateName))
329
330 f, err := os.CreateTemp("/tmp/hs-test/", "hst-config")
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100331 c.suite.assertNil(err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100332 defer os.Remove(f.Name())
333
334 err = template.Execute(f, values)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100335 c.suite.assertNil(err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100336
337 err = f.Close()
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100338 c.suite.assertNil(err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100339
340 c.copy(f.Name(), targetConfigName)
341}
Maros Ondrejickaf4ddf162023-03-08 16:01:43 +0100342
343func init() {
344 cmd := exec.Command("mkdir", "-p", logDir)
345 if err := cmd.Run(); err != nil {
346 panic(err)
347 }
348}