-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
exec_test.go
198 lines (191 loc) · 4.11 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
187
188
189
190
191
192
193
194
195
196
197
198
package runn
import (
"bytes"
"context"
"fmt"
"strings"
"testing"
"github.com/cli/safeexec"
"github.com/google/go-cmp/cmp"
"github.com/k1LoW/donegroup"
)
func TestExecRun(t *testing.T) {
if err := setScopes(ScopeAllowRunExec); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := setScopes(ScopeDenyRunExec); err != nil {
t.Fatal(err)
}
})
tests := []struct {
command string
stdin string
shell string
background bool
want map[string]any
}{
{"echo hello!!", "", "", false, map[string]any{
"stdout": "hello!!\n",
"stderr": "",
"exit_code": 0,
}},
{"cat", "hello!!", "", false, map[string]any{
"stdout": "hello!!",
"stderr": "",
"exit_code": 0,
}},
{"sleep 1000", "", "", true, map[string]any{}},
}
for _, tt := range tests {
t.Run(tt.command, func(t *testing.T) {
ctx, cancel := donegroup.WithCancel(context.Background())
t.Cleanup(cancel)
o, err := New()
if err != nil {
t.Fatal(err)
}
r := newExecRunner()
s := newStep(0, "stepKey", o, nil)
c := &execCommand{command: tt.command, stdin: tt.stdin, shell: tt.shell, background: tt.background}
if err := r.run(ctx, c, s); err != nil {
t.Error(err)
return
}
got := o.store.steps[0]
if diff := cmp.Diff(got, tt.want, nil); diff != "" {
t.Error(diff)
}
})
}
}
func TestExecShell(t *testing.T) {
if err := setScopes(ScopeAllowRunExec); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := setScopes(ScopeDenyRunExec); err != nil {
t.Fatal(err)
}
})
tests := []struct {
shell string
want string
}{
{"", execDefaultShell},
{"bash", "bash"},
{"sh", "sh"},
}
ctx := context.Background()
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
o, err := New()
if err != nil {
t.Fatal(err)
}
r := newExecRunner()
s := newStep(0, "stepKey", o, nil)
c := &execCommand{command: "echo $0", shell: tt.shell}
if err := r.run(ctx, c, s); err != nil {
t.Error(err)
return
}
want, err := safeexec.LookPath(tt.want)
if err != nil {
t.Fatal(err)
}
got, ok := o.store.steps[0]["stdout"].(string)
if !ok {
t.Fatal("stdout is not string")
}
if !strings.HasPrefix(got, want) {
t.Errorf("got %s, want %s", got, want)
}
})
}
}
func TestExecRunWithSecrets(t *testing.T) {
if err := setScopes(ScopeAllowRunExec); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := setScopes(ScopeDenyRunExec); err != nil {
t.Fatal(err)
}
})
tests := []struct {
vars map[string]any
secrets []string
command string
liveOutput bool
want map[string]any
wantStdout string
wantStderr string
}{
{
map[string]any{"message": "hello"},
[]string{"vars.message"},
"echo hello!!",
false,
map[string]any{
"stdout": "hello!!\n",
"stderr": "",
"exit_code": 0,
},
"",
"",
},
{
map[string]any{"message": "hello"},
[]string{"vars.message"},
"echo hello!!",
true,
map[string]any{
"stdout": "hello!!\n",
"stderr": "",
"exit_code": 0,
},
"*****!!\n",
"",
},
}
for _, tt := range tests {
t.Run(tt.command, func(t *testing.T) {
ctx, cancel := donegroup.WithCancel(context.Background())
t.Cleanup(cancel)
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
opts := []Option{
Stdout(stdout),
Stderr(stderr),
Secret(tt.secrets...),
}
for k, v := range tt.vars {
opts = append(opts, Var(k, v))
}
o, err := New(opts...)
if err != nil {
t.Fatal(err)
}
r := newExecRunner()
s := newStep(0, "stepKey", o, nil)
c := &execCommand{command: tt.command, liveOutput: tt.liveOutput}
if err := r.run(ctx, c, s); err != nil {
t.Error(err)
return
}
got := o.store.steps[0]
if diff := cmp.Diff(got, tt.want, nil); diff != "" {
t.Error(diff)
}
gotStdout := stdout.String()
if gotStdout != tt.wantStdout {
t.Errorf("got %s, want %s", gotStdout, tt.wantStdout)
}
gotStderr := stderr.String()
if gotStderr != tt.wantStderr {
t.Errorf("got %s, want %s", gotStderr, tt.wantStderr)
}
})
}
}