mirror of
https://github.com/valitydev/salt.git
synced 2024-11-06 16:45:27 +00:00
provide True, False and None returns on deregister_instances and register_instances methods
This commit is contained in:
parent
78b7ace6e1
commit
754c01448e
@ -501,16 +501,40 @@ def register_instances(name, instances, region=None, key=None, keyid=None,
|
||||
Register instances with an ELB. Instances is either a string
|
||||
instance id or a list of string instance id's.
|
||||
|
||||
CLI example to set attributes on an ELB::
|
||||
Returns:
|
||||
|
||||
- ``True``: instance(s) registered successfully
|
||||
- ``False``: instance(s) failed to be registered
|
||||
|
||||
CLI example::
|
||||
|
||||
salt myminion boto_elb.register_instances myelb instance_id
|
||||
salt myminion boto_elb.register_instances myelb "[instance_id,instance_id]"
|
||||
'''
|
||||
# convert instances to list type, enabling consistent use of instances
|
||||
# variable throughout the register_instances method
|
||||
if isinstance(instances, str) or isinstance(instances, unicode):
|
||||
instances = [instances]
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return False
|
||||
load_balancer = conn.get_all_load_balancers(name)[0]
|
||||
return load_balancer.register_instances(instances)
|
||||
try:
|
||||
registered_instances = conn.register_instances(name, instances)
|
||||
except boto.exception.BotoServerError as e:
|
||||
log.warn(e)
|
||||
return False
|
||||
registered_instance_ids = [instance.id for instance in
|
||||
registered_instances]
|
||||
# register_failues is a set that will contain any instances that were not
|
||||
# able to be registered with the given ELB
|
||||
register_failures = set(instances).difference(set(registered_instance_ids))
|
||||
if register_failures:
|
||||
log.warn('Instance(s): {0} not registered with ELB {1}.'
|
||||
.format(list(register_failures), name))
|
||||
register_result = False
|
||||
else:
|
||||
register_result = True
|
||||
return register_result
|
||||
|
||||
|
||||
def deregister_instances(name, instances, region=None, key=None, keyid=None,
|
||||
@ -519,16 +543,51 @@ def deregister_instances(name, instances, region=None, key=None, keyid=None,
|
||||
Deregister instances with an ELB. Instances is either a string
|
||||
instance id or a list of string instance id's.
|
||||
|
||||
CLI example to set attributes on an ELB::
|
||||
Returns:
|
||||
|
||||
- ``True``: instance(s) deregistered successfully
|
||||
- ``False``: instance(s) failed to be deregistered
|
||||
- ``None``: instance(s) not valid or not registered, no action taken
|
||||
|
||||
CLI example::
|
||||
|
||||
salt myminion boto_elb.deregister_instances myelb instance_id
|
||||
salt myminion boto_elb.deregister_instances myelb "[instance_id,instance_id]"
|
||||
salt myminion boto_elb.deregister_instances myelb "[instance_id, instance_id]"
|
||||
'''
|
||||
# convert instances to list type, enabling consistent use of instances
|
||||
# variable throughout the deregister_instances method
|
||||
if isinstance(instances, str) or isinstance(instances, unicode):
|
||||
instances = [instances]
|
||||
conn = _get_conn(region, key, keyid, profile)
|
||||
if not conn:
|
||||
return False
|
||||
load_balancer = conn.get_all_load_balancers(name)[0]
|
||||
return load_balancer.deregister_instances(instances)
|
||||
try:
|
||||
registered_instances = conn.deregister_instances(name, instances)
|
||||
except boto.exception.BotoServerError as e:
|
||||
# if the instance(s) given as an argument are not members of the ELB
|
||||
# boto returns e.error_code == 'InvalidInstance'
|
||||
# deregister_instances returns "None" because the instances are
|
||||
# effectively deregistered from ELB
|
||||
if e.error_code == 'InvalidInstance':
|
||||
log.warn('One or more of instance(s) {0} are not part of ELB {1}.'
|
||||
' deregister_instances not performed.'
|
||||
.format(instances, name))
|
||||
return None
|
||||
else:
|
||||
log.warn(e)
|
||||
return False
|
||||
registered_instance_ids = [instance.id for instance in
|
||||
registered_instances]
|
||||
# deregister_failures is a set that will contain any instances that were
|
||||
# unable to be deregistered from the given ELB
|
||||
deregister_failures = set(instances).intersection(set(registered_instance_ids))
|
||||
if deregister_failures:
|
||||
log.warn('Instance(s): {0} not deregistered from ELB {1}.'
|
||||
.format(list(deregister_failures)), name)
|
||||
deregister_result = False
|
||||
else:
|
||||
deregister_result = True
|
||||
return deregister_result
|
||||
|
||||
|
||||
def get_instance_health(name, region=None, key=None, keyid=None, profile=None, instances=None):
|
||||
|
189
tests/unit/modules/boto_elb_test.py
Normal file
189
tests/unit/modules/boto_elb_test.py
Normal file
@ -0,0 +1,189 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# import Python Libs
|
||||
from copy import deepcopy
|
||||
|
||||
# import Python Third Party Libs
|
||||
try:
|
||||
import boto
|
||||
import boto.ec2.elb
|
||||
HAS_BOTO = True
|
||||
except ImportError:
|
||||
HAS_BOTO = False
|
||||
|
||||
try:
|
||||
from moto import mock_ec2, mock_elb
|
||||
HAS_MOTO = True
|
||||
except ImportError:
|
||||
HAS_MOTO = False
|
||||
|
||||
def mock_ec2(self):
|
||||
'''
|
||||
if the mock_ec2 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.
|
||||
'''
|
||||
def stub_function(self):
|
||||
pass
|
||||
return stub_function
|
||||
|
||||
def mock_elb(self):
|
||||
'''
|
||||
if the mock_ec2 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.
|
||||
'''
|
||||
def stub_function(self):
|
||||
pass
|
||||
return stub_function
|
||||
|
||||
# Import Salt Libs
|
||||
from salt.modules import boto_elb
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from salttesting import skipIf, TestCase
|
||||
from salttesting.mock import NO_MOCK, NO_MOCK_REASON
|
||||
from salttesting.helpers import ensure_in_syspath
|
||||
|
||||
ensure_in_syspath('../../')
|
||||
|
||||
region = 'us-east-1'
|
||||
access_key = 'GKTADJGHEIQSXMKKRBJ08H'
|
||||
secret_key = 'askdjghsdfjkghWupUjasdflkdfklgjsdfjajkghs'
|
||||
conn_parameters = {'region': region, 'key': access_key, 'keyid': secret_key,
|
||||
'profile': {}}
|
||||
boto_conn_parameters = {'aws_access_key_id': access_key,
|
||||
'aws_secret_access_key': secret_key}
|
||||
instance_parameters = {'instance_type': 't1.micro'}
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
|
||||
@skipIf(HAS_MOTO is False, 'The moto module must be installed.')
|
||||
class BotoElbTestCase(TestCase):
|
||||
'''
|
||||
TestCase for salt.modules.boto_elb module
|
||||
'''
|
||||
@mock_ec2
|
||||
@mock_elb
|
||||
def test_register_instances_valid_id_result_true(self):
|
||||
'''
|
||||
tests that given a valid instance id and valid ELB that
|
||||
register_instances returns True.
|
||||
'''
|
||||
conn_ec2 = boto.ec2.connect_to_region(region, **boto_conn_parameters)
|
||||
conn_elb = boto.ec2.elb.connect_to_region(region,
|
||||
**boto_conn_parameters)
|
||||
zones = [zone.name for zone in conn_ec2.get_all_zones()]
|
||||
elb_name = 'TestRegisterInstancesValidIdResult'
|
||||
conn_elb.create_load_balancer(elb_name, zones, [(80, 80, 'http')])
|
||||
reservations = conn_ec2.run_instances('ami-08389d60')
|
||||
register_result = boto_elb.register_instances(elb_name,
|
||||
reservations.instances[0].id,
|
||||
**conn_parameters)
|
||||
self.assertEqual(True, register_result)
|
||||
|
||||
@mock_ec2
|
||||
@mock_elb
|
||||
def test_register_instances_valid_id_string(self):
|
||||
'''
|
||||
tests that given a string containing a instance id and valid ELB that
|
||||
register_instances adds the given instance to an ELB
|
||||
'''
|
||||
conn_ec2 = boto.ec2.connect_to_region(region, **boto_conn_parameters)
|
||||
conn_elb = boto.ec2.elb.connect_to_region(region,
|
||||
**boto_conn_parameters)
|
||||
zones = [zone.name for zone in conn_ec2.get_all_zones()]
|
||||
elb_name = 'TestRegisterInstancesValidIdResult'
|
||||
conn_elb.create_load_balancer(elb_name, zones, [(80, 80, 'http')])
|
||||
reservations = conn_ec2.run_instances('ami-08389d60')
|
||||
boto_elb.register_instances(elb_name, reservations.instances[0].id,
|
||||
**conn_parameters)
|
||||
load_balancer_refreshed = conn_elb.get_all_load_balancers(elb_name)[0]
|
||||
registered_instance_ids = [instance.id for instance in
|
||||
load_balancer_refreshed.instances]
|
||||
|
||||
print load_balancer_refreshed.instances
|
||||
self.assertEqual([reservations.instances[0].id], registered_instance_ids)
|
||||
|
||||
@mock_ec2
|
||||
@mock_elb
|
||||
def test_deregister_instances_valid_id_result_true(self):
|
||||
'''
|
||||
tests that given an valid id the boto_elb deregister_instances method
|
||||
removes exactly one of a number of ELB registered instances
|
||||
'''
|
||||
conn_ec2 = boto.ec2.connect_to_region(region, **boto_conn_parameters)
|
||||
conn_elb = boto.ec2.elb.connect_to_region(region,
|
||||
**boto_conn_parameters)
|
||||
zones = [zone.name for zone in conn_ec2.get_all_zones()]
|
||||
elb_name = 'TestDeregisterInstancesValidIdResult'
|
||||
load_balancer = conn_elb.create_load_balancer(elb_name, zones,
|
||||
[(80, 80, 'http')])
|
||||
reservations = conn_ec2.run_instances('ami-08389d60')
|
||||
load_balancer.register_instances(reservations.instances[0].id)
|
||||
deregister_result = boto_elb.deregister_instances(elb_name,
|
||||
reservations.instances[0].id,
|
||||
**conn_parameters)
|
||||
self.assertEqual(True, deregister_result)
|
||||
|
||||
@mock_ec2
|
||||
@mock_elb
|
||||
def test_deregister_instances_valid_id_string(self):
|
||||
'''
|
||||
tests that given an valid id the boto_elb deregister_instances method
|
||||
removes exactly one of a number of ELB registered instances
|
||||
'''
|
||||
conn_ec2 = boto.ec2.connect_to_region(region, **boto_conn_parameters)
|
||||
conn_elb = boto.ec2.elb.connect_to_region(region,
|
||||
**boto_conn_parameters)
|
||||
zones = [zone.name for zone in conn_ec2.get_all_zones()]
|
||||
elb_name = 'TestDeregisterInstancesValidIdString'
|
||||
load_balancer = conn_elb.create_load_balancer(elb_name, zones,
|
||||
[(80, 80, 'http')])
|
||||
reservations = conn_ec2.run_instances('ami-08389d60', min_count=2)
|
||||
all_instance_ids = [instance.id for instance in reservations.instances]
|
||||
load_balancer.register_instances(all_instance_ids)
|
||||
boto_elb.deregister_instances(elb_name, reservations.instances[0].id,
|
||||
**conn_parameters)
|
||||
load_balancer_refreshed = conn_elb.get_all_load_balancers(elb_name)[0]
|
||||
expected_instances = deepcopy(all_instance_ids)
|
||||
expected_instances.remove(reservations.instances[0].id)
|
||||
actual_instances = [instance.id for instance in
|
||||
load_balancer_refreshed.instances]
|
||||
self.assertEqual(actual_instances, expected_instances)
|
||||
|
||||
@mock_ec2
|
||||
@mock_elb
|
||||
def test_deregister_instances_valid_id_list(self):
|
||||
'''
|
||||
tests that given an valid ids in the form of a list that the boto_elb
|
||||
deregister_instances all members of the given list
|
||||
'''
|
||||
conn_ec2 = boto.ec2.connect_to_region(region, **boto_conn_parameters)
|
||||
conn_elb = boto.ec2.elb.connect_to_region(region,
|
||||
**boto_conn_parameters)
|
||||
zones = [zone.name for zone in conn_ec2.get_all_zones()]
|
||||
elb_name = 'TestDeregisterInstancesValidIdList'
|
||||
load_balancer = conn_elb.create_load_balancer(elb_name, zones,
|
||||
[(80, 80, 'http')])
|
||||
reservations = conn_ec2.run_instances('ami-08389d60', min_count=3)
|
||||
all_instance_ids = [instance.id for instance in reservations.instances]
|
||||
load_balancer.register_instances(all_instance_ids)
|
||||
# reservations.instances[:-1] refers to all instances except list
|
||||
# instance
|
||||
deregister_instances = [instance.id for instance in
|
||||
reservations.instances[:-1]]
|
||||
expected_instances = [reservations.instances[-1].id]
|
||||
boto_elb.deregister_instances(elb_name, deregister_instances,
|
||||
**conn_parameters)
|
||||
load_balancer_refreshed = conn_elb.get_all_load_balancers(elb_name)[0]
|
||||
actual_instances = [instance.id for instance in
|
||||
load_balancer_refreshed.instances]
|
||||
self.assertEqual(actual_instances, expected_instances)
|
||||
|
||||
if __name__ == '__main__':
|
||||
from integration import run_tests
|
||||
run_tests(BotoElbTestCase, needs_daemon=False)
|
Loading…
Reference in New Issue
Block a user