-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ssh_alias.py
149 lines (141 loc) · 4.18 KB
/
test_ssh_alias.py
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
from ssh_alias import (
read_host_config,
read_host_configs,
write_host_config,
delete_host_config,
)
import os
from tempfile import NamedTemporaryFile
def test_read_host_configs():
"""
Test the read_host_configs function.
"""
lines = [
'Host foo',
' HostName foo.example.com',
' Port 23',
' User foouser',
'',
'host bar',
' hostname bar.example.com',
' port 24',
' user baruser',
' identityfile ~/.ssh/bar.key',
]
tf = NamedTemporaryFile(delete=False)
for line in lines:
tf.write("{0}\n".format(line))
tf.close()
configs = read_host_configs(tf.name)
assert len(configs) == 2
assert 'foo' in configs
foo_config = configs['foo']
assert foo_config['hostname'] == 'foo.example.com'
assert foo_config['port'] == '23'
assert foo_config['user'] == 'foouser'
assert 'identityfile' not in foo_config
assert 'bar' in configs
bar_config = configs['bar']
assert bar_config['hostname'] == 'bar.example.com'
assert bar_config['port'] == '24'
assert bar_config['user'] == 'baruser'
assert bar_config['identityfile'] == '~/.ssh/bar.key'
os.remove(tf.name)
def test_read_host_config():
"""
Test the read_host_config function.
"""
lines = [
'Host foo',
' HostName foo.example.com',
' Port 23',
' User foouser',
'',
'host bar',
' hostname bar.example.com',
' port 24',
' user baruser',
' identityfile ~/.ssh/bar.key',
]
tf = NamedTemporaryFile(delete=False)
for line in lines:
tf.write("{0}\n".format(line))
tf.close()
foo_config = read_host_config('foo', tf.name)
assert foo_config['hostname'] == 'foo.example.com'
assert foo_config['port'] == '23'
assert foo_config['user'] == 'foouser'
assert 'identityfile' not in foo_config
bar_config = read_host_config('bar', tf.name)
assert bar_config['hostname'] == 'bar.example.com'
assert bar_config['port'] == '24'
assert bar_config['user'] == 'baruser'
assert bar_config['identityfile'] == '~/.ssh/bar.key'
os.remove(tf.name)
def test_write_host_config():
""" Test the write_host_config function. """
lines = [
'Host foo',
' HostName foo.example.com',
' Port 23',
' User foouser',
]
tf = NamedTemporaryFile(delete=False)
for line in lines:
tf.write("{0}\n".format(line))
tf.close()
new_foo = {
'hostname': 'newfoo.example.com',
'port': '24',
'user': 'newfoouser',
'identityfile': 'newidfile',
}
write_host_config('foo', new_foo, tf.name)
lines = [ln.rstrip() for ln in open(tf.name)]
assert len(lines) == 5
assert lines[0] == 'host foo'
assert ' hostname newfoo.example.com' in lines[1:]
assert ' user newfoouser' in lines[1:]
assert ' port 24' in lines[1:]
assert ' identityfile newidfile' in lines[1:]
bar = {
'hostname': 'bar.example.com',
'port': '25',
'user': 'baruser',
}
write_host_config('bar', bar, tf.name)
lines = [ln.rstrip() for ln in open(tf.name)]
assert len(lines) == 10
assert lines[6] == 'host bar'
assert ' hostname bar.example.com' in lines[7:]
assert ' port 25' in lines[7:]
assert ' user baruser' in lines[7:]
os.remove(tf.name)
def test_delete_host_config():
"""
Test the delete_host_config function.
"""
lines = [
'Host foo',
' HostName foo.example.com',
' Port 23',
' User foouser',
'',
'host bar',
' hostname bar.example.com',
' port 24',
' user baruser',
' identityfile ~/.ssh/bar.key',
]
tf = NamedTemporaryFile(delete=False)
for line in lines:
tf.write("{0}\n".format(line))
tf.close()
delete_host_config('foo', tf.name)
lines = [ln.rstrip() for ln in open(tf.name)]
assert len(lines) == 5
assert lines[0] == 'host bar'
assert lines[1] == ' hostname bar.example.com'
assert lines[2] == ' port 24'
assert lines[3] == ' user baruser'
assert lines[4] == ' identityfile ~/.ssh/bar.key'