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