From addafa187d71b21dd35a00d457c3b4d95a2a3e98 Mon Sep 17 00:00:00 2001 From: Giles Hutton Date: Mon, 12 Feb 2024 13:07:52 +0000 Subject: [PATCH 1/2] Always try to stop collector on cleanup --- integration-tests/suites/base.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/integration-tests/suites/base.go b/integration-tests/suites/base.go index 2cd9ba0de5..41177c5be1 100644 --- a/integration-tests/suites/base.go +++ b/integration-tests/suites/base.go @@ -164,9 +164,10 @@ func (s *IntegrationTestSuiteBase) RegisterCleanup(containers ...string) { // if resources are already gone. containers = append(containers, containerStatsName) s.cleanupContainers(containers...) - if running, _ := s.Collector().IsRunning(); running { - s.StopCollector() - } + // StopCollector is safe when collector isn't running, so + // unconditionally try to stop it. This will ensure that logs + // are still written even when test setup fails + s.StopCollector() }) } From 8ad387e506eed20554892c42d39c165430eafcbf Mon Sep 17 00:00:00 2001 From: Giles Hutton Date: Mon, 12 Feb 2024 14:59:26 +0000 Subject: [PATCH 2/2] Only cleanup collector when the container exists (doesnt have to be running) --- integration-tests/suites/base.go | 9 +++++---- integration-tests/suites/common/executor.go | 9 +++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/integration-tests/suites/base.go b/integration-tests/suites/base.go index 41177c5be1..3dd5c625d9 100644 --- a/integration-tests/suites/base.go +++ b/integration-tests/suites/base.go @@ -164,10 +164,11 @@ func (s *IntegrationTestSuiteBase) RegisterCleanup(containers ...string) { // if resources are already gone. containers = append(containers, containerStatsName) s.cleanupContainers(containers...) - // StopCollector is safe when collector isn't running, so - // unconditionally try to stop it. This will ensure that logs - // are still written even when test setup fails - s.StopCollector() + // StopCollector is safe when collector isn't running, but the container must exist. + // This will ensure that logs are still written even when test setup fails + if exists, _ := s.Executor().ContainerExists("collector"); exists { + s.StopCollector() + } }) } diff --git a/integration-tests/suites/common/executor.go b/integration-tests/suites/common/executor.go index 9265e11126..bad3448684 100644 --- a/integration-tests/suites/common/executor.go +++ b/integration-tests/suites/common/executor.go @@ -24,6 +24,7 @@ type Executor interface { CopyFromHost(src string, dst string) (string, error) PullImage(image string) error IsContainerRunning(image string) (bool, error) + ContainerExists(container string) (bool, error) ExitCode(container string) (int, error) Exec(args ...string) (string, error) ExecWithStdin(pipedContent string, args ...string) (string, error) @@ -200,6 +201,14 @@ func (e *executor) IsContainerRunning(containerID string) (bool, error) { return strconv.ParseBool(strings.Trim(result, "\"'")) } +func (e *executor) ContainerExists(container string) (bool, error) { + result, err := e.Exec(RuntimeCommand, "ps", "-aq", "--format=id="+container) + if err != nil { + return false, err + } + return strconv.ParseBool(strings.Trim(result, "\"'")) +} + func (e *executor) ExitCode(containerID string) (int, error) { result, err := e.Exec(RuntimeCommand, "inspect", containerID, "--format='{{.State.ExitCode}}'") if err != nil {