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