Skip to content
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
58 changes: 30 additions & 28 deletions dev-tools/systemtests/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@
FatalLogMessages []string
// MonitorPID will tell tests to specifically check the correctness of process-level monitoring for this PID
MonitorPID int
// CreateHostProcess: this will start a process with the following args outside of the container,
// and use the integration tests to monitor it.
// Useful as "monitor random running processes" as a test heuristic tends to be flaky.
// This will overrite MonitorPID, so only set either this or MonitorPID.
CreateHostProcess *exec.Cmd
}

// RunResult returns the logs and return code from the container
Expand Down Expand Up @@ -131,13 +126,17 @@
defer apiClient.Close()

baseRunner := tr.Runner // some odd recursion happens here if we just refer to tr.Runner
parallel := len(cases) > 1
for _, tc := range cases {
baseRunner.Run(tc.String(), func(t *testing.T) {
runner := tr
runner := *tr
runner.Runner = t
runner.CgroupNSMode = tc.nsmode
runner.Privileged = tc.priv
runner.RunAsUser = tc.user
if parallel {
t.Parallel()
}
runner.RunTestsOnDocker(ctx, apiClient)
})
}
Expand Down Expand Up @@ -171,7 +170,9 @@
}

// create monitored process, if we need to
tr.createMonitoredProcess(ctx, log)
if cmd := tr.createMonitoredProcess(ctx, log); cmd != nil {
defer cmd.Cancel()

Check failure on line 174 in dev-tools/systemtests/container.go

View workflow job for this annotation

GitHub Actions / lint-darwin

Error return value is not checked (errcheck)

Check failure on line 174 in dev-tools/systemtests/container.go

View workflow job for this annotation

GitHub Actions / lint (windows)

Error return value is not checked (errcheck)

Check failure on line 174 in dev-tools/systemtests/container.go

View workflow job for this annotation

GitHub Actions / lint (linux)

Error return value is not checked (errcheck)
}

resp := tr.createTestContainer(ctx, log, apiClient)

Expand Down Expand Up @@ -298,26 +299,27 @@
return res
}

func (tr *DockerTestRunner) createMonitoredProcess(ctx context.Context, logger *logp.Logger) {
// if user has specified a process to monitor, start it now
// skip if the process has already been created
if tr.CreateHostProcess != nil && tr.CreateHostProcess.Process == nil {
// We don't need to do this in a channel, but it prevents races between this goroutine
// and the rest of test framework
startPid := make(chan int)
logger.Infof("Creating test Process...")
go func() {
err := tr.CreateHostProcess.Start()
// if the process fails to start up, the resulting tests will fail anyway, so just log it
assert.NoError(tr.Runner, err, "error starting monitor process")
startPid <- tr.CreateHostProcess.Process.Pid

}()
select {
case pid := <-startPid:
tr.MonitorPID = pid
case <-ctx.Done():
}
logger.Infof("Monitoring pid %d", tr.MonitorPID)
func (tr *DockerTestRunner) createMonitoredProcess(ctx context.Context, logger *logp.Logger) *exec.Cmd {
if tr.MonitorPID != 0 {
return nil
}
cmd := exec.CommandContext(ctx, "sleep", "240")
// We don't need to do this in a channel, but it prevents races between this goroutine
// and the rest of test framework
startPid := make(chan int)
logger.Infof("Creating test Process...")
go func() {
err := cmd.Start()
// if the process fails to start up, the resulting tests will fail anyway, so just log it
assert.NoError(tr.Runner, err, "error starting monitor process")
startPid <- cmd.Process.Pid

}()
select {
case pid := <-startPid:
tr.MonitorPID = pid
case <-ctx.Done():
}
logger.Infof("Monitoring pid %d", tr.MonitorPID)
return cmd
}
3 changes: 0 additions & 3 deletions tests/container_system_mon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import (
"context"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
Expand All @@ -42,7 +41,7 @@
if runtime.GOOS != "linux" {
t.Skip("test is linux-only")
}
_ = logp.DevelopmentSetup()

Check failure on line 44 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint-darwin

SA1019: logp.DevelopmentSetup is deprecated: Prefer using localized loggers. Use logp.NewDevelopmentLogger. (staticcheck)

Check failure on line 44 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

SA1019: logp.DevelopmentSetup is deprecated: Prefer using localized loggers. Use logp.NewDevelopmentLogger. (staticcheck)

Check failure on line 44 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint (linux)

SA1019: logp.DevelopmentSetup is deprecated: Prefer using localized loggers. Use logp.NewDevelopmentLogger. (staticcheck)
//manually fetch a kernel process
// kernel processes will have a parent pid of 2
dir, err := os.Open("/proc")
Expand Down Expand Up @@ -96,17 +95,16 @@
}

func TestProcessMetricsElevatedPerms(t *testing.T) {
_ = logp.DevelopmentSetup()

Check failure on line 98 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint-darwin

SA1019: logp.DevelopmentSetup is deprecated: Prefer using localized loggers. Use logp.NewDevelopmentLogger. (staticcheck)

Check failure on line 98 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

SA1019: logp.DevelopmentSetup is deprecated: Prefer using localized loggers. Use logp.NewDevelopmentLogger. (staticcheck)

Check failure on line 98 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint (linux)

SA1019: logp.DevelopmentSetup is deprecated: Prefer using localized loggers. Use logp.NewDevelopmentLogger. (staticcheck)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()
// runs test cases where we do not expect any kind of permissions errors
baseRunner := systemtests.DockerTestRunner{
Runner: t,

Check failure on line 103 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint-darwin

File is not properly formatted (goimports)

Check failure on line 103 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

File is not properly formatted (goimports)

Check failure on line 103 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint (linux)

File is not properly formatted (goimports)
Basepath: "./metric/system/process",
Verbose: true,
Privileged: true,
Testname: "TestSystemHostFromContainer",
CreateHostProcess: exec.Command("sleep", "240"),
FatalLogMessages: []string{"Error fetching PID info for", "Non-fatal error fetching"},
}

Expand All @@ -115,7 +113,7 @@
}

func TestProcessAllSettings(t *testing.T) {
_ = logp.DevelopmentSetup()

Check failure on line 116 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint-darwin

SA1019: logp.DevelopmentSetup is deprecated: Prefer using localized loggers. Use logp.NewDevelopmentLogger. (staticcheck)

Check failure on line 116 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

SA1019: logp.DevelopmentSetup is deprecated: Prefer using localized loggers. Use logp.NewDevelopmentLogger. (staticcheck)

Check failure on line 116 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint (linux)

SA1019: logp.DevelopmentSetup is deprecated: Prefer using localized loggers. Use logp.NewDevelopmentLogger. (staticcheck)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()
// runs test cases where we do not expect any kind of permissions errors
Expand All @@ -125,7 +123,6 @@
Verbose: false,
Privileged: true,
Testname: "TestSystemHostFromContainer",
CreateHostProcess: exec.Command("sleep", "480"),
FatalLogMessages: []string{"Error fetching PID info for"},
}

Expand All @@ -136,7 +133,7 @@
}

func TestContainerProcess(t *testing.T) {
_ = logp.DevelopmentSetup()

Check failure on line 136 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint-darwin

SA1019: logp.DevelopmentSetup is deprecated: Prefer using localized loggers. Use logp.NewDevelopmentLogger. (staticcheck)

Check failure on line 136 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

SA1019: logp.DevelopmentSetup is deprecated: Prefer using localized loggers. Use logp.NewDevelopmentLogger. (staticcheck)

Check failure on line 136 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint (linux)

SA1019: logp.DevelopmentSetup is deprecated: Prefer using localized loggers. Use logp.NewDevelopmentLogger. (staticcheck)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()
// Make sure that monitoring container procs from within the container still works
Expand All @@ -156,7 +153,7 @@
}

func TestFilesystem(t *testing.T) {
_ = logp.DevelopmentSetup()

Check failure on line 156 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint-darwin

SA1019: logp.DevelopmentSetup is deprecated: Prefer using localized loggers. Use logp.NewDevelopmentLogger. (staticcheck)

Check failure on line 156 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

SA1019: logp.DevelopmentSetup is deprecated: Prefer using localized loggers. Use logp.NewDevelopmentLogger. (staticcheck)

Check failure on line 156 in tests/container_system_mon_test.go

View workflow job for this annotation

GitHub Actions / lint (linux)

SA1019: logp.DevelopmentSetup is deprecated: Prefer using localized loggers. Use logp.NewDevelopmentLogger. (staticcheck)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()

Expand Down
Loading