|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright (C) 2023 The Falco Authors |
| 3 | +// |
| 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 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package driverconfig_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "regexp" |
| 20 | + |
| 21 | + . "github.com/onsi/ginkgo/v2" |
| 22 | + . "github.com/onsi/gomega" |
| 23 | + "github.com/onsi/gomega/gbytes" |
| 24 | + |
| 25 | + "github.com/falcosecurity/falcoctl/cmd" |
| 26 | +) |
| 27 | + |
| 28 | +var driverConfigHelp = `Configure a driver for future usages with other driver subcommands.` |
| 29 | + |
| 30 | +var addAssertFailedBehavior = func(specificError string) { |
| 31 | + It("check that fails and the usage is not printed", func() { |
| 32 | + Expect(err).To(HaveOccurred()) |
| 33 | + Expect(output).Should(gbytes.Say(regexp.QuoteMeta(specificError))) |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +var _ = Describe("config", func() { |
| 38 | + |
| 39 | + var ( |
| 40 | + driverCmd = "driver" |
| 41 | + configCmd = "config" |
| 42 | + ) |
| 43 | + |
| 44 | + // Each test gets its own root command and runs it. |
| 45 | + // The err variable is asserted by each test. |
| 46 | + JustBeforeEach(func() { |
| 47 | + rootCmd = cmd.New(ctx, opt) |
| 48 | + err = executeRoot(args) |
| 49 | + }) |
| 50 | + |
| 51 | + JustAfterEach(func() { |
| 52 | + Expect(output.Clear()).ShouldNot(HaveOccurred()) |
| 53 | + }) |
| 54 | + |
| 55 | + Context("help message", func() { |
| 56 | + BeforeEach(func() { |
| 57 | + args = []string{driverCmd, configCmd, "--help"} |
| 58 | + }) |
| 59 | + |
| 60 | + It("should match the saved one", func() { |
| 61 | + Expect(output).Should(gbytes.Say(driverConfigHelp)) |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + // Here we are testing failure cases for configuring a driver. |
| 66 | + Context("failure", func() { |
| 67 | + When("with non absolute host-root", func() { |
| 68 | + BeforeEach(func() { |
| 69 | + args = []string{driverCmd, configCmd, "--config", configFile, "--host-root", "foo/"} |
| 70 | + }) |
| 71 | + addAssertFailedBehavior("ERROR host-root must be an absolute path: foo/") |
| 72 | + }) |
| 73 | + |
| 74 | + When("with invalid driver type", func() { |
| 75 | + BeforeEach(func() { |
| 76 | + args = []string{driverCmd, configCmd, "--config", configFile, "--type", "foo"} |
| 77 | + }) |
| 78 | + addAssertFailedBehavior(`ERROR invalid argument "foo" for "--type" flag: invalid argument "foo",` + |
| 79 | + ` please provide one of (auto, ebpf, kmod, modern_ebpf)`) |
| 80 | + }) |
| 81 | + }) |
| 82 | +}) |
0 commit comments