From a2d5262afb0a6a7a0d0d4ce3a78fee94ce05be0c Mon Sep 17 00:00:00 2001 From: Maros Ondrejicka Date: Fri, 24 Feb 2023 11:26:39 +0100 Subject: hs-test: store logs Type: test Signed-off-by: Maros Ondrejicka Change-Id: I50ad5d8c2e5066d8d24f7959aeb534a2f0a6fae0 --- extras/hs-test/container.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'extras/hs-test/container.go') diff --git a/extras/hs-test/container.go b/extras/hs-test/container.go index 4087d851945..c55fddead54 100644 --- a/extras/hs-test/container.go +++ b/extras/hs-test/container.go @@ -9,6 +9,10 @@ import ( "github.com/edwarnicke/exechelper" ) +const ( + logDir string = "/tmp/hs-test/" +) + var ( workDir, _ = os.Getwd() ) @@ -218,6 +222,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)) } @@ -226,16 +231,54 @@ 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) 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") } -- cgit 1.2.3-korg