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

add args parameter for exec command #272

Merged
merged 1 commit into from
Mar 17, 2025
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
411 changes: 411 additions & 0 deletions README.md

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion pkg/chaos/docker/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
Usage: "shell command, that will be sent by Pumba to the target container(s)",
Value: "kill 1",
},
cli.StringSliceFlag{
Name: "args, a",
Usage: "additional arguments for the command",
},
cli.IntFlag{
Name: "limit, l",
Usage: "limit number of container to exec (0: exec all matching)",
Expand All @@ -47,10 +51,12 @@
}
// get command
command := c.String("command")
// get args
args := c.StringSlice("args")

Check warning on line 55 in pkg/chaos/docker/cmd/exec.go

View check run for this annotation

Codecov / codecov/patch

pkg/chaos/docker/cmd/exec.go#L55

Added line #L55 was not covered by tests
// get limit for number of containers to exec
limit := c.Int("limit")
// init exec command
execCommand := docker.NewExecCommand(chaos.DockerClient, params, command, limit)
execCommand := docker.NewExecCommand(chaos.DockerClient, params, command, args, limit)

Check warning on line 59 in pkg/chaos/docker/cmd/exec.go

View check run for this annotation

Codecov / codecov/patch

pkg/chaos/docker/cmd/exec.go#L59

Added line #L59 was not covered by tests
// run exec command
err = chaos.RunChaosCommand(cmd.context, execCommand, params)
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions pkg/chaos/docker/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ type execCommand struct {
pattern string
labels []string
command string
args []string
limit int
dryRun bool
}

// NewExecCommand create new Exec Command instance
func NewExecCommand(client container.Client, params *chaos.GlobalParams, command string, limit int) chaos.Command {
func NewExecCommand(client container.Client, params *chaos.GlobalParams, command string, args []string, limit int) chaos.Command {
exec := &execCommand{
client: client,
names: params.Names,
pattern: params.Pattern,
labels: params.Labels,
command: command,
args: args,
limit: limit,
dryRun: params.DryRun,
}
Expand Down Expand Up @@ -66,9 +68,10 @@ func (k *execCommand) Run(ctx context.Context, random bool) error {
log.WithFields(log.Fields{
"c": *c,
"command": k.command,
"args": k.args,
}).Debug("execing c")
cc := c
err = k.client.ExecContainer(ctx, cc, k.command, k.dryRun)
err = k.client.ExecContainer(ctx, cc, k.command, k.args, k.dryRun)
if err != nil {
return errors.Wrap(err, "failed to run exec command")
}
Expand Down
27 changes: 18 additions & 9 deletions pkg/chaos/docker/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestExecCommand_Run(t *testing.T) {
type fields struct {
params *chaos.GlobalParams
command string
args []string
limit int
}
type args struct {
Expand All @@ -38,7 +39,8 @@ func TestExecCommand_Run(t *testing.T) {
params: &chaos.GlobalParams{
Names: []string{"c1", "c2"},
},
command: "kill 1",
command: "kill",
args: []string{"-9"},
},
args: args{
ctx: context.TODO(),
Expand All @@ -52,7 +54,8 @@ func TestExecCommand_Run(t *testing.T) {
Names: []string{"c1", "c2", "c3"},
Labels: []string{"key=value"},
},
command: "kill 1",
command: "ls",
args: []string{"-la"},
},
args: args{
ctx: context.TODO(),
Expand All @@ -65,7 +68,8 @@ func TestExecCommand_Run(t *testing.T) {
params: &chaos.GlobalParams{
Pattern: "^c?",
},
command: "kill -STOP 1",
command: "kill",
args: []string{"-STOP", "1"},
limit: 2,
},
args: args{
Expand All @@ -79,7 +83,8 @@ func TestExecCommand_Run(t *testing.T) {
params: &chaos.GlobalParams{
Names: []string{"c1", "c2", "c3"},
},
command: "kill 1",
command: "kill",
args: []string{"1"},
},
args: args{
ctx: context.TODO(),
Expand All @@ -93,7 +98,8 @@ func TestExecCommand_Run(t *testing.T) {
params: &chaos.GlobalParams{
Names: []string{"c1", "c2", "c3"},
},
command: "kill 1",
command: "kill",
args: []string{"1"},
},
args: args{
ctx: context.TODO(),
Expand All @@ -105,7 +111,8 @@ func TestExecCommand_Run(t *testing.T) {
params: &chaos.GlobalParams{
Names: []string{"c1", "c2", "c3"},
},
command: "kill 1",
command: "kill",
args: []string{"1"},
},
args: args{
ctx: context.TODO(),
Expand All @@ -119,7 +126,8 @@ func TestExecCommand_Run(t *testing.T) {
params: &chaos.GlobalParams{
Names: []string{"c1", "c2", "c3"},
},
command: "kill 1",
command: "kill",
args: []string{"1"},
},
args: args{
ctx: context.TODO(),
Expand All @@ -138,6 +146,7 @@ func TestExecCommand_Run(t *testing.T) {
pattern: tt.fields.params.Pattern,
labels: tt.fields.params.Labels,
command: tt.fields.command,
args: tt.fields.args,
limit: tt.fields.limit,
dryRun: tt.fields.params.DryRun,
}
Expand All @@ -153,11 +162,11 @@ func TestExecCommand_Run(t *testing.T) {
}
}
if tt.args.random {
mockClient.On("ExecContainer", tt.args.ctx, mock.AnythingOfType("*container.Container"), tt.fields.command, tt.fields.params.DryRun).Return(nil)
mockClient.On("ExecContainer", tt.args.ctx, mock.AnythingOfType("*container.Container"), tt.fields.command, tt.fields.args, tt.fields.params.DryRun).Return(nil)
} else {
for i := range tt.expected {
if tt.fields.limit == 0 || i < tt.fields.limit {
call = mockClient.On("ExecContainer", tt.args.ctx, mock.AnythingOfType("*container.Container"), tt.fields.command, tt.fields.params.DryRun)
call = mockClient.On("ExecContainer", tt.args.ctx, mock.AnythingOfType("*container.Container"), tt.fields.command, tt.fields.args, tt.fields.params.DryRun)
if tt.errs.execError {
call.Return(errors.New("ERROR"))
goto Invoke
Expand Down
2 changes: 1 addition & 1 deletion pkg/container/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Client interface {
ListContainers(context.Context, FilterFunc, ListOpts) ([]*Container, error)
StopContainer(context.Context, *Container, int, bool) error
KillContainer(context.Context, *Container, string, bool) error
ExecContainer(context.Context, *Container, string, bool) error
ExecContainer(context.Context, *Container, string, []string, bool) error
RestartContainer(context.Context, *Container, time.Duration, bool) error
RemoveContainer(context.Context, *Container, bool, bool, bool, bool) error
NetemContainer(context.Context, *Container, string, []string, []*net.IPNet, []string, []string, time.Duration, string, bool, bool) error
Expand Down
Loading
Loading