-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathexec_test.go
186 lines (183 loc) · 4.34 KB
/
exec_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package docker
import (
"context"
"errors"
"testing"
"github.com/alexei-led/pumba/pkg/chaos"
"github.com/alexei-led/pumba/pkg/container"
"github.com/stretchr/testify/mock"
)
func TestExecCommand_Run(t *testing.T) {
type wantErrors struct {
listError bool
execError bool
}
type fields struct {
params *chaos.GlobalParams
command string
args []string
limit int
}
type args struct {
ctx context.Context
random bool
}
tests := []struct {
name string
fields fields
args args
expected []*container.Container
wantErr bool
errs wantErrors
}{
{
name: "exec matching containers by names",
fields: fields{
params: &chaos.GlobalParams{
Names: []string{"c1", "c2"},
},
command: "kill",
args: []string{"-9"},
},
args: args{
ctx: context.TODO(),
},
expected: container.CreateTestContainers(3),
},
{
name: "exec matching labeled containers by names",
fields: fields{
params: &chaos.GlobalParams{
Names: []string{"c1", "c2", "c3"},
Labels: []string{"key=value"},
},
command: "ls",
args: []string{"-la"},
},
args: args{
ctx: context.TODO(),
},
expected: container.CreateLabeledTestContainers(3, map[string]string{"key": "value"}),
},
{
name: "exec matching containers by filter with limit",
fields: fields{
params: &chaos.GlobalParams{
Pattern: "^c?",
},
command: "kill",
args: []string{"-STOP", "1"},
limit: 2,
},
args: args{
ctx: context.TODO(),
},
expected: container.CreateTestContainers(3),
},
{
name: "exec random matching container by names",
fields: fields{
params: &chaos.GlobalParams{
Names: []string{"c1", "c2", "c3"},
},
command: "kill",
args: []string{"1"},
},
args: args{
ctx: context.TODO(),
random: true,
},
expected: container.CreateTestContainers(3),
},
{
name: "no matching containers by names",
fields: fields{
params: &chaos.GlobalParams{
Names: []string{"c1", "c2", "c3"},
},
command: "kill",
args: []string{"1"},
},
args: args{
ctx: context.TODO(),
},
},
{
name: "error listing containers",
fields: fields{
params: &chaos.GlobalParams{
Names: []string{"c1", "c2", "c3"},
},
command: "kill",
args: []string{"1"},
},
args: args{
ctx: context.TODO(),
},
wantErr: true,
errs: wantErrors{listError: true},
},
{
name: "error execing container",
fields: fields{
params: &chaos.GlobalParams{
Names: []string{"c1", "c2", "c3"},
},
command: "kill",
args: []string{"1"},
},
args: args{
ctx: context.TODO(),
},
expected: container.CreateTestContainers(3),
wantErr: true,
errs: wantErrors{execError: true},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockClient := new(container.MockClient)
k := &execCommand{
client: mockClient,
names: tt.fields.params.Names,
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,
}
opts := container.ListOpts{Labels: tt.fields.params.Labels}
call := mockClient.On("ListContainers", tt.args.ctx, mock.AnythingOfType("container.FilterFunc"), opts)
if tt.errs.listError {
call.Return(tt.expected, errors.New("ERROR"))
goto Invoke
} else {
call.Return(tt.expected, nil)
if tt.expected == nil {
goto Invoke
}
}
if tt.args.random {
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.args, tt.fields.params.DryRun)
if tt.errs.execError {
call.Return(errors.New("ERROR"))
goto Invoke
} else {
call.Return(nil)
}
}
}
}
Invoke:
if err := k.Run(tt.args.ctx, tt.args.random); (err != nil) != tt.wantErr {
t.Errorf("ExecCommand.Run() error = %v, wantErr %v", err, tt.wantErr)
}
mockClient.AssertExpectations(t)
})
}
}