diff options
author | Maros Ondrejicka <mondreji@cisco.com> | 2023-02-24 11:26:39 +0100 |
---|---|---|
committer | Maros Ondrejicka <mondreji@cisco.com> | 2023-02-24 18:56:56 +0100 |
commit | a2d5262afb0a6a7a0d0d4ce3a78fee94ce05be0c (patch) | |
tree | 8197f9e523c89a270ec01683abd53c29006ee94d /extras/hs-test/container.go | |
parent | ad406077afa1138e1a666a120782ebf66b340b30 (diff) |
hs-test: store logs
Type: test
Signed-off-by: Maros Ondrejicka <mondreji@cisco.com>
Change-Id: I50ad5d8c2e5066d8d24f7959aeb534a2f0a6fae0
Diffstat (limited to 'extras/hs-test/container.go')
-rw-r--r-- | extras/hs-test/container.go | 43 |
1 files changed, 43 insertions, 0 deletions
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") } |