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