Skip to content

Commit

Permalink
remove extra step
Browse files Browse the repository at this point in the history
  • Loading branch information
robbycochran committed Aug 29, 2024
1 parent 233552d commit 4c7d4f0
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 43 deletions.
3 changes: 0 additions & 3 deletions integration-tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ $(foreach element,$(ALL_TESTS),$(eval $(call make-test-target-dockerized,$(eleme
build:
mkdir -p bin
go test -tags bench,k8s -c -o bin/collector-tests
go version
set -o pipefail; \
go test -timeout 120m -v 2>&1 | tee -a integration-test.log

.PHONY: build-image
build-image:
Expand Down
6 changes: 1 addition & 5 deletions integration-tests/pkg/executor/executor_docker_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,10 @@ func (d *dockerAPIExecutor) IsContainerFoundFiltered(containerID, filter string)
}

func (d *dockerAPIExecutor) GetContainerHealthCheck(containerID string) (string, error) {
ctx := context.Background()
defer d.client.Close()

inspectResp, err := d.client.ContainerInspect(ctx, containerID)
inspectResp, err := d.inspectContainer(containerID)
if err != nil {
return "", fmt.Errorf("error inspecting container: %w", err)
}

if inspectResp.Config.Healthcheck == nil {
return "", fmt.Errorf("container %s does not have a health check", containerID)
}
Expand Down
21 changes: 10 additions & 11 deletions integration-tests/pkg/executor/executor_k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/stackrox/collector/integration-tests/pkg/common"
"github.com/stackrox/collector/integration-tests/pkg/log"
coreV1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
Expand All @@ -24,17 +25,15 @@ type K8sExecutor struct {
}

func NewK8sExecutor() (*K8sExecutor, error) {
fmt.Println("Creating k8s configuration")
log.Info("Creating k8s configuration")
config, err := rest.InClusterConfig()
if err != nil {
fmt.Printf("Error: Failed to get cluster config: %s\n", err)
return nil, err
return nil, log.ErrorWrap(err, "Failed to get cluster config")
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
fmt.Printf("Error: Failed to create client: %s", err)
return nil, err
return nil, log.ErrorWrap(err, "Failed to create client")
}

k8s := &K8sExecutor{
Expand All @@ -59,7 +58,7 @@ func (e *K8sExecutor) IsPodRunning(podName string) (bool, error) {
func (e *K8sExecutor) PodContainerID(podFilter ContainerFilter) string {
pod, err := e.ClientSet().CoreV1().Pods(podFilter.Namespace).Get(context.Background(), podFilter.Name, metaV1.GetOptions{})
if err != nil {
fmt.Printf("%s\n", err)
log.Error("namespace: %s pod: %s error: %s\n", podFilter.Namespace, podFilter.Name, err)
return ""
}

Expand All @@ -73,13 +72,14 @@ func (e *K8sExecutor) PodContainerID(podFilter ContainerFilter) string {
*/
containerID := pod.Status.ContainerStatuses[0].ContainerID
if len(containerID) < 12 {
fmt.Printf("Invalid container ID: %q\n", containerID)
log.Error("Invalid container ID: %q", containerID)
return ""
return ""
}

i := strings.LastIndex(containerID, "/")
if i == -1 {
fmt.Printf("Invalid container ID: %q\n", containerID)
log.Error("Invalid container ID: %q", containerID)
return ""
}

Expand Down Expand Up @@ -186,14 +186,13 @@ func (e *K8sExecutor) CreateNamespaceEventWatcher(testName, ns string) (watch.In
for event := range watcher.ResultChan() {
eventJson, err := json.Marshal(event)
if err != nil {
fmt.Printf("Failed to marshal event: %+v\n", event)
fmt.Printf("%s\n", err)
log.Error("Failed to marshal event %+v: %s", event, err)
return
}

_, err = logFile.WriteString(string(eventJson) + "\n")
if err != nil {
fmt.Printf("Failed to write to event file: %s\n", err)
log.Error("Failed to write to event file: %s", err)
return
}
}
Expand Down
29 changes: 28 additions & 1 deletion integration-tests/pkg/log/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
type LogLevel int

const (
DebugLevel LogLevel = iota
TraceLevel LogLevel = iota
DebugLevel
InfoLevel
WarnLevel
ErrorLevel
)

Expand All @@ -30,8 +32,12 @@ func init() {

func parseLogLevel(levelStr string) LogLevel {
switch strings.ToLower(levelStr) {
case "trace":
return TraceLevel
case "debug":
return DebugLevel
case "warn":
return WarnLevel
case "info":
return InfoLevel
case "error":
Expand Down Expand Up @@ -63,6 +69,12 @@ func (l *Logger) Info(format string, v ...interface{}) {
}
}

func (l *Logger) Warn(format string, v ...interface{}) {
if l.level <= WarnLevel {
l.logger.Printf("WARN: "+format, v...)
}
}

func (l *Logger) Log(format string, v ...interface{}) {
l.logger.Printf(format, v...)
}
Expand All @@ -72,6 +84,13 @@ func (l *Logger) Error(format string, v ...interface{}) error {
return fmt.Errorf(format, v...)
}

func (l *Logger) ErrorWrap(err error, format string, v ...interface{}) error {
errMsg := fmt.Sprintf(format, v...)
errMsgWithErr := fmt.Errorf("%s: %w", errMsg, err)
l.logger.Printf("ERROR: %s", errMsgWithErr)
return err
}

func Log(format string, v ...interface{}) {
DefaultLogger.Log(format, v...)
}
Expand All @@ -84,6 +103,14 @@ func Info(format string, v ...interface{}) {
DefaultLogger.Info(format, v...)
}

func Warn(format string, v ...interface{}) {
DefaultLogger.Warn(format, v...)
}

func Error(format string, v ...interface{}) error {
return DefaultLogger.Error(format, v...)
}

func ErrorWrap(err error, format string, v ...interface{}) error {
return DefaultLogger.ErrorWrap(err, format, v...)
}
14 changes: 7 additions & 7 deletions integration-tests/suites/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,24 +319,24 @@ func (s *IntegrationTestSuiteBase) launchContainer(name string, args ...string)
}
c := config.ContainerStartConfig{}
c.Name = name
skip := 0
skip := false
for i, arg := range args {
if skip == 0 && len(arg) > 0 && arg[0] != '-' {
if skip && len(arg) > 0 && arg[0] != '-' {
c.Image = arg
if len(args) > i+1 {
c.Command = args[i+1:]
}
goto done
}
if skip > 0 {
skip--
if skip {
skip = false
continue
}
switch arg {
case "--entrypoint":
s.Require().True(len(args) > i+1)
c.Entrypoint = []string{args[i+1]}
skip = 1
skip = true
break
case "-e":
s.Require().True(len(args) > i+1)
Expand All @@ -349,7 +349,7 @@ func (s *IntegrationTestSuiteBase) launchContainer(name string, args ...string)
if len(env) > 1 {
c.Env[env[0]] = env[1]
}
skip = 1
skip = true
break
case "-v":
s.Require().True(len(args) > i+1)
Expand All @@ -362,7 +362,7 @@ func (s *IntegrationTestSuiteBase) launchContainer(name string, args ...string)
if len(vols) == 3 {
c.Mounts[vols[0]] += ":" + vols[2]
}
skip = 1
skip = true
break
case "--privileged":
c.Privileged = true
Expand Down
8 changes: 4 additions & 4 deletions integration-tests/suites/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/google/shlex"
"github.com/stackrox/collector/integration-tests/pkg/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -38,6 +39,7 @@ func (b *BenchmarkTestSuiteBase) StartPerfTools() {

if skipInit && (perf == "" && bpftrace == "" && bcc == "") {
fmt.Fprintf(os.Stderr, "COLLECTOR_SKIP_HEADERS_INIT set, but no performance tool requested - ignoring.")
log.Warn("COLLECTOR_SKIP_HEADERS_INIT set, but no performance tool requested - ignoring.")
return
}

Expand Down Expand Up @@ -114,12 +116,10 @@ func (b *BenchmarkTestSuiteBase) startContainer(name string, image string, args
}

func (b *BenchmarkTestSuiteBase) FetchWorkloadLogs() {
fmt.Println("Berserker logs:")
for _, container := range b.loadContainers {
log, err := b.containerLogs(container)
containerLog, err := b.containerLogs(container)
require.NoError(b.T(), err)

fmt.Println(log)
log.Info("benchmark workload log: %s %s`", container, containerLog)
}

b.loadContainers = nil
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/suites/http_endpoint_availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"

"github.com/stackrox/collector/integration-tests/pkg/collector"
"github.com/stackrox/collector/integration-tests/pkg/log"
)

type HttpEndpointAvailabilityTestSuite struct {
Expand All @@ -24,7 +25,7 @@ func (s *HttpEndpointAvailabilityTestSuite) TestAvailability() {
for _, endpoint := range s.Endpoints {
url := fmt.Sprintf("http://localhost:%d%s", s.Port, endpoint)

fmt.Println(url)
log.Info("url: %s", url)

resp, err := http.Get(url)
s.Require().NoError(err)
Expand Down
15 changes: 7 additions & 8 deletions integration-tests/suites/k8s_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ package suites
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"

"github.com/stackrox/collector/integration-tests/pkg/mock_sensor"
"github.com/stretchr/testify/suite"

"github.com/stackrox/collector/integration-tests/pkg/collector"
"github.com/stackrox/collector/integration-tests/pkg/executor"
"github.com/stackrox/collector/integration-tests/pkg/log"
"github.com/stackrox/collector/integration-tests/pkg/mock_sensor"

"github.com/stretchr/testify/suite"
coreV1 "k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
Expand Down Expand Up @@ -110,7 +109,7 @@ func (k *K8sNamespaceTestSuite) SetupSuite() {
// Self-check process is not going to be sent via GRPC, instead
// create at least one canary process to make sure everything is
// fine.
fmt.Printf("Spawn a canary process, container ID: %s\n", k.Collector().ContainerID())
log.Info("spawn a canary process, container ID: %s", k.Collector().ContainerID())
_, err = k.execPod("collector", collector.TEST_NAMESPACE, []string{"echo"})
k.Require().NoError(err)
})
Expand Down Expand Up @@ -141,14 +140,14 @@ func (k *K8sNamespaceTestSuite) TestK8sNamespace() {

for _, tt := range k.tests {
uri := baseUri + tt.containerID
fmt.Printf("Querying: %s\n", uri)
log.Info("Querying: %s\n", uri)
resp, err := http.Get(uri)
k.Require().NoError(err)
k.Require().True(resp.StatusCode == 200)

defer resp.Body.Close()
raw, err := io.ReadAll(resp.Body)
fmt.Printf("Response: %s\n", raw)
log.Info("Response: %s\n", raw)

var body map[string]interface{}
err = json.Unmarshal(raw, &body)
Expand Down Expand Up @@ -275,7 +274,7 @@ func (k *K8sNamespaceTestSuite) launchNginxPod() string {
Namespace: NAMESPACE,
}

fmt.Println("Waiting for nginx to start running")
log.Info("Waiting for nginx to start running")
k.watchPod("app=nginx", NAMESPACE, func() bool {
return k.Executor().PodContainerID(pf) != ""
})
Expand Down
6 changes: 3 additions & 3 deletions integration-tests/suites/perf_event_open.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package suites

import (
"fmt"
"strconv"
"strings"
"time"

"github.com/stackrox/collector/integration-tests/pkg/config"
"github.com/stackrox/collector/integration-tests/pkg/log"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -36,13 +36,13 @@ func (s *PerfEventOpenTestSuite) TestReadingTracepoints() {
if finished, _ := s.waitForContainerToExit("perf-event-open", containerID, 5*time.Second, 0); finished {
logs, err := s.containerLogs("perf-event-open")
if err != nil {
fmt.Println(logs)
log.Info(logs)
assert.FailNow(s.T(), "Failed to initialize host for performance testing")
}

count, err := strconv.Atoi(strings.TrimSpace(logs))
if err != nil {
fmt.Println(logs)
log.Info(logs)
assert.FailNow(s.T(), "Cannot convert result to the integer type")
}

Expand Down

0 comments on commit 4c7d4f0

Please sign in to comment.