|
| 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 | +} |
0 commit comments