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(tests,pkg,action): added 2 new tests around prometheus metrics. #59

Merged
merged 2 commits into from
Jun 4, 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
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ jobs:
test-falco: 'true'
test-falcoctl: 'true'
test-k8saudit: 'true'
test-dummy: 'true'
show-all: 'true'
sudo: ''
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ inputs:
description: 'Whether to run k8saudit tests. Default disabled.'
required: false
default: 'false'
test-dummy:
description: 'Whether to run dummy plugin tests. Default disabled.'
required: false
default: 'false'
test-drivers:
description: 'Whether to run drivers tests. Requires kernel headers to be installed. Default disabled.'
required: false
Expand Down Expand Up @@ -68,6 +72,7 @@ runs:
${{ inputs.sudo }} mkdir -p /usr/share/falco/plugins
${{ inputs.sudo }} falcoctl artifact install k8saudit-rules
${{ inputs.sudo }} falcoctl artifact install cloudtrail-rules
${{ inputs.sudo }} falcoctl artifact install dummy

- name: Install dependencies for falco-driver-loader tests
if: ${{ inputs.test-drivers == 'true' }}
Expand All @@ -93,6 +98,9 @@ runs:
if ${{ inputs.test-k8saudit == 'true' }}; then
./build/k8saudit.test -test.timeout=180s -test.v >> ./report.txt 2>&1 || true
fi
if ${{ inputs.test-dummy == 'true' }}; then
./build/dummy.test -test.timeout=180s -test.v >> ./report.txt 2>&1 || true
fi
if ${{ inputs.test-drivers == 'true' }}; then
${{ inputs.sudo }} ./build/falco-driver-loader.test -test.timeout=180s -test.v >> ./report.txt 2>&1 || true
fi
Expand Down
11 changes: 11 additions & 0 deletions pkg/falco/tester_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ func WithDisabledSources(sources ...string) TestOption {
return withMultipleArgValues("--disable-source", sources...)
}

// WithPrometheusMetrics runs Falco enabling prometheus metrics endpoint.
func WithPrometheusMetrics() TestOption {
return func(o *testOptions) {
o.args = append(o.args, "-o", "metrics.enabled=true")
o.args = append(o.args, "-o", "metrics.output_rule=true")
o.args = append(o.args, "-o", "metrics.interval=2s")
o.args = append(o.args, "-o", "webserver.enabled=true")
o.args = append(o.args, "-o", "webserver.prometheus_metrics_enabled=true")
}
}

// WithMinRulePriority runs Falco by forcing a mimimum rules priority.
func WithMinRulePriority(priority string) TestOption {
return func(o *testOptions) {
Expand Down
4 changes: 4 additions & 0 deletions tests/data/plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ var CloudtrailPlugin = run.NewLocalFileAccessor(
var JSONPlugin = run.NewLocalFileAccessor(
"libjson.so",
"/usr/share/falco/plugins/libjson.so")

var DummyPlugin = run.NewLocalFileAccessor(
"libdummy.so",
"/usr/share/falco/plugins/libdummy.so")
62 changes: 62 additions & 0 deletions tests/dummy/dummy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package testdummy

import (
"github.com/falcosecurity/testing/pkg/falco"
"github.com/falcosecurity/testing/pkg/run"
"github.com/falcosecurity/testing/tests"
"github.com/falcosecurity/testing/tests/data/plugins"
"github.com/falcosecurity/testing/tests/data/rules"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"sync"
"testing"
"time"
)

func runFalcoWithDummy(t *testing.T, r run.Runner, opts ...falco.TestOption) *falco.TestOutput {
config, err := falco.NewPluginConfig(
"plugin-config.yaml",
&falco.PluginConfigInfo{
Name: "dummy",
Library: plugins.DummyPlugin.Name(),
OpenParams: `'{"start": 1, "maxEvents": 2000000000}'`,
},
)
require.Nil(t, err)
options := []falco.TestOption{
falco.WithEnabledSources("dummy"),
falco.WithConfig(config),
falco.WithExtraFiles(plugins.DummyPlugin),
}
options = append(options, opts...)
return falco.Test(r, options...)
}

func TestDummy_PrometheusMetrics(t *testing.T) {
var (
wg sync.WaitGroup
metricsErr error
)
wg.Add(1)

go func() {
defer wg.Done()
time.Sleep(2 * time.Second)
_, metricsErr = http.Get("http://127.0.0.1:8765/metrics")
}()

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

wg.Wait()

assert.NoError(t, metricsErr)
}
21 changes: 21 additions & 0 deletions tests/dummy/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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 testdummy

//go:generate go test ./... -c -o ../../build/dummy.test
30 changes: 30 additions & 0 deletions tests/falco/miscs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ package testfalco
import (
"github.com/falcosecurity/testing/pkg/run"
"github.com/falcosecurity/testing/tests/data/rules"
"net/http"
"os"
"path/filepath"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -106,3 +108,31 @@ func TestFalco_Miscs_HotReload(t *testing.T) {
// We want to be sure that the hot reload was triggered
assert.Regexp(t, `SIGHUP received, restarting...`, falcoRes.Stderr())
}

func TestFalco_Miscs_PrometheusMetricsNoDriver(t *testing.T) {
var (
wg sync.WaitGroup
metricsErr error
)
wg.Add(1)

go func() {
defer wg.Done()
time.Sleep(2 * time.Second)
_, metricsErr = http.Get("http://127.0.0.1:8765/metrics")
}()

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

wg.Wait()

assert.NoError(t, metricsErr)
}
Loading