<s> import mock <EOL> from rally . plugins . openstack . context . neutron import lbaas as lbaas_context <EOL> from tests . unit import test <EOL> NET = "<STR_LIT>" <EOL> class LbaasTestCase ( test . TestCase ) : <EOL> def get_context ( self , ** kwargs ) : <EOL> foo_tenant = { "<STR_LIT>" : [ { "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] } ] } <EOL> bar_tenant = { "<STR_LIT>" : [ { "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] } ] } <EOL> return { "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } , <EOL> "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT>" } , <EOL> "<STR_LIT>" : [ { "<STR_LIT:id>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } , <EOL> { "<STR_LIT:id>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } ] , <EOL> "<STR_LIT>" : { "<STR_LIT>" : kwargs } , <EOL> "<STR_LIT>" : { "<STR_LIT>" : foo_tenant , <EOL> "<STR_LIT>" : bar_tenant } } <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> @ mock . patch ( NET + "<STR_LIT>" , return_value = "<STR_LIT>" ) <EOL> def test__init__default ( self , mock_wrap , mock_clients ) : <EOL> context = lbaas_context . Lbaas ( self . get_context ( ) ) <EOL> self . assertEqual ( <EOL> context . config [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> lbaas_context . Lbaas . DEFAULT_CONFIG [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> self . assertEqual ( <EOL> context . config [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> lbaas_context . Lbaas . DEFAULT_CONFIG [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> self . assertEqual ( <EOL> context . config [ "<STR_LIT>" ] , <EOL> lbaas_context . Lbaas . DEFAULT_CONFIG [ "<STR_LIT>" ] ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> @ mock . patch ( NET + "<STR_LIT>" , return_value = "<STR_LIT>" ) <EOL> def test__init__explicit ( self , mock_wrap , mock_clients ) : <EOL> context = lbaas_context . Lbaas ( <EOL> self . get_context ( pool = { "<STR_LIT>" : "<STR_LIT>" } ) ) <EOL> self . assertEqual ( context . config [ "<STR_LIT>" ] [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" ) <EOL> @ mock . patch ( NET + "<STR_LIT>" ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test_setup_with_lbaas ( self , mock_clients , mock_utils , mock_wrap ) : <EOL> mock_utils . iterate_per_tenants . return_value = [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) ] <EOL> foo_net = { "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ { "<STR_LIT>" : { "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } } ] } <EOL> bar_net = { "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ { "<STR_LIT>" : { "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } } ] } <EOL> expected_net = [ bar_net , foo_net ] <EOL> mock_create = mock . Mock ( <EOL> side_effect = lambda t , s , <EOL> ** kw : { "<STR_LIT>" : { "<STR_LIT:id>" : str ( t . split ( "<STR_LIT:_>" ) [ <NUM_LIT:0> ] ) + "<STR_LIT>" , <EOL> "<STR_LIT>" : t } } ) <EOL> actual_net = [ ] <EOL> mock_wrap . return_value = mock . Mock ( create_v1_pool = mock_create ) <EOL> net_wrapper = mock_wrap ( mock_clients . return_value ) <EOL> net_wrapper . supports_extension . return_value = ( True , None ) <EOL> fake_args = { "<STR_LIT>" : <NUM_LIT:1> } <EOL> lb_context = lbaas_context . Lbaas ( self . get_context ( ** fake_args ) ) <EOL> lb_context . setup ( ) <EOL> mock_utils . iterate_per_tenants . assert_called_once_with ( <EOL> lb_context . context [ "<STR_LIT>" ] ) <EOL> net_wrapper . supports_extension . assert_called_once_with ( "<STR_LIT>" ) <EOL> for tenant_id , tenant_ctx in ( <EOL> sorted ( lb_context . context [ "<STR_LIT>" ] . items ( ) ) ) : <EOL> for network in tenant_ctx [ "<STR_LIT>" ] : <EOL> actual_net . append ( network ) <EOL> self . assertEqual ( expected_net , actual_net ) <EOL> @ mock . patch ( NET + "<STR_LIT>" ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test_setup_with_no_lbaas ( self , mock_clients , mock_utils , mock_wrap ) : <EOL> mock_utils . iterate_per_tenants . return_value = [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) ] <EOL> mock_create = mock . Mock ( side_effect = lambda t , ** kw : t + "<STR_LIT>" ) <EOL> mock_wrap . return_value = mock . Mock ( create_v1_pool = mock_create ) <EOL> fake_args = { "<STR_LIT>" : <NUM_LIT:1> } <EOL> lb_context = lbaas_context . Lbaas ( self . get_context ( ** fake_args ) ) <EOL> net_wrapper = mock_wrap ( mock_clients . return_value ) <EOL> net_wrapper . supports_extension . return_value = ( False , None ) <EOL> lb_context . setup ( ) <EOL> mock_utils . iterate_per_tenants . assert_not_called ( ) <EOL> net_wrapper . supports_extension . assert_called_once_with ( "<STR_LIT>" ) <EOL> assert not net_wrapper . create_v1_pool . called <EOL> @ mock . patch ( NET + "<STR_LIT>" ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test_setup_with_lbaas_version_not_one ( self , mock_clients , <EOL> mock_utils , mock_wrap ) : <EOL> mock_utils . iterate_per_tenants . return_value = [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) ] <EOL> mock_create = mock . Mock ( side_effect = lambda t , ** kw : t + "<STR_LIT>" ) <EOL> mock_wrap . return_value = mock . Mock ( create_v1_pool = mock_create ) <EOL> fake_args = { "<STR_LIT>" : <NUM_LIT:2> } <EOL> lb_context = lbaas_context . Lbaas ( self . get_context ( ** fake_args ) ) <EOL> net_wrapper = mock_wrap ( mock_clients . return_value ) <EOL> net_wrapper . supports_extension . return_value = ( True , None ) <EOL> self . assertRaises ( NotImplementedError , lb_context . setup ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> @ mock . patch ( NET + "<STR_LIT>" ) <EOL> def test_cleanup ( self , mock_wrap , mock_clients ) : <EOL> net_wrapper = mock_wrap ( mock_clients . return_value ) <EOL> lb_context = lbaas_context . Lbaas ( self . get_context ( ) ) <EOL> expected_pools = [ ] <EOL> for tenant_id , tenant_ctx in lb_context . context [ "<STR_LIT>" ] . items ( ) : <EOL> resultant_pool = { "<STR_LIT>" : { <EOL> "<STR_LIT:id>" : str ( tenant_id . split ( "<STR_LIT:_>" ) [ <NUM_LIT:0> ] ) + "<STR_LIT>" } } <EOL> expected_pools . append ( resultant_pool ) <EOL> for network in ( <EOL> lb_context . context [ "<STR_LIT>" ] [ tenant_id ] [ "<STR_LIT>" ] ) : <EOL> network . setdefault ( "<STR_LIT>" , [ ] ) . append ( resultant_pool ) <EOL> lb_context . cleanup ( ) <EOL> net_wrapper . delete_v1_pool . assert_has_calls ( <EOL> [ mock . call ( pool [ "<STR_LIT>" ] [ "<STR_LIT:id>" ] ) for pool in expected_pools ] ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> @ mock . patch ( NET + "<STR_LIT>" ) <EOL> def test_cleanup_lbaas_version_not_one ( self , mock_wrap , mock_clients ) : <EOL> fakeargs = { "<STR_LIT>" : <NUM_LIT:2> } <EOL> net_wrapper = mock_wrap ( mock_clients . return_value ) <EOL> lb_context = lbaas_context . Lbaas ( self . get_context ( ** fakeargs ) ) <EOL> for tenant_id , tenant_ctx in lb_context . context [ "<STR_LIT>" ] . items ( ) : <EOL> resultant_pool = { "<STR_LIT>" : { <EOL> "<STR_LIT:id>" : str ( tenant_id . split ( "<STR_LIT:_>" ) [ <NUM_LIT:0> ] ) + "<STR_LIT>" } } <EOL> for network in ( <EOL> lb_context . context [ "<STR_LIT>" ] [ tenant_id ] [ "<STR_LIT>" ] ) : <EOL> network . setdefault ( "<STR_LIT>" , [ ] ) . append ( resultant_pool ) <EOL> lb_context . cleanup ( ) <EOL> assert not net_wrapper . delete_v1_pool . called </s>
