1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-06 20:17:15 +00:00

Move modules and module_utils unit tests to correct place (#81)

* Move modules and module_utils unit tests to correct place.

* Update ignore.txt

* Fix imports.

* Fix typos.

* Fix more typos.
This commit is contained in:
Felix Fontein 2020-03-31 10:42:38 +02:00 committed by GitHub
parent ab3c2120fb
commit be191cce6c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
1170 changed files with 732 additions and 751 deletions

View file

@ -0,0 +1,86 @@
# (c) 2018 Red Hat Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import os
import json
from ansible_collections.community.general.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
fixture_data = {}
def load_fixture(name):
path = os.path.join(fixture_path, name)
if path in fixture_data:
return fixture_data[path]
with open(path) as f:
data = f.read()
try:
data = json.loads(data)
except Exception:
pass
fixture_data[path] = data
return data
class TestEdgeosModule(ModuleTestCase):
def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False):
self.load_fixtures(commands)
if failed:
result = self.failed()
self.assertTrue(result['failed'], result)
else:
result = self.changed(changed)
self.assertEqual(result['changed'], changed, result)
if commands is not None:
if sort:
self.assertEqual(sorted(commands), sorted(result['commands']), result['commands'])
else:
self.assertEqual(commands, result['commands'], result['commands'])
return result
def failed(self):
with self.assertRaises(AnsibleFailJson) as exc:
self.module.main()
result = exc.exception.args[0]
self.assertTrue(result['failed'], result)
return result
def changed(self, changed=False):
with self.assertRaises(AnsibleExitJson) as exc:
self.module.main()
result = exc.exception.args[0]
self.assertEqual(result['changed'], changed, result)
return result
def load_fixtures(self, commands=None):
pass

View file

@ -0,0 +1,10 @@
set system host-name 'router'
set system domain-name 'acme.com'
set system domain-search domain 'acme.com'
set system name-server 208.67.220.220
set system name-server 208.67.222.222
set interfaces ethernet eth0 address 1.2.3.4/24
set interfaces ethernet eth0 description 'Outside'
set interfaces ethernet eth1 address 10.77.88.1/24
set interfaces ethernet eth1 description 'Inside'
set interfaces ethernet eth1 disable

View file

@ -0,0 +1,5 @@
set system host-name er01
delete interfaces ethernet eth0 address
set interfaces ethernet eth1 address 10.77.88.1/24
set interfaces ethernet eth1 description 'Inside'
set interfaces ethernet eth1 disable

View file

@ -0,0 +1,13 @@
interfaces {
ethernet eth0 {
address 10.10.10.10/24
}
ethernet eth1 {
address 10.77.88.1/24
description 'Inside'
disable
}
}
system {
host-name er01
}

View file

@ -0,0 +1 @@
er01

View file

@ -0,0 +1,7 @@
Version: v1.9.7+hotfix.4
Build ID: 5024004
Build on: 10/05/17 04:03
Copyright: 2012-2017 Ubiquiti Networks, Inc.
HW model: EdgeRouter PoE 5-Port
HW S/N: 802AA84D6394
Uptime: 09:39:34 up 56 days, 21 min, 2 users, load average: 0.14, 0.11, 0.07

View file

@ -0,0 +1,106 @@
# (c) 2018 Red Hat Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import json
from ansible_collections.community.general.tests.unit.compat.mock import patch
from ansible_collections.community.general.plugins.modules.network.edgeos import edgeos_command
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args
from .edgeos_module import TestEdgeosModule, load_fixture
class TestEdgeosCommandModule(TestEdgeosModule):
module = edgeos_command
def setUp(self):
super(TestEdgeosCommandModule, self).setUp()
self.mock_run_commands = patch('ansible_collections.community.general.plugins.modules.network.edgeos.edgeos_command.run_commands')
self.run_commands = self.mock_run_commands.start()
def tearDown(self):
super(TestEdgeosCommandModule, self).tearDown()
self.mock_run_commands.stop()
def load_fixtures(self, commands=None):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
for item in commands:
try:
obj = json.loads(item)
command = obj['command']
except (ValueError, TypeError):
command = item['command']
filename = str(command).replace(' ', '_')
output.append(load_fixture(filename))
return output
self.run_commands.side_effect = load_from_file
def test_edgeos_command_simple(self):
set_module_args(dict(commands=['show version']))
result = self.execute_module()
self.assertEqual(len(result['stdout']), 1)
self.assertTrue(result['stdout'][0].startswith('Version: v1.9.7'))
def test_edgeos_command_multiple(self):
set_module_args(dict(commands=['show version', 'show version']))
result = self.execute_module()
self.assertEqual(len(result['stdout']), 2)
self.assertTrue(result['stdout'][0].startswith('Version: v1.9.7'))
def test_edgeos_commond_wait_for(self):
wait_for = 'result[0] contains "Ubiquiti Networks"'
set_module_args(dict(commands=['show version'], wait_for=wait_for))
self.execute_module()
def test_edgeos_command_wait_for_fails(self):
wait_for = 'result[0] contains "bad string"'
set_module_args(dict(commands=['show version'], wait_for=wait_for))
self.execute_module(failed=True)
self.assertEqual(self.run_commands.call_count, 10)
def test_edgeos_command_retries(self):
wait_for = 'result[0] contains "bad string"'
set_module_args(dict(commands=['show version'], wait_for=wait_for, retries=2))
self.execute_module(failed=True)
self.assertEqual(self.run_commands.call_count, 2)
def test_edgeos_command_match_any(self):
wait_for = ['result[0] contains "Ubiquiti Networks"',
'result[0] contains "bad string"']
set_module_args(dict(commands=['show version'], wait_for=wait_for, match='any'))
self.execute_module()
def test_edgeos_command_match_all(self):
wait_for = ['result[0] contains "Ubiquiti Networks"',
'result[0] contains "EdgeRouter"']
set_module_args(dict(commands=['show version'], wait_for=wait_for, match='all'))
self.execute_module()
def test_vyos_command_match_all_failure(self):
wait_for = ['result[0] contains "Ubiquiti Networks"',
'result[0] contains "bad string"']
commands = ['show version', 'show version']
set_module_args(dict(commands=commands, wait_for=wait_for, match='all'))
self.execute_module(failed=True)

View file

@ -0,0 +1,105 @@
#
# (c) 2018 Red Hat Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible_collections.community.general.tests.unit.compat.mock import patch
from ansible_collections.community.general.plugins.modules.network.edgeos import edgeos_config
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args
from .edgeos_module import TestEdgeosModule, load_fixture
class TestEdgeosConfigModule(TestEdgeosModule):
module = edgeos_config
def setUp(self):
super(TestEdgeosConfigModule, self).setUp()
self.mock_get_config = patch('ansible_collections.community.general.plugins.modules.network.edgeos.edgeos_config.get_config')
self.get_config = self.mock_get_config.start()
self.mock_load_config = patch('ansible_collections.community.general.plugins.modules.network.edgeos.edgeos_config.load_config')
self.load_config = self.mock_load_config.start()
self.mock_run_commands = patch('ansible_collections.community.general.plugins.modules.network.edgeos.edgeos_config.run_commands')
self.run_commands = self.mock_run_commands.start()
def tearDown(self):
super(TestEdgeosConfigModule, self).tearDown()
self.mock_get_config.stop()
self.mock_load_config.stop()
self.mock_run_commands.stop()
def load_fixtures(self, commands=None):
config_file = 'edgeos_config_config.cfg'
self.get_config.return_value = load_fixture(config_file)
self.load_config.return_value = None
def test_edgeos_config_unchanged(self):
src = load_fixture('edgeos_config_config.cfg')
set_module_args(dict(src=src))
self.execute_module()
def test_edgeos_config_src(self):
src = load_fixture('edgeos_config_src.cfg')
set_module_args(dict(src=src))
commands = ['set system host-name er01', 'delete interfaces ethernet eth0 address']
self.execute_module(changed=True, commands=commands)
def test_edgeos_config_src_brackets(self):
src = load_fixture('edgeos_config_src_brackets.cfg')
set_module_args(dict(src=src))
commands = ['set interfaces ethernet eth0 address 10.10.10.10/24', 'set system host-name er01']
self.execute_module(changed=True, commands=commands)
def test_edgeos_config_backup(self):
set_module_args(dict(backup=True))
result = self.execute_module()
self.assertIn('__backup__', result)
def test_edgeos_config_lines(self):
commands = ['set system host-name er01']
set_module_args(dict(lines=commands))
self.execute_module(changed=True, commands=commands)
def test_edgeos_config_config(self):
config = 'set system host-name localhost'
new_config = ['set system host-name er01']
set_module_args(dict(lines=new_config, config=config))
self.execute_module(changed=True, commands=new_config)
def test_edgeos_config_match_none(self):
lines = ['set system interfaces ethernet eth0 address 1.2.3.4/24',
'set system interfaces ethernet eth0 description Outside']
set_module_args(dict(lines=lines, match='none'))
self.execute_module(changed=True, commands=lines, sort=False)
def test_edgeos_config_single_quote_wrapped_values(self):
lines = ["set system interfaces ethernet eth0 description 'tests single quotes'"]
set_module_args(dict(lines=lines))
commands = ["set system interfaces ethernet eth0 description 'tests single quotes'"]
self.execute_module(changed=True, commands=commands)
def test_edgeos_config_single_quote_wrapped_values_failure(self):
lines = ["set system interfaces ethernet eth0 description 'test's single quotes'"]
set_module_args(dict(lines=lines))
self.execute_module(failed=True)

View file

@ -0,0 +1,86 @@
# (c) 2018 Red Hat Inc.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import json
from ansible_collections.community.general.tests.unit.compat.mock import patch
from ansible_collections.community.general.plugins.modules.network.edgeos import edgeos_facts
from ansible_collections.community.general.tests.unit.plugins.modules.utils import set_module_args
from .edgeos_module import TestEdgeosModule, load_fixture
class TestEdgeosFactsModule(TestEdgeosModule):
module = edgeos_facts
def setUp(self):
super(TestEdgeosFactsModule, self).setUp()
self.mock_run_commands = patch('ansible_collections.community.general.plugins.modules.network.edgeos.edgeos_facts.run_commands')
self.run_commands = self.mock_run_commands.start()
def tearDown(self):
super(TestEdgeosFactsModule, self).tearDown()
self.mock_run_commands.stop()
def load_fixtures(self, commands=None):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
for item in commands:
try:
obj = json.loads(item)
command = obj['command']
except ValueError:
command = item
filename = str(command).replace(' ', '_')
output.append(load_fixture(filename))
return output
self.run_commands.side_effect = load_from_file
def test_edgeos_facts_default(self):
set_module_args(dict(gather_subset='default'))
result = self.execute_module()
facts = result.get('ansible_facts')
self.assertEqual(len(facts), 5)
self.assertEqual(facts['ansible_net_hostname'].strip(), 'er01')
self.assertEqual(facts['ansible_net_version'], '1.9.7+hotfix.4')
def test_edgeos_facts_not_all(self):
set_module_args(dict(gather_subset='!all'))
result = self.execute_module()
facts = result.get('ansible_facts')
self.assertEqual(len(facts), 5)
self.assertEqual(facts['ansible_net_hostname'].strip(), 'er01')
self.assertEqual(facts['ansible_net_version'], '1.9.7+hotfix.4')
def test_edgeos_facts_exclude_most(self):
set_module_args(dict(gather_subset=['!neighbors', '!config']))
result = self.execute_module()
facts = result.get('ansible_facts')
self.assertEqual(len(facts), 5)
self.assertEqual(facts['ansible_net_hostname'].strip(), 'er01')
self.assertEqual(facts['ansible_net_version'], '1.9.7+hotfix.4')
def test_edgeos_facts_invalid_subset(self):
set_module_args(dict(gather_subset='cereal'))
result = self.execute_module(failed=True)