hs-test: use relative paths for docker volumes
[vpp.git] / extras / hs-test / container.go
index 1dc49b7..86f511c 100644 (file)
@@ -6,12 +6,14 @@ import (
        "os/exec"
        "strings"
        "text/template"
+       "time"
 
        "github.com/edwarnicke/exechelper"
 )
 
 const (
-       logDir string = "/tmp/hs-test/"
+       logDir    string = "/tmp/hs-test/"
+       volumeDir string = "/volumes"
 )
 
 var (
@@ -36,7 +38,7 @@ type Container struct {
        vppInstance      *VppInstance
 }
 
-func newContainer(yamlInput ContainerConfig) (*Container, error) {
+func newContainer(suite *HstSuite, yamlInput ContainerConfig) (*Container, error) {
        containerName := yamlInput["name"].(string)
        if len(containerName) == 0 {
                err := fmt.Errorf("container name must not be blank")
@@ -47,6 +49,7 @@ func newContainer(yamlInput ContainerConfig) (*Container, error) {
        container.volumes = make(map[string]Volume)
        container.envVars = make(map[string]string)
        container.name = containerName
+       container.suite = suite
 
        if image, ok := yamlInput["image"]; ok {
                container.image = image.(string)
@@ -73,19 +76,20 @@ func newContainer(yamlInput ContainerConfig) (*Container, error) {
        }
 
        if _, ok := yamlInput["volumes"]; ok {
-               r := strings.NewReplacer("$HST_DIR", workDir)
+               workingVolumeDir := logDir + container.suite.T().Name() + volumeDir
+               workDirReplacer := strings.NewReplacer("$HST_DIR", workDir)
+               volDirReplacer := strings.NewReplacer("$HST_VOLUME_DIR", workingVolumeDir)
                for _, volu := range yamlInput["volumes"].([]interface{}) {
                        volumeMap := volu.(ContainerConfig)
-                       hostDir := r.Replace(volumeMap["host-dir"].(string))
+                       hostDir := workDirReplacer.Replace(volumeMap["host-dir"].(string))
+                       hostDir = volDirReplacer.Replace(hostDir)
                        containerDir := volumeMap["container-dir"].(string)
                        isDefaultWorkDir := false
 
                        if isDefault, ok := volumeMap["is-default-work-dir"]; ok {
                                isDefaultWorkDir = isDefault.(bool)
                        }
-
                        container.addVolume(hostDir, containerDir, isDefaultWorkDir)
-
                }
        }
 
@@ -129,11 +133,26 @@ func (c *Container) getContainerArguments() string {
        args := "--ulimit nofile=90000:90000 --cap-add=all --privileged --network host --rm"
        args += c.getVolumesAsCliOption()
        args += c.getEnvVarsAsCliOption()
+       if *vppSourceFileDir != "" {
+               args += fmt.Sprintf(" -v %s:%s", *vppSourceFileDir, *vppSourceFileDir)
+       }
        args += " --name " + c.name + " " + c.image
        args += " " + c.extraRunningArgs
        return args
 }
 
+func (c *Container) runWithRetry(cmd string) error {
+       nTries := 5
+       for i := 0; i < nTries; i++ {
+               err := exechelper.Run(cmd)
+               if err == nil {
+                       return nil
+               }
+               time.Sleep(1 * time.Second)
+       }
+       return fmt.Errorf("failed to run container command")
+}
+
 func (c *Container) create() error {
        cmd := "docker create " + c.getContainerArguments()
        c.suite.log(cmd)
@@ -143,7 +162,7 @@ func (c *Container) create() error {
 func (c *Container) start() error {
        cmd := "docker start " + c.name
        c.suite.log(cmd)
-       return exechelper.Run(cmd)
+       return c.runWithRetry(cmd)
 }
 
 func (c *Container) prepareCommand() (string, error) {
@@ -176,8 +195,7 @@ func (c *Container) run() error {
        if err != nil {
                return err
        }
-
-       return exechelper.Run(cmd)
+       return c.runWithRetry(cmd)
 }
 
 func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) {
@@ -216,16 +234,12 @@ func (c *Container) getEnvVarsAsCliOption() string {
        return cliOption
 }
 
-func (c *Container) newVppInstance(additionalConfig ...Stanza) (*VppInstance, error) {
+func (c *Container) newVppInstance(cpus []int, additionalConfigs ...Stanza) (*VppInstance, error) {
        vpp := new(VppInstance)
        vpp.container = c
-
-       if len(additionalConfig) > 0 {
-               vpp.additionalConfig = additionalConfig[0]
-       }
-
+       vpp.cpus = cpus
+       vpp.additionalConfig = append(vpp.additionalConfig, additionalConfigs...)
        c.vppInstance = vpp
-
        return vpp, nil
 }
 
@@ -331,7 +345,7 @@ func (c *Container) stop() error {
 func (c *Container) createConfig(targetConfigName string, templateName string, values any) {
        template := template.Must(template.ParseFiles(templateName))
 
-       f, err := os.CreateTemp("/tmp/hs-test/", "hst-config")
+       f, err := os.CreateTemp(logDir, "hst-config")
        c.suite.assertNil(err)
        defer os.Remove(f.Name())