-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_test.go
More file actions
127 lines (94 loc) · 2.31 KB
/
cmd_test.go
File metadata and controls
127 lines (94 loc) · 2.31 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
package simpleexec
import (
"bytes"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestCmd(t *testing.T) {
require := require.New(t)
testString := "this is a test string"
cmd := ParseCmd(fmt.Sprintf("echo -n '%v'", testString))
require.NotNil(cmd)
buf := bytes.NewBuffer(nil)
cmd.Stdout = buf
err := cmd.Start()
require.Nil(err)
err = cmd.Wait()
require.Nil(err)
require.Equal(testString, buf.String())
}
func TestBadCmd(t *testing.T) {
require := require.New(t)
cmd := ParseCmd(fmt.Sprintf("echoa 'Hello"))
require.Nil(cmd)
}
func TestPipe(t *testing.T) {
require := require.New(t)
testString := "this is a test 'string'"
cmd := ParseCmd(fmt.Sprintf(`echo -n "%v"`, testString))
require.NotNil(cmd)
pipedCmd := cmd.Pipe("sed -e 's/this is //g'")
require.NotNil(pipedCmd)
buf := bytes.NewBuffer(nil)
pipedCmd.Stdout = buf
err := pipedCmd.Start()
require.Nil(err)
err = pipedCmd.Wait()
require.Nil(err)
require.Equal("a test 'string'", buf.String())
}
func TestComplexPipe(t *testing.T) {
require := require.New(t)
testString := "this is a test 'string'"
cmd := ParseCmd(fmt.Sprintf(`echo -n "%v"`, testString))
require.NotNil(cmd)
pipedCmd := cmd.Pipe("sed -e 's/this is //g'").
Pipe(`sed -e "s/ 'string'//g"`).
Pipe(`sed -e "s/a /ab /g"`).
Pipe("wc -c")
require.NotNil(pipedCmd)
buf := bytes.NewBuffer(nil)
pipedCmd.Stdout = buf
err := pipedCmd.Start()
require.Nil(err)
err = pipedCmd.Wait()
require.Nil(err)
require.Equal("7\n", buf.String())
}
//FIXME: Currently requires manually checking for zombies
func TestForZombies(t *testing.T) {
start := time.Now()
for {
now := time.Now()
if now.Sub(start) > 2*time.Second {
break
}
TestComplexPipe(t)
time.Sleep(100 * time.Millisecond)
}
}
//TODO: Add more complex testing for parent error and child error
func TestWaitError(t *testing.T) {
require := require.New(t)
// First test single command error
cmd := ParseCmd("/bin/false")
require.NotNil(cmd)
buf := bytes.NewBuffer(nil)
cmd.Stdout = buf
err := cmd.Start()
require.Nil(err)
err = cmd.Wait()
require.NotNil(err)
}
func TestSu(t *testing.T) {
require := require.New(t)
buf := bytes.NewBuffer(nil)
cmd := ParseCmd("/bin/su -c 'ls' - guru")
cmd.Stdout = buf
require.NotNil(cmd)
cmd.Start()
cmd.Wait()
fmt.Println(buf.String())
}