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

Test For command falco -i (ignore default events) #8

Merged
merged 8 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 37 additions & 0 deletions tests/falco/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package testfalco

import (
"encoding/json"
"os"
"regexp"
"testing"

Expand Down Expand Up @@ -132,3 +134,38 @@ func TestFalco_Cmd_PluginInfo(t *testing.T) {
`No suggested open params available.*`),
res.Stdout())
}

func TestFalco_Print_IgnoredEvents(t *testing.T) {
t.Parallel()
checkDefaultConfig(t)
type Data struct {
Events []string `json:"events"`
}
evntfile, err := os.Open("events.json")
if err != nil {
panic(err)
}
defer evntfile.Close()

var events Data
err = json.NewDecoder(evntfile).Decode(&events)
if err != nil {
panic(err)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at the comment below: I think this should be part of the test data package under tests/data/outputs, so that it could be accessible by other tests as well and reused.


runner := tests.NewFalcoExecutableRunner(t)
t.Run("all-events-ignored-by-default", func(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we don't have other tests here, I would avoid having a sub-test with t.Run and would just proceed with the test code in the main TestFalco_Print_IgnoredEvents test body, what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test the behaviour and expected output of the Falco CLI depending on its options, considered both individually and in combinations:

Yes, since there no combinations for -i we can proceed with the main TestFalco_Print_IgnoredEvents

res := falco.Test(
runner,
falco.WithArgs("-i"),
)
assert.Regexp(t, regexp.MustCompile(
`Ignored\sEvent\(s\):\n`,
), res.Stdout())
for _, event := range events.Events {
assert.Regexp(t, regexp.MustCompile(`\b`+event+`\b`), res.Stdout())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe assert.Contains would be more lightweight here, since events are simple known strings. Remember, the more we make tests optimized, the less the CI will take to run them!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

}
assert.NoError(t, res.Err(), "%s", res.Stderr())
assert.Equal(t, res.ExitCode(), 0)
})
}
278 changes: 278 additions & 0 deletions tests/falco/events.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense! What about putting this under a tests/data/outputs directory, and masking this through run.NewStringFileAccessor? That would allow the framework to carry around the test file in a runner-agnostic way (it will work whether Falco runs in a container or in the local machine). Accordingly, I would also add the deserialization code in the same package (the part of code responsible of converting the JSON in a Go struct). Take a look at how some of the existing test data is handled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will do

"events": [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, which Falco version did you use to generate this list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Falco version: 0.34.1
Libs version:  0.10.4
Plugin API:    2.0.0
Engine:        16
Driver:
  API version:    3.0.0
  Schema version: 2.0.0
  Default driver: 4.0.0+driver

Falco version: 0.34.1

"getdents64",
"getdents",
"getresgid",
"getresuid",
"getegid",
"geteuid",
"munmap",
"sendmmsg",
"splice",
"getpeername",
"write",
"stat",
"getsockname",
"access",
"nanosleep",
"setsockopt",
"read",
"futex",
"semget",
"getgid",
"page_fault",
"brk",
"readahead",
"set_mempolicy_home_node",
"io_pgetevents",
"setreuid",
"rt_sigtimedwait",
"process_madvise",
"vmsplice",
"lstat",
"preadv2",
"io_uring_register",
"rt_sigreturn",
"sched_get_priority_min",
"pread",
"sched_setscheduler",
"sched_getparam",
"init_module",
"sched_setparam",
"rt_sigpending",
"rt_sigsuspend",
"getpgid",
"delete_module",
"fremovexattr",
"msgsnd",
"uname",
"syslog",
"ioperm",
"fstatfs",
"getpgrp",
"sched_getscheduler",
"setpriority",
"open_tree",
"kcmp",
"getuid",
"prctl",
"llistxattr",
"waitid",
"arch_prctl",
"getpriority",
"send",
"ftruncate",
"truncate",
"mremap",
"reboot",
"settimeofday",
"pidfd_get_fd",
"readlink",
"gettimeofday",
"clock_gettime",
"getrusage",
"sethostname",
"timer_settime",
"mmap2",
"sysinfo",
"setdomainname",
"io_setup",
"inotify_add_watch",
"recvmmsg",
"pidfd_send_signal",
"getxattr",
"ppoll",
"move_mount",
"getpid",
"utime",
"mknod",
"semop",
"getppid",
"exit",
"recv",
"munlockall",
"sched_get_priority_max",
"sched_yield",
"mmap",
"restart_syscall",
"fchownat",
"getrlimit",
"fstat64",
"pkey_free",
"copy_file_range",
"fdatasync",
"clock_settime",
"get_mempolicy",
"pwritev",
"iopl",
"getsid",
"msgrcv",
"semctl",
"finit_module",
"writev",
"statx",
"lchown",
"set_mempolicy",
"listxattr",
"shmdt",
"sched_rr_get_interval",
"fsync",
"lgetxattr",
"getitimer",
"timerfd_settime",
"memfd_secret",
"mknodat",
"mlock2",
"pause",
"llseek",
"syncfs",
"uselib",
"keyctl",
"swapoff",
"umask",
"readv",
"add_key",
"select",
"getcwd",
"alarm",
"rt_sigprocmask",
"name_to_handle_at",
"lseek",
"poll",
"rt_tgsigqueueinfo",
"_sysctl",
"vhangup",
"timerfd_gettime",
"io_uring_enter",
"mlock",
"lstat64",
"futex_waitv",
"move_pages",
"clock_getres",
"munlock",
"query_module",
"adjtimex",
"swapon",
"mq_unlink",
"pidfd_open",
"capget",
"set_thread_area",
"faccessat",
"newfstatat",
"timer_delete",
"fspick",
"io_getevents",
"sysfs",
"get_kernel_syms",
"epoll_pwait",
"acct",
"preadv",
"epoll_wait",
"times",
"ioprio_set",
"pwrite",
"setxattr",
"mq_timedsend",
"personality",
"rt_sigaction",
"wait4",
"fstat",
"setgroups",
"flistxattr",
"sync",
"pluginevent",
"clock_adjtime",
"mprotect",
"timer_getoverrun",
"statfs",
"sched_getaffinity",
"utimensat",
"mq_timedreceive",
"switch",
"landlock_add_rule",
"setregid",
"inotify_rm_watch",
"tee",
"epoll_wait_old",
"ustat",
"fchown",
"remap_file_pages",
"chown",
"setfsuid",
"setfsgid",
"pivot_root",
"mincore",
"madvise",
"timer_create",
"gettid",
"lsetxattr",
"shmat",
"fsetxattr",
"quotactl_fd",
"lookup_dcookie",
"fgetxattr",
"lremovexattr",
"landlock_create_ruleset",
"sched_setaffinity",
"setitimer",
"io_submit",
"fsconfig",
"request_key",
"fanotify_init",
"fsopen",
"perf_event_open",
"epoll_pwait2",
"sched_setattr",
"get_thread_area",
"io_destroy",
"exit_group",
"fsmount",
"epoll_ctl",
"signaldeliver",
"set_tid_address",
"get_robust_list",
"timer_gettime",
"clock_nanosleep",
"pkey_mprotect",
"utimes",
"mq_open",
"mq_notify",
"pwritev2",
"mq_getsetattr",
"kexec_load",
"io_cancel",
"ioprio_get",
"futimesat",
"mount_setattr",
"readlinkat",
"stat64",
"shmget",
"pselect6",
"set_robust_list",
"time",
"shmctl",
"getcpu",
"msgctl",
"fallocate",
"process_vm_readv",
"getgroups",
"removexattr",
"process_vm_writev",
"sigaltstack",
"getrandom",
"fadvise64",
"rt_sigqueueinfo",
"memfd_create",
"mlockall",
"kexec_file_load",
"pkey_alloc",
"msgget",
"landlock_restrict_self",
"msync",
"modify_ldt",
"migrate_pages",
"sendfile",
"mbind",
"epoll_ctl_old"
]
}