-
Notifications
You must be signed in to change notification settings - Fork 289
/
Copy pathhelpers.go
129 lines (111 loc) · 3.2 KB
/
helpers.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
package e2e
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
"testing"
)
// CommandResult captures the output from running an external command
type CommandResult struct {
Stdout []byte
Stderr []byte
ExitCode int
Err error
}
// Option is a func that sets optional attributes for a call. This does not need
// to be used directly, but instead option arguments are built from the
// functions in this package. WithX options set a value to that given in the
// argument; DefaultX options indicate that the value should be set to its
// default. When an API call is made options are processed in ther order they
// appear in the function call, so for a given argument X, a succession of WithX
// or DefaultX calls will result in the last call taking effect.
type Option func(*options)
type options struct {
withArgs []string
withEnv map[string]string
}
func getOpts(opt ...Option) options {
opts := options{}
for _, o := range opt {
if o != nil {
o(&opts)
}
}
return opts
}
const EnvToCheckSkip = "E2E_TESTS"
// RunCommand executes external commands on the system. Returns the results
// of running the provided command.
//
// RunCommand(context.Background(), "ls")
// RunCommand(context.Background(), "ls", WithArgs("-al", "/path"))
//
// CommandResult is always valid even if there is an error.
func RunCommand(ctx context.Context, command string, opt ...Option) *CommandResult {
var cmd *exec.Cmd
var outbuf, errbuf bytes.Buffer
opts := getOpts(opt...)
if opts.withArgs == nil {
cmd = exec.CommandContext(ctx, command)
} else {
cmd = exec.CommandContext(ctx, command, opts.withArgs...)
}
if opts.withEnv != nil {
cmd.Env = os.Environ()
for k, v := range opts.withEnv {
cmd.Env = append(cmd.Env, k+"="+v)
}
}
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf
err := cmd.Run()
var ee *exec.ExitError
var exitCode int
if errors.As(err, &ee) {
exitCode = ee.ExitCode()
}
return &CommandResult{
Stdout: outbuf.Bytes(),
Stderr: errbuf.Bytes(),
ExitCode: exitCode,
Err: err,
}
}
// WithArgs is an option to RunCommand that allows the user to specify arguments
// for the provided command. This option can be used multiple times in one command.
//
// RunCommand(context.Background(), "ls", WithArgs("-al"))
func WithArgs(args ...string) Option {
return func(o *options) {
if o.withArgs == nil {
o.withArgs = args
} else {
o.withArgs = append(o.withArgs, args...)
}
}
}
// WithEnv is an option to RunCommand that allows the user to specify environment variables
// to be set when running the command. This option can be used multiple times in one command.
//
// RunCommand(context.Background(), "ls", WithEnv("NAME", "VALUE"), WithEnv("NAME", "VALUE"))
func WithEnv(name string, value string) Option {
return func(o *options) {
if o.withEnv == nil {
o.withEnv = map[string]string{name: value}
} else {
o.withEnv[name] = value
}
}
}
// MaybeSkipTest is a check used at the start of the test to determine if the test should run
func MaybeSkipTest(t testing.TB) {
if _, ok := os.LookupEnv(EnvToCheckSkip); !ok {
t.Skip(fmt.Sprintf(
"Skipping test because environment variable '%s' is not set. This is needed for e2e tests.",
EnvToCheckSkip,
))
}
}