Skip to content

Commit

Permalink
Deflake enricher unit tests
Browse files Browse the repository at this point in the history
There is a race between the for loop evaluating the line and the test
assertion. We now wait for a function call to happen and then assert
correctly the results of it.

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
  • Loading branch information
saschagrunert authored and k8s-ci-robot committed Jul 28, 2023
1 parent cb7f885 commit 71d2a20
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
6 changes: 6 additions & 0 deletions internal/pkg/cli/runner/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package runner

import (
"encoding/json"
"log"
"os"
"sync/atomic"

Expand Down Expand Up @@ -56,6 +57,7 @@ type impl interface {
ExtractAuditLine(string) (*types.AuditLine, error)
GetName(libseccomp.ScmpSyscall) (string, error)
PidLoad() uint32
Printf(format string, v ...any)
}

func (*defaultImpl) ReadFile(name string) ([]byte, error) {
Expand Down Expand Up @@ -113,3 +115,7 @@ func (*defaultImpl) GetName(s libseccomp.ScmpSyscall) (string, error) {
func (*defaultImpl) PidLoad() uint32 {
return atomic.LoadUint32(&pid)
}

func (*defaultImpl) Printf(format string, v ...any) {
log.Printf(format, v...)
}
6 changes: 3 additions & 3 deletions internal/pkg/cli/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (r *Runner) printAuditLine(line *types.AuditLine) {
}

func (r *Runner) printSelinuxLine(line *types.AuditLine) {
log.Printf(
r.Printf(
"SELinux: perm: %s, scontext: %s, tcontext: %s, tclass: %s",
line.Perm, line.Scontext, line.Tcontext, line.Tclass,
)
Expand All @@ -180,11 +180,11 @@ func (r *Runner) printSeccompLine(line *types.AuditLine) {
return
}

log.Printf("Seccomp: %s (%d)", syscallName, line.SystemCallID)
r.Printf("Seccomp: %s (%d)", syscallName, line.SystemCallID)
}

func (r *Runner) printApparmorLine(line *types.AuditLine) {
log.Printf(
r.Printf(
"AppArmor: %s, operation: %s, profile: %s, name: %s, extra: %s",
line.Apparmor, line.Operation, line.Profile, line.Name, line.ExtraInfo,
)
Expand Down
27 changes: 26 additions & 1 deletion internal/pkg/cli/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package runner
import (
"errors"
"testing"
"time"

"github.com/nxadm/tail"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -137,6 +138,20 @@ func TestRun(t *testing.T) {
}
}

func waitForFunctionCall(t *testing.T, fn func() int) {
t.Helper()

countGreaterZero := false
for i := 0; i < 5; i++ {
if fn() > 0 {
countGreaterZero = true
break
}
time.Sleep(time.Second)
}
require.True(t, countGreaterZero)
}

func TestStartEnricher(t *testing.T) {
const testPid = 123

Expand All @@ -160,7 +175,9 @@ func TestStartEnricher(t *testing.T) {
},
assert: func(mock *runnerfakes.FakeImpl, lineChan chan *tail.Line) {
lineChan <- &tail.Line{}
require.Equal(t, 1, mock.GetNameCallCount())
waitForFunctionCall(t, mock.PrintfCallCount)
arg, _ := mock.PrintfArgsForCall(0)
require.Contains(t, arg, "Seccomp")
},
},
{
Expand All @@ -178,6 +195,8 @@ func TestStartEnricher(t *testing.T) {
},
assert: func(mock *runnerfakes.FakeImpl, lineChan chan *tail.Line) {
lineChan <- &tail.Line{}
waitForFunctionCall(t, mock.GetNameCallCount)
require.Zero(t, mock.PrintfCallCount())
},
},
{
Expand All @@ -194,6 +213,9 @@ func TestStartEnricher(t *testing.T) {
},
assert: func(mock *runnerfakes.FakeImpl, lineChan chan *tail.Line) {
lineChan <- &tail.Line{}
waitForFunctionCall(t, mock.PrintfCallCount)
arg, _ := mock.PrintfArgsForCall(0)
require.Contains(t, arg, "AppArmor")
},
},
{
Expand All @@ -210,6 +232,9 @@ func TestStartEnricher(t *testing.T) {
},
assert: func(mock *runnerfakes.FakeImpl, lineChan chan *tail.Line) {
lineChan <- &tail.Line{}
waitForFunctionCall(t, mock.PrintfCallCount)
arg, _ := mock.PrintfArgsForCall(0)
require.Contains(t, arg, "SELinux")
},
},
{
Expand Down
41 changes: 41 additions & 0 deletions internal/pkg/cli/runner/runnerfakes/fake_impl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 71d2a20

Please sign in to comment.