Skip to content

Commit

Permalink
test(pkg/generate): ensure configurations are generated
Browse files Browse the repository at this point in the history
Signed-off-by: Massimiliano Giovagnoli <me@maxgio.it>
  • Loading branch information
maxgio92 authored and poiana committed Sep 26, 2023
1 parent a689ae7 commit ef0b165
Showing 1 changed file with 78 additions and 15 deletions.
93 changes: 78 additions & 15 deletions pkg/generate/generate_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package generate

import (
"os"
"testing"

"github.com/falcosecurity/dbg-go/pkg/root"
"github.com/falcosecurity/dbg-go/pkg/stats"
testutils "github.com/falcosecurity/dbg-go/pkg/utils/test"
"github.com/falcosecurity/dbg-go/pkg/validate"
"github.com/stretchr/testify/assert"
"os"
"testing"
)

func BenchmarkAutogenerate(b *testing.B) {
Expand All @@ -33,8 +35,9 @@ func BenchmarkAutogenerate(b *testing.B) {
func TestGenerate(t *testing.T) {
testCacheData = true // enable json data caching for subsequent tests
tests := map[string]struct {
opts Options
expectError bool
opts Options
expectError bool
expectedMinConfigs int
}{
"run in auto mode with loaded from kernel-crawler distro on multiple driver versions": {
opts: Options{
Expand All @@ -49,7 +52,8 @@ func TestGenerate(t *testing.T) {
},
Auto: true,
},
expectError: false,
expectError: false,
expectedMinConfigs: 1,
},
"run in auto mode with any target distro filter on single driver version": {
opts: Options{
Expand All @@ -61,7 +65,8 @@ func TestGenerate(t *testing.T) {
},
Auto: true,
},
expectError: false,
expectError: false,
expectedMinConfigs: 1,
},
"run in auto mode with target distro filter on single driver version": {
opts: Options{
Expand All @@ -76,7 +81,8 @@ func TestGenerate(t *testing.T) {
},
Auto: true,
},
expectError: false,
expectError: false,
expectedMinConfigs: 1,
},
"run in auto mode with non existent target distro on single driver version": {
opts: Options{
Expand All @@ -91,7 +97,8 @@ func TestGenerate(t *testing.T) {
},
Auto: true,
},
expectError: false, // we do not expect any error; no configs will be generated though
expectError: false, // we do not expect any error; no configs will be generated though
expectedMinConfigs: 0,
},
"run in auto mode with single target distro on single driver version with custom driver name": {
opts: Options{
Expand All @@ -106,7 +113,43 @@ func TestGenerate(t *testing.T) {
},
Auto: true,
},
expectError: false,
expectError: false,
expectedMinConfigs: 1,
},
"run in auto mode with single target distro with regex kernel version on single driver version": {
opts: Options{
Options: root.Options{
RepoRoot: "./test/",
Architecture: "amd64",
DriverVersion: []string{"1.0.0+driver"},
Target: root.Target{
Distro: "centos",
KernelVersion: "^1$",
},
DriverName: "falco",
},
Auto: true,
},
expectError: false,
expectedMinConfigs: 1,
},
"run in auto mode with single target distro with regex kernel release on single driver version": {
opts: Options{
Options: root.Options{
RepoRoot: "./test/",
Architecture: "amd64",
DriverVersion: []string{"1.0.0+driver"},
Target: root.Target{
Distro: "centos",
KernelRelease: `^5\..+$`,
KernelVersion: "1",
},
DriverName: "falco",
},
Auto: true,
},
expectError: false,
expectedMinConfigs: 1,
},
"run in auto mode with regex target distro on single driver version with custom driver name": {
opts: Options{
Expand All @@ -121,7 +164,8 @@ func TestGenerate(t *testing.T) {
},
Auto: true,
},
expectError: false,
expectError: false,
expectedMinConfigs: 1,
},
"run with empty target kernel release on single driver version": {
opts: Options{
Expand All @@ -136,7 +180,8 @@ func TestGenerate(t *testing.T) {
DriverName: "falco",
},
},
expectError: true,
expectError: true,
expectedMinConfigs: 1,
},
"run with empty target kernel version on single driver version": {
opts: Options{
Expand All @@ -151,7 +196,8 @@ func TestGenerate(t *testing.T) {
DriverName: "falco",
},
},
expectError: true,
expectError: true,
expectedMinConfigs: 1,
},
"run with empty target distro on single driver version": {
opts: Options{
Expand All @@ -166,7 +212,8 @@ func TestGenerate(t *testing.T) {
DriverName: "falco",
},
},
expectError: true,
expectError: true,
expectedMinConfigs: 1,
},
"run with unsupported target distro on single driver version": {
opts: Options{
Expand All @@ -182,7 +229,8 @@ func TestGenerate(t *testing.T) {
DriverName: "falco",
},
},
expectError: true,
expectError: true,
expectedMinConfigs: 0,
},
// NOTE: the below test is flaky: if debian pulls down the headers, we will break.
// in case, just update to a newer version.
Expand All @@ -200,7 +248,8 @@ func TestGenerate(t *testing.T) {
DriverName: "falco",
},
},
expectError: false,
expectError: false,
expectedMinConfigs: 1,
},
}

Expand All @@ -216,6 +265,20 @@ func TestGenerate(t *testing.T) {
assert.Error(t, err)
} else {
assert.NoError(t, err)

// Ensure configs are generated.
statter := stats.NewFileStatter()

// Get stats on generated configurations for all kernels and distros.
statsOpts := test.opts.Options
statsOpts.KernelVersion = ""
statsOpts.KernelRelease = ""
statsOpts.Distro = ""

stats, err := statter.GetDriverStats(statsOpts)
assert.NoError(t, err)
assert.GreaterOrEqual(t, len(stats), test.expectedMinConfigs)

// Validate all generated files
validateOpts := validate.Options{Options: test.opts.Options}
if validateOpts.Distro == "load" {
Expand Down

0 comments on commit ef0b165

Please sign in to comment.