diff --git a/pkg/falcodriverloader/tester.go b/pkg/falcodriverloader/tester.go deleted file mode 100644 index 35383cb..0000000 --- a/pkg/falcodriverloader/tester.go +++ /dev/null @@ -1,91 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -/* -Copyright (C) 2023 The Falco Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. - -*/ - -package falcodriverloader - -import ( - "bytes" - "context" - "time" - - "github.com/falcosecurity/testing/pkg/run" - "github.com/sirupsen/logrus" -) - -const ( - // DefaultMaxDuration is the default max duration of a falco-driver-loader run - DefaultMaxDuration = time.Second * 30 - // - // DefaultExecutable is the default path of the falco-driver-loader executable - // when installed from a Falco package - DefaultExecutable = "/usr/bin/falco-driver-loader" -) - -type testOptions struct { - workdir string - err error - args []string - duration time.Duration - files []run.FileAccessor -} - -// TestOutput is the output of a falco-driver-loader test run -type TestOutput struct { - opts *testOptions - err error - stdout bytes.Buffer - stderr bytes.Buffer -} - -// TestOption is an option for testing falco-driver-loader -type TestOption func(*testOptions) - -// Test runs a FalcoDriverLoader runner with the given test options, and produces -// an output representing the outcome of the run. -func Test(runner run.Runner, options ...TestOption) *TestOutput { - res := &TestOutput{ - opts: &testOptions{ - workdir: runner.WorkDir(), - duration: DefaultMaxDuration, - }, - } - for _, o := range options { - o(res.opts) - } - if res.opts.err != nil { - return res - } - - logrus.WithField("deadline", res.opts.duration).Info("running falco-driver-loader with runner") - ctx, cancel := context.WithTimeout(context.Background(), skewedDuration(res.opts.duration)) - defer cancel() - res.err = runner.Run(ctx, - run.WithArgs(res.opts.args...), - run.WithFiles(res.opts.files...), - run.WithStdout(&res.stdout), - run.WithStderr(&res.stderr), - ) - if res.err != nil { - logrus.WithError(res.err).Warn("error running falco-driver-loader with runner") - } - return res -} - -func skewedDuration(d time.Duration) time.Duration { - return time.Duration(float64(d) * 1.10) -} diff --git a/pkg/falcodriverloader/tester_options.go b/pkg/falcodriverloader/tester_options.go deleted file mode 100644 index cd31a74..0000000 --- a/pkg/falcodriverloader/tester_options.go +++ /dev/null @@ -1,24 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -/* -Copyright (C) 2023 The Falco Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. - -*/ - -package falcodriverloader - -// WithArgs runs falco-driver-loader with the given arguments. -func WithArgs(args ...string) TestOption { - return func(ro *testOptions) { ro.args = append(ro.args, args...) } -} diff --git a/pkg/falcodriverloader/tester_output.go b/pkg/falcodriverloader/tester_output.go deleted file mode 100644 index 064ebd5..0000000 --- a/pkg/falcodriverloader/tester_output.go +++ /dev/null @@ -1,62 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -/* -Copyright (C) 2023 The Falco Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. - -*/ - -package falcodriverloader - -import ( - "context" - - "github.com/falcosecurity/testing/pkg/run" - "go.uber.org/multierr" -) - -// Err returns a non-nil error in case of issues when running falco-driver-loader. -func (t *TestOutput) Err() error { - return multierr.Append(t.opts.err, t.err) -} - -// DurationExceeded returns true if the falco-driver-loader run exceeded the expected -// duration or if the context had expired. -func (t *TestOutput) DurationExceeded() bool { - for _, err := range multierr.Errors(t.Err()) { - if err == context.DeadlineExceeded { - return true - } - } - return false -} - -// ExitCode returns the numeric exit code of the falco-driver-loader process. -func (t *TestOutput) ExitCode() int { - for _, err := range multierr.Errors(t.Err()) { - if exitCodeErr, ok := err.(*run.ExitCodeError); ok { - return exitCodeErr.Code - } - } - return 0 -} - -// Stdout returns a string containing the stdout output of the falco-driver-loader run. -func (t *TestOutput) Stdout() string { - return t.stdout.String() -} - -// Stderr returns a string containing the stderr output of the falco-driver-loader run. -func (t *TestOutput) Stderr() string { - return t.stderr.String() -} diff --git a/tests/falcodriverloader/drivers_test.go b/tests/falcodriverloader/drivers_test.go index 8464ff2..bfa0bc8 100644 --- a/tests/falcodriverloader/drivers_test.go +++ b/tests/falcodriverloader/drivers_test.go @@ -19,11 +19,11 @@ limitations under the License. package testfalcodriverloader import ( + "github.com/falcosecurity/testing/pkg/falcoctl" "testing" "time" "github.com/falcosecurity/testing/pkg/falco" - "github.com/falcosecurity/testing/pkg/falcodriverloader" "github.com/falcosecurity/testing/tests" "github.com/stretchr/testify/assert" ) @@ -40,20 +40,29 @@ import ( // // We need to use the `--compile` flag because we test against dev versions func TestFalcoLegacyBPF(t *testing.T) { - loaderRes := falcodriverloader.Test( - tests.NewFalcoDriverLoaderExecutableRunner(t), - falcodriverloader.WithArgs("bpf", "--compile"), + // First, configure falcoctl driver + configRes := falcoctl.Test( + tests.NewFalcoctlExecutableRunner(t), + falcoctl.WithArgs("driver", "config", "--type", "ebpf"), + ) + assert.NoError(t, configRes.Err(), "%s", configRes.Stderr()) + assert.Equal(t, 0, configRes.ExitCode()) + + loaderRes := falcoctl.Test( + tests.NewFalcoctlExecutableRunner(t), + falcoctl.WithArgs("driver", "install", "--download=false"), ) assert.NoError(t, loaderRes.Err(), "%s", loaderRes.Stderr()) assert.Equal(t, 0, loaderRes.ExitCode()) - // We expect the probe to be symlinked in '/root/.falco/falco-bpf.o' - assert.Regexp(t, `Success: eBPF probe symlinked`, loaderRes.Stdout()) + // We expect the probe to be succesfully built and copied to /root/.falco/falco-bpf.o + assert.Regexp(t, `Probe successfully built.`, loaderRes.Stdout()) // Now running Falco with `FALCO_BPF_PROBE=/root/.falco/falco-bpf.o` we should be able to run the bpf driver falcoRes := falco.Test( tests.NewFalcoExecutableRunner(t), falco.WithStopAfter(3*time.Second), - falco.WithEnvVars(map[string]string{"FALCO_BPF_PROBE": "/root/.falco/falco-bpf.o"}), + falco.WithArgs("-o", "engine.kind=ebpf"), + falco.WithArgs("-o", "engine.ebpf.probe=/root/.falco/falco-bpf.o"), ) assert.NoError(t, falcoRes.Err(), "%s", falcoRes.Stderr()) assert.Equal(t, 0, falcoRes.ExitCode()) @@ -75,19 +84,28 @@ func TestFalcoLegacyBPF(t *testing.T) { // // We need to use the `--compile` flag because we test against dev versions func TestFalcoKmod(t *testing.T) { - loaderRes := falcodriverloader.Test( - tests.NewFalcoDriverLoaderExecutableRunner(t), - falcodriverloader.WithArgs("module", "--compile"), + // First, configure falcoctl driver + configRes := falcoctl.Test( + tests.NewFalcoctlExecutableRunner(t), + falcoctl.WithArgs("driver", "config", "--type", "kmod"), + ) + assert.NoError(t, configRes.Err(), "%s", configRes.Stderr()) + assert.Equal(t, 0, configRes.ExitCode()) + + loaderRes := falcoctl.Test( + tests.NewFalcoctlExecutableRunner(t), + falcoctl.WithArgs("driver", "install", "--download=false"), ) assert.NoError(t, loaderRes.Err(), "%s", loaderRes.Stderr()) assert.Equal(t, 0, loaderRes.ExitCode()) // We expect the module to be loaded in dkms - assert.Regexp(t, `Success: falco module found and loaded in dkms`, loaderRes.Stdout()) + assert.Regexp(t, `Module installed in dkms.`, loaderRes.Stdout()) // Now running Falco we should be able to run the kernel module falcoRes := falco.Test( tests.NewFalcoExecutableRunner(t), falco.WithStopAfter(3*time.Second), + falco.WithArgs("-o", "engine.kind=kmod"), ) assert.NoError(t, falcoRes.Err(), "%s", falcoRes.Stderr()) assert.Equal(t, 0, falcoRes.ExitCode()) diff --git a/tests/tests.go b/tests/tests.go index 78be849..28b108a 100644 --- a/tests/tests.go +++ b/tests/tests.go @@ -26,22 +26,21 @@ import ( "github.com/falcosecurity/testing/pkg/falco" "github.com/falcosecurity/testing/pkg/falcoctl" - "github.com/falcosecurity/testing/pkg/falcodriverloader" "github.com/falcosecurity/testing/pkg/run" "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" ) -var falcoStatic = false -var falcoBinary = falco.DefaultExecutable -var falcoctlBinary = falcoctl.DefaultLocalExecutable -var falcoDriverLoaderBinary = falcodriverloader.DefaultExecutable +var ( + falcoStatic = false + falcoBinary = falco.DefaultExecutable + falcoctlBinary = falcoctl.DefaultLocalExecutable +) func init() { flag.BoolVar(&falcoStatic, "falco-static", falcoStatic, "True if the Falco executable is from a static build") flag.StringVar(&falcoBinary, "falco-binary", falcoBinary, "Falco executable binary path") flag.StringVar(&falcoctlBinary, "falcoctl-binary", falcoctlBinary, "falcoctl executable binary path") - flag.StringVar(&falcoDriverLoaderBinary, "falco-driver-loader-binary", falcoDriverLoaderBinary, "falco-driver-loader executable binary path") logrus.SetLevel(logrus.DebugLevel) logrus.SetFormatter(&logrus.JSONFormatter{}) @@ -54,13 +53,6 @@ func NewFalcoExecutableRunner(t *testing.T) run.Runner { return runner } -// NewFalcoExecutableRunner returns an executable runner for falco-driver-loader. -func NewFalcoDriverLoaderExecutableRunner(t *testing.T) run.Runner { - runner, err := run.NewExecutableRunner(falcoDriverLoaderBinary) - require.Nil(t, err) - return runner -} - // NewFalcoctlExecutableRunner returns an executable runner for falcoctl. func NewFalcoctlExecutableRunner(t *testing.T) run.Runner { if _, err := os.Stat(falcoctlBinary); err == nil {