forked from miron0xff/vyatta-conf-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_vyattaconfparser.py
157 lines (150 loc) · 4.62 KB
/
test_vyattaconfparser.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
# coding:utf-8
import unittest
import re
import sys
import datadiff
from datadiff.tools import assert_equal
import vyattaconfparser as vparser
class TestBackupOspfRoutesEdgemax(unittest.TestCase):
def test_basic_parse_works_a1(self, dos_line_endings=False):
s = """interfaces {
ethernet eth0 {
address 192.168.0.2/24
address 192.168.1.2/24
description eth0-upstream
duplex auto
speed auto
}
ethernet eth1 {
address 192.168.2.2/24
description eth1-other
duplex auto
speed auto
}"""
if dos_line_endings:
s = s.replace('\n', '\r\n')
correct = {
'interfaces': {
'ethernet': {
'eth0': {
'address': ['192.168.0.2/24', '192.168.1.2/24'],
'description': 'eth0-upstream',
'duplex': 'auto',
'speed': 'auto'
},
'eth1': {
'address': '192.168.2.2/24',
'description': 'eth1-other',
'duplex': 'auto',
'speed': 'auto'
}
}
}
}
rv = vparser.parse_conf(s)
assert isinstance(rv, dict)
assert_equal(rv, correct)
def test_basic_parse_works_a1_dos_endings(self):
self.test_basic_parse_works_a1(dos_line_endings=True)
def test_parsing_quoted_config_vals(self):
s = """interfaces {
ethernet eth0 {
description "eth0-upstream 302.5-19a"
duplex auto
speed auto
}
}"""
correct = {
'interfaces': {
'ethernet': {
'eth0': {
'description': 'eth0-upstream 302.5-19a',
'duplex': 'auto',
'speed': 'auto'
}
}
}
}
rv = vparser.parse_conf(s)
assert isinstance(rv, dict)
assert_equal(rv, correct)
def test_parsing_quoted_config_vals_special_chars(self):
s = """interfaces {
ethernet eth0 {
description "eth0-upstream #302.5-19a (temp path)"
duplex auto
speed auto
}
}"""
correct = {
'interfaces': {
'ethernet': {
'eth0': {
'description': 'eth0-upstream #302.5-19a (temp path)',
'duplex': 'auto',
'speed': 'auto'
}
}
}
}
rv = vparser.parse_conf(s)
assert isinstance(rv, dict)
assert_equal(rv, correct)
## Future comment parsing (using '_comment' key -- parsed obj format not yet selected).
# def test_parsing_config_comments(self):
# s = """interfaces {
# ethernet eth0 {
# /* temp subnet -- chars NORMAL */
# address 192.168.0.2/24
# address 192.168.1.2/24
# duplex auto
# speed auto
# }
# }"""
# correct = {
# 'interfaces': {
# 'ethernet': {
# 'eth0': {
# 'address': {
# '192.168.0.2/24': {'_comment': 'temp subnet -- chars NORMAL'},
# '192.168.1.2/24': {}
# },
# 'duplex': 'auto',
# 'speed': 'auto'
# }
# }
# }
# }
# rv = vparser.parse_conf(s)
# assert isinstance(rv, dict)
# assert_equal(rv, correct)
#
# def test_parsing_config_comments_special_chars(self):
# s = """interfaces {
# ethernet eth0 {
# /* temp subnet -- chars Sw #23 (temp 2.3.9) */
# address 192.168.0.2/24
# address 192.168.1.2/24
# duplex auto
# speed auto
# }
# }"""
# correct = {
# 'interfaces': {
# 'ethernet': {
# 'eth0': {
# 'address': {
# '192.168.0.2/24': {'_comment': 'temp subnet -- chars Sw #23 (temp 2.3.9)'},
# '192.168.1.2/24': {}
# },
# 'duplex': 'auto',
# 'speed': 'auto'
# }
# }
# }
# }
# rv = vparser.parse_conf(s)
# assert isinstance(rv, dict)
# assert_equal(rv, correct)
if __name__ == "__main__":
unittest.main()