-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhost_test.go
146 lines (130 loc) · 2.67 KB
/
host_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
package gossh
import (
"fmt"
"os"
"os/user"
"testing"
)
var sudopass string
var currentuser string
func init() {
sudopass = os.Getenv("SUDOPASS") // TODO - This is probably not a good idea
if sudopass == "" {
fmt.Println("###### Remember to set the env var SUDOPASS using \" export SUDOPASS=pwd\"")
}
u, _ := user.Current()
currentuser = u.Username
}
func TestNSpaces(t *testing.T) {
var tests = []struct {
in int
expect string
}{
{-9, ""},
{-2, ""},
{-1, ""},
{0, ""},
{1, " "},
{2, " │"},
{9, " │ │ │ │ "},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%d", test.in), func(t *testing.T) {
got := nSpaces(test.in)
if got != test.expect {
t.Errorf("value: got \"%s\" - expect \"%s\"", got, test.expect)
}
})
}
}
/*
func TestLocal(t *testing.T) {
l := local{sudopass: sudopass}
var tests = []struct {
cmd string
sudo bool
user string
stdin string
expect Response
}{
{
cmd: `echo "hello"`,
expect: Response{
Stdout: "hello",
Stderr: "",
ExitStatus: 0,
},
},
{
cmd: `echo -n "hello"`,
expect: Response{
Stdout: "hello",
Stderr: "",
ExitStatus: 0,
},
},
{
cmd: `somecommandthatdoesnotexist`,
expect: Response{
Stdout: "",
Stderr: "bash: somecommandthatdoesnotexist: command not found",
ExitStatus: 127,
},
},
{
cmd: `cat filethatdoesntexist`,
expect: Response{
Stdout: "",
Stderr: "cat: filethatdoesntexist: No such file or directory",
ExitStatus: 1,
},
},
{
cmd: `sed s/a/X/ | sed s/c/Z/`,
stdin: "abc",
expect: Response{
Stdout: "XbZ",
Stderr: "",
ExitStatus: 0,
},
},
{
cmd: `sed s/a/X/ | sed s/c/Z/`,
sudo: true,
user: "root",
stdin: "abc",
expect: Response{
Stdout: "XbZ",
Stderr: "",
ExitStatus: 0,
},
},
{
cmd: `ls /root`,
sudo: true,
expect: Response{
Stdout: "",
Stderr: "",
ExitStatus: 0,
},
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%s %s %v %s", test.cmd, test.stdin, test.sudo, test.user), func(t *testing.T) {
got, err := l.run(test.cmd, test.stdin, test.sudo, test.user)
if err != nil {
t.Errorf("errored: %v", err)
}
if got.Stdout != test.expect.Stdout {
t.Errorf("stdout: got \"%s\" - expect \"%s\"", got.Stdout, test.expect.Stdout)
}
if got.Stderr != test.expect.Stderr {
t.Errorf("stderr: got \"%s\" - expect \"%s\"", got.Stderr, test.expect.Stderr)
}
if got.ExitStatus != test.expect.ExitStatus {
t.Errorf("exitstatus: got \"%d\" - expect \"%d\"", got.ExitStatus, test.expect.ExitStatus)
}
})
}
}
*/