mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-14 11:48:53 +00:00
chore: add cli unit tests (#8365)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
parent
e3188fca8c
commit
34c1615090
8 changed files with 148 additions and 26 deletions
|
@ -10,15 +10,16 @@ import (
|
||||||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/oci"
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/oci"
|
||||||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test"
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/test"
|
||||||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/version"
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/version"
|
||||||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/experimental"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RootCommand() *cobra.Command {
|
func RootCommand(experimental bool) *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "kyverno",
|
Use: "kyverno",
|
||||||
Short: command.FormatDescription(true, websiteUrl, false, description...),
|
Short: command.FormatDescription(true, websiteUrl, false, description...),
|
||||||
Long: command.FormatDescription(false, websiteUrl, false, description...),
|
Long: command.FormatDescription(false, websiteUrl, false, description...),
|
||||||
|
SilenceErrors: true,
|
||||||
|
SilenceUsage: true,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return cmd.Help()
|
return cmd.Help()
|
||||||
},
|
},
|
||||||
|
@ -31,7 +32,7 @@ func RootCommand() *cobra.Command {
|
||||||
test.Command(),
|
test.Command(),
|
||||||
version.Command(),
|
version.Command(),
|
||||||
)
|
)
|
||||||
if experimental.IsExperimentalEnabled() {
|
if experimental {
|
||||||
cmd.AddCommand(
|
cmd.AddCommand(
|
||||||
fix.Command(),
|
fix.Command(),
|
||||||
oci.Command(),
|
oci.Command(),
|
||||||
|
|
23
cmd/cli/kubectl-kyverno/commands/command_test.go
Normal file
23
cmd/cli/kubectl-kyverno/commands/command_test.go
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRootCommand(t *testing.T) {
|
||||||
|
cmd := RootCommand(false)
|
||||||
|
assert.NotNil(t, cmd)
|
||||||
|
assert.Len(t, cmd.Commands(), 6)
|
||||||
|
err := cmd.Execute()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRootCommandExperimental(t *testing.T) {
|
||||||
|
cmd := RootCommand(true)
|
||||||
|
assert.NotNil(t, cmd)
|
||||||
|
assert.Len(t, cmd.Commands(), 8)
|
||||||
|
err := cmd.Execute()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
|
@ -10,11 +10,13 @@ import (
|
||||||
|
|
||||||
func Command() *cobra.Command {
|
func Command() *cobra.Command {
|
||||||
return &cobra.Command{
|
return &cobra.Command{
|
||||||
Use: "version",
|
Use: "version",
|
||||||
Short: command.FormatDescription(true, websiteUrl, false, description...),
|
Short: command.FormatDescription(true, websiteUrl, false, description...),
|
||||||
Long: command.FormatDescription(false, websiteUrl, false, description...),
|
Long: command.FormatDescription(false, websiteUrl, false, description...),
|
||||||
Example: command.FormatExamples(examples...),
|
Example: command.FormatExamples(examples...),
|
||||||
Args: cobra.NoArgs,
|
SilenceErrors: true,
|
||||||
|
SilenceUsage: true,
|
||||||
|
Args: cobra.NoArgs,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
fmt.Fprintf(cmd.OutOrStdout(), "Version: %s\n", version.Version())
|
fmt.Fprintf(cmd.OutOrStdout(), "Version: %s\n", version.Version())
|
||||||
fmt.Fprintf(cmd.OutOrStdout(), "Time: %s\n", version.Time())
|
fmt.Fprintf(cmd.OutOrStdout(), "Time: %s\n", version.Time())
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
|
|
||||||
const experimentalEnv = "KYVERNO_EXPERIMENTAL"
|
const experimentalEnv = "KYVERNO_EXPERIMENTAL"
|
||||||
|
|
||||||
func IsExperimentalEnabled() bool {
|
func IsEnabled() bool {
|
||||||
if b, err := strconv.ParseBool(os.Getenv(experimentalEnv)); err == nil {
|
if b, err := strconv.ParseBool(os.Getenv(experimentalEnv)); err == nil {
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package experimental
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestIsExperimentalEnabled(t *testing.T) {
|
func TestIsEnabled(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
env map[string]string
|
env map[string]string
|
||||||
|
@ -54,8 +54,8 @@ func TestIsExperimentalEnabled(t *testing.T) {
|
||||||
for k, v := range tt.env {
|
for k, v := range tt.env {
|
||||||
t.Setenv(k, v)
|
t.Setenv(k, v)
|
||||||
}
|
}
|
||||||
if got := IsExperimentalEnabled(); got != tt.want {
|
if got := IsEnabled(); got != tt.want {
|
||||||
t.Errorf("IsExperimentalEnabled() = %v, want %v", got, tt.want)
|
t.Errorf("IsEnabled() = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,17 +12,24 @@ const loggerName = "kubectl-kyverno"
|
||||||
var Log = logging.WithName(loggerName)
|
var Log = logging.WithName(loggerName)
|
||||||
|
|
||||||
func Configure() error {
|
func Configure() error {
|
||||||
|
return configure(os.Args[1:]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func configure(args ...string) error {
|
||||||
logging.InitFlags(nil)
|
logging.InitFlags(nil)
|
||||||
verbose := false
|
if isVerbose(args...) {
|
||||||
for _, arg := range os.Args[1:] {
|
|
||||||
if arg == "-v" || arg == "--v" {
|
|
||||||
verbose = true
|
|
||||||
} else if strings.HasPrefix(arg, "-v=") || strings.HasPrefix(arg, "--v=") {
|
|
||||||
verbose = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if verbose {
|
|
||||||
return logging.Setup(logging.TextFormat, 0)
|
return logging.Setup(logging.TextFormat, 0)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isVerbose(args ...string) bool {
|
||||||
|
for _, arg := range args {
|
||||||
|
if arg == "-v" || arg == "--v" {
|
||||||
|
return true
|
||||||
|
} else if strings.HasPrefix(arg, "-v=") || strings.HasPrefix(arg, "--v=") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
|
@ -1,9 +1,97 @@
|
||||||
package log
|
package log
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestConfigure(t *testing.T) {
|
func TestConfigure(t *testing.T) {
|
||||||
if err := Configure(); (err != nil) != false {
|
if err := Configure(); (err != nil) != false {
|
||||||
t.Errorf("Configure() error = %v, wantErr %v", err, false)
|
t.Errorf("Configure() error = %v, wantErr %v", err, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_isVerbose(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args []string
|
||||||
|
want bool
|
||||||
|
}{{
|
||||||
|
name: "nil",
|
||||||
|
args: nil,
|
||||||
|
want: false,
|
||||||
|
}, {
|
||||||
|
name: "empty",
|
||||||
|
args: []string{},
|
||||||
|
want: false,
|
||||||
|
}, {
|
||||||
|
name: "not verbose",
|
||||||
|
args: []string{"-verbose", "--verbose", "-vv", "--vv"},
|
||||||
|
want: false,
|
||||||
|
}, {
|
||||||
|
name: "verbose",
|
||||||
|
args: []string{"-v", "3"},
|
||||||
|
want: true,
|
||||||
|
}, {
|
||||||
|
name: "verbose",
|
||||||
|
args: []string{"-v=3"},
|
||||||
|
want: true,
|
||||||
|
}, {
|
||||||
|
name: "verbose",
|
||||||
|
args: []string{"--v", "3"},
|
||||||
|
want: true,
|
||||||
|
}, {
|
||||||
|
name: "verbose",
|
||||||
|
args: []string{"--v=3"},
|
||||||
|
want: true,
|
||||||
|
}}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := isVerbose(tt.args...); got != tt.want {
|
||||||
|
t.Errorf("isVerbose() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_configure(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args []string
|
||||||
|
wantErr bool
|
||||||
|
}{{
|
||||||
|
name: "nil",
|
||||||
|
args: nil,
|
||||||
|
wantErr: false,
|
||||||
|
}, {
|
||||||
|
name: "empty",
|
||||||
|
args: []string{},
|
||||||
|
wantErr: false,
|
||||||
|
}, {
|
||||||
|
name: "not verbose",
|
||||||
|
args: []string{"-verbose", "--verbose", "-vv", "--vv"},
|
||||||
|
wantErr: false,
|
||||||
|
}, {
|
||||||
|
name: "verbose",
|
||||||
|
args: []string{"-v", "3"},
|
||||||
|
wantErr: false,
|
||||||
|
}, {
|
||||||
|
name: "verbose",
|
||||||
|
args: []string{"-v=3"},
|
||||||
|
wantErr: false,
|
||||||
|
}, {
|
||||||
|
name: "verbose",
|
||||||
|
args: []string{"--v", "3"},
|
||||||
|
wantErr: false,
|
||||||
|
}, {
|
||||||
|
name: "verbose",
|
||||||
|
args: []string{"--v=3"},
|
||||||
|
wantErr: false,
|
||||||
|
}}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if err := configure(tt.args...); (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("configure() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands"
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands"
|
||||||
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/experimental"
|
||||||
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/log"
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/log"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
@ -18,7 +19,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func run() error {
|
func run() error {
|
||||||
cmd := commands.RootCommand()
|
cmd := commands.RootCommand(experimental.IsEnabled())
|
||||||
if err := configureLogs(cmd); err != nil {
|
if err := configureLogs(cmd); err != nil {
|
||||||
return fmt.Errorf("Failed to setup logging (%w)", err)
|
return fmt.Errorf("Failed to setup logging (%w)", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue