forked from microsoft/hcsshim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_test.go
More file actions
279 lines (235 loc) · 5.39 KB
/
Copy pathexec_test.go
File metadata and controls
279 lines (235 loc) · 5.39 KB
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
//go:build windows
package exec
import (
"context"
"io"
"os"
"path/filepath"
"testing"
"time"
"github.com/Microsoft/hcsshim/internal/conpty"
"github.com/Microsoft/hcsshim/internal/jobobject"
)
func TestExec(t *testing.T) {
// Exec a simple process and wait for exit.
e, err := New(
`C:\Windows\System32\ping.exe`,
"ping 127.0.0.1",
WithEnv(os.Environ()),
)
if err != nil {
t.Fatal(err)
}
err = e.Start()
if err != nil {
t.Fatalf("failed to start process: %v", err)
}
err = e.Wait()
if err != nil {
t.Fatalf("error waiting for process: %v", err)
}
t.Logf("exit code was: %d", e.ExitCode())
}
func TestExecWithDir(t *testing.T) {
// Test that the working directory is successfully set to whatever was passed in.
dir := t.TempDir()
e, err := New(
`C:\Windows\System32\cmd.exe`,
"cmd /c echo 'test' > test.txt",
WithDir(dir),
WithEnv(os.Environ()),
)
if err != nil {
t.Fatal(err)
}
err = e.Start()
if err != nil {
t.Fatalf("failed to start process: %v", err)
}
err = e.Wait()
if err != nil {
t.Fatalf("error waiting for process: %v", err)
}
if _, err := os.Stat(filepath.Join(dir, "test.txt")); err != nil {
t.Fatal(err)
}
t.Logf("exit code was: %d", e.ExitCode())
}
func TestExecStdinPowershell(t *testing.T) {
// Exec a powershell instance and test that we can write commands to stdin and receive the output from stdout.
e, err := New(
`C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe`,
"powershell",
WithStdio(true, false, true),
WithEnv(os.Environ()),
)
if err != nil {
t.Fatal(err)
}
err = e.Start()
if err != nil {
t.Fatalf("failed to start process: %v", err)
}
errChan := make(chan error)
go func() {
_, _ = io.Copy(os.Stdout, e.Stdout())
}()
go func() {
cmd := "ping 127.0.0.1\r\n"
if _, err := e.Stdin().Write([]byte(cmd)); err != nil {
errChan <- err
}
exit := "exit\r\n"
if _, err := e.Stdin().Write([]byte(exit)); err != nil {
errChan <- err
}
close(errChan)
}()
err = <-errChan
if err != nil {
t.Fatal(err)
}
waitChan := make(chan error)
go func() {
waitChan <- e.Wait()
}()
select {
case err := <-waitChan:
if err != nil {
t.Fatalf("error waiting for process: %v", err)
}
case <-time.After(time.Second * 10):
_ = e.Kill()
t.Fatal("timed out waiting for process to complete")
}
t.Logf("exit code was: %d", e.ExitCode())
}
func TestExecsWithJob(t *testing.T) {
// Test that we can assign processes to a job object at creation time.
job, err := jobobject.Create(context.Background(), nil)
if err != nil {
t.Fatal(err)
}
defer job.Close()
e, err := New(
`C:\Windows\System32\ping.exe`,
"ping -t 127.0.0.1",
WithJobObject(job),
WithStdio(false, false, false),
WithEnv(os.Environ()),
)
if err != nil {
t.Fatal(err)
}
err = e.Start()
if err != nil {
t.Fatalf("failed to start process: %v", err)
}
// Launch a second process and check pids.
e2, err := New(
`C:\Windows\System32\ping.exe`,
"ping -t 127.0.0.1",
WithJobObject(job),
WithStdio(false, false, false),
WithEnv(os.Environ()),
)
if err != nil {
t.Fatal(err)
}
err = e2.Start()
if err != nil {
t.Fatalf("failed to start process: %v", err)
}
pidMap := map[int]struct{}{
e.Pid(): {},
e2.Pid(): {},
}
pids, err := job.Pids()
if err != nil {
t.Fatal(err)
}
if len(pids) != 2 {
t.Fatalf("should be two pids in job object, got: %d. Pids: %+v", len(pids), pids)
}
for _, pid := range pids {
if _, ok := pidMap[int(pid)]; !ok {
t.Fatalf("failed to find pid %d in job object", pid)
}
}
err = e.Kill()
if err != nil {
t.Fatalf("error killing process: %v", err)
}
err = e2.Kill()
if err != nil {
t.Fatalf("error killing process: %v", err)
}
_ = e.Wait()
_ = e2.Wait()
if !e.Exited() {
t.Fatalf("Process %d should have exited after kill", e.Pid())
}
if !e2.Exited() {
t.Fatalf("Process %d should have exited after kill", e2.Pid())
}
}
func TestPseudoConsolePowershell(t *testing.T) {
// This test is fairly flaky on the Github CI but seems to run fine locally. Skip this for now to let contributions continue without a hitch
// and until we can replace this with a better suited test shortly.
//
// TODO(dcantah): Fix/find a better test here
t.Skip("Skipping flaky test")
cpty, err := conpty.Create(80, 20, 0)
if err != nil {
t.Fatal(err)
}
defer cpty.Close()
// Exec a powershell instance and test that we can write commands to the input side of the pty and receive data
// from the output end.
e, err := New(
`C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe`,
"powershell",
WithEnv(os.Environ()),
WithConPty(cpty),
)
if err != nil {
t.Fatal(err)
}
err = e.Start()
if err != nil {
t.Fatalf("failed to start process: %v", err)
}
errChan := make(chan error)
go func() {
_, _ = io.Copy(os.Stdout, cpty.OutPipe())
}()
go func() {
cmd := "ping 127.0.0.1\r\n"
if _, err := cpty.Write([]byte(cmd)); err != nil {
errChan <- err
}
exit := "exit\r\n"
if _, err := cpty.Write([]byte(exit)); err != nil {
errChan <- err
}
close(errChan)
}()
err = <-errChan
if err != nil {
t.Fatal(err)
}
waitChan := make(chan error)
go func() {
waitChan <- e.Wait()
}()
select {
case err := <-waitChan:
if err != nil {
t.Fatalf("error waiting for process: %v", err)
}
case <-time.After(time.Second * 10):
_ = e.Kill()
t.Fatal("timed out waiting for process to complete")
}
t.Logf("exit code was: %d", e.ExitCode())
}