hs-test: fix envoy test
[vpp.git] / extras / hs-test / container.go
index c55fdde..4b3002f 100644 (file)
@@ -5,6 +5,7 @@ import (
        "os"
        "os/exec"
        "strings"
+       "text/template"
 
        "github.com/edwarnicke/exechelper"
 )
@@ -121,12 +122,25 @@ func (c *Container) GetContainerWorkDir() (res string) {
        return
 }
 
-func (c *Container) getRunCommand() string {
-       cmd := "docker run --cap-add=all -d --privileged --network host --rm"
-       cmd += c.getVolumesAsCliOption()
-       cmd += c.getEnvVarsAsCliOption()
-       cmd += " --name " + c.name + " " + c.image + " " + c.extraRunningArgs
-       return cmd
+func (c *Container) getContainerArguments() string {
+       args := "--cap-add=all --privileged --network host --rm"
+       args += c.getVolumesAsCliOption()
+       args += c.getEnvVarsAsCliOption()
+       args += " --name " + c.name + " " + c.image
+       args += " " + c.extraRunningArgs
+       return args
+}
+
+func (c *Container) create() error {
+       cmd := "docker create " + c.getContainerArguments()
+       c.Suite().log(cmd)
+       return exechelper.Run(cmd)
+}
+
+func (c *Container) start() error {
+       cmd := "docker start " + c.name
+       c.Suite().log(cmd)
+       return exechelper.Run(cmd)
 }
 
 func (c *Container) run() error {
@@ -134,7 +148,8 @@ func (c *Container) run() error {
                return fmt.Errorf("run container failed: name is blank")
        }
 
-       cmd := c.getRunCommand()
+       cmd := "docker run -d " + c.getContainerArguments()
+       c.Suite().log(cmd)
        err := exechelper.Run(cmd)
        if err != nil {
                return fmt.Errorf("container run failed: %s", err)
@@ -273,6 +288,14 @@ func (c *Container) saveLogs() {
        f.Close()
 }
 
+func (c *Container) log() string {
+       cmd := "docker logs " + c.name
+       c.Suite().log(cmd)
+       o, err := exechelper.CombinedOutput(cmd)
+       c.Suite().assertNil(err)
+       return string(o)
+}
+
 func (c *Container) stop() error {
        if c.vppInstance != nil && c.vppInstance.apiChannel != nil {
                c.vppInstance.saveLogs()
@@ -282,3 +305,19 @@ func (c *Container) stop() error {
        c.saveLogs()
        return exechelper.Run("docker stop " + c.name + " -t 0")
 }
+
+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")
+       c.Suite().assertNil(err)
+       defer os.Remove(f.Name())
+
+       err = template.Execute(f, values)
+       c.Suite().assertNil(err)
+
+       err = f.Close()
+       c.Suite().assertNil(err)
+
+       c.copy(f.Name(), targetConfigName)
+}