blob: 812198d62db834bd3070c66d18f225d738e5d6ef [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"
12)
13
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010014const (
15 logDir string = "/tmp/hs-test/"
16)
17
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010018var (
19 workDir, _ = os.Getwd()
20)
21
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +010022type Volume struct {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010023 hostDir string
24 containerDir string
25 isDefaultWorkDir bool
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +010026}
27
28type Container struct {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010029 suite *HstSuite
Filip Tehlar3f951432023-01-13 21:33:43 +010030 isOptional bool
Filip Tehlarb41b0af2023-03-20 12:39:20 +010031 runDetached bool
Filip Tehlar3f951432023-01-13 21:33:43 +010032 name string
33 image string
Filip Tehlar3f951432023-01-13 21:33:43 +010034 extraRunningArgs string
35 volumes map[string]Volume
36 envVars map[string]string
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010037 vppInstance *VppInstance
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010038}
39
Maros Ondrejickae7625d02023-02-28 16:55:01 +010040func newContainer(yamlInput ContainerConfig) (*Container, error) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010041 containerName := yamlInput["name"].(string)
42 if len(containerName) == 0 {
43 err := fmt.Errorf("container name must not be blank")
44 return nil, err
45 }
46
47 var container = new(Container)
48 container.volumes = make(map[string]Volume)
49 container.envVars = make(map[string]string)
50 container.name = containerName
51
52 if image, ok := yamlInput["image"]; ok {
53 container.image = image.(string)
54 } else {
55 container.image = "hs-test/vpp"
56 }
57
Filip Tehlar3f951432023-01-13 21:33:43 +010058 if args, ok := yamlInput["extra-args"]; ok {
59 container.extraRunningArgs = args.(string)
60 } else {
61 container.extraRunningArgs = ""
62 }
63
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010064 if isOptional, ok := yamlInput["is-optional"]; ok {
65 container.isOptional = isOptional.(bool)
66 } else {
67 container.isOptional = false
68 }
69
Filip Tehlarb41b0af2023-03-20 12:39:20 +010070 if runDetached, ok := yamlInput["run-detached"]; ok {
71 container.runDetached = runDetached.(bool)
72 } else {
73 container.runDetached = true
74 }
75
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010076 if _, ok := yamlInput["volumes"]; ok {
Filip Tehlarf3ee2b62023-01-09 12:07:09 +010077 r := strings.NewReplacer("$HST_DIR", workDir)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010078 for _, volu := range yamlInput["volumes"].([]interface{}) {
79 volumeMap := volu.(ContainerConfig)
80 hostDir := r.Replace(volumeMap["host-dir"].(string))
81 containerDir := volumeMap["container-dir"].(string)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010082 isDefaultWorkDir := false
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010083
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010084 if isDefault, ok := volumeMap["is-default-work-dir"]; ok {
85 isDefaultWorkDir = isDefault.(bool)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010086 }
87
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010088 container.addVolume(hostDir, containerDir, isDefaultWorkDir)
89
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010090 }
91 }
92
93 if _, ok := yamlInput["vars"]; ok {
94 for _, envVar := range yamlInput["vars"].([]interface{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010095 envVarMap := envVar.(ContainerConfig)
96 name := envVarMap["name"].(string)
97 value := envVarMap["value"].(string)
98 container.addEnvVar(name, value)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010099 }
100 }
101 return container, nil
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100102}
103
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100104func (c *Container) getWorkDirVolume() (res Volume, exists bool) {
105 for _, v := range c.volumes {
106 if v.isDefaultWorkDir {
107 res = v
108 exists = true
109 return
110 }
111 }
112 return
113}
114
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100115func (c *Container) getHostWorkDir() (res string) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100116 if v, ok := c.getWorkDirVolume(); ok {
117 res = v.hostDir
118 }
119 return
120}
121
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100122func (c *Container) getContainerWorkDir() (res string) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100123 if v, ok := c.getWorkDirVolume(); ok {
124 res = v.containerDir
125 }
126 return
127}
128
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100129func (c *Container) getContainerArguments() string {
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100130 args := "--ulimit nofile=90000:90000 --cap-add=all --privileged --network host --rm"
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100131 args += c.getVolumesAsCliOption()
132 args += c.getEnvVarsAsCliOption()
Filip Tehlar109f3ce2023-09-05 15:36:28 +0200133 if *vppSourceFileDir != "" {
134 args += fmt.Sprintf(" -v %s:%s", *vppSourceFileDir, *vppSourceFileDir)
135 }
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100136 args += " --name " + c.name + " " + c.image
Filip Tehlar9abba112023-03-07 10:13:19 +0100137 args += " " + c.extraRunningArgs
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100138 return args
139}
140
Filip Tehlar4fa0ba62023-12-06 11:35:11 +0100141func (c *Container) runWithRetry(cmd string) error {
142 nTries := 5
143 for i := 0; i < nTries; i++ {
144 err := exechelper.Run(cmd)
145 if err == nil {
146 return nil
147 }
148 time.Sleep(1 * time.Second)
149 }
150 return fmt.Errorf("failed to run container command")
151}
152
Filip Tehlar9abba112023-03-07 10:13:19 +0100153func (c *Container) create() error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100154 cmd := "docker create " + c.getContainerArguments()
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100155 c.suite.log(cmd)
Filip Tehlar9abba112023-03-07 10:13:19 +0100156 return exechelper.Run(cmd)
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100157}
158
Filip Tehlar9abba112023-03-07 10:13:19 +0100159func (c *Container) start() error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100160 cmd := "docker start " + c.name
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100161 c.suite.log(cmd)
Filip Tehlar4fa0ba62023-12-06 11:35:11 +0100162 return c.runWithRetry(cmd)
Maros Ondrejicka87531802022-12-19 20:35:27 +0100163}
164
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100165func (c *Container) prepareCommand() (string, error) {
Maros Ondrejicka87531802022-12-19 20:35:27 +0100166 if c.name == "" {
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100167 return "", fmt.Errorf("run container failed: name is blank")
Maros Ondrejicka87531802022-12-19 20:35:27 +0100168 }
169
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100170 cmd := "docker run "
171 if c.runDetached {
172 cmd += " -d"
173 }
174 cmd += " " + c.getContainerArguments()
175
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100176 c.suite.log(cmd)
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100177 return cmd, nil
178}
179
180func (c *Container) combinedOutput() (string, error) {
181 cmd, err := c.prepareCommand()
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100182 if err != nil {
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100183 return "", err
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100184 }
185
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100186 byteOutput, err := exechelper.CombinedOutput(cmd)
187 return string(byteOutput), err
188}
189
190func (c *Container) run() error {
191 cmd, err := c.prepareCommand()
192 if err != nil {
193 return err
194 }
Filip Tehlar4fa0ba62023-12-06 11:35:11 +0100195 return c.runWithRetry(cmd)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100196}
197
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100198func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100199 var volume Volume
200 volume.hostDir = hostDir
201 volume.containerDir = containerDir
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100202 volume.isDefaultWorkDir = isDefaultWorkDir
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100203 c.volumes[hostDir] = volume
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100204}
205
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100206func (c *Container) getVolumesAsCliOption() string {
207 cliOption := ""
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100208
209 if len(c.volumes) > 0 {
210 for _, volume := range c.volumes {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100211 cliOption += fmt.Sprintf(" -v %s:%s", volume.hostDir, volume.containerDir)
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100212 }
213 }
214
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100215 return cliOption
216}
217
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100218func (c *Container) addEnvVar(name string, value string) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100219 c.envVars[name] = value
220}
221
222func (c *Container) getEnvVarsAsCliOption() string {
223 cliOption := ""
224 if len(c.envVars) == 0 {
225 return cliOption
226 }
227
228 for name, value := range c.envVars {
229 cliOption += fmt.Sprintf(" -e %s=%s", name, value)
230 }
231 return cliOption
232}
233
Filip Tehlar608d0062023-04-28 10:29:47 +0200234func (c *Container) newVppInstance(cpus []int, additionalConfigs ...Stanza) (*VppInstance, error) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100235 vpp := new(VppInstance)
236 vpp.container = c
Filip Tehlar608d0062023-04-28 10:29:47 +0200237 vpp.cpus = cpus
238 vpp.additionalConfig = append(vpp.additionalConfig, additionalConfigs...)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100239 c.vppInstance = vpp
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100240 return vpp, nil
241}
242
243func (c *Container) copy(sourceFileName string, targetFileName string) error {
244 cmd := exec.Command("docker", "cp", sourceFileName, c.name+":"+targetFileName)
245 return cmd.Run()
246}
247
248func (c *Container) createFile(destFileName string, content string) error {
249 f, err := os.CreateTemp("/tmp", "hst-config")
250 if err != nil {
251 return err
252 }
253 defer os.Remove(f.Name())
254
255 if _, err := f.Write([]byte(content)); err != nil {
256 return err
257 }
258 if err := f.Close(); err != nil {
259 return err
260 }
261 c.copy(f.Name(), destFileName)
262 return nil
263}
264
265/*
266 * Executes in detached mode so that the started application can continue to run
267 * without blocking execution of test
268 */
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100269func (c *Container) execServer(command string, arguments ...any) {
270 serverCommand := fmt.Sprintf(command, arguments...)
271 containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() +
272 " " + c.name + " " + serverCommand
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100273 c.suite.T().Helper()
274 c.suite.log(containerExecCommand)
275 c.suite.assertNil(exechelper.Run(containerExecCommand))
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100276}
277
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100278func (c *Container) exec(command string, arguments ...any) string {
279 cliCommand := fmt.Sprintf(command, arguments...)
280 containerExecCommand := "docker exec" + c.getEnvVarsAsCliOption() +
281 " " + c.name + " " + cliCommand
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100282 c.suite.T().Helper()
283 c.suite.log(containerExecCommand)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100284 byteOutput, err := exechelper.CombinedOutput(containerExecCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100285 c.suite.assertNil(err)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100286 return string(byteOutput)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100287}
288
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100289func (c *Container) getLogDirPath() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100290 testId := c.suite.getTestId()
291 testName := c.suite.T().Name()
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100292 logDirPath := logDir + testName + "/" + testId + "/"
293
294 cmd := exec.Command("mkdir", "-p", logDirPath)
295 if err := cmd.Run(); err != nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100296 c.suite.T().Fatalf("mkdir error: %v", err)
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100297 }
298
299 return logDirPath
300}
301
302func (c *Container) saveLogs() {
303 cmd := exec.Command("docker", "inspect", "--format='{{.State.Status}}'", c.name)
304 if output, _ := cmd.CombinedOutput(); !strings.Contains(string(output), "running") {
305 return
306 }
307
308 testLogFilePath := c.getLogDirPath() + "container-" + c.name + ".log"
309
310 cmd = exec.Command("docker", "logs", "--details", "-t", c.name)
311 output, err := cmd.CombinedOutput()
312 if err != nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100313 c.suite.T().Fatalf("fetching logs error: %v", err)
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100314 }
315
316 f, err := os.Create(testLogFilePath)
317 if err != nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100318 c.suite.T().Fatalf("file create error: %v", err)
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100319 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100320 fmt.Fprint(f, string(output))
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100321 f.Close()
322}
323
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100324func (c *Container) log() string {
325 cmd := "docker logs " + c.name
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100326 c.suite.log(cmd)
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100327 o, err := exechelper.CombinedOutput(cmd)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100328 c.suite.assertNil(err)
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100329 return string(o)
330}
331
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100332func (c *Container) stop() error {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100333 if c.vppInstance != nil && c.vppInstance.apiChannel != nil {
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100334 c.vppInstance.saveLogs()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100335 c.vppInstance.disconnect()
336 }
337 c.vppInstance = nil
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100338 c.saveLogs()
Florin Corasf4fe0162023-01-17 13:02:51 -0800339 return exechelper.Run("docker stop " + c.name + " -t 0")
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100340}
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100341
342func (c *Container) createConfig(targetConfigName string, templateName string, values any) {
343 template := template.Must(template.ParseFiles(templateName))
344
345 f, err := os.CreateTemp("/tmp/hs-test/", "hst-config")
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100346 c.suite.assertNil(err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100347 defer os.Remove(f.Name())
348
349 err = template.Execute(f, values)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100350 c.suite.assertNil(err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100351
352 err = f.Close()
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100353 c.suite.assertNil(err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100354
355 c.copy(f.Name(), targetConfigName)
356}
Maros Ondrejickaf4ddf162023-03-08 16:01:43 +0100357
358func init() {
359 cmd := exec.Command("mkdir", "-p", logDir)
360 if err := cmd.Run(); err != nil {
361 panic(err)
362 }
363}