From 82e44d9b8c0161f0482a6fc5b2dae97743c09deb Mon Sep 17 00:00:00 2001 From: Hiroki Okui Date: Sat, 21 Sep 2024 12:43:35 +0900 Subject: [PATCH 1/2] fix: Make flags work --- internal/cmd/init.go | 4 ++-- internal/cmd/root.go | 8 +++++--- internal/cmd/run.go | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/internal/cmd/init.go b/internal/cmd/init.go index 7fecb72..b6e8033 100644 --- a/internal/cmd/init.go +++ b/internal/cmd/init.go @@ -23,7 +23,7 @@ import ( "github.com/spf13/cobra" ) -func newInitCmd(cfg tester.CmdConfig) *cobra.Command { +func newInitCmd(cfg *tester.CmdConfig) *cobra.Command { return &cobra.Command{ Use: "init [path to admission policy file]", Short: "Generate skeleton manifests for writing tests", @@ -32,7 +32,7 @@ func newInitCmd(cfg tester.CmdConfig) *cobra.Command { return fmt.Errorf("path is required") } targetFilePath := args[0] - return tester.RunInit(cfg, targetFilePath) + return tester.RunInit(*cfg, targetFilePath) }, } } diff --git a/internal/cmd/root.go b/internal/cmd/root.go index f35799a..f093513 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -41,10 +41,12 @@ func newRootCmd() *cobra.Command { cmd.PersistentFlags().BoolVarP(&cfg.Verbose, "verbose", "v", false, "Verbose output") cmd.PersistentFlags().BoolVarP(&cfg.Debug, "debug", "d", false, "Debug output") - initLog(cfg) + cobra.OnInitialize(func() { + initLog(cfg) + }) - cmd.AddCommand(newInitCmd(cfg)) - cmd.AddCommand(newRunCmd(cfg)) + cmd.AddCommand(newInitCmd(&cfg)) + cmd.AddCommand(newRunCmd(&cfg)) cmd.AddCommand(newVersionCmd()) return cmd } diff --git a/internal/cmd/run.go b/internal/cmd/run.go index c7499c4..7d12704 100644 --- a/internal/cmd/run.go +++ b/internal/cmd/run.go @@ -23,7 +23,7 @@ import ( "github.com/spf13/cobra" ) -func newRunCmd(cfg tester.CmdConfig) *cobra.Command { +func newRunCmd(cfg *tester.CmdConfig) *cobra.Command { return &cobra.Command{ Use: "run [path to test manifest]...", Short: "Run the tests of ValidatingAdmissionPolicy", @@ -31,7 +31,7 @@ func newRunCmd(cfg tester.CmdConfig) *cobra.Command { if len(args) == 0 { return fmt.Errorf("path is required") } - return tester.Run(cfg, args) + return tester.Run(*cfg, args) }, } } From 4675bc8fd77e7a931288da2f860d3759fef05673 Mon Sep 17 00:00:00 2001 From: Hiroki Okui Date: Sat, 21 Sep 2024 12:50:07 +0900 Subject: [PATCH 2/2] fix: Show detailed message of failed tests even when verbose flag not set --- internal/tester/result.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/internal/tester/result.go b/internal/tester/result.go index c3020ac..4498115 100644 --- a/internal/tester/result.go +++ b/internal/tester/result.go @@ -84,12 +84,11 @@ func (r *policyEvalResult) String(verbose bool) string { } summary += fmt.Sprintf(" - %s ==> %s", strings.ToUpper(string(r.TestCase.Expect)), strings.ToUpper(string(r.Result))) - if !verbose { - return summary - } - out := []string{summary} for _, d := range r.Decisions { + if r.Pass() && !verbose { + continue + } // Workaround to handle the case where the evaluation is not set // TODO remove this workaround after htcps://github.com/kubernetes/kubernetes/pull/126867 is released if d.Evaluation == "" {