-
Notifications
You must be signed in to change notification settings - Fork 1
/
binarywrapper_test.go
284 lines (253 loc) · 7.3 KB
/
binarywrapper_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// SPDX-License-Identifier: Apache-2.0
// binarywrapper_test was chosen to be blackbox testing of the wrapper
// to try and better understand the behavior that end consumers of the wrapper
// might experience as they're using this. That, and there isn't much "internal"
// to whitebox test anyhow.
package binarywrapper_test
import (
"bytes"
"errors"
"fmt"
"os"
"strings"
"testing"
"github.com/go-vela/vela-openssh/pkg/binarywrapper"
"github.com/sirupsen/logrus"
)
const (
testMainEnvVar = "env-var"
testMainSuccessOutput = "success-output"
testMainFailOutput = "fail-output"
)
// TestMain is used so that we can mock calls to binaries that need
// to return specific output, or errors, exit codes, etc.
func TestMain(m *testing.M) {
switch os.Getenv("GO_MAIN_TEST_CASE") {
case "":
os.Exit(m.Run())
case testMainEnvVar:
if strings.Contains(strings.Join(os.Args, ""), "$") {
os.Exit(1)
}
os.Exit(0)
case testMainSuccessOutput:
if len(os.Args) != 4 {
fmt.Printf("invalid os.Args: %s", strings.Join(os.Args, " "))
os.Exit(2)
}
fmt.Println(os.Args[2])
fmt.Fprint(os.Stderr, os.Args[3])
os.Exit(0)
case testMainFailOutput:
if len(os.Args) != 4 {
fmt.Printf("invalid os.Args: %s", strings.Join(os.Args, " "))
os.Exit(3)
}
fmt.Println(os.Args[2])
fmt.Fprint(os.Stderr, os.Args[3])
os.Exit(4)
}
}
type mockExecConfig struct {
validationError string
setupError string
binaryPath string
arguments []string
environment map[string]string
}
func (m *mockExecConfig) Validate() error {
if m.validationError != "" {
return errors.New(m.validationError)
}
return nil
}
func (m *mockExecConfig) Setup() error {
if m.setupError != "" {
return errors.New(m.setupError)
}
return nil
}
func (m *mockExecConfig) Binary() string {
return m.binaryPath
}
func (m *mockExecConfig) Arguments() []string {
if len(m.arguments) > 0 {
return m.arguments
}
return []string{}
}
func (m *mockExecConfig) Environment() map[string]string {
if m.environment != nil {
return m.environment
}
return map[string]string{}
}
func TestExecSuccess(t *testing.T) {
tests := map[string]struct {
config *mockExecConfig
execStyle binarywrapper.ExecStyle
wantStdOut string
wantStdErr string
}{
"formats arguments with env vars before executing": {
execStyle: binarywrapper.OSExecCommand,
config: &mockExecConfig{
binaryPath: os.Args[0],
arguments: []string{"$SOME_TEST"},
environment: map[string]string{
"GO_MAIN_TEST_CASE": testMainEnvVar,
"SOME_TEST": "Howdy!",
},
},
},
"OSExecCommand captures stdout and stderr of successful run": {
execStyle: binarywrapper.OSExecCommand,
config: &mockExecConfig{
binaryPath: os.Args[0],
arguments: []string{"$VAR1", "$VAR2"},
environment: map[string]string{
"GO_MAIN_TEST_CASE": testMainSuccessOutput,
"VAR1": "stdout",
"VAR2": "stderr",
},
},
wantStdOut: "stdout",
wantStdErr: "stderr",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
p := binarywrapper.Plugin{}
p.ExecStyle = test.execStyle
p.PluginConfig = test.config
if test.execStyle == binarywrapper.SyscallExec && len(test.wantStdOut)+len(test.wantStdErr) > 0 {
t.Error("Exec() can't check output for the SyscallExec ExecStyle")
t.FailNow()
}
if test.execStyle > 0 {
p.ExecStyle = test.execStyle
}
// This is just so we can capture what output is coming back
// from the logging in our exec method.
var outputBuffer bytes.Buffer
logrus.SetOutput(&outputBuffer)
if err := p.Exec(); err != nil {
t.Errorf("Exec() should not have raised error %q", err)
t.FailNow()
}
if len(test.wantStdOut) > 0 && !strings.Contains(outputBuffer.String(), test.wantStdOut) {
t.Errorf("Exec() mismatch stdout\ngot: %s\nwanted: %s", outputBuffer.String(), test.wantStdOut)
t.FailNow()
}
if len(test.wantStdErr) > 0 && !strings.Contains(outputBuffer.String(), test.wantStdErr) {
t.Errorf("Exec() mismatch stderr\ngot: %s\nwanted: %s", outputBuffer.String(), test.wantStdErr)
}
})
}
}
func TestExecError(t *testing.T) {
tests := map[string]struct {
plugin *binarywrapper.Plugin
wantErr error
wantStdOut string
wantStdErr string
}{
"returns error when no plugin configured": {
wantErr: binarywrapper.ErrExec,
},
"returns error when Validate fails": {
plugin: func() *binarywrapper.Plugin {
p := binarywrapper.Plugin{
PluginConfig: &mockExecConfig{
validationError: "validation has failed",
},
}
return &p
}(),
wantErr: binarywrapper.ErrValidation,
},
"returns error when Setup fails": {
plugin: func() *binarywrapper.Plugin {
p := binarywrapper.Plugin{
PluginConfig: &mockExecConfig{
setupError: "setup has failed",
},
}
return &p
}(),
wantErr: binarywrapper.ErrSetup,
},
"returns error with unknown ExecStyle": {
plugin: func() *binarywrapper.Plugin {
p := binarywrapper.Plugin{
ExecStyle: -1,
PluginConfig: &mockExecConfig{},
}
return &p
}(),
wantErr: binarywrapper.ErrUnknownExecStyle,
},
"SyscallExec returns with error if binary missing": {
plugin: func() *binarywrapper.Plugin {
p := binarywrapper.Plugin{
ExecStyle: binarywrapper.SyscallExec,
PluginConfig: &mockExecConfig{binaryPath: "this-should-not-exist"},
}
return &p
}(),
wantErr: binarywrapper.ErrMissingBinary,
},
"OSExecCommand returns with error if binary missing": {
plugin: func() *binarywrapper.Plugin {
p := binarywrapper.Plugin{
ExecStyle: binarywrapper.OSExecCommand,
PluginConfig: &mockExecConfig{binaryPath: "this-should-not-exist"},
}
return &p
}(),
wantErr: binarywrapper.ErrExec,
},
"OSExecCommand captures stdout and stderr of failed run": {
plugin: func() *binarywrapper.Plugin {
p := binarywrapper.Plugin{
ExecStyle: binarywrapper.OSExecCommand,
PluginConfig: &mockExecConfig{
binaryPath: os.Args[0],
arguments: []string{"$VAR1", "$VAR2"},
environment: map[string]string{
"GO_MAIN_TEST_CASE": testMainFailOutput,
"VAR1": "stdout",
"VAR2": "stderr",
},
},
}
return &p
}(),
wantErr: binarywrapper.ErrExec,
wantStdOut: "stdout",
wantStdErr: "stderr",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
// This is just so we can capture what output is coming back
// from the logging in our exec method.
var outputBuffer bytes.Buffer
logrus.SetOutput(&outputBuffer)
if err := test.plugin.Exec(); err == nil {
t.Errorf("Exec() should have raised an error")
t.FailNow()
} else if test.wantErr != nil && err != nil && !errors.Is(err, test.wantErr) {
t.Errorf("Exec() returned wrong error\ngot: %s\nwanted: %s", err, test.wantErr)
t.FailNow()
}
if len(test.wantStdOut) > 0 && !strings.Contains(outputBuffer.String(), test.wantStdOut) {
t.Errorf("Exec() mismatch stdout\ngot: %s\nwanted: %s", outputBuffer.String(), test.wantStdOut)
t.FailNow()
}
if len(test.wantStdErr) > 0 && !strings.Contains(outputBuffer.String(), test.wantStdErr) {
t.Errorf("Exec() mismatch stderr\ngot: %s\nwanted: %s", outputBuffer.String(), test.wantStdErr)
}
})
}
}