-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjunos_test.py
executable file
·241 lines (216 loc) · 9.17 KB
/
junos_test.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/python
"""Tests for JunOS devices."""
import tempfile
import textwrap
import mox
import unittest
import junos
import paramiko_device
import push_exceptions as exceptions
class JunosTest(unittest.TestCase):
def setUp(self):
self._mox = mox.Mox()
self.device = junos.JunosDevice(host='pr01.dub01')
def tearDown(self):
self._mox.UnsetStubs()
self._mox.ResetAll()
self._mox.UnsetStubs()
def testGetConfigSuccessfulConfigTransfer(self):
self._mox.StubOutWithMock(paramiko_device.ParamikoDevice, '_Cmd')
paramiko_device.ParamikoDevice._Cmd(
'show configuration', mode=None, merge_stderr_first=False,
require_low_chanid=True).AndReturn(
'Some configuration\n response.')
self._mox.ReplayAll()
response = self.device._GetConfig('running-config')
self._mox.VerifyAll()
self.assertEquals('Some configuration\n response.', response)
def testGetConfigFailedConfigTransfer(self):
self._mox.StubOutWithMock(paramiko_device.ParamikoDevice, '_Cmd')
paramiko_device.ParamikoDevice._Cmd(
'show configuration', mode=None, merge_stderr_first=False,
require_low_chanid=True).AndRaise(exceptions.CmdError)
self._mox.ReplayAll()
self.assertRaises(exceptions.GetConfigError, self.device._GetConfig,
'running-config')
self._mox.VerifyAll()
def testGetConfigEmptyConfigTransfer(self):
self._mox.StubOutWithMock(paramiko_device.ParamikoDevice, '_Cmd')
paramiko_device.ParamikoDevice._Cmd(
'show configuration', mode=None, merge_stderr_first=False,
require_low_chanid=True).AndReturn('')
self._mox.ReplayAll()
self.assertRaises(exceptions.EmptyConfigError, self.device._GetConfig,
'running-config')
self._mox.VerifyAll()
def testGetConfigSuccessfulFileTransfer(self):
tempfile_ptr = tempfile.NamedTemporaryFile()
tempfile_ptr.write('Fake file content.')
tempfile_ptr.seek(0)
self._mox.StubOutWithMock(tempfile, 'NamedTemporaryFile')
self._mox.StubOutWithMock(junos.JunosDevice, '_GetFileViaSftp')
tempfile.NamedTemporaryFile().AndReturn(tempfile_ptr)
self.device._GetFileViaSftp(local_filename=tempfile_ptr.name,
remote_filename='/var/tmp/testfile')
self._mox.ReplayAll()
response = self.device._GetConfig('/var/tmp/testfile')
self._mox.VerifyAll()
self.assertEquals('Fake file content.', response)
def testGetConfigFailedFileTransfer(self):
tempfile_ptr = tempfile.NamedTemporaryFile()
self._mox.StubOutWithMock(tempfile, 'NamedTemporaryFile')
self._mox.StubOutWithMock(junos.JunosDevice, '_GetFileViaSftp')
tempfile.NamedTemporaryFile().AndReturn(tempfile_ptr)
self.device._GetFileViaSftp(
local_filename=tempfile_ptr.name,
remote_filename='/var/tmp/testfile').AndRaise(IOError)
self._mox.ReplayAll()
self.assertRaises(exceptions.GetConfigError, self.device._GetConfig,
'/var/tmp/testfile')
self._mox.VerifyAll()
def testCleanupErrorLine(self):
self.assertEquals('', self.device._CleanupErrorLine(''))
self.assertEquals('a', self.device._CleanupErrorLine('a'))
self.assertEquals('invalid value ',
self.device._CleanupErrorLine(
'invalid value \'257\' in ip address: \'257.0.0.0'))
self.assertEquals('description ',
self.device._CleanupErrorLine('description "foo";'))
self.assertEquals(
'', self.device._CleanupErrorLine('+ description "error: foo";'))
self.assertEquals(
'', self.device._CleanupErrorLine('- description "1 errors";'))
self.assertEquals(
'', self.device._CleanupErrorLine('! description "error foo";'))
self.assertEquals('foo -1', self.device._CleanupErrorLine('foo -1'))
def testLoadErrors(self):
# Make an alias for the function under test, _RaiseExceptionIfLoadError,
# because writing "self.device._RaiseExceptionIfLoadError" is verbose.
test_function = self.device._RaiseExceptionIfLoadError
# Check some non-throwing cases.
self.assertTrue(test_function('') is None)
self.assertTrue(test_function('', expect_config_check=True) is None)
self.assertTrue(
test_function('+ description "error: syntax error";',
expect_config_check=True)
is None)
self.assertTrue(
test_function('! description "error: syntax error";',
expect_config_check=True)
is None)
self.assertTrue(test_function('[edit ... ]') is None)
self.assertTrue(test_function('[edit ... ]\n error: foo') is None)
self.assertTrue(test_function('[edit ... ]\n+ error: foo') is None)
missing_re_output = textwrap.dedent("""\
Entering configuration mode
load complete
error: Could not connect to re1 : No route to host
warning: Cannot connect to other RE, ignoring it
commit complete
Exiting configuration mode
""")
self.assertTrue(test_function(missing_re_output, expect_commit=True)
is None)
# This is a successful commit.
warning_output = textwrap.dedent("""\
[edit]
Entering configuration mode
'interfaces'
warning: statement has no contents; ignored
load complete
commit complete
Exiting configuration mode
""")
self.assertIsNone(
test_function(warning_output, expect_config_check=False,
expect_commit=True))
# Also a successful commit from a switch-type device.
output = textwrap.dedent("""\
Entering configuration mode
|load complete
configuration check succeedscommit complete
Exiting configuration mode
""")
self.assertIsNone(
test_function(output, expect_config_check=True, expect_commit=True))
# Check throwing cases.
self.assertRaises(
exceptions.SetConfigSyntaxError,
test_function, 'foo\n syntax error: ')
self.assertRaises(
exceptions.SetConfigError,
test_function, ' load failed (1 errors)')
self.assertRaises(
exceptions.SetConfigError,
test_function, ' load complete (1 errors)')
self.assertRaises(
exceptions.SetConfigError,
test_function, '[edit ...]\n syntax error\nerror: foo')
self.assertRaises(
exceptions.SetConfigError,
test_function, 'error: configuration check-out failed')
self.assertRaises(
exceptions.SetConfigSyntaxError,
test_function, 'syntax error: "connect to re1 :"')
self.assertRaises(
exceptions.SetConfigSyntaxError,
test_function, 'syntax error: connect to re1 :')
# Check all JUNOS_LOAD_ERRORS strings
for error in self.device.JUNOS_LOAD_ERRORS:
self.assertRaises(exceptions.SetConfigError,
test_function, error)
self.assertRaises(exceptions.SetConfigError,
test_function, error, expect_commit=True)
# Check the commit_check parameter.
self.assertRaises(
exceptions.SetConfigSyntaxError,
test_function, ' load failed (1 errors)', expect_config_check=True)
self.assertRaises(
exceptions.SetConfigSyntaxError,
test_function, 'error:\nsyntax error', expect_config_check=True)
self.assertRaises(
exceptions.SetConfigError,
test_function, 'configuration check succeeds\n(1 errors)',
expect_config_check=True)
self.assertRaises(
exceptions.SetConfigSyntaxError,
test_function, '\'configuration check succeeds\'\nerror:',
expect_config_check=True)
# Check that we don't raise a syntax error just because someone wrote
# "syntax error" in a description.
self.assertRaises(
exceptions.SetConfigError,
test_function, '+description "syntax error";\nerror:')
syntax_error_with_missing_re = textwrap.dedent("""
Entering configuration mode
Users currently editing the configuration:
netops terminal p3 (pid 44448) on since 2012-09-05 10:00:49 PDT, ...
[edit]
netops terminal p4 (pid 63408) on since 2012-09-16 23:57:00 PDT, ...
private [edit]
|\x08tmpPjACa3:1:(10) syntax error: deactivate
load complete (1 errors)
error: Could not connect to re1 : No route to host
warning: Cannot connect to other RE, ignoring it
commit complete
Exiting configuration mode
""")
self.assertRaises(exceptions.SetConfigSyntaxError,
test_function, syntax_error_with_missing_re,
expect_config_check=False,
expect_commit=False)
# Also do the test with commit_check=True
self.assertRaises(exceptions.SetConfigSyntaxError,
test_function, syntax_error_with_missing_re,
expect_config_check=False, expect_commit=True)
failed_commit_b_9750034 = textwrap.dedent("""\
re0:
error: Could not connect to re1 : No route to host
[edit]
""")
self.assertRaises(
exceptions.SetConfigError,
test_function, failed_commit_b_9750034, expect_config_check=False,
expect_commit=True)
if __name__ == '__main__':
unittest.main()