mirror of
https://github.com/lucaspalomodevelop/core.git
synced 2026-03-14 16:44:39 +00:00
(configd) add initial unit tests
This commit is contained in:
parent
9c84f2b435
commit
dbfa92c72b
37
src/opnsense/service/run_unittests.py
Normal file
37
src/opnsense/service/run_unittests.py
Normal file
@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env python2.7
|
||||
"""
|
||||
Copyright (c) 2016 Ad Schellevis
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
||||
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
--------------------------------------------------------------------------------------
|
||||
|
||||
package : configd
|
||||
"""
|
||||
import unittest
|
||||
from tests.template import *
|
||||
|
||||
__author__ = 'Ad Schellevis'
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
25
src/opnsense/service/tests/__init__.py
Normal file
25
src/opnsense/service/tests/__init__.py
Normal file
@ -0,0 +1,25 @@
|
||||
"""
|
||||
Copyright (c) 2016 Ad Schellevis
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
||||
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
"""
|
||||
92
src/opnsense/service/tests/config/config.xml
Normal file
92
src/opnsense/service/tests/config/config.xml
Normal file
@ -0,0 +1,92 @@
|
||||
<?xml version="1.0"?>
|
||||
<opnsense>
|
||||
<interfaces>
|
||||
<wan>
|
||||
<enable/>
|
||||
<if>em1</if>
|
||||
<descr>WAN</descr>
|
||||
<ipaddr>dhcp</ipaddr>
|
||||
<dhcphostname/>
|
||||
<alias-address/>
|
||||
<alias-subnet>32</alias-subnet>
|
||||
<dhcprejectfrom/>
|
||||
<adv_dhcp_pt_timeout/>
|
||||
<adv_dhcp_pt_retry/>
|
||||
<adv_dhcp_pt_select_timeout/>
|
||||
<adv_dhcp_pt_reboot/>
|
||||
<adv_dhcp_pt_backoff_cutoff/>
|
||||
<adv_dhcp_pt_initial_interval/>
|
||||
<adv_dhcp_pt_values>SavedCfg</adv_dhcp_pt_values>
|
||||
<adv_dhcp_send_options/>
|
||||
<adv_dhcp_request_options/>
|
||||
<adv_dhcp_required_options/>
|
||||
<adv_dhcp_option_modifiers/>
|
||||
<adv_dhcp_config_advanced/>
|
||||
<adv_dhcp_config_file_override/>
|
||||
<adv_dhcp_config_file_override_path/>
|
||||
<ipaddrv6>dhcp6</ipaddrv6>
|
||||
<dhcp6-duid/>
|
||||
<dhcp6-ia-pd-len>0</dhcp6-ia-pd-len>
|
||||
<adv_dhcp6_interface_statement_send_options/>
|
||||
<adv_dhcp6_interface_statement_request_options/>
|
||||
<adv_dhcp6_interface_statement_information_only_enable/>
|
||||
<adv_dhcp6_interface_statement_script/>
|
||||
<adv_dhcp6_id_assoc_statement_address_enable/>
|
||||
<adv_dhcp6_id_assoc_statement_address/>
|
||||
<adv_dhcp6_id_assoc_statement_address_id/>
|
||||
<adv_dhcp6_id_assoc_statement_address_pltime/>
|
||||
<adv_dhcp6_id_assoc_statement_address_vltime/>
|
||||
<adv_dhcp6_id_assoc_statement_prefix_enable/>
|
||||
<adv_dhcp6_id_assoc_statement_prefix/>
|
||||
<adv_dhcp6_id_assoc_statement_prefix_id/>
|
||||
<adv_dhcp6_id_assoc_statement_prefix_pltime/>
|
||||
<adv_dhcp6_id_assoc_statement_prefix_vltime/>
|
||||
<adv_dhcp6_prefix_interface_statement_sla_id/>
|
||||
<adv_dhcp6_prefix_interface_statement_sla_len/>
|
||||
<adv_dhcp6_authentication_statement_authname/>
|
||||
<adv_dhcp6_authentication_statement_protocol/>
|
||||
<adv_dhcp6_authentication_statement_algorithm/>
|
||||
<adv_dhcp6_authentication_statement_rdm/>
|
||||
<adv_dhcp6_key_info_statement_keyname/>
|
||||
<adv_dhcp6_key_info_statement_realm/>
|
||||
<adv_dhcp6_key_info_statement_keyid/>
|
||||
<adv_dhcp6_key_info_statement_secret/>
|
||||
<adv_dhcp6_key_info_statement_expire/>
|
||||
<adv_dhcp6_config_advanced/>
|
||||
<adv_dhcp6_config_file_override/>
|
||||
<adv_dhcp6_config_file_override_path/>
|
||||
<spoofmac/>
|
||||
</wan>
|
||||
<lan>
|
||||
<enable>1</enable>
|
||||
<if>em0</if>
|
||||
<ipaddr>10.211.55.100</ipaddr>
|
||||
<subnet>24</subnet>
|
||||
<ipaddrv6/>
|
||||
<subnetv6/>
|
||||
<media/>
|
||||
<mediaopt/>
|
||||
<track6-interface>wan</track6-interface>
|
||||
<track6-prefix-id>0</track6-prefix-id>
|
||||
<gateway/>
|
||||
<gatewayv6/>
|
||||
</lan>
|
||||
<opt1>
|
||||
<enable>1</enable>
|
||||
<if>em2</if>
|
||||
<ipaddr>10.211.56.100</ipaddr>
|
||||
<subnet>24</subnet>
|
||||
<ipaddrv6/>
|
||||
<subnetv6/>
|
||||
<media/>
|
||||
<mediaopt/>
|
||||
<track6-interface>wan</track6-interface>
|
||||
<track6-prefix-id>0</track6-prefix-id>
|
||||
<gateway/>
|
||||
<gatewayv6/>
|
||||
</opt1>
|
||||
</interfaces>
|
||||
<filter>
|
||||
<rule></rule>
|
||||
</filter>
|
||||
</opnsense>
|
||||
96
src/opnsense/service/tests/template.py
Normal file
96
src/opnsense/service/tests/template.py
Normal file
@ -0,0 +1,96 @@
|
||||
"""
|
||||
Copyright (c) 2016 Ad Schellevis
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
||||
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
--------------------------------------------------------------------------------------
|
||||
|
||||
package : configd
|
||||
"""
|
||||
import os
|
||||
import unittest
|
||||
import collections
|
||||
from modules import config
|
||||
from modules import template
|
||||
|
||||
class TestConfigMethods(unittest.TestCase):
|
||||
def setUp(self):
|
||||
""" setup test, load config
|
||||
:return:
|
||||
"""
|
||||
conf_path = '%s/config/config.xml' % '/'.join(__file__.split('/')[:-1])
|
||||
self.conf = config.Config(conf_path)
|
||||
|
||||
def tearDown(self):
|
||||
""" end test
|
||||
:return:
|
||||
"""
|
||||
self.conf = None
|
||||
|
||||
def test_type(self):
|
||||
""" test correct config type
|
||||
:return:
|
||||
"""
|
||||
self.assertEquals(type(self.conf.get()), collections.OrderedDict)
|
||||
|
||||
def test_interface(self):
|
||||
""" test existence of interface
|
||||
:return:
|
||||
"""
|
||||
self.assertIn('interfaces', self.conf.get(), 'interfaces section missing')
|
||||
self.assertIn('lan', self.conf.get()['interfaces'], 'lan section missing')
|
||||
self.assertIn('ipaddr', self.conf.get()['interfaces']['lan'], 'lan address missing')
|
||||
|
||||
|
||||
class TestTemplateMethods(unittest.TestCase):
|
||||
def setUp(self):
|
||||
""" setup test, load config create temp directory
|
||||
:return:
|
||||
"""
|
||||
conf_path = '%s/config/config.xml' % '/'.join(__file__.split('/')[:-1])
|
||||
self.output_path = '%s/output/' % '/'.join(__file__.split('/')[:-1])
|
||||
self.conf = config.Config(conf_path)
|
||||
self.tmpl = template.Template(target_root_directory=self.output_path)
|
||||
self.tmpl.set_config(self.conf.get())
|
||||
if not os.path.exists(self.output_path):
|
||||
os.mkdir(self.output_path)
|
||||
|
||||
def tearDown(self):
|
||||
""" end test, remove test data
|
||||
:return:
|
||||
"""
|
||||
self.conf = None
|
||||
if os.path.exists(self.output_path):
|
||||
for root, dirs, files in os.walk(self.output_path, topdown=False):
|
||||
for filename in files:
|
||||
os.unlink('%s/%s' % (root, filename))
|
||||
for dirname in dirs:
|
||||
os.rmdir('%s/%s' % (root, dirname))
|
||||
os.rmdir(self.output_path)
|
||||
|
||||
def test_sample(self):
|
||||
""" test sample template
|
||||
:return:
|
||||
"""
|
||||
generated_filenames = self.tmpl.generate('OPNsense.Sample')
|
||||
self.assertEquals(len(generated_filenames), 3, 'number of output files <> 3')
|
||||
Loading…
x
Reference in New Issue
Block a user