blob: 97b71b5385fc7ec23260d5195dd66a1789978868 [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"
Filip Tehlar4fa0ba62023-12-06 11:35:11 +01009 "time"
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010010
11 "github.com/edwarnicke/exechelper"
Adrian Villincee15aa2024-03-14 11:42:55 -040012 . "github.com/onsi/ginkgo/v2"
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010013)
14
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010015const (
Filip Tehlara1bd50c2024-01-24 11:59:44 +010016 logDir string = "/tmp/hs-test/"
17 volumeDir string = "/volumes"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010018)
19
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010020var (
21 workDir, _ = os.Getwd()
22)
23
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +010024type Volume struct {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010025 hostDir string
26 containerDir string
27 isDefaultWorkDir bool
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +010028}
29
30type Container struct {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010031 suite *HstSuite
Filip Tehlar3f951432023-01-13 21:33:43 +010032 isOptional bool
Filip Tehlarb41b0af2023-03-20 12:39:20 +010033 runDetached bool
Filip Tehlar3f951432023-01-13 21:33:43 +010034 name string
35 image string
Filip Tehlar3f951432023-01-13 21:33:43 +010036 extraRunningArgs string
37 volumes map[string]Volume
38 envVars map[string]string
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010039 vppInstance *VppInstance
Adrian Villinb9464cd2024-05-27 09:52:59 -040040 allocatedCpus []int
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010041}
42
Adrian Villincee15aa2024-03-14 11:42:55 -040043func newContainer(suite *HstSuite, yamlInput ContainerConfig) (*Container, error) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010044 containerName := yamlInput["name"].(string)
45 if len(containerName) == 0 {
46 err := fmt.Errorf("container name must not be blank")
47 return nil, err
48 }
49
50 var container = new(Container)
51 container.volumes = make(map[string]Volume)
52 container.envVars = make(map[string]string)
Adrian Villincee15aa2024-03-14 11:42:55 -040053 container.name = containerName
Filip Tehlara1bd50c2024-01-24 11:59:44 +010054 container.suite = suite
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010055
56 if image, ok := yamlInput["image"]; ok {
57 container.image = image.(string)
58 } else {
59 container.image = "hs-test/vpp"
60 }
61
Filip Tehlar3f951432023-01-13 21:33:43 +010062 if args, ok := yamlInput["extra-args"]; ok {
63 container.extraRunningArgs = args.(string)
64 } else {
65 container.extraRunningArgs = ""
66 }
67
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010068 if isOptional, ok := yamlInput["is-optional"]; ok {
69 container.isOptional = isOptional.(bool)
70 } else {
71 container.isOptional = false
72 }
73
Filip Tehlarb41b0af2023-03-20 12:39:20 +010074 if runDetached, ok := yamlInput["run-detached"]; ok {
75 container.runDetached = runDetached.(bool)
76 } else {
77 container.runDetached = true
78 }
79
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010080 if _, ok := yamlInput["volumes"]; ok {
Adrian Villineaa7d912024-06-06 04:26:30 -040081 workingVolumeDir := logDir + suite.getCurrentTestName() + volumeDir
Filip Tehlara1bd50c2024-01-24 11:59:44 +010082 workDirReplacer := strings.NewReplacer("$HST_DIR", workDir)
83 volDirReplacer := strings.NewReplacer("$HST_VOLUME_DIR", workingVolumeDir)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010084 for _, volu := range yamlInput["volumes"].([]interface{}) {
85 volumeMap := volu.(ContainerConfig)
Filip Tehlara1bd50c2024-01-24 11:59:44 +010086 hostDir := workDirReplacer.Replace(volumeMap["host-dir"].(string))
87 hostDir = volDirReplacer.Replace(hostDir)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010088 containerDir := volumeMap["container-dir"].(string)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010089 isDefaultWorkDir := false
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010090
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010091 if isDefault, ok := volumeMap["is-default-work-dir"]; ok {
92 isDefaultWorkDir = isDefault.(bool)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010093 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010094 container.addVolume(hostDir, containerDir, isDefaultWorkDir)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010095 }
96 }
97
98 if _, ok := yamlInput["vars"]; ok {
99 for _, envVar := range yamlInput["vars"].([]interface{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100100 envVarMap := envVar.(ContainerConfig)
101 name := envVarMap["name"].(string)
102 value := envVarMap["value"].(string)
103 container.addEnvVar(name, value)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100104 }
105 }
106 return container, nil
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100107}
108
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100109func (c *Container) getWorkDirVolume() (res Volume, exists bool) {
110 for _, v := range c.volumes {
111 if v.isDefaultWorkDir {
112 res = v
113 exists = true
114 return
115 }
116 }
117 return
118}
119
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100120func (c *Container) getHostWorkDir() (res string) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100121 if v, ok := c.getWorkDirVolume(); ok {
122 res = v.hostDir
123 }
124 return
125}
126
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100127func (c *Container) getContainerWorkDir() (res string) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100128 if v, ok := c.getWorkDirVolume(); ok {
129 res = v.containerDir
130 }
131 return
132}
133
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100134func (c *Container) getContainerArguments() string {
Adrian Villineaa7d912024-06-06 04:26:30 -0400135 args := "--ulimit nofile=90000:90000 --cap-add=all --privileged --network host"
136 c.allocateCpus()
137 args += fmt.Sprintf(" --cpuset-cpus=\"%d-%d\"", c.allocatedCpus[0], c.allocatedCpus[len(c.allocatedCpus)-1])
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100138 args += c.getVolumesAsCliOption()
139 args += c.getEnvVarsAsCliOption()
Filip Tehlar109f3ce2023-09-05 15:36:28 +0200140 if *vppSourceFileDir != "" {
141 args += fmt.Sprintf(" -v %s:%s", *vppSourceFileDir, *vppSourceFileDir)
142 }
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100143 args += " --name " + c.name + " " + c.image
Filip Tehlar9abba112023-03-07 10:13:19 +0100144 args += " " + c.extraRunningArgs
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100145 return args
146}
147
Filip Tehlar4fa0ba62023-12-06 11:35:11 +0100148func (c *Container) runWithRetry(cmd string) error {
149 nTries := 5
150 for i := 0; i < nTries; i++ {
151 err := exechelper.Run(cmd)
152 if err == nil {
153 return nil
154 }
155 time.Sleep(1 * time.Second)
156 }
157 return fmt.Errorf("failed to run container command")
158}
159
Filip Tehlar9abba112023-03-07 10:13:19 +0100160func (c *Container) create() error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100161 cmd := "docker create " + c.getContainerArguments()
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100162 c.suite.log(cmd)
Filip Tehlar9abba112023-03-07 10:13:19 +0100163 return exechelper.Run(cmd)
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100164}
165
Adrian Villinb9464cd2024-05-27 09:52:59 -0400166func (c *Container) allocateCpus() {
Adrian Villinfd366b42024-05-31 06:46:52 -0400167 c.suite.startedContainers = append(c.suite.startedContainers, c)
Adrian Villinb9464cd2024-05-27 09:52:59 -0400168 c.allocatedCpus = c.suite.AllocateCpus()
169 c.suite.log("Allocated CPUs " + fmt.Sprint(c.allocatedCpus) + " to container " + c.name)
170}
171
Filip Tehlar9abba112023-03-07 10:13:19 +0100172func (c *Container) start() error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100173 cmd := "docker start " + c.name
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100174 c.suite.log(cmd)
Filip Tehlar4fa0ba62023-12-06 11:35:11 +0100175 return c.runWithRetry(cmd)
Maros Ondrejicka87531802022-12-19 20:35:27 +0100176}
177
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100178func (c *Container) prepareCommand() (string, error) {
Maros Ondrejicka87531802022-12-19 20:35:27 +0100179 if c.name == "" {
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100180 return "", fmt.Errorf("run container failed: name is blank")
Maros Ondrejicka87531802022-12-19 20:35:27 +0100181 }
182
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100183 cmd := "docker run "
184 if c.runDetached {
Adrian Villineaa7d912024-06-06 04:26:30 -0400185 cmd += " -d"
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100186 }
Adrian Villinb9464cd2024-05-27 09:52:59 -0400187
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100188 cmd += " " + c.getContainerArguments()
189
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100190 c.suite.log(cmd)
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100191 return cmd, nil
192}
193
194func (c *Container) combinedOutput() (string, error) {
195 cmd, err := c.prepareCommand()
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100196 if err != nil {
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100197 return "", err
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100198 }
199
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100200 byteOutput, err := exechelper.CombinedOutput(cmd)
201 return string(byteOutput), err
202}
203
204func (c *Container) run() error {
205 cmd, err := c.prepareCommand()
206 if err != nil {
207 return err
208 }
Filip Tehlar4fa0ba62023-12-06 11:35:11 +0100209 return c.runWithRetry(cmd)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100210}
211
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100212func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100213 var volume Volume
214 volume.hostDir = hostDir
215 volume.containerDir = containerDir
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100216 volume.isDefaultWorkDir = isDefaultWorkDir
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100217 c.volumes[hostDir] = volume
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100218}
219
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100220func (c *Container) getVolumesAsCliOption() string {
221 cliOption := ""
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100222
223 if len(c.volumes) > 0 {
224 for _, volume := range c.volumes {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100225 cliOption += fmt.Sprintf(" -v %s:%s", volume.hostDir, volume.containerDir)
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100226 }
227 }
228
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100229 return cliOption
230}
231
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100232func (c *Container) addEnvVar(name string, value string) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100233 c.envVars[name] = value
234}
235
236func (c *Container) getEnvVarsAsCliOption() string {
237 cliOption := ""
238 if len(c.envVars) == 0 {
239 return cliOption
240 }
241
242 for name, value := range c.envVars {
243 cliOption += fmt.Sprintf(" -e %s=%s", name, value)
244 }
245 return cliOption
246}
247
Filip Tehlar608d0062023-04-28 10:29:47 +0200248func (c *Container) newVppInstance(cpus []int, additionalConfigs ...Stanza) (*VppInstance, error) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100249 vpp := new(VppInstance)
250 vpp.container = c
Filip Tehlar608d0062023-04-28 10:29:47 +0200251 vpp.cpus = cpus
252 vpp.additionalConfig = append(vpp.additionalConfig, additionalConfigs...)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100253 c.vppInstance = vpp
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100254 return vpp, nil
255}
256
257func (c *Container) copy(sourceFileName string, targetFileName string) error {
258 cmd := exec.Command("docker", "cp", sourceFileName, c.name+":"+targetFileName)
259 return cmd.Run()
260}
261
262func (c *Container) createFile(destFileName string, content string) error {
Adrian Villineaa7d912024-06-06 04:26:30 -0400263 f, err := os.CreateTemp("/tmp", "hst-config"+c.suite.ppid)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100264 if err != nil {
265 return err
266 }
267 defer os.Remove(f.Name())
268
269 if _, err := f.Write([]byte(content)); err != nil {
270 return err
271 }
272 if err := f.Close(); err != nil {
273 return err
274 }
275 c.copy(f.Name(), destFileName)
276 return nil
277}
278
279/*
280 * Executes in detached mode so that the started application can continue to run
281 * without blocking execution of test
282 */
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100283func (c *Container) execServer(command string, arguments ...any) {
284 serverCommand := fmt.Sprintf(command, arguments...)
285 containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() +
286 " " + c.name + " " + serverCommand
Adrian Villincee15aa2024-03-14 11:42:55 -0400287 GinkgoHelper()
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100288 c.suite.log(containerExecCommand)
289 c.suite.assertNil(exechelper.Run(containerExecCommand))
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100290}
291
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100292func (c *Container) exec(command string, arguments ...any) string {
293 cliCommand := fmt.Sprintf(command, arguments...)
294 containerExecCommand := "docker exec" + c.getEnvVarsAsCliOption() +
295 " " + c.name + " " + cliCommand
Adrian Villincee15aa2024-03-14 11:42:55 -0400296 GinkgoHelper()
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100297 c.suite.log(containerExecCommand)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100298 byteOutput, err := exechelper.CombinedOutput(containerExecCommand)
Adrian Villinb9464cd2024-05-27 09:52:59 -0400299 c.suite.assertNil(err, fmt.Sprint(err))
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100300 return string(byteOutput)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100301}
302
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100303func (c *Container) getLogDirPath() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100304 testId := c.suite.getTestId()
Adrian Villineaa7d912024-06-06 04:26:30 -0400305 testName := c.suite.getCurrentTestName()
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100306 logDirPath := logDir + testName + "/" + testId + "/"
307
308 cmd := exec.Command("mkdir", "-p", logDirPath)
309 if err := cmd.Run(); err != nil {
Adrian Villincee15aa2024-03-14 11:42:55 -0400310 Fail("mkdir error: " + fmt.Sprint(err))
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100311 }
312
313 return logDirPath
314}
315
316func (c *Container) saveLogs() {
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100317 testLogFilePath := c.getLogDirPath() + "container-" + c.name + ".log"
318
Adrian Villineaa7d912024-06-06 04:26:30 -0400319 cmd := exec.Command("docker", "logs", "--details", "-t", c.name)
320 c.suite.log(cmd)
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100321 output, err := cmd.CombinedOutput()
322 if err != nil {
Adrian Villineaa7d912024-06-06 04:26:30 -0400323 c.suite.log(err)
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100324 }
325
326 f, err := os.Create(testLogFilePath)
327 if err != nil {
Adrian Villincee15aa2024-03-14 11:42:55 -0400328 Fail("file create error: " + fmt.Sprint(err))
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100329 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100330 fmt.Fprint(f, string(output))
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100331 f.Close()
332}
333
adrianvillin7c675472024-02-12 02:44:53 -0500334// Outputs logs from docker containers. Set 'maxLines' to 0 to output the full log.
335func (c *Container) log(maxLines int) (string, error) {
336 var cmd string
337 if maxLines == 0 {
338 cmd = "docker logs " + c.name
339 } else {
340 cmd = fmt.Sprintf("docker logs --tail %d %s", maxLines, c.name)
341 }
342
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100343 c.suite.log(cmd)
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100344 o, err := exechelper.CombinedOutput(cmd)
adrianvillin7c675472024-02-12 02:44:53 -0500345 return string(o), err
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100346}
347
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100348func (c *Container) stop() error {
Adrian Villin46d66002024-05-15 04:33:41 -0400349 if c.vppInstance != nil && c.vppInstance.apiStream != nil {
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100350 c.vppInstance.saveLogs()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100351 c.vppInstance.disconnect()
352 }
353 c.vppInstance = nil
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100354 c.saveLogs()
Adrian Villineaa7d912024-06-06 04:26:30 -0400355 c.suite.log("docker stop " + c.name + " -t 0")
Florin Corasf4fe0162023-01-17 13:02:51 -0800356 return exechelper.Run("docker stop " + c.name + " -t 0")
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100357}
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100358
359func (c *Container) createConfig(targetConfigName string, templateName string, values any) {
360 template := template.Must(template.ParseFiles(templateName))
361
Filip Tehlara1bd50c2024-01-24 11:59:44 +0100362 f, err := os.CreateTemp(logDir, "hst-config")
adrianvillin7c675472024-02-12 02:44:53 -0500363 c.suite.assertNil(err, err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100364 defer os.Remove(f.Name())
365
366 err = template.Execute(f, values)
adrianvillin7c675472024-02-12 02:44:53 -0500367 c.suite.assertNil(err, err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100368
369 err = f.Close()
adrianvillin7c675472024-02-12 02:44:53 -0500370 c.suite.assertNil(err, err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100371
372 c.copy(f.Name(), targetConfigName)
373}
Maros Ondrejickaf4ddf162023-03-08 16:01:43 +0100374
375func init() {
376 cmd := exec.Command("mkdir", "-p", logDir)
377 if err := cmd.Run(); err != nil {
378 panic(err)
379 }
380}