diff options
Diffstat (limited to 'extras/hs-test/container.go')
-rw-r--r-- | extras/hs-test/container.go | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/extras/hs-test/container.go b/extras/hs-test/container.go index 87e8aa38e98..080eb736ad4 100644 --- a/extras/hs-test/container.go +++ b/extras/hs-test/container.go @@ -9,6 +9,7 @@ import ( "time" "github.com/edwarnicke/exechelper" + . "github.com/onsi/ginkgo/v2" ) const ( @@ -36,9 +37,10 @@ type Container struct { volumes map[string]Volume envVars map[string]string vppInstance *VppInstance + allocatedCpus []int } -func newContainer(suite *HstSuite, yamlInput ContainerConfig, pid string) (*Container, error) { +func newContainer(suite *HstSuite, yamlInput ContainerConfig) (*Container, error) { containerName := yamlInput["name"].(string) if len(containerName) == 0 { err := fmt.Errorf("container name must not be blank") @@ -48,7 +50,7 @@ func newContainer(suite *HstSuite, yamlInput ContainerConfig, pid string) (*Cont var container = new(Container) container.volumes = make(map[string]Volume) container.envVars = make(map[string]string) - container.name = containerName + pid + container.name = containerName container.suite = suite if image, ok := yamlInput["image"]; ok { @@ -76,7 +78,7 @@ func newContainer(suite *HstSuite, yamlInput ContainerConfig, pid string) (*Cont } if _, ok := yamlInput["volumes"]; ok { - workingVolumeDir := logDir + container.suite.T().Name() + pid + volumeDir + workingVolumeDir := logDir + CurrentSpecReport().LeafNodeText + volumeDir workDirReplacer := strings.NewReplacer("$HST_DIR", workDir) volDirReplacer := strings.NewReplacer("$HST_VOLUME_DIR", workingVolumeDir) for _, volu := range yamlInput["volumes"].([]interface{}) { @@ -159,6 +161,12 @@ func (c *Container) create() error { return exechelper.Run(cmd) } +func (c *Container) allocateCpus() { + c.suite.startedContainers = append(c.suite.startedContainers, c) + c.allocatedCpus = c.suite.AllocateCpus() + c.suite.log("Allocated CPUs " + fmt.Sprint(c.allocatedCpus) + " to container " + c.name) +} + func (c *Container) start() error { cmd := "docker start " + c.name c.suite.log(cmd) @@ -172,8 +180,11 @@ func (c *Container) prepareCommand() (string, error) { cmd := "docker run " if c.runDetached { - cmd += " -d" + cmd += " -dt" } + + c.allocateCpus() + cmd += fmt.Sprintf(" --cpuset-cpus=\"%d-%d\"", c.allocatedCpus[0], c.allocatedCpus[len(c.allocatedCpus)-1]) cmd += " " + c.getContainerArguments() c.suite.log(cmd) @@ -249,7 +260,7 @@ func (c *Container) copy(sourceFileName string, targetFileName string) error { } func (c *Container) createFile(destFileName string, content string) error { - f, err := os.CreateTemp("/tmp", "hst-config" + c.suite.pid) + f, err := os.CreateTemp("/tmp", "hst-config"+c.suite.pid) if err != nil { return err } @@ -273,7 +284,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() + GinkgoHelper() c.suite.log(containerExecCommand) c.suite.assertNil(exechelper.Run(containerExecCommand)) } @@ -282,21 +293,21 @@ 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() + GinkgoHelper() c.suite.log(containerExecCommand) byteOutput, err := exechelper.CombinedOutput(containerExecCommand) - c.suite.assertNil(err, err) + c.suite.assertNil(err, fmt.Sprint(err)) return string(byteOutput) } func (c *Container) getLogDirPath() string { testId := c.suite.getTestId() - testName := c.suite.T().Name() + testName := CurrentSpecReport().LeafNodeText logDirPath := logDir + testName + "/" + testId + "/" cmd := exec.Command("mkdir", "-p", logDirPath) if err := cmd.Run(); err != nil { - c.suite.T().Fatalf("mkdir error: %v", err) + Fail("mkdir error: " + fmt.Sprint(err)) } return logDirPath @@ -313,12 +324,12 @@ func (c *Container) saveLogs() { 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) + Fail("fetching logs error: " + fmt.Sprint(err)) } f, err := os.Create(testLogFilePath) if err != nil { - c.suite.T().Fatalf("file create error: %v", err) + Fail("file create error: " + fmt.Sprint(err)) } fmt.Fprint(f, string(output)) f.Close() @@ -339,7 +350,7 @@ func (c *Container) log(maxLines int) (string, error) { } func (c *Container) stop() error { - if c.vppInstance != nil && c.vppInstance.apiChannel != nil { + if c.vppInstance != nil && c.vppInstance.apiStream != nil { c.vppInstance.saveLogs() c.vppInstance.disconnect() } |