-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new(cmd/driver,pkg/options,internal/config): added
driver config
co…
…mmand tests. Signed-off-by: Federico Di Pierro <[email protected]>
- Loading branch information
Showing
6 changed files
with
175 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// 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 driverconfig_test | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/onsi/gomega/gbytes" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/falcosecurity/falcoctl/cmd" | ||
commonoptions "github.com/falcosecurity/falcoctl/pkg/options" | ||
testutils "github.com/falcosecurity/falcoctl/pkg/test" | ||
) | ||
|
||
//nolint:unused // false positive | ||
var ( | ||
ctx = context.Background() | ||
output = gbytes.NewBuffer() | ||
rootCmd *cobra.Command | ||
opt *commonoptions.Common | ||
configFile string | ||
err error | ||
args []string | ||
) | ||
|
||
func TestConfig(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Config Suite") | ||
} | ||
|
||
var _ = BeforeSuite(func() { | ||
|
||
// Create and configure the common options. | ||
opt = commonoptions.NewOptions() | ||
opt.Initialize(commonoptions.WithWriter(output)) | ||
|
||
// Create temporary directory used to save the configuration file. | ||
configFile, err = testutils.CreateEmptyFile("falcoctl.yaml") | ||
Expect(err).Should(Succeed()) | ||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
configDir := filepath.Dir(configFile) | ||
Expect(os.RemoveAll(configDir)).Should(Succeed()) | ||
}) | ||
|
||
//nolint:unused // false positive | ||
func executeRoot(args []string) error { | ||
rootCmd.SetArgs(args) | ||
rootCmd.SetOut(output) | ||
return cmd.Execute(rootCmd, opt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// 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 driverconfig_test | ||
|
||
import ( | ||
"regexp" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/onsi/gomega/gbytes" | ||
|
||
"github.com/falcosecurity/falcoctl/cmd" | ||
) | ||
|
||
//nolint:unused // false positive | ||
var driverConfigHelp = `Configure a driver for future usages with other driver subcommands.` | ||
|
||
//nolint:unused // false positive | ||
var addAssertFailedBehavior = func(specificError string) { | ||
It("check that fails and the usage is not printed", func() { | ||
Expect(err).To(HaveOccurred()) | ||
Expect(output).Should(gbytes.Say(regexp.QuoteMeta(specificError))) | ||
}) | ||
} | ||
|
||
//nolint:unused // false positive | ||
var driverConfigTests = Describe("config", func() { | ||
|
||
var ( | ||
driverCmd = "driver" | ||
configCmd = "config" | ||
) | ||
|
||
// Each test gets its own root command and runs it. | ||
// The err variable is asserted by each test. | ||
JustBeforeEach(func() { | ||
rootCmd = cmd.New(ctx, opt) | ||
err = executeRoot(args) | ||
}) | ||
|
||
JustAfterEach(func() { | ||
Expect(output.Clear()).ShouldNot(HaveOccurred()) | ||
}) | ||
|
||
Context("help message", func() { | ||
BeforeEach(func() { | ||
args = []string{driverCmd, configCmd, "--help"} | ||
}) | ||
|
||
It("should match the saved one", func() { | ||
Expect(output).Should(gbytes.Say(driverConfigHelp)) | ||
}) | ||
}) | ||
|
||
// Here we are testing failure cases for configuring a driver. | ||
Context("failure", func() { | ||
When("with non absolute host-root", func() { | ||
BeforeEach(func() { | ||
args = []string{driverCmd, configCmd, "--config", configFile, "--host-root", "foo/"} | ||
}) | ||
addAssertFailedBehavior("ERROR host-root must be an absolute path: foo/") | ||
}) | ||
|
||
When("with invalid driver type", func() { | ||
BeforeEach(func() { | ||
args = []string{driverCmd, configCmd, "--config", configFile, "--type", "foo"} | ||
}) | ||
addAssertFailedBehavior(`ERROR invalid argument "foo" for "--type" flag: invalid argument "foo",` + | ||
` please provide one of (auto, ebpf, kmod, modern_ebpf)`) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters