mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-15 17:40:50 -07:00
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:
parent
31c59ad5f9
commit
4efec414e7
110 changed files with 1702 additions and 1547 deletions
|
@ -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')
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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):
|
||||
'''
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue