summaryrefslogtreecommitdiff
path: root/reconfigure/tests/parsers
diff options
context:
space:
mode:
authorAndrew Shadura <andrew@shadura.me>2015-08-20 15:58:26 +0200
committerAndrew Shadura <andrew@shadura.me>2015-08-20 15:58:26 +0200
commitff1408420159488a106492ccd11dd234967029b6 (patch)
tree473420cee1c5229a427ec4cafead1aa6c0a26800 /reconfigure/tests/parsers
Imported Upstream version 0.1.29
Diffstat (limited to 'reconfigure/tests/parsers')
-rw-r--r--reconfigure/tests/parsers/__init__.py0
-rw-r--r--reconfigure/tests/parsers/base_test.py30
-rw-r--r--reconfigure/tests/parsers/bind9_tests.py55
-rw-r--r--reconfigure/tests/parsers/crontab_tests.py57
-rw-r--r--reconfigure/tests/parsers/exports_tests.py50
-rw-r--r--reconfigure/tests/parsers/ini_tests.py26
-rw-r--r--reconfigure/tests/parsers/iptables_tests.py61
-rw-r--r--reconfigure/tests/parsers/jsonparser_tests.py24
-rw-r--r--reconfigure/tests/parsers/nginx_tests.py38
-rw-r--r--reconfigure/tests/parsers/nsd_tests.py29
-rw-r--r--reconfigure/tests/parsers/squid_tests.py29
-rw-r--r--reconfigure/tests/parsers/ssv_tests.py39
12 files changed, 438 insertions, 0 deletions
diff --git a/reconfigure/tests/parsers/__init__.py b/reconfigure/tests/parsers/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/reconfigure/tests/parsers/__init__.py
diff --git a/reconfigure/tests/parsers/base_test.py b/reconfigure/tests/parsers/base_test.py
new file mode 100644
index 0000000..74c94b5
--- /dev/null
+++ b/reconfigure/tests/parsers/base_test.py
@@ -0,0 +1,30 @@
+import unittest
+
+
+class BaseParserTest (unittest.TestCase):
+ source = ""
+ parsed = None
+ parser = None
+
+ @property
+ def stringified(self):
+ return self.source
+
+ def test_parse(self):
+ if not self.__class__.parser:
+ return
+
+ nodetree = self.parser.parse(self.__class__.source)
+ if self.__class__.parsed != nodetree:
+ print 'TARGET: %s\n\nPARSED: %s' % (self.__class__.parsed, nodetree)
+ self.assertEquals(self.__class__.parsed, nodetree)
+
+ def test_stringify(self):
+ if not self.__class__.parser:
+ return
+
+ unparsed = self.parser.stringify(self.__class__.parsed)
+ a, b = self.stringified, unparsed
+ if a.split() != b.split():
+ print 'SOURCE: %s\n\nGENERATED: %s' % (a, b)
+ self.assertEquals(a.split(), b.split())
diff --git a/reconfigure/tests/parsers/bind9_tests.py b/reconfigure/tests/parsers/bind9_tests.py
new file mode 100644
index 0000000..0f2527e
--- /dev/null
+++ b/reconfigure/tests/parsers/bind9_tests.py
@@ -0,0 +1,55 @@
+from reconfigure.tests.parsers.base_test import BaseParserTest
+from reconfigure.parsers import BIND9Parser
+from reconfigure.nodes import *
+
+
+class BIND9ParserTest (BaseParserTest):
+ parser = BIND9Parser()
+ source = """p1 asd;
+
+sec {
+ s1p1 asd;
+ /*s1p2 wqe;*/
+
+ sec2 test {
+ ::1;
+ s2p1 qwe;
+ };
+};
+"""
+
+ @property
+ def stringified(self):
+ return """
+ p1 asd;
+
+sec {
+ s1p1 asd;
+
+ # s1p2 wqe;
+ sec2 test {
+ ::1;
+ s2p1 qwe;
+ };
+};
+"""
+
+ parsed = RootNode(
+ None,
+ PropertyNode('p1', 'asd'),
+ Node(
+ 'sec',
+ PropertyNode('s1p1', 'asd'),
+ Node(
+ 'sec2',
+ PropertyNode('', '::1'),
+ PropertyNode('s2p1', 'qwe'),
+ parameter='test',
+ comment='s1p2 wqe;',
+ ),
+ parameter=None,
+ )
+ )
+
+
+del BaseParserTest
diff --git a/reconfigure/tests/parsers/crontab_tests.py b/reconfigure/tests/parsers/crontab_tests.py
new file mode 100644
index 0000000..481a2f6
--- /dev/null
+++ b/reconfigure/tests/parsers/crontab_tests.py
@@ -0,0 +1,57 @@
+from reconfigure.parsers import CrontabParser
+from reconfigure.nodes import RootNode, Node, PropertyNode
+from reconfigure.tests.parsers.base_test import BaseParserTest
+
+
+class CrontabParserTest (BaseParserTest):
+ parser = CrontabParser()
+
+ source = '\n'.join(['#comment line',
+ '* * * * * date',
+ '@reboot ls -al',
+ '1 * 0 1 2 date -s',
+ 'NAME = TEST',
+ ])
+ parsed = RootNode(None,
+ children=[
+ Node('normal_task',
+ comment='comment line',
+ children=[
+ PropertyNode('minute', '*'),
+ PropertyNode('hour', '*'),
+ PropertyNode('day_of_month', '*'),
+ PropertyNode('month', '*'),
+ PropertyNode('day_of_week', '*'),
+ PropertyNode('command', 'date'),
+ ]
+ ),
+ Node('special_task',
+ children=[
+ PropertyNode('special', '@reboot'),
+ PropertyNode('command', 'ls -al'),
+ ]
+ ),
+ Node('normal_task',
+ children=[
+ PropertyNode('minute', '1'),
+ PropertyNode('hour', '*'),
+ PropertyNode('day_of_month', '0'),
+ PropertyNode('month', '1'),
+ PropertyNode('day_of_week', '2'),
+ PropertyNode('command', 'date -s'),
+ ]
+ ),
+ Node('env_setting',
+ children=[
+ PropertyNode('name', 'NAME'),
+ PropertyNode('value', 'TEST'),
+ ]
+ ),
+ ]
+ )
+# bad_source = '\n'.join(['* * * * dd', #Wrong line
+# ' = FAIL', #wrong line
+# ])
+
+
+del BaseParserTest
diff --git a/reconfigure/tests/parsers/exports_tests.py b/reconfigure/tests/parsers/exports_tests.py
new file mode 100644
index 0000000..e83552a
--- /dev/null
+++ b/reconfigure/tests/parsers/exports_tests.py
@@ -0,0 +1,50 @@
+from reconfigure.tests.parsers.base_test import BaseParserTest
+from reconfigure.parsers import ExportsParser
+from reconfigure.nodes import *
+
+
+class ExportsParserTest (BaseParserTest):
+ parser = ExportsParser()
+ source = """
+/another/exported/directory 192.168.0.3(rw,sync) \
+192.168.0.4(ro)
+# comment
+/one 192.168.0.1
+"""
+ parsed = RootNode(
+ None,
+ Node(
+ '/another/exported/directory',
+ Node(
+ 'clients',
+ Node(
+ '192.168.0.3',
+ PropertyNode('options', 'rw,sync')
+ ),
+ Node(
+ '192.168.0.4',
+ PropertyNode('options', 'ro')
+ ),
+ ),
+ ),
+ Node(
+ '/one',
+ Node(
+ 'clients',
+ Node(
+ '192.168.0.1',
+ PropertyNode('options', '')
+ ),
+ ),
+ comment='comment'
+ )
+ )
+
+ @property
+ def stringified(self):
+ return """/another/exported/directory\t192.168.0.3(rw,sync)\t192.168.0.4(ro)
+/one\t192.168.0.1\t# comment
+"""
+
+
+del BaseParserTest
diff --git a/reconfigure/tests/parsers/ini_tests.py b/reconfigure/tests/parsers/ini_tests.py
new file mode 100644
index 0000000..cdb3c02
--- /dev/null
+++ b/reconfigure/tests/parsers/ini_tests.py
@@ -0,0 +1,26 @@
+from reconfigure.tests.parsers.base_test import BaseParserTest
+from reconfigure.parsers import IniFileParser
+from reconfigure.nodes import *
+
+
+class IniParserTest (BaseParserTest):
+ parser = IniFileParser(sectionless=True)
+ source = """a=b
+
+[section1] ;section comment
+s1p1=asd ;comment 2
+s1p2=123
+"""
+ parsed = RootNode(None,
+ Node(None,
+ PropertyNode('a', 'b'),
+ ),
+ Node('section1',
+ PropertyNode('s1p1', 'asd', comment='comment 2'),
+ PropertyNode('s1p2', '123'),
+ comment='section comment'
+ ),
+ )
+
+
+del BaseParserTest
diff --git a/reconfigure/tests/parsers/iptables_tests.py b/reconfigure/tests/parsers/iptables_tests.py
new file mode 100644
index 0000000..21e09fa
--- /dev/null
+++ b/reconfigure/tests/parsers/iptables_tests.py
@@ -0,0 +1,61 @@
+from reconfigure.tests.parsers.base_test import BaseParserTest
+from reconfigure.parsers import IPTablesParser
+from reconfigure.nodes import *
+
+
+class IPTablesParserTest (BaseParserTest):
+ parser = IPTablesParser()
+ source = """*filter
+:INPUT ACCEPT [0:0]
+:FORWARD DROP [0:0]
+:OUTPUT ACCEPT [0:0]
+-A INPUT ! -s 202.54.1.2/32 -j DROP # test
+-A INPUT -m state --state NEW,ESTABLISHED -j ACCEPT
+COMMIT
+"""
+ parsed = RootNode(None,
+ Node('filter',
+ Node('INPUT',
+ PropertyNode('default', 'ACCEPT'),
+ Node('append',
+ Node('option',
+ Node('argument', PropertyNode('value', '202.54.1.2/32')),
+ PropertyNode('negative', True),
+ PropertyNode('name', 's')
+ ),
+ Node('option',
+ Node('argument', PropertyNode('value', 'DROP')),
+ PropertyNode('negative', False),
+ PropertyNode('name', 'j')
+ ),
+ comment='test'
+ ),
+ Node('append',
+ Node('option',
+ Node('argument', PropertyNode('value', 'state')),
+ PropertyNode('negative', False),
+ PropertyNode('name', 'm')
+ ),
+ Node('option',
+ Node('argument', PropertyNode('value', 'NEW,ESTABLISHED')),
+ PropertyNode('negative', False),
+ PropertyNode('name', 'state')
+ ),
+ Node('option',
+ Node('argument', PropertyNode('value', 'ACCEPT')),
+ PropertyNode('negative', False),
+ PropertyNode('name', 'j')
+ ),
+ ),
+ ),
+ Node('FORWARD',
+ PropertyNode('default', 'DROP'),
+ ),
+ Node('OUTPUT',
+ PropertyNode('default', 'ACCEPT'),
+ ),
+ )
+ )
+
+
+del BaseParserTest
diff --git a/reconfigure/tests/parsers/jsonparser_tests.py b/reconfigure/tests/parsers/jsonparser_tests.py
new file mode 100644
index 0000000..1c1312e
--- /dev/null
+++ b/reconfigure/tests/parsers/jsonparser_tests.py
@@ -0,0 +1,24 @@
+from reconfigure.tests.parsers.base_test import BaseParserTest
+from reconfigure.parsers import JsonParser
+from reconfigure.nodes import *
+
+
+class JsonParserTest (BaseParserTest):
+ parser = JsonParser()
+ source = """{
+ "p2": 123,
+ "s1": {
+ "s1p1": "qwerty"
+ }
+}
+"""
+
+ parsed = RootNode(None,
+ PropertyNode('p2', 123),
+ Node('s1',
+ PropertyNode('s1p1', 'qwerty'),
+ ),
+ )
+
+
+del BaseParserTest
diff --git a/reconfigure/tests/parsers/nginx_tests.py b/reconfigure/tests/parsers/nginx_tests.py
new file mode 100644
index 0000000..83fe8b2
--- /dev/null
+++ b/reconfigure/tests/parsers/nginx_tests.py
@@ -0,0 +1,38 @@
+from reconfigure.tests.parsers.base_test import BaseParserTest
+from reconfigure.parsers import NginxParser
+from reconfigure.nodes import *
+
+
+class NginxParserTest (BaseParserTest):
+ parser = NginxParser()
+ source = """p1 asd;
+
+sec {
+ s1p1 asd;
+ s1p2 wqe;
+
+ # test
+ sec2 test {
+ s2p1 qwe;
+ }
+}
+"""
+ parsed = RootNode(
+ None,
+ PropertyNode('p1', 'asd'),
+ Node(
+ 'sec',
+ PropertyNode('s1p1', 'asd'),
+ PropertyNode('s1p2', 'wqe'),
+ Node(
+ 'sec2',
+ PropertyNode('s2p1', 'qwe'),
+ parameter='test',
+ comment='test',
+ ),
+ parameter=None,
+ )
+ )
+
+
+del BaseParserTest
diff --git a/reconfigure/tests/parsers/nsd_tests.py b/reconfigure/tests/parsers/nsd_tests.py
new file mode 100644
index 0000000..54bd7a4
--- /dev/null
+++ b/reconfigure/tests/parsers/nsd_tests.py
@@ -0,0 +1,29 @@
+from reconfigure.tests.parsers.base_test import BaseParserTest
+from reconfigure.parsers import NSDParser
+from reconfigure.nodes import *
+
+
+class BIND9ParserTest (BaseParserTest):
+ parser = NSDParser()
+ source = """# asd
+ server:
+ ip4-only: no
+key:
+ name: "mskey"
+"""
+
+ parsed = RootNode(
+ None,
+ Node(
+ 'server',
+ PropertyNode('ip4-only', 'no'),
+ comment='asd'
+ ),
+ Node(
+ 'key',
+ PropertyNode('name', '"mskey"'),
+ )
+ )
+
+
+del BaseParserTest
diff --git a/reconfigure/tests/parsers/squid_tests.py b/reconfigure/tests/parsers/squid_tests.py
new file mode 100644
index 0000000..1896b5f
--- /dev/null
+++ b/reconfigure/tests/parsers/squid_tests.py
@@ -0,0 +1,29 @@
+from reconfigure.tests.parsers.base_test import BaseParserTest
+from reconfigure.parsers import SquidParser
+from reconfigure.nodes import *
+
+
+class SquidParserTest (BaseParserTest):
+ parser = SquidParser()
+ source = """# line1
+# long comment
+a\tbc
+efgh # line2
+"""
+ parsed = RootNode(None,
+ Node('line',
+ PropertyNode('name', 'a'),
+ Node('arguments',
+ PropertyNode('1', 'bc'),
+ ),
+ comment='line1\nlong comment',
+ ),
+ Node('line',
+ PropertyNode('name', 'efgh'),
+ Node('arguments'),
+ comment='line2',
+ ),
+ )
+
+
+del BaseParserTest
diff --git a/reconfigure/tests/parsers/ssv_tests.py b/reconfigure/tests/parsers/ssv_tests.py
new file mode 100644
index 0000000..4df9ff5
--- /dev/null
+++ b/reconfigure/tests/parsers/ssv_tests.py
@@ -0,0 +1,39 @@
+from reconfigure.tests.parsers.base_test import BaseParserTest
+from reconfigure.parsers import SSVParser
+from reconfigure.nodes import *
+
+
+class SSVParserTest (BaseParserTest):
+ parser = SSVParser(continuation='\\')
+ source = """# line1
+# long comment
+a\tbc\\
+\tdef
+efgh # line2
+"""
+ parsed = RootNode(
+ None,
+ Node(
+ 'line',
+ Node('token', PropertyNode('value', 'a')),
+ Node('token', PropertyNode('value', 'bc')),
+ Node('token', PropertyNode('value', 'def')),
+ comment='line1\nlong comment',
+ ),
+ Node(
+ 'line',
+ Node('token', PropertyNode('value', 'efgh')),
+ comment='line2',
+ ),
+ )
+
+ @property
+ def stringified(self):
+ return """# line1
+# long comment
+a\tbc\tdef
+efgh # line2
+"""
+
+
+del BaseParserTest