Code Review
/
vpp.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
review
|
tree
raw
|
inline
| side by side
hs-test: support for multiple workers
[vpp.git]
/
extras
/
hs-test
/
container.go
diff --git
a/extras/hs-test/container.go
b/extras/hs-test/container.go
index
40dc082
..
fd3aa47
100644
(file)
--- a/
extras/hs-test/container.go
+++ b/
extras/hs-test/container.go
@@
-27,6
+27,7
@@
type Volume struct {
type Container struct {
suite *HstSuite
isOptional bool
type Container struct {
suite *HstSuite
isOptional bool
+ runDetached bool
name string
image string
extraRunningArgs string
name string
image string
extraRunningArgs string
@@
-35,7
+36,7
@@
type Container struct {
vppInstance *VppInstance
}
vppInstance *VppInstance
}
-func
N
ewContainer(yamlInput ContainerConfig) (*Container, error) {
+func
n
ewContainer(yamlInput ContainerConfig) (*Container, error) {
containerName := yamlInput["name"].(string)
if len(containerName) == 0 {
err := fmt.Errorf("container name must not be blank")
containerName := yamlInput["name"].(string)
if len(containerName) == 0 {
err := fmt.Errorf("container name must not be blank")
@@
-65,6
+66,12
@@
func NewContainer(yamlInput ContainerConfig) (*Container, error) {
container.isOptional = false
}
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)
for _, volu := range yamlInput["volumes"].([]interface{}) {
if _, ok := yamlInput["volumes"]; ok {
r := strings.NewReplacer("$HST_DIR", workDir)
for _, volu := range yamlInput["volumes"].([]interface{}) {
@@
-93,10
+100,6
@@
func NewContainer(yamlInput ContainerConfig) (*Container, error) {
return container, nil
}
return container, nil
}
-func (c *Container) Suite() *HstSuite {
- return c.suite
-}
-
func (c *Container) getWorkDirVolume() (res Volume, exists bool) {
for _, v := range c.volumes {
if v.isDefaultWorkDir {
func (c *Container) getWorkDirVolume() (res Volume, exists bool) {
for _, v := range c.volumes {
if v.isDefaultWorkDir {
@@
-108,14
+111,14
@@
func (c *Container) getWorkDirVolume() (res Volume, exists bool) {
return
}
return
}
-func (c *Container)
G
etHostWorkDir() (res string) {
+func (c *Container)
g
etHostWorkDir() (res string) {
if v, ok := c.getWorkDirVolume(); ok {
res = v.hostDir
}
return
}
if v, ok := c.getWorkDirVolume(); ok {
res = v.hostDir
}
return
}
-func (c *Container)
G
etContainerWorkDir() (res string) {
+func (c *Container)
g
etContainerWorkDir() (res string) {
if v, ok := c.getWorkDirVolume(); ok {
res = v.containerDir
}
if v, ok := c.getWorkDirVolume(); ok {
res = v.containerDir
}
@@
-123,36
+126,58
@@
func (c *Container) GetContainerWorkDir() (res string) {
}
func (c *Container) getContainerArguments() string {
}
func (c *Container) getContainerArguments() string {
- args := "--cap-add=all --privileged --network host --rm"
+ args := "--
ulimit nofile=90000:90000 --
cap-add=all --privileged --network host --rm"
args += c.getVolumesAsCliOption()
args += c.getEnvVarsAsCliOption()
args += " --name " + c.name + " " + c.image
args += c.getVolumesAsCliOption()
args += c.getEnvVarsAsCliOption()
args += " --name " + c.name + " " + c.image
+ args += " " + c.extraRunningArgs
return args
}
return args
}
-func (c *Container) create() {
+func (c *Container) create()
error
{
cmd := "docker create " + c.getContainerArguments()
cmd := "docker create " + c.getContainerArguments()
- exechelper.Run(cmd)
+ c.suite.log(cmd)
+ return exechelper.Run(cmd)
}
}
-func (c *Container) start() {
+func (c *Container) start()
error
{
cmd := "docker start " + c.name
cmd := "docker start " + c.name
- exechelper.Run(cmd)
+ c.suite.log(cmd)
+ return exechelper.Run(cmd)
}
}
-func (c *Container)
run() error
{
+func (c *Container)
prepareCommand() (string, error)
{
if c.name == "" {
if c.name == "" {
- return fmt.Errorf("run container failed: name is blank")
+ return
"",
fmt.Errorf("run container failed: name is blank")
}
}
- cmd := "docker run -d " + c.getContainerArguments() + " " + c.extraRunningArgs
- c.Suite().log(cmd)
- err := exechelper.Run(cmd)
+ 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 {
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, isDefaultWorkDir bool) {
}
func (c *Container) addVolume(hostDir string, containerDir string, isDefaultWorkDir bool) {
@@
-191,16
+216,12
@@
func (c *Container) getEnvVarsAsCliOption() string {
return cliOption
}
return cliOption
}
-func (c *Container) newVppInstance(
additionalConfig
...Stanza) (*VppInstance, error) {
+func (c *Container) newVppInstance(
cpus []int, additionalConfigs
...Stanza) (*VppInstance, error) {
vpp := new(VppInstance)
vpp.container = c
vpp := new(VppInstance)
vpp.container = c
-
- if len(additionalConfig) > 0 {
- vpp.additionalConfig = additionalConfig[0]
- }
-
+ vpp.cpus = cpus
+ vpp.additionalConfig = append(vpp.additionalConfig, additionalConfigs...)
c.vppInstance = vpp
c.vppInstance = vpp
-
return vpp, nil
}
return vpp, nil
}
@@
-234,30
+255,30
@@
func (c *Container) execServer(command string, arguments ...any) {
serverCommand := fmt.Sprintf(command, arguments...)
containerExecCommand := "docker exec -d" + c.getEnvVarsAsCliOption() +
" " + c.name + " " + serverCommand
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))
+ 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
}
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)
+ c.
suite
.T().Helper()
+ c.
suite
.log(containerExecCommand)
byteOutput, err := exechelper.CombinedOutput(containerExecCommand)
byteOutput, err := exechelper.CombinedOutput(containerExecCommand)
- c.
Suite()
.assertNil(err)
+ c.
suite
.assertNil(err)
return string(byteOutput)
}
func (c *Container) getLogDirPath() string {
return string(byteOutput)
}
func (c *Container) getLogDirPath() string {
- testId := c.
Suite()
.getTestId()
- testName := c.
Suite()
.T().Name()
+ 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 {
logDirPath := logDir + testName + "/" + testId + "/"
cmd := exec.Command("mkdir", "-p", logDirPath)
if err := cmd.Run(); err != nil {
- c.
Suite()
.T().Fatalf("mkdir error: %v", err)
+ c.
suite
.T().Fatalf("mkdir error: %v", err)
}
return logDirPath
}
return logDirPath
@@
-274,22
+295,22
@@
func (c *Container) saveLogs() {
cmd = exec.Command("docker", "logs", "--details", "-t", c.name)
output, err := cmd.CombinedOutput()
if err != nil {
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)
+ c.
suite
.T().Fatalf("fetching logs error: %v", err)
}
f, err := os.Create(testLogFilePath)
if err != nil {
}
f, err := os.Create(testLogFilePath)
if err != nil {
- c.
Suite()
.T().Fatalf("file create error: %v", err)
+ c.
suite
.T().Fatalf("file create error: %v", err)
}
}
- fmt.Fprint
f
(f, string(output))
+ fmt.Fprint(f, string(output))
f.Close()
}
func (c *Container) log() string {
cmd := "docker logs " + c.name
f.Close()
}
func (c *Container) log() string {
cmd := "docker logs " + c.name
- c.
Suite()
.log(cmd)
+ c.
suite
.log(cmd)
o, err := exechelper.CombinedOutput(cmd)
o, err := exechelper.CombinedOutput(cmd)
- c.
Suite()
.assertNil(err)
+ c.
suite
.assertNil(err)
return string(o)
}
return string(o)
}
@@
-307,14
+328,21
@@
func (c *Container) createConfig(targetConfigName string, templateName string, v
template := template.Must(template.ParseFiles(templateName))
f, err := os.CreateTemp("/tmp/hs-test/", "hst-config")
template := template.Must(template.ParseFiles(templateName))
f, err := os.CreateTemp("/tmp/hs-test/", "hst-config")
- c.
Suite()
.assertNil(err)
+ c.
suite
.assertNil(err)
defer os.Remove(f.Name())
err = template.Execute(f, values)
defer os.Remove(f.Name())
err = template.Execute(f, values)
- c.
Suite()
.assertNil(err)
+ c.
suite
.assertNil(err)
err = f.Close()
err = f.Close()
- c.
Suite()
.assertNil(err)
+ c.
suite
.assertNil(err)
c.copy(f.Name(), targetConfigName)
}
c.copy(f.Name(), targetConfigName)
}
+
+func init() {
+ cmd := exec.Command("mkdir", "-p", logDir)
+ if err := cmd.Run(); err != nil {
+ panic(err)
+ }
+}