-
Notifications
You must be signed in to change notification settings - Fork 23
/
command_linux_test.go
170 lines (135 loc) · 4.45 KB
/
command_linux_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
package cmd
import (
"bytes"
"context"
"os"
"os/exec"
"strings"
"syscall"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCommand_ExecuteStderr(t *testing.T) {
cmd := NewCommand(">&2 echo hello")
err := cmd.Execute()
assert.Nil(t, err)
assert.Equal(t, "hello\n", cmd.Stderr())
}
func TestCommand_WithTimeout(t *testing.T) {
cmd := NewCommand("sleep 0.1;", WithTimeout(1*time.Millisecond))
err := cmd.Execute()
assert.NotNil(t, err)
// Sadly a process can not be killed every time :(
containsMsg := strings.Contains(err.Error(), "timeout occurred and can not kill process with pid") || strings.Contains(err.Error(), "command timed out after 1ms")
assert.True(t, containsMsg)
}
func TestCommand_WithValidTimeout(t *testing.T) {
cmd := NewCommand("sleep 0.01;", WithTimeout(500*time.Millisecond))
err := cmd.Execute()
assert.Nil(t, err)
}
func TestCommand_WithWorkingDir(t *testing.T) {
setWorkingDir := func(c *Command) {
c.WorkingDir = "/tmp"
}
cmd := NewCommand("pwd", setWorkingDir)
cmd.Execute()
assert.Equal(t, "/tmp\n", cmd.Stdout())
}
func TestCommand_WithStandardStreams(t *testing.T) {
tmpFile, err := os.CreateTemp("/tmp", "stdout_")
require.NoError(t, err)
originalStdout := os.Stdout
os.Stdout = tmpFile
// Reset os.Stdout to its original value
defer func() {
os.Stdout = originalStdout
}()
cmd := NewCommand("echo hey", WithStandardStreams)
cmd.Execute()
r, err := os.ReadFile(tmpFile.Name())
require.NoError(t, err)
assert.Equal(t, "hey\n", string(r))
}
func TestCommand_WithoutTimeout(t *testing.T) {
cmd := NewCommand("sleep 0.001; echo hello", WithoutTimeout)
err := cmd.Execute()
assert.Nil(t, err)
assert.Equal(t, "hello\n", cmd.Stdout())
}
func TestCommand_WithInvalidDir(t *testing.T) {
cmd := NewCommand("echo hello", WithWorkingDir("/invalid"))
err := cmd.Execute()
assert.NotNil(t, err)
assert.Equal(t, "chdir /invalid: no such file or directory", err.Error())
}
func TestWithInheritedEnvironment(t *testing.T) {
os.Setenv("FROM_OS", "is on os")
os.Setenv("OVERWRITE", "is on os but should be overwritten")
defer func() {
os.Unsetenv("FROM_OS")
os.Unsetenv("OVERWRITE")
}()
c := NewCommand(
"echo $FROM_OS $OVERWRITE",
WithInheritedEnvironment(map[string]string{"OVERWRITE": "overwritten"}))
c.Execute()
assertEqualWithLineBreak(t, "is on os overwritten", c.Stdout())
}
func TestWithCustomStderr(t *testing.T) {
writer := bytes.Buffer{}
c := NewCommand(">&2 echo stderr; sleep 0.01; echo stdout;", WithCustomStderr(&writer))
c.Execute()
assertEqualWithLineBreak(t, "stderr", writer.String())
assertEqualWithLineBreak(t, "stdout", c.Stdout())
assertEqualWithLineBreak(t, "stderr", c.Stderr())
assertEqualWithLineBreak(t, "stderr\nstdout", c.Combined())
}
func TestWithCustomStdout(t *testing.T) {
writer := bytes.Buffer{}
c := NewCommand(">&2 echo stderr; sleep 0.01; echo stdout;", WithCustomStdout(&writer))
c.Execute()
assertEqualWithLineBreak(t, "stdout", writer.String())
assertEqualWithLineBreak(t, "stdout", c.Stdout())
assertEqualWithLineBreak(t, "stderr", c.Stderr())
assertEqualWithLineBreak(t, "stderr\nstdout", c.Combined())
}
func TestWithEnvironmentVariables(t *testing.T) {
c := NewCommand("echo $env", WithEnvironmentVariables(map[string]string{"env": "value"}))
c.Execute()
assertEqualWithLineBreak(t, "value", c.Stdout())
}
func TestCommand_WithContext(t *testing.T) {
// ensure legacy timeout is honored
cmd := NewCommand("sleep 3;", WithTimeout(1*time.Second))
err := cmd.Execute()
assert.NotNil(t, err)
assert.Equal(t, "command timed out after 1s", err.Error())
// set context timeout to 2 seconds to ensure
// context takes precedence over timeout
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
cmd = NewCommand("sleep 3;", WithTimeout(1*time.Second))
err = cmd.ExecuteContext(ctx)
assert.NotNil(t, err)
assert.Equal(t, "context deadline exceeded", err.Error())
}
func TestCommand_WithCustomBaseCommand(t *testing.T) {
cmd := NewCommand(
"echo $0",
WithCustomBaseCommand(exec.Command("/bin/bash", "-c")),
)
err := cmd.Execute()
assert.Nil(t, err)
// on darwin we use /bin/sh by default test if we're using bash
assert.NotEqual(t, "/bin/sh\n", cmd.Stdout())
assert.Equal(t, "/bin/bash\n", cmd.Stdout())
}
func TestCommand_WithUser(t *testing.T) {
cred := syscall.Credential{}
cmd := NewCommand("echo hello", WithUser(cred))
err := cmd.Execute()
assert.Error(t, err)
}