From 4eb6a64b494adbff0bb4a54ac2f452470eb2a8ad Mon Sep 17 00:00:00 2001 From: Etienne Champetier Date: Fri, 28 Apr 2023 13:47:23 -0400 Subject: [PATCH 1/4] Do not display help when deleting all pods/containers Running `crictl rmp -a` or `crictl rm -a` with nothing to delete was displaying the help text instead of saying that there was nothing to delete. Signed-off-by: Etienne Champetier --- cmd/crictl/container.go | 4 ++++ cmd/crictl/sandbox.go | 7 +++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/cmd/crictl/container.go b/cmd/crictl/container.go index 878386d704..9e5fc857da 100644 --- a/cmd/crictl/container.go +++ b/cmd/crictl/container.go @@ -351,6 +351,10 @@ var removeContainerCommand = &cli.Command{ } if len(ids) == 0 { + if ctx.Bool("all") { + logrus.Info("No containers to remove") + return nil + } return cli.ShowSubcommandHelp(ctx) } diff --git a/cmd/crictl/sandbox.go b/cmd/crictl/sandbox.go index 3d752db49f..e7e0646680 100644 --- a/cmd/crictl/sandbox.go +++ b/cmd/crictl/sandbox.go @@ -144,8 +144,11 @@ var removePodCommand = &cli.Command{ } } - lenIDs := len(ids) - if lenIDs == 0 { + if len(ids) == 0 { + if ctx.Bool("all") { + logrus.Info("No pods to remove") + return nil + } return cli.ShowSubcommandHelp(ctx) } From c714ecd63c8855294d560d66507fe095670e4ab8 Mon Sep 17 00:00:00 2001 From: Etienne Champetier Date: Fri, 28 Apr 2023 15:59:48 -0400 Subject: [PATCH 2/4] Display help when running 'crictl rmi' 'crictl rmi' can take multiple IMAGE-ID, -a and/or -p If we pass nothing, display help text Signed-off-by: Etienne Champetier --- cmd/crictl/image.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cmd/crictl/image.go b/cmd/crictl/image.go index 2adfc3586c..568ab6bbd1 100644 --- a/cmd/crictl/image.go +++ b/cmd/crictl/image.go @@ -393,8 +393,11 @@ var removeImageCommand = &cli.Command{ } if len(ids) == 0 { - logrus.Info("No images to remove") - return nil + if all || prune { + logrus.Info("No images to remove") + return nil + } + return cli.ShowSubcommandHelp(cliCtx) } errored := false From 13dbe21fa21563e203b0b587b126b05e41d80435 Mon Sep 17 00:00:00 2001 From: Etienne Champetier Date: Fri, 28 Apr 2023 20:04:06 -0400 Subject: [PATCH 3/4] Add CrictlExpect() to test framework This is a generic version of the CrictlExpect* allowing tests such as Exit(0), empty stdout, specific stderr. --- test/e2e/info_test.go | 2 +- test/framework/framework.go | 46 ++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/test/e2e/info_test.go b/test/e2e/info_test.go index cfa43ff495..35049f33e8 100644 --- a/test/e2e/info_test.go +++ b/test/e2e/info_test.go @@ -41,6 +41,6 @@ var _ = t.Describe("info", func() { }) It("should fail with additional argument", func() { - t.CrictlExpectFailure("--invalid", "", "flag provided but not defined") + t.CrictlExpectFailure("--invalid", "crictl - client for CRI", "flag provided but not defined") }) }) diff --git a/test/framework/framework.go b/test/framework/framework.go index be07453402..02ae9a19c3 100644 --- a/test/framework/framework.go +++ b/test/framework/framework.go @@ -83,50 +83,54 @@ func lcmd(format string, args ...interface{}) *Session { return cmd("", format, args...) } -// Run crictl and return the resulting session -func (t *TestFramework) Crictl(args string) *Session { - return lcmd("crictl %s", args).Wait() -} - // Run crictl on the specified endpoint and return the resulting session func (t *TestFramework) CrictlWithEndpoint(endpoint, args string) *Session { return lcmd("crictl --runtime-endpoint=%s %s", endpoint, args).Wait(time.Minute) } +// Run crictl and expect exit, expectedOut, expectedErr +func (t *TestFramework) CrictlExpect( + endpoint, args string, exit int, expectedOut, expectedErr string, +) { + // When + res := t.CrictlWithEndpoint(endpoint, args) + + // Then + Expect(res).To(Exit(exit)) + if expectedOut == "" { + Expect(string(res.Out.Contents())).To(BeEmpty()) + } else { + Expect(res.Out).To(Say(expectedOut)) + } + if expectedErr == "" { + Expect(string(res.Err.Contents())).To(BeEmpty()) + } else { + Expect(res.Err).To(Say(expectedErr)) + } +} + // Run crictl and expect success containing the specified output func (t *TestFramework) CrictlExpectSuccess(args, expectedOut string) { - t.CrictlExpectSuccessWithEndpoint("", args, expectedOut) + t.CrictlExpect("", args, 0, expectedOut, "") } // Run crictl and expect success containing the specified output func (t *TestFramework) CrictlExpectSuccessWithEndpoint(endpoint, args, expectedOut string) { - // When - res := t.CrictlWithEndpoint(endpoint, args) - - // Then - Expect(res).To(Exit(0)) - Expect(res.Out).To(Say(expectedOut)) - Expect(string(res.Err.Contents())).To(BeEmpty()) + t.CrictlExpect(endpoint, args, 0, expectedOut, "") } // Run crictl and expect error containing the specified outputs func (t *TestFramework) CrictlExpectFailure( args string, expectedOut, expectedErr string, ) { - t.CrictlExpectFailureWithEndpoint("", args, expectedOut, expectedErr) + t.CrictlExpect("", args, 1, expectedOut, expectedErr) } // Run crictl and expect failure containing the specified output func (t *TestFramework) CrictlExpectFailureWithEndpoint( endpoint, args, expectedOut, expectedErr string, ) { - // When - res := t.CrictlWithEndpoint(endpoint, args) - - // Then - Expect(res).To(Exit(1)) - Expect(res.Out).To(Say(expectedOut)) - Expect(res.Err).To(Say(expectedErr)) + t.CrictlExpect(endpoint, args, 1, expectedOut, expectedErr) } func SetupCrio() string { From dadffe3e4feeb64ab5ae7ace4c171f64f18345eb Mon Sep 17 00:00:00 2001 From: Etienne Champetier Date: Fri, 28 Apr 2023 16:21:28 -0400 Subject: [PATCH 4/4] test rm{,i,p} subcommands help message Signed-off-by: Etienne Champetier --- test/e2e/help_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/e2e/help_test.go b/test/e2e/help_test.go index e31af5cf15..2a94f2b3ea 100644 --- a/test/e2e/help_test.go +++ b/test/e2e/help_test.go @@ -18,6 +18,7 @@ package e2e import ( . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega/gexec" ) // The actual test suite @@ -42,3 +43,43 @@ var _ = t.Describe("help", func() { "flag provided but not defined") }) }) + +// The actual test suite +var _ = t.Describe("help subcommand", func() { + + var ( + endpoint, testDir string + crio *Session + ) + BeforeEach(func() { + endpoint, testDir, crio = t.StartCrio() + }) + + AfterEach(func() { + t.StopCrio(testDir, crio) + }) + + It("should show help running rm without params", func() { + t.CrictlExpectSuccessWithEndpoint(endpoint, "rm", "crictl rm command") + }) + + It("should show help running rmi without params", func() { + t.CrictlExpectSuccessWithEndpoint(endpoint, "rmi", "crictl rmi command") + }) + + It("should show help running rmp without params", func() { + t.CrictlExpectSuccessWithEndpoint(endpoint, "rmp", "crictl rmp command") + }) + + It("should not show help running rm -a", func() { + t.CrictlExpect(endpoint, "rm -a", 0, "", "No containers to remove") + }) + + It("should not show help running rmi -a", func() { + t.CrictlExpect(endpoint, "rmi -a", 0, "", "No images to remove") + }) + + It("should not show help running rmp -a", func() { + t.CrictlExpect(endpoint, "rmp -a", 0, "", "No pods to remove") + }) +})