Skip to content

Commit 5655a7b

Browse files
alexei-ledMarvin
andauthored
fix: require container arguments for kill, stop, and rm (#290)
Fixes #129 - kill, stop, and rm commands now return clear error when no container arguments provided - Added unit tests for argument validation - Added integration tests for missing-argument errors Co-authored-by: Marvin <marvin@openclaw.ai>
1 parent c7c10f0 commit 5655a7b

File tree

6 files changed

+69
-1
lines changed

6 files changed

+69
-1
lines changed

docs/guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pumba --random kill "re2:^test"
5858

5959
## Container Chaos Commands
6060

61-
Each command targets containers using the [targeting methods](#container-targeting) described above. Run `pumba <command> --help` for the full list of options.
61+
Each command targets containers using the [targeting methods](#container-targeting) described above. Run `pumba <command> --help` for the full list of options. The `kill`, `stop`, and `rm` commands require at least one container argument (name, list of names, or RE2 regex).
6262

6363
### kill
6464

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"flag"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/urfave/cli"
10+
)
11+
12+
func newTestCLIContext(args []string) *cli.Context {
13+
app := cli.NewApp()
14+
fs := flag.NewFlagSet("test", flag.ContinueOnError)
15+
_ = fs.Parse(args)
16+
return cli.NewContext(app, fs, nil)
17+
}
18+
19+
func TestKillRequiresContainerArgument(t *testing.T) {
20+
cmd := &killContext{context: context.Background()}
21+
c := newTestCLIContext(nil)
22+
err := cmd.kill(c)
23+
assert.EqualError(t, err, "container name, list of names, or RE2 regex is required")
24+
}
25+
26+
func TestStopRequiresContainerArgument(t *testing.T) {
27+
cmd := &stopContext{context: context.Background()}
28+
c := newTestCLIContext(nil)
29+
err := cmd.stop(c)
30+
assert.EqualError(t, err, "container name, list of names, or RE2 regex is required")
31+
}
32+
33+
func TestRemoveRequiresContainerArgument(t *testing.T) {
34+
cmd := &removeContext{context: context.Background()}
35+
c := newTestCLIContext(nil)
36+
err := cmd.remove(c)
37+
assert.EqualError(t, err, "container name, list of names, or RE2 regex is required")
38+
}

pkg/chaos/docker/cmd/kill.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ func NewKillCLICommand(ctx context.Context) *cli.Command {
3939

4040
// KILL Command
4141
func (cmd *killContext) kill(c *cli.Context) error {
42+
if !c.Args().Present() {
43+
return fmt.Errorf("container name, list of names, or RE2 regex is required")
44+
}
4245
// parse common chaos flags
4346
params, err := chaos.ParseGlobalParams(c)
4447
if err != nil {

pkg/chaos/docker/cmd/remove.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ func NewRemoveCLICommand(ctx context.Context) *cli.Command {
4646

4747
// REMOVE Command
4848
func (cmd *removeContext) remove(c *cli.Context) error {
49+
if !c.Args().Present() {
50+
return fmt.Errorf("container name, list of names, or RE2 regex is required")
51+
}
4952
// parse common chaos flags
5053
params, err := chaos.ParseGlobalParams(c)
5154
if err != nil {

pkg/chaos/docker/cmd/stop.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ func NewStopCLICommand(ctx context.Context) *cli.Command {
4949

5050
// STOP Command
5151
func (cmd *stopContext) stop(c *cli.Context) error {
52+
if !c.Args().Present() {
53+
return fmt.Errorf("container name, list of names, or RE2 regex is required")
54+
}
5255
// parse common chaos flags
5356
params, err := chaos.ParseGlobalParams(c)
5457
if err != nil {

tests/error_handling.bats

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,27 @@ teardown() {
148148
[ $status -eq 0 ]
149149
}
150150

151+
@test "Should fail when kill command has no container arguments" {
152+
run pumba kill
153+
154+
[ $status -ne 0 ]
155+
[[ $output =~ "container name, list of names, or RE2 regex is required" ]]
156+
}
157+
158+
@test "Should fail when stop command has no container arguments" {
159+
run pumba stop
160+
161+
[ $status -ne 0 ]
162+
[[ $output =~ "container name, list of names, or RE2 regex is required" ]]
163+
}
164+
165+
@test "Should fail when rm command has no container arguments" {
166+
run pumba rm
167+
168+
[ $status -ne 0 ]
169+
[[ $output =~ "container name, list of names, or RE2 regex is required" ]]
170+
}
171+
151172
@test "Should handle CIDR notation formats" {
152173
# This test skips actual execution since we're just testing CLI parsing
153174
run pumba iptables --help

0 commit comments

Comments
 (0)