blob: 1f600f9fe24a20df1b0510b86bcd23d5442906f3 [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
30 name string
31 image string
Filip Tehlar3f951432023-01-13 21:33:43 +010032 extraRunningArgs string
33 volumes map[string]Volume
34 envVars map[string]string
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010035 vppInstance *VppInstance
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010036}
37
Maros Ondrejickae7625d02023-02-28 16:55:01 +010038func newContainer(yamlInput ContainerConfig) (*Container, error) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010039 containerName := yamlInput["name"].(string)
40 if len(containerName) == 0 {
41 err := fmt.Errorf("container name must not be blank")
42 return nil, err
43 }
44
45 var container = new(Container)
46 container.volumes = make(map[string]Volume)
47 container.envVars = make(map[string]string)
48 container.name = containerName
49
50 if image, ok := yamlInput["image"]; ok {
51 container.image = image.(string)
52 } else {
53 container.image = "hs-test/vpp"
54 }
55
Filip Tehlar3f951432023-01-13 21:33:43 +010056 if args, ok := yamlInput["extra-args"]; ok {
57 container.extraRunningArgs = args.(string)
58 } else {
59 container.extraRunningArgs = ""
60 }
61
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010062 if isOptional, ok := yamlInput["is-optional"]; ok {
63 container.isOptional = isOptional.(bool)
64 } else {
65 container.isOptional = false
66 }
67
68 if _, ok := yamlInput["volumes"]; ok {
Filip Tehlarf3ee2b62023-01-09 12:07:09 +010069 r := strings.NewReplacer("$HST_DIR", workDir)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010070 for _, volu := range yamlInput["volumes"].([]interface{}) {
71 volumeMap := volu.(ContainerConfig)
72 hostDir := r.Replace(volumeMap["host-dir"].(string))
73 containerDir := volumeMap["container-dir"].(string)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010074 isDefaultWorkDir := false
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010075
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010076 if isDefault, ok := volumeMap["is-default-work-dir"]; ok {
77 isDefaultWorkDir = isDefault.(bool)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010078 }
79
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010080 container.addVolume(hostDir, containerDir, isDefaultWorkDir)
81
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010082 }
83 }
84
85 if _, ok := yamlInput["vars"]; ok {
86 for _, envVar := range yamlInput["vars"].([]interface{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010087 envVarMap := envVar.(ContainerConfig)
88 name := envVarMap["name"].(string)
89 value := envVarMap["value"].(string)
90 container.addEnvVar(name, value)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010091 }
92 }
93 return container, nil
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010094}
95
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010096func (c *Container) getWorkDirVolume() (res Volume, exists bool) {
97 for _, v := range c.volumes {
98 if v.isDefaultWorkDir {
99 res = v
100 exists = true
101 return
102 }
103 }
104 return
105}
106
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100107func (c *Container) getHostWorkDir() (res string) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100108 if v, ok := c.getWorkDirVolume(); ok {
109 res = v.hostDir
110 }
111 return
112}
113
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100114func (c *Container) getContainerWorkDir() (res string) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100115 if v, ok := c.getWorkDirVolume(); ok {
116 res = v.containerDir
117 }
118 return
119}
120
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100121func (c *Container) getContainerArguments() string {
122 args := "--cap-add=all --privileged --network host --rm"
123 args += c.getVolumesAsCliOption()
124 args += c.getEnvVarsAsCliOption()
125 args += " --name " + c.name + " " + c.image
Filip Tehlar9abba112023-03-07 10:13:19 +0100126 args += " " + c.extraRunningArgs
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100127 return args
128}
129
Filip Tehlar9abba112023-03-07 10:13:19 +0100130func (c *Container) create() error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100131 cmd := "docker create " + c.getContainerArguments()
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100132 c.suite.log(cmd)
Filip Tehlar9abba112023-03-07 10:13:19 +0100133 return exechelper.Run(cmd)
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100134}
135
Filip Tehlar9abba112023-03-07 10:13:19 +0100136func (c *Container) start() error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100137 cmd := "docker start " + c.name
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100138 c.suite.log(cmd)
Filip Tehlar9abba112023-03-07 10:13:19 +0100139 return exechelper.Run(cmd)
Maros Ondrejicka87531802022-12-19 20:35:27 +0100140}
141
142func (c *Container) run() error {
143 if c.name == "" {
144 return fmt.Errorf("run container failed: name is blank")
145 }
146
Filip Tehlar9abba112023-03-07 10:13:19 +0100147 cmd := "docker run -d " + c.getContainerArguments()
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100148 c.suite.log(cmd)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100149 err := exechelper.Run(cmd)
150 if err != nil {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100151 return fmt.Errorf("container run failed: %s", err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100152 }
153
154 return nil
155}
156
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100157func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100158 var volume Volume
159 volume.hostDir = hostDir
160 volume.containerDir = containerDir
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100161 volume.isDefaultWorkDir = isDefaultWorkDir
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100162 c.volumes[hostDir] = volume
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100163}
164
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100165func (c *Container) getVolumesAsCliOption() string {
166 cliOption := ""
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100167
168 if len(c.volumes) > 0 {
169 for _, volume := range c.volumes {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100170 cliOption += fmt.Sprintf(" -v %s:%s", volume.hostDir, volume.containerDir)
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100171 }
172 }
173
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100174 return cliOption
175}
176
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100177func (c *Container) addEnvVar(name string, value string) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100178 c.envVars[name] = value
179}
180
181func (c *Container) getEnvVarsAsCliOption() string {
182 cliOption := ""
183 if len(c.envVars) == 0 {
184 return cliOption
185 }
186
187 for name, value := range c.envVars {
188 cliOption += fmt.Sprintf(" -e %s=%s", name, value)
189 }
190 return cliOption
191}
192
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100193func (c *Container) newVppInstance(additionalConfig ...Stanza) (*VppInstance, error) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100194 vpp := new(VppInstance)
195 vpp.container = c
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100196
197 if len(additionalConfig) > 0 {
198 vpp.additionalConfig = additionalConfig[0]
199 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100200
201 c.vppInstance = vpp
202
203 return vpp, nil
204}
205
206func (c *Container) copy(sourceFileName string, targetFileName string) error {
207 cmd := exec.Command("docker", "cp", sourceFileName, c.name+":"+targetFileName)
208 return cmd.Run()
209}
210
211func (c *Container) createFile(destFileName string, content string) error {
212 f, err := os.CreateTemp("/tmp", "hst-config")
213 if err != nil {
214 return err
215 }
216 defer os.Remove(f.Name())
217
218 if _, err := f.Write([]byte(content)); err != nil {
219 return err
220 }
221 if err := f.Close(); err != nil {
222 return err
223 }
224 c.copy(f.Name(), destFileName)
225 return nil
226}
227
228/*
229 * Executes in detached mode so that the started application can continue to run
230 * without blocking execution of test
231 */
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100232func (c *Container) execServer(command string, arguments ...any) {
233 serverCommand := fmt.Sprintf(command, arguments...)
234 containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() +
235 " " + c.name + " " + serverCommand
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100236 c.suite.T().Helper()
237 c.suite.log(containerExecCommand)
238 c.suite.assertNil(exechelper.Run(containerExecCommand))
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100239}
240
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100241func (c *Container) exec(command string, arguments ...any) string {
242 cliCommand := fmt.Sprintf(command, arguments...)
243 containerExecCommand := "docker exec" + c.getEnvVarsAsCliOption() +
244 " " + c.name + " " + cliCommand
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100245 c.suite.T().Helper()
246 c.suite.log(containerExecCommand)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100247 byteOutput, err := exechelper.CombinedOutput(containerExecCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100248 c.suite.assertNil(err)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100249 return string(byteOutput)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100250}
251
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100252func (c *Container) getLogDirPath() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100253 testId := c.suite.getTestId()
254 testName := c.suite.T().Name()
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100255 logDirPath := logDir + testName + "/" + testId + "/"
256
257 cmd := exec.Command("mkdir", "-p", logDirPath)
258 if err := cmd.Run(); err != nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100259 c.suite.T().Fatalf("mkdir error: %v", err)
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100260 }
261
262 return logDirPath
263}
264
265func (c *Container) saveLogs() {
266 cmd := exec.Command("docker", "inspect", "--format='{{.State.Status}}'", c.name)
267 if output, _ := cmd.CombinedOutput(); !strings.Contains(string(output), "running") {
268 return
269 }
270
271 testLogFilePath := c.getLogDirPath() + "container-" + c.name + ".log"
272
273 cmd = exec.Command("docker", "logs", "--details", "-t", c.name)
274 output, err := cmd.CombinedOutput()
275 if err != nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100276 c.suite.T().Fatalf("fetching logs error: %v", err)
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100277 }
278
279 f, err := os.Create(testLogFilePath)
280 if err != nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100281 c.suite.T().Fatalf("file create error: %v", err)
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100282 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100283 fmt.Fprint(f, string(output))
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100284 f.Close()
285}
286
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100287func (c *Container) log() string {
288 cmd := "docker logs " + c.name
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100289 c.suite.log(cmd)
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100290 o, err := exechelper.CombinedOutput(cmd)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100291 c.suite.assertNil(err)
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100292 return string(o)
293}
294
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100295func (c *Container) stop() error {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100296 if c.vppInstance != nil && c.vppInstance.apiChannel != nil {
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100297 c.vppInstance.saveLogs()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100298 c.vppInstance.disconnect()
299 }
300 c.vppInstance = nil
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100301 c.saveLogs()
Florin Corasf4fe0162023-01-17 13:02:51 -0800302 return exechelper.Run("docker stop " + c.name + " -t 0")
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100303}
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100304
305func (c *Container) createConfig(targetConfigName string, templateName string, values any) {
306 template := template.Must(template.ParseFiles(templateName))
307
308 f, err := os.CreateTemp("/tmp/hs-test/", "hst-config")
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100309 c.suite.assertNil(err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100310 defer os.Remove(f.Name())
311
312 err = template.Execute(f, values)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100313 c.suite.assertNil(err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100314
315 err = f.Close()
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100316 c.suite.assertNil(err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100317
318 c.copy(f.Name(), targetConfigName)
319}
Maros Ondrejickaf4ddf162023-03-08 16:01:43 +0100320
321func init() {
322 cmd := exec.Command("mkdir", "-p", logDir)
323 if err := cmd.Run(); err != nil {
324 panic(err)
325 }
326}