forked from k0sproject/rig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_test.go
124 lines (108 loc) · 2.95 KB
/
connection_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
package rig
import (
"bytes"
"fmt"
"io"
"testing"
"github.com/creasty/defaults"
"github.com/k0sproject/rig/exec"
"github.com/stretchr/testify/require"
)
type Host struct {
Connection
}
type mockClient struct {
commands []string
}
func (m *mockClient) Connect() error { return nil }
func (m *mockClient) Disconnect() {}
func (m *mockClient) Upload(_, _ string, _ ...exec.Option) error { return nil }
func (m *mockClient) IsWindows() bool { return false }
func (m *mockClient) ExecInteractive(_ string) error { return nil }
func (m *mockClient) String() string { return "mockclient" }
func (m *mockClient) Protocol() string { return "null" }
func (m *mockClient) IPAddress() string { return "127.0.0.1" }
func (m *mockClient) IsConnected() bool { return true }
func (m *mockClient) Exec(cmd string, opts ...exec.Option) error {
o := exec.Build(opts...)
cmd, err := o.Command(cmd)
if err != nil {
return err
}
m.commands = append(m.commands, cmd)
return nil
}
func (m *mockClient) ExecStreams(cmd string, stdin io.ReadCloser, stdout, stderr io.Writer, opts ...exec.Option) (waiter, error) {
return nil, fmt.Errorf("not implemented")
}
var stubSudofunc = func(in string) string {
return "sudo-goes-here " + in
}
func TestHostFunctions(t *testing.T) {
h := Host{
Connection: Connection{
Localhost: &Localhost{
Enabled: true,
},
},
}
require.NoError(t, defaults.Set(&h))
require.NoError(t, h.Connect())
require.Equal(t, "[local] localhost", h.String())
require.True(t, h.IsConnected())
require.Equal(t, "Local", h.Protocol())
require.Equal(t, "127.0.0.1", h.Address())
h.Disconnect()
require.False(t, h.IsConnected())
h = Host{
Connection: Connection{
SSH: &SSH{
Address: "127.0.0.1",
},
},
}
require.NoError(t, defaults.Set(&h))
require.Equal(t, "SSH", h.Protocol())
require.Equal(t, "127.0.0.1", h.Address())
}
func TestOutputWriter(t *testing.T) {
h := Host{
Connection: Connection{
Localhost: &Localhost{
Enabled: true,
},
},
}
require.NoError(t, defaults.Set(&h))
require.NoError(t, h.Connect())
var writer bytes.Buffer
require.NoError(t, h.Exec("echo hello world", exec.Writer(&writer)))
lt := "\n"
if h.IsWindows() {
lt = "\r\n"
}
require.Equal(t, "hello world"+lt, writer.String())
}
func TestGrouping(t *testing.T) {
mc := mockClient{}
h := Host{
Connection: Connection{
client: &mc,
sudofunc: stubSudofunc,
},
}
opts, args := GroupParams(h, "ls", 1, exec.HideOutput(), exec.Sudo(h))
require.Len(t, opts, 2)
require.Len(t, args, 3)
}
func TestSudo(t *testing.T) {
mc := mockClient{}
h := Host{
Connection: Connection{
client: &mc,
sudofunc: stubSudofunc,
},
}
require.NoError(t, h.Execf("ls %s", "/tmp", exec.Sudo(h)))
require.Contains(t, mc.commands, "sudo-goes-here ls /tmp")
}