Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not display help when deleting all pods/containers #1147

Merged
merged 4 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ var removeContainerCommand = &cli.Command{
}

champtar marked this conversation as resolved.
Show resolved Hide resolved
if len(ids) == 0 {
if ctx.Bool("all") {
logrus.Info("No containers to remove")
return nil
champtar marked this conversation as resolved.
Show resolved Hide resolved
}
return cli.ShowSubcommandHelp(ctx)
}

Expand Down
7 changes: 5 additions & 2 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
41 changes: 41 additions & 0 deletions test/e2e/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package e2e

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega/gexec"
)

// The actual test suite
Expand All @@ -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")
})
})
2 changes: 1 addition & 1 deletion test/e2e/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
})
46 changes: 25 additions & 21 deletions test/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down