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" |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 10 | "slices" |
| 11 | "strconv" |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 12 | "strings" |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 13 | "text/template" |
Filip Tehlar | 4fa0ba6 | 2023-12-06 11:35:11 +0100 | [diff] [blame] | 14 | "time" |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 15 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 16 | containerTypes "github.com/docker/docker/api/types/container" |
| 17 | "github.com/docker/docker/api/types/image" |
| 18 | "github.com/docker/docker/pkg/stdcopy" |
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 | |
Matus Fabian | e99d266 | 2024-07-19 16:04:09 +0200 | [diff] [blame^] | 385 | func (c *Container) GetFile(sourceFileName, targetFileName string) error { |
| 386 | cmd := exec.Command("docker", "cp", c.Name+":"+sourceFileName, targetFileName) |
| 387 | return cmd.Run() |
| 388 | } |
| 389 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 390 | /* |
| 391 | * Executes in detached mode so that the started application can continue to run |
| 392 | * without blocking execution of test |
| 393 | */ |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 394 | func (c *Container) ExecServer(command string, arguments ...any) { |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 395 | serverCommand := fmt.Sprintf(command, arguments...) |
| 396 | containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() + |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 397 | " " + c.Name + " " + serverCommand |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 398 | GinkgoHelper() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 399 | c.Suite.Log(containerExecCommand) |
| 400 | c.Suite.AssertNil(exechelper.Run(containerExecCommand)) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 401 | } |
| 402 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 403 | func (c *Container) Exec(command string, arguments ...any) string { |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 404 | cliCommand := fmt.Sprintf(command, arguments...) |
| 405 | containerExecCommand := "docker exec" + c.getEnvVarsAsCliOption() + |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 406 | " " + c.Name + " " + cliCommand |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 407 | GinkgoHelper() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 408 | c.Suite.Log(containerExecCommand) |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 409 | byteOutput, err := exechelper.CombinedOutput(containerExecCommand) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 410 | c.Suite.AssertNil(err, fmt.Sprint(err)) |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 411 | return string(byteOutput) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 412 | } |
| 413 | |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 414 | func (c *Container) getLogDirPath() string { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 415 | testId := c.Suite.GetTestId() |
| 416 | testName := c.Suite.GetCurrentTestName() |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 417 | logDirPath := logDir + testName + "/" + testId + "/" |
| 418 | |
| 419 | cmd := exec.Command("mkdir", "-p", logDirPath) |
| 420 | if err := cmd.Run(); err != nil { |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 421 | Fail("mkdir error: " + fmt.Sprint(err)) |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | return logDirPath |
| 425 | } |
| 426 | |
| 427 | func (c *Container) saveLogs() { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 428 | testLogFilePath := c.getLogDirPath() + "container-" + c.Name + ".log" |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 429 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 430 | logs, err := c.log(0) |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 431 | if err != nil { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 432 | c.Suite.Log(err) |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 433 | return |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | f, err := os.Create(testLogFilePath) |
| 437 | if err != nil { |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 438 | c.Suite.Log(err) |
| 439 | return |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 440 | } |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 441 | defer f.Close() |
| 442 | fmt.Fprint(f, logs) |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 443 | } |
| 444 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 445 | // 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] | 446 | func (c *Container) log(maxLines int) (string, error) { |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 447 | var logOptions containerTypes.LogsOptions |
adrianvillin | 7c67547 | 2024-02-12 02:44:53 -0500 | [diff] [blame] | 448 | if maxLines == 0 { |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 449 | logOptions = containerTypes.LogsOptions{ShowStdout: true, ShowStderr: true, Details: true} |
adrianvillin | 7c67547 | 2024-02-12 02:44:53 -0500 | [diff] [blame] | 450 | } else { |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 451 | logOptions = containerTypes.LogsOptions{ShowStdout: true, ShowStderr: true, Details: true, Tail: strconv.Itoa(maxLines)} |
adrianvillin | 7c67547 | 2024-02-12 02:44:53 -0500 | [diff] [blame] | 452 | } |
| 453 | |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 454 | out, err := c.Suite.Docker.ContainerLogs(c.ctx, c.ID, logOptions) |
| 455 | if err != nil { |
| 456 | c.Suite.Log(err) |
| 457 | return "", err |
| 458 | } |
| 459 | defer out.Close() |
| 460 | |
| 461 | var stdoutBuf, stderrBuf bytes.Buffer |
| 462 | |
| 463 | _, err = stdcopy.StdCopy(&stdoutBuf, &stderrBuf, out) |
| 464 | if err != nil { |
| 465 | c.Suite.Log(err) |
| 466 | } |
| 467 | |
| 468 | stdout := stdoutBuf.String() |
| 469 | stderr := stderrBuf.String() |
| 470 | |
Adrian Villin | 09b19fe | 2024-07-18 08:33:59 +0200 | [diff] [blame] | 471 | if strings.Contains(stdout, "==> /dev/null <==") { |
| 472 | stdout = "" |
| 473 | } |
| 474 | if strings.Contains(stderr, "tail: cannot open") { |
| 475 | stderr = "" |
| 476 | } |
| 477 | |
| 478 | return stdout + stderr, err |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 479 | } |
| 480 | |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 481 | func (c *Container) stop() error { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 482 | if c.VppInstance != nil && c.VppInstance.ApiStream != nil { |
| 483 | c.VppInstance.saveLogs() |
| 484 | c.VppInstance.Disconnect() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 485 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 486 | c.VppInstance = nil |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 487 | c.saveLogs() |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 488 | |
| 489 | c.Suite.Log("Stopping container " + c.Name) |
| 490 | timeout := 0 |
| 491 | if err := c.Suite.Docker.ContainerStop(c.ctx, c.ID, containerTypes.StopOptions{Timeout: &timeout}); err != nil { |
| 492 | return err |
| 493 | } |
| 494 | return nil |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 495 | } |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 496 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 497 | func (c *Container) CreateConfig(targetConfigName string, templateName string, values any) { |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 498 | template := template.Must(template.ParseFiles(templateName)) |
| 499 | |
Filip Tehlar | a1bd50c | 2024-01-24 11:59:44 +0100 | [diff] [blame] | 500 | f, err := os.CreateTemp(logDir, "hst-config") |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 501 | c.Suite.AssertNil(err, err) |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 502 | defer os.Remove(f.Name()) |
| 503 | |
| 504 | err = template.Execute(f, values) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 505 | c.Suite.AssertNil(err, err) |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 506 | |
| 507 | err = f.Close() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 508 | c.Suite.AssertNil(err, err) |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 509 | |
| 510 | c.copy(f.Name(), targetConfigName) |
| 511 | } |
Maros Ondrejicka | f4ddf16 | 2023-03-08 16:01:43 +0100 | [diff] [blame] | 512 | |
| 513 | func init() { |
| 514 | cmd := exec.Command("mkdir", "-p", logDir) |
| 515 | if err := cmd.Run(); err != nil { |
| 516 | panic(err) |
| 517 | } |
| 518 | } |