blob: 91ca2c2b0b1e992703bd1a22a511122c7019accf [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 Ondrejicka11a03e92022-12-01 09:56:37 +01008
9 "github.com/edwarnicke/exechelper"
10)
11
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +010012type Volume struct {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010013 hostDir string
14 containerDir string
15 isDefaultWorkDir bool
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +010016}
17
18type Container struct {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010019 suite *HstSuite
Filip Tehlar3f951432023-01-13 21:33:43 +010020 isOptional bool
21 name string
22 image string
Filip Tehlar3f951432023-01-13 21:33:43 +010023 extraRunningArgs string
24 volumes map[string]Volume
25 envVars map[string]string
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010026 vppInstance *VppInstance
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010027}
28
29func NewContainer(yamlInput ContainerConfig) (*Container, error) {
30 containerName := yamlInput["name"].(string)
31 if len(containerName) == 0 {
32 err := fmt.Errorf("container name must not be blank")
33 return nil, err
34 }
35
36 var container = new(Container)
37 container.volumes = make(map[string]Volume)
38 container.envVars = make(map[string]string)
39 container.name = containerName
40
41 if image, ok := yamlInput["image"]; ok {
42 container.image = image.(string)
43 } else {
44 container.image = "hs-test/vpp"
45 }
46
Filip Tehlar3f951432023-01-13 21:33:43 +010047 if args, ok := yamlInput["extra-args"]; ok {
48 container.extraRunningArgs = args.(string)
49 } else {
50 container.extraRunningArgs = ""
51 }
52
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010053 if isOptional, ok := yamlInput["is-optional"]; ok {
54 container.isOptional = isOptional.(bool)
55 } else {
56 container.isOptional = false
57 }
58
59 if _, ok := yamlInput["volumes"]; ok {
Filip Tehlarf3ee2b62023-01-09 12:07:09 +010060 r := strings.NewReplacer("$HST_DIR", workDir)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010061 for _, volu := range yamlInput["volumes"].([]interface{}) {
62 volumeMap := volu.(ContainerConfig)
63 hostDir := r.Replace(volumeMap["host-dir"].(string))
64 containerDir := volumeMap["container-dir"].(string)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010065 isDefaultWorkDir := false
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010066
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010067 if isDefault, ok := volumeMap["is-default-work-dir"]; ok {
68 isDefaultWorkDir = isDefault.(bool)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010069 }
70
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010071 container.addVolume(hostDir, containerDir, isDefaultWorkDir)
72
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010073 }
74 }
75
76 if _, ok := yamlInput["vars"]; ok {
77 for _, envVar := range yamlInput["vars"].([]interface{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010078 envVarMap := envVar.(ContainerConfig)
79 name := envVarMap["name"].(string)
80 value := envVarMap["value"].(string)
81 container.addEnvVar(name, value)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010082 }
83 }
84 return container, nil
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010085}
86
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010087func (c *Container) getWorkDirVolume() (res Volume, exists bool) {
88 for _, v := range c.volumes {
89 if v.isDefaultWorkDir {
90 res = v
91 exists = true
92 return
93 }
94 }
95 return
96}
97
98func (c *Container) GetHostWorkDir() (res string) {
99 if v, ok := c.getWorkDirVolume(); ok {
100 res = v.hostDir
101 }
102 return
103}
104
105func (c *Container) GetContainerWorkDir() (res string) {
106 if v, ok := c.getWorkDirVolume(); ok {
107 res = v.containerDir
108 }
109 return
110}
111
Maros Ondrejicka87531802022-12-19 20:35:27 +0100112func (c *Container) getRunCommand() string {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100113 syncPath := fmt.Sprintf(" -v %s:/tmp/sync", c.getSyncPath())
114 cmd := "docker run --cap-add=all -d --privileged --network host --rm"
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100115 cmd += syncPath
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100116 cmd += c.getVolumesAsCliOption()
117 cmd += c.getEnvVarsAsCliOption()
Filip Tehlar3f951432023-01-13 21:33:43 +0100118 cmd += " --name " + c.name + " " + c.image + " " + c.extraRunningArgs
Maros Ondrejicka87531802022-12-19 20:35:27 +0100119 return cmd
120}
121
122func (c *Container) run() error {
123 if c.name == "" {
124 return fmt.Errorf("run container failed: name is blank")
125 }
126
127 exechelper.Run(fmt.Sprintf("mkdir -p /tmp/%s/sync", c.name))
128 cmd := c.getRunCommand()
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100129 err := exechelper.Run(cmd)
130 if err != nil {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100131 return fmt.Errorf("container run failed: %s", err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100132 }
133
134 return nil
135}
136
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100137func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100138 var volume Volume
139 volume.hostDir = hostDir
140 volume.containerDir = containerDir
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100141 volume.isDefaultWorkDir = isDefaultWorkDir
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100142 c.volumes[hostDir] = volume
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100143}
144
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100145func (c *Container) getVolumeByHostDir(hostDir string) Volume {
146 return c.volumes[hostDir]
147}
148
149func (c *Container) getVolumesAsCliOption() string {
150 cliOption := ""
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100151
152 if len(c.volumes) > 0 {
153 for _, volume := range c.volumes {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100154 cliOption += fmt.Sprintf(" -v %s:%s", volume.hostDir, volume.containerDir)
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100155 }
156 }
157
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100158 return cliOption
159}
160
161func (c *Container) getWorkDirAsCliOption() string {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100162 if _, ok := c.getWorkDirVolume(); ok {
163 return fmt.Sprintf(" --workdir=\"%s\"", c.GetContainerWorkDir())
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100164 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100165 return ""
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100166}
167
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100168func (c *Container) addEnvVar(name string, value string) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100169 c.envVars[name] = value
170}
171
172func (c *Container) getEnvVarsAsCliOption() string {
173 cliOption := ""
174 if len(c.envVars) == 0 {
175 return cliOption
176 }
177
178 for name, value := range c.envVars {
179 cliOption += fmt.Sprintf(" -e %s=%s", name, value)
180 }
181 return cliOption
182}
183
184func (c *Container) getSyncPath() string {
185 return fmt.Sprintf("/tmp/%s/sync", c.name)
186}
187
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100188func (c *Container) newVppInstance(additionalConfig ...Stanza) (*VppInstance, error) {
189 vppConfig := new(VppConfig)
190 vppConfig.CliSocketFilePath = defaultCliSocketFilePath
191 if len(additionalConfig) > 0 {
192 vppConfig.additionalConfig = additionalConfig[0]
193 }
194
195 vpp := new(VppInstance)
196 vpp.container = c
197 vpp.config = vppConfig
198
199 c.vppInstance = vpp
200
201 return vpp, nil
202}
203
204func (c *Container) copy(sourceFileName string, targetFileName string) error {
205 cmd := exec.Command("docker", "cp", sourceFileName, c.name+":"+targetFileName)
206 return cmd.Run()
207}
208
209func (c *Container) createFile(destFileName string, content string) error {
210 f, err := os.CreateTemp("/tmp", "hst-config")
211 if err != nil {
212 return err
213 }
214 defer os.Remove(f.Name())
215
216 if _, err := f.Write([]byte(content)); err != nil {
217 return err
218 }
219 if err := f.Close(); err != nil {
220 return err
221 }
222 c.copy(f.Name(), destFileName)
223 return nil
224}
225
226/*
227 * Executes in detached mode so that the started application can continue to run
228 * without blocking execution of test
229 */
230func (c *Container) execServer(command string) error {
231 return exechelper.Run("docker exec -d" + c.getEnvVarsAsCliOption() + " " + c.name + " " + command)
232}
233
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100234func (c *Container) exec(command string) (string, error) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100235 cliCommand := "docker exec" + c.getEnvVarsAsCliOption() + " " + c.name + " " + command
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100236 byteOutput, err := exechelper.CombinedOutput(cliCommand)
237 return string(byteOutput), err
238}
239
240func (c *Container) execAction(args string) (string, error) {
241 syncFile := c.getSyncPath() + "/rc"
242 os.Remove(syncFile)
243
244 workDir := c.getWorkDirAsCliOption()
245 cmd := fmt.Sprintf("docker exec -d %s %s hs-test %s",
246 workDir,
247 c.name,
248 args)
249 err := exechelper.Run(cmd)
250 if err != nil {
251 return "", err
252 }
253 res, err := waitForSyncFile(syncFile)
254 if err != nil {
255 return "", fmt.Errorf("failed to read sync file while executing 'hs-test %s': %v", args, err)
256 }
257 o := res.StdOutput + res.ErrOutput
258 if res.Code != 0 {
259 return o, fmt.Errorf("cmd resulted in non-zero value %d: %s", res.Code, res.Desc)
260 }
261 return o, err
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100262}
263
264func (c *Container) stop() error {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100265 if c.vppInstance != nil && c.vppInstance.apiChannel != nil {
266 c.vppInstance.disconnect()
267 }
268 c.vppInstance = nil
Florin Corasf4fe0162023-01-17 13:02:51 -0800269 return exechelper.Run("docker stop " + c.name + " -t 0")
Maros Ondrejicka0e79abb2022-12-06 19:46:24 +0100270}