Replace @mock_ec2 calls with @mock_ec2_deprecated calls

moto versions >= 1.0.0 have changed the way the mocked connections through
boto are handled with the @mock_ec2 decorator. They use the boto3 connection
method. However, since we are still using boto in many places, we need to use
the new @mock_ec2_deprecated decorator instead to handle the boto connection
functions for the unit tests.

Versions of moto < 1.0.0 are not Python 3 compatible, so salt-jenkins should
be installing newer versions of moto for those tests. Unfortunately, we cannot
install an older version of moto for Python2 that use the original @mock_ec2 call
and also import the @mock_ec2_deprecated function for newer versions of moto
simultaneously as the @mock_ec2_deprecated function doesn't exist in older
versions of moto.
This commit is contained in:
rallytime 2017-08-10 16:40:11 -04:00
parent 6366e05d0d
commit 6ae1111295
4 changed files with 177 additions and 187 deletions

View file

@ -15,17 +15,17 @@ except ImportError:
HAS_BOTO = False
try:
from moto import mock_ec2, mock_elb
from moto import mock_ec2_deprecated, mock_elb
HAS_MOTO = True
except ImportError:
HAS_MOTO = False
def mock_ec2(self):
def mock_ec2_deprecated(self):
'''
if the mock_ec2 function is not available due to import failure
if the mock_ec2_deprecated function is not available due to import failure
this replaces the decorated function with stub_function.
Allows boto_vpc unit tests to use the @mock_ec2 decorator
without a "NameError: name 'mock_ec2' is not defined" error.
Allows boto_vpc unit tests to use the @mock_ec2_deprecated decorator
without a "NameError: name 'mock_ec2_deprecated' is not defined" error.
'''
def stub_function(self):
pass
@ -33,10 +33,10 @@ except ImportError:
def mock_elb(self):
'''
if the mock_ec2 function is not available due to import failure
if the mock_ec2_deprecated function is not available due to import failure
this replaces the decorated function with stub_function.
Allows boto_vpc unit tests to use the @mock_ec2 decorator
without a "NameError: name 'mock_ec2' is not defined" error.
Allows boto_vpc unit tests to use the @mock_ec2_deprecated decorator
without a "NameError: name 'mock_ec2_deprecated' is not defined" error.
'''
def stub_function(self):
pass
@ -83,7 +83,7 @@ class BotoElbTestCase(TestCase):
'''
TestCase for salt.modules.boto_elb module
'''
@mock_ec2
@mock_ec2_deprecated
@mock_elb
def test_register_instances_valid_id_result_true(self):
'''
@ -102,7 +102,7 @@ class BotoElbTestCase(TestCase):
**conn_parameters)
self.assertEqual(True, register_result)
@mock_ec2
@mock_ec2_deprecated
@mock_elb
def test_register_instances_valid_id_string(self):
'''
@ -125,7 +125,7 @@ class BotoElbTestCase(TestCase):
log.debug(load_balancer_refreshed.instances)
self.assertEqual([reservations.instances[0].id], registered_instance_ids)
@mock_ec2
@mock_ec2_deprecated
@mock_elb
def test_deregister_instances_valid_id_result_true(self):
'''
@ -146,7 +146,7 @@ class BotoElbTestCase(TestCase):
**conn_parameters)
self.assertEqual(True, deregister_result)
@mock_ec2
@mock_ec2_deprecated
@mock_elb
def test_deregister_instances_valid_id_string(self):
'''
@ -172,7 +172,7 @@ class BotoElbTestCase(TestCase):
load_balancer_refreshed.instances]
self.assertEqual(actual_instances, expected_instances)
@mock_ec2
@mock_ec2_deprecated
@mock_elb
def test_deregister_instances_valid_id_list(self):
'''

View file

@ -29,17 +29,17 @@ except ImportError:
HAS_BOTO = False
try:
from moto import mock_ec2
from moto import mock_ec2_deprecated
HAS_MOTO = True
except ImportError:
HAS_MOTO = False
def mock_ec2(self):
def mock_ec2_deprecated(self):
'''
if the mock_ec2 function is not available due to import failure
if the mock_ec2_deprecated function is not available due to import failure
this replaces the decorated function with stub_function.
Allows boto_secgroup unit tests to use the @mock_ec2 decorator
without a "NameError: name 'mock_ec2' is not defined" error.
Allows boto_secgroup unit tests to use the @mock_ec2_deprecated decorator
without a "NameError: name 'mock_ec2_deprecated' is not defined" error.
'''
def stub_function(self):
pass
@ -111,7 +111,7 @@ class BotoSecgroupTestCase(TestCase):
{'to_port': 80, 'from_port': 80, 'ip_protocol': u'tcp', 'cidr_ip': u'0.0.0.0/0'}]
self.assertEqual(boto_secgroup._split_rules(rules), split_rules)
@mock_ec2
@mock_ec2_deprecated
def test_create_ec2_classic(self):
'''
Test of creation of an EC2-Classic security group. The test ensures
@ -131,7 +131,7 @@ class BotoSecgroupTestCase(TestCase):
secgroup_created_group[0].vpc_id]
self.assertEqual(expected_create_result, secgroup_create_result)
@mock_ec2
@mock_ec2_deprecated
def test_create_ec2_vpc(self):
'''
test of creation of an EC2-VPC security group. The test ensures that a
@ -151,7 +151,7 @@ class BotoSecgroupTestCase(TestCase):
@skipIf(True, 'test skipped due to error in moto return - fixed in'
' https://github.com/spulec/moto/commit/cc0166964371f7b5247a49d45637a8f936ccbe6f')
@mock_ec2
@mock_ec2_deprecated
def test_get_group_id_ec2_classic(self):
'''
tests that given a name of a group in EC2-Classic that the correct
@ -173,7 +173,7 @@ class BotoSecgroupTestCase(TestCase):
@skipIf(True, 'test skipped because moto does not yet support group'
' filters https://github.com/spulec/moto/issues/154')
@mock_ec2
@mock_ec2_deprecated
def test_get_group_id_ec2_vpc(self):
'''
tests that given a name of a group in EC2-VPC that the correct
@ -193,7 +193,7 @@ class BotoSecgroupTestCase(TestCase):
**conn_parameters)
self.assertEqual(group_vpc.id, retrieved_group_id)
@mock_ec2
@mock_ec2_deprecated
def test_get_config_single_rule_group_name(self):
'''
tests return of 'config' when given group name. get_config returns an OrderedDict.
@ -219,7 +219,7 @@ class BotoSecgroupTestCase(TestCase):
@skipIf(True, 'test skipped due to error in moto return - fixed in '
'https://github.com/spulec/moto/commit/cc0166964371f7b5247a49d45637a8f936ccbe6f')
@mock_ec2
@mock_ec2_deprecated
def test_exists_true_name_classic(self):
'''
tests 'true' existence of a group in EC2-Classic when given name
@ -234,11 +234,11 @@ class BotoSecgroupTestCase(TestCase):
@skipIf(True, 'test skipped because moto does not yet support group'
' filters https://github.com/spulec/moto/issues/154')
@mock_ec2
@mock_ec2_deprecated
def test_exists_false_name_classic(self):
pass
@mock_ec2
@mock_ec2_deprecated
def test_exists_true_name_vpc(self):
'''
tests 'true' existence of a group in EC2-VPC when given name and vpc_id
@ -250,7 +250,7 @@ class BotoSecgroupTestCase(TestCase):
salt_exists_result = boto_secgroup.exists(name=group_name, vpc_id=vpc_id, **conn_parameters)
self.assertTrue(salt_exists_result)
@mock_ec2
@mock_ec2_deprecated
def test_exists_false_name_vpc(self):
'''
tests 'false' existence of a group in vpc when given name and vpc_id
@ -259,7 +259,7 @@ class BotoSecgroupTestCase(TestCase):
salt_exists_result = boto_secgroup.exists(group_name, vpc_id=vpc_id, **conn_parameters)
self.assertFalse(salt_exists_result)
@mock_ec2
@mock_ec2_deprecated
def test_exists_true_group_id(self):
'''
tests 'true' existence of a group when given group_id
@ -271,7 +271,7 @@ class BotoSecgroupTestCase(TestCase):
salt_exists_result = boto_secgroup.exists(group_id=group.id, **conn_parameters)
self.assertTrue(salt_exists_result)
@mock_ec2
@mock_ec2_deprecated
def test_exists_false_group_id(self):
'''
tests 'false' existence of a group when given group_id
@ -282,7 +282,7 @@ class BotoSecgroupTestCase(TestCase):
@skipIf(True, 'test skipped due to error in moto return - fixed in'
' https://github.com/spulec/moto/commit/cc0166964371f7b5247a49d45637a8f936ccbe6f')
@mock_ec2
@mock_ec2_deprecated
def test_delete_group_ec2_classic(self):
'''
test deletion of a group in EC2-Classic. Test does the following:
@ -310,11 +310,11 @@ class BotoSecgroupTestCase(TestCase):
@skipIf(True, 'test skipped because moto does not yet support group'
' filters https://github.com/spulec/moto/issues/154')
@mock_ec2
@mock_ec2_deprecated
def test_delete_group_name_ec2_vpc(self):
pass
@mock_ec2
@mock_ec2_deprecated
def test__get_conn_true(self):
'''
tests ensures that _get_conn returns an boto.ec2.connection.EC2Connection object.

File diff suppressed because it is too large Load diff

View file

@ -34,18 +34,18 @@ except ImportError:
HAS_BOTO = False
try:
from moto import mock_ec2
from moto import mock_ec2_deprecated
HAS_MOTO = True
except ImportError:
HAS_MOTO = False
def mock_ec2(self):
def mock_ec2_deprecated(self):
'''
if the mock_ec2 function is not available due to import failure
if the mock_ec2_deprecated function is not available due to import failure
this replaces the decorated function with stub_function.
Allows boto_vpc unit tests to use the @mock_ec2 decorator
without a "NameError: name 'mock_ec2' is not defined" error.
Allows boto_vpc unit tests to use the @mock_ec2_deprecated decorator
without a "NameError: name 'mock_ec2_deprecated' is not defined" error.
'''
def stub_function(self):
@ -112,7 +112,7 @@ class BotoVpcTestCase(BotoVpcStateTestCaseBase, BotoVpcTestCaseMixin):
TestCase for salt.states.boto_vpc state.module
'''
@mock_ec2
@mock_ec2_deprecated
def test_present_when_vpc_does_not_exist(self):
'''
Tests present on a VPC that does not exist.
@ -123,14 +123,14 @@ class BotoVpcTestCase(BotoVpcStateTestCaseBase, BotoVpcTestCaseMixin):
self.assertTrue(vpc_present_result['result'])
self.assertEqual(vpc_present_result['changes']['new']['vpc']['state'], 'available')
@mock_ec2
@mock_ec2_deprecated
def test_present_when_vpc_exists(self):
vpc = self._create_vpc(name='test')
vpc_present_result = salt_states['boto_vpc.present']('test', cidr_block)
self.assertTrue(vpc_present_result['result'])
self.assertEqual(vpc_present_result['changes'], {})
@mock_ec2
@mock_ec2_deprecated
@skipIf(True, 'Disabled pending https://github.com/spulec/moto/issues/493')
def test_present_with_failure(self):
with patch('moto.ec2.models.VPCBackend.create_vpc', side_effect=BotoServerError(400, 'Mocked error')):
@ -138,7 +138,7 @@ class BotoVpcTestCase(BotoVpcStateTestCaseBase, BotoVpcTestCaseMixin):
self.assertFalse(vpc_present_result['result'])
self.assertTrue('Mocked error' in vpc_present_result['comment'])
@mock_ec2
@mock_ec2_deprecated
def test_absent_when_vpc_does_not_exist(self):
'''
Tests absent on a VPC that does not exist.
@ -148,7 +148,7 @@ class BotoVpcTestCase(BotoVpcStateTestCaseBase, BotoVpcTestCaseMixin):
self.assertTrue(vpc_absent_result['result'])
self.assertEqual(vpc_absent_result['changes'], {})
@mock_ec2
@mock_ec2_deprecated
def test_absent_when_vpc_exists(self):
vpc = self._create_vpc(name='test')
with patch.dict('salt.utils.boto.__salt__', funcs):
@ -156,7 +156,7 @@ class BotoVpcTestCase(BotoVpcStateTestCaseBase, BotoVpcTestCaseMixin):
self.assertTrue(vpc_absent_result['result'])
self.assertEqual(vpc_absent_result['changes']['new']['vpc'], None)
@mock_ec2
@mock_ec2_deprecated
@skipIf(True, 'Disabled pending https://github.com/spulec/moto/issues/493')
def test_absent_with_failure(self):
vpc = self._create_vpc(name='test')
@ -176,7 +176,7 @@ class BotoVpcResourceTestCaseMixin(BotoVpcTestCaseMixin):
_create = getattr(self, '_create_' + self.resource_type)
_create(vpc_id=vpc_id, name=name, **self.extra_kwargs)
@mock_ec2
@mock_ec2_deprecated
def test_present_when_resource_does_not_exist(self):
'''
Tests present on a resource that does not exist.
@ -191,7 +191,7 @@ class BotoVpcResourceTestCaseMixin(BotoVpcTestCaseMixin):
exists = funcs['boto_vpc.resource_exists'](self.resource_type, 'test').get('exists')
self.assertTrue(exists)
@mock_ec2
@mock_ec2_deprecated
def test_present_when_resource_exists(self):
vpc = self._create_vpc(name='test')
resource = self._create_resource(vpc_id=vpc.id, name='test')
@ -201,7 +201,7 @@ class BotoVpcResourceTestCaseMixin(BotoVpcTestCaseMixin):
self.assertTrue(resource_present_result['result'])
self.assertEqual(resource_present_result['changes'], {})
@mock_ec2
@mock_ec2_deprecated
@skipIf(True, 'Disabled pending https://github.com/spulec/moto/issues/493')
def test_present_with_failure(self):
vpc = self._create_vpc(name='test')
@ -212,7 +212,7 @@ class BotoVpcResourceTestCaseMixin(BotoVpcTestCaseMixin):
self.assertFalse(resource_present_result['result'])
self.assertTrue('Mocked error' in resource_present_result['comment'])
@mock_ec2
@mock_ec2_deprecated
def test_absent_when_resource_does_not_exist(self):
'''
Tests absent on a resource that does not exist.
@ -222,7 +222,7 @@ class BotoVpcResourceTestCaseMixin(BotoVpcTestCaseMixin):
self.assertTrue(resource_absent_result['result'])
self.assertEqual(resource_absent_result['changes'], {})
@mock_ec2
@mock_ec2_deprecated
def test_absent_when_resource_exists(self):
vpc = self._create_vpc(name='test')
self._create_resource(vpc_id=vpc.id, name='test')
@ -234,7 +234,7 @@ class BotoVpcResourceTestCaseMixin(BotoVpcTestCaseMixin):
exists = funcs['boto_vpc.resource_exists'](self.resource_type, 'test').get('exists')
self.assertFalse(exists)
@mock_ec2
@mock_ec2_deprecated
@skipIf(True, 'Disabled pending https://github.com/spulec/moto/issues/493')
def test_absent_with_failure(self):
vpc = self._create_vpc(name='test')
@ -282,7 +282,7 @@ class BotoVpcRouteTableTestCase(BotoVpcStateTestCaseBase, BotoVpcResourceTestCas
backend_create = 'RouteTableBackend.create_route_table'
backend_delete = 'RouteTableBackend.delete_route_table'
@mock_ec2
@mock_ec2_deprecated
def test_present_with_subnets(self):
vpc = self._create_vpc(name='test')
subnet1 = self._create_subnet(vpc_id=vpc.id, name='test1')
@ -307,7 +307,7 @@ class BotoVpcRouteTableTestCase(BotoVpcStateTestCaseBase, BotoVpcResourceTestCas
new_subnets = changes['new']['subnets_associations']
self.assertEqual(new_subnets[0]['subnet_id'], subnet2.id)
@mock_ec2
@mock_ec2_deprecated
def test_present_with_routes(self):
vpc = self._create_vpc(name='test')
igw = self._create_internet_gateway(name='test', vpc_id=vpc.id)