-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_putty_session_manager.py
194 lines (160 loc) · 5.38 KB
/
test_putty_session_manager.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
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
import putty_session_manager as psm
from unittest import TestCase
DEFAULT_SESSION = 'test-case-default'
AUX_SESSION = 'test-case-aux'
NO_SUCH_SESSION = 'no_such_session'
def delete_session(session):
"""
Delete session if it exists
"""
try:
psm.delete(Container({'session': session}))
except psm.SessionNotFoundError as e:
print('"%s" does not exist' % (session))
def delete_default_session():
"""
Delete default session if it exists
"""
delete_session(DEFAULT_SESSION)
def delete_aux_session():
"""
Delete auxiliary session if it exists
"""
delete_session(AUX_SESSION)
def create_default_session():
"""
Creates default session if it does not exist.
"""
delete_default_session()
psm.create(Container({'session': DEFAULT_SESSION}))
class Container(object):
"""
A helper class to instantiate objects with attributes
from a given dictionary
"""
def __init__(self, dictionary):
for k, v in dictionary.items():
setattr(self, k, v)
class CreateTest(TestCase):
def test_create_session(self):
pass
def test_second_create_should_fail(self):
self.assertRaises(
psm.SessionAlreadyExistsError,
psm.create,
Container({'session': DEFAULT_SESSION})
)
def setUp(self):
create_default_session()
def tearDown(self):
delete_default_session()
class GetTest(TestCase):
def test_get_default_session(self):
create_default_session()
try:
psm.get(Container({'session': DEFAULT_SESSION}))
except Exception as err:
self.fail(
'%s raised unexpected error "%s": "%s"' \
% ('get', err.__class__.__name__, err)
)
def test_get_no_such_session(self):
self.assertRaises(
psm.SessionNotFoundError,
psm.get,
Container({'session': NO_SUCH_SESSION})
)
def tearDown(self):
delete_default_session()
class ListTest(TestCase):
def test_should_not_fail(self):
try:
psm.list(Container({}))
except Exception as err:
self.fail(
'%s raised unexpected error "%s": "%s"' \
% ('list', err.__class__.__name__, err)
)
class CopyAttrTest(TestCase):
def test_fail_on_wrong_from_session(self):
self.assertRaises(
psm.SessionNotFoundError,
psm.copy_attr,
Container({'from_session': NO_SUCH_SESSION,
'to_session_pattern': NO_SUCH_SESSION,
'attr_pattern': '*'})
)
def test_fail_on_wrong_to_session(self):
self.assertRaises(
psm.SessionNotFoundError,
psm.copy_attr,
Container({'from_session': DEFAULT_SESSION,
'to_session_pattern': NO_SUCH_SESSION,
'attr_pattern': '.*'})
)
def test_fail_when_attributes_not_found(self):
self.assertRaises(
psm.SessionAttributeNotFoundError,
psm.copy_attr,
Container({'from_session': DEFAULT_SESSION,
'to_session_pattern': DEFAULT_SESSION,
'attr_pattern': 'no_such_attribute'})
)
def test_copy_attr_should_not_fail(self):
try:
psm.copy(Container({'source': DEFAULT_SESSION,
'dest': AUX_SESSION}))
psm.copy_attr(Container({'from_session': DEFAULT_SESSION,
'to_session_pattern': AUX_SESSION,
'attr_pattern': '.*'}))
except Exception as e:
self.fail(
'%s raised unexpected error "%s": "%s"' \
% ('copy-attr', err.__class__.__name__, err)
)
def setUp(self):
create_default_session()
def tearDown(self):
delete_default_session()
delete_aux_session()
class CopyTest(TestCase):
def test_fail_on_wrong_src_session(self):
self.assertRaises(
psm.SessionNotFoundError,
psm.copy,
Container({'source': NO_SUCH_SESSION,
'dest': NO_SUCH_SESSION})
)
def test_copy_should_not_fail(self):
try:
psm.copy(Container({'source': DEFAULT_SESSION,
'dest': AUX_SESSION}))
psm.delete(Container({'session': AUX_SESSION}))
except Exception as e:
self.fail(
'%s raised unexpected error "%s": "%s"' \
% ('copy', err.__class__.__name__, err)
)
def setUp(self):
create_default_session()
def tearDown(self):
delete_aux_session()
delete_default_session()
class ComplexTest(TestCase):
def test_get_after_copy_should_work(self):
try:
psm.copy(Container({'source': DEFAULT_SESSION,
'dest': AUX_SESSION}))
psm.get(Container({'session': AUX_SESSION}))
except Exception as e:
self.fail(
'%s raised unexpected error "%s": "%s"' \
% ('copy', err.__class__.__name__, err)
)
def setUp(self):
create_default_session()
def tearDown(self):
delete_aux_session()
if __name__ == "__main__":
import unittest
unittest.main()