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

Pr/jrfastab/config map testing #3355

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion cmd/tetragon/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
}
)

func readConfigSettings(defaultConfDir string, defaultConfDropIn string, dropInsDir []string) {
func ReadConfigSettings(defaultConfDir string, defaultConfDropIn string, dropInsDir []string) {
viper.SetEnvPrefix("tetragon")
replacer := strings.NewReplacer("-", "_")
viper.SetEnvKeyReplacer(replacer)
Expand Down
2 changes: 1 addition & 1 deletion cmd/tetragon/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ func runTestCases(t *testing.T) {
packageConfDropIns = append(packageConfDropIns, filepath.Join(testDir, c))
}
log.Infof("Test %s index %d dumping settings before: %+v", c.description, globalTestIndex, viper.AllSettings())
readConfigSettings(defaultConfYamlFile, defaultConfDropIn, packageConfDropIns)
ReadConfigSettings(defaultConfYamlFile, defaultConfDropIn, packageConfDropIns)
log.Infof("Test %s index %d expected settings: %+v", c.description, globalTestIndex, c.expectedOptions)
log.Infof("Test %s index %d dumping settings after: %+v", c.description, globalTestIndex, viper.AllSettings())

Expand Down
2 changes: 1 addition & 1 deletion cmd/tetragon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ func execute() error {
}

cobra.OnInitialize(func() {
readConfigSettings(adminTgConfDir, adminTgConfDropIn, packageTgConfDropIns)
ReadConfigSettings(adminTgConfDir, adminTgConfDropIn, packageTgConfDropIns)
})

flags := rootCmd.PersistentFlags()
Expand Down
16 changes: 16 additions & 0 deletions pkg/observer/observertesthelper/observer_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,22 @@ func WaitForProcess(process string) error {
return fmt.Errorf("process '%s' did not start", process)
}

func WriteConfigMap(dir, key, value string) error {
fileName := filepath.Join(dir, key)
if err := os.MkdirAll(filepath.Dir(fileName), 0777); err != nil {
return err
}

out, err := os.Create(fileName)
if err != nil {
return err
}
if _, err := out.Write([]byte(value)); err != nil {
return err
}
return out.Sync()
}

func WriteConfigFile(fileName, config string) error {
out, err := os.Create(fileName)
if err != nil {
Expand Down
48 changes: 48 additions & 0 deletions pkg/sensors/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
"github.com/cilium/tetragon/pkg/testutils/perfring"
tus "github.com/cilium/tetragon/pkg/testutils/sensors"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -1632,3 +1634,49 @@
func TestThrottle2(t *testing.T) {
testThrottle(t)
}

func TestConfigDir(t *testing.T) {
var flags pflag.FlagSet
configDir := "/tmp/tetragon.configMap"
observertesthelper.WriteConfigMap(configDir, "enable-process-cred", "false")
observertesthelper.WriteConfigMap(configDir, "enable-process-ns", "false")

var doneWG, readyWG sync.WaitGroup
defer doneWG.Wait()

ctx, cancel := context.WithTimeout(context.Background(), tus.Conf().CmdWaitTime)
defer cancel()

obs, err := observertesthelper.GetDefaultObserver(t, ctx, tus.Conf().TetragonLib, observertesthelper.WithConfigMap(configDir))
if err != nil {

Check failure on line 1651 in pkg/sensors/exec/exec_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: observertesthelper.WithConfigMap

Check failure on line 1651 in pkg/sensors/exec/exec_test.go

View workflow job for this annotation

GitHub Actions / analyze

undefined: observertesthelper.WithConfigMap
t.Fatalf("Failed to run observer: %s", err)
}

option.ReadConfigDir(configDir)
option.AddFlags(&flags)
viper.BindPFlags(&flags)
option.ReadAndSetFlags()

observertesthelper.LoopEvents(ctx, t, &doneWG, &readyWG, obs)
readyWG.Wait()

testNop := testutils.RepoRootPath("contrib/tester-progs/nop")

myCaps := ec.NewCapabilitiesChecker().FromCapabilities(caps.GetCurrentCapabilities())
myNs := ec.NewNamespacesChecker().FromNamespaces(namespace.GetCurrentNamespace())
procChecker := ec.NewProcessChecker().
WithBinary(sm.Full(testNop)).
WithArguments(sm.Full("arg1 arg2 arg3")).
WithCap(myCaps).
WithNs(myNs)

execChecker := ec.NewProcessExecChecker("").WithProcess(procChecker)
checker := ec.NewUnorderedEventChecker(execChecker)

if err := exec.Command(testNop, "arg1", "arg2", "arg3").Run(); err != nil {
t.Fatalf("Failed to execute test binary: %s\n", err)
}

err = jsonchecker.JsonTestCheck(t, checker)
assert.Error(t, err)
}
Loading