<s> from __future__ import absolute_import <EOL> import logging <EOL> from copy import deepcopy <EOL> try : <EOL> import boto <EOL> import boto . ec2 . elb <EOL> HAS_BOTO = True <EOL> except ImportError : <EOL> HAS_BOTO = False <EOL> try : <EOL> from moto import mock_ec2 , mock_elb <EOL> HAS_MOTO = True <EOL> except ImportError : <EOL> HAS_MOTO = False <EOL> def mock_ec2 ( self ) : <EOL> '''<STR_LIT>''' <EOL> def stub_function ( self ) : <EOL> pass <EOL> return stub_function <EOL> def mock_elb ( self ) : <EOL> '''<STR_LIT>''' <EOL> def stub_function ( self ) : <EOL> pass <EOL> return stub_function <EOL> import salt . config <EOL> import salt . loader <EOL> from salt . modules import boto_elb <EOL> from salttesting import skipIf , TestCase <EOL> from salttesting . mock import NO_MOCK , NO_MOCK_REASON <EOL> from salttesting . helpers import ensure_in_syspath <EOL> ensure_in_syspath ( '<STR_LIT>' ) <EOL> log = logging . getLogger ( __name__ ) <EOL> region = '<STR_LIT>' <EOL> access_key = '<STR_LIT>' <EOL> secret_key = '<STR_LIT>' <EOL> conn_parameters = { '<STR_LIT>' : region , '<STR_LIT:key>' : access_key , '<STR_LIT>' : secret_key , <EOL> '<STR_LIT>' : { } } <EOL> boto_conn_parameters = { '<STR_LIT>' : access_key , <EOL> '<STR_LIT>' : secret_key } <EOL> instance_parameters = { '<STR_LIT>' : '<STR_LIT>' } <EOL> opts = salt . config . DEFAULT_MASTER_OPTS <EOL> utils = salt . loader . utils ( opts , whitelist = [ '<STR_LIT>' ] ) <EOL> funcs = salt . loader . minion_mods ( opts , utils = utils ) <EOL> boto_elb . __salt__ = funcs <EOL> boto_elb . __utils__ = utils <EOL> boto_elb . __virtual__ ( ) <EOL> @ skipIf ( NO_MOCK , NO_MOCK_REASON ) <EOL> @ skipIf ( HAS_BOTO is False , '<STR_LIT>' ) <EOL> @ skipIf ( HAS_MOTO is False , '<STR_LIT>' ) <EOL> class BotoElbTestCase ( TestCase ) : <EOL> '''<STR_LIT>''' <EOL> @ mock_ec2 <EOL> @ mock_elb <EOL> def test_register_instances_valid_id_result_true ( self ) : <EOL> '''<STR_LIT>''' <EOL> conn_ec2 = boto . ec2 . connect_to_region ( region , ** boto_conn_parameters ) <EOL> conn_elb = boto . ec2 . elb . connect_to_region ( region , <EOL> ** boto_conn_parameters ) <EOL> zones = [ zone . name for zone in conn_ec2 . get_all_zones ( ) ] <EOL> elb_name = '<STR_LIT>' <EOL> conn_elb . create_load_balancer ( elb_name , zones , [ ( <NUM_LIT> , <NUM_LIT> , '<STR_LIT:http>' ) ] ) <EOL> reservations = conn_ec2 . run_instances ( '<STR_LIT>' ) <EOL> register_result = boto_elb . register_instances ( elb_name , <EOL> reservations . instances [ <NUM_LIT:0> ] . id , <EOL> ** conn_parameters ) <EOL> self . assertEqual ( True , register_result ) <EOL> @ mock_ec2 <EOL> @ mock_elb <EOL> def test_register_instances_valid_id_string ( self ) : <EOL> '''<STR_LIT>''' <EOL> conn_ec2 = boto . ec2 . connect_to_region ( region , ** boto_conn_parameters ) <EOL> conn_elb = boto . ec2 . elb . connect_to_region ( region , <EOL> ** boto_conn_parameters ) <EOL> zones = [ zone . name for zone in conn_ec2 . get_all_zones ( ) ] <EOL> elb_name = '<STR_LIT>' <EOL> conn_elb . create_load_balancer ( elb_name , zones , [ ( <NUM_LIT> , <NUM_LIT> , '<STR_LIT:http>' ) ] ) <EOL> reservations = conn_ec2 . run_instances ( '<STR_LIT>' ) <EOL> boto_elb . register_instances ( elb_name , reservations . instances [ <NUM_LIT:0> ] . id , <EOL> ** conn_parameters ) <EOL> load_balancer_refreshed = conn_elb . get_all_load_balancers ( elb_name ) [ <NUM_LIT:0> ] <EOL> registered_instance_ids = [ instance . id for instance in <EOL> load_balancer_refreshed . instances ] <EOL> log . debug ( load_balancer_refreshed . instances ) <EOL> self . assertEqual ( [ reservations . instances [ <NUM_LIT:0> ] . id ] , registered_instance_ids ) <EOL> @ mock_ec2 <EOL> @ mock_elb <EOL> def test_deregister_instances_valid_id_result_true ( self ) : <EOL> '''<STR_LIT>''' <EOL> conn_ec2 = boto . ec2 . connect_to_region ( region , ** boto_conn_parameters ) <EOL> conn_elb = boto . ec2 . elb . connect_to_region ( region , <EOL> ** boto_conn_parameters ) <EOL> zones = [ zone . name for zone in conn_ec2 . get_all_zones ( ) ] <EOL> elb_name = '<STR_LIT>' <EOL> load_balancer = conn_elb . create_load_balancer ( elb_name , zones , <EOL> [ ( <NUM_LIT> , <NUM_LIT> , '<STR_LIT:http>' ) ] ) <EOL> reservations = conn_ec2 . run_instances ( '<STR_LIT>' ) <EOL> load_balancer . register_instances ( reservations . instances [ <NUM_LIT:0> ] . id ) <EOL> deregister_result = boto_elb . deregister_instances ( elb_name , <EOL> reservations . instances [ <NUM_LIT:0> ] . id , <EOL> ** conn_parameters ) <EOL> self . assertEqual ( True , deregister_result ) <EOL> @ mock_ec2 <EOL> @ mock_elb <EOL> def test_deregister_instances_valid_id_string ( self ) : <EOL> '''<STR_LIT>''' <EOL> conn_ec2 = boto . ec2 . connect_to_region ( region , ** boto_conn_parameters ) <EOL> conn_elb = boto . ec2 . elb . connect_to_region ( region , <EOL> ** boto_conn_parameters ) <EOL> zones = [ zone . name for zone in conn_ec2 . get_all_zones ( ) ] <EOL> elb_name = '<STR_LIT>' <EOL> load_balancer = conn_elb . create_load_balancer ( elb_name , zones , <EOL> [ ( <NUM_LIT> , <NUM_LIT> , '<STR_LIT:http>' ) ] ) <EOL> reservations = conn_ec2 . run_instances ( '<STR_LIT>' , min_count = <NUM_LIT:2> ) <EOL> all_instance_ids = [ instance . id for instance in reservations . instances ] <EOL> load_balancer . register_instances ( all_instance_ids ) <EOL> boto_elb . deregister_instances ( elb_name , reservations . instances [ <NUM_LIT:0> ] . id , <EOL> ** conn_parameters ) <EOL> load_balancer_refreshed = conn_elb . get_all_load_balancers ( elb_name ) [ <NUM_LIT:0> ] <EOL> expected_instances = deepcopy ( all_instance_ids ) <EOL> expected_instances . remove ( reservations . instances [ <NUM_LIT:0> ] . id ) <EOL> actual_instances = [ instance . id for instance in <EOL> load_balancer_refreshed . instances ] <EOL> self . assertEqual ( actual_instances , expected_instances ) <EOL> @ mock_ec2 <EOL> @ mock_elb <EOL> def test_deregister_instances_valid_id_list ( self ) : <EOL> '''<STR_LIT>''' <EOL> conn_ec2 = boto . ec2 . connect_to_region ( region , ** boto_conn_parameters ) <EOL> conn_elb = boto . ec2 . elb . connect_to_region ( region , <EOL> ** boto_conn_parameters ) <EOL> zones = [ zone . name for zone in conn_ec2 . get_all_zones ( ) ] <EOL> elb_name = '<STR_LIT>' <EOL> load_balancer = conn_elb . create_load_balancer ( elb_name , zones , <EOL> [ ( <NUM_LIT> , <NUM_LIT> , '<STR_LIT:http>' ) ] ) <EOL> reservations = conn_ec2 . run_instances ( '<STR_LIT>' , min_count = <NUM_LIT:3> ) <EOL> all_instance_ids = [ instance . id for instance in reservations . instances ] <EOL> load_balancer . register_instances ( all_instance_ids ) <EOL> deregister_instances = [ instance . id for instance in <EOL> reservations . instances [ : - <NUM_LIT:1> ] ] <EOL> expected_instances = [ reservations . instances [ - <NUM_LIT:1> ] . id ] <EOL> boto_elb . deregister_instances ( elb_name , deregister_instances , <EOL> ** conn_parameters ) <EOL> load_balancer_refreshed = conn_elb . get_all_load_balancers ( elb_name ) [ <NUM_LIT:0> ] <EOL> actual_instances = [ instance . id for instance in <EOL> load_balancer_refreshed . instances ] <EOL> self . assertEqual ( actual_instances , expected_instances ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from integration import run_tests <EOL> run_tests ( BotoElbTestCase , needs_daemon = False ) </s>
