-
Notifications
You must be signed in to change notification settings - Fork 29
/
manssh_test.go
227 lines (202 loc) · 5.65 KB
/
manssh_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
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
package manssh
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
const (
mainConfigContent = `
Include %s/config.d/*
Host home1
hostname 192.168.1.11
Host main1
hostname 192.168.1.10
Host main2
hostname 192.168.1.20
user wen
port 77
Host main3
hostname 192.168.1.30
user ROOT
port 77
`
testConfigContent = `
Host *
port 22022
Host test1
hostname 192.168.2.10
user root
port 22
Host test2 main2
hostname 192.168.2.20
port 77
Host Test3
hostname 192.168.2.30
user ROOT
port 77
`
homeConfigContent = `
Host home1
hostname 192.168.3.10
user ROOT
port 77
Host home2
hostname 192.168.3.20
user root
port 77
Host home3
hostname 192.168.3.30
user ROOT
port 77
`
)
var (
configRootDir = filepath.Join(os.TempDir(), "manssh")
mainConfigPath = filepath.Join(configRootDir, "config")
testConfigPath = filepath.Join(configRootDir, "config.d", "test")
homeConfigPath = filepath.Join(configRootDir, "config.d", "home")
)
func initConfig() {
_ = os.MkdirAll(configRootDir, os.ModePerm)
_ = os.MkdirAll(filepath.Join(configRootDir, "config.d"), os.ModePerm)
if err := ioutil.WriteFile(mainConfigPath, []byte(fmt.Sprintf(mainConfigContent, configRootDir)), 0644); err != nil {
panic(err)
}
if err := ioutil.WriteFile(testConfigPath, []byte(testConfigContent), 0644); err != nil {
panic(err)
}
if err := ioutil.WriteFile(homeConfigPath, []byte(homeConfigContent), 0644); err != nil {
panic(err)
}
}
func TestList(t *testing.T) {
initConfig()
defer os.Remove(configRootDir)
hosts, err := List(mainConfigPath, ListOption{})
require.Nil(t, err)
require.Equal(t, 10, len(hosts))
hostMap := map[string]*HostConfig{}
for _, host := range hosts {
hostMap[host.Alias] = host
}
main2 := hostMap["main2"]
require.NotNil(t, main2)
require.Equal(t, 2, len(main2.OwnConfig))
require.Equal(t, 1, len(main2.ImplicitConfig))
require.Empty(t, main2.OwnConfig["port"])
require.Equal(t, "22022", main2.ImplicitConfig["port"])
require.Equal(t, "192.168.2.20", main2.OwnConfig["hostname"])
// FIXME when use INCLUDE load configs and them have the same alias config, there may be uncertain results here. I'll fix this later.
// home1 := hostMap["home1"]
// require.NotNil(t, home1)
// require.Equal(t, 3, len(home1.OwnConfig))
// require.Equal(t, 0, len(home1.ImplicitConfig))
// require.Equal(t, "77", home1.OwnConfig["port"])
// require.Equal(t, "ROOT", home1.OwnConfig["user"])
// require.Equal(t, "192.168.3.10", home1.OwnConfig["hostname"])
hosts, err = List(mainConfigPath, ListOption{
Keywords: []string{"Test"},
})
require.Nil(t, err)
require.Equal(t, 1, len(hosts))
hosts, err = List(mainConfigPath, ListOption{
Keywords: []string{"Test"},
IgnoreCase: true,
})
require.Nil(t, err)
require.Equal(t, 3, len(hosts))
}
func TestAdd(t *testing.T) {
initConfig()
defer os.Remove(configRootDir)
_, err := Add(mainConfigPath, &AddOption{
Path: testConfigPath,
Alias: "test1",
Connect: "xxx@1.2.3.4:11",
Config: map[string]string{},
})
require.NotNil(t, err)
host, err := Add(mainConfigPath, &AddOption{
Path: testConfigPath,
Alias: "test4",
Connect: "xxx@1.2.3.4",
Config: map[string]string{},
})
require.Nil(t, err)
require.Equal(t, "22022", host.ImplicitConfig["port"])
require.Equal(t, "1.2.3.4", host.OwnConfig["hostname"])
require.Equal(t, "xxx", host.OwnConfig["user"])
hosts, err := List(mainConfigPath, ListOption{})
require.Nil(t, err)
require.Equal(t, 11, len(hosts))
}
func TestUpdate(t *testing.T) {
initConfig()
defer os.Remove(configRootDir)
_, err := Update(mainConfigPath, &UpdateOption{
Alias: "test4",
Connect: "xxx@1.2.3.4:11",
})
require.NotNil(t, err)
host, err := Update(mainConfigPath, &UpdateOption{
Alias: "test1",
NewAlias: "test4",
Connect: "xxx@1.2.3.4:11",
Config: map[string]string{
"IdentifyFile": "~/.ssh/test4",
},
})
require.Nil(t, err)
require.Equal(t, "1.2.3.4", host.OwnConfig["hostname"])
require.Equal(t, "xxx", host.OwnConfig["user"])
require.Equal(t, "~/.ssh/test4", host.OwnConfig["identifyfile"])
require.Equal(t, "22022", host.ImplicitConfig["port"])
// FIXME when use INCLUDE load configs and them have the same alias config, there may be uncertain results here. I'll fix this later.
// host, err = Update(mainConfigPath, &UpdateOption{
// Alias: "home1",
// Connect: "1.2.3.4:11",
// Config: map[string]string{},
// })
// require.Nil(t, err)
// require.Equal(t, "1.2.3.4", host.OwnConfig["hostname"])
// require.Equal(t, "11", host.OwnConfig["port"])
hosts, err := List(mainConfigPath, ListOption{})
require.Nil(t, err)
require.Equal(t, 10, len(hosts))
hostMap := map[string]*HostConfig{}
for _, host := range hosts {
hostMap[host.Alias] = host
}
require.Nil(t, hostMap["test1"])
require.NotNil(t, hostMap["test4"])
}
func TestDelete(t *testing.T) {
initConfig()
defer os.Remove(configRootDir)
_, err := Delete(mainConfigPath, "home1", "test1", "main4")
require.NotNil(t, err)
hosts, err := Delete(mainConfigPath, "home1", "test1", "*")
require.Nil(t, err)
require.Equal(t, 3, len(hosts))
hosts, err = List(mainConfigPath, ListOption{})
require.Nil(t, err)
require.Equal(t, 8, len(hosts))
hostMap := map[string]*HostConfig{}
for _, host := range hosts {
hostMap[host.Alias] = host
}
require.Nil(t, hostMap["home1"])
require.Nil(t, hostMap["test1"])
require.NotNil(t, hostMap["*"])
require.Equal(t, "22", hostMap["main1"].ImplicitConfig["port"])
}
func TestGetFilePaths(t *testing.T) {
initConfig()
defer os.Remove(configRootDir)
paths, err := GetFilePaths(mainConfigPath)
require.Nil(t, err)
require.Equal(t, 3, len(paths))
}