Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 1 | package main |
| 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 | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 8 | |
| 9 | "github.com/edwarnicke/exechelper" |
| 10 | ) |
| 11 | |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 12 | var ( |
| 13 | workDir, _ = os.Getwd() |
| 14 | ) |
| 15 | |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 16 | type Volume struct { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 17 | hostDir string |
| 18 | containerDir string |
| 19 | isDefaultWorkDir bool |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | type Container struct { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 23 | suite *HstSuite |
Filip Tehlar | 3f95143 | 2023-01-13 21:33:43 +0100 | [diff] [blame] | 24 | isOptional bool |
| 25 | name string |
| 26 | image string |
Filip Tehlar | 3f95143 | 2023-01-13 21:33:43 +0100 | [diff] [blame] | 27 | extraRunningArgs string |
| 28 | volumes map[string]Volume |
| 29 | envVars map[string]string |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 30 | vppInstance *VppInstance |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | func NewContainer(yamlInput ContainerConfig) (*Container, error) { |
| 34 | containerName := yamlInput["name"].(string) |
| 35 | if len(containerName) == 0 { |
| 36 | err := fmt.Errorf("container name must not be blank") |
| 37 | return nil, err |
| 38 | } |
| 39 | |
| 40 | var container = new(Container) |
| 41 | container.volumes = make(map[string]Volume) |
| 42 | container.envVars = make(map[string]string) |
| 43 | container.name = containerName |
| 44 | |
| 45 | if image, ok := yamlInput["image"]; ok { |
| 46 | container.image = image.(string) |
| 47 | } else { |
| 48 | container.image = "hs-test/vpp" |
| 49 | } |
| 50 | |
Filip Tehlar | 3f95143 | 2023-01-13 21:33:43 +0100 | [diff] [blame] | 51 | if args, ok := yamlInput["extra-args"]; ok { |
| 52 | container.extraRunningArgs = args.(string) |
| 53 | } else { |
| 54 | container.extraRunningArgs = "" |
| 55 | } |
| 56 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 57 | if isOptional, ok := yamlInput["is-optional"]; ok { |
| 58 | container.isOptional = isOptional.(bool) |
| 59 | } else { |
| 60 | container.isOptional = false |
| 61 | } |
| 62 | |
| 63 | if _, ok := yamlInput["volumes"]; ok { |
Filip Tehlar | f3ee2b6 | 2023-01-09 12:07:09 +0100 | [diff] [blame] | 64 | r := strings.NewReplacer("$HST_DIR", workDir) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 65 | for _, volu := range yamlInput["volumes"].([]interface{}) { |
| 66 | volumeMap := volu.(ContainerConfig) |
| 67 | hostDir := r.Replace(volumeMap["host-dir"].(string)) |
| 68 | containerDir := volumeMap["container-dir"].(string) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 69 | isDefaultWorkDir := false |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 70 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 71 | if isDefault, ok := volumeMap["is-default-work-dir"]; ok { |
| 72 | isDefaultWorkDir = isDefault.(bool) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 75 | container.addVolume(hostDir, containerDir, isDefaultWorkDir) |
| 76 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | if _, ok := yamlInput["vars"]; ok { |
| 81 | for _, envVar := range yamlInput["vars"].([]interface{}) { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 82 | envVarMap := envVar.(ContainerConfig) |
| 83 | name := envVarMap["name"].(string) |
| 84 | value := envVarMap["value"].(string) |
| 85 | container.addEnvVar(name, value) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | return container, nil |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 91 | func (c *Container) Suite() *HstSuite { |
| 92 | return c.suite |
| 93 | } |
| 94 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 95 | func (c *Container) getWorkDirVolume() (res Volume, exists bool) { |
| 96 | for _, v := range c.volumes { |
| 97 | if v.isDefaultWorkDir { |
| 98 | res = v |
| 99 | exists = true |
| 100 | return |
| 101 | } |
| 102 | } |
| 103 | return |
| 104 | } |
| 105 | |
| 106 | func (c *Container) GetHostWorkDir() (res string) { |
| 107 | if v, ok := c.getWorkDirVolume(); ok { |
| 108 | res = v.hostDir |
| 109 | } |
| 110 | return |
| 111 | } |
| 112 | |
| 113 | func (c *Container) GetContainerWorkDir() (res string) { |
| 114 | if v, ok := c.getWorkDirVolume(); ok { |
| 115 | res = v.containerDir |
| 116 | } |
| 117 | return |
| 118 | } |
| 119 | |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 120 | func (c *Container) getRunCommand() string { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 121 | cmd := "docker run --cap-add=all -d --privileged --network host --rm" |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 122 | cmd += c.getVolumesAsCliOption() |
| 123 | cmd += c.getEnvVarsAsCliOption() |
Filip Tehlar | 3f95143 | 2023-01-13 21:33:43 +0100 | [diff] [blame] | 124 | cmd += " --name " + c.name + " " + c.image + " " + c.extraRunningArgs |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 125 | return cmd |
| 126 | } |
| 127 | |
| 128 | func (c *Container) run() error { |
| 129 | if c.name == "" { |
| 130 | return fmt.Errorf("run container failed: name is blank") |
| 131 | } |
| 132 | |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 133 | cmd := c.getRunCommand() |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 134 | err := exechelper.Run(cmd) |
| 135 | if err != nil { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 136 | return fmt.Errorf("container run failed: %s", err) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | return nil |
| 140 | } |
| 141 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 142 | func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 143 | var volume Volume |
| 144 | volume.hostDir = hostDir |
| 145 | volume.containerDir = containerDir |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 146 | volume.isDefaultWorkDir = isDefaultWorkDir |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 147 | c.volumes[hostDir] = volume |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 150 | func (c *Container) getVolumesAsCliOption() string { |
| 151 | cliOption := "" |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 152 | |
| 153 | if len(c.volumes) > 0 { |
| 154 | for _, volume := range c.volumes { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 155 | cliOption += fmt.Sprintf(" -v %s:%s", volume.hostDir, volume.containerDir) |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 159 | return cliOption |
| 160 | } |
| 161 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 162 | func (c *Container) addEnvVar(name string, value string) { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 163 | c.envVars[name] = value |
| 164 | } |
| 165 | |
| 166 | func (c *Container) getEnvVarsAsCliOption() string { |
| 167 | cliOption := "" |
| 168 | if len(c.envVars) == 0 { |
| 169 | return cliOption |
| 170 | } |
| 171 | |
| 172 | for name, value := range c.envVars { |
| 173 | cliOption += fmt.Sprintf(" -e %s=%s", name, value) |
| 174 | } |
| 175 | return cliOption |
| 176 | } |
| 177 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 178 | func (c *Container) newVppInstance(additionalConfig ...Stanza) (*VppInstance, error) { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 179 | vpp := new(VppInstance) |
| 180 | vpp.container = c |
Maros Ondrejicka | 7d7ab10 | 2023-02-14 12:56:49 +0100 | [diff] [blame] | 181 | |
| 182 | if len(additionalConfig) > 0 { |
| 183 | vpp.additionalConfig = additionalConfig[0] |
| 184 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 185 | |
| 186 | c.vppInstance = vpp |
| 187 | |
| 188 | return vpp, nil |
| 189 | } |
| 190 | |
| 191 | func (c *Container) copy(sourceFileName string, targetFileName string) error { |
| 192 | cmd := exec.Command("docker", "cp", sourceFileName, c.name+":"+targetFileName) |
| 193 | return cmd.Run() |
| 194 | } |
| 195 | |
| 196 | func (c *Container) createFile(destFileName string, content string) error { |
| 197 | f, err := os.CreateTemp("/tmp", "hst-config") |
| 198 | if err != nil { |
| 199 | return err |
| 200 | } |
| 201 | defer os.Remove(f.Name()) |
| 202 | |
| 203 | if _, err := f.Write([]byte(content)); err != nil { |
| 204 | return err |
| 205 | } |
| 206 | if err := f.Close(); err != nil { |
| 207 | return err |
| 208 | } |
| 209 | c.copy(f.Name(), destFileName) |
| 210 | return nil |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Executes in detached mode so that the started application can continue to run |
| 215 | * without blocking execution of test |
| 216 | */ |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 217 | func (c *Container) execServer(command string, arguments ...any) { |
| 218 | serverCommand := fmt.Sprintf(command, arguments...) |
| 219 | containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() + |
| 220 | " " + c.name + " " + serverCommand |
| 221 | c.Suite().log(containerExecCommand) |
| 222 | c.Suite().assertNil(exechelper.Run(containerExecCommand)) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 225 | func (c *Container) exec(command string, arguments ...any) string { |
| 226 | cliCommand := fmt.Sprintf(command, arguments...) |
| 227 | containerExecCommand := "docker exec" + c.getEnvVarsAsCliOption() + |
| 228 | " " + c.name + " " + cliCommand |
| 229 | c.Suite().log(containerExecCommand) |
| 230 | byteOutput, err := exechelper.CombinedOutput(containerExecCommand) |
| 231 | c.Suite().assertNil(err) |
| 232 | return string(byteOutput) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 235 | func (c *Container) stop() error { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 236 | if c.vppInstance != nil && c.vppInstance.apiChannel != nil { |
| 237 | c.vppInstance.disconnect() |
| 238 | } |
| 239 | c.vppInstance = nil |
Florin Coras | f4fe016 | 2023-01-17 13:02:51 -0800 | [diff] [blame] | 240 | return exechelper.Run("docker stop " + c.name + " -t 0") |
Maros Ondrejicka | 0e79abb | 2022-12-06 19:46:24 +0100 | [diff] [blame] | 241 | } |