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 |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 30 | runDetached bool |
Filip Tehlar | 3f95143 | 2023-01-13 21:33:43 +0100 | [diff] [blame] | 31 | name string |
| 32 | image string |
Filip Tehlar | 3f95143 | 2023-01-13 21:33:43 +0100 | [diff] [blame] | 33 | extraRunningArgs string |
| 34 | volumes map[string]Volume |
| 35 | envVars map[string]string |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 36 | vppInstance *VppInstance |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 37 | } |
| 38 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 39 | func newContainer(yamlInput ContainerConfig) (*Container, error) { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 40 | containerName := yamlInput["name"].(string) |
| 41 | if len(containerName) == 0 { |
| 42 | err := fmt.Errorf("container name must not be blank") |
| 43 | return nil, err |
| 44 | } |
| 45 | |
| 46 | var container = new(Container) |
| 47 | container.volumes = make(map[string]Volume) |
| 48 | container.envVars = make(map[string]string) |
| 49 | container.name = containerName |
| 50 | |
| 51 | if image, ok := yamlInput["image"]; ok { |
| 52 | container.image = image.(string) |
| 53 | } else { |
| 54 | container.image = "hs-test/vpp" |
| 55 | } |
| 56 | |
Filip Tehlar | 3f95143 | 2023-01-13 21:33:43 +0100 | [diff] [blame] | 57 | if args, ok := yamlInput["extra-args"]; ok { |
| 58 | container.extraRunningArgs = args.(string) |
| 59 | } else { |
| 60 | container.extraRunningArgs = "" |
| 61 | } |
| 62 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 63 | if isOptional, ok := yamlInput["is-optional"]; ok { |
| 64 | container.isOptional = isOptional.(bool) |
| 65 | } else { |
| 66 | container.isOptional = false |
| 67 | } |
| 68 | |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 69 | if runDetached, ok := yamlInput["run-detached"]; ok { |
| 70 | container.runDetached = runDetached.(bool) |
| 71 | } else { |
| 72 | container.runDetached = true |
| 73 | } |
| 74 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 75 | if _, ok := yamlInput["volumes"]; ok { |
Filip Tehlar | f3ee2b6 | 2023-01-09 12:07:09 +0100 | [diff] [blame] | 76 | r := strings.NewReplacer("$HST_DIR", workDir) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 77 | for _, volu := range yamlInput["volumes"].([]interface{}) { |
| 78 | volumeMap := volu.(ContainerConfig) |
| 79 | hostDir := r.Replace(volumeMap["host-dir"].(string)) |
| 80 | containerDir := volumeMap["container-dir"].(string) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 81 | isDefaultWorkDir := false |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 82 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 83 | if isDefault, ok := volumeMap["is-default-work-dir"]; ok { |
| 84 | isDefaultWorkDir = isDefault.(bool) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 87 | container.addVolume(hostDir, containerDir, isDefaultWorkDir) |
| 88 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | if _, ok := yamlInput["vars"]; ok { |
| 93 | for _, envVar := range yamlInput["vars"].([]interface{}) { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 94 | envVarMap := envVar.(ContainerConfig) |
| 95 | name := envVarMap["name"].(string) |
| 96 | value := envVarMap["value"].(string) |
| 97 | container.addEnvVar(name, value) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | return container, nil |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 103 | func (c *Container) getWorkDirVolume() (res Volume, exists bool) { |
| 104 | for _, v := range c.volumes { |
| 105 | if v.isDefaultWorkDir { |
| 106 | res = v |
| 107 | exists = true |
| 108 | return |
| 109 | } |
| 110 | } |
| 111 | return |
| 112 | } |
| 113 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 114 | func (c *Container) getHostWorkDir() (res string) { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 115 | if v, ok := c.getWorkDirVolume(); ok { |
| 116 | res = v.hostDir |
| 117 | } |
| 118 | return |
| 119 | } |
| 120 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 121 | func (c *Container) getContainerWorkDir() (res string) { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 122 | if v, ok := c.getWorkDirVolume(); ok { |
| 123 | res = v.containerDir |
| 124 | } |
| 125 | return |
| 126 | } |
| 127 | |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 128 | func (c *Container) getContainerArguments() string { |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 129 | args := "--ulimit nofile=90000:90000 --cap-add=all --privileged --network host --rm" |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 130 | args += c.getVolumesAsCliOption() |
| 131 | args += c.getEnvVarsAsCliOption() |
Filip Tehlar | 109f3ce | 2023-09-05 15:36:28 +0200 | [diff] [blame] | 132 | if *vppSourceFileDir != "" { |
| 133 | args += fmt.Sprintf(" -v %s:%s", *vppSourceFileDir, *vppSourceFileDir) |
| 134 | } |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 135 | args += " --name " + c.name + " " + c.image |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 136 | args += " " + c.extraRunningArgs |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 137 | return args |
| 138 | } |
| 139 | |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 140 | func (c *Container) create() error { |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 141 | cmd := "docker create " + c.getContainerArguments() |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 142 | c.suite.log(cmd) |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 143 | return exechelper.Run(cmd) |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 146 | func (c *Container) start() error { |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 147 | cmd := "docker start " + c.name |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 148 | c.suite.log(cmd) |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 149 | return exechelper.Run(cmd) |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 152 | func (c *Container) prepareCommand() (string, error) { |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 153 | if c.name == "" { |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 154 | return "", fmt.Errorf("run container failed: name is blank") |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 157 | cmd := "docker run " |
| 158 | if c.runDetached { |
| 159 | cmd += " -d" |
| 160 | } |
| 161 | cmd += " " + c.getContainerArguments() |
| 162 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 163 | c.suite.log(cmd) |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 164 | return cmd, nil |
| 165 | } |
| 166 | |
| 167 | func (c *Container) combinedOutput() (string, error) { |
| 168 | cmd, err := c.prepareCommand() |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 169 | if err != nil { |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 170 | return "", err |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 173 | byteOutput, err := exechelper.CombinedOutput(cmd) |
| 174 | return string(byteOutput), err |
| 175 | } |
| 176 | |
| 177 | func (c *Container) run() error { |
| 178 | cmd, err := c.prepareCommand() |
| 179 | if err != nil { |
| 180 | return err |
| 181 | } |
| 182 | |
| 183 | return exechelper.Run(cmd) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 186 | func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 187 | var volume Volume |
| 188 | volume.hostDir = hostDir |
| 189 | volume.containerDir = containerDir |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 190 | volume.isDefaultWorkDir = isDefaultWorkDir |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 191 | c.volumes[hostDir] = volume |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 194 | func (c *Container) getVolumesAsCliOption() string { |
| 195 | cliOption := "" |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 196 | |
| 197 | if len(c.volumes) > 0 { |
| 198 | for _, volume := range c.volumes { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 199 | cliOption += fmt.Sprintf(" -v %s:%s", volume.hostDir, volume.containerDir) |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 203 | return cliOption |
| 204 | } |
| 205 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 206 | func (c *Container) addEnvVar(name string, value string) { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 207 | c.envVars[name] = value |
| 208 | } |
| 209 | |
| 210 | func (c *Container) getEnvVarsAsCliOption() string { |
| 211 | cliOption := "" |
| 212 | if len(c.envVars) == 0 { |
| 213 | return cliOption |
| 214 | } |
| 215 | |
| 216 | for name, value := range c.envVars { |
| 217 | cliOption += fmt.Sprintf(" -e %s=%s", name, value) |
| 218 | } |
| 219 | return cliOption |
| 220 | } |
| 221 | |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 222 | func (c *Container) newVppInstance(cpus []int, additionalConfigs ...Stanza) (*VppInstance, error) { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 223 | vpp := new(VppInstance) |
| 224 | vpp.container = c |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 225 | vpp.cpus = cpus |
| 226 | vpp.additionalConfig = append(vpp.additionalConfig, additionalConfigs...) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 227 | c.vppInstance = vpp |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 228 | return vpp, nil |
| 229 | } |
| 230 | |
| 231 | func (c *Container) copy(sourceFileName string, targetFileName string) error { |
| 232 | cmd := exec.Command("docker", "cp", sourceFileName, c.name+":"+targetFileName) |
| 233 | return cmd.Run() |
| 234 | } |
| 235 | |
| 236 | func (c *Container) createFile(destFileName string, content string) error { |
| 237 | f, err := os.CreateTemp("/tmp", "hst-config") |
| 238 | if err != nil { |
| 239 | return err |
| 240 | } |
| 241 | defer os.Remove(f.Name()) |
| 242 | |
| 243 | if _, err := f.Write([]byte(content)); err != nil { |
| 244 | return err |
| 245 | } |
| 246 | if err := f.Close(); err != nil { |
| 247 | return err |
| 248 | } |
| 249 | c.copy(f.Name(), destFileName) |
| 250 | return nil |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Executes in detached mode so that the started application can continue to run |
| 255 | * without blocking execution of test |
| 256 | */ |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 257 | func (c *Container) execServer(command string, arguments ...any) { |
| 258 | serverCommand := fmt.Sprintf(command, arguments...) |
| 259 | containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() + |
| 260 | " " + c.name + " " + serverCommand |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 261 | c.suite.T().Helper() |
| 262 | c.suite.log(containerExecCommand) |
| 263 | c.suite.assertNil(exechelper.Run(containerExecCommand)) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 266 | func (c *Container) exec(command string, arguments ...any) string { |
| 267 | cliCommand := fmt.Sprintf(command, arguments...) |
| 268 | containerExecCommand := "docker exec" + c.getEnvVarsAsCliOption() + |
| 269 | " " + c.name + " " + cliCommand |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 270 | c.suite.T().Helper() |
| 271 | c.suite.log(containerExecCommand) |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 272 | byteOutput, err := exechelper.CombinedOutput(containerExecCommand) |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 273 | c.suite.assertNil(err) |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 274 | return string(byteOutput) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 275 | } |
| 276 | |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 277 | func (c *Container) getLogDirPath() string { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 278 | testId := c.suite.getTestId() |
| 279 | testName := c.suite.T().Name() |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 280 | logDirPath := logDir + testName + "/" + testId + "/" |
| 281 | |
| 282 | cmd := exec.Command("mkdir", "-p", logDirPath) |
| 283 | if err := cmd.Run(); err != nil { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 284 | c.suite.T().Fatalf("mkdir error: %v", err) |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | return logDirPath |
| 288 | } |
| 289 | |
| 290 | func (c *Container) saveLogs() { |
| 291 | cmd := exec.Command("docker", "inspect", "--format='{{.State.Status}}'", c.name) |
| 292 | if output, _ := cmd.CombinedOutput(); !strings.Contains(string(output), "running") { |
| 293 | return |
| 294 | } |
| 295 | |
| 296 | testLogFilePath := c.getLogDirPath() + "container-" + c.name + ".log" |
| 297 | |
| 298 | cmd = exec.Command("docker", "logs", "--details", "-t", c.name) |
| 299 | output, err := cmd.CombinedOutput() |
| 300 | if err != nil { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 301 | c.suite.T().Fatalf("fetching logs error: %v", err) |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | f, err := os.Create(testLogFilePath) |
| 305 | if err != nil { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 306 | c.suite.T().Fatalf("file create error: %v", err) |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 307 | } |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 308 | fmt.Fprint(f, string(output)) |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 309 | f.Close() |
| 310 | } |
| 311 | |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 312 | func (c *Container) log() string { |
| 313 | cmd := "docker logs " + c.name |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 314 | c.suite.log(cmd) |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 315 | o, err := exechelper.CombinedOutput(cmd) |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 316 | c.suite.assertNil(err) |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 317 | return string(o) |
| 318 | } |
| 319 | |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 320 | func (c *Container) stop() error { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 321 | if c.vppInstance != nil && c.vppInstance.apiChannel != nil { |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 322 | c.vppInstance.saveLogs() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 323 | c.vppInstance.disconnect() |
| 324 | } |
| 325 | c.vppInstance = nil |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 326 | c.saveLogs() |
Florin Coras | f4fe016 | 2023-01-17 13:02:51 -0800 | [diff] [blame] | 327 | return exechelper.Run("docker stop " + c.name + " -t 0") |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 328 | } |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 329 | |
| 330 | func (c *Container) createConfig(targetConfigName string, templateName string, values any) { |
| 331 | template := template.Must(template.ParseFiles(templateName)) |
| 332 | |
| 333 | f, err := os.CreateTemp("/tmp/hs-test/", "hst-config") |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 334 | c.suite.assertNil(err) |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 335 | defer os.Remove(f.Name()) |
| 336 | |
| 337 | err = template.Execute(f, values) |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 338 | c.suite.assertNil(err) |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 339 | |
| 340 | err = f.Close() |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 341 | c.suite.assertNil(err) |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 342 | |
| 343 | c.copy(f.Name(), targetConfigName) |
| 344 | } |
Maros Ondrejicka | f4ddf16 | 2023-03-08 16:01:43 +0100 | [diff] [blame] | 345 | |
| 346 | func init() { |
| 347 | cmd := exec.Command("mkdir", "-p", logDir) |
| 348 | if err := cmd.Run(); err != nil { |
| 349 | panic(err) |
| 350 | } |
| 351 | } |