Skip to content

Commit

Permalink
new(tests,pkg,action): added 2 new tests around prometheus metrics.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
  • Loading branch information
FedeDP authored and poiana committed Jun 4, 2024
1 parent f03d938 commit e67e329
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,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 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)
}

0 comments on commit e67e329

Please sign in to comment.