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

Create ContainerID wrapper for integration tests #1995

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion integration-tests/pkg/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Manager interface {
Launch() error
TearDown() error
IsRunning() (bool, error)
ContainerID() string
ContainerID() executor.ContainerID
TestName() string
}

Expand Down
23 changes: 10 additions & 13 deletions integration-tests/pkg/collector/collector_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"golang.org/x/exp/maps"

"github.com/hashicorp/go-multierror"
"github.com/stackrox/collector/integration-tests/pkg/common"
"github.com/stackrox/collector/integration-tests/pkg/config"
"github.com/stackrox/collector/integration-tests/pkg/executor"
)
Expand All @@ -22,7 +21,7 @@ type DockerCollectorManager struct {
testName string

CollectorOutput string
containerID string
containerID executor.ContainerID
}

func NewDockerCollectorManager(e executor.Executor, name string) *DockerCollectorManager {
Expand Down Expand Up @@ -91,7 +90,7 @@ func (c *DockerCollectorManager) TearDown() error {

// Check if collector container segfaulted or exited with error
exitCode, err := c.executor.ExitCode(executor.ContainerFilter{
Name: "collector",
Id: executor.ContainerID("collector"),
})
if err != nil || exitCode != 0 {
logsEnd := ""
Expand Down Expand Up @@ -145,24 +144,22 @@ func (c *DockerCollectorManager) launchCollector() error {
if err != nil {
return err
}
output, err := c.executor.StartContainer(startConfig)
c.CollectorOutput = output
id, err := c.executor.StartContainer(startConfig)
if err != nil {
return err
}
outLines := strings.Split(output, "\n")
c.containerID = common.ContainerShortID(string(outLines[len(outLines)-1]))
return err
c.containerID = id
return nil
}

func (c *DockerCollectorManager) captureLogs(containerName string) (string, error) {
return c.executor.CaptureLogs(c.testName, containerName)
return c.executor.CaptureLogs(c.testName, executor.ContainerID(containerName))
}

func (c *DockerCollectorManager) killContainer(name string) error {
_, err1 := c.executor.KillContainer(name)
_, err1 := c.executor.KillContainer(executor.ContainerID(name))
_, err2 := c.executor.RemoveContainer(executor.ContainerFilter{
Name: name,
Id: executor.ContainerID(name),
})

var result error
Expand All @@ -177,11 +174,11 @@ func (c *DockerCollectorManager) killContainer(name string) error {
}

func (c *DockerCollectorManager) stopContainer(name string) error {
_, err := c.executor.StopContainer(name)
_, err := c.executor.StopContainer(executor.ContainerID(name))
return err
}

func (c *DockerCollectorManager) ContainerID() string {
func (c *DockerCollectorManager) ContainerID() executor.ContainerID {
return c.containerID
}

Expand Down
10 changes: 5 additions & 5 deletions integration-tests/pkg/collector/collector_k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type K8sCollectorManager struct {

eventWatcher watch.Interface

containerID string
containerID executor.ContainerID
ip string
}

Expand Down Expand Up @@ -165,7 +165,7 @@ func (k *K8sCollectorManager) TearDown() error {

if !isRunning {
exitCode, err := k.executor.ExitCode(executor.ContainerFilter{
Name: "collector",
Id: executor.ContainerID("collector"),
Namespace: TEST_NAMESPACE,
})
if err != nil {
Expand All @@ -185,7 +185,7 @@ func (k *K8sCollectorManager) TearDown() error {
}

return k.executor.WaitPodRemoved(executor.ContainerFilter{
Name: "collector",
Id: executor.ContainerID("collector"),
Namespace: TEST_NAMESPACE,
})
}
Expand All @@ -199,10 +199,10 @@ func (k *K8sCollectorManager) IsRunning() (bool, error) {
return *pod.Status.ContainerStatuses[0].Started, nil
}

func (k *K8sCollectorManager) ContainerID() string {
func (k *K8sCollectorManager) ContainerID() executor.ContainerID {
if k.containerID == "" {
cf := executor.ContainerFilter{
Name: "collector",
Id: executor.ContainerID("collector"),
Namespace: TEST_NAMESPACE,
}

Expand Down
16 changes: 16 additions & 0 deletions integration-tests/pkg/common/container_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package common

// ContainerID is an identifier for a container. It can
// be a container name *OR* a hex ID
type ContainerID string

// Long returns the whole containerID as a string
func (c ContainerID) Long() string {
return string(c)
}

// Short returns the first twelve character of a containerID
// to match the shortened IDs returned by docker.
func (c ContainerID) Short() string {
return string(c)[:12]
}
6 changes: 0 additions & 6 deletions integration-tests/pkg/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ import (
"k8s.io/utils/strings/slices"
)

// containerShortID returns the first twelve character of a containerID
// to match the shortened IDs returned by docker.
func ContainerShortID(containerID string) string {
return containerID[0:12]
}

// quoteArgs will add quotes around any arguments require it for the shell.
func QuoteArgs(args []string) []string {
quotedArgs := []string{}
Expand Down
39 changes: 27 additions & 12 deletions integration-tests/pkg/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,23 @@ import (
"github.com/stackrox/collector/integration-tests/pkg/config"
)

// ContainerID is an identifier for a container. It can
// be a container name *OR* a hex ID
type ContainerID string

// Long returns the whole containerID as a string
func (c ContainerID) Long() string {
return string(c)
}

// Short returns the first twelve character of a containerID
// to match the shortened IDs returned by docker.
func (c ContainerID) Short() string {
return string(c)[:12]
}

type ContainerFilter struct {
Name string
Id ContainerID
Namespace string
}

Expand All @@ -32,20 +47,20 @@ func (l *ContainerLogs) GetSingleLog() string {

type Executor interface {
PullImage(image string) error
IsContainerRunning(container string) (bool, error)
IsContainerRunning(container ContainerID) (bool, error)
ContainerExists(filter ContainerFilter) (bool, error)
ExitCode(filter ContainerFilter) (int, error)
ExecContainer(containerName string, command []string) (string, error)
KillContainer(name string) (string, error)
ExecContainer(container ContainerID, command []string) (string, error)
KillContainer(container ContainerID) (string, error)
RemoveContainer(filter ContainerFilter) (string, error)
StopContainer(name string) (string, error)
StartContainer(config config.ContainerStartConfig) (string, error)
GetContainerHealthCheck(containerID string) (string, error)
GetContainerIP(containerID string) (string, error)
GetContainerLogs(containerID string) (ContainerLogs, error)
CaptureLogs(testName, containerName string) (string, error)
GetContainerPort(containerID string) (string, error)
IsContainerFoundFiltered(containerID, filter string) (bool, error)
StopContainer(container ContainerID) (string, error)
StartContainer(config config.ContainerStartConfig) (ContainerID, error)
GetContainerHealthCheck(container ContainerID) (string, error)
GetContainerIP(container ContainerID) (string, error)
GetContainerLogs(container ContainerID) (ContainerLogs, error)
CaptureLogs(testName string, container ContainerID) (string, error)
GetContainerPort(container ContainerID) (string, error)
IsContainerFoundFiltered(container ContainerID, filter string) (bool, error)
}

type CommandBuilder interface {
Expand Down
Loading
Loading