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() |
| 132 | args += " --name " + c.name + " " + c.image |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 133 | args += " " + c.extraRunningArgs |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 134 | return args |
| 135 | } |
| 136 | |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 137 | func (c *Container) create() error { |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 138 | cmd := "docker create " + c.getContainerArguments() |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 139 | c.suite.log(cmd) |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 140 | return exechelper.Run(cmd) |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 143 | func (c *Container) start() error { |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 144 | cmd := "docker start " + c.name |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 145 | c.suite.log(cmd) |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 146 | return exechelper.Run(cmd) |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 149 | func (c *Container) prepareCommand() (string, error) { |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 150 | if c.name == "" { |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 151 | return "", fmt.Errorf("run container failed: name is blank") |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 154 | cmd := "docker run " |
| 155 | if c.runDetached { |
| 156 | cmd += " -d" |
| 157 | } |
| 158 | cmd += " " + c.getContainerArguments() |
| 159 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 160 | c.suite.log(cmd) |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 161 | return cmd, nil |
| 162 | } |
| 163 | |
| 164 | func (c *Container) combinedOutput() (string, error) { |
| 165 | cmd, err := c.prepareCommand() |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 166 | if err != nil { |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 167 | return "", err |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 170 | byteOutput, err := exechelper.CombinedOutput(cmd) |
| 171 | return string(byteOutput), err |
| 172 | } |
| 173 | |
| 174 | func (c *Container) run() error { |
| 175 | cmd, err := c.prepareCommand() |
| 176 | if err != nil { |
| 177 | return err |
| 178 | } |
| 179 | |
| 180 | return exechelper.Run(cmd) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 183 | func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 184 | var volume Volume |
| 185 | volume.hostDir = hostDir |
| 186 | volume.containerDir = containerDir |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 187 | volume.isDefaultWorkDir = isDefaultWorkDir |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 188 | c.volumes[hostDir] = volume |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 191 | func (c *Container) getVolumesAsCliOption() string { |
| 192 | cliOption := "" |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 193 | |
| 194 | if len(c.volumes) > 0 { |
| 195 | for _, volume := range c.volumes { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 196 | cliOption += fmt.Sprintf(" -v %s:%s", volume.hostDir, volume.containerDir) |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 200 | return cliOption |
| 201 | } |
| 202 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 203 | func (c *Container) addEnvVar(name string, value string) { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 204 | c.envVars[name] = value |
| 205 | } |
| 206 | |
| 207 | func (c *Container) getEnvVarsAsCliOption() string { |
| 208 | cliOption := "" |
| 209 | if len(c.envVars) == 0 { |
| 210 | return cliOption |
| 211 | } |
| 212 | |
| 213 | for name, value := range c.envVars { |
| 214 | cliOption += fmt.Sprintf(" -e %s=%s", name, value) |
| 215 | } |
| 216 | return cliOption |
| 217 | } |
| 218 | |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 219 | func (c *Container) newVppInstance(cpus []int, additionalConfigs ...Stanza) (*VppInstance, error) { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 220 | vpp := new(VppInstance) |
| 221 | vpp.container = c |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 222 | vpp.cpus = cpus |
| 223 | vpp.additionalConfig = append(vpp.additionalConfig, additionalConfigs...) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 224 | c.vppInstance = vpp |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 225 | return vpp, nil |
| 226 | } |
| 227 | |
| 228 | func (c *Container) copy(sourceFileName string, targetFileName string) error { |
| 229 | cmd := exec.Command("docker", "cp", sourceFileName, c.name+":"+targetFileName) |
| 230 | return cmd.Run() |
| 231 | } |
| 232 | |
| 233 | func (c *Container) createFile(destFileName string, content string) error { |
| 234 | f, err := os.CreateTemp("/tmp", "hst-config") |
| 235 | if err != nil { |
| 236 | return err |
| 237 | } |
| 238 | defer os.Remove(f.Name()) |
| 239 | |
| 240 | if _, err := f.Write([]byte(content)); err != nil { |
| 241 | return err |
| 242 | } |
| 243 | if err := f.Close(); err != nil { |
| 244 | return err |
| 245 | } |
| 246 | c.copy(f.Name(), destFileName) |
| 247 | return nil |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Executes in detached mode so that the started application can continue to run |
| 252 | * without blocking execution of test |
| 253 | */ |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 254 | func (c *Container) execServer(command string, arguments ...any) { |
| 255 | serverCommand := fmt.Sprintf(command, arguments...) |
| 256 | containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() + |
| 257 | " " + c.name + " " + serverCommand |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 258 | c.suite.T().Helper() |
| 259 | c.suite.log(containerExecCommand) |
| 260 | c.suite.assertNil(exechelper.Run(containerExecCommand)) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 261 | } |
| 262 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 263 | func (c *Container) exec(command string, arguments ...any) string { |
| 264 | cliCommand := fmt.Sprintf(command, arguments...) |
| 265 | containerExecCommand := "docker exec" + c.getEnvVarsAsCliOption() + |
| 266 | " " + c.name + " " + cliCommand |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 267 | c.suite.T().Helper() |
| 268 | c.suite.log(containerExecCommand) |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 269 | byteOutput, err := exechelper.CombinedOutput(containerExecCommand) |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 270 | c.suite.assertNil(err) |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 271 | return string(byteOutput) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 274 | func (c *Container) getLogDirPath() string { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 275 | testId := c.suite.getTestId() |
| 276 | testName := c.suite.T().Name() |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 277 | logDirPath := logDir + testName + "/" + testId + "/" |
| 278 | |
| 279 | cmd := exec.Command("mkdir", "-p", logDirPath) |
| 280 | if err := cmd.Run(); err != nil { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 281 | c.suite.T().Fatalf("mkdir error: %v", err) |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | return logDirPath |
| 285 | } |
| 286 | |
| 287 | func (c *Container) saveLogs() { |
| 288 | cmd := exec.Command("docker", "inspect", "--format='{{.State.Status}}'", c.name) |
| 289 | if output, _ := cmd.CombinedOutput(); !strings.Contains(string(output), "running") { |
| 290 | return |
| 291 | } |
| 292 | |
| 293 | testLogFilePath := c.getLogDirPath() + "container-" + c.name + ".log" |
| 294 | |
| 295 | cmd = exec.Command("docker", "logs", "--details", "-t", c.name) |
| 296 | output, err := cmd.CombinedOutput() |
| 297 | if err != nil { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 298 | c.suite.T().Fatalf("fetching logs error: %v", err) |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | f, err := os.Create(testLogFilePath) |
| 302 | if err != nil { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 303 | c.suite.T().Fatalf("file create error: %v", err) |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 304 | } |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 305 | fmt.Fprint(f, string(output)) |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 306 | f.Close() |
| 307 | } |
| 308 | |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 309 | func (c *Container) log() string { |
| 310 | cmd := "docker logs " + c.name |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 311 | c.suite.log(cmd) |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 312 | o, err := exechelper.CombinedOutput(cmd) |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 313 | c.suite.assertNil(err) |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 314 | return string(o) |
| 315 | } |
| 316 | |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 317 | func (c *Container) stop() error { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 318 | if c.vppInstance != nil && c.vppInstance.apiChannel != nil { |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 319 | c.vppInstance.saveLogs() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 320 | c.vppInstance.disconnect() |
| 321 | } |
| 322 | c.vppInstance = nil |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 323 | c.saveLogs() |
Florin Coras | f4fe016 | 2023-01-17 13:02:51 -0800 | [diff] [blame] | 324 | return exechelper.Run("docker stop " + c.name + " -t 0") |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 325 | } |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 326 | |
| 327 | func (c *Container) createConfig(targetConfigName string, templateName string, values any) { |
| 328 | template := template.Must(template.ParseFiles(templateName)) |
| 329 | |
| 330 | f, err := os.CreateTemp("/tmp/hs-test/", "hst-config") |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 331 | c.suite.assertNil(err) |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 332 | defer os.Remove(f.Name()) |
| 333 | |
| 334 | err = template.Execute(f, values) |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 335 | c.suite.assertNil(err) |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 336 | |
| 337 | err = f.Close() |
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 | c.copy(f.Name(), targetConfigName) |
| 341 | } |
Maros Ondrejicka | f4ddf16 | 2023-03-08 16:01:43 +0100 | [diff] [blame] | 342 | |
| 343 | func init() { |
| 344 | cmd := exec.Command("mkdir", "-p", logDir) |
| 345 | if err := cmd.Run(); err != nil { |
| 346 | panic(err) |
| 347 | } |
| 348 | } |