hs-test: retry command on test setup failure
[vpp.git] / extras / hs-test / container.go
index 1f600f9..812198d 100644 (file)
@@ -6,6 +6,7 @@ import (
        "os/exec"
        "strings"
        "text/template"
+       "time"
 
        "github.com/edwarnicke/exechelper"
 )
@@ -27,6 +28,7 @@ type Volume struct {
 type Container struct {
        suite            *HstSuite
        isOptional       bool
+       runDetached      bool
        name             string
        image            string
        extraRunningArgs string
@@ -65,6 +67,12 @@ func newContainer(yamlInput ContainerConfig) (*Container, error) {
                container.isOptional = false
        }
 
+       if runDetached, ok := yamlInput["run-detached"]; ok {
+               container.runDetached = runDetached.(bool)
+       } else {
+               container.runDetached = true
+       }
+
        if _, ok := yamlInput["volumes"]; ok {
                r := strings.NewReplacer("$HST_DIR", workDir)
                for _, volu := range yamlInput["volumes"].([]interface{}) {
@@ -119,14 +127,29 @@ func (c *Container) getContainerWorkDir() (res string) {
 }
 
 func (c *Container) getContainerArguments() string {
-       args := "--cap-add=all --privileged --network host --rm"
+       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)
@@ -136,22 +159,40 @@ 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) run() error {
+func (c *Container) prepareCommand() (string, error) {
        if c.name == "" {
-               return fmt.Errorf("run container failed: name is blank")
+               return "", fmt.Errorf("run container failed: name is blank")
        }
 
-       cmd := "docker run -d " + c.getContainerArguments()
+       cmd := "docker run "
+       if c.runDetached {
+               cmd += " -d"
+       }
+       cmd += " " + c.getContainerArguments()
+
        c.suite.log(cmd)
-       err := exechelper.Run(cmd)
+       return cmd, nil
+}
+
+func (c *Container) combinedOutput() (string, error) {
+       cmd, err := c.prepareCommand()
        if err != nil {
-               return fmt.Errorf("container run failed: %s", err)
+               return "", err
        }
 
-       return nil
+       byteOutput, err := exechelper.CombinedOutput(cmd)
+       return string(byteOutput), err
+}
+
+func (c *Container) run() error {
+       cmd, err := c.prepareCommand()
+       if err != nil {
+               return err
+       }
+       return c.runWithRetry(cmd)
 }
 
 func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) {
@@ -190,16 +231,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
 }