test/: PEP8 compliancy (#24803)

* test/: PEP8 compliancy

- Make PEP8 compliant

* Python3 chokes on casting int to bytes (#24952)

But if we tell the formatter that the var is a number, it works
This commit is contained in:
Dag Wieers
2017-05-30 19:05:19 +02:00
committed by John R Barker
parent 31c59ad5f9
commit 4efec414e7
110 changed files with 1702 additions and 1547 deletions

View File

@@ -1,9 +1,6 @@
import pytest
import unittest
boto3 = pytest.importorskip("boto3")
botocore = pytest.importorskip("botocore")
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
@@ -13,6 +10,10 @@ from ansible.executor.task_queue_manager import TaskQueueManager
import ansible.modules.cloud.amazon.ec2_vpc_nat_gateway as ng
boto3 = pytest.importorskip("boto3")
botocore = pytest.importorskip("botocore")
Options = (
namedtuple(
'Options', [
@@ -44,6 +45,7 @@ aws_region = 'us-west-2'
inventory = InventoryManager(loader=loader)
variable_manager.set_inventory(inventory)
def run(play):
tqm = None
results = None
@@ -62,14 +64,15 @@ def run(play):
tqm.cleanup()
return tqm, results
class AnsibleVpcNatGatewayTasks(unittest.TestCase):
def test_create_gateway_using_allocation_id(self):
play_source = dict(
name = "Create new nat gateway with eip allocation-id",
hosts = 'localhost',
gather_facts = 'no',
tasks = [
play_source = dict(
name="Create new nat gateway with eip allocation-id",
hosts='localhost',
gather_facts='no',
tasks=[
dict(
action=dict(
module='ec2_vpc_nat_gateway',
@@ -98,11 +101,11 @@ class AnsibleVpcNatGatewayTasks(unittest.TestCase):
self.failUnless(tqm._stats.changed['localhost'] == 1)
def test_create_gateway_using_allocation_id_idempotent(self):
play_source = dict(
name = "Create new nat gateway with eip allocation-id",
hosts = 'localhost',
gather_facts = 'no',
tasks = [
play_source = dict(
name="Create new nat gateway with eip allocation-id",
hosts='localhost',
gather_facts='no',
tasks=[
dict(
action=dict(
module='ec2_vpc_nat_gateway',
@@ -131,11 +134,11 @@ class AnsibleVpcNatGatewayTasks(unittest.TestCase):
self.assertFalse('localhost' in tqm._stats.changed)
def test_create_gateway_using_eip_address(self):
play_source = dict(
name = "Create new nat gateway with eip address",
hosts = 'localhost',
gather_facts = 'no',
tasks = [
play_source = dict(
name="Create new nat gateway with eip address",
hosts='localhost',
gather_facts='no',
tasks=[
dict(
action=dict(
module='ec2_vpc_nat_gateway',
@@ -164,11 +167,11 @@ class AnsibleVpcNatGatewayTasks(unittest.TestCase):
self.failUnless(tqm._stats.changed['localhost'] == 1)
def test_create_gateway_using_eip_address_idempotent(self):
play_source = dict(
name = "Create new nat gateway with eip address",
hosts = 'localhost',
gather_facts = 'no',
tasks = [
play_source = dict(
name="Create new nat gateway with eip address",
hosts='localhost',
gather_facts='no',
tasks=[
dict(
action=dict(
module='ec2_vpc_nat_gateway',
@@ -197,11 +200,11 @@ class AnsibleVpcNatGatewayTasks(unittest.TestCase):
self.assertFalse('localhost' in tqm._stats.changed)
def test_create_gateway_in_subnet_only_if_one_does_not_exist_already(self):
play_source = dict(
name = "Create new nat gateway only if one does not exist already",
hosts = 'localhost',
gather_facts = 'no',
tasks = [
play_source = dict(
name="Create new nat gateway only if one does not exist already",
hosts='localhost',
gather_facts='no',
tasks=[
dict(
action=dict(
module='ec2_vpc_nat_gateway',
@@ -230,11 +233,11 @@ class AnsibleVpcNatGatewayTasks(unittest.TestCase):
self.assertFalse('localhost' in tqm._stats.changed)
def test_delete_gateway(self):
play_source = dict(
name = "Delete Nat Gateway",
hosts = 'localhost',
gather_facts = 'no',
tasks = [
play_source = dict(
name="Delete Nat Gateway",
hosts='localhost',
gather_facts='no',
tasks=[
dict(
action=dict(
module='ec2_vpc_nat_gateway',
@@ -262,6 +265,7 @@ class AnsibleVpcNatGatewayTasks(unittest.TestCase):
self.failUnless(tqm._stats.ok['localhost'] == 2)
self.assertTrue('localhost' in tqm._stats.changed)
class AnsibleEc2VpcNatGatewayFunctions(unittest.TestCase):
def test_get_nat_gateways(self):
@@ -326,7 +330,7 @@ class AnsibleEc2VpcNatGatewayFunctions(unittest.TestCase):
client = boto3.client('ec2', region_name=aws_region)
gws, err_msg = (
ng.gateway_in_subnet_exists(
client, 'subnet-123456789', check_mode=True
client, 'subnet-123456789', check_mode=True
)
)
should_return = ng.DRY_RUN_GATEWAYS
@@ -336,7 +340,7 @@ class AnsibleEc2VpcNatGatewayFunctions(unittest.TestCase):
client = boto3.client('ec2', region_name=aws_region)
allocation_id, _ = (
ng.get_eip_allocation_id_by_address(
client, '55.55.55.55', check_mode=True
client, '55.55.55.55', check_mode=True
)
)
should_return = 'eipalloc-1234567'
@@ -346,7 +350,7 @@ class AnsibleEc2VpcNatGatewayFunctions(unittest.TestCase):
client = boto3.client('ec2', region_name=aws_region)
allocation_id, err_msg = (
ng.get_eip_allocation_id_by_address(
client, '52.52.52.52', check_mode=True
client, '52.52.52.52', check_mode=True
)
)
self.assertEqual(err_msg, 'EIP 52.52.52.52 does not exist')

View File

@@ -19,42 +19,45 @@
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
import pytest
boto3 = pytest.importorskip("boto3")
import json
import copy
from ansible.module_utils._text import to_bytes
from ansible.module_utils import basic
import json
import pytest
from ansible.compat.tests.mock import MagicMock, Mock, patch
from ansible.module_utils import basic
from ansible.module_utils._text import to_bytes
boto3 = pytest.importorskip("boto3")
# lambda is a keyword so we have to hack this.
_temp = __import__("ansible.modules.cloud.amazon.lambda")
lda = getattr(_temp.modules.cloud.amazon,"lambda")
lda = getattr(_temp.modules.cloud.amazon, "lambda")
def set_module_args(args):
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
basic._ANSIBLE_ARGS = to_bytes(args)
base_lambda_config={
'FunctionName' : 'lambda_name',
'Role' : 'arn:aws:iam::987654321012:role/lambda_basic_execution',
'Handler' : 'lambda_python.my_handler',
'Description' : 'this that the other',
'Timeout' : 3,
'MemorySize' : 128,
'Runtime' : 'python2.7',
'CodeSha256' : 'AqMZ+xptM7aC9VXu+5jyp1sqO+Nj4WFMNzQxtPMP2n8=',
base_lambda_config = {
'FunctionName': 'lambda_name',
'Role': 'arn:aws:iam::987654321012:role/lambda_basic_execution',
'Handler': 'lambda_python.my_handler',
'Description': 'this that the other',
'Timeout': 3,
'MemorySize': 128,
'Runtime': 'python2.7',
'CodeSha256': 'AqMZ+xptM7aC9VXu+5jyp1sqO+Nj4WFMNzQxtPMP2n8=',
}
one_change_lambda_config=copy.copy(base_lambda_config)
one_change_lambda_config['Timeout']=4
two_change_lambda_config=copy.copy(one_change_lambda_config)
two_change_lambda_config['Role']='arn:aws:iam::987654321012:role/lambda_advanced_execution'
code_change_lambda_config=copy.copy(base_lambda_config)
code_change_lambda_config['CodeSha256']='P+Zy8U4T4RiiHWElhL10VBKj9jw4rSJ5bm/TiW+4Rts='
one_change_lambda_config = copy.copy(base_lambda_config)
one_change_lambda_config['Timeout'] = 4
two_change_lambda_config = copy.copy(one_change_lambda_config)
two_change_lambda_config['Role'] = 'arn:aws:iam::987654321012:role/lambda_advanced_execution'
code_change_lambda_config = copy.copy(base_lambda_config)
code_change_lambda_config['CodeSha256'] = 'P+Zy8U4T4RiiHWElhL10VBKj9jw4rSJ5bm/TiW+4Rts='
base_module_args={
base_module_args = {
"region": "us-west-1",
"name": "lambda_name",
"state": "present",
@@ -62,10 +65,10 @@ base_module_args={
"runtime": 'python2.7',
"role": 'arn:aws:iam::987654321012:role/lambda_basic_execution',
"memory_size": 128,
"timeout" : 3,
"timeout": 3,
"handler": 'lambda_python.my_handler'
}
module_args_with_environment=dict(base_module_args, environment_variables={
module_args_with_environment = dict(base_module_args, environment_variables={
"variable_name": "variable_value"
})
@@ -78,26 +81,27 @@ def make_mock_no_connection_connection(config):
)
lambda_client_double.update_function_configuration.configure_mock(
return_value={
'Version' : 1
'Version': 1
}
)
fake_boto3_conn=Mock(return_value=lambda_client_double)
fake_boto3_conn = Mock(return_value=lambda_client_double)
return (fake_boto3_conn, lambda_client_double)
def make_mock_connection(config):
"""return a mock of ansible's boto3_conn ready to return a mock AWS API client"""
lambda_client_double = MagicMock()
lambda_client_double.get_function.configure_mock(
return_value={
'Configuration' : config
'Configuration': config
}
)
lambda_client_double.update_function_configuration.configure_mock(
return_value={
'Version' : 1
'Version': 1
}
)
fake_boto3_conn=Mock(return_value=lambda_client_double)
fake_boto3_conn = Mock(return_value=lambda_client_double)
return (fake_boto3_conn, lambda_client_double)
@@ -111,13 +115,13 @@ def fail_json_double(*args, **kwargs):
raise AnsibleFailJson(kwargs)
#TODO: def test_handle_different_types_in_config_params():
# TODO: def test_handle_different_types_in_config_params():
def test_create_lambda_if_not_exist():
set_module_args(base_module_args)
(boto3_conn_double, lambda_client_double)=make_mock_no_connection_connection(code_change_lambda_config)
(boto3_conn_double, lambda_client_double) = make_mock_no_connection_connection(code_change_lambda_config)
with patch.object(lda, 'boto3_conn', boto3_conn_double):
try:
@@ -133,7 +137,7 @@ def test_create_lambda_if_not_exist():
"update lambda function code when function should have been created only"
assert(len(lambda_client_double.create_function.mock_calls) > 0), \
"failed to call create_function "
(create_args, create_kwargs)=lambda_client_double.create_function.call_args
(create_args, create_kwargs) = lambda_client_double.create_function.call_args
assert (len(create_kwargs) > 0), "expected create called with keyword args, none found"
try:
@@ -143,12 +147,13 @@ def test_create_lambda_if_not_exist():
create_kwargs["Environment"]
raise(Exception("Environment sent to boto when none expected"))
except KeyError:
pass #We are happy, no environment is fine
pass # We are happy, no environment is fine
def test_update_lambda_if_code_changed():
set_module_args(base_module_args)
(boto3_conn_double, lambda_client_double)=make_mock_connection(code_change_lambda_config)
(boto3_conn_double, lambda_client_double) = make_mock_connection(code_change_lambda_config)
with patch.object(lda, 'boto3_conn', boto3_conn_double):
try:
@@ -169,10 +174,11 @@ def test_update_lambda_if_code_changed():
assert(len(lambda_client_double.update_function_code.mock_calls) < 3), \
"lambda function code update called multiple times when only one time should be needed"
def test_update_lambda_if_config_changed():
set_module_args(base_module_args)
(boto3_conn_double,lambda_client_double)=make_mock_connection(two_change_lambda_config)
(boto3_conn_double, lambda_client_double) = make_mock_connection(two_change_lambda_config)
with patch.object(lda, 'boto3_conn', boto3_conn_double):
try:
@@ -189,10 +195,11 @@ def test_update_lambda_if_config_changed():
assert(len(lambda_client_double.update_function_code.mock_calls) == 0), \
"updated lambda code when no change should have happened"
def test_update_lambda_if_only_one_config_item_changed():
set_module_args(base_module_args)
(boto3_conn_double,lambda_client_double)=make_mock_connection(one_change_lambda_config)
(boto3_conn_double, lambda_client_double) = make_mock_connection(one_change_lambda_config)
with patch.object(lda, 'boto3_conn', boto3_conn_double):
try:
@@ -209,10 +216,11 @@ def test_update_lambda_if_only_one_config_item_changed():
assert(len(lambda_client_double.update_function_code.mock_calls) == 0), \
"updated lambda code when no change should have happened"
def test_update_lambda_if_added_environment_variable():
set_module_args(module_args_with_environment)
(boto3_conn_double,lambda_client_double)=make_mock_connection(base_lambda_config)
(boto3_conn_double, lambda_client_double) = make_mock_connection(base_lambda_config)
with patch.object(lda, 'boto3_conn', boto3_conn_double):
try:
@@ -229,14 +237,14 @@ def test_update_lambda_if_added_environment_variable():
assert(len(lambda_client_double.update_function_code.mock_calls) == 0), \
"updated lambda code when no change should have happened"
(update_args, update_kwargs)=lambda_client_double.update_function_configuration.call_args
(update_args, update_kwargs) = lambda_client_double.update_function_configuration.call_args
assert (len(update_kwargs) > 0), "expected update configuration called with keyword args, none found"
assert update_kwargs['Environment']['Variables'] == module_args_with_environment['environment_variables']
def test_dont_update_lambda_if_nothing_changed():
def test_dont_update_lambda_if_nothing_changed():
set_module_args(base_module_args)
(boto3_conn_double,lambda_client_double)=make_mock_connection(base_lambda_config)
(boto3_conn_double, lambda_client_double) = make_mock_connection(base_lambda_config)
with patch.object(lda, 'boto3_conn', boto3_conn_double):
try:
@@ -248,9 +256,10 @@ def test_dont_update_lambda_if_nothing_changed():
assert(len(boto3_conn_double.mock_calls) == 1), "multiple boto connections used unexpectedly"
assert(len(lambda_client_double.update_function_configuration.mock_calls) == 0), \
"updated lambda function when no configuration changed"
assert(len(lambda_client_double.update_function_code.mock_calls) == 0 ), \
assert(len(lambda_client_double.update_function_code.mock_calls) == 0), \
"updated lambda code when no change should have happened"
def test_warn_region_not_specified():
set_module_args({
@@ -263,7 +272,7 @@ def test_warn_region_not_specified():
"role": 'arn:aws:iam::987654321012:role/lambda_basic_execution',
"handler": 'lambda_python.my_handler'})
get_aws_connection_info_double=Mock(return_value=(None,None,None))
get_aws_connection_info_double = Mock(return_value=(None, None, None))
with patch.object(lda, 'get_aws_connection_info', get_aws_connection_info_double):
with patch.object(basic.AnsibleModule, 'fail_json', fail_json_double):

View File

@@ -1,10 +1,12 @@
import pytest
boto = pytest.importorskip("boto")
import unittest
import ansible.modules.cloud.amazon.s3 as s3
from ansible.module_utils.six.moves.urllib.parse import urlparse
boto = pytest.importorskip("boto")
class TestUrlparse(unittest.TestCase):
def test_urlparse(self):
@@ -19,16 +21,16 @@ class TestUrlparse(unittest.TestCase):
def test_is_walrus(self):
actual = s3.is_walrus("trulywalrus_but_invalid_url")
#I don't know if this makes sense, but this is the current behaviour...
# I don't know if this makes sense, but this is the current behaviour...
self.assertEqual(True, actual)
actual = s3.is_walrus("http://notwalrus.amazonaws.com")
self.assertEqual(False, actual)
def test_get_s3_connection(self):
aws_connect_kwargs = dict(aws_access_key_id="access_key",
aws_secret_access_key="secret_key")
location=None
rgw=True
s3_url="http://bla.blubb"
aws_secret_access_key="secret_key")
location = None
rgw = True
s3_url = "http://bla.blubb"
actual = s3.get_s3_connection(aws_connect_kwargs, location, rgw, s3_url)
self.assertEqual("bla.blubb", actual.host)

View File

@@ -4,6 +4,7 @@ import unittest
from ansible.modules.cloud.docker._docker import get_split_image_tag
class DockerSplitImageTagTestCase(unittest.TestCase):
def test_trivial(self):

View File

@@ -2,6 +2,7 @@ import unittest
from ansible.modules.cloud.google.gce_tag import _get_changed_items, _intersect_items, _union_items
class TestGCETag(unittest.TestCase):
"""Unit tests for gce_tag module."""
@@ -29,7 +30,7 @@ class TestGCETag(unittest.TestCase):
# tags removed
new_tags = ['one', 'two']
existing_tags = ['two']
want = ['two'] # only remove the tag that was present
want = ['two'] # only remove the tag that was present
got = _intersect_items(existing_tags, new_tags)
self.assertEqual(want, got)

View File

@@ -1,8 +1,8 @@
import collections
import inspect
import mock
import pytest
import yaml
import inspect
import collections
from ansible.module_utils.six import string_types
from ansible.modules.cloud.openstack import os_server
@@ -188,12 +188,9 @@ class TestCreateServer(object):
os_server._create_server(self.module, self.cloud)
assert(self.cloud.create_server.call_count == 1)
assert(self.cloud.create_server.call_args[1]['image']
== self.cloud.get_image_id('cirros'))
assert(self.cloud.create_server.call_args[1]['flavor']
== self.cloud.get_flavor('m1.tiny')['id'])
assert(self.cloud.create_server.call_args[1]['nics'][0]['net-id']
== self.cloud.get_network('network1')['id'])
assert(self.cloud.create_server.call_args[1]['image'] == self.cloud.get_image_id('cirros'))
assert(self.cloud.create_server.call_args[1]['flavor'] == self.cloud.get_flavor('m1.tiny')['id'])
assert(self.cloud.create_server.call_args[1]['nics'][0]['net-id'] == self.cloud.get_network('network1')['id'])
def test_create_server_bad_flavor(self):
'''

View File

@@ -14,13 +14,12 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import unittest
from ansible.modules.network.cumulus import nclu
import sys
import time
from ansible.module_utils.basic import *
import unittest
from ansible.module_utils.basic import *
from ansible.modules.network.cumulus import nclu
class FakeModule(object):
@@ -172,7 +171,6 @@ class TestNclu(unittest.TestCase):
self.assertEqual(module.fail_code, {})
self.assertEqual(changed, True)
def test_command_atomic(self):
module = FakeModule()
changed, output = nclu.run_nclu(module,

View File

@@ -19,8 +19,8 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import os
import json
import os
from ansible.compat.tests import unittest
from ansible.compat.tests.mock import patch
@@ -35,6 +35,7 @@ def set_module_args(args):
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
fixture_data = {}
def load_fixture(name):
path = os.path.join(fixture_path, name)
@@ -56,13 +57,14 @@ def load_fixture(name):
class AnsibleExitJson(Exception):
pass
class AnsibleFailJson(Exception):
pass
class TestEosModule(unittest.TestCase):
def execute_module(self, failed=False, changed=False, commands=None,
sort=True, defaults=False, transport='cli'):
def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False, transport='cli'):
self.load_fixtures(commands, transport=transport)
@@ -110,4 +112,3 @@ class TestEosModule(unittest.TestCase):
def load_fixtures(self, commands=None, transport='cli'):
pass

View File

@@ -25,6 +25,7 @@ from ansible.compat.tests.mock import patch
from ansible.modules.network.eos import eos_command
from .eos_module import TestEosModule, load_fixture, set_module_args
class TestEosCommandModule(TestEosModule):
module = eos_command

View File

@@ -71,7 +71,7 @@ class TestEosConfigModule(TestEosModule):
def test_eos_config_before(self):
args = dict(lines=['hostname switch01', 'ip domain-name eng.ansible.com'],
before=['before command'])
before=['before command'])
set_module_args(args)
@@ -83,7 +83,7 @@ class TestEosConfigModule(TestEosModule):
def test_eos_config_after(self):
args = dict(lines=['hostname switch01', 'ip domain-name eng.ansible.com'],
after=['after command'])
after=['after command'])
set_module_args(args)

View File

@@ -25,6 +25,7 @@ from ansible.compat.tests.mock import patch
from ansible.modules.network.eos import eos_system
from .eos_module import TestEosModule, load_fixture, set_module_args
class TestEosSystemModule(TestEosModule):
module = eos_system
@@ -75,20 +76,20 @@ class TestEosSystemModule(TestEosModule):
'ip domain lookup source-interface Ethernet1']
self.execute_module(changed=True, commands=commands)
#def test_eos_system_name_servers(self):
# name_servers = ['8.8.8.8', '8.8.4.4']
# set_module_args(dict(name_servers=name_servers))
# commands = ['ip name-server 8.8.4.4',
# 'no ip name-server vrf mgmt 8.8.4.4']
# self.execute_module(changed=True, commands=commands)
# def test_eos_system_name_servers(self):
# name_servers = ['8.8.8.8', '8.8.4.4']
# set_module_args(dict(name_servers=name_servers))
# commands = ['ip name-server 8.8.4.4',
# 'no ip name-server vrf mgmt 8.8.4.4']
# self.execute_module(changed=True, commands=commands)
#def rest_eos_system_name_servers_complex(self):
# name_servers = dict(server='8.8.8.8', vrf='test')
# set_module_args(dict(name_servers=name_servers))
# commands = ['ip name-server vrf test 8.8.8.8',
# 'no ip name-server vrf default 8.8.8.8',
# 'no ip name-server vrf mgmt 8.8.4.4']
# self.execute_module(changed=True, commands=commands)
# def rest_eos_system_name_servers_complex(self):
# name_servers = dict(server='8.8.8.8', vrf='test')
# set_module_args(dict(name_servers=name_servers))
# commands = ['ip name-server vrf test 8.8.8.8',
# 'no ip name-server vrf default 8.8.8.8',
# 'no ip name-server vrf mgmt 8.8.4.4']
# self.execute_module(changed=True, commands=commands)
def test_eos_system_state_absent(self):
set_module_args(dict(state='absent'))
@@ -104,4 +105,3 @@ class TestEosSystemModule(TestEosModule):
name_servers = dict(server='8.8.8.8', vrf='missing')
set_module_args(dict(name_servers=name_servers))
result = self.execute_module(failed=True)

View File

@@ -95,5 +95,3 @@ class TestEosUserModule(TestEosModule):
set_module_args(dict(username='ansible', password='test', update_password='always'))
commands = ['username ansible secret test']
self.execute_module(changed=True, commands=commands)

View File

@@ -35,6 +35,7 @@ def set_module_args(args):
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
fixture_data = {}
def load_fixture(name):
path = os.path.join(fixture_path, name)
@@ -56,13 +57,14 @@ def load_fixture(name):
class AnsibleExitJson(Exception):
pass
class AnsibleFailJson(Exception):
pass
class TestIosModule(unittest.TestCase):
def execute_module(self, failed=False, changed=False, commands=None,
sort=True, defaults=False):
def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False):
self.load_fixtures(commands)
@@ -110,4 +112,3 @@ class TestIosModule(unittest.TestCase):
def load_fixtures(self, commands=None):
pass

View File

@@ -40,8 +40,7 @@ class TestIosBannerModule(TestIosModule):
self.mock_load_config.stop()
def load_fixtures(self, commands=None):
self.exec_command.return_value = (0,
load_fixture('ios_banner_show_banner.txt').strip(), None)
self.exec_command.return_value = (0, load_fixture('ios_banner_show_banner.txt').strip(), None)
self.load_config.return_value = dict(diff=None, session='session')
def test_ios_banner_create(self):

View File

@@ -25,6 +25,7 @@ from ansible.compat.tests.mock import patch
from ansible.modules.network.ios import ios_command
from .ios_module import TestIosModule, load_fixture, set_module_args
class TestIosCommandModule(TestIosModule):
module = ios_command

View File

@@ -89,19 +89,19 @@ class TestIosConfigModule(TestIosModule):
self.execute_module(changed=True, commands=commands)
def test_ios_config_before(self):
set_module_args(dict(lines=['hostname foo'], before=['test1','test2']))
set_module_args(dict(lines=['hostname foo'], before=['test1', 'test2']))
commands = ['test1', 'test2', 'hostname foo']
self.execute_module(changed=True, commands=commands, sort=False)
def test_ios_config_after(self):
set_module_args(dict(lines=['hostname foo'], after=['test1','test2']))
set_module_args(dict(lines=['hostname foo'], after=['test1', 'test2']))
commands = ['hostname foo', 'test1', 'test2']
self.execute_module(changed=True, commands=commands, sort=False)
def test_ios_config_before_after_no_change(self):
set_module_args(dict(lines=['hostname router'],
before=['test1', 'test2'],
after=['test3','test4']))
after=['test3', 'test4']))
self.execute_module()
def test_ios_config_config(self):

View File

@@ -51,7 +51,6 @@ class TestIosSystemModule(TestIosModule):
commands = ['hostname foo']
self.execute_module(changed=True, commands=commands)
def test_ios_system_domain_name(self):
set_module_args(dict(domain_name=['test.com']))
commands = ['ip domain name test.com',
@@ -120,4 +119,3 @@ class TestIosSystemModule(TestIosModule):
name_servers = dict(server='8.8.8.8', vrf='missing')
set_module_args(dict(name_servers=name_servers))
self.execute_module(failed=True)

View File

@@ -26,6 +26,7 @@ from ansible.compat.tests.mock import patch
from ansible.modules.network.ios import _ios_template
from .ios_module import TestIosModule, load_fixture, set_module_args
class TestIosTemplateModule(TestIosModule):
module = _ios_template

View File

@@ -122,5 +122,3 @@ class TestIosVrfModule(TestIosModule):
commands = ['no vrf definition test_1', 'vrf definition test_2',
'description test string']
self.execute_module(changed=True, commands=commands, sort=False)

View File

@@ -35,6 +35,7 @@ def set_module_args(args):
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
fixture_data = {}
def load_fixture(name):
path = os.path.join(fixture_path, name)
@@ -56,13 +57,14 @@ def load_fixture(name):
class AnsibleExitJson(Exception):
pass
class AnsibleFailJson(Exception):
pass
class TestIosxrModule(unittest.TestCase):
def execute_module(self, failed=False, changed=False, commands=None,
sort=True, defaults=False):
def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False):
self.load_fixtures(commands)
@@ -110,4 +112,3 @@ class TestIosxrModule(unittest.TestCase):
def load_fixtures(self, commands=None):
pass

View File

@@ -26,6 +26,7 @@ from ansible.compat.tests.mock import patch
from ansible.modules.network.iosxr import iosxr_config
from .iosxr_module import TestIosxrModule, load_fixture, set_module_args
class TestIosxrConfigModule(TestIosxrModule):
module = iosxr_config
@@ -73,19 +74,19 @@ class TestIosxrConfigModule(TestIosxrModule):
self.execute_module(changed=True, commands=commands)
def test_iosxr_config_before(self):
set_module_args(dict(lines=['hostname foo'], before=['test1','test2']))
set_module_args(dict(lines=['hostname foo'], before=['test1', 'test2']))
commands = ['test1', 'test2', 'hostname foo']
self.execute_module(changed=True, commands=commands, sort=False)
def test_iosxr_config_after(self):
set_module_args(dict(lines=['hostname foo'], after=['test1','test2']))
set_module_args(dict(lines=['hostname foo'], after=['test1', 'test2']))
commands = ['hostname foo', 'test1', 'test2']
self.execute_module(changed=True, commands=commands, sort=False)
def test_iosxr_config_before_after_no_change(self):
set_module_args(dict(lines=['hostname router'],
before=['test1', 'test2'],
after=['test3','test4']))
after=['test3', 'test4']))
self.execute_module()
def test_iosxr_config_config(self):

View File

@@ -66,8 +66,7 @@ class TestIosxrFacts(TestIosxrModule):
self.assertIn('interfaces', ansible_facts['ansible_net_gather_subset'])
self.assertEquals('iosxr01', ansible_facts['ansible_net_hostname'])
self.assertEquals(['disk0:', 'flash0:'], ansible_facts['ansible_net_filesystems'])
self.assertIn('GigabitEthernet0/0/0/0',
ansible_facts['ansible_net_interfaces'].keys())
self.assertIn('GigabitEthernet0/0/0/0', ansible_facts['ansible_net_interfaces'].keys())
self.assertEquals('3095', ansible_facts['ansible_net_memtotal_mb'])
self.assertEquals('1499', ansible_facts['ansible_net_memfree_mb'])

View File

@@ -57,7 +57,7 @@ class TestIosxrSystemModule(TestIosxrModule):
def test_iosxr_system_domain_search(self):
set_module_args(dict(domain_search=['ansible.com', 'redhat.com']))
commands=['domain list ansible.com', 'no domain list cisco.com']
commands = ['domain list ansible.com', 'no domain list cisco.com']
self.execute_module(changed=True, commands=commands)
def test_iosxr_system_lookup_source(self):
@@ -78,14 +78,18 @@ class TestIosxrSystemModule(TestIosxrModule):
def test_iosxr_system_state_absent(self):
set_module_args(dict(state='absent'))
commands = ['no hostname', 'no domain name',
'no domain lookup disable',
'no domain lookup source-interface MgmtEth0/0/CPU0/0',
'no domain list redhat.com', 'no domain list cisco.com',
'no domain name-server 8.8.8.8', 'no domain name-server 8.8.4.4']
commands = [
'no hostname',
'no domain name',
'no domain lookup disable',
'no domain lookup source-interface MgmtEth0/0/CPU0/0',
'no domain list redhat.com',
'no domain list cisco.com',
'no domain name-server 8.8.8.8',
'no domain name-server 8.8.4.4'
]
self.execute_module(changed=True, commands=commands)
def test_iosxr_system_no_change(self):
set_module_args(dict(hostname='iosxr01', domain_name='eng.ansible.com'))
self.execute_module()

View File

@@ -35,6 +35,7 @@ def set_module_args(args):
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
fixture_data = {}
def load_fixture(name):
path = os.path.join(fixture_path, name)
@@ -56,13 +57,14 @@ def load_fixture(name):
class AnsibleExitJson(Exception):
pass
class AnsibleFailJson(Exception):
pass
class TestNxosModule(unittest.TestCase):
def execute_module(self, failed=False, changed=False, commands=None,
sort=True, defaults=False):
def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False):
self.load_fixtures(commands)
@@ -110,4 +112,3 @@ class TestNxosModule(unittest.TestCase):
def load_fixtures(self, commands=None):
pass

View File

@@ -25,6 +25,7 @@ from ansible.compat.tests.mock import patch
from ansible.modules.network.nxos import nxos_command
from .nxos_module import TestNxosModule, load_fixture, set_module_args
class TestNxosCommandModule(TestNxosModule):
module = nxos_command

View File

@@ -73,7 +73,7 @@ class TestNxosConfigModule(TestNxosModule):
def test_nxos_config_before(self):
args = dict(lines=['hostname switch01', 'ip domain-name eng.ansible.com'],
before=['before command'])
before=['before command'])
set_module_args(args)
@@ -85,7 +85,7 @@ class TestNxosConfigModule(TestNxosModule):
def test_nxos_config_after(self):
args = dict(lines=['hostname switch01', 'ip domain-name eng.ansible.com'],
after=['after command'])
after=['after command'])
set_module_args(args)
@@ -134,6 +134,3 @@ class TestNxosConfigModule(TestNxosModule):
set_module_args(args)
result = self.execute_module()
self.assertIn('__backup__', result)

View File

@@ -61,4 +61,3 @@ class TestNxosEvpnGlobalModule(TestNxosModule):
set_module_args(dict(nv_overlay_evpn=False))
commands = ['no nv overlay evpn']
self.start_configured(changed=True, commands=commands)

View File

@@ -126,5 +126,3 @@ class TestNxosSystemModule(TestNxosModule):
'vrf context management', 'no ip name-server 172.26.1.1', 'exit',
'no system jumbomtu']
self.execute_module(changed=True, commands=commands)

View File

@@ -25,6 +25,7 @@ from ansible.compat.tests.mock import patch
from ansible.modules.network.vyos import vyos_command
from .vyos_module import TestVyosModule, load_fixture, set_module_args
class TestVyosCommandModule(TestVyosModule):
module = vyos_command

View File

@@ -35,6 +35,7 @@ def set_module_args(args):
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
fixture_data = {}
def load_fixture(name):
path = os.path.join(fixture_path, name)
@@ -56,14 +57,14 @@ def load_fixture(name):
class AnsibleExitJson(Exception):
pass
class AnsibleFailJson(Exception):
pass
class TestVyosModule(unittest.TestCase):
def execute_module(self, failed=False, changed=False, commands=None,
sort=True, defaults=False):
def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False):
self.load_fixtures(commands)
if failed:
@@ -110,4 +111,3 @@ class TestVyosModule(unittest.TestCase):
def load_fixtures(self, commands=None):
pass

View File

@@ -20,9 +20,10 @@ class AptExpandPkgspecTestCase(unittest.TestCase):
def setUp(self):
FakePackage = collections.namedtuple("Package", ("name",))
self.fake_cache = [ FakePackage("apt"),
FakePackage("apt-utils"),
FakePackage("not-selected"),
self.fake_cache = [
FakePackage("apt"),
FakePackage("apt-utils"),
FakePackage("not-selected"),
]
def test_trivial(self):

View File

@@ -36,11 +36,8 @@ class KnownHostsDiffTestCase(unittest.TestCase):
self.assertEqual(diff, {
'before_header': path,
'after_header': path,
'before':
'two.example.com ssh-rsa BBBBetc\n',
'after':
'two.example.com ssh-rsa BBBBetc\n'
'one.example.com ssh-rsa AAAAetc\n',
'before': 'two.example.com ssh-rsa BBBBetc\n',
'after': 'two.example.com ssh-rsa BBBBetc\none.example.com ssh-rsa AAAAetc\n',
})
def test_no_change(self):
@@ -53,12 +50,8 @@ class KnownHostsDiffTestCase(unittest.TestCase):
self.assertEqual(diff, {
'before_header': path,
'after_header': path,
'before':
'one.example.com ssh-rsa AAAAetc\n'
'two.example.com ssh-rsa BBBBetc\n',
'after':
'one.example.com ssh-rsa AAAAetc\n'
'two.example.com ssh-rsa BBBBetc\n',
'before': 'one.example.com ssh-rsa AAAAetc\ntwo.example.com ssh-rsa BBBBetc\n',
'after': 'one.example.com ssh-rsa AAAAetc\ntwo.example.com ssh-rsa BBBBetc\n',
})
def test_key_change(self):
@@ -71,12 +64,8 @@ class KnownHostsDiffTestCase(unittest.TestCase):
self.assertEqual(diff, {
'before_header': path,
'after_header': path,
'before':
'one.example.com ssh-rsa AAAaetc\n'
'two.example.com ssh-rsa BBBBetc\n',
'after':
'two.example.com ssh-rsa BBBBetc\n'
'one.example.com ssh-rsa AAAAetc\n',
'before': 'one.example.com ssh-rsa AAAaetc\ntwo.example.com ssh-rsa BBBBetc\n',
'after': 'two.example.com ssh-rsa BBBBetc\none.example.com ssh-rsa AAAAetc\n',
})
def test_key_removal(self):
@@ -89,11 +78,8 @@ class KnownHostsDiffTestCase(unittest.TestCase):
self.assertEqual(diff, {
'before_header': path,
'after_header': path,
'before':
'one.example.com ssh-rsa AAAAetc\n'
'two.example.com ssh-rsa BBBBetc\n',
'after':
'two.example.com ssh-rsa BBBBetc\n',
'before': 'one.example.com ssh-rsa AAAAetc\ntwo.example.com ssh-rsa BBBBetc\n',
'after': 'two.example.com ssh-rsa BBBBetc\n',
})
def test_key_removal_no_change(self):
@@ -105,8 +91,6 @@ class KnownHostsDiffTestCase(unittest.TestCase):
self.assertEqual(diff, {
'before_header': path,
'after_header': path,
'before':
'two.example.com ssh-rsa BBBBetc\n',
'after':
'two.example.com ssh-rsa BBBBetc\n',
'before': 'two.example.com ssh-rsa BBBBetc\n',
'after': 'two.example.com ssh-rsa BBBBetc\n',
})