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