<s> import mock <EOL> import time <EOL> from oslo_config import cfg <EOL> from nova import test <EOL> from nova . tests import fixtures as nova_fixtures <EOL> from nova . tests . functional . api import client <EOL> from nova . tests . functional import api_paste_fixture <EOL> from nova . tests . unit import fake_network <EOL> from nova . tests . unit import policy_fixture <EOL> import nova . scheduler . utils <EOL> import nova . servicegroup <EOL> import nova . tests . unit . image . fake <EOL> CONF = cfg . CONF <EOL> PROJECT_ID_ALT = "<STR_LIT>" <EOL> class ServerGroupTestBase ( test . TestCase ) : <EOL> REQUIRES_LOCKING = True <EOL> api_major_version = '<STR_LIT>' <EOL> microversion = None <EOL> _image_ref_parameter = '<STR_LIT>' <EOL> _flavor_ref_parameter = '<STR_LIT>' <EOL> _scheduler_default_filters = ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> _service_down_time = <NUM_LIT:2> <EOL> _report_interval = <NUM_LIT:1> <EOL> anti_affinity = { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> affinity = { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> def _get_weight_classes ( self ) : <EOL> return [ ] <EOL> def setUp ( self ) : <EOL> super ( ServerGroupTestBase , self ) . setUp ( ) <EOL> self . flags ( scheduler_default_filters = self . _scheduler_default_filters ) <EOL> self . flags ( scheduler_weight_classes = self . _get_weight_classes ( ) ) <EOL> self . flags ( service_down_time = self . _service_down_time ) <EOL> self . flags ( report_interval = self . _report_interval ) <EOL> self . useFixture ( policy_fixture . RealPolicyFixture ( ) ) <EOL> if self . api_major_version == '<STR_LIT>' : <EOL> api_fixture = self . useFixture ( nova_fixtures . OSAPIFixture ( <EOL> api_version = '<STR_LIT>' ) ) <EOL> else : <EOL> self . useFixture ( api_paste_fixture . ApiPasteLegacyV2Fixture ( ) ) <EOL> api_fixture = self . useFixture ( nova_fixtures . OSAPIFixture ( <EOL> api_version = '<STR_LIT>' ) ) <EOL> self . api = api_fixture . api <EOL> self . api . microversion = self . microversion <EOL> self . admin_api = api_fixture . admin_api <EOL> self . admin_api . microversion = self . microversion <EOL> nova . tests . unit . image . fake . stub_out_image_service ( self ) <EOL> self . start_service ( '<STR_LIT>' , manager = CONF . conductor . manager ) <EOL> self . start_service ( '<STR_LIT>' ) <EOL> self . addCleanup ( nova . tests . unit . image . fake . FakeImageService_reset ) <EOL> def _wait_for_state_change ( self , server , expected_status , max_retries = <NUM_LIT:10> ) : <EOL> retry_count = <NUM_LIT:0> <EOL> while True : <EOL> server = self . admin_api . get_server ( server [ '<STR_LIT:id>' ] ) <EOL> if server [ '<STR_LIT:status>' ] == expected_status : <EOL> break <EOL> retry_count += <NUM_LIT:1> <EOL> if retry_count == max_retries : <EOL> self . fail ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( expected_status , server [ '<STR_LIT:status>' ] ) ) <EOL> time . sleep ( <NUM_LIT:0.5> ) <EOL> return server <EOL> def _boot_a_server_to_group ( self , group , <EOL> expected_status = '<STR_LIT>' , flavor = None ) : <EOL> server = self . _build_minimal_create_server_request ( '<STR_LIT>' ) <EOL> if flavor : <EOL> server [ self . _flavor_ref_parameter ] = ( '<STR_LIT>' <EOL> % flavor [ '<STR_LIT:id>' ] ) <EOL> post = { '<STR_LIT>' : server , <EOL> '<STR_LIT>' : { '<STR_LIT>' : group [ '<STR_LIT:id>' ] } } <EOL> created_server = self . api . post_server ( post ) <EOL> self . assertTrue ( created_server [ '<STR_LIT:id>' ] ) <EOL> found_server = self . _wait_for_state_change ( created_server , <EOL> expected_status ) <EOL> return found_server <EOL> def _build_minimal_create_server_request ( self , name ) : <EOL> server = { } <EOL> image = self . api . get_images ( ) [ <NUM_LIT:0> ] <EOL> if self . _image_ref_parameter in image : <EOL> image_href = image [ self . _image_ref_parameter ] <EOL> else : <EOL> image_href = image [ '<STR_LIT:id>' ] <EOL> image_href = '<STR_LIT>' % image_href <EOL> server [ self . _image_ref_parameter ] = image_href <EOL> flavor = self . api . get_flavors ( ) [ <NUM_LIT:1> ] <EOL> server [ self . _flavor_ref_parameter ] = ( '<STR_LIT>' <EOL> % flavor [ '<STR_LIT:id>' ] ) <EOL> server [ '<STR_LIT:name>' ] = name <EOL> return server <EOL> class ServerGroupTestV2 ( ServerGroupTestBase ) : <EOL> api_major_version = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> super ( ServerGroupTestV2 , self ) . setUp ( ) <EOL> self . start_service ( '<STR_LIT>' ) <EOL> self . compute = self . start_service ( '<STR_LIT>' ) <EOL> self . compute2 = self . start_service ( '<STR_LIT>' , host = '<STR_LIT>' ) <EOL> fake_network . set_stub_network_methods ( self ) <EOL> def test_get_no_groups ( self ) : <EOL> groups = self . api . get_server_groups ( ) <EOL> self . assertEqual ( [ ] , groups ) <EOL> def test_create_and_delete_groups ( self ) : <EOL> groups = [ self . anti_affinity , <EOL> self . affinity ] <EOL> created_groups = [ ] <EOL> for group in groups : <EOL> created_group = self . api . post_server_groups ( group ) <EOL> created_groups . append ( created_group ) <EOL> self . assertEqual ( group [ '<STR_LIT:name>' ] , created_group [ '<STR_LIT:name>' ] ) <EOL> self . assertEqual ( group [ '<STR_LIT>' ] , created_group [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( [ ] , created_group [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( { } , created_group [ '<STR_LIT>' ] ) <EOL> self . assertIn ( '<STR_LIT:id>' , created_group ) <EOL> group_details = self . api . get_server_group ( created_group [ '<STR_LIT:id>' ] ) <EOL> self . assertEqual ( created_group , group_details ) <EOL> existing_groups = self . api . get_server_groups ( ) <EOL> self . assertIn ( created_group , existing_groups ) <EOL> existing_groups = self . api . get_server_groups ( ) <EOL> self . assertEqual ( len ( groups ) , len ( existing_groups ) ) <EOL> for group in created_groups : <EOL> self . api . delete_server_group ( group [ '<STR_LIT:id>' ] ) <EOL> existing_groups = self . api . get_server_groups ( ) <EOL> self . assertNotIn ( group , existing_groups ) <EOL> def test_create_wrong_policy ( self ) : <EOL> ex = self . assertRaises ( client . OpenStackApiException , <EOL> self . api . post_server_groups , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] } ) <EOL> self . assertEqual ( <NUM_LIT> , ex . response . status_code ) <EOL> self . assertIn ( '<STR_LIT>' , ex . response . text ) <EOL> self . assertIn ( '<STR_LIT>' , ex . response . text ) <EOL> def test_get_groups_all_projects ( self ) : <EOL> api_openstack1 = self . useFixture ( nova_fixtures . OSAPIFixture ( <EOL> api_version = self . api_major_version , <EOL> project_id = PROJECT_ID_ALT ) ) . api <EOL> api_openstack1 . microversion = self . microversion <EOL> group1 = self . anti_affinity <EOL> openstack_group = self . api . post_server_groups ( group1 ) <EOL> group2 = self . affinity <EOL> openstack1_group = api_openstack1 . post_server_groups ( group2 ) <EOL> all_projects_admin = self . admin_api . get_server_groups ( <EOL> all_projects = True ) <EOL> self . assertIn ( openstack_group , all_projects_admin ) <EOL> self . assertIn ( openstack1_group , all_projects_admin ) <EOL> all_projects_non_admin = api_openstack1 . get_server_groups ( <EOL> all_projects = True ) <EOL> self . assertNotIn ( openstack_group , all_projects_non_admin ) <EOL> self . assertIn ( openstack1_group , all_projects_non_admin ) <EOL> def test_create_duplicated_policy ( self ) : <EOL> ex = self . assertRaises ( client . OpenStackApiException , <EOL> self . api . post_server_groups , <EOL> { "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] } ) <EOL> self . assertEqual ( <NUM_LIT> , ex . response . status_code ) <EOL> self . assertIn ( '<STR_LIT>' , ex . response . text ) <EOL> def test_create_multiple_policies ( self ) : <EOL> ex = self . assertRaises ( client . OpenStackApiException , <EOL> self . api . post_server_groups , <EOL> { "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] } ) <EOL> self . assertEqual ( <NUM_LIT> , ex . response . status_code ) <EOL> def _boot_servers_to_group ( self , group , flavor = None ) : <EOL> servers = [ ] <EOL> for _ in range ( <NUM_LIT:0> , <NUM_LIT:2> ) : <EOL> server = self . _boot_a_server_to_group ( group , <EOL> flavor = flavor ) <EOL> servers . append ( server ) <EOL> return servers <EOL> def test_boot_servers_with_affinity ( self ) : <EOL> created_group = self . api . post_server_groups ( self . affinity ) <EOL> servers = self . _boot_servers_to_group ( created_group ) <EOL> members = self . api . get_server_group ( created_group [ '<STR_LIT:id>' ] ) [ '<STR_LIT>' ] <EOL> host = servers [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> for server in servers : <EOL> self . assertIn ( server [ '<STR_LIT:id>' ] , members ) <EOL> self . assertEqual ( host , server [ '<STR_LIT>' ] ) <EOL> def test_boot_servers_with_affinity_no_valid_host ( self ) : <EOL> created_group = self . api . post_server_groups ( self . affinity ) <EOL> flavor = self . api . get_flavors ( ) [ <NUM_LIT:2> ] <EOL> self . _boot_servers_to_group ( created_group , flavor = flavor ) <EOL> failed_server = self . _boot_a_server_to_group ( created_group , <EOL> flavor = flavor , <EOL> expected_status = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> failed_server [ '<STR_LIT>' ] [ '<STR_LIT:message>' ] ) <EOL> def test_boot_servers_with_anti_affinity ( self ) : <EOL> created_group = self . api . post_server_groups ( self . anti_affinity ) <EOL> servers = self . _boot_servers_to_group ( created_group ) <EOL> members = self . api . get_server_group ( created_group [ '<STR_LIT:id>' ] ) [ '<STR_LIT>' ] <EOL> self . assertNotEqual ( servers [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <EOL> servers [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> for server in servers : <EOL> self . assertIn ( server [ '<STR_LIT:id>' ] , members ) <EOL> def test_boot_server_with_anti_affinity_no_valid_host ( self ) : <EOL> created_group = self . api . post_server_groups ( self . anti_affinity ) <EOL> self . _boot_servers_to_group ( created_group ) <EOL> failed_server = self . _boot_a_server_to_group ( created_group , <EOL> expected_status = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> failed_server [ '<STR_LIT>' ] [ '<STR_LIT:message>' ] ) <EOL> def _rebuild_with_group ( self , group ) : <EOL> created_group = self . api . post_server_groups ( group ) <EOL> servers = self . _boot_servers_to_group ( created_group ) <EOL> post = { '<STR_LIT>' : { self . _image_ref_parameter : <EOL> '<STR_LIT>' } } <EOL> self . api . post_server_action ( servers [ <NUM_LIT:1> ] [ '<STR_LIT:id>' ] , post ) <EOL> rebuilt_server = self . _wait_for_state_change ( servers [ <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( post [ '<STR_LIT>' ] [ self . _image_ref_parameter ] , <EOL> rebuilt_server . get ( '<STR_LIT:image>' ) [ '<STR_LIT:id>' ] ) <EOL> return [ servers [ <NUM_LIT:0> ] , rebuilt_server ] <EOL> def test_rebuild_with_affinity ( self ) : <EOL> untouched_server , rebuilt_server = self . _rebuild_with_group ( <EOL> self . affinity ) <EOL> self . assertEqual ( untouched_server [ '<STR_LIT>' ] , <EOL> rebuilt_server [ '<STR_LIT>' ] ) <EOL> def test_rebuild_with_anti_affinity ( self ) : <EOL> untouched_server , rebuilt_server = self . _rebuild_with_group ( <EOL> self . anti_affinity ) <EOL> self . assertNotEqual ( untouched_server [ '<STR_LIT>' ] , <EOL> rebuilt_server [ '<STR_LIT>' ] ) <EOL> def _migrate_with_group_no_valid_host ( self , group ) : <EOL> created_group = self . api . post_server_groups ( group ) <EOL> servers = self . _boot_servers_to_group ( created_group ) <EOL> post = { '<STR_LIT>' : { } } <EOL> ex = self . assertRaises ( client . OpenStackApiException , <EOL> self . admin_api . post_server_action , <EOL> servers [ <NUM_LIT:1> ] [ '<STR_LIT:id>' ] , post ) <EOL> self . assertEqual ( <NUM_LIT> , ex . response . status_code ) <EOL> self . assertIn ( '<STR_LIT>' , ex . response . text ) <EOL> def test_migrate_with_group_no_valid_host ( self ) : <EOL> for group in [ self . affinity , self . anti_affinity ] : <EOL> self . _migrate_with_group_no_valid_host ( group ) <EOL> def test_migrate_with_anti_affinity ( self ) : <EOL> self . start_service ( '<STR_LIT>' , host = '<STR_LIT>' ) <EOL> created_group = self . api . post_server_groups ( self . anti_affinity ) <EOL> servers = self . _boot_servers_to_group ( created_group ) <EOL> post = { '<STR_LIT>' : { } } <EOL> self . admin_api . post_server_action ( servers [ <NUM_LIT:1> ] [ '<STR_LIT:id>' ] , post ) <EOL> migrated_server = self . _wait_for_state_change ( servers [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' ) <EOL> self . assertNotEqual ( servers [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <EOL> migrated_server [ '<STR_LIT>' ] ) <EOL> def _get_compute_service_by_host_name ( self , host_name ) : <EOL> host = None <EOL> if self . compute . host == host_name : <EOL> host = self . compute <EOL> elif self . compute2 . host == host_name : <EOL> host = self . compute2 <EOL> else : <EOL> raise AssertionError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( host_name , str ( [ self . compute . host , <EOL> self . compute2 . host ] ) ) ) <EOL> return host <EOL> def test_evacuate_with_anti_affinity ( self ) : <EOL> created_group = self . api . post_server_groups ( self . anti_affinity ) <EOL> servers = self . _boot_servers_to_group ( created_group ) <EOL> host = self . _get_compute_service_by_host_name ( <EOL> servers [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> host . stop ( ) <EOL> time . sleep ( self . _service_down_time ) <EOL> self . start_service ( '<STR_LIT>' , host = '<STR_LIT>' ) <EOL> post = { '<STR_LIT>' : { '<STR_LIT>' : False } } <EOL> self . admin_api . post_server_action ( servers [ <NUM_LIT:1> ] [ '<STR_LIT:id>' ] , post ) <EOL> evacuated_server = self . _wait_for_state_change ( servers [ <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> self . assertNotEqual ( evacuated_server [ '<STR_LIT>' ] , <EOL> servers [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> host . start ( ) <EOL> def test_evacuate_with_anti_affinity_no_valid_host ( self ) : <EOL> created_group = self . api . post_server_groups ( self . anti_affinity ) <EOL> servers = self . _boot_servers_to_group ( created_group ) <EOL> host = self . _get_compute_service_by_host_name ( <EOL> servers [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> host . stop ( ) <EOL> time . sleep ( self . _service_down_time ) <EOL> post = { '<STR_LIT>' : { '<STR_LIT>' : False } } <EOL> self . admin_api . post_server_action ( servers [ <NUM_LIT:1> ] [ '<STR_LIT:id>' ] , post ) <EOL> server_after_failed_evac = self . _wait_for_state_change ( servers [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( server_after_failed_evac [ '<STR_LIT>' ] , <EOL> servers [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> host . start ( ) <EOL> def test_evacuate_with_affinity_no_valid_host ( self ) : <EOL> created_group = self . api . post_server_groups ( self . affinity ) <EOL> servers = self . _boot_servers_to_group ( created_group ) <EOL> host = self . _get_compute_service_by_host_name ( <EOL> servers [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> host . stop ( ) <EOL> time . sleep ( self . _service_down_time ) <EOL> post = { '<STR_LIT>' : { '<STR_LIT>' : False } } <EOL> self . admin_api . post_server_action ( servers [ <NUM_LIT:1> ] [ '<STR_LIT:id>' ] , post ) <EOL> server_after_failed_evac = self . _wait_for_state_change ( servers [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( server_after_failed_evac [ '<STR_LIT>' ] , <EOL> servers [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> host . start ( ) <EOL> class ServerGroupAffinityConfTest ( ServerGroupTestBase ) : <EOL> api_major_version = '<STR_LIT>' <EOL> _scheduler_default_filters = '<STR_LIT>' <EOL> @ mock . patch ( '<STR_LIT>' , None ) <EOL> def test_affinity_no_filter ( self ) : <EOL> created_group = self . api . post_server_groups ( self . affinity ) <EOL> failed_server = self . _boot_a_server_to_group ( created_group , <EOL> expected_status = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> failed_server [ '<STR_LIT>' ] [ '<STR_LIT:message>' ] ) <EOL> self . assertEqual ( <NUM_LIT> , failed_server [ '<STR_LIT>' ] [ '<STR_LIT:code>' ] ) <EOL> class ServerGroupAntiAffinityConfTest ( ServerGroupTestBase ) : <EOL> api_major_version = '<STR_LIT>' <EOL> _scheduler_default_filters = '<STR_LIT>' <EOL> @ mock . patch ( '<STR_LIT>' , None ) <EOL> def test_anti_affinity_no_filter ( self ) : <EOL> created_group = self . api . post_server_groups ( self . anti_affinity ) <EOL> failed_server = self . _boot_a_server_to_group ( created_group , <EOL> expected_status = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> failed_server [ '<STR_LIT>' ] [ '<STR_LIT:message>' ] ) <EOL> self . assertEqual ( <NUM_LIT> , failed_server [ '<STR_LIT>' ] [ '<STR_LIT:code>' ] ) <EOL> class ServerGroupSoftAffinityConfTest ( ServerGroupTestBase ) : <EOL> api_major_version = '<STR_LIT>' <EOL> microversion = '<STR_LIT>' <EOL> soft_affinity = { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> def _get_weight_classes ( self ) : <EOL> return [ '<STR_LIT>' <EOL> '<STR_LIT>' ] <EOL> @ mock . patch ( '<STR_LIT>' , None ) <EOL> def test_soft_affinity_no_filter ( self ) : <EOL> created_group = self . api . post_server_groups ( self . soft_affinity ) <EOL> failed_server = self . _boot_a_server_to_group ( created_group , <EOL> expected_status = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> failed_server [ '<STR_LIT>' ] [ '<STR_LIT:message>' ] ) <EOL> self . assertEqual ( <NUM_LIT> , failed_server [ '<STR_LIT>' ] [ '<STR_LIT:code>' ] ) <EOL> class ServerGroupSoftAntiAffinityConfTest ( ServerGroupTestBase ) : <EOL> api_major_version = '<STR_LIT>' <EOL> microversion = '<STR_LIT>' <EOL> soft_anti_affinity = { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> _scheduler_weight_classes = [ '<STR_LIT>' <EOL> '<STR_LIT>' ] <EOL> def _get_weight_classes ( self ) : <EOL> return [ '<STR_LIT>' <EOL> '<STR_LIT>' ] <EOL> @ mock . patch ( '<STR_LIT>' , None ) <EOL> def test_soft_anti_affinity_no_filter ( self ) : <EOL> created_group = self . api . post_server_groups ( self . soft_anti_affinity ) <EOL> failed_server = self . _boot_a_server_to_group ( created_group , <EOL> expected_status = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> failed_server [ '<STR_LIT>' ] [ '<STR_LIT:message>' ] ) <EOL> self . assertEqual ( <NUM_LIT> , failed_server [ '<STR_LIT>' ] [ '<STR_LIT:code>' ] ) <EOL> class ServerGroupTestV21 ( ServerGroupTestV2 ) : <EOL> api_major_version = '<STR_LIT>' <EOL> def test_soft_affinity_not_supported ( self ) : <EOL> ex = self . assertRaises ( client . OpenStackApiException , <EOL> self . api . post_server_groups , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] } ) <EOL> self . assertEqual ( <NUM_LIT> , ex . response . status_code ) <EOL> self . assertIn ( '<STR_LIT>' , ex . response . text ) <EOL> self . assertIn ( '<STR_LIT>' , ex . response . text ) <EOL> class ServerGroupTestV215 ( ServerGroupTestV2 ) : <EOL> api_major_version = '<STR_LIT>' <EOL> microversion = '<STR_LIT>' <EOL> soft_anti_affinity = { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> soft_affinity = { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> def setUp ( self ) : <EOL> super ( ServerGroupTestV215 , self ) . setUp ( ) <EOL> soft_affinity_patcher = mock . patch ( <EOL> '<STR_LIT>' ) <EOL> soft_anti_affinity_patcher = mock . patch ( <EOL> '<STR_LIT>' ) <EOL> self . addCleanup ( soft_affinity_patcher . stop ) <EOL> self . addCleanup ( soft_anti_affinity_patcher . stop ) <EOL> self . mock_soft_affinity = soft_affinity_patcher . start ( ) <EOL> self . mock_soft_anti_affinity = soft_anti_affinity_patcher . start ( ) <EOL> self . mock_soft_affinity . return_value = None <EOL> self . mock_soft_anti_affinity . return_value = None <EOL> def _get_weight_classes ( self ) : <EOL> return [ '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ] <EOL> def test_evacuate_with_anti_affinity ( self ) : <EOL> created_group = self . api . post_server_groups ( self . anti_affinity ) <EOL> servers = self . _boot_servers_to_group ( created_group ) <EOL> host = self . _get_compute_service_by_host_name ( <EOL> servers [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> host . stop ( ) <EOL> time . sleep ( self . _service_down_time ) <EOL> compute3 = self . start_service ( '<STR_LIT>' , host = '<STR_LIT>' ) <EOL> post = { '<STR_LIT>' : { } } <EOL> self . admin_api . post_server_action ( servers [ <NUM_LIT:1> ] [ '<STR_LIT:id>' ] , post ) <EOL> evacuated_server = self . _wait_for_state_change ( servers [ <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> self . assertNotEqual ( evacuated_server [ '<STR_LIT>' ] , <EOL> servers [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> compute3 . kill ( ) <EOL> host . start ( ) <EOL> def test_evacuate_with_anti_affinity_no_valid_host ( self ) : <EOL> created_group = self . api . post_server_groups ( self . anti_affinity ) <EOL> servers = self . _boot_servers_to_group ( created_group ) <EOL> host = self . _get_compute_service_by_host_name ( <EOL> servers [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> host . stop ( ) <EOL> time . sleep ( self . _service_down_time ) <EOL> post = { '<STR_LIT>' : { } } <EOL> self . admin_api . post_server_action ( servers [ <NUM_LIT:1> ] [ '<STR_LIT:id>' ] , post ) <EOL> server_after_failed_evac = self . _wait_for_state_change ( servers [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( server_after_failed_evac [ '<STR_LIT>' ] , <EOL> servers [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> host . start ( ) <EOL> def test_evacuate_with_affinity_no_valid_host ( self ) : <EOL> created_group = self . api . post_server_groups ( self . affinity ) <EOL> servers = self . _boot_servers_to_group ( created_group ) <EOL> host = self . _get_compute_service_by_host_name ( <EOL> servers [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> host . stop ( ) <EOL> time . sleep ( self . _service_down_time ) <EOL> post = { '<STR_LIT>' : { } } <EOL> self . admin_api . post_server_action ( servers [ <NUM_LIT:1> ] [ '<STR_LIT:id>' ] , post ) <EOL> server_after_failed_evac = self . _wait_for_state_change ( servers [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( server_after_failed_evac [ '<STR_LIT>' ] , <EOL> servers [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> host . start ( ) <EOL> def test_create_and_delete_groups ( self ) : <EOL> groups = [ self . anti_affinity , <EOL> self . affinity , <EOL> self . soft_affinity , <EOL> self . soft_anti_affinity ] <EOL> created_groups = [ ] <EOL> for group in groups : <EOL> created_group = self . api . post_server_groups ( group ) <EOL> created_groups . append ( created_group ) <EOL> self . assertEqual ( group [ '<STR_LIT:name>' ] , created_group [ '<STR_LIT:name>' ] ) <EOL> self . assertEqual ( group [ '<STR_LIT>' ] , created_group [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( [ ] , created_group [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( { } , created_group [ '<STR_LIT>' ] ) <EOL> self . assertIn ( '<STR_LIT:id>' , created_group ) <EOL> group_details = self . api . get_server_group ( created_group [ '<STR_LIT:id>' ] ) <EOL> self . assertEqual ( created_group , group_details ) <EOL> existing_groups = self . api . get_server_groups ( ) <EOL> self . assertIn ( created_group , existing_groups ) <EOL> existing_groups = self . api . get_server_groups ( ) <EOL> self . assertEqual ( len ( groups ) , len ( existing_groups ) ) <EOL> for group in created_groups : <EOL> self . api . delete_server_group ( group [ '<STR_LIT:id>' ] ) <EOL> existing_groups = self . api . get_server_groups ( ) <EOL> self . assertNotIn ( group , existing_groups ) <EOL> def test_boot_servers_with_soft_affinity ( self ) : <EOL> created_group = self . api . post_server_groups ( self . soft_affinity ) <EOL> servers = self . _boot_servers_to_group ( created_group ) <EOL> members = self . api . get_server_group ( created_group [ '<STR_LIT:id>' ] ) [ '<STR_LIT>' ] <EOL> self . assertEqual ( <NUM_LIT:2> , len ( servers ) ) <EOL> self . assertIn ( servers [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] , members ) <EOL> self . assertIn ( servers [ <NUM_LIT:1> ] [ '<STR_LIT:id>' ] , members ) <EOL> self . assertEqual ( servers [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <EOL> servers [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> def test_boot_servers_with_soft_affinity_no_resource_on_first_host ( self ) : <EOL> created_group = self . api . post_server_groups ( self . soft_affinity ) <EOL> flavor = self . api . get_flavors ( ) [ <NUM_LIT:2> ] <EOL> servers = self . _boot_servers_to_group ( created_group , flavor ) <EOL> third_server = self . _boot_a_server_to_group ( created_group , <EOL> flavor = flavor ) <EOL> members = self . api . get_server_group ( created_group [ '<STR_LIT:id>' ] ) [ '<STR_LIT>' ] <EOL> hosts = [ ] <EOL> for server in servers : <EOL> hosts . append ( server [ '<STR_LIT>' ] ) <EOL> self . assertIn ( third_server [ '<STR_LIT:id>' ] , members ) <EOL> self . assertNotIn ( third_server [ '<STR_LIT>' ] , hosts ) <EOL> def test_boot_servers_with_soft_anti_affinity ( self ) : <EOL> created_group = self . api . post_server_groups ( self . soft_anti_affinity ) <EOL> servers = self . _boot_servers_to_group ( created_group ) <EOL> members = self . api . get_server_group ( created_group [ '<STR_LIT:id>' ] ) [ '<STR_LIT>' ] <EOL> self . assertEqual ( <NUM_LIT:2> , len ( servers ) ) <EOL> self . assertIn ( servers [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] , members ) <EOL> self . assertIn ( servers [ <NUM_LIT:1> ] [ '<STR_LIT:id>' ] , members ) <EOL> self . assertNotEqual ( servers [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <EOL> servers [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> def test_boot_servers_with_soft_anti_affinity_one_available_host ( self ) : <EOL> self . compute2 . kill ( ) <EOL> created_group = self . api . post_server_groups ( self . soft_anti_affinity ) <EOL> servers = self . _boot_servers_to_group ( created_group ) <EOL> members = self . api . get_server_group ( created_group [ '<STR_LIT:id>' ] ) [ '<STR_LIT>' ] <EOL> host = servers [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> for server in servers : <EOL> self . assertIn ( server [ '<STR_LIT:id>' ] , members ) <EOL> self . assertEqual ( host , server [ '<STR_LIT>' ] ) <EOL> def test_rebuild_with_soft_affinity ( self ) : <EOL> untouched_server , rebuilt_server = self . _rebuild_with_group ( <EOL> self . soft_affinity ) <EOL> self . assertEqual ( untouched_server [ '<STR_LIT>' ] , <EOL> rebuilt_server [ '<STR_LIT>' ] ) <EOL> def test_rebuild_with_soft_anti_affinity ( self ) : <EOL> untouched_server , rebuilt_server = self . _rebuild_with_group ( <EOL> self . soft_anti_affinity ) <EOL> self . assertNotEqual ( untouched_server [ '<STR_LIT>' ] , <EOL> rebuilt_server [ '<STR_LIT>' ] ) <EOL> def _migrate_with_soft_affinity_policies ( self , group ) : <EOL> created_group = self . api . post_server_groups ( group ) <EOL> servers = self . _boot_servers_to_group ( created_group ) <EOL> post = { '<STR_LIT>' : { } } <EOL> self . admin_api . post_server_action ( servers [ <NUM_LIT:1> ] [ '<STR_LIT:id>' ] , post ) <EOL> migrated_server = self . _wait_for_state_change ( servers [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' ) <EOL> return [ migrated_server [ '<STR_LIT>' ] , <EOL> servers [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ] <EOL> def test_migrate_with_soft_affinity ( self ) : <EOL> migrated_server , other_server = ( <EOL> self . _migrate_with_soft_affinity_policies ( self . soft_affinity ) ) <EOL> self . assertNotEqual ( migrated_server , other_server ) <EOL> def test_migrate_with_soft_anti_affinity ( self ) : <EOL> migrated_server , other_server = ( <EOL> self . _migrate_with_soft_affinity_policies ( self . soft_anti_affinity ) ) <EOL> self . assertEqual ( migrated_server , other_server ) <EOL> def _evacuate_with_soft_anti_affinity_policies ( self , group ) : <EOL> created_group = self . api . post_server_groups ( group ) <EOL> servers = self . _boot_servers_to_group ( created_group ) <EOL> host = self . _get_compute_service_by_host_name ( <EOL> servers [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) <EOL> host . stop ( ) <EOL> time . sleep ( self . _service_down_time ) <EOL> post = { '<STR_LIT>' : { } } <EOL> self . admin_api . post_server_action ( servers [ <NUM_LIT:1> ] [ '<STR_LIT:id>' ] , post ) <EOL> evacuated_server = self . _wait_for_state_change ( servers [ <NUM_LIT:1> ] , '<STR_LIT>' ) <EOL> evacuated_server = self . admin_api . get_server ( evacuated_server [ '<STR_LIT:id>' ] ) <EOL> host . start ( ) <EOL> return [ evacuated_server [ '<STR_LIT>' ] , <EOL> servers [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ] <EOL> def test_evacuate_with_soft_affinity ( self ) : <EOL> evacuated_server , other_server = ( <EOL> self . _evacuate_with_soft_anti_affinity_policies ( <EOL> self . soft_affinity ) ) <EOL> self . assertNotEqual ( evacuated_server , other_server ) <EOL> def test_evacuate_with_soft_anti_affinity ( self ) : <EOL> evacuated_server , other_server = ( <EOL> self . _evacuate_with_soft_anti_affinity_policies ( <EOL> self . soft_anti_affinity ) ) <EOL> self . assertEqual ( evacuated_server , other_server ) </s>
