Skip to content

Commit e5dc911

Browse files
committed
Added declarative subcommadn for test command
Signed-off-by: GLVS Kiriti <glvskiriti2003369@gmail.com>
1 parent 5a356c1 commit e5dc911

File tree

3 files changed

+98
-16
lines changed

3 files changed

+98
-16
lines changed

cmd/declarativetest.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
/*
3+
Copyright (C) 2024 The Falco Authors.
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
15+
package cmd
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"time"
21+
22+
"github.com/falcosecurity/event-generator/pkg/tester"
23+
logger "github.com/sirupsen/logrus"
24+
"github.com/spf13/cobra"
25+
)
26+
27+
// NewDeclarative instantiates the declarative subcommand for test command.
28+
func NewDeclarativeTest() *cobra.Command {
29+
c := &cobra.Command{
30+
Use: "declarative [yaml-file-path]",
31+
Short: "Execute and test Falco rules using a declarative approach",
32+
Long: `This command takes the path to a YAML file as an argument.
33+
The YAML file defines tests that are parsed and executed,
34+
and checks if specific Falco rules are triggered.`,
35+
Args: cobra.MaximumNArgs(1),
36+
DisableAutoGenTag: true,
37+
}
38+
39+
var testTimeout time.Duration
40+
flags := c.Flags()
41+
flags.DurationVar(&testTimeout, "test-timeout", tester.DefaultTestTimeout, "Test duration timeout")
42+
43+
grpcCfg := grpcFlags(flags)
44+
45+
c.RunE = func(c *cobra.Command, args []string) error {
46+
t, err := tester.New(grpcCfg, tester.WithTestTimeout(testTimeout))
47+
if err != nil {
48+
return err
49+
}
50+
51+
tests, err := parseYamlFile(args[0])
52+
if err != nil {
53+
return err
54+
}
55+
56+
var failedTests []error // stores the errors of failed tests
57+
58+
// Execute each test in the YAML file
59+
for _, eachTest := range tests.Tests {
60+
// Execute the test steps
61+
err := runTestSteps(eachTest)
62+
if err != nil {
63+
failedTests = append(failedTests, fmt.Errorf("test %v failed with err: %v", eachTest.Rule, err))
64+
continue
65+
}
66+
67+
// Prepare the logger
68+
log := logger.WithField("test", eachTest.Rule)
69+
70+
// Test if the Falco rule is triggered
71+
err = t.PostRun(context.Background(), log, "declarative."+eachTest.Rule, nil, nil)
72+
if err != nil {
73+
failedTests = append(failedTests, fmt.Errorf("falco rule %v did not trigger as expected: %v", eachTest.Rule, err))
74+
}
75+
}
76+
77+
// Print all errors
78+
if len(failedTests) > 0 {
79+
for _, ft := range failedTests {
80+
fmt.Println(ft)
81+
}
82+
return fmt.Errorf("some tests failed, see previous logs")
83+
}
84+
85+
return nil
86+
}
87+
88+
return c
89+
}

cmd/test.go

+2
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,7 @@ Without arguments it tests all actions, otherwise only those actions matching th
5454
return runEWithOpts(c, args, runner.WithPlugin(t))
5555
}
5656

57+
c.AddCommand(NewDeclarativeTest())
58+
5759
return c
5860
}

events/exampleyamlfile.yml

+7-16
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,6 @@ tests:
2121
mode: 0644
2222
after: ""
2323

24-
- rule: ReadSensitiveFileTrustedAfterStartup
25-
runner: HostRunner
26-
before: ""
27-
steps:
28-
- syscall: "open"
29-
args:
30-
filepath: "/etc/shadow"
31-
flag: 0
32-
mode: 0644
33-
after: ""
34-
3524
- rule: ClearLogActivities
3625
runner: HostRunner
3726
before: "mkdir /tmp/created-by-event-generator && touch /tmp/created-by-event-generator/syslog"
@@ -63,8 +52,10 @@ tests:
6352
newpath: "/created-by-event-generator/newpath"
6453
after: "rm /created-by-event-generator/newpath && rmdir /created-by-event-generator"
6554

66-
- rule: LaunchIngressRemoteFileCopyToolsInsideContainer
67-
runner: ContainerRunner
68-
before: "wget example.com"
69-
steps:
70-
after: ""
55+
56+
# not a stable rule
57+
# - rule: LaunchIngressRemoteFileCopyToolsInsideContainer
58+
# runner: ContainerRunner
59+
# before: "wget example.com"
60+
# steps:
61+
# after: ""

0 commit comments

Comments
 (0)