aboutsummaryrefslogtreecommitdiffstats
path: root/extras/hs-test/container.go
diff options
context:
space:
mode:
Diffstat (limited to 'extras/hs-test/container.go')
-rw-r--r--extras/hs-test/container.go115
1 files changed, 97 insertions, 18 deletions
diff --git a/extras/hs-test/container.go b/extras/hs-test/container.go
index 971f3b60ef9..91ca2c2b0b1 100644
--- a/extras/hs-test/container.go
+++ b/extras/hs-test/container.go
@@ -3,24 +3,27 @@ package main
import (
"fmt"
"os"
+ "os/exec"
"strings"
"github.com/edwarnicke/exechelper"
)
type Volume struct {
- hostDir string
- containerDir string
+ hostDir string
+ containerDir string
+ isDefaultWorkDir bool
}
type Container struct {
+ suite *HstSuite
isOptional bool
name string
image string
- workDir string
extraRunningArgs string
volumes map[string]Volume
envVars map[string]string
+ vppInstance *VppInstance
}
func NewContainer(yamlInput ContainerConfig) (*Container, error) {
@@ -59,25 +62,53 @@ func NewContainer(yamlInput ContainerConfig) (*Container, error) {
volumeMap := volu.(ContainerConfig)
hostDir := r.Replace(volumeMap["host-dir"].(string))
containerDir := volumeMap["container-dir"].(string)
- container.addVolume(hostDir, containerDir)
+ isDefaultWorkDir := false
- if isDefaultWorkDir, ok := volumeMap["is-default-work-dir"]; ok &&
- isDefaultWorkDir.(bool) &&
- len(container.workDir) == 0 {
- container.workDir = containerDir
+ if isDefault, ok := volumeMap["is-default-work-dir"]; ok {
+ isDefaultWorkDir = isDefault.(bool)
}
+ container.addVolume(hostDir, containerDir, isDefaultWorkDir)
+
}
}
if _, ok := yamlInput["vars"]; ok {
for _, envVar := range yamlInput["vars"].([]interface{}) {
- container.addEnvVar(envVar)
+ envVarMap := envVar.(ContainerConfig)
+ name := envVarMap["name"].(string)
+ value := envVarMap["value"].(string)
+ container.addEnvVar(name, value)
}
}
return container, nil
}
+func (c *Container) getWorkDirVolume() (res Volume, exists bool) {
+ for _, v := range c.volumes {
+ if v.isDefaultWorkDir {
+ res = v
+ exists = true
+ return
+ }
+ }
+ return
+}
+
+func (c *Container) GetHostWorkDir() (res string) {
+ if v, ok := c.getWorkDirVolume(); ok {
+ res = v.hostDir
+ }
+ return
+}
+
+func (c *Container) GetContainerWorkDir() (res string) {
+ if v, ok := c.getWorkDirVolume(); ok {
+ res = v.containerDir
+ }
+ return
+}
+
func (c *Container) getRunCommand() string {
syncPath := fmt.Sprintf(" -v %s:/tmp/sync", c.getSyncPath())
cmd := "docker run --cap-add=all -d --privileged --network host --rm"
@@ -103,10 +134,11 @@ func (c *Container) run() error {
return nil
}
-func (c *Container) addVolume(hostDir string, containerDir string) {
+func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) {
var volume Volume
volume.hostDir = hostDir
volume.containerDir = containerDir
+ volume.isDefaultWorkDir = isDefaultWorkDir
c.volumes[hostDir] = volume
}
@@ -127,16 +159,13 @@ func (c *Container) getVolumesAsCliOption() string {
}
func (c *Container) getWorkDirAsCliOption() string {
- if len(c.workDir) == 0 {
- return ""
+ if _, ok := c.getWorkDirVolume(); ok {
+ return fmt.Sprintf(" --workdir=\"%s\"", c.GetContainerWorkDir())
}
- return fmt.Sprintf(" --workdir=\"%s\"", c.workDir)
+ return ""
}
-func (c *Container) addEnvVar(envVar interface{}) {
- envVarMap := envVar.(ContainerConfig)
- name := envVarMap["name"].(string)
- value := envVarMap["value"].(string)
+func (c *Container) addEnvVar(name string, value string) {
c.envVars[name] = value
}
@@ -156,8 +185,54 @@ func (c *Container) getSyncPath() string {
return fmt.Sprintf("/tmp/%s/sync", c.name)
}
+func (c *Container) newVppInstance(additionalConfig ...Stanza) (*VppInstance, error) {
+ vppConfig := new(VppConfig)
+ vppConfig.CliSocketFilePath = defaultCliSocketFilePath
+ if len(additionalConfig) > 0 {
+ vppConfig.additionalConfig = additionalConfig[0]
+ }
+
+ vpp := new(VppInstance)
+ vpp.container = c
+ vpp.config = vppConfig
+
+ c.vppInstance = vpp
+
+ return vpp, nil
+}
+
+func (c *Container) copy(sourceFileName string, targetFileName string) error {
+ cmd := exec.Command("docker", "cp", sourceFileName, c.name+":"+targetFileName)
+ return cmd.Run()
+}
+
+func (c *Container) createFile(destFileName string, content string) error {
+ f, err := os.CreateTemp("/tmp", "hst-config")
+ if err != nil {
+ return err
+ }
+ defer os.Remove(f.Name())
+
+ if _, err := f.Write([]byte(content)); err != nil {
+ return err
+ }
+ if err := f.Close(); err != nil {
+ return err
+ }
+ c.copy(f.Name(), destFileName)
+ return nil
+}
+
+/*
+ * Executes in detached mode so that the started application can continue to run
+ * without blocking execution of test
+ */
+func (c *Container) execServer(command string) error {
+ return exechelper.Run("docker exec -d" + c.getEnvVarsAsCliOption() + " " + c.name + " " + command)
+}
+
func (c *Container) exec(command string) (string, error) {
- cliCommand := "docker exec -d " + c.name + " " + command
+ cliCommand := "docker exec" + c.getEnvVarsAsCliOption() + " " + c.name + " " + command
byteOutput, err := exechelper.CombinedOutput(cliCommand)
return string(byteOutput), err
}
@@ -187,5 +262,9 @@ func (c *Container) execAction(args string) (string, error) {
}
func (c *Container) stop() error {
+ if c.vppInstance != nil && c.vppInstance.apiChannel != nil {
+ c.vppInstance.disconnect()
+ }
+ c.vppInstance = nil
return exechelper.Run("docker stop " + c.name + " -t 0")
}