X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=extras%2Fhs-test%2Fcontainer.go;h=76d08c7d2d6ab7c1354ae1923c178ea767e731c8;hb=109f3ce4eba0a6ba869752f24cfaae73ab70bb2d;hp=1dc65fff308f8d82e3bdba1fc018b54dc26b4069;hpb=db823ed6e9543741f6969ff160314093002e037e;p=vpp.git diff --git a/extras/hs-test/container.go b/extras/hs-test/container.go index 1dc65fff308..76d08c7d2d6 100644 --- a/extras/hs-test/container.go +++ b/extras/hs-test/container.go @@ -3,26 +3,40 @@ package main import ( "fmt" "os" + "os/exec" "strings" + "text/template" "github.com/edwarnicke/exechelper" ) +const ( + logDir string = "/tmp/hs-test/" +) + +var ( + workDir, _ = os.Getwd() +) + type Volume struct { - hostDir string - containerDir string + hostDir string + containerDir string + isDefaultWorkDir bool } type Container struct { - isOptional bool - name string - image string - workDir string - volumes map[string]Volume - envVars map[string]string + suite *HstSuite + isOptional bool + runDetached bool + name string + image string + extraRunningArgs string + volumes map[string]Volume + envVars map[string]string + vppInstance *VppInstance } -func NewContainer(yamlInput ContainerConfig) (*Container, error) { +func newContainer(yamlInput ContainerConfig) (*Container, error) { containerName := yamlInput["name"].(string) if len(containerName) == 0 { err := fmt.Errorf("container name must not be blank") @@ -40,69 +54,143 @@ func NewContainer(yamlInput ContainerConfig) (*Container, error) { container.image = "hs-test/vpp" } + if args, ok := yamlInput["extra-args"]; ok { + container.extraRunningArgs = args.(string) + } else { + container.extraRunningArgs = "" + } + if isOptional, ok := yamlInput["is-optional"]; ok { container.isOptional = isOptional.(bool) } else { 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) + r := strings.NewReplacer("$HST_DIR", workDir) for _, volu := range yamlInput["volumes"].([]interface{}) { 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) run() error { +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) 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) 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) prepareCommand() (string, error) { if c.name == "" { - return fmt.Errorf("create volume failed: container name is blank") - } - - exechelper.Run(fmt.Sprintf("mkdir -p /tmp/%s/sync", c.name)) - syncPath := fmt.Sprintf(" -v %s:/tmp/sync", c.getSyncPath()) - cmd := "docker run --cap-add=all -d --privileged --network host --rm" - cmd += syncPath - cmd += c.getVolumesAsCliOption() - cmd += c.getEnvVarsAsCliOption() - cmd += " --name " + c.name + " " + c.image - fmt.Println(cmd) - err := exechelper.Run(cmd) + return "", fmt.Errorf("run container failed: name is blank") + } + + cmd := "docker run " + if c.runDetached { + cmd += " -d" + } + cmd += " " + c.getContainerArguments() + + c.suite.log(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 exechelper.Run(cmd) } -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 } -func (c *Container) getVolumeByHostDir(hostDir string) Volume { - return c.volumes[hostDir] -} - func (c *Container) getVolumesAsCliOption() string { cliOption := "" @@ -115,17 +203,7 @@ func (c *Container) getVolumesAsCliOption() string { return cliOption } -func (c *Container) getWorkDirAsCliOption() string { - if len(c.workDir) == 0 { - return "" - } - return fmt.Sprintf(" --workdir=\"%s\"", c.workDir) -} - -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 } @@ -141,40 +219,133 @@ func (c *Container) getEnvVarsAsCliOption() string { return cliOption } -func (c *Container) getSyncPath() string { - return fmt.Sprintf("/tmp/%s/sync", c.name) +func (c *Container) newVppInstance(cpus []int, additionalConfigs ...Stanza) (*VppInstance, error) { + vpp := new(VppInstance) + vpp.container = c + vpp.cpus = cpus + vpp.additionalConfig = append(vpp.additionalConfig, additionalConfigs...) + c.vppInstance = vpp + return vpp, nil } -func (c *Container) exec(command string) (string, error) { - cliCommand := "docker exec -d " + c.name + " " + command - byteOutput, err := exechelper.CombinedOutput(cliCommand) - return string(byteOutput), err +func (c *Container) copy(sourceFileName string, targetFileName string) error { + cmd := exec.Command("docker", "cp", sourceFileName, c.name+":"+targetFileName) + return cmd.Run() } -func (c *Container) execAction(args string) (string, error) { - syncFile := c.getSyncPath() + "/rc" - os.Remove(syncFile) - - workDir := c.getWorkDirAsCliOption() - cmd := fmt.Sprintf("docker exec -d %s %s hs-test %s", - workDir, - c.name, - args) - err := exechelper.Run(cmd) +func (c *Container) createFile(destFileName string, content string) error { + f, err := os.CreateTemp("/tmp", "hst-config") if err != nil { - return "", err + 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, arguments ...any) { + serverCommand := fmt.Sprintf(command, arguments...) + containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() + + " " + c.name + " " + serverCommand + c.suite.T().Helper() + c.suite.log(containerExecCommand) + c.suite.assertNil(exechelper.Run(containerExecCommand)) +} + +func (c *Container) exec(command string, arguments ...any) string { + cliCommand := fmt.Sprintf(command, arguments...) + containerExecCommand := "docker exec" + c.getEnvVarsAsCliOption() + + " " + c.name + " " + cliCommand + c.suite.T().Helper() + c.suite.log(containerExecCommand) + byteOutput, err := exechelper.CombinedOutput(containerExecCommand) + c.suite.assertNil(err) + return string(byteOutput) +} + +func (c *Container) getLogDirPath() string { + testId := c.suite.getTestId() + testName := c.suite.T().Name() + logDirPath := logDir + testName + "/" + testId + "/" + + cmd := exec.Command("mkdir", "-p", logDirPath) + if err := cmd.Run(); err != nil { + c.suite.T().Fatalf("mkdir error: %v", err) + } + + return logDirPath +} + +func (c *Container) saveLogs() { + cmd := exec.Command("docker", "inspect", "--format='{{.State.Status}}'", c.name) + if output, _ := cmd.CombinedOutput(); !strings.Contains(string(output), "running") { + return } - res, err := waitForSyncFile(syncFile) + + testLogFilePath := c.getLogDirPath() + "container-" + c.name + ".log" + + cmd = exec.Command("docker", "logs", "--details", "-t", c.name) + output, err := cmd.CombinedOutput() if err != nil { - return "", fmt.Errorf("failed to read sync file while executing 'hs-test %s': %v", args, err) + c.suite.T().Fatalf("fetching logs error: %v", err) } - o := res.StdOutput + res.ErrOutput - if res.Code != 0 { - return o, fmt.Errorf("cmd resulted in non-zero value %d: %s", res.Code, res.Desc) + + f, err := os.Create(testLogFilePath) + if err != nil { + c.suite.T().Fatalf("file create error: %v", err) } - return o, err + fmt.Fprint(f, string(output)) + 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 { - return exechelper.Run("docker stop " + c.name) + if c.vppInstance != nil && c.vppInstance.apiChannel != nil { + c.vppInstance.saveLogs() + c.vppInstance.disconnect() + } + c.vppInstance = nil + 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) +} + +func init() { + cmd := exec.Command("mkdir", "-p", logDir) + if err := cmd.Run(); err != nil { + panic(err) + } }