Skip to content

Commit 480a751

Browse files
authored
Merge 253e6be into 34e73d9
2 parents 34e73d9 + 253e6be commit 480a751

File tree

10 files changed

+689
-158
lines changed

10 files changed

+689
-158
lines changed

README.md

+411
Large diffs are not rendered by default.

pkg/chaos/docker/cmd/exec.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ func NewExecCLICommand(ctx context.Context) *cli.Command {
2525
Usage: "shell command, that will be sent by Pumba to the target container(s)",
2626
Value: "kill 1",
2727
},
28+
cli.StringSliceFlag{
29+
Name: "args, a",
30+
Usage: "additional arguments for the command",
31+
},
2832
cli.IntFlag{
2933
Name: "limit, l",
3034
Usage: "limit number of container to exec (0: exec all matching)",
@@ -47,10 +51,12 @@ func (cmd *execContext) exec(c *cli.Context) error {
4751
}
4852
// get command
4953
command := c.String("command")
54+
// get args
55+
args := c.StringSlice("args")
5056
// get limit for number of containers to exec
5157
limit := c.Int("limit")
5258
// init exec command
53-
execCommand := docker.NewExecCommand(chaos.DockerClient, params, command, limit)
59+
execCommand := docker.NewExecCommand(chaos.DockerClient, params, command, args, limit)
5460
// run exec command
5561
err = chaos.RunChaosCommand(cmd.context, execCommand, params)
5662
if err != nil {

pkg/chaos/docker/exec.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@ type execCommand struct {
1616
pattern string
1717
labels []string
1818
command string
19+
args []string
1920
limit int
2021
dryRun bool
2122
}
2223

2324
// NewExecCommand create new Exec Command instance
24-
func NewExecCommand(client container.Client, params *chaos.GlobalParams, command string, limit int) chaos.Command {
25+
func NewExecCommand(client container.Client, params *chaos.GlobalParams, command string, args []string, limit int) chaos.Command {
2526
exec := &execCommand{
2627
client: client,
2728
names: params.Names,
2829
pattern: params.Pattern,
2930
labels: params.Labels,
3031
command: command,
32+
args: args,
3133
limit: limit,
3234
dryRun: params.DryRun,
3335
}
@@ -66,9 +68,10 @@ func (k *execCommand) Run(ctx context.Context, random bool) error {
6668
log.WithFields(log.Fields{
6769
"c": *c,
6870
"command": k.command,
71+
"args": k.args,
6972
}).Debug("execing c")
7073
cc := c
71-
err = k.client.ExecContainer(ctx, cc, k.command, k.dryRun)
74+
err = k.client.ExecContainer(ctx, cc, k.command, k.args, k.dryRun)
7275
if err != nil {
7376
return errors.Wrap(err, "failed to run exec command")
7477
}

pkg/chaos/docker/exec_test.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func TestExecCommand_Run(t *testing.T) {
1818
type fields struct {
1919
params *chaos.GlobalParams
2020
command string
21+
args []string
2122
limit int
2223
}
2324
type args struct {
@@ -38,7 +39,8 @@ func TestExecCommand_Run(t *testing.T) {
3839
params: &chaos.GlobalParams{
3940
Names: []string{"c1", "c2"},
4041
},
41-
command: "kill 1",
42+
command: "kill",
43+
args: []string{"-9"},
4244
},
4345
args: args{
4446
ctx: context.TODO(),
@@ -52,7 +54,8 @@ func TestExecCommand_Run(t *testing.T) {
5254
Names: []string{"c1", "c2", "c3"},
5355
Labels: []string{"key=value"},
5456
},
55-
command: "kill 1",
57+
command: "ls",
58+
args: []string{"-la"},
5659
},
5760
args: args{
5861
ctx: context.TODO(),
@@ -65,7 +68,8 @@ func TestExecCommand_Run(t *testing.T) {
6568
params: &chaos.GlobalParams{
6669
Pattern: "^c?",
6770
},
68-
command: "kill -STOP 1",
71+
command: "kill",
72+
args: []string{"-STOP", "1"},
6973
limit: 2,
7074
},
7175
args: args{
@@ -79,7 +83,8 @@ func TestExecCommand_Run(t *testing.T) {
7983
params: &chaos.GlobalParams{
8084
Names: []string{"c1", "c2", "c3"},
8185
},
82-
command: "kill 1",
86+
command: "kill",
87+
args: []string{"1"},
8388
},
8489
args: args{
8590
ctx: context.TODO(),
@@ -93,7 +98,8 @@ func TestExecCommand_Run(t *testing.T) {
9398
params: &chaos.GlobalParams{
9499
Names: []string{"c1", "c2", "c3"},
95100
},
96-
command: "kill 1",
101+
command: "kill",
102+
args: []string{"1"},
97103
},
98104
args: args{
99105
ctx: context.TODO(),
@@ -105,7 +111,8 @@ func TestExecCommand_Run(t *testing.T) {
105111
params: &chaos.GlobalParams{
106112
Names: []string{"c1", "c2", "c3"},
107113
},
108-
command: "kill 1",
114+
command: "kill",
115+
args: []string{"1"},
109116
},
110117
args: args{
111118
ctx: context.TODO(),
@@ -119,7 +126,8 @@ func TestExecCommand_Run(t *testing.T) {
119126
params: &chaos.GlobalParams{
120127
Names: []string{"c1", "c2", "c3"},
121128
},
122-
command: "kill 1",
129+
command: "kill",
130+
args: []string{"1"},
123131
},
124132
args: args{
125133
ctx: context.TODO(),
@@ -138,6 +146,7 @@ func TestExecCommand_Run(t *testing.T) {
138146
pattern: tt.fields.params.Pattern,
139147
labels: tt.fields.params.Labels,
140148
command: tt.fields.command,
149+
args: tt.fields.args,
141150
limit: tt.fields.limit,
142151
dryRun: tt.fields.params.DryRun,
143152
}
@@ -153,11 +162,11 @@ func TestExecCommand_Run(t *testing.T) {
153162
}
154163
}
155164
if tt.args.random {
156-
mockClient.On("ExecContainer", tt.args.ctx, mock.AnythingOfType("*container.Container"), tt.fields.command, tt.fields.params.DryRun).Return(nil)
165+
mockClient.On("ExecContainer", tt.args.ctx, mock.AnythingOfType("*container.Container"), tt.fields.command, tt.fields.args, tt.fields.params.DryRun).Return(nil)
157166
} else {
158167
for i := range tt.expected {
159168
if tt.fields.limit == 0 || i < tt.fields.limit {
160-
call = mockClient.On("ExecContainer", tt.args.ctx, mock.AnythingOfType("*container.Container"), tt.fields.command, tt.fields.params.DryRun)
169+
call = mockClient.On("ExecContainer", tt.args.ctx, mock.AnythingOfType("*container.Container"), tt.fields.command, tt.fields.args, tt.fields.params.DryRun)
161170
if tt.errs.execError {
162171
call.Return(errors.New("ERROR"))
163172
goto Invoke

pkg/container/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Client interface {
2222
ListContainers(context.Context, FilterFunc, ListOpts) ([]*Container, error)
2323
StopContainer(context.Context, *Container, int, bool) error
2424
KillContainer(context.Context, *Container, string, bool) error
25-
ExecContainer(context.Context, *Container, string, bool) error
25+
ExecContainer(context.Context, *Container, string, []string, bool) error
2626
RestartContainer(context.Context, *Container, time.Duration, bool) error
2727
RemoveContainer(context.Context, *Container, bool, bool, bool, bool) error
2828
NetemContainer(context.Context, *Container, string, []string, []*net.IPNet, []string, []string, time.Duration, string, bool, bool) error

0 commit comments

Comments
 (0)