blob: 080eb736ad40c34a4b6c5910bb54b72774e2bad6 [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 Villin637edda2024-05-06 06:55:34 -040081 workingVolumeDir := logDir + CurrentSpecReport().LeafNodeText + 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 {
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100135 args := "--ulimit nofile=90000:90000 --cap-add=all --privileged --network host --rm"
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100136 args += c.getVolumesAsCliOption()
137 args += c.getEnvVarsAsCliOption()
Filip Tehlar109f3ce2023-09-05 15:36:28 +0200138 if *vppSourceFileDir != "" {
139 args += fmt.Sprintf(" -v %s:%s", *vppSourceFileDir, *vppSourceFileDir)
140 }
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100141 args += " --name " + c.name + " " + c.image
Filip Tehlar9abba112023-03-07 10:13:19 +0100142 args += " " + c.extraRunningArgs
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100143 return args
144}
145
Filip Tehlar4fa0ba62023-12-06 11:35:11 +0100146func (c *Container) runWithRetry(cmd string) error {
147 nTries := 5
148 for i := 0; i < nTries; i++ {
149 err := exechelper.Run(cmd)
150 if err == nil {
151 return nil
152 }
153 time.Sleep(1 * time.Second)
154 }
155 return fmt.Errorf("failed to run container command")
156}
157
Filip Tehlar9abba112023-03-07 10:13:19 +0100158func (c *Container) create() error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100159 cmd := "docker create " + c.getContainerArguments()
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100160 c.suite.log(cmd)
Filip Tehlar9abba112023-03-07 10:13:19 +0100161 return exechelper.Run(cmd)
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100162}
163
Adrian Villinb9464cd2024-05-27 09:52:59 -0400164func (c *Container) allocateCpus() {
Adrian Villinfd366b42024-05-31 06:46:52 -0400165 c.suite.startedContainers = append(c.suite.startedContainers, c)
Adrian Villinb9464cd2024-05-27 09:52:59 -0400166 c.allocatedCpus = c.suite.AllocateCpus()
167 c.suite.log("Allocated CPUs " + fmt.Sprint(c.allocatedCpus) + " to container " + c.name)
168}
169
Filip Tehlar9abba112023-03-07 10:13:19 +0100170func (c *Container) start() error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100171 cmd := "docker start " + c.name
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100172 c.suite.log(cmd)
Filip Tehlar4fa0ba62023-12-06 11:35:11 +0100173 return c.runWithRetry(cmd)
Maros Ondrejicka87531802022-12-19 20:35:27 +0100174}
175
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100176func (c *Container) prepareCommand() (string, error) {
Maros Ondrejicka87531802022-12-19 20:35:27 +0100177 if c.name == "" {
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100178 return "", fmt.Errorf("run container failed: name is blank")
Maros Ondrejicka87531802022-12-19 20:35:27 +0100179 }
180
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100181 cmd := "docker run "
182 if c.runDetached {
Adrian Villin7ad37b52024-06-05 05:53:50 -0400183 cmd += " -dt"
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100184 }
Adrian Villinb9464cd2024-05-27 09:52:59 -0400185
186 c.allocateCpus()
187 cmd += fmt.Sprintf(" --cpuset-cpus=\"%d-%d\"", c.allocatedCpus[0], c.allocatedCpus[len(c.allocatedCpus)-1])
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 Villincee15aa2024-03-14 11:42:55 -0400263 f, err := os.CreateTemp("/tmp", "hst-config"+c.suite.pid)
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 Villincee15aa2024-03-14 11:42:55 -0400305 testName := CurrentSpecReport().LeafNodeText
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() {
317 cmd := exec.Command("docker", "inspect", "--format='{{.State.Status}}'", c.name)
318 if output, _ := cmd.CombinedOutput(); !strings.Contains(string(output), "running") {
319 return
320 }
321
322 testLogFilePath := c.getLogDirPath() + "container-" + c.name + ".log"
323
324 cmd = exec.Command("docker", "logs", "--details", "-t", c.name)
325 output, err := cmd.CombinedOutput()
326 if err != nil {
Adrian Villincee15aa2024-03-14 11:42:55 -0400327 Fail("fetching logs error: " + fmt.Sprint(err))
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100328 }
329
330 f, err := os.Create(testLogFilePath)
331 if err != nil {
Adrian Villincee15aa2024-03-14 11:42:55 -0400332 Fail("file create error: " + fmt.Sprint(err))
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100333 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100334 fmt.Fprint(f, string(output))
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100335 f.Close()
336}
337
adrianvillin7c675472024-02-12 02:44:53 -0500338// Outputs logs from docker containers. Set 'maxLines' to 0 to output the full log.
339func (c *Container) log(maxLines int) (string, error) {
340 var cmd string
341 if maxLines == 0 {
342 cmd = "docker logs " + c.name
343 } else {
344 cmd = fmt.Sprintf("docker logs --tail %d %s", maxLines, c.name)
345 }
346
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100347 c.suite.log(cmd)
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100348 o, err := exechelper.CombinedOutput(cmd)
adrianvillin7c675472024-02-12 02:44:53 -0500349 return string(o), err
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100350}
351
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100352func (c *Container) stop() error {
Adrian Villin46d66002024-05-15 04:33:41 -0400353 if c.vppInstance != nil && c.vppInstance.apiStream != nil {
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100354 c.vppInstance.saveLogs()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100355 c.vppInstance.disconnect()
356 }
357 c.vppInstance = nil
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100358 c.saveLogs()
Florin Corasf4fe0162023-01-17 13:02:51 -0800359 return exechelper.Run("docker stop " + c.name + " -t 0")
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100360}
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100361
362func (c *Container) createConfig(targetConfigName string, templateName string, values any) {
363 template := template.Must(template.ParseFiles(templateName))
364
Filip Tehlara1bd50c2024-01-24 11:59:44 +0100365 f, err := os.CreateTemp(logDir, "hst-config")
adrianvillin7c675472024-02-12 02:44:53 -0500366 c.suite.assertNil(err, err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100367 defer os.Remove(f.Name())
368
369 err = template.Execute(f, values)
adrianvillin7c675472024-02-12 02:44:53 -0500370 c.suite.assertNil(err, err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100371
372 err = f.Close()
adrianvillin7c675472024-02-12 02:44:53 -0500373 c.suite.assertNil(err, err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100374
375 c.copy(f.Name(), targetConfigName)
376}
Maros Ondrejickaf4ddf162023-03-08 16:01:43 +0100377
378func init() {
379 cmd := exec.Command("mkdir", "-p", logDir)
380 if err := cmd.Run(); err != nil {
381 panic(err)
382 }
383}