Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Collector is always cleaned up #1548

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion integration-tests/suites/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ func (s *IntegrationTestSuiteBase) RegisterCleanup(containers ...string) {
// if resources are already gone.
containers = append(containers, containerStatsName)
s.cleanupContainers(containers...)
if running, _ := s.Collector().IsRunning(); running {
// 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()
}
})
Expand Down
9 changes: 9 additions & 0 deletions integration-tests/suites/common/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
Loading