forked from google/textfsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextfsm_test.py
executable file
·789 lines (648 loc) · 25.7 KB
/
textfsm_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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
#!/usr/bin/python
#
# Copyright 2010 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
"""Unittest for textfsm module."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import unittest
from six import StringIO
import textfsm
class UnitTestFSM(unittest.TestCase):
"""Tests the FSM engine."""
def testFSMValue(self):
# Check basic line is parsed.
line = r'Value beer (\S+)'
v = textfsm.TextFSMValue()
v.Parse(line)
self.assertEqual(v.name, 'beer')
self.assertEqual(v.regex, r'(\S+)')
self.assertEqual(v.template, r'(?P<beer>\S+)')
self.assertFalse(v.options)
# Test options
line = r'Value Filldown,Required beer (\S+)'
v = textfsm.TextFSMValue(options_class=textfsm.TextFSMOptions)
v.Parse(line)
self.assertEqual(v.name, 'beer')
self.assertEqual(v.regex, r'(\S+)')
self.assertEqual(v.OptionNames(), ['Filldown', 'Required'])
# Multiple parenthesis.
v = textfsm.TextFSMValue(options_class=textfsm.TextFSMOptions)
v.Parse('Value Required beer (boo(hoo))')
self.assertEqual(v.name, 'beer')
self.assertEqual(v.regex, '(boo(hoo))')
self.assertEqual(v.template, '(?P<beer>boo(hoo))')
self.assertEqual(v.OptionNames(), ['Required'])
# regex must be bounded by parenthesis.
self.assertRaises(textfsm.TextFSMTemplateError,
v.Parse,
'Value beer (boo(hoo)))boo')
self.assertRaises(textfsm.TextFSMTemplateError,
v.Parse,
'Value beer boo(boo(hoo)))')
self.assertRaises(textfsm.TextFSMTemplateError,
v.Parse,
'Value beer (boo)hoo)')
# String function.
v = textfsm.TextFSMValue(options_class=textfsm.TextFSMOptions)
v.Parse('Value Required beer (boo(hoo))')
self.assertEqual(str(v), 'Value Required beer (boo(hoo))')
v = textfsm.TextFSMValue(options_class=textfsm.TextFSMOptions)
v.Parse(
r'Value Required,Filldown beer (bo\S+(hoo))')
self.assertEqual(str(v), r'Value Required,Filldown beer (bo\S+(hoo))')
def testFSMRule(self):
# Basic line, no action
line = ' ^A beer called ${beer}'
r = textfsm.TextFSMRule(line)
self.assertEqual(r.match, '^A beer called ${beer}')
self.assertEqual(r.line_op, '')
self.assertEqual(r.new_state, '')
self.assertEqual(r.record_op, '')
# Multiple matches
line = ' ^A $hi called ${beer}'
r = textfsm.TextFSMRule(line)
self.assertEqual(r.match, '^A $hi called ${beer}')
self.assertEqual(r.line_op, '')
self.assertEqual(r.new_state, '')
self.assertEqual(r.record_op, '')
# Line with action.
line = ' ^A beer called ${beer} -> Next'
r = textfsm.TextFSMRule(line)
self.assertEqual(r.match, '^A beer called ${beer}')
self.assertEqual(r.line_op, 'Next')
self.assertEqual(r.new_state, '')
self.assertEqual(r.record_op, '')
# Line with record.
line = ' ^A beer called ${beer} -> Continue.Record'
r = textfsm.TextFSMRule(line)
self.assertEqual(r.match, '^A beer called ${beer}')
self.assertEqual(r.line_op, 'Continue')
self.assertEqual(r.new_state, '')
self.assertEqual(r.record_op, 'Record')
# Line with new state.
line = ' ^A beer called ${beer} -> Next.NoRecord End'
r = textfsm.TextFSMRule(line)
self.assertEqual(r.match, '^A beer called ${beer}')
self.assertEqual(r.line_op, 'Next')
self.assertEqual(r.new_state, 'End')
self.assertEqual(r.record_op, 'NoRecord')
# Bad syntax tests.
self.assertRaises(textfsm.TextFSMTemplateError, textfsm.TextFSMRule,
' ^A beer called ${beer} -> Next Next Next')
self.assertRaises(textfsm.TextFSMTemplateError, textfsm.TextFSMRule,
' ^A beer called ${beer} -> Boo.hoo')
self.assertRaises(textfsm.TextFSMTemplateError, textfsm.TextFSMRule,
' ^A beer called ${beer} -> Continue.Record $Hi')
def testImplicitDefaultRules(self):
for line in (' ^A beer called ${beer} -> Record End',
' ^A beer called ${beer} -> End',
' ^A beer called ${beer} -> Next.NoRecord End',
' ^A beer called ${beer} -> Clear End',
' ^A beer called ${beer} -> Error "Hello World"'):
r = textfsm.TextFSMRule(line)
self.assertEqual(str(r), line)
for line in (' ^A beer called ${beer} -> Next "Hello World"',
' ^A beer called ${beer} -> Record.Next',
' ^A beer called ${beer} -> Continue End',
' ^A beer called ${beer} -> Beer End'):
self.assertRaises(textfsm.TextFSMTemplateError,
textfsm.TextFSMRule, line)
def testSpacesAroundAction(self):
for line in (' ^Hello World -> Boo',
' ^Hello World -> Boo',
' ^Hello World -> Boo'):
self.assertEqual(
str(textfsm.TextFSMRule(line)), ' ^Hello World -> Boo')
# A '->' without a leading space is considered part of the matching line.
self.assertEqual(' A simple line-> Boo -> Next',
str(textfsm.TextFSMRule(' A simple line-> Boo -> Next')))
def testParseFSMVariables(self):
# Trivial template to initiate object.
f = StringIO('Value unused (.)\n\nStart\n')
t = textfsm.TextFSM(f)
# Trivial entry
buf = 'Value Filldown Beer (beer)\n\n'
f = StringIO(buf)
t._ParseFSMVariables(f)
# Single variable with commented header.
buf = '# Headline\nValue Filldown Beer (beer)\n\n'
f = StringIO(buf)
t._ParseFSMVariables(f)
self.assertEqual(str(t._GetValue('Beer')), 'Value Filldown Beer (beer)')
# Multiple variables.
buf = ('# Headline\n'
'Value Filldown Beer (beer)\n'
'Value Required Spirits (whiskey)\n'
'Value Filldown Wine (claret)\n'
'\n')
t._line_num = 0
f = StringIO(buf)
t._ParseFSMVariables(f)
self.assertEqual(str(t._GetValue('Beer')), 'Value Filldown Beer (beer)')
self.assertEqual(
str(t._GetValue('Spirits')), 'Value Required Spirits (whiskey)')
self.assertEqual(str(t._GetValue('Wine')), 'Value Filldown Wine (claret)')
# Multiple variables.
buf = ('# Headline\n'
'Value Filldown Beer (beer)\n'
' # A comment\n'
'Value Spirits ()\n'
'Value Filldown,Required Wine ((c|C)laret)\n'
'\n')
f = StringIO(buf)
t._ParseFSMVariables(f)
self.assertEqual(str(t._GetValue('Beer')), 'Value Filldown Beer (beer)')
self.assertEqual(
str(t._GetValue('Spirits')), 'Value Spirits ()')
self.assertEqual(str(t._GetValue('Wine')),
'Value Filldown,Required Wine ((c|C)laret)')
# Malformed variables.
buf = 'Value Beer (beer) beer'
f = StringIO(buf)
self.assertRaises(textfsm.TextFSMTemplateError, t._ParseFSMVariables, f)
buf = 'Value Filldown, Required Spirits ()'
f = StringIO(buf)
self.assertRaises(textfsm.TextFSMTemplateError, t._ParseFSMVariables, f)
buf = 'Value filldown,Required Wine ((c|C)laret)'
f = StringIO(buf)
self.assertRaises(textfsm.TextFSMTemplateError, t._ParseFSMVariables, f)
# Values that look bad but are OK.
buf = ('# Headline\n'
'Value Filldown Beer (bee(r), (and) (M)ead$)\n'
'# A comment\n'
'Value Spirits,and,some ()\n'
'Value Filldown,Required Wine ((c|C)laret)\n'
'\n')
f = StringIO(buf)
t._ParseFSMVariables(f)
self.assertEqual(str(t._GetValue('Beer')),
'Value Filldown Beer (bee(r), (and) (M)ead$)')
self.assertEqual(
str(t._GetValue('Spirits,and,some')), 'Value Spirits,and,some ()')
self.assertEqual(str(t._GetValue('Wine')),
'Value Filldown,Required Wine ((c|C)laret)')
# Variable name too long.
buf = ('Value Filldown '
'nametoolong_nametoolong_nametoolo_nametoolong_nametoolong '
'(beer)\n\n')
f = StringIO(buf)
self.assertRaises(textfsm.TextFSMTemplateError,
t._ParseFSMVariables, f)
def testParseFSMState(self):
f = StringIO('Value Beer (.)\nValue Wine (\\w)\n\nStart\n')
t = textfsm.TextFSM(f)
# Fails as we already have 'Start' state.
buf = 'Start\n ^.\n'
f = StringIO(buf)
self.assertRaises(textfsm.TextFSMTemplateError, t._ParseFSMState, f)
# Remove start so we can test new Start state.
t.states = {}
# Single state.
buf = '# Headline\nStart\n ^.\n\n'
f = StringIO(buf)
t._ParseFSMState(f)
self.assertEqual(str(t.states['Start'][0]), ' ^.')
try:
_ = t.states['Start'][1]
except IndexError:
pass
# Multiple states.
buf = '# Headline\nStart\n ^.\n ^Hello World\n ^Last-[Cc]ha$$nge\n'
f = StringIO(buf)
t._line_num = 0
t.states = {}
t._ParseFSMState(f)
self.assertEqual(str(t.states['Start'][0]), ' ^.')
self.assertEqual(str(t.states['Start'][1]), ' ^Hello World')
self.assertEqual(t.states['Start'][1].line_num, 4)
self.assertEqual(str(t.states['Start'][2]), ' ^Last-[Cc]ha$$nge')
try:
_ = t.states['Start'][3]
except IndexError:
pass
t.states = {}
# Malformed states.
buf = 'St%art\n ^.\n ^Hello World\n'
f = StringIO(buf)
self.assertRaises(textfsm.TextFSMTemplateError, t._ParseFSMState, f)
buf = 'Start\n^.\n ^Hello World\n'
f = StringIO(buf)
self.assertRaises(textfsm.TextFSMTemplateError, t._ParseFSMState, f)
buf = ' Start\n ^.\n ^Hello World\n'
f = StringIO(buf)
self.assertRaises(textfsm.TextFSMTemplateError, t._ParseFSMState, f)
# Multiple variables and substitution (depends on _ParseFSMVariables).
buf = ('# Headline\nStart\n ^.${Beer}${Wine}.\n'
' ^Hello $Beer\n ^Last-[Cc]ha$$nge\n')
f = StringIO(buf)
t.states = {}
t._ParseFSMState(f)
self.assertEqual(str(t.states['Start'][0]), ' ^.${Beer}${Wine}.')
self.assertEqual(str(t.states['Start'][1]), ' ^Hello $Beer')
self.assertEqual(str(t.states['Start'][2]), ' ^Last-[Cc]ha$$nge')
try:
_ = t.states['Start'][3]
except IndexError:
pass
t.states['bogus'] = []
# State name too long (>32 char).
buf = 'rnametoolong_nametoolong_nametoolong_nametoolong_nametoolo\n ^.\n\n'
f = StringIO(buf)
self.assertRaises(textfsm.TextFSMTemplateError, t._ParseFSMState, f)
def testInvalidStates(self):
# 'Continue' should not accept a destination.
self.assertRaises(textfsm.TextFSMTemplateError, textfsm.TextFSMRule,
'^.* -> Continue Start')
# 'Error' accepts a text string but "next' state does not.
self.assertEqual(str(textfsm.TextFSMRule(' ^ -> Error "hi there"')),
' ^ -> Error "hi there"')
self.assertRaises(textfsm.TextFSMTemplateError, textfsm.TextFSMRule,
'^.* -> Next "Hello World"')
def testRuleStartsWithCarrot(self):
f = StringIO(
'Value Beer (.)\nValue Wine (\\w)\n\nStart\n A Simple line')
self.assertRaises(textfsm.TextFSMTemplateError, textfsm.TextFSM, f)
def testValidateFSM(self):
# No Values.
f = StringIO('\nNotStart\n')
self.assertRaises(textfsm.TextFSMTemplateError, textfsm.TextFSM, f)
# No states.
f = StringIO('Value unused (.)\n\n')
self.assertRaises(textfsm.TextFSMTemplateError, textfsm.TextFSM, f)
# No 'Start' state.
f = StringIO('Value unused (.)\n\nNotStart\n')
self.assertRaises(textfsm.TextFSMTemplateError, textfsm.TextFSM, f)
# Has 'Start' state with valid destination
f = StringIO('Value unused (.)\n\nStart\n')
t = textfsm.TextFSM(f)
t.states['Start'] = []
t.states['Start'].append(textfsm.TextFSMRule('^.* -> Start'))
t._ValidateFSM()
# Invalid destination.
t.states['Start'].append(textfsm.TextFSMRule('^.* -> bogus'))
self.assertRaises(textfsm.TextFSMTemplateError, t._ValidateFSM)
# Now valid again.
t.states['bogus'] = []
t.states['bogus'].append(textfsm.TextFSMRule('^.* -> Start'))
t._ValidateFSM()
# Valid destination with options.
t.states['bogus'] = []
t.states['bogus'].append(textfsm.TextFSMRule('^.* -> Next.Record Start'))
t._ValidateFSM()
# Error with and without messages string.
t.states['bogus'] = []
t.states['bogus'].append(textfsm.TextFSMRule('^.* -> Error'))
t._ValidateFSM()
t.states['bogus'].append(textfsm.TextFSMRule('^.* -> Error "Boo hoo"'))
t._ValidateFSM()
def testTextFSM(self):
# Trivial template
buf = 'Value Beer (.*)\n\nStart\n ^\\w\n'
buf_result = buf
f = StringIO(buf)
t = textfsm.TextFSM(f)
self.assertEqual(str(t), buf_result)
# Slightly more complex, multple vars.
buf = 'Value A (.*)\nValue B (.*)\n\nStart\n ^\\w\n\nState1\n ^.\n'
buf_result = buf
f = StringIO(buf)
t = textfsm.TextFSM(f)
self.assertEqual(str(t), buf_result)
# Complex template, multiple vars and states with comments (no var options).
buf = r"""# Header
# Header 2
Value Beer (.*)
Value Wine (\w+)
# An explanation.
Start
^hi there ${Wine}. -> Next.Record State1
State1
^\w
^$Beer .. -> Start
# Some comments
^$$ -> Next
^$$ -> End
End
# Tail comment.
"""
buf_result = r"""Value Beer (.*)
Value Wine (\w+)
Start
^hi there ${Wine}. -> Next.Record State1
State1
^\w
^$Beer .. -> Start
^$$ -> Next
^$$ -> End
"""
f = StringIO(buf)
t = textfsm.TextFSM(f)
self.assertEqual(str(t), buf_result)
def testParseText(self):
# Trivial FSM, no records produced.
tplt = 'Value unused (.)\n\nStart\n ^Trivial SFM\n'
t = textfsm.TextFSM(StringIO(tplt))
data = 'Non-matching text\nline1\nline 2\n'
self.assertFalse(t.ParseText(data))
# Matching.
data = 'Matching text\nTrivial SFM\nline 2\n'
self.assertFalse(t.ParseText(data))
# Simple FSM, One Variable no options.
tplt = 'Value boo (.*)\n\nStart\n ^$boo -> Next.Record\n\nEOF\n'
t = textfsm.TextFSM(StringIO(tplt))
# Matching one line.
# Tests 'Next' & 'Record' actions.
data = 'Matching text'
result = t.ParseText(data)
self.assertEqual(str(result), "[['Matching text']]")
# Matching two lines. Reseting FSM before Parsing.
t.Reset()
data = 'Matching text\nAnd again'
result = t.ParseText(data)
self.assertEqual(str(result), "[['Matching text'], ['And again']]")
# Two Variables and singular options.
tplt = ('Value Required boo (one)\nValue Filldown hoo (two)\n\n'
'Start\n ^$boo -> Next.Record\n ^$hoo -> Next.Record\n\n'
'EOF\n')
t = textfsm.TextFSM(StringIO(tplt))
# Matching two lines. Only one records returned due to 'Required' flag.
# Tests 'Filldown' and 'Required' options.
data = 'two\none'
result = t.ParseText(data)
self.assertEqual(str(result), "[['one', 'two']]")
t = textfsm.TextFSM(StringIO(tplt))
# Matching two lines. Two records returned due to 'Filldown' flag.
data = 'two\none\none'
t.Reset()
result = t.ParseText(data)
self.assertEqual(
str(result), "[['one', 'two'], ['one', 'two']]")
# Multiple Variables and options.
tplt = ('Value Required,Filldown boo (one)\n'
'Value Filldown,Required hoo (two)\n\n'
'Start\n ^$boo -> Next.Record\n ^$hoo -> Next.Record\n\n'
'EOF\n')
t = textfsm.TextFSM(StringIO(tplt))
data = 'two\none\none'
result = t.ParseText(data)
self.assertEqual(
str(result), "[['one', 'two'], ['one', 'two']]")
def testParseNullText(self):
# Simple FSM, One Variable no options.
tplt = 'Value boo (.*)\n\nStart\n ^$boo -> Next.Record\n\n'
t = textfsm.TextFSM(StringIO(tplt))
# Null string
data = ''
result = t.ParseText(data)
self.assertEqual(result, [])
def testReset(self):
tplt = 'Value boo (.*)\n\nStart\n ^$boo -> Next.Record\n\nEOF\n'
t = textfsm.TextFSM(StringIO(tplt))
data = 'Matching text'
result1 = t.ParseText(data)
t.Reset()
result2 = t.ParseText(data)
self.assertEqual(str(result1), str(result2))
tplt = ('Value boo (one)\nValue hoo (two)\n\n'
'Start\n ^$boo -> State1\n\n'
'State1\n ^$hoo -> Start\n\n'
'EOF')
t = textfsm.TextFSM(StringIO(tplt))
data = 'one'
t.ParseText(data)
t.Reset()
self.assertEqual(t._cur_state[0].match, '^$boo')
self.assertEqual(t._GetValue('boo').value, None)
self.assertEqual(t._GetValue('hoo').value, None)
self.assertEqual(t._result, [])
def testClear(self):
# Clear Filldown variable.
# Tests 'Clear'.
tplt = ('Value Required boo (on.)\n'
'Value Filldown,Required hoo (tw.)\n\n'
'Start\n ^$boo -> Next.Record\n ^$hoo -> Next.Clear')
t = textfsm.TextFSM(StringIO(tplt))
data = 'one\ntwo\nonE\ntwO'
result = t.ParseText(data)
self.assertEqual(str(result), ("[['onE', 'two']]"))
# Clearall, with Filldown variable.
# Tests 'Clearall'.
tplt = ('Value Filldown boo (on.)\n'
'Value Filldown hoo (tw.)\n\n'
'Start\n ^$boo -> Next.Clearall\n'
' ^$hoo')
t = textfsm.TextFSM(StringIO(tplt))
data = 'one\ntwo'
result = t.ParseText(data)
self.assertEqual(str(result), ("[['', 'two']]"))
def testContinue(self):
tplt = ('Value Required boo (on.)\n'
'Value Filldown,Required hoo (on.)\n\n'
'Start\n ^$boo -> Continue\n ^$hoo -> Continue.Record')
t = textfsm.TextFSM(StringIO(tplt))
data = 'one\non0'
result = t.ParseText(data)
self.assertEqual(
str(result), ("[['one', 'one'], ['on0', 'on0']]"))
def testError(self):
tplt = ('Value Required boo (on.)\n'
'Value Filldown,Required hoo (on.)\n\n'
'Start\n ^$boo -> Continue\n ^$hoo -> Error')
t = textfsm.TextFSM(StringIO(tplt))
data = 'one'
self.assertRaises(textfsm.TextFSMError, t.ParseText, data)
tplt = ('Value Required boo (on.)\n'
'Value Filldown,Required hoo (on.)\n\n'
'Start\n ^$boo -> Continue\n ^$hoo -> Error "Hello World"')
t = textfsm.TextFSM(StringIO(tplt))
self.assertRaises(textfsm.TextFSMError, t.ParseText, data)
def testKey(self):
tplt = ('Value Required boo (on.)\n'
'Value Required,Key hoo (on.)\n\n'
'Start\n ^$boo -> Continue\n ^$hoo -> Record')
t = textfsm.TextFSM(StringIO(tplt))
self.assertTrue('Key' in t._GetValue('hoo').OptionNames())
self.assertTrue('Key' not in t._GetValue('boo').OptionNames())
def testList(self):
tplt = ('Value List boo (on.)\n'
'Value hoo (tw.)\n\n'
'Start\n ^$boo\n ^$hoo -> Next.Record\n\n'
'EOF')
t = textfsm.TextFSM(StringIO(tplt))
data = 'one\ntwo\non0\ntw0'
result = t.ParseText(data)
self.assertEqual(
str(result), ("[[['one'], 'two'], "
"[['on0'], 'tw0']]"))
tplt = ('Value List,Filldown boo (on.)\n'
'Value hoo (on.)\n\n'
'Start\n ^$boo -> Continue\n ^$hoo -> Next.Record\n\n'
'EOF')
t = textfsm.TextFSM(StringIO(tplt))
data = 'one\non0\non1'
result = t.ParseText(data)
self.assertEqual(
str(result), ("[[['one'], 'one'], "
"[['one', 'on0'], 'on0'], "
"[['one', 'on0', 'on1'], 'on1']]"))
tplt = ('Value List,Required boo (on.)\n'
'Value hoo (tw.)\n\n'
'Start\n ^$boo -> Continue\n ^$hoo -> Next.Record\n\n'
'EOF')
t = textfsm.TextFSM(StringIO(tplt))
data = 'one\ntwo\ntw2'
result = t.ParseText(data)
self.assertEqual(str(result), ("[[['one'], 'two']]"))
def testNestedMatching(self):
"""
Ensures that List-type values with nested regex capture groups are parsed correctly
as a list of dictionaries.
Additionaly, another value is used with the same group-name as one of the nested groups to ensure that
there are no conflicts when the same name is used.
"""
tplt = (
"Value List foo ((?P<name>\w+):\s+(?P<age>\d+)\s+(?P<state>\w{2})\s*)\n" # A nested group is called "name"
"Value name (\w+)\n\n" # A regular value is called "name"
"Start\n ^\s*${foo}\n ^\s*${name}\n ^\s*$$ -> Record" # "${name}" here refers to the Value called "name"
)
t = textfsm.TextFSM(StringIO(tplt))
data = " Bob: 32 NC\n Alice: 27 NY\n Jeff: 45 CA\nJulia\n\n" # Julia should be parsed as "name" separately
result = t.ParseText(data)
self.assertEqual(
str(result), (
"[[[{'name': 'Bob', 'age': '32', 'state': 'NC'}, "
"{'name': 'Alice', 'age': '27', 'state': 'NY'}, "
"{'name': 'Jeff', 'age': '45', 'state': 'CA'}], 'Julia']]"
))
def testNestedNameConflict(self):
tplt = (
# Two nested groups are called "name"
"Value List foo ((?P<name>\w+)\s+(?P<name>\w+):\s+(?P<age>\d+)\s+(?P<state>\w{2})\s*)\n"
"Start\n ^\s*${foo}\n ^\s*$$ -> Record"
)
self.assertRaises(textfsm.TextFSMTemplateError, textfsm.TextFSM, StringIO(tplt))
def testGetValuesByAttrib(self):
tplt = ('Value Required boo (on.)\n'
'Value Required,List hoo (on.)\n\n'
'Start\n ^$boo -> Continue\n ^$hoo -> Record')
# Explicit default.
t = textfsm.TextFSM(StringIO(tplt))
self.assertEqual(t.GetValuesByAttrib('List'), ['hoo'])
self.assertEqual(t.GetValuesByAttrib('Filldown'), [])
result = t.GetValuesByAttrib('Required')
result.sort()
self.assertEqual(result, ['boo', 'hoo'])
def testStateChange(self):
# Sinple state change, no actions
tplt = ('Value boo (one)\nValue hoo (two)\n\n'
'Start\n ^$boo -> State1\n\nState1\n ^$hoo -> Start\n\n'
'EOF')
t = textfsm.TextFSM(StringIO(tplt))
data = 'one'
t.ParseText(data)
self.assertEqual(t._cur_state[0].match, '^$hoo')
self.assertEqual('one', t._GetValue('boo').value)
self.assertEqual(None, t._GetValue('hoo').value)
self.assertEqual(t._result, [])
# State change with actions.
tplt = ('Value boo (one)\nValue hoo (two)\n\n'
'Start\n ^$boo -> Next.Record State1\n\n'
'State1\n ^$hoo -> Start\n\n'
'EOF')
t = textfsm.TextFSM(StringIO(tplt))
data = 'one'
t.ParseText(data)
self.assertEqual(t._cur_state[0].match, '^$hoo')
self.assertEqual(None, t._GetValue('boo').value)
self.assertEqual(None, t._GetValue('hoo').value)
self.assertEqual(t._result, [['one', '']])
def testEOF(self):
# Implicit EOF.
tplt = 'Value boo (.*)\n\nStart\n ^$boo -> Next\n'
t = textfsm.TextFSM(StringIO(tplt))
data = 'Matching text'
result = t.ParseText(data)
self.assertEqual(str(result), "[['Matching text']]")
# EOF explicitly suppressed in template.
tplt = 'Value boo (.*)\n\nStart\n ^$boo -> Next\n\nEOF\n'
t = textfsm.TextFSM(StringIO(tplt))
result = t.ParseText(data)
self.assertEqual(str(result), '[]')
# Implicit EOF suppressed by argument.
tplt = 'Value boo (.*)\n\nStart\n ^$boo -> Next\n'
t = textfsm.TextFSM(StringIO(tplt))
result = t.ParseText(data, eof=False)
self.assertEqual(str(result), '[]')
def testEnd(self):
# End State, EOF is skipped.
tplt = 'Value boo (.*)\n\nStart\n ^$boo -> End\n ^$boo -> Record\n'
t = textfsm.TextFSM(StringIO(tplt))
data = 'Matching text A\nMatching text B'
result = t.ParseText(data)
self.assertEqual(str(result), '[]')
# End State, with explicit Record.
tplt = 'Value boo (.*)\n\nStart\n ^$boo -> Record End\n'
t = textfsm.TextFSM(StringIO(tplt))
result = t.ParseText(data)
self.assertEqual(str(result), "[['Matching text A']]")
# EOF state transition is followed by implicit End State.
tplt = 'Value boo (.*)\n\nStart\n ^$boo -> EOF\n ^$boo -> Record\n'
t = textfsm.TextFSM(StringIO(tplt))
result = t.ParseText(data)
self.assertEqual(str(result), "[['Matching text A']]")
def testInvalidRegexp(self):
tplt = 'Value boo (.$*)\n\nStart\n ^$boo -> Next\n'
self.assertRaises(textfsm.TextFSMTemplateError,
textfsm.TextFSM, StringIO(tplt))
def testValidRegexp(self):
"""RegexObjects uncopyable in Python 2.6."""
tplt = 'Value boo (fo*)\n\nStart\n ^$boo -> Record\n'
t = textfsm.TextFSM(StringIO(tplt))
data = 'f\nfo\nfoo\n'
result = t.ParseText(data)
self.assertEqual(str(result), "[['f'], ['fo'], ['foo']]")
def testReEnteringState(self):
"""Issue 2. TextFSM should leave file pointer at top of template file."""
tplt = 'Value boo (.*)\n\nStart\n ^$boo -> Next Stop\n\nStop\n ^abc\n'
output_text = 'one\ntwo'
tmpl_file = StringIO(tplt)
t = textfsm.TextFSM(tmpl_file)
t.ParseText(output_text)
t = textfsm.TextFSM(tmpl_file)
t.ParseText(output_text)
def testFillup(self):
"""Fillup should work ok."""
tplt = """Value Required Col1 ([^-]+)
Value Fillup Col2 ([^-]+)
Value Fillup Col3 ([^-]+)
Start
^$Col1 -- -- -> Record
^$Col1 $Col2 -- -> Record
^$Col1 -- $Col3 -> Record
^$Col1 $Col2 $Col3 -> Record
"""
data = """
1 -- B1
2 A2 --
3 -- B3
"""
t = textfsm.TextFSM(StringIO(tplt))
result = t.ParseText(data)
self.assertEqual(
"[['1', 'A2', 'B1'], ['2', 'A2', 'B3'], ['3', '', 'B3']]",
str(result))
if __name__ == '__main__':
unittest.main()