Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1 | package hst |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 2 | |
| 3 | import ( |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 4 | "bytes" |
| 5 | "context" |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 6 | "fmt" |
Matus Fabian | e99d266 | 2024-07-19 16:04:09 +0200 | [diff] [blame] | 7 | "github.com/docker/go-units" |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 8 | "os" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 9 | "os/exec" |
Matus Fabian | d464338 | 2024-07-29 13:30:23 +0200 | [diff] [blame] | 10 | "regexp" |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 11 | "slices" |
| 12 | "strconv" |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 13 | "strings" |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 14 | "text/template" |
Filip Tehlar | 4fa0ba6 | 2023-12-06 11:35:11 +0100 | [diff] [blame] | 15 | "time" |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 16 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 17 | containerTypes "github.com/docker/docker/api/types/container" |
Adrian Villin | 007be4f | 2024-07-23 12:14:19 +0200 | [diff] [blame] | 18 | "github.com/docker/docker/api/types/filters" |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 19 | "github.com/docker/docker/api/types/image" |
| 20 | "github.com/docker/docker/pkg/stdcopy" |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 21 | "github.com/edwarnicke/exechelper" |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 22 | . "github.com/onsi/ginkgo/v2" |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 23 | ) |
| 24 | |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 25 | const ( |
Filip Tehlar | a1bd50c | 2024-01-24 11:59:44 +0100 | [diff] [blame] | 26 | logDir string = "/tmp/hs-test/" |
| 27 | volumeDir string = "/volumes" |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 28 | ) |
| 29 | |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 30 | var ( |
| 31 | workDir, _ = os.Getwd() |
| 32 | ) |
| 33 | |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 34 | type Volume struct { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 35 | HostDir string |
| 36 | ContainerDir string |
| 37 | IsDefaultWorkDir bool |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | type Container struct { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 41 | Suite *HstSuite |
| 42 | IsOptional bool |
| 43 | RunDetached bool |
| 44 | Name string |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 45 | ID string |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 46 | Image string |
| 47 | ExtraRunningArgs string |
| 48 | Volumes map[string]Volume |
| 49 | EnvVars map[string]string |
| 50 | VppInstance *VppInstance |
| 51 | AllocatedCpus []int |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 52 | ctx context.Context |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 55 | func newContainer(suite *HstSuite, yamlInput ContainerConfig) (*Container, error) { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 56 | containerName := yamlInput["name"].(string) |
| 57 | if len(containerName) == 0 { |
| 58 | err := fmt.Errorf("container name must not be blank") |
| 59 | return nil, err |
| 60 | } |
| 61 | |
| 62 | var container = new(Container) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 63 | container.Volumes = make(map[string]Volume) |
| 64 | container.EnvVars = make(map[string]string) |
| 65 | container.Name = containerName |
| 66 | container.Suite = suite |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 67 | container.ctx = context.Background() |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 68 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 69 | if Image, ok := yamlInput["image"]; ok { |
| 70 | container.Image = Image.(string) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 71 | } else { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 72 | container.Image = "hs-test/vpp" |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Filip Tehlar | 3f95143 | 2023-01-13 21:33:43 +0100 | [diff] [blame] | 75 | if args, ok := yamlInput["extra-args"]; ok { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 76 | container.ExtraRunningArgs = args.(string) |
Filip Tehlar | 3f95143 | 2023-01-13 21:33:43 +0100 | [diff] [blame] | 77 | } else { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 78 | container.ExtraRunningArgs = "" |
Filip Tehlar | 3f95143 | 2023-01-13 21:33:43 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 81 | if isOptional, ok := yamlInput["is-optional"]; ok { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 82 | container.IsOptional = isOptional.(bool) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 83 | } else { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 84 | container.IsOptional = false |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 87 | if runDetached, ok := yamlInput["run-detached"]; ok { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 88 | container.RunDetached = runDetached.(bool) |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 89 | } else { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 90 | container.RunDetached = true |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 93 | if _, ok := yamlInput["volumes"]; ok { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 94 | workingVolumeDir := logDir + suite.GetCurrentTestName() + volumeDir |
Filip Tehlar | a1bd50c | 2024-01-24 11:59:44 +0100 | [diff] [blame] | 95 | workDirReplacer := strings.NewReplacer("$HST_DIR", workDir) |
| 96 | volDirReplacer := strings.NewReplacer("$HST_VOLUME_DIR", workingVolumeDir) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 97 | for _, volu := range yamlInput["volumes"].([]interface{}) { |
| 98 | volumeMap := volu.(ContainerConfig) |
Filip Tehlar | a1bd50c | 2024-01-24 11:59:44 +0100 | [diff] [blame] | 99 | hostDir := workDirReplacer.Replace(volumeMap["host-dir"].(string)) |
| 100 | hostDir = volDirReplacer.Replace(hostDir) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 101 | containerDir := volumeMap["container-dir"].(string) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 102 | isDefaultWorkDir := false |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 103 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 104 | if isDefault, ok := volumeMap["is-default-work-dir"]; ok { |
| 105 | isDefaultWorkDir = isDefault.(bool) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 106 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 107 | container.addVolume(hostDir, containerDir, isDefaultWorkDir) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | if _, ok := yamlInput["vars"]; ok { |
| 112 | for _, envVar := range yamlInput["vars"].([]interface{}) { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 113 | envVarMap := envVar.(ContainerConfig) |
| 114 | name := envVarMap["name"].(string) |
| 115 | value := envVarMap["value"].(string) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 116 | container.AddEnvVar(name, value) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | return container, nil |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 122 | func (c *Container) getWorkDirVolume() (res Volume, exists bool) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 123 | for _, v := range c.Volumes { |
| 124 | if v.IsDefaultWorkDir { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 125 | res = v |
| 126 | exists = true |
| 127 | return |
| 128 | } |
| 129 | } |
| 130 | return |
| 131 | } |
| 132 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 133 | func (c *Container) GetHostWorkDir() (res string) { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 134 | if v, ok := c.getWorkDirVolume(); ok { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 135 | res = v.HostDir |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 136 | } |
| 137 | return |
| 138 | } |
| 139 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 140 | func (c *Container) GetContainerWorkDir() (res string) { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 141 | if v, ok := c.getWorkDirVolume(); ok { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 142 | res = v.ContainerDir |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 143 | } |
| 144 | return |
| 145 | } |
| 146 | |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 147 | func (c *Container) getContainerArguments() string { |
Adrian Villin | eaa7d91 | 2024-06-06 04:26:30 -0400 | [diff] [blame] | 148 | args := "--ulimit nofile=90000:90000 --cap-add=all --privileged --network host" |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 149 | args += c.getVolumesAsCliOption() |
| 150 | args += c.getEnvVarsAsCliOption() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 151 | if *VppSourceFileDir != "" { |
| 152 | args += fmt.Sprintf(" -v %s:%s", *VppSourceFileDir, *VppSourceFileDir) |
Filip Tehlar | 109f3ce | 2023-09-05 15:36:28 +0200 | [diff] [blame] | 153 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 154 | args += " --name " + c.Name + " " + c.Image |
| 155 | args += " " + c.ExtraRunningArgs |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 156 | return args |
| 157 | } |
| 158 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 159 | func (c *Container) PullDockerImage(name string, ctx context.Context) { |
| 160 | // "func (*Client) ImagePull" doesn't work, returns "No such image" |
| 161 | c.Suite.Log("Pulling image: " + name) |
| 162 | _, err := exechelper.CombinedOutput("docker pull " + name) |
| 163 | c.Suite.AssertNil(err) |
Filip Tehlar | 4fa0ba6 | 2023-12-06 11:35:11 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 166 | // Creates a container |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 167 | func (c *Container) Create() error { |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 168 | var sliceOfImageNames []string |
| 169 | images, err := c.Suite.Docker.ImageList(c.ctx, image.ListOptions{}) |
| 170 | c.Suite.AssertNil(err) |
| 171 | |
| 172 | for _, image := range images { |
| 173 | sliceOfImageNames = append(sliceOfImageNames, strings.Split(image.RepoTags[0], ":")[0]) |
| 174 | } |
| 175 | if !slices.Contains(sliceOfImageNames, c.Image) { |
| 176 | c.PullDockerImage(c.Image, c.ctx) |
| 177 | } |
| 178 | |
| 179 | c.allocateCpus() |
| 180 | cpuSet := fmt.Sprintf("%d-%d", c.AllocatedCpus[0], c.AllocatedCpus[len(c.AllocatedCpus)-1]) |
| 181 | resp, err := c.Suite.Docker.ContainerCreate( |
| 182 | c.ctx, |
| 183 | &containerTypes.Config{ |
Adrian Villin | 007be4f | 2024-07-23 12:14:19 +0200 | [diff] [blame] | 184 | Hostname: c.Name, |
Matus Fabian | d464338 | 2024-07-29 13:30:23 +0200 | [diff] [blame] | 185 | Image: c.Image, |
| 186 | Env: c.getEnvVars(), |
| 187 | Cmd: strings.Split(c.ExtraRunningArgs, " "), |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 188 | }, |
| 189 | &containerTypes.HostConfig{ |
| 190 | Resources: containerTypes.Resources{ |
| 191 | Ulimits: []*units.Ulimit{ |
| 192 | { |
| 193 | Name: "nofile", |
| 194 | Soft: 90000, |
| 195 | Hard: 90000, |
| 196 | }, |
| 197 | }, |
| 198 | CpusetCpus: cpuSet, |
| 199 | }, |
| 200 | CapAdd: []string{"ALL"}, |
| 201 | Privileged: true, |
| 202 | NetworkMode: "host", |
| 203 | Binds: c.getVolumesAsSlice(), |
| 204 | }, |
| 205 | nil, |
| 206 | nil, |
| 207 | c.Name, |
| 208 | ) |
| 209 | c.ID = resp.ID |
| 210 | return err |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Adrian Villin | b9464cd | 2024-05-27 09:52:59 -0400 | [diff] [blame] | 213 | func (c *Container) allocateCpus() { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 214 | c.Suite.StartedContainers = append(c.Suite.StartedContainers, c) |
| 215 | c.AllocatedCpus = c.Suite.AllocateCpus() |
| 216 | c.Suite.Log("Allocated CPUs " + fmt.Sprint(c.AllocatedCpus) + " to container " + c.Name) |
Adrian Villin | b9464cd | 2024-05-27 09:52:59 -0400 | [diff] [blame] | 217 | } |
| 218 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 219 | // Starts a container |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 220 | func (c *Container) Start() error { |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 221 | var err error |
Adrian Villin | 007be4f | 2024-07-23 12:14:19 +0200 | [diff] [blame] | 222 | var nTries int |
| 223 | |
| 224 | for nTries = 0; nTries < 5; nTries++ { |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 225 | err = c.Suite.Docker.ContainerStart(c.ctx, c.ID, containerTypes.StartOptions{}) |
| 226 | if err == nil { |
| 227 | continue |
| 228 | } |
| 229 | c.Suite.Log("Error while starting " + c.Name + ". Retrying...") |
| 230 | time.Sleep(1 * time.Second) |
| 231 | } |
Adrian Villin | 007be4f | 2024-07-23 12:14:19 +0200 | [diff] [blame] | 232 | if nTries >= 5 { |
| 233 | return err |
| 234 | } |
| 235 | |
| 236 | // wait for container to start |
| 237 | time.Sleep(1 * time.Second) |
| 238 | |
| 239 | // check if container exited right after startup |
| 240 | containers, err := c.Suite.Docker.ContainerList(c.ctx, containerTypes.ListOptions{ |
| 241 | All: true, |
| 242 | Filters: filters.NewArgs(filters.Arg("name", c.Name)), |
| 243 | }) |
| 244 | if err != nil { |
| 245 | return err |
| 246 | } |
| 247 | if containers[0].State == "exited" { |
| 248 | c.Suite.Log("Container details: " + fmt.Sprint(containers[0])) |
| 249 | return fmt.Errorf("Container %s exited: '%s'", c.Name, containers[0].Status) |
| 250 | } |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 251 | |
| 252 | return err |
| 253 | } |
| 254 | |
| 255 | func (c *Container) GetOutput() (string, string) { |
| 256 | // Wait for the container to finish executing |
| 257 | statusCh, errCh := c.Suite.Docker.ContainerWait(c.ctx, c.ID, containerTypes.WaitConditionNotRunning) |
| 258 | select { |
| 259 | case err := <-errCh: |
| 260 | c.Suite.AssertNil(err) |
| 261 | case <-statusCh: |
| 262 | } |
| 263 | |
| 264 | // Get the logs from the container |
| 265 | logOptions := containerTypes.LogsOptions{ShowStdout: true, ShowStderr: true} |
| 266 | logReader, err := c.Suite.Docker.ContainerLogs(c.ctx, c.ID, logOptions) |
| 267 | c.Suite.AssertNil(err) |
| 268 | defer logReader.Close() |
| 269 | |
| 270 | var stdoutBuf, stderrBuf bytes.Buffer |
| 271 | |
| 272 | // Use stdcopy.StdCopy to demultiplex the multiplexed stream |
| 273 | _, err = stdcopy.StdCopy(&stdoutBuf, &stderrBuf, logReader) |
| 274 | c.Suite.AssertNil(err) |
| 275 | |
| 276 | stdout := stdoutBuf.String() |
| 277 | stderr := stderrBuf.String() |
| 278 | return stdout, stderr |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 281 | func (c *Container) prepareCommand() (string, error) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 282 | if c.Name == "" { |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 283 | return "", fmt.Errorf("run container failed: name is blank") |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 286 | cmd := "docker exec " |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 287 | if c.RunDetached { |
Adrian Villin | eaa7d91 | 2024-06-06 04:26:30 -0400 | [diff] [blame] | 288 | cmd += " -d" |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 289 | } |
Adrian Villin | b9464cd | 2024-05-27 09:52:59 -0400 | [diff] [blame] | 290 | |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 291 | cmd += " " + c.getContainerArguments() |
| 292 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 293 | c.Suite.Log(cmd) |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 294 | return cmd, nil |
| 295 | } |
| 296 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 297 | func (c *Container) CombinedOutput() (string, error) { |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 298 | cmd, err := c.prepareCommand() |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 299 | if err != nil { |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 300 | return "", err |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 303 | byteOutput, err := exechelper.CombinedOutput(cmd) |
| 304 | return string(byteOutput), err |
| 305 | } |
| 306 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 307 | // Creates and starts a container |
| 308 | func (c *Container) Run() { |
| 309 | c.Suite.AssertNil(c.Create()) |
| 310 | c.Suite.AssertNil(c.Start()) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 311 | } |
| 312 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 313 | func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 314 | var volume Volume |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 315 | volume.HostDir = hostDir |
| 316 | volume.ContainerDir = containerDir |
| 317 | volume.IsDefaultWorkDir = isDefaultWorkDir |
| 318 | c.Volumes[hostDir] = volume |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 319 | } |
| 320 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 321 | func (c *Container) getVolumesAsSlice() []string { |
| 322 | var volumeSlice []string |
| 323 | |
| 324 | if *VppSourceFileDir != "" { |
| 325 | volumeSlice = append(volumeSlice, fmt.Sprintf("%s:%s", *VppSourceFileDir, *VppSourceFileDir)) |
| 326 | } |
| 327 | |
| 328 | if len(c.Volumes) > 0 { |
| 329 | for _, volume := range c.Volumes { |
| 330 | volumeSlice = append(volumeSlice, fmt.Sprintf("%s:%s", volume.HostDir, volume.ContainerDir)) |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | return volumeSlice |
| 335 | } |
| 336 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 337 | func (c *Container) getVolumesAsCliOption() string { |
| 338 | cliOption := "" |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 339 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 340 | if len(c.Volumes) > 0 { |
| 341 | for _, volume := range c.Volumes { |
| 342 | cliOption += fmt.Sprintf(" -v %s:%s", volume.HostDir, volume.ContainerDir) |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 346 | return cliOption |
| 347 | } |
| 348 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 349 | func (c *Container) AddEnvVar(name string, value string) { |
| 350 | c.EnvVars[name] = value |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | func (c *Container) getEnvVarsAsCliOption() string { |
| 354 | cliOption := "" |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 355 | if len(c.EnvVars) == 0 { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 356 | return cliOption |
| 357 | } |
| 358 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 359 | for name, value := range c.EnvVars { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 360 | cliOption += fmt.Sprintf(" -e %s=%s", name, value) |
| 361 | } |
| 362 | return cliOption |
| 363 | } |
| 364 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 365 | func (c *Container) getEnvVars() []string { |
| 366 | var envVars []string |
| 367 | if len(c.EnvVars) == 0 { |
| 368 | return envVars |
| 369 | } |
| 370 | |
| 371 | for name, value := range c.EnvVars { |
| 372 | envVars = append(envVars, fmt.Sprintf("%s=%s", name, value)) |
| 373 | } |
| 374 | return envVars |
| 375 | } |
| 376 | |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 377 | func (c *Container) newVppInstance(cpus []int, additionalConfigs ...Stanza) (*VppInstance, error) { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 378 | vpp := new(VppInstance) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 379 | vpp.Container = c |
| 380 | vpp.Cpus = cpus |
Hadi Rayan Al-Sandid | e0e8513 | 2024-06-24 10:28:58 +0200 | [diff] [blame] | 381 | vpp.setDefaultCpuConfig() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 382 | vpp.AdditionalConfig = append(vpp.AdditionalConfig, additionalConfigs...) |
| 383 | c.VppInstance = vpp |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 384 | return vpp, nil |
| 385 | } |
| 386 | |
| 387 | func (c *Container) copy(sourceFileName string, targetFileName string) error { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 388 | cmd := exec.Command("docker", "cp", sourceFileName, c.Name+":"+targetFileName) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 389 | return cmd.Run() |
| 390 | } |
| 391 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 392 | func (c *Container) CreateFile(destFileName string, content string) error { |
| 393 | f, err := os.CreateTemp("/tmp", "hst-config"+c.Suite.Ppid) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 394 | if err != nil { |
| 395 | return err |
| 396 | } |
| 397 | defer os.Remove(f.Name()) |
| 398 | |
| 399 | if _, err := f.Write([]byte(content)); err != nil { |
| 400 | return err |
| 401 | } |
| 402 | if err := f.Close(); err != nil { |
| 403 | return err |
| 404 | } |
| 405 | c.copy(f.Name(), destFileName) |
| 406 | return nil |
| 407 | } |
| 408 | |
Matus Fabian | e99d266 | 2024-07-19 16:04:09 +0200 | [diff] [blame] | 409 | func (c *Container) GetFile(sourceFileName, targetFileName string) error { |
| 410 | cmd := exec.Command("docker", "cp", c.Name+":"+sourceFileName, targetFileName) |
| 411 | return cmd.Run() |
| 412 | } |
| 413 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 414 | /* |
| 415 | * Executes in detached mode so that the started application can continue to run |
| 416 | * without blocking execution of test |
| 417 | */ |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 418 | func (c *Container) ExecServer(command string, arguments ...any) { |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 419 | serverCommand := fmt.Sprintf(command, arguments...) |
| 420 | containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() + |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 421 | " " + c.Name + " " + serverCommand |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 422 | GinkgoHelper() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 423 | c.Suite.Log(containerExecCommand) |
| 424 | c.Suite.AssertNil(exechelper.Run(containerExecCommand)) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 425 | } |
| 426 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 427 | func (c *Container) Exec(command string, arguments ...any) string { |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 428 | cliCommand := fmt.Sprintf(command, arguments...) |
| 429 | containerExecCommand := "docker exec" + c.getEnvVarsAsCliOption() + |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 430 | " " + c.Name + " " + cliCommand |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 431 | GinkgoHelper() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 432 | c.Suite.Log(containerExecCommand) |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 433 | byteOutput, err := exechelper.CombinedOutput(containerExecCommand) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 434 | c.Suite.AssertNil(err, fmt.Sprint(err)) |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 435 | return string(byteOutput) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 436 | } |
| 437 | |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 438 | func (c *Container) getLogDirPath() string { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 439 | testId := c.Suite.GetTestId() |
| 440 | testName := c.Suite.GetCurrentTestName() |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 441 | logDirPath := logDir + testName + "/" + testId + "/" |
| 442 | |
| 443 | cmd := exec.Command("mkdir", "-p", logDirPath) |
| 444 | if err := cmd.Run(); err != nil { |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 445 | Fail("mkdir error: " + fmt.Sprint(err)) |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | return logDirPath |
| 449 | } |
| 450 | |
| 451 | func (c *Container) saveLogs() { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 452 | testLogFilePath := c.getLogDirPath() + "container-" + c.Name + ".log" |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 453 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 454 | logs, err := c.log(0) |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 455 | if err != nil { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 456 | c.Suite.Log(err) |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 457 | return |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | f, err := os.Create(testLogFilePath) |
| 461 | if err != nil { |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 462 | c.Suite.Log(err) |
| 463 | return |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 464 | } |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 465 | defer f.Close() |
| 466 | fmt.Fprint(f, logs) |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 467 | } |
| 468 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 469 | // Returns logs from docker containers. Set 'maxLines' to 0 to output the full log. |
adrianvillin | 7c67547 | 2024-02-12 02:44:53 -0500 | [diff] [blame] | 470 | func (c *Container) log(maxLines int) (string, error) { |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 471 | var logOptions containerTypes.LogsOptions |
adrianvillin | 7c67547 | 2024-02-12 02:44:53 -0500 | [diff] [blame] | 472 | if maxLines == 0 { |
Matus Fabian | d464338 | 2024-07-29 13:30:23 +0200 | [diff] [blame] | 473 | logOptions = containerTypes.LogsOptions{ShowStdout: true, ShowStderr: true, Details: true, Timestamps: true} |
adrianvillin | 7c67547 | 2024-02-12 02:44:53 -0500 | [diff] [blame] | 474 | } else { |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 475 | logOptions = containerTypes.LogsOptions{ShowStdout: true, ShowStderr: true, Details: true, Tail: strconv.Itoa(maxLines)} |
adrianvillin | 7c67547 | 2024-02-12 02:44:53 -0500 | [diff] [blame] | 476 | } |
| 477 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 478 | out, err := c.Suite.Docker.ContainerLogs(c.ctx, c.ID, logOptions) |
| 479 | if err != nil { |
| 480 | c.Suite.Log(err) |
| 481 | return "", err |
| 482 | } |
| 483 | defer out.Close() |
| 484 | |
| 485 | var stdoutBuf, stderrBuf bytes.Buffer |
| 486 | |
| 487 | _, err = stdcopy.StdCopy(&stdoutBuf, &stderrBuf, out) |
| 488 | if err != nil { |
| 489 | c.Suite.Log(err) |
| 490 | } |
| 491 | |
| 492 | stdout := stdoutBuf.String() |
| 493 | stderr := stderrBuf.String() |
| 494 | |
Matus Fabian | d464338 | 2024-07-29 13:30:23 +0200 | [diff] [blame] | 495 | re := regexp.MustCompile("(?m)^.*==> /dev/null <==.*$[\r\n]+") |
| 496 | stdout = re.ReplaceAllString(stdout, "") |
Adrian Villin | d9da4ee | 2024-07-24 17:46:53 +0200 | [diff] [blame] | 497 | |
Matus Fabian | d464338 | 2024-07-29 13:30:23 +0200 | [diff] [blame] | 498 | re = regexp.MustCompile("(?m)^.*tail: cannot open '' for reading: No such file or directory.*$[\r\n]+") |
| 499 | stderr = re.ReplaceAllString(stderr, "") |
Adrian Villin | 09b19fe | 2024-07-18 08:33:59 +0200 | [diff] [blame] | 500 | |
| 501 | return stdout + stderr, err |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 502 | } |
| 503 | |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 504 | func (c *Container) stop() error { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 505 | if c.VppInstance != nil && c.VppInstance.ApiStream != nil { |
| 506 | c.VppInstance.saveLogs() |
| 507 | c.VppInstance.Disconnect() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 508 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 509 | c.VppInstance = nil |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 510 | c.saveLogs() |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 511 | |
| 512 | c.Suite.Log("Stopping container " + c.Name) |
| 513 | timeout := 0 |
| 514 | if err := c.Suite.Docker.ContainerStop(c.ctx, c.ID, containerTypes.StopOptions{Timeout: &timeout}); err != nil { |
| 515 | return err |
| 516 | } |
| 517 | return nil |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 518 | } |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 519 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 520 | func (c *Container) CreateConfig(targetConfigName string, templateName string, values any) { |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 521 | template := template.Must(template.ParseFiles(templateName)) |
| 522 | |
Filip Tehlar | a1bd50c | 2024-01-24 11:59:44 +0100 | [diff] [blame] | 523 | f, err := os.CreateTemp(logDir, "hst-config") |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 524 | c.Suite.AssertNil(err, err) |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 525 | defer os.Remove(f.Name()) |
| 526 | |
| 527 | err = template.Execute(f, values) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 528 | c.Suite.AssertNil(err, err) |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 529 | |
| 530 | err = f.Close() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 531 | c.Suite.AssertNil(err, err) |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 532 | |
| 533 | c.copy(f.Name(), targetConfigName) |
| 534 | } |
Maros Ondrejicka | f4ddf16 | 2023-03-08 16:01:43 +0100 | [diff] [blame] | 535 | |
| 536 | func init() { |
| 537 | cmd := exec.Command("mkdir", "-p", logDir) |
| 538 | if err := cmd.Run(); err != nil { |
| 539 | panic(err) |
| 540 | } |
| 541 | } |