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