blob: 40dc0828376b7121475148dc39ff114134bf043e [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
38func NewContainer(yamlInput ContainerConfig) (*Container, error) {
39 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 Ondrejicka2908f8c2023-02-02 08:58:04 +010096func (c *Container) Suite() *HstSuite {
97 return c.suite
98}
99
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100100func (c *Container) getWorkDirVolume() (res Volume, exists bool) {
101 for _, v := range c.volumes {
102 if v.isDefaultWorkDir {
103 res = v
104 exists = true
105 return
106 }
107 }
108 return
109}
110
111func (c *Container) GetHostWorkDir() (res string) {
112 if v, ok := c.getWorkDirVolume(); ok {
113 res = v.hostDir
114 }
115 return
116}
117
118func (c *Container) GetContainerWorkDir() (res string) {
119 if v, ok := c.getWorkDirVolume(); ok {
120 res = v.containerDir
121 }
122 return
123}
124
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100125func (c *Container) getContainerArguments() string {
126 args := "--cap-add=all --privileged --network host --rm"
127 args += c.getVolumesAsCliOption()
128 args += c.getEnvVarsAsCliOption()
129 args += " --name " + c.name + " " + c.image
130 return args
131}
132
133func (c *Container) create() {
134 cmd := "docker create " + c.getContainerArguments()
135 exechelper.Run(cmd)
136}
137
138func (c *Container) start() {
139 cmd := "docker start " + c.name
140 exechelper.Run(cmd)
Maros Ondrejicka87531802022-12-19 20:35:27 +0100141}
142
143func (c *Container) run() error {
144 if c.name == "" {
145 return fmt.Errorf("run container failed: name is blank")
146 }
147
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100148 cmd := "docker run -d " + c.getContainerArguments() + " " + c.extraRunningArgs
149 c.Suite().log(cmd)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100150 err := exechelper.Run(cmd)
151 if err != nil {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100152 return fmt.Errorf("container run failed: %s", err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100153 }
154
155 return nil
156}
157
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100158func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100159 var volume Volume
160 volume.hostDir = hostDir
161 volume.containerDir = containerDir
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100162 volume.isDefaultWorkDir = isDefaultWorkDir
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100163 c.volumes[hostDir] = volume
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100164}
165
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100166func (c *Container) getVolumesAsCliOption() string {
167 cliOption := ""
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100168
169 if len(c.volumes) > 0 {
170 for _, volume := range c.volumes {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100171 cliOption += fmt.Sprintf(" -v %s:%s", volume.hostDir, volume.containerDir)
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100172 }
173 }
174
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100175 return cliOption
176}
177
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100178func (c *Container) addEnvVar(name string, value string) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100179 c.envVars[name] = value
180}
181
182func (c *Container) getEnvVarsAsCliOption() string {
183 cliOption := ""
184 if len(c.envVars) == 0 {
185 return cliOption
186 }
187
188 for name, value := range c.envVars {
189 cliOption += fmt.Sprintf(" -e %s=%s", name, value)
190 }
191 return cliOption
192}
193
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100194func (c *Container) newVppInstance(additionalConfig ...Stanza) (*VppInstance, error) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100195 vpp := new(VppInstance)
196 vpp.container = c
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100197
198 if len(additionalConfig) > 0 {
199 vpp.additionalConfig = additionalConfig[0]
200 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100201
202 c.vppInstance = vpp
203
204 return vpp, nil
205}
206
207func (c *Container) copy(sourceFileName string, targetFileName string) error {
208 cmd := exec.Command("docker", "cp", sourceFileName, c.name+":"+targetFileName)
209 return cmd.Run()
210}
211
212func (c *Container) createFile(destFileName string, content string) error {
213 f, err := os.CreateTemp("/tmp", "hst-config")
214 if err != nil {
215 return err
216 }
217 defer os.Remove(f.Name())
218
219 if _, err := f.Write([]byte(content)); err != nil {
220 return err
221 }
222 if err := f.Close(); err != nil {
223 return err
224 }
225 c.copy(f.Name(), destFileName)
226 return nil
227}
228
229/*
230 * Executes in detached mode so that the started application can continue to run
231 * without blocking execution of test
232 */
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100233func (c *Container) execServer(command string, arguments ...any) {
234 serverCommand := fmt.Sprintf(command, arguments...)
235 containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() +
236 " " + c.name + " " + serverCommand
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100237 c.Suite().T().Helper()
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100238 c.Suite().log(containerExecCommand)
239 c.Suite().assertNil(exechelper.Run(containerExecCommand))
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100240}
241
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100242func (c *Container) exec(command string, arguments ...any) string {
243 cliCommand := fmt.Sprintf(command, arguments...)
244 containerExecCommand := "docker exec" + c.getEnvVarsAsCliOption() +
245 " " + c.name + " " + cliCommand
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100246 c.Suite().T().Helper()
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100247 c.Suite().log(containerExecCommand)
248 byteOutput, err := exechelper.CombinedOutput(containerExecCommand)
249 c.Suite().assertNil(err)
250 return string(byteOutput)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100251}
252
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100253func (c *Container) getLogDirPath() string {
254 testId := c.Suite().getTestId()
255 testName := c.Suite().T().Name()
256 logDirPath := logDir + testName + "/" + testId + "/"
257
258 cmd := exec.Command("mkdir", "-p", logDirPath)
259 if err := cmd.Run(); err != nil {
260 c.Suite().T().Fatalf("mkdir error: %v", err)
261 }
262
263 return logDirPath
264}
265
266func (c *Container) saveLogs() {
267 cmd := exec.Command("docker", "inspect", "--format='{{.State.Status}}'", c.name)
268 if output, _ := cmd.CombinedOutput(); !strings.Contains(string(output), "running") {
269 return
270 }
271
272 testLogFilePath := c.getLogDirPath() + "container-" + c.name + ".log"
273
274 cmd = exec.Command("docker", "logs", "--details", "-t", c.name)
275 output, err := cmd.CombinedOutput()
276 if err != nil {
277 c.Suite().T().Fatalf("fetching logs error: %v", err)
278 }
279
280 f, err := os.Create(testLogFilePath)
281 if err != nil {
282 c.Suite().T().Fatalf("file create error: %v", err)
283 }
284 fmt.Fprintf(f, string(output))
285 f.Close()
286}
287
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100288func (c *Container) log() string {
289 cmd := "docker logs " + c.name
290 c.Suite().log(cmd)
291 o, err := exechelper.CombinedOutput(cmd)
292 c.Suite().assertNil(err)
293 return string(o)
294}
295
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100296func (c *Container) stop() error {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100297 if c.vppInstance != nil && c.vppInstance.apiChannel != nil {
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100298 c.vppInstance.saveLogs()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100299 c.vppInstance.disconnect()
300 }
301 c.vppInstance = nil
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100302 c.saveLogs()
Florin Corasf4fe0162023-01-17 13:02:51 -0800303 return exechelper.Run("docker stop " + c.name + " -t 0")
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100304}
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100305
306func (c *Container) createConfig(targetConfigName string, templateName string, values any) {
307 template := template.Must(template.ParseFiles(templateName))
308
309 f, err := os.CreateTemp("/tmp/hs-test/", "hst-config")
310 c.Suite().assertNil(err)
311 defer os.Remove(f.Name())
312
313 err = template.Execute(f, values)
314 c.Suite().assertNil(err)
315
316 err = f.Close()
317 c.Suite().assertNil(err)
318
319 c.copy(f.Name(), targetConfigName)
320}