keepalived-formula/test/test_keepalived_config.py

119 lines
5.1 KiB
Python
Raw Normal View History

2017-01-19 01:11:23 +01:00
#!/usr/bin/python
import os, unittest
from jinja2 import Environment, FileSystemLoader
class TestKeepalivedConfiguration(unittest.TestCase):
def setUp(self):
self.t_dir = os.path.abspath(os.path.join(
os.path.dirname(__file__),
os.pardir,
'keepalived',
'templates'))
self.t_conf = Environment(loader=FileSystemLoader(self.t_dir),
2017-01-19 03:29:15 +01:00
trim_blocks=True)
2017-01-19 01:11:23 +01:00
2017-01-19 01:51:16 +01:00
def renderTest(self, data, result):
2017-01-19 01:11:23 +01:00
holder = self.t_conf.get_template('test_config.jinja').render(testdata=data)
2017-01-19 01:51:16 +01:00
output = repr(holder) + ' did not equal ' + repr(result)
self.assertEqual(holder, result, output)
2017-01-19 01:11:23 +01:00
def test_string(self):
testdata = 'stuff'
result = 'stuff\n'
2017-01-19 01:51:16 +01:00
self.renderTest(testdata, result)
2017-01-19 01:11:23 +01:00
def test_number(self):
testdata = 3
result = '3\n'
2017-01-19 01:51:16 +01:00
self.renderTest(testdata, result)
2017-01-19 01:11:23 +01:00
def test_null(self):
testdata = None
result = '\n'
2017-01-19 01:51:16 +01:00
self.renderTest(testdata, result)
def test_key_value_pair(self):
testdata = {'flintstone': 'fred'}
result = 'flintstone fred\n'
self.renderTest(testdata, result)
2017-01-19 01:11:23 +01:00
2017-01-19 02:06:48 +01:00
def test_key_array_pair(self):
testdata = {'flintstone': ['fred', 'wilma', 'pebbles']}
result = 'flintstone {\n fred\n wilma\n pebbles\n}\n'
2017-01-19 02:06:48 +01:00
self.renderTest(testdata, result)
2017-01-19 02:40:19 +01:00
def test_key_hash_pair(self):
testdata = {'friends': {'rubble': 'barney'}}
2017-01-21 04:56:08 +01:00
result = 'friends {\n rubble barney\n}\n'
2017-01-19 02:40:19 +01:00
self.renderTest(testdata, result)
2017-01-19 03:29:15 +01:00
def test_key_ordered_hashs(self):
testdata = {'friends': {'fred': 'flintstone', 'barney': 'rubble', 'wilma': 'flintstone', 'betty': 'rubble'}}
2017-01-21 04:56:08 +01:00
result = 'friends {\n barney rubble\n betty rubble\n fred flintstone\n wilma flintstone\n}\n'
2017-01-19 03:29:15 +01:00
self.renderTest(testdata, result)
2017-01-19 03:38:51 +01:00
def test_ordered_hashes(self):
testdata = [{'fred': 'flintstone'}, {'wilma': 'flintstone'}, {'barney': 'rubble'}, {'betty': 'rubble'}]
result = 'fred flintstone\nwilma flintstone\nbarney rubble\nbetty rubble\n'
self.renderTest(testdata, result)
2017-01-19 13:48:26 +01:00
def test_carryover(self):
testdata = {'vrrp_script': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}}
2017-01-21 04:56:08 +01:00
result = 'vrrp_script gizmo {\n barney rubble\n fred flintstone\n}\n'
2017-01-19 13:48:26 +01:00
self.renderTest(testdata, result)
2017-01-19 14:09:44 +01:00
def test_carryover_contains_arry(self):
testdata = {'vrrp_script': {'gizmo': [{'fred': 'flintstone'}, {'barney': 'rubble'}]}}
2017-01-21 04:56:08 +01:00
result = 'vrrp_script gizmo {\n fred flintstone\n barney rubble\n}\n'
2017-01-19 14:09:44 +01:00
self.renderTest(testdata, result)
2017-01-21 05:27:03 +01:00
def test_carryover_vrrp_instance(self):
2017-01-19 14:09:44 +01:00
testdata = {'vrrp_instance': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}}
2017-01-21 04:56:08 +01:00
result = 'vrrp_instance gizmo {\n barney rubble\n fred flintstone\n}\n'
2017-01-19 14:09:44 +01:00
self.renderTest(testdata, result)
def test_carryovers_in_an_array(self):
testdata = [{'vrrp_script': {'gizmo': {'running': 'dumdums'}}}, {'vrrp_instance': {'dumdums': {'fred': 'flintstone'}}}]
2017-01-21 04:56:08 +01:00
result = 'vrrp_script gizmo {\n running dumdums\n}\nvrrp_instance dumdums {\n fred flintstone\n}\n'
2017-01-19 14:09:44 +01:00
self.renderTest(testdata, result)
2017-01-21 05:27:03 +01:00
def test_carryover_vrrp_sync_group(self):
2017-01-19 14:25:12 +01:00
testdata = {'vrrp_sync_group': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}}
2017-01-21 04:56:08 +01:00
result = 'vrrp_sync_group gizmo {\n barney rubble\n fred flintstone\n}\n'
2017-01-19 14:25:12 +01:00
self.renderTest(testdata, result)
2017-01-21 05:27:03 +01:00
def test_carryover_virtual_server_group(self):
2017-01-19 14:25:12 +01:00
testdata = {'virtual_server_group': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}}
2017-01-21 04:56:08 +01:00
result = 'virtual_server_group gizmo {\n barney rubble\n fred flintstone\n}\n'
2017-01-19 14:25:12 +01:00
self.renderTest(testdata, result)
2017-01-21 05:27:03 +01:00
def test_carryover_virtual_server(self):
2017-01-19 14:25:12 +01:00
testdata = {'virtual_server': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}}
2017-01-21 04:56:08 +01:00
result = 'virtual_server gizmo {\n barney rubble\n fred flintstone\n}\n'
2017-01-19 14:25:12 +01:00
self.renderTest(testdata, result)
2017-01-21 05:27:03 +01:00
def test_carryover_real_server(self):
2017-01-19 14:25:12 +01:00
testdata = {'real_server': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}}
2017-01-21 04:56:08 +01:00
result = 'real_server gizmo {\n barney rubble\n fred flintstone\n}\n'
2017-01-19 14:25:12 +01:00
self.renderTest(testdata, result)
2017-01-21 05:27:03 +01:00
def test_carryover_within_carryover(self):
testdata = {'virtual_server': {'gizmo': {'real_server': {'dumdums': {'fred': 'flintstone', 'barney': 'rubble'}}}}}
result = 'virtual_server gizmo {\n real_server dumdums {\n barney rubble\n fred flintstone\n }\n}\n'
self.renderTest(testdata, result)
2017-01-21 06:12:44 +01:00
def test_indent_nested_hashes(self):
testdata = {'flintstone': {'fred': {'role': {'family': 'father'}}}}
result = 'flintstone {\n fred {\n role {\n family father\n }\n }\n}\n'
self.renderTest(testdata, result)
def test_indent_carryover_with_nested_hashes(self):
testdata = {'real_server': {'gizmo': {'another': 'day', 'dumdums': {'fred': 'flintstone', 'barney': 'rubble'}}}}
result = 'real_server gizmo {\n another day\n dumdums {\n barney rubble\n fred flintstone\n }\n}\n'
self.renderTest(testdata, result)
2017-01-19 01:11:23 +01:00
if __name__ == '__main__':
unittest.main()