Skip to content

Commit 15aa180

Browse files
FedeDPpoiana
authored andcommitted
new(cmd/driver,pkg/options,internal/config): added driver config command tests.
Signed-off-by: Federico Di Pierro <[email protected]>
1 parent a39b5ac commit 15aa180

File tree

6 files changed

+170
-5
lines changed

6 files changed

+170
-5
lines changed

cmd/driver/config/config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ func (o *driverConfigOptions) RunDriverConfig(ctx context.Context, cmd *cobra.Co
158158

159159
d, err := driverdistro.Discover(info, driverCfg.HostRoot)
160160
if err != nil {
161-
return err
161+
if !errors.Is(err, driverdistro.ErrUnsupported) {
162+
return err
163+
}
164+
o.Printer.Logger.Info("Detected an unsupported target system; falling back at generic logic.")
162165
}
163166
o.Printer.Logger.Debug("Discovered distro", o.Printer.Logger.Args("target", d))
164167

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
"context"
20+
"os"
21+
"path/filepath"
22+
"testing"
23+
24+
. "github.com/onsi/ginkgo/v2"
25+
. "github.com/onsi/gomega"
26+
"github.com/onsi/gomega/gbytes"
27+
"github.com/spf13/cobra"
28+
29+
"github.com/falcosecurity/falcoctl/cmd"
30+
commonoptions "github.com/falcosecurity/falcoctl/pkg/options"
31+
testutils "github.com/falcosecurity/falcoctl/pkg/test"
32+
)
33+
34+
var (
35+
ctx = context.Background()
36+
output = gbytes.NewBuffer()
37+
rootCmd *cobra.Command
38+
opt *commonoptions.Common
39+
configFile string
40+
err error
41+
args []string
42+
)
43+
44+
func TestConfig(t *testing.T) {
45+
RegisterFailHandler(Fail)
46+
RunSpecs(t, "Config Suite")
47+
}
48+
49+
var _ = BeforeSuite(func() {
50+
51+
// Create and configure the common options.
52+
opt = commonoptions.NewOptions()
53+
opt.Initialize(commonoptions.WithWriter(output))
54+
55+
// Create temporary directory used to save the configuration file.
56+
configFile, err = testutils.CreateEmptyFile("falcoctl.yaml")
57+
Expect(err).Should(Succeed())
58+
})
59+
60+
var _ = AfterSuite(func() {
61+
configDir := filepath.Dir(configFile)
62+
Expect(os.RemoveAll(configDir)).Should(Succeed())
63+
})
64+
65+
func executeRoot(args []string) error {
66+
rootCmd.SetArgs(args)
67+
rootCmd.SetOut(output)
68+
return cmd.Execute(rootCmd, opt)
69+
}

cmd/driver/config/config_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
})

cmd/driver/printenv/printenv.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package driverprintenv
1717

1818
import (
19+
"errors"
1920
"strings"
2021

2122
"github.com/spf13/cobra"
@@ -67,7 +68,9 @@ func (o *driverPrintenvOptions) RunDriverPrintenv(_ context.Context) error {
6768

6869
d, err := driverdistro.Discover(kr, driver.HostRoot)
6970
if err != nil {
70-
return err
71+
if !errors.Is(err, driverdistro.ErrUnsupported) {
72+
return err
73+
}
7174
}
7275
o.Printer.DefaultText.Printf("TARGET_ID=%q\n", d.String())
7376

internal/config/config.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,11 @@ func Load(path string) error {
231231
// Set default registry auth config path
232232
viper.SetDefault(RegistryCredentialConfigKey, DefaultRegistryCredentialConfPath)
233233
// Set default driver
234-
viper.SetDefault(DriverKey, DefaultDriver)
234+
viper.SetDefault(DriverTypeKey, DefaultDriver.Type)
235+
viper.SetDefault(DriverHostRootKey, DefaultDriver.HostRoot)
236+
viper.SetDefault(DriverNameKey, DefaultDriver.Name)
237+
viper.SetDefault(DriverReposKey, DefaultDriver.Repos)
238+
viper.SetDefault(DriverVersionKey, DefaultDriver.Version)
235239

236240
err = viper.ReadInConfig()
237241
if errors.As(err, &viper.ConfigFileNotFoundError{}) || os.IsNotExist(err) {
@@ -642,7 +646,7 @@ func UpdateConfigFile(key string, value interface{}, path string) error {
642646
v.Set(key, value)
643647

644648
if err := v.WriteConfig(); err != nil {
645-
return fmt.Errorf("unable to set key %q to config file: %w", IndexesKey, err)
649+
return fmt.Errorf("unable to set key %q to config file: %w", key, err)
646650
}
647651

648652
return nil

pkg/options/driver.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package options
1717

1818
import (
19+
"sort"
20+
1921
drivertype "github.com/falcosecurity/falcoctl/pkg/driver/type"
2022
)
2123

@@ -26,7 +28,9 @@ type DriverTypes struct {
2628

2729
// NewDriverTypes returns a new Enum configured for the driver types.
2830
func NewDriverTypes() *DriverTypes {
31+
types := drivertype.GetTypes()
32+
sort.Strings(types)
2933
return &DriverTypes{
30-
Enum: NewEnum(drivertype.GetTypes(), drivertype.TypeKmod),
34+
Enum: NewEnum(types, drivertype.TypeKmod),
3135
}
3236
}

0 commit comments

Comments
 (0)