blob: 76d08c7d2d6ab7c1354ae1923c178ea767e731c8 [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()
Filip Tehlar109f3ce2023-09-05 15:36:28 +0200132 if *vppSourceFileDir != "" {
133 args += fmt.Sprintf(" -v %s:%s", *vppSourceFileDir, *vppSourceFileDir)
134 }
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100135 args += " --name " + c.name + " " + c.image
Filip Tehlar9abba112023-03-07 10:13:19 +0100136 args += " " + c.extraRunningArgs
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100137 return args
138}
139
Filip Tehlar9abba112023-03-07 10:13:19 +0100140func (c *Container) create() error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100141 cmd := "docker create " + c.getContainerArguments()
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100142 c.suite.log(cmd)
Filip Tehlar9abba112023-03-07 10:13:19 +0100143 return exechelper.Run(cmd)
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100144}
145
Filip Tehlar9abba112023-03-07 10:13:19 +0100146func (c *Container) start() error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100147 cmd := "docker start " + c.name
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100148 c.suite.log(cmd)
Filip Tehlar9abba112023-03-07 10:13:19 +0100149 return exechelper.Run(cmd)
Maros Ondrejicka87531802022-12-19 20:35:27 +0100150}
151
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100152func (c *Container) prepareCommand() (string, error) {
Maros Ondrejicka87531802022-12-19 20:35:27 +0100153 if c.name == "" {
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100154 return "", fmt.Errorf("run container failed: name is blank")
Maros Ondrejicka87531802022-12-19 20:35:27 +0100155 }
156
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100157 cmd := "docker run "
158 if c.runDetached {
159 cmd += " -d"
160 }
161 cmd += " " + c.getContainerArguments()
162
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100163 c.suite.log(cmd)
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100164 return cmd, nil
165}
166
167func (c *Container) combinedOutput() (string, error) {
168 cmd, err := c.prepareCommand()
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100169 if err != nil {
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100170 return "", err
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100171 }
172
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100173 byteOutput, err := exechelper.CombinedOutput(cmd)
174 return string(byteOutput), err
175}
176
177func (c *Container) run() error {
178 cmd, err := c.prepareCommand()
179 if err != nil {
180 return err
181 }
182
183 return exechelper.Run(cmd)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100184}
185
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100186func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100187 var volume Volume
188 volume.hostDir = hostDir
189 volume.containerDir = containerDir
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100190 volume.isDefaultWorkDir = isDefaultWorkDir
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100191 c.volumes[hostDir] = volume
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100192}
193
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100194func (c *Container) getVolumesAsCliOption() string {
195 cliOption := ""
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100196
197 if len(c.volumes) > 0 {
198 for _, volume := range c.volumes {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100199 cliOption += fmt.Sprintf(" -v %s:%s", volume.hostDir, volume.containerDir)
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100200 }
201 }
202
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100203 return cliOption
204}
205
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100206func (c *Container) addEnvVar(name string, value string) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100207 c.envVars[name] = value
208}
209
210func (c *Container) getEnvVarsAsCliOption() string {
211 cliOption := ""
212 if len(c.envVars) == 0 {
213 return cliOption
214 }
215
216 for name, value := range c.envVars {
217 cliOption += fmt.Sprintf(" -e %s=%s", name, value)
218 }
219 return cliOption
220}
221
Filip Tehlar608d0062023-04-28 10:29:47 +0200222func (c *Container) newVppInstance(cpus []int, additionalConfigs ...Stanza) (*VppInstance, error) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100223 vpp := new(VppInstance)
224 vpp.container = c
Filip Tehlar608d0062023-04-28 10:29:47 +0200225 vpp.cpus = cpus
226 vpp.additionalConfig = append(vpp.additionalConfig, additionalConfigs...)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100227 c.vppInstance = vpp
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100228 return vpp, nil
229}
230
231func (c *Container) copy(sourceFileName string, targetFileName string) error {
232 cmd := exec.Command("docker", "cp", sourceFileName, c.name+":"+targetFileName)
233 return cmd.Run()
234}
235
236func (c *Container) createFile(destFileName string, content string) error {
237 f, err := os.CreateTemp("/tmp", "hst-config")
238 if err != nil {
239 return err
240 }
241 defer os.Remove(f.Name())
242
243 if _, err := f.Write([]byte(content)); err != nil {
244 return err
245 }
246 if err := f.Close(); err != nil {
247 return err
248 }
249 c.copy(f.Name(), destFileName)
250 return nil
251}
252
253/*
254 * Executes in detached mode so that the started application can continue to run
255 * without blocking execution of test
256 */
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100257func (c *Container) execServer(command string, arguments ...any) {
258 serverCommand := fmt.Sprintf(command, arguments...)
259 containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() +
260 " " + c.name + " " + serverCommand
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100261 c.suite.T().Helper()
262 c.suite.log(containerExecCommand)
263 c.suite.assertNil(exechelper.Run(containerExecCommand))
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100264}
265
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100266func (c *Container) exec(command string, arguments ...any) string {
267 cliCommand := fmt.Sprintf(command, arguments...)
268 containerExecCommand := "docker exec" + c.getEnvVarsAsCliOption() +
269 " " + c.name + " " + cliCommand
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100270 c.suite.T().Helper()
271 c.suite.log(containerExecCommand)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100272 byteOutput, err := exechelper.CombinedOutput(containerExecCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100273 c.suite.assertNil(err)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100274 return string(byteOutput)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100275}
276
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100277func (c *Container) getLogDirPath() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100278 testId := c.suite.getTestId()
279 testName := c.suite.T().Name()
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100280 logDirPath := logDir + testName + "/" + testId + "/"
281
282 cmd := exec.Command("mkdir", "-p", logDirPath)
283 if err := cmd.Run(); err != nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100284 c.suite.T().Fatalf("mkdir error: %v", err)
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100285 }
286
287 return logDirPath
288}
289
290func (c *Container) saveLogs() {
291 cmd := exec.Command("docker", "inspect", "--format='{{.State.Status}}'", c.name)
292 if output, _ := cmd.CombinedOutput(); !strings.Contains(string(output), "running") {
293 return
294 }
295
296 testLogFilePath := c.getLogDirPath() + "container-" + c.name + ".log"
297
298 cmd = exec.Command("docker", "logs", "--details", "-t", c.name)
299 output, err := cmd.CombinedOutput()
300 if err != nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100301 c.suite.T().Fatalf("fetching logs error: %v", err)
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100302 }
303
304 f, err := os.Create(testLogFilePath)
305 if err != nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100306 c.suite.T().Fatalf("file create error: %v", err)
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100307 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100308 fmt.Fprint(f, string(output))
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100309 f.Close()
310}
311
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100312func (c *Container) log() string {
313 cmd := "docker logs " + c.name
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100314 c.suite.log(cmd)
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100315 o, err := exechelper.CombinedOutput(cmd)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100316 c.suite.assertNil(err)
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100317 return string(o)
318}
319
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100320func (c *Container) stop() error {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100321 if c.vppInstance != nil && c.vppInstance.apiChannel != nil {
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100322 c.vppInstance.saveLogs()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100323 c.vppInstance.disconnect()
324 }
325 c.vppInstance = nil
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100326 c.saveLogs()
Florin Corasf4fe0162023-01-17 13:02:51 -0800327 return exechelper.Run("docker stop " + c.name + " -t 0")
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100328}
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100329
330func (c *Container) createConfig(targetConfigName string, templateName string, values any) {
331 template := template.Must(template.ParseFiles(templateName))
332
333 f, err := os.CreateTemp("/tmp/hs-test/", "hst-config")
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100334 c.suite.assertNil(err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100335 defer os.Remove(f.Name())
336
337 err = template.Execute(f, values)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100338 c.suite.assertNil(err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100339
340 err = f.Close()
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100341 c.suite.assertNil(err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100342
343 c.copy(f.Name(), targetConfigName)
344}
Maros Ondrejickaf4ddf162023-03-08 16:01:43 +0100345
346func init() {
347 cmd := exec.Command("mkdir", "-p", logDir)
348 if err := cmd.Run(); err != nil {
349 panic(err)
350 }
351}