test_indent.py 4.65 KB
Newer Older
Dmitry Volodin's avatar
Dmitry Volodin committed
1
2
3
4
5
6
7
8
9
10
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------
# indent tokenizer tests
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------

# Third-party modules
import pytest
Dmitry Volodin's avatar
Dmitry Volodin committed
11

Dmitry Volodin's avatar
Dmitry Volodin committed
12
# NOC modules
Dmitry Volodin's avatar
Dmitry Volodin committed
13
from noc.core.confdb.tokenizer.indent import IndentTokenizer
Dmitry Volodin's avatar
Dmitry Volodin committed
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


CFG1 = """# comment

interface Gi 0/1
    description testing interface
    ip address 10.0.0.1/24
    qos
        input input-map
        output output-map
#
interface Gi 0/2
    description testing interface 2
    ip address 10.0.1.1/24
    qos
        input input-map
        output output-map
"""

TOKENS1 = [
    ("interface", "Gi", "0/1"),
    ("interface", "Gi", "0/1", "description", "testing", "interface"),
    ("interface", "Gi", "0/1", "ip", "address", "10.0.0.1/24"),
    ("interface", "Gi", "0/1", "qos"),
    ("interface", "Gi", "0/1", "qos", "input", "input-map"),
    ("interface", "Gi", "0/1", "qos", "output", "output-map"),
    ("interface", "Gi", "0/2"),
    ("interface", "Gi", "0/2", "description", "testing", "interface", "2"),
    ("interface", "Gi", "0/2", "ip", "address", "10.0.1.1/24"),
    ("interface", "Gi", "0/2", "qos"),
    ("interface", "Gi", "0/2", "qos", "input", "input-map"),
Dmitry Volodin's avatar
Dmitry Volodin committed
45
    ("interface", "Gi", "0/2", "qos", "output", "output-map"),
Dmitry Volodin's avatar
Dmitry Volodin committed
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
]

CFG2 = """# Comment
interface Gi 0/1
    description testing interface
    ip address 10.0.0.1/24
    qos
        input input-map
        output output-map
        end
    end
#
interface Gi 0/2
    description testing interface 2
    ip address 10.0.1.1/24
    qos
        input input-map
        output output-map
        end
    end
"""

TOKENS2 = [
    ("interface", "Gi", "0/1"),
    ("interface", "Gi", "0/1", "description", "testing", "interface"),
    ("interface", "Gi", "0/1", "ip", "address", "10.0.0.1/24"),
    ("interface", "Gi", "0/1", "qos"),
    ("interface", "Gi", "0/1", "qos", "input", "input-map"),
    ("interface", "Gi", "0/1", "qos", "output", "output-map"),
    ("interface", "Gi", "0/2"),
    ("interface", "Gi", "0/2", "description", "testing", "interface", "2"),
    ("interface", "Gi", "0/2", "ip", "address", "10.0.1.1/24"),
    ("interface", "Gi", "0/2", "qos"),
    ("interface", "Gi", "0/2", "qos", "input", "input-map"),
Dmitry Volodin's avatar
Dmitry Volodin committed
80
    ("interface", "Gi", "0/2", "qos", "output", "output-map"),
Dmitry Volodin's avatar
Dmitry Volodin committed
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
CFG3 = """#
vlan 333
 description MGMT
 name MGMT
#
aaa
 authentication-scheme default
 authentication-scheme remote
  authentication-mode local radius
 authorization-scheme default
 accounting-scheme default
 domain default
 domain default_admin
  authentication-scheme remote
  radius-server aaa_server
 local-user root password irreversible-cipher %^%#XXXXXXX%#
 local-user root privilege level 15
 local-user root ftp-directory flash:
 local-user root service-type telnet terminal ssh ftp
#
ntp-service server disable"""

TOKENS3 = [
Dmitry Volodin's avatar
Dmitry Volodin committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    ("vlan", "333"),
    ("vlan", "333", "description", "MGMT"),
    ("vlan", "333", "name", "MGMT"),
    ("aaa",),
    ("aaa", "authentication-scheme", "default"),
    ("aaa", "authentication-scheme", "remote"),
    ("aaa", "authentication-scheme", "remote", "authentication-mode", "local", "radius"),
    ("aaa", "authorization-scheme", "default"),
    ("aaa", "accounting-scheme", "default"),
    ("aaa", "domain", "default"),
    ("aaa", "domain", "default_admin"),
    ("aaa", "domain", "default_admin", "authentication-scheme", "remote"),
    ("aaa", "domain", "default_admin", "radius-server", "aaa_server"),
    ("aaa", "local-user", "root", "password", "irreversible-cipher", "%^%#XXXXXXX%#"),
    ("aaa", "local-user", "root", "privilege", "level", "15"),
    ("aaa", "local-user", "root", "ftp-directory", "flash:"),
    ("aaa", "local-user", "root", "service-type", "telnet", "terminal", "ssh", "ftp"),
    ("ntp-service", "server", "disable"),
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
CFG4 = """! Test config
#
  http server
#
interface Fa 0/1
  description 1
#
interface Fa 0/2
  description 2
#
#
router ospf    
"""

TOKENS4 = [
    ("http", "server"),
    ("interface", "Fa", "0/1"),
    ("interface", "Fa", "0/1", "description", "1"),
    ("interface", "Fa", "0/2"),
    ("interface", "Fa", "0/2", "description", "2"),
    ("router", "ospf"),
]

Dmitry Volodin's avatar
Dmitry Volodin committed
149

Dmitry Volodin's avatar
Dmitry Volodin committed
150
151
152
153
154
155
@pytest.mark.parametrize(
    "input,config,expected",
    [
        (CFG1, {"line_comment": "#"}, TOKENS1),
        (CFG2, {"line_comment": "#", "end_of_context": "end"}, TOKENS2),
        (CFG3, {"line_comment": "#"}, TOKENS3),
156
        (CFG4, {"line_comment": "!", "end_of_context": "#"}, TOKENS4),
Dmitry Volodin's avatar
Dmitry Volodin committed
157
158
    ],
)
Dmitry Volodin's avatar
Dmitry Volodin committed
159
160
161
def test_tokenizer(input, config, expected):
    tokenizer = IndentTokenizer(input, **config)
    assert list(tokenizer) == expected