hs-test: store logs

Type: test
Signed-off-by: Maros Ondrejicka <mondreji@cisco.com>
Change-Id: I50ad5d8c2e5066d8d24f7959aeb534a2f0a6fae0
This commit is contained in:
Maros Ondrejicka
2023-02-24 11:26:39 +01:00
parent ad406077af
commit a2d5262afb
3 changed files with 79 additions and 2 deletions

View File

@ -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")
}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"time"
"github.com/edwarnicke/exechelper"
"github.com/stretchr/testify/assert"
@ -26,6 +27,7 @@ type HstSuite struct {
netConfigs []NetConfig
netInterfaces map[string]NetInterface
addresser *Addresser
testIds map[string]string
}
func (s *HstSuite) TearDownSuite() {
@ -109,6 +111,7 @@ func (s *HstSuite) assertNotEmpty(object interface{}, msgAndArgs ...interface{})
func (s *HstSuite) log(args ...any) {
if *IsVerbose {
s.T().Helper()
s.T().Log(args...)
}
}
@ -245,6 +248,20 @@ func (s *HstSuite) unconfigureNetworkTopology() {
}
}
func (s *HstSuite) getTestId() string {
testName := s.T().Name()
if s.testIds == nil {
s.testIds = map[string]string{}
}
if _, ok := s.testIds[testName]; !ok {
s.testIds[testName] = time.Now().Format(time.RFC3339)
}
return s.testIds[testName]
}
type NetworkAddresses struct {
network int
numberOfAddresses int

View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"github.com/edwarnicke/exechelper"
"os/exec"
"strings"
"time"
@ -19,7 +20,7 @@ import (
const vppConfigTemplate = `unix {
nodaemon
log %[1]s/var/log/vpp/vpp.log
log %[1]s%[4]s
full-coredump
cli-listen %[1]s%[2]s
runtime-dir %[1]s/var/run
@ -52,11 +53,17 @@ plugins {
plugin http_plugin.so { enable }
}
logging {
default-log-level debug
default-syslog-log-level debug
}
`
const (
defaultCliSocketFilePath = "/var/run/vpp/cli.sock"
defaultApiSocketFilePath = "/var/run/vpp/api.sock"
defaultLogFilePath = "/var/log/vpp/vpp.log"
)
type VppInstance struct {
@ -100,13 +107,14 @@ func (vpp *VppInstance) start() error {
containerWorkDir,
defaultCliSocketFilePath,
defaultApiSocketFilePath,
defaultLogFilePath,
)
configContent += vpp.additionalConfig.ToString()
startupFileName := vpp.getEtcDir() + "/startup.conf"
vpp.container.createFile(startupFileName, configContent)
// Start VPP
vpp.container.execServer("vpp -c " + startupFileName)
vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
// Connect to VPP and store the connection
sockAddress := vpp.container.GetHostWorkDir() + defaultApiSocketFilePath
@ -290,6 +298,15 @@ func (vpp *VppInstance) createTap(
return nil
}
func (vpp *VppInstance) saveLogs() {
logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
logSource := vpp.container.GetHostWorkDir() + defaultLogFilePath
cmd := exec.Command("cp", logSource, logTarget)
vpp.Suite().T().Helper()
vpp.Suite().log(cmd.String())
cmd.Run()
}
func (vpp *VppInstance) disconnect() {
vpp.connection.Disconnect()
vpp.apiChannel.Close()