X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=extras%2Fhs-test%2Fcontainer.go;h=4b3002f15bee608324ae3e748000d947a215dc21;hb=9abba11a9434e152f35a863de3e7631fe3a00070;hp=8ece8a8952b5331b5a5bffaa1af358a9437a3c0d;hpb=7550dd268f80334cbb9127feefe35319b9c7e572;p=vpp.git diff --git a/extras/hs-test/container.go b/extras/hs-test/container.go index 8ece8a8952b..4b3002f15be 100644 --- a/extras/hs-test/container.go +++ b/extras/hs-test/container.go @@ -5,10 +5,15 @@ import ( "os" "os/exec" "strings" + "text/template" "github.com/edwarnicke/exechelper" ) +const ( + logDir string = "/tmp/hs-test/" +) + var ( workDir, _ = os.Getwd() ) @@ -117,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 { @@ -130,8 +148,8 @@ func (c *Container) run() error { return fmt.Errorf("run container failed: name is blank") } - exechelper.Run(fmt.Sprintf("mkdir -p /tmp/%s/sync", c.name)) - 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) @@ -148,10 +166,6 @@ func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWork c.volumes[hostDir] = volume } -func (c *Container) getVolumeByHostDir(hostDir string) Volume { - return c.volumes[hostDir] -} - func (c *Container) getVolumesAsCliOption() string { cliOption := "" @@ -164,13 +178,6 @@ func (c *Container) getVolumesAsCliOption() string { return cliOption } -func (c *Container) getWorkDirAsCliOption() string { - if _, ok := c.getWorkDirVolume(); ok { - return fmt.Sprintf(" --workdir=\"%s\"", c.GetContainerWorkDir()) - } - return "" -} - func (c *Container) addEnvVar(name string, value string) { c.envVars[name] = value } @@ -188,15 +195,12 @@ func (c *Container) getEnvVarsAsCliOption() string { } 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 + + if len(additionalConfig) > 0 { + vpp.additionalConfig = additionalConfig[0] + } c.vppInstance = vpp @@ -233,6 +237,7 @@ 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)) } @@ -241,16 +246,78 @@ 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 + } + + testLogFilePath := c.getLogDirPath() + "container-" + c.name + ".log" + + cmd = exec.Command("docker", "logs", "--details", "-t", c.name) + output, err := cmd.CombinedOutput() + if err != nil { + c.Suite().T().Fatalf("fetching logs error: %v", err) + } + + f, err := os.Create(testLogFilePath) + if err != nil { + c.Suite().T().Fatalf("file create error: %v", err) + } + fmt.Fprintf(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 { 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) +}