Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new(pkg,tests): added a test for Falco hot reload. #54

Merged
merged 1 commit into from
May 9, 2024
Merged
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
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())
}
Loading