9 "github.com/edwarnicke/exechelper"
18 type Container struct {
23 extraRunningArgs string
24 volumes map[string]Volume
25 envVars map[string]string
26 vppInstance *VppInstance
29 func NewContainer(yamlInput ContainerConfig) (*Container, error) {
30 containerName := yamlInput["name"].(string)
31 if len(containerName) == 0 {
32 err := fmt.Errorf("container name must not be blank")
36 var container = new(Container)
37 container.volumes = make(map[string]Volume)
38 container.envVars = make(map[string]string)
39 container.name = containerName
41 if image, ok := yamlInput["image"]; ok {
42 container.image = image.(string)
44 container.image = "hs-test/vpp"
47 if args, ok := yamlInput["extra-args"]; ok {
48 container.extraRunningArgs = args.(string)
50 container.extraRunningArgs = ""
53 if isOptional, ok := yamlInput["is-optional"]; ok {
54 container.isOptional = isOptional.(bool)
56 container.isOptional = false
59 if _, ok := yamlInput["volumes"]; ok {
60 r := strings.NewReplacer("$HST_DIR", workDir)
61 for _, volu := range yamlInput["volumes"].([]interface{}) {
62 volumeMap := volu.(ContainerConfig)
63 hostDir := r.Replace(volumeMap["host-dir"].(string))
64 containerDir := volumeMap["container-dir"].(string)
65 isDefaultWorkDir := false
67 if isDefault, ok := volumeMap["is-default-work-dir"]; ok {
68 isDefaultWorkDir = isDefault.(bool)
71 container.addVolume(hostDir, containerDir, isDefaultWorkDir)
76 if _, ok := yamlInput["vars"]; ok {
77 for _, envVar := range yamlInput["vars"].([]interface{}) {
78 envVarMap := envVar.(ContainerConfig)
79 name := envVarMap["name"].(string)
80 value := envVarMap["value"].(string)
81 container.addEnvVar(name, value)
87 func (c *Container) Suite() *HstSuite {
91 func (c *Container) getWorkDirVolume() (res Volume, exists bool) {
92 for _, v := range c.volumes {
93 if v.isDefaultWorkDir {
102 func (c *Container) GetHostWorkDir() (res string) {
103 if v, ok := c.getWorkDirVolume(); ok {
109 func (c *Container) GetContainerWorkDir() (res string) {
110 if v, ok := c.getWorkDirVolume(); ok {
116 func (c *Container) getRunCommand() string {
117 syncPath := fmt.Sprintf(" -v %s:/tmp/sync", c.getSyncPath())
118 cmd := "docker run --cap-add=all -d --privileged --network host --rm"
120 cmd += c.getVolumesAsCliOption()
121 cmd += c.getEnvVarsAsCliOption()
122 cmd += " --name " + c.name + " " + c.image + " " + c.extraRunningArgs
126 func (c *Container) run() error {
128 return fmt.Errorf("run container failed: name is blank")
131 exechelper.Run(fmt.Sprintf("mkdir -p /tmp/%s/sync", c.name))
132 cmd := c.getRunCommand()
133 err := exechelper.Run(cmd)
135 return fmt.Errorf("container run failed: %s", err)
141 func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) {
143 volume.hostDir = hostDir
144 volume.containerDir = containerDir
145 volume.isDefaultWorkDir = isDefaultWorkDir
146 c.volumes[hostDir] = volume
149 func (c *Container) getVolumeByHostDir(hostDir string) Volume {
150 return c.volumes[hostDir]
153 func (c *Container) getVolumesAsCliOption() string {
156 if len(c.volumes) > 0 {
157 for _, volume := range c.volumes {
158 cliOption += fmt.Sprintf(" -v %s:%s", volume.hostDir, volume.containerDir)
165 func (c *Container) getWorkDirAsCliOption() string {
166 if _, ok := c.getWorkDirVolume(); ok {
167 return fmt.Sprintf(" --workdir=\"%s\"", c.GetContainerWorkDir())
172 func (c *Container) addEnvVar(name string, value string) {
173 c.envVars[name] = value
176 func (c *Container) getEnvVarsAsCliOption() string {
178 if len(c.envVars) == 0 {
182 for name, value := range c.envVars {
183 cliOption += fmt.Sprintf(" -e %s=%s", name, value)
188 func (c *Container) getSyncPath() string {
189 return fmt.Sprintf("/tmp/%s/sync", c.name)
192 func (c *Container) newVppInstance(additionalConfig ...Stanza) (*VppInstance, error) {
193 vppConfig := new(VppConfig)
194 vppConfig.CliSocketFilePath = defaultCliSocketFilePath
195 if len(additionalConfig) > 0 {
196 vppConfig.additionalConfig = additionalConfig[0]
199 vpp := new(VppInstance)
201 vpp.config = vppConfig
208 func (c *Container) copy(sourceFileName string, targetFileName string) error {
209 cmd := exec.Command("docker", "cp", sourceFileName, c.name+":"+targetFileName)
213 func (c *Container) createFile(destFileName string, content string) error {
214 f, err := os.CreateTemp("/tmp", "hst-config")
218 defer os.Remove(f.Name())
220 if _, err := f.Write([]byte(content)); err != nil {
223 if err := f.Close(); err != nil {
226 c.copy(f.Name(), destFileName)
231 * Executes in detached mode so that the started application can continue to run
232 * without blocking execution of test
234 func (c *Container) execServer(command string, arguments ...any) {
235 serverCommand := fmt.Sprintf(command, arguments...)
236 containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() +
237 " " + c.name + " " + serverCommand
238 c.Suite().log(containerExecCommand)
239 c.Suite().assertNil(exechelper.Run(containerExecCommand))
242 func (c *Container) exec(command string, arguments ...any) string {
243 cliCommand := fmt.Sprintf(command, arguments...)
244 containerExecCommand := "docker exec" + c.getEnvVarsAsCliOption() +
245 " " + c.name + " " + cliCommand
246 c.Suite().log(containerExecCommand)
247 byteOutput, err := exechelper.CombinedOutput(containerExecCommand)
248 c.Suite().assertNil(err)
249 return string(byteOutput)
252 func (c *Container) execAction(args string) (string, error) {
253 syncFile := c.getSyncPath() + "/rc"
256 workDir := c.getWorkDirAsCliOption()
257 cmd := fmt.Sprintf("docker exec -d %s %s hs-test %s",
261 err := exechelper.Run(cmd)
265 res, err := waitForSyncFile(syncFile)
267 return "", fmt.Errorf("failed to read sync file while executing 'hs-test %s': %v", args, err)
269 o := res.StdOutput + res.ErrOutput
271 return o, fmt.Errorf("cmd resulted in non-zero value %d: %s", res.Code, res.Desc)
276 func (c *Container) stop() error {
277 if c.vppInstance != nil && c.vppInstance.apiChannel != nil {
278 c.vppInstance.disconnect()
281 return exechelper.Run("docker stop " + c.name + " -t 0")