Skip to content

Commit

Permalink
fix(pkg): disable log styling for tests.
Browse files Browse the repository at this point in the history
Also, fixed tests.

Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP authored and poiana committed May 2, 2024
1 parent f29a571 commit cb3b85f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 104 deletions.
60 changes: 30 additions & 30 deletions pkg/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestBuild(t *testing.T) {
RepoRoot: "./test",
Target: root.Target{
Distro: "centos",
KernelRelease: "4.18.0-535.el8.x86_64",
KernelRelease: "4.18.0-545.el8.x86_64",
KernelVersion: "1",
},
},
Expand All @@ -64,15 +64,15 @@ func TestBuild(t *testing.T) {
IgnoreErrors: false,
},
expectedLocalObjects: []string{
"falco_centos_4.18.0-535.el8.x86_64_1.ko",
"falco_centos_4.18.0-535.el8.x86_64_1.o",
"falco_centos_4.18.0-545.el8.x86_64_1.ko",
"falco_centos_4.18.0-545.el8.x86_64_1.o",
},
expectedBucketObjects: []string{
"falco_centos_4.18.0-535.el8.x86_64_1.ko",
"falco_centos_4.18.0-535.el8.x86_64_1.o",
"falco_centos_4.18.0-545.el8.x86_64_1.ko",
"falco_centos_4.18.0-545.el8.x86_64_1.o",
},
shouldCreate: true,
name: "build 5.0.1+driver centos 4.18.0-535.el8.x86_64",
name: "build 5.0.1+driver centos 4.18.0-545.el8.x86_64",
},
{
opts: Options{
Expand All @@ -83,7 +83,7 @@ func TestBuild(t *testing.T) {
RepoRoot: "./test",
Target: root.Target{
Distro: "centos",
KernelRelease: "4.18.0-539.el8.x86_64",
KernelRelease: "4.18.0-546.el8.x86_64",
KernelVersion: "1",
},
},
Expand All @@ -92,17 +92,17 @@ func TestBuild(t *testing.T) {
IgnoreErrors: false,
},
expectedLocalObjects: []string{
"falco_centos_4.18.0-539.el8.x86_64_1.ko",
"falco_centos_4.18.0-539.el8.x86_64_1.o",
"falco_centos_4.18.0-535.el8.x86_64_1.ko",
"falco_centos_4.18.0-535.el8.x86_64_1.o",
"falco_centos_4.18.0-546.el8.x86_64_1.ko",
"falco_centos_4.18.0-546.el8.x86_64_1.o",
"falco_centos_4.18.0-545.el8.x86_64_1.ko",
"falco_centos_4.18.0-545.el8.x86_64_1.o",
},
expectedBucketObjects: []string{
"falco_centos_4.18.0-535.el8.x86_64_1.ko",
"falco_centos_4.18.0-535.el8.x86_64_1.o",
"falco_centos_4.18.0-545.el8.x86_64_1.ko",
"falco_centos_4.18.0-545.el8.x86_64_1.o",
},
shouldCreate: false, // since it is not publishing
name: "build 5.0.1+driver centos 4.18.0-539.el8.x86_64",
name: "build 5.0.1+driver centos 4.18.0-546.el8.x86_64",
},
{
opts: Options{
Expand All @@ -113,7 +113,7 @@ func TestBuild(t *testing.T) {
RepoRoot: "./test",
Target: root.Target{
Distro: "centos",
KernelRelease: "4.18.0-535.el8.x86_64", // try to rebuild same object.
KernelRelease: "4.18.0-545.el8.x86_64", // try to rebuild same object.
KernelVersion: "1",
},
},
Expand All @@ -122,17 +122,17 @@ func TestBuild(t *testing.T) {
IgnoreErrors: false,
},
expectedLocalObjects: []string{
"falco_centos_4.18.0-539.el8.x86_64_1.ko",
"falco_centos_4.18.0-539.el8.x86_64_1.o",
"falco_centos_4.18.0-535.el8.x86_64_1.ko",
"falco_centos_4.18.0-535.el8.x86_64_1.o",
"falco_centos_4.18.0-546.el8.x86_64_1.ko",
"falco_centos_4.18.0-546.el8.x86_64_1.o",
"falco_centos_4.18.0-545.el8.x86_64_1.ko",
"falco_centos_4.18.0-545.el8.x86_64_1.o",
},
expectedBucketObjects: []string{
"falco_centos_4.18.0-535.el8.x86_64_1.ko",
"falco_centos_4.18.0-535.el8.x86_64_1.o",
"falco_centos_4.18.0-545.el8.x86_64_1.ko",
"falco_centos_4.18.0-545.el8.x86_64_1.o",
},
shouldCreate: false, // since objects are already present, nothing should be created
name: "rebuild 5.0.1+driver centos 4.18.0-535.el8.x86_64",
name: "rebuild 5.0.1+driver centos 4.18.0-545.el8.x86_64",
},
{
opts: Options{
Expand All @@ -143,7 +143,7 @@ func TestBuild(t *testing.T) {
RepoRoot: "./test",
Target: root.Target{
Distro: "centos",
KernelRelease: "4.18.0-535.el8.x86_64", // try to rebuild same object.
KernelRelease: "4.18.0-545.el8.x86_64", // try to rebuild same object.
KernelVersion: "1",
},
},
Expand All @@ -152,17 +152,17 @@ func TestBuild(t *testing.T) {
IgnoreErrors: false,
},
expectedLocalObjects: []string{
"falco_centos_4.18.0-539.el8.x86_64_1.ko",
"falco_centos_4.18.0-539.el8.x86_64_1.o",
"falco_centos_4.18.0-535.el8.x86_64_1.ko",
"falco_centos_4.18.0-535.el8.x86_64_1.o",
"falco_centos_4.18.0-546.el8.x86_64_1.ko",
"falco_centos_4.18.0-546.el8.x86_64_1.o",
"falco_centos_4.18.0-545.el8.x86_64_1.ko",
"falco_centos_4.18.0-545.el8.x86_64_1.o",
},
expectedBucketObjects: []string{
"falco_centos_4.18.0-535.el8.x86_64_1.ko",
"falco_centos_4.18.0-535.el8.x86_64_1.o",
"falco_centos_4.18.0-545.el8.x86_64_1.ko",
"falco_centos_4.18.0-545.el8.x86_64_1.o",
},
shouldCreate: true,
name: "rebuild and publish 5.0.1+driver centos 4.18.0-535.el8.x86_64",
name: "rebuild and publish 5.0.1+driver centos 4.18.0-545.el8.x86_64",
},
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/cleanup/cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package cleanup

import (
"context"
"encoding/json"
"io"
"os"
"strings"
Expand Down Expand Up @@ -175,7 +176,9 @@ func TestCleanupFiltered(t *testing.T) {
lines := 0
testutils.RunTestParsingLogs(t, func() error {
return Run(test.opts, NewFileCleaner())
}, &messageJSON, func() bool {
}, func(line []byte) bool {
err = json.Unmarshal(line, &messageJSON)
assert.NoError(t, err)
if messageJSON.Path == "" {
return true // skip and go on
}
Expand Down
13 changes: 1 addition & 12 deletions pkg/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,20 @@ package stats

import (
"github.com/falcosecurity/dbg-go/pkg/root"
"io"
"os"
"strconv"

"github.com/olekukonko/tablewriter"
)

// Used by tests!
// We cannot simply use table = tablewriter.NewWriter(log.Default().Writer())
// as that would completely break tablewriter output.
var testOutputWriter io.Writer

func Run(opts Options, statter Statter) error {
root.Printer.Logger.Info(statter.Info())
driverStatsByVersion, err := statter.GetDriverStats(opts.Options)
if err != nil {
return err
}

var table *tablewriter.Table
if testOutputWriter != nil {
table = tablewriter.NewWriter(testOutputWriter)
} else {
table = tablewriter.NewWriter(os.Stdout)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Version", "Modules", "Probes"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
Expand Down
57 changes: 4 additions & 53 deletions pkg/stats/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ limitations under the License.
package stats

import (
"log"
"os"
"strconv"
"strings"
"testing"

"github.com/falcosecurity/dbg-go/pkg/root"
Expand Down Expand Up @@ -218,58 +215,12 @@ func TestStats(t *testing.T) {
},
}

// capture output!

statter := NewFileStatter()
for name, test := range tests {
t.Run(name, func(t *testing.T) {
// Use logged output to ensure we fetched correct stats
type MessageJSON struct {
Message string `json:"msg"`
}
var messageJSON MessageJSON
outputStats := driverStats{}
startParsing := false
parsingIdx := 0
testutils.RunTestParsingLogs(t,
func() error {
testOutputWriter = log.Default().Writer()
return Run(test.opts, NewFileStatter())
},
&messageJSON,
func() bool {
messageJSON.Message = strings.ReplaceAll(messageJSON.Message, " ", "")
// Example lines:
//{"time":"2023-08-29T11:38:35.692782942+02:00","level":"INFO","msg":"1.0.0+driver"}
//{"time":"2023-08-29T11:38:35.692784013+02:00","level":"INFO","msg":""}
//{"time":"2023-08-29T11:38:35.692784775+02:00","level":"INFO","msg":"|"}
//{"time":"2023-08-29T11:38:35.692785487+02:00","level":"INFO","msg":""}
//{"time":"2023-08-29T11:38:35.69279484+02:00","level":"INFO","msg":"4"}
//{"time":"2023-08-29T11:38:35.692796064+02:00","level":"INFO","msg":""}
//{"time":"2023-08-29T11:38:35.692797001+02:00","level":"INFO","msg":"|"}
//{"time":"2023-08-29T11:38:35.692797848+02:00","level":"INFO","msg":""}
//{"time":"2023-08-29T11:38:35.69280042+02:00","level":"INFO","msg":"3"}
if startParsing {
parsingIdx++
if parsingIdx%4 == 0 {
n, err := strconv.ParseInt(messageJSON.Message, 10, 64)
assert.NoError(t, err)
switch parsingIdx / 4 {
case 1:
outputStats.NumModules = n
case 2:
outputStats.NumProbes = n
}
}
}
if messageJSON.Message == "1.0.0+driver" {
startParsing = true
} else if parsingIdx == 8 {
// parsed both numbers
return false // break out
}
return true // continue
})
assert.Equal(t, test.expectedStats, outputStats)
driverStatsByVersion, err := statter.GetDriverStats(test.opts.Options)
assert.NoError(t, err)
assert.Equal(t, test.expectedStats, driverStatsByVersion["1.0.0+driver"])
})
}
}
Expand Down
17 changes: 11 additions & 6 deletions pkg/utils/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,30 @@ import (
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/falcosecurity/dbg-go/pkg/root"
s3utils "github.com/falcosecurity/dbg-go/pkg/utils/s3"
"github.com/falcosecurity/falcoctl/pkg/output"
"github.com/johannesboyne/gofakes3"
"github.com/johannesboyne/gofakes3/backend/s3mem"
json "github.com/json-iterator/go"
"github.com/pterm/pterm"
"github.com/stretchr/testify/assert"
)

func RunTestParsingLogs(t *testing.T, runTest func() error, parsedMsg interface{}, parsingCB func() bool) {
func RunTestParsingLogs(t *testing.T, runTest func() error, parsingCB func([]byte) bool) {
var buf bytes.Buffer

root.Printer = root.Printer.WithWriter(&buf)
// disable styling for tests and set json format
printer := root.Printer
root.Printer = output.NewPrinter(pterm.LogLevelInfo, pterm.LogFormatterJSON, &buf)
defer func() {
// reset default printer
root.Printer = printer
}()

err := runTest()
assert.NoError(t, err)

scanner := bufio.NewScanner(&buf)
for scanner.Scan() {
err = json.Unmarshal(scanner.Bytes(), parsedMsg)
assert.NoError(t, err)
if parsingCB() == false {
if parsingCB(scanner.Bytes()) == false {
break
}
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
package validate

import (
"encoding/json"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -455,8 +456,9 @@ func TestValidateConfigFiltered(t *testing.T) {
func() error {
return Run(test.opts)
},
&messageJSON,
func() bool {
func(line []byte) bool {
err = json.Unmarshal(line, &messageJSON)
assert.NoError(t, err)
if messageJSON.Config == "" {
return true // go on
}
Expand Down

0 comments on commit cb3b85f

Please sign in to comment.