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