Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 5 | "os" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 6 | "os/exec" |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 7 | "strings" |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 8 | "text/template" |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 9 | |
| 10 | "github.com/edwarnicke/exechelper" |
| 11 | ) |
| 12 | |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 13 | const ( |
| 14 | logDir string = "/tmp/hs-test/" |
| 15 | ) |
| 16 | |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 17 | var ( |
| 18 | workDir, _ = os.Getwd() |
| 19 | ) |
| 20 | |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 21 | type Volume struct { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 22 | hostDir string |
| 23 | containerDir string |
| 24 | isDefaultWorkDir bool |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | type Container struct { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 28 | suite *HstSuite |
Filip Tehlar | 3f95143 | 2023-01-13 21:33:43 +0100 | [diff] [blame] | 29 | isOptional bool |
| 30 | name string |
| 31 | image string |
Filip Tehlar | 3f95143 | 2023-01-13 21:33:43 +0100 | [diff] [blame] | 32 | extraRunningArgs string |
| 33 | volumes map[string]Volume |
| 34 | envVars map[string]string |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 35 | vppInstance *VppInstance |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | func 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 Tehlar | 3f95143 | 2023-01-13 21:33:43 +0100 | [diff] [blame] | 56 | if args, ok := yamlInput["extra-args"]; ok { |
| 57 | container.extraRunningArgs = args.(string) |
| 58 | } else { |
| 59 | container.extraRunningArgs = "" |
| 60 | } |
| 61 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 62 | 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 Tehlar | f3ee2b6 | 2023-01-09 12:07:09 +0100 | [diff] [blame] | 69 | r := strings.NewReplacer("$HST_DIR", workDir) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 70 | 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 Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 74 | isDefaultWorkDir := false |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 75 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 76 | if isDefault, ok := volumeMap["is-default-work-dir"]; ok { |
| 77 | isDefaultWorkDir = isDefault.(bool) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 80 | container.addVolume(hostDir, containerDir, isDefaultWorkDir) |
| 81 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | if _, ok := yamlInput["vars"]; ok { |
| 86 | for _, envVar := range yamlInput["vars"].([]interface{}) { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 87 | envVarMap := envVar.(ContainerConfig) |
| 88 | name := envVarMap["name"].(string) |
| 89 | value := envVarMap["value"].(string) |
| 90 | container.addEnvVar(name, value) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | return container, nil |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 96 | func (c *Container) Suite() *HstSuite { |
| 97 | return c.suite |
| 98 | } |
| 99 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 100 | func (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 | |
| 111 | func (c *Container) GetHostWorkDir() (res string) { |
| 112 | if v, ok := c.getWorkDirVolume(); ok { |
| 113 | res = v.hostDir |
| 114 | } |
| 115 | return |
| 116 | } |
| 117 | |
| 118 | func (c *Container) GetContainerWorkDir() (res string) { |
| 119 | if v, ok := c.getWorkDirVolume(); ok { |
| 120 | res = v.containerDir |
| 121 | } |
| 122 | return |
| 123 | } |
| 124 | |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 125 | func (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 |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 130 | args += " " + c.extraRunningArgs |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 131 | return args |
| 132 | } |
| 133 | |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 134 | func (c *Container) create() error { |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 135 | cmd := "docker create " + c.getContainerArguments() |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 136 | c.Suite().log(cmd) |
| 137 | return exechelper.Run(cmd) |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 140 | func (c *Container) start() error { |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 141 | cmd := "docker start " + c.name |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 142 | c.Suite().log(cmd) |
| 143 | return exechelper.Run(cmd) |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | func (c *Container) run() error { |
| 147 | if c.name == "" { |
| 148 | return fmt.Errorf("run container failed: name is blank") |
| 149 | } |
| 150 | |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 151 | cmd := "docker run -d " + c.getContainerArguments() |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 152 | c.Suite().log(cmd) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 153 | err := exechelper.Run(cmd) |
| 154 | if err != nil { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 155 | return fmt.Errorf("container run failed: %s", err) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return nil |
| 159 | } |
| 160 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 161 | func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 162 | var volume Volume |
| 163 | volume.hostDir = hostDir |
| 164 | volume.containerDir = containerDir |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 165 | volume.isDefaultWorkDir = isDefaultWorkDir |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 166 | c.volumes[hostDir] = volume |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 169 | func (c *Container) getVolumesAsCliOption() string { |
| 170 | cliOption := "" |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 171 | |
| 172 | if len(c.volumes) > 0 { |
| 173 | for _, volume := range c.volumes { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 174 | cliOption += fmt.Sprintf(" -v %s:%s", volume.hostDir, volume.containerDir) |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 178 | return cliOption |
| 179 | } |
| 180 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 181 | func (c *Container) addEnvVar(name string, value string) { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 182 | c.envVars[name] = value |
| 183 | } |
| 184 | |
| 185 | func (c *Container) getEnvVarsAsCliOption() string { |
| 186 | cliOption := "" |
| 187 | if len(c.envVars) == 0 { |
| 188 | return cliOption |
| 189 | } |
| 190 | |
| 191 | for name, value := range c.envVars { |
| 192 | cliOption += fmt.Sprintf(" -e %s=%s", name, value) |
| 193 | } |
| 194 | return cliOption |
| 195 | } |
| 196 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 197 | func (c *Container) newVppInstance(additionalConfig ...Stanza) (*VppInstance, error) { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 198 | vpp := new(VppInstance) |
| 199 | vpp.container = c |
Maros Ondrejicka | 7d7ab10 | 2023-02-14 12:56:49 +0100 | [diff] [blame] | 200 | |
| 201 | if len(additionalConfig) > 0 { |
| 202 | vpp.additionalConfig = additionalConfig[0] |
| 203 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 204 | |
| 205 | c.vppInstance = vpp |
| 206 | |
| 207 | return vpp, nil |
| 208 | } |
| 209 | |
| 210 | func (c *Container) copy(sourceFileName string, targetFileName string) error { |
| 211 | cmd := exec.Command("docker", "cp", sourceFileName, c.name+":"+targetFileName) |
| 212 | return cmd.Run() |
| 213 | } |
| 214 | |
| 215 | func (c *Container) createFile(destFileName string, content string) error { |
| 216 | f, err := os.CreateTemp("/tmp", "hst-config") |
| 217 | if err != nil { |
| 218 | return err |
| 219 | } |
| 220 | defer os.Remove(f.Name()) |
| 221 | |
| 222 | if _, err := f.Write([]byte(content)); err != nil { |
| 223 | return err |
| 224 | } |
| 225 | if err := f.Close(); err != nil { |
| 226 | return err |
| 227 | } |
| 228 | c.copy(f.Name(), destFileName) |
| 229 | return nil |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Executes in detached mode so that the started application can continue to run |
| 234 | * without blocking execution of test |
| 235 | */ |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 236 | func (c *Container) execServer(command string, arguments ...any) { |
| 237 | serverCommand := fmt.Sprintf(command, arguments...) |
| 238 | containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() + |
| 239 | " " + c.name + " " + serverCommand |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 240 | c.Suite().T().Helper() |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 241 | c.Suite().log(containerExecCommand) |
| 242 | c.Suite().assertNil(exechelper.Run(containerExecCommand)) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 245 | func (c *Container) exec(command string, arguments ...any) string { |
| 246 | cliCommand := fmt.Sprintf(command, arguments...) |
| 247 | containerExecCommand := "docker exec" + c.getEnvVarsAsCliOption() + |
| 248 | " " + c.name + " " + cliCommand |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 249 | c.Suite().T().Helper() |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 250 | c.Suite().log(containerExecCommand) |
| 251 | byteOutput, err := exechelper.CombinedOutput(containerExecCommand) |
| 252 | c.Suite().assertNil(err) |
| 253 | return string(byteOutput) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 256 | func (c *Container) getLogDirPath() string { |
| 257 | testId := c.Suite().getTestId() |
| 258 | testName := c.Suite().T().Name() |
| 259 | logDirPath := logDir + testName + "/" + testId + "/" |
| 260 | |
| 261 | cmd := exec.Command("mkdir", "-p", logDirPath) |
| 262 | if err := cmd.Run(); err != nil { |
| 263 | c.Suite().T().Fatalf("mkdir error: %v", err) |
| 264 | } |
| 265 | |
| 266 | return logDirPath |
| 267 | } |
| 268 | |
| 269 | func (c *Container) saveLogs() { |
| 270 | cmd := exec.Command("docker", "inspect", "--format='{{.State.Status}}'", c.name) |
| 271 | if output, _ := cmd.CombinedOutput(); !strings.Contains(string(output), "running") { |
| 272 | return |
| 273 | } |
| 274 | |
| 275 | testLogFilePath := c.getLogDirPath() + "container-" + c.name + ".log" |
| 276 | |
| 277 | cmd = exec.Command("docker", "logs", "--details", "-t", c.name) |
| 278 | output, err := cmd.CombinedOutput() |
| 279 | if err != nil { |
| 280 | c.Suite().T().Fatalf("fetching logs error: %v", err) |
| 281 | } |
| 282 | |
| 283 | f, err := os.Create(testLogFilePath) |
| 284 | if err != nil { |
| 285 | c.Suite().T().Fatalf("file create error: %v", err) |
| 286 | } |
| 287 | fmt.Fprintf(f, string(output)) |
| 288 | f.Close() |
| 289 | } |
| 290 | |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 291 | func (c *Container) log() string { |
| 292 | cmd := "docker logs " + c.name |
| 293 | c.Suite().log(cmd) |
| 294 | o, err := exechelper.CombinedOutput(cmd) |
| 295 | c.Suite().assertNil(err) |
| 296 | return string(o) |
| 297 | } |
| 298 | |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 299 | func (c *Container) stop() error { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 300 | if c.vppInstance != nil && c.vppInstance.apiChannel != nil { |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 301 | c.vppInstance.saveLogs() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 302 | c.vppInstance.disconnect() |
| 303 | } |
| 304 | c.vppInstance = nil |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 305 | c.saveLogs() |
Florin Coras | f4fe016 | 2023-01-17 13:02:51 -0800 | [diff] [blame] | 306 | return exechelper.Run("docker stop " + c.name + " -t 0") |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 307 | } |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 308 | |
| 309 | func (c *Container) createConfig(targetConfigName string, templateName string, values any) { |
| 310 | template := template.Must(template.ParseFiles(templateName)) |
| 311 | |
| 312 | f, err := os.CreateTemp("/tmp/hs-test/", "hst-config") |
| 313 | c.Suite().assertNil(err) |
| 314 | defer os.Remove(f.Name()) |
| 315 | |
| 316 | err = template.Execute(f, values) |
| 317 | c.Suite().assertNil(err) |
| 318 | |
| 319 | err = f.Close() |
| 320 | c.Suite().assertNil(err) |
| 321 | |
| 322 | c.copy(f.Name(), targetConfigName) |
| 323 | } |