hs-test: refactor test cases from no-topo suite
[vpp.git] / extras / hs-test / container.go
1 package main
2
3 import (
4         "fmt"
5         "os"
6         "os/exec"
7         "strings"
8
9         "github.com/edwarnicke/exechelper"
10 )
11
12 var (
13         workDir, _ = os.Getwd()
14 )
15
16 type Volume struct {
17         hostDir          string
18         containerDir     string
19         isDefaultWorkDir bool
20 }
21
22 type Container struct {
23         suite            *HstSuite
24         isOptional       bool
25         name             string
26         image            string
27         extraRunningArgs string
28         volumes          map[string]Volume
29         envVars          map[string]string
30         vppInstance      *VppInstance
31 }
32
33 func NewContainer(yamlInput ContainerConfig) (*Container, error) {
34         containerName := yamlInput["name"].(string)
35         if len(containerName) == 0 {
36                 err := fmt.Errorf("container name must not be blank")
37                 return nil, err
38         }
39
40         var container = new(Container)
41         container.volumes = make(map[string]Volume)
42         container.envVars = make(map[string]string)
43         container.name = containerName
44
45         if image, ok := yamlInput["image"]; ok {
46                 container.image = image.(string)
47         } else {
48                 container.image = "hs-test/vpp"
49         }
50
51         if args, ok := yamlInput["extra-args"]; ok {
52                 container.extraRunningArgs = args.(string)
53         } else {
54                 container.extraRunningArgs = ""
55         }
56
57         if isOptional, ok := yamlInput["is-optional"]; ok {
58                 container.isOptional = isOptional.(bool)
59         } else {
60                 container.isOptional = false
61         }
62
63         if _, ok := yamlInput["volumes"]; ok {
64                 r := strings.NewReplacer("$HST_DIR", workDir)
65                 for _, volu := range yamlInput["volumes"].([]interface{}) {
66                         volumeMap := volu.(ContainerConfig)
67                         hostDir := r.Replace(volumeMap["host-dir"].(string))
68                         containerDir := volumeMap["container-dir"].(string)
69                         isDefaultWorkDir := false
70
71                         if isDefault, ok := volumeMap["is-default-work-dir"]; ok {
72                                 isDefaultWorkDir = isDefault.(bool)
73                         }
74
75                         container.addVolume(hostDir, containerDir, isDefaultWorkDir)
76
77                 }
78         }
79
80         if _, ok := yamlInput["vars"]; ok {
81                 for _, envVar := range yamlInput["vars"].([]interface{}) {
82                         envVarMap := envVar.(ContainerConfig)
83                         name := envVarMap["name"].(string)
84                         value := envVarMap["value"].(string)
85                         container.addEnvVar(name, value)
86                 }
87         }
88         return container, nil
89 }
90
91 func (c *Container) Suite() *HstSuite {
92         return c.suite
93 }
94
95 func (c *Container) getWorkDirVolume() (res Volume, exists bool) {
96         for _, v := range c.volumes {
97                 if v.isDefaultWorkDir {
98                         res = v
99                         exists = true
100                         return
101                 }
102         }
103         return
104 }
105
106 func (c *Container) GetHostWorkDir() (res string) {
107         if v, ok := c.getWorkDirVolume(); ok {
108                 res = v.hostDir
109         }
110         return
111 }
112
113 func (c *Container) GetContainerWorkDir() (res string) {
114         if v, ok := c.getWorkDirVolume(); ok {
115                 res = v.containerDir
116         }
117         return
118 }
119
120 func (c *Container) getRunCommand() string {
121         cmd := "docker run --cap-add=all -d --privileged --network host --rm"
122         cmd += c.getVolumesAsCliOption()
123         cmd += c.getEnvVarsAsCliOption()
124         cmd += " --name " + c.name + " " + c.image + " " + c.extraRunningArgs
125         return cmd
126 }
127
128 func (c *Container) run() error {
129         if c.name == "" {
130                 return fmt.Errorf("run container failed: name is blank")
131         }
132
133         exechelper.Run(fmt.Sprintf("mkdir -p /tmp/%s/sync", c.name))
134         cmd := c.getRunCommand()
135         err := exechelper.Run(cmd)
136         if err != nil {
137                 return fmt.Errorf("container run failed: %s", err)
138         }
139
140         return nil
141 }
142
143 func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) {
144         var volume Volume
145         volume.hostDir = hostDir
146         volume.containerDir = containerDir
147         volume.isDefaultWorkDir = isDefaultWorkDir
148         c.volumes[hostDir] = volume
149 }
150
151 func (c *Container) getVolumeByHostDir(hostDir string) Volume {
152         return c.volumes[hostDir]
153 }
154
155 func (c *Container) getVolumesAsCliOption() string {
156         cliOption := ""
157
158         if len(c.volumes) > 0 {
159                 for _, volume := range c.volumes {
160                         cliOption += fmt.Sprintf(" -v %s:%s", volume.hostDir, volume.containerDir)
161                 }
162         }
163
164         return cliOption
165 }
166
167 func (c *Container) getWorkDirAsCliOption() string {
168         if _, ok := c.getWorkDirVolume(); ok {
169                 return fmt.Sprintf(" --workdir=\"%s\"", c.GetContainerWorkDir())
170         }
171         return ""
172 }
173
174 func (c *Container) addEnvVar(name string, value string) {
175         c.envVars[name] = value
176 }
177
178 func (c *Container) getEnvVarsAsCliOption() string {
179         cliOption := ""
180         if len(c.envVars) == 0 {
181                 return cliOption
182         }
183
184         for name, value := range c.envVars {
185                 cliOption += fmt.Sprintf(" -e %s=%s", name, value)
186         }
187         return cliOption
188 }
189
190 func (c *Container) newVppInstance(additionalConfig ...Stanza) (*VppInstance, error) {
191         vppConfig := new(VppConfig)
192         vppConfig.CliSocketFilePath = defaultCliSocketFilePath
193         if len(additionalConfig) > 0 {
194                 vppConfig.additionalConfig = additionalConfig[0]
195         }
196
197         vpp := new(VppInstance)
198         vpp.container = c
199         vpp.config = vppConfig
200
201         c.vppInstance = vpp
202
203         return vpp, nil
204 }
205
206 func (c *Container) copy(sourceFileName string, targetFileName string) error {
207         cmd := exec.Command("docker", "cp", sourceFileName, c.name+":"+targetFileName)
208         return cmd.Run()
209 }
210
211 func (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  */
232 func (c *Container) execServer(command string, arguments ...any) {
233         serverCommand := fmt.Sprintf(command, arguments...)
234         containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() +
235                 " " + c.name + " " + serverCommand
236         c.Suite().log(containerExecCommand)
237         c.Suite().assertNil(exechelper.Run(containerExecCommand))
238 }
239
240 func (c *Container) exec(command string, arguments ...any) string {
241         cliCommand := fmt.Sprintf(command, arguments...)
242         containerExecCommand := "docker exec" + c.getEnvVarsAsCliOption() +
243                 " " + c.name + " " + cliCommand
244         c.Suite().log(containerExecCommand)
245         byteOutput, err := exechelper.CombinedOutput(containerExecCommand)
246         c.Suite().assertNil(err)
247         return string(byteOutput)
248 }
249
250 func (c *Container) stop() error {
251         if c.vppInstance != nil && c.vppInstance.apiChannel != nil {
252                 c.vppInstance.disconnect()
253         }
254         c.vppInstance = nil
255         return exechelper.Run("docker stop " + c.name + " -t 0")
256 }