Skip to content

Commit

Permalink
new(pkg,tests): added a test for Falco hot reload.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP authored and poiana committed May 9, 2024
1 parent 527631d commit dca902f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/run/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type localFileAccessor struct {

// NewLocalFileAccessor creates a FileAccessor of which content is the content
// of a file in the local filesystem
func NewLocalFileAccessor(name, path string) *localFileAccessor {
func NewLocalFileAccessor(name, path string) FileAccessor {
return &localFileAccessor{name: name, path: path}
}

Expand All @@ -55,12 +55,12 @@ type memFileAccessor struct {
}

// NewStringFileAccessor creates a FileAccessor that has a string as its content
func NewStringFileAccessor(name, content string) *memFileAccessor {
func NewStringFileAccessor(name, content string) FileAccessor {
return &memFileAccessor{name: name, content: ([]byte)(content)}
}

// NewBytesFileAccessor creates a FileAccessor that has a byte buf as its content
func NewBytesFileAccessor(name string, content []byte) *memFileAccessor {
func NewBytesFileAccessor(name string, content []byte) FileAccessor {
return &memFileAccessor{name: name, content: content}
}

Expand Down
39 changes: 39 additions & 0 deletions tests/falco/miscs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ limitations under the License.
package testfalco

import (
"fmt"
"github.com/falcosecurity/testing/pkg/run"
"github.com/falcosecurity/testing/tests/data/rules"
"os"
"path/filepath"
"testing"
"time"

"github.com/falcosecurity/testing/pkg/falco"
"github.com/falcosecurity/testing/tests"
Expand Down Expand Up @@ -71,3 +76,37 @@ func TestFalco_Miscs_StartupFail(t *testing.T) {
assert.Contains(t, res.Stderr(), "You must specify at least one rules file")
})
}

func TestFalco_Miscs_HotReload(t *testing.T) {
cwd, err := os.Getwd()
assert.NoError(t, err)
path := filepath.Join(cwd, "hot_reload_enabled.yaml")
_ = os.WriteFile(path, []byte(`watch_config_files: true`), 0700)
hotReloadCfg := run.NewLocalFileAccessor(path, path)

t.Cleanup(func() {
_ = os.Remove(path)
})

go func() {
time.Sleep(2 * time.Second)
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0700)
_, _ = f.WriteString(" \n\n")
_ = f.Close()
}()

falcoRes := falco.Test(
tests.NewFalcoExecutableRunner(t),
falco.WithConfig(hotReloadCfg),
falco.WithRules(rules.SingleRule),
falco.WithStopAfter(5*time.Second),
falco.WithArgs("-o", "engine.kind=modern_ebpf"),
)
assert.NoError(t, falcoRes.Err(), "%s", falcoRes.Stderr())
assert.Equal(t, 0, falcoRes.ExitCode())

fmt.Println(falcoRes.Stderr())
fmt.Println(falcoRes.Stdout())
// We want to be sure that the hot reload was triggered
assert.Regexp(t, `SIGHUP received, restarting...`, falcoRes.Stderr())
}

0 comments on commit dca902f

Please sign in to comment.