Skip to content

Commit

Permalink
chore(cmd,docs): updated docs and fixed cmd related tests.
Browse files Browse the repository at this point in the history
Moreover, moved cmd output to more strictly follow `falcoctl` one,
with regards to printing usage/helper messages.

Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Apr 11, 2024
1 parent 24c5ade commit c1d2c62
Show file tree
Hide file tree
Showing 23 changed files with 124 additions and 71 deletions.
9 changes: 7 additions & 2 deletions cmd/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -131,6 +130,8 @@ var tests = []testCase{
"ubuntu-aws",
"--output-module",
"/tmp/falco-ubuntu-aws.ko",
"--output-probe",
"/tmp/falco-ubuntu-aws.o",
"--loglevel",
"debug",
},
Expand All @@ -143,6 +144,7 @@ var tests = []testCase{
env: map[string]string{
"DRIVERKIT_KERNELVERSION": "59",
"DRIVERKIT_OUTPUT_MODULE": "/tmp/falco-ubuntu-aws.ko",
"DRIVERKIT_OUTPUT_PROBE": "/tmp/falco-ubuntu-aws.o",
},
args: []string{
"docker",
Expand Down Expand Up @@ -321,6 +323,7 @@ func run(t *testing.T, test testCase) {
var buf bytes.Buffer
configOpts.setOutput(&buf, true)
c := NewRootCmd(configOpts, rootOpts)
c.SetOutput(&buf)
if len(test.args) == 0 || (test.args[0] != "__complete" && test.args[0] != "__completeNoDesc" && test.args[0] != "help" && test.args[0] != "completion") {
test.args = append(test.args, "--dryrun")
}
Expand All @@ -338,6 +341,8 @@ func run(t *testing.T, test testCase) {
} else {
assert.Error(t, err, test.expect.err)
}
// Exactly same behavior as rootCmd.Start(), but here we use ERROR instead of FATAL to avoid leaving
configOpts.Printer.Logger.Error("error executing driverkit", configOpts.Printer.Logger.Args("err", err.Error()))
}
out := buf.String()
res := stripansi.Strip(out)
Expand Down Expand Up @@ -365,7 +370,7 @@ type testTemplateData struct {
}

func readTemplateFile(t *testing.T, s string) string {
out, err := ioutil.ReadFile("testdata/templates/" + s)
out, err := os.ReadFile("testdata/templates/" + s)
assert.NilError(t, err)
return string(out)
}
Expand Down
17 changes: 13 additions & 4 deletions cmd/config_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,16 @@ type ConfigOptions struct {
}

func (co *ConfigOptions) initPrinter() {
// DisableStyling is only enforced by tests.
if co.disableStyling {
pterm.DisableColor()
pterm.DisableStyling()
}
co.Printer = output.NewPrinter(co.logLevel.ToPtermLogLevel(), pterm.LogFormatterColorful, co.writer)
if co.disableStyling {
// Disable time print for tests
co.Printer.Logger = co.Printer.Logger.WithTime(false)
}

}

// Called by tests to disable styling and set bytes buffer as output
Expand Down Expand Up @@ -97,7 +103,7 @@ func (co *ConfigOptions) validate() []error {
// AddFlags registers the common flags.
func (co *ConfigOptions) AddFlags(flags *pflag.FlagSet) {
flags.StringVarP(&co.configFile, "config", "c", co.configFile, "config file path (default $HOME/.driverkit.yaml if exists)")
flags.VarP(co.logLevel, "loglevel", "l", "Set level for logs "+co.logLevel.Allowed())
flags.VarP(co.logLevel, "loglevel", "l", "set level for logs "+co.logLevel.Allowed())
flags.IntVar(&co.Timeout, "timeout", co.Timeout, "timeout in seconds")
flags.StringVar(&co.ProxyURL, "proxy", co.ProxyURL, "the proxy to use to download data")
flags.BoolVar(&co.dryRun, "dryrun", co.dryRun, "do not actually perform the action")
Expand Down Expand Up @@ -133,7 +139,11 @@ func (co *ConfigOptions) Init() bool {
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
err := viper.ReadInConfig()
// Init printer with either read or existent one,
// so that we can further log considering log level set.
co.initPrinter()
if err == nil {
co.Printer.Logger.Info("using config file",
co.Printer.Logger.Args("file", viper.ConfigFileUsed()))
} else {
Expand All @@ -143,6 +153,5 @@ func (co *ConfigOptions) Init() bool {
co.Printer.Logger.Debug("running without a configuration file")
}
}
co.initPrinter()
return configErr
}
14 changes: 12 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ limitations under the License.
package cmd

import (
"errors"
"fmt"
"io"
"os"
"sort"
"strings"
Expand All @@ -31,10 +33,11 @@ import (

func persistentValidateFunc(rootCommand *RootCmd, configOpts *ConfigOptions, rootOpts *RootOptions) func(c *cobra.Command, args []string) error {
return func(c *cobra.Command, args []string) error {
var validationError = errors.New("exiting for validation errors")
configErr := configOpts.Init()
// Early exit if detect some error into config flags
if configErr {
return fmt.Errorf("exiting for validation errors")
return validationError
}
// Merge environment variables or config file values into the RootOptions instance
skip := map[string]bool{ // do not merge these
Expand Down Expand Up @@ -89,7 +92,7 @@ func persistentValidateFunc(rootCommand *RootCmd, configOpts *ConfigOptions, roo
configOpts.Printer.Logger.Error("error validating build options",
configOpts.Printer.Logger.Args("err", err.Error()))
}
return fmt.Errorf("exiting for validation errors")
return validationError
}
rootOpts.Log(configOpts.Printer)
}
Expand All @@ -112,6 +115,7 @@ func NewRootCmd(configOpts *ConfigOptions, rootOpts *RootOptions) *RootCmd {
Args: cobra.OnlyValidArgs,
DisableFlagsInUseLine: true,
DisableAutoGenTag: true,
SilenceErrors: true,
SilenceUsage: true,
Version: version.String(),
RunE: func(c *cobra.Command, args []string) error {
Expand Down Expand Up @@ -189,6 +193,12 @@ func (r *RootCmd) SetArgs(args []string) {
r.c.SetArgs(args)
}

// SetOutput sets the main command output writer.
func (r *RootCmd) SetOutput(w io.Writer) {
r.c.SetOut(w)
r.c.SetErr(w)
}

// Execute proxies the cobra.Command execution.
func (r *RootCmd) Execute() error {
return r.c.Execute()
Expand Down
3 changes: 2 additions & 1 deletion cmd/testdata/autohelp.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
level=INFO msg="specify a valid processor" processors="[docker kubernetes kubernetes-in-cluster local]"
INFO specify a valid processor
└ processors: [docker kubernetes kubernetes-in-cluster local]
{{ .Desc }}

{{ .Usage }}
Expand Down
1 change: 1 addition & 0 deletions cmd/testdata/configs/1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ kernelversion: 59
target: ubuntu-aws
output:
module: /tmp/falco-ubuntu-aws.ko
probe: /tmp/falco-ubuntu-aws.o
driverversion: master
1 change: 1 addition & 0 deletions cmd/testdata/configs/2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ kernelurls: [
target: ubuntu-aws
output:
module: /tmp/falco-ubuntu-aws.ko
probe: /tmp/falco-ubuntu-aws.o
driverversion: master
16 changes: 13 additions & 3 deletions cmd/testdata/docker-from-config-debug.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
level=INFO msg="using config file" file=testdata/configs/1.yaml
level=DEBUG msg="running with options" output-module=/tmp/falco-ubuntu-aws.ko output-probe="" driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=59 target=ubuntu-aws arch={{ .CurrentArch }} kernelurls=[] repo-org=falcosecurity repo-name=libs
level=INFO msg="driver building, it will take a few seconds" processor=docker
INFO using config file file: testdata/configs/1.yaml
DEBUG running with options
├ output-module: /tmp/falco-ubuntu-aws.ko
├ output-probe: /tmp/falco-ubuntu-aws.o
├ driverversion: master
├ kernelrelease: 4.15.0-1057-aws
├ kernelversion: 59
├ target: ubuntu-aws
├ arch: amd64
├ kernelurls: []
├ repo-org: falcosecurity
└ repo-name: libs
INFO starting build processor: docker
16 changes: 13 additions & 3 deletions cmd/testdata/docker-override-from-config-debug.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
level=INFO msg="using config file" file=testdata/configs/1.yaml
level=DEBUG msg="running with options" output-module=/tmp/override.ko output-probe="" driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=229 target=ubuntu-aws arch={{ .CurrentArch }} kernelurls=[] repo-org=falcosecurity repo-name=libs
level=INFO msg="driver building, it will take a few seconds" processor=docker
INFO using config file file: testdata/configs/1.yaml
DEBUG running with options
├ output-module: /tmp/override.ko
├ output-probe: /tmp/falco-ubuntu-aws.o
├ driverversion: master
├ kernelrelease: 4.15.0-1057-aws
├ kernelversion: 229
├ target: ubuntu-aws
├ arch: amd64
├ kernelurls: []
├ repo-org: falcosecurity
└ repo-name: libs
INFO starting build processor: docker
16 changes: 13 additions & 3 deletions cmd/testdata/docker-override-urls-from-config-debug.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
level=INFO msg="using config file" file=testdata/configs/2.yaml
level=DEBUG msg="running with options" output-module=/tmp/falco-ubuntu-aws.ko output-probe="" driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=59 target=ubuntu-aws arch={{ .CurrentArch }} kernelurls="[https://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-aws/linux-aws-headers-4.15.0-1057_4.15.0-1057.59_all.deb https://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-aws/linux-headers-4.15.0-1057-aws_4.15.0-1057.59_amd64.deb]" repo-org=falcosecurity repo-name=libs
level=INFO msg="driver building, it will take a few seconds" processor=docker
INFO using config file file: testdata/configs/2.yaml
DEBUG running with options
├ output-module: /tmp/falco-ubuntu-aws.ko
├ output-probe: /tmp/falco-ubuntu-aws.o
├ driverversion: master
├ kernelrelease: 4.15.0-1057-aws
├ kernelversion: 59
├ target: ubuntu-aws
├ arch: amd64
├ kernelurls: [https://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-aws/linux-aws-headers-4.15.0-1057_4.15.0-1057.59_all.deb https://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-aws/linux-headers-4.15.0-1057-aws_4.15.0-1057.59_amd64.deb]
├ repo-org: falcosecurity
└ repo-name: libs
INFO starting build processor: docker
16 changes: 13 additions & 3 deletions cmd/testdata/docker-related-target-debug.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
level=DEBUG msg="running without a configuration file"
level=DEBUG msg="running with options" output-module=/tmp/falco-ubuntu-azure.ko output-probe="" driverversion=master kernelrelease=4.15.0-1057-azure kernelversion=62 target=ubuntu-azure arch={{ .CurrentArch }} kernelurls="[http://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-azure/linux-azure-headers-4.15.0-1057_4.15.0-1057.62_all.deb http://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-azure/linux-headers-4.15.0-1057-azure_4.15.0-1057.62_amd64.deb]" repo-org=falcosecurity repo-name=libs
level=INFO msg="driver building, it will take a few seconds" processor=docker
DEBUG running without a configuration file
DEBUG running with options
├ output-module: /tmp/falco-ubuntu-azure.ko
├ output-probe: /tmp/falco-ubuntu-aws.o
├ driverversion: master
├ kernelrelease: 4.15.0-1057-azure
├ kernelversion: 62
├ target: ubuntu-azure
├ arch: amd64
├ kernelurls: [http://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-azure/linux-azure-headers-4.15.0-1057_4.15.0-1057.62_all.deb http://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-azure/linux-headers-4.15.0-1057-azure_4.15.0-1057.62_amd64.deb]
├ repo-org: falcosecurity
└ repo-name: libs
INFO starting build processor: docker
12 changes: 4 additions & 8 deletions cmd/testdata/docker-target-redhat-validation-error-debug.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
level=DEBUG msg="running without a configuration file"
level=ERROR msg="error validating build options" err="builder image is a required field when target is redhat"
Error: exiting for validation errors
Usage:
driverkit docker [flags]

{{ .Flags }}

DEBUG running without a configuration file
ERROR error validating build options
└ err: builder image is a required field when target is redhat
ERROR error executing driverkit err: exiting for validation errors
16 changes: 13 additions & 3 deletions cmd/testdata/docker-with-flags-debug.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
level=DEBUG msg="running without a configuration file"
level=DEBUG msg="running with options" output-module=/tmp/falco-ubuntu-aws.ko output-probe="" driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=59 target=ubuntu-aws arch={{ .CurrentArch }} kernelurls="[https://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-aws/linux-aws-headers-4.15.0-1057_4.15.0-1057.59_all.deb https://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-aws/linux-headers-4.15.0-1057-aws_4.15.0-1057.59_amd64.deb]" repo-org=falcosecurity repo-name=libs
level=INFO msg="driver building, it will take a few seconds" processor=docker
DEBUG running without a configuration file
DEBUG running with options
├ output-module: /tmp/falco-ubuntu-aws.ko
├ output-probe: /tmp/falco-ubuntu-aws.o
├ driverversion: master
├ kernelrelease: 4.15.0-1057-aws
├ kernelversion: 59
├ target: ubuntu-aws
├ arch: amd64
├ kernelurls: [https://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-aws/linux-aws-headers-4.15.0-1057_4.15.0-1057.59_all.deb https://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-aws/linux-headers-4.15.0-1057-aws_4.15.0-1057.59_amd64.deb]
├ repo-org: falcosecurity
└ repo-name: libs
INFO starting build processor: docker
2 changes: 1 addition & 1 deletion cmd/testdata/docker-with-flags.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
level=INFO msg="driver building, it will take a few seconds" processor=docker
INFO starting build processor: docker
17 changes: 7 additions & 10 deletions cmd/testdata/dockernoopts.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
level=ERROR msg="error validating build options" err="kernel release is a required field"
level=ERROR msg="error validating build options" err="target is a required field"
level=ERROR msg="error validating build options" err="output module path is required when probe is missing"
level=ERROR msg="error validating build options" err="output probe path is required when module is missing"
Error: exiting for validation errors
Usage:
driverkit docker [flags]

{{ .Flags }}

ERROR error validating build options err: kernel release is a required field
ERROR error validating build options err: target is a required field
ERROR error validating build options
└ err: output module path is required when probe is missing
ERROR error validating build options
└ err: output probe path is required when module is missing
ERROR error executing driverkit err: exiting for validation errors
14 changes: 3 additions & 11 deletions cmd/testdata/invalid-proxyconfig.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
level=ERROR msg="error validating config options" err="proxy url must start with http:// or https:// or socks5:// prefix"
Error: exiting for validation errors
{{ .Usage }}

{{ .Commands }}

{{ .Flags }}
-v, --version version for driverkit

{{ .Info }}

ERROR error validating config options
└ err: proxy url must start with http:// or https:// or socks5:// prefix
ERROR error executing driverkit err: exiting for validation errors
11 changes: 1 addition & 10 deletions cmd/testdata/non-existent-processor.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
Error: invalid argument "abc" for "driverkit"
{{ .Usage }}

{{ .Commands }}

{{ .Flags }}
-v, --version version for driverkit

{{ .Info }}

ERROR error executing driverkit err: invalid argument "abc" for "driverkit"
2 changes: 1 addition & 1 deletion cmd/testdata/templates/flags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Flags:
--kernelrelease string kernel release to build the module for, it can be found by executing 'uname -v'
--kernelurls strings list of kernel header urls (e.g. --kernelurls <URL1> --kernelurls <URL2> --kernelurls "<URL3>,<URL4>")
--kernelversion string kernel version to build the module for, it's the numeric value after the hash when you execute 'uname -v' (default "1")
-l, --loglevel string log level (default "INFO")
-l, --loglevel string set level for logs (info, warn, debug, trace) (default "info")
--moduledevicename string kernel module device name (the default is falco, so the device will be under /dev/falco*) (default "falco")
--moduledrivername string kernel module driver name, i.e. the name you see when you check installed modules via lsmod (default "falco")
--output-module string filepath where to save the resulting kernel module
Expand Down
2 changes: 1 addition & 1 deletion docs/driverkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ driverkit
--kernelrelease string kernel release to build the module for, it can be found by executing 'uname -v'
--kernelurls strings list of kernel header urls (e.g. --kernelurls <URL1> --kernelurls <URL2> --kernelurls "<URL3>,<URL4>")
--kernelversion string kernel version to build the module for, it's the numeric value after the hash when you execute 'uname -v' (default "1")
-l, --loglevel string Set level for logs (info, warn, debug, trace) (default "info")
-l, --loglevel string set level for logs (info, warn, debug, trace) (default "info")
--moduledevicename string kernel module device name (the default is falco, so the device will be under /dev/falco*) (default "falco")
--moduledrivername string kernel module driver name, i.e. the name you see when you check installed modules via lsmod (default "falco")
--output-module string filepath where to save the resulting kernel module
Expand Down
2 changes: 1 addition & 1 deletion docs/driverkit_docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ driverkit docker [flags]
--kernelrelease string kernel release to build the module for, it can be found by executing 'uname -v'
--kernelurls strings list of kernel header urls (e.g. --kernelurls <URL1> --kernelurls <URL2> --kernelurls "<URL3>,<URL4>")
--kernelversion string kernel version to build the module for, it's the numeric value after the hash when you execute 'uname -v' (default "1")
-l, --loglevel string Set level for logs (info, warn, debug, trace) (default "info")
-l, --loglevel string set level for logs (info, warn, debug, trace) (default "info")
--moduledevicename string kernel module device name (the default is falco, so the device will be under /dev/falco*) (default "falco")
--moduledrivername string kernel module driver name, i.e. the name you see when you check installed modules via lsmod (default "falco")
--output-module string filepath where to save the resulting kernel module
Expand Down
2 changes: 1 addition & 1 deletion docs/driverkit_images.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ driverkit images [flags]
--kernelrelease string kernel release to build the module for, it can be found by executing 'uname -v'
--kernelurls strings list of kernel header urls (e.g. --kernelurls <URL1> --kernelurls <URL2> --kernelurls "<URL3>,<URL4>")
--kernelversion string kernel version to build the module for, it's the numeric value after the hash when you execute 'uname -v' (default "1")
-l, --loglevel string Set level for logs (info, warn, debug, trace) (default "info")
-l, --loglevel string set level for logs (info, warn, debug, trace) (default "info")
--moduledevicename string kernel module device name (the default is falco, so the device will be under /dev/falco*) (default "falco")
--moduledrivername string kernel module driver name, i.e. the name you see when you check installed modules via lsmod (default "falco")
--output-module string filepath where to save the resulting kernel module
Expand Down
2 changes: 1 addition & 1 deletion docs/driverkit_kubernetes-in-cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ driverkit kubernetes-in-cluster [flags]
--kernelrelease string kernel release to build the module for, it can be found by executing 'uname -v'
--kernelurls strings list of kernel header urls (e.g. --kernelurls <URL1> --kernelurls <URL2> --kernelurls "<URL3>,<URL4>")
--kernelversion string kernel version to build the module for, it's the numeric value after the hash when you execute 'uname -v' (default "1")
-l, --loglevel string Set level for logs (info, warn, debug, trace) (default "info")
-l, --loglevel string set level for logs (info, warn, debug, trace) (default "info")
--moduledevicename string kernel module device name (the default is falco, so the device will be under /dev/falco*) (default "falco")
--moduledrivername string kernel module driver name, i.e. the name you see when you check installed modules via lsmod (default "falco")
-n, --namespace string If present, the namespace scope for the pods and its config (default "default")
Expand Down
2 changes: 1 addition & 1 deletion docs/driverkit_kubernetes.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ driverkit kubernetes [flags]
--kernelurls strings list of kernel header urls (e.g. --kernelurls <URL1> --kernelurls <URL2> --kernelurls "<URL3>,<URL4>")
--kernelversion string kernel version to build the module for, it's the numeric value after the hash when you execute 'uname -v' (default "1")
--kubeconfig string path to the kubeconfig file to use for CLI requests
-l, --loglevel string Set level for logs (info, warn, debug, trace) (default "info")
-l, --loglevel string set level for logs (info, warn, debug, trace) (default "info")
--moduledevicename string kernel module device name (the default is falco, so the device will be under /dev/falco*) (default "falco")
--moduledrivername string kernel module driver name, i.e. the name you see when you check installed modules via lsmod (default "falco")
-n, --namespace string If present, the namespace scope for the pods and its config (default "default")
Expand Down
Loading

0 comments on commit c1d2c62

Please sign in to comment.