class TestCrossRegionExternalBackendServiceMigration(unittest.TestCase): def setUp(self): print('Initialize test environment.') project = os.environ["PROJECT_ID"] credentials, default_project = google.auth.default() self.compute = discovery.build('compute', 'v1', credentials=credentials) self.google_api_interface_region_1 = GoogleApiInterface( self.compute, project, 'us-central1', 'us-central1-a') self.test_resource_creator_region_1 = TestResourceCreator( self.google_api_interface_region_1) self.google_api_interface_region_2 = GoogleApiInterface( self.compute, project, 'us-east1', 'us-east1-b') self.test_resource_creator_region_2 = TestResourceCreator( self.google_api_interface_region_2) def testAutomodeSubnetworkExistsInBothRegions(self): """ A backend service has two instance groups from two regions as backends. The network is an auto-mode network, and it has subnet with the same name in all regions. Expectation: all the backends will be migrated """ ### create test resources auto_subnetwork_name = 'end-to-end-test-auto-subnetwork' try: target_network_selfLink = \ self.google_api_interface_region_1.get_network( auto_subnetwork_name)['selfLink'] except: target_network_selfLink = \ self.google_api_interface_region_1.create_auto_subnetwork( auto_subnetwork_name)[ 'targetLink'] group_name_1 = 'end-to-end-test-managed-instance-group-1' operation = self.test_resource_creator_region_1.create_regional_managed_instance_group( self.test_resource_creator_region_1. legacy_instance_template_selfLink, group_name_1, 'sample_multi_zone_managed_instance_group.json', ) instance_group_1_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_1_configs = self.google_api_interface_region_1.get_multi_zone_instance_template_configs( group_name_1) group_name_2 = 'end-to-end-test-managed-instance-group-2' operation = self.test_resource_creator_region_2.create_regional_managed_instance_group( self.test_resource_creator_region_2. legacy_instance_template_selfLink, group_name_2, 'sample_multi_zone_managed_instance_group.json', ) instance_group_2_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_2_configs = self.google_api_interface_region_2.get_multi_zone_instance_template_configs( group_name_2) backend_service_name = 'end-to-end-test-backend-service' operation = self.test_resource_creator_region_1.create_global_backend_service( 'sample_external_backend_service.json', backend_service_name, [instance_group_2_selfLink, instance_group_1_selfLink]) original_backend_service_configs = self.google_api_interface_region_1.get_global_backend_service_configs( backend_service_name) backend_service_selfLink = operation['targetLink'] ### start migration selfLink_executor = SelfLinkExecutor(self.compute, backend_service_selfLink, auto_subnetwork_name, auto_subnetwork_name) migration_handler = selfLink_executor.build_migration_handler() migration_handler.network_migration() ### check migration result new_backend_service_configs = self.google_api_interface_region_1.get_global_backend_service_configs( backend_service_name) # check backend service config self.assertTrue( resource_config_is_unchanged_including_network( original_backend_service_configs, new_backend_service_configs)) # check its backends new_instance_template_1_configs = self.google_api_interface_region_1.get_multi_zone_instance_template_configs( group_name_1) new_instance_template_2_configs = self.google_api_interface_region_2.get_multi_zone_instance_template_configs( group_name_2) self.assertTrue( instance_template_config_is_unchanged_except_for_network_and_name( original_instance_template_1_configs, new_instance_template_1_configs)) self.assertTrue( instance_template_config_is_unchanged_except_for_network_and_name( original_instance_template_2_configs, new_instance_template_2_configs)) # network changed self.assertTrue( check_instance_template_network(new_instance_template_1_configs, target_network_selfLink)) self.assertTrue( check_instance_template_network(new_instance_template_2_configs, target_network_selfLink)) print('Pass the current test') def testCustomSubnetworkExistsInBothRegions(self): """ A backend service has two instance groups from two regions as backends. Expectation: all the backends will be migrated """ ### create test resources group_name_1 = 'end-to-end-test-managed-instance-group-1' operation = self.test_resource_creator_region_1.create_regional_managed_instance_group( self.test_resource_creator_region_1. legacy_instance_template_selfLink, group_name_1, 'sample_multi_zone_managed_instance_group.json', ) instance_group_1_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_1_configs = self.google_api_interface_region_1.get_multi_zone_instance_template_configs( group_name_1) group_name_2 = 'end-to-end-test-managed-instance-group-2' operation = self.test_resource_creator_region_2.create_regional_managed_instance_group( self.test_resource_creator_region_2. legacy_instance_template_selfLink, group_name_2, 'sample_multi_zone_managed_instance_group.json', ) instance_group_2_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_2_configs = self.google_api_interface_region_2.get_multi_zone_instance_template_configs( group_name_2) backend_service_name = 'end-to-end-test-backend-service' operation = self.test_resource_creator_region_1.create_global_backend_service( 'sample_external_backend_service.json', backend_service_name, [instance_group_2_selfLink, instance_group_1_selfLink]) original_backend_service_configs = self.google_api_interface_region_1.get_global_backend_service_configs( backend_service_name) backend_service_selfLink = operation['targetLink'] ### start migration selfLink_executor = SelfLinkExecutor( self.compute, backend_service_selfLink, self.test_resource_creator_region_1.network_name, self.test_resource_creator_region_1.subnetwork_name) migration_handler = selfLink_executor.build_migration_handler() migration_handler.network_migration() ### check migration result new_backend_service_configs = self.google_api_interface_region_1.get_global_backend_service_configs( backend_service_name) # check backend service config self.assertTrue( resource_config_is_unchanged_including_network( original_backend_service_configs, new_backend_service_configs)) # check its backends new_instance_template_1_configs = self.google_api_interface_region_1.get_multi_zone_instance_template_configs( group_name_1) new_instance_template_2_configs = self.google_api_interface_region_2.get_multi_zone_instance_template_configs( group_name_2) self.assertTrue( instance_template_config_is_unchanged_except_for_network_and_name( original_instance_template_1_configs, new_instance_template_1_configs)) self.assertTrue( instance_template_config_is_unchanged_except_for_network_and_name( original_instance_template_2_configs, new_instance_template_2_configs)) # network changed self.assertTrue( check_instance_template_network( new_instance_template_1_configs, self.test_resource_creator_region_1.network_selfLink, self.test_resource_creator_region_1.subnetwork_selfLink)) self.assertTrue( check_instance_template_network( new_instance_template_2_configs, self.test_resource_creator_region_2.network_selfLink, self.test_resource_creator_region_2.subnetwork_selfLink)) print('Pass the current test') def testCustomSubnetworkExistsInOnlyOneRegion(self): """ A backend service has two instance groups from two regions as backends. Expectation: rollback will happen """ ### create test resources target_subnetwork_name = 'subnetwork-only-in-region-1' target_subnetwork_selfLink = \ self.google_api_interface_region_1.create_subnetwork_using_random_ip_range( target_subnetwork_name, self.test_resource_creator_region_1.network_selfLink )['targetLink'] group_name_1 = 'end-to-end-test-managed-instance-group-1' operation = self.test_resource_creator_region_1.create_regional_managed_instance_group( self.test_resource_creator_region_1. legacy_instance_template_selfLink, group_name_1, 'sample_multi_zone_managed_instance_group.json', ) instance_group_1_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_1_configs = self.google_api_interface_region_1.get_multi_zone_instance_template_configs( group_name_1) group_name_2 = 'end-to-end-test-managed-instance-group-2' operation = self.test_resource_creator_region_2.create_regional_managed_instance_group( self.test_resource_creator_region_2. legacy_instance_template_selfLink, group_name_2, 'sample_multi_zone_managed_instance_group.json', ) instance_group_2_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_2_configs = self.google_api_interface_region_2.get_multi_zone_instance_template_configs( group_name_2) backend_service_name = 'end-to-end-test-backend-service' operation = self.test_resource_creator_region_1.create_global_backend_service( 'sample_external_backend_service.json', backend_service_name, [instance_group_2_selfLink, instance_group_1_selfLink]) original_backend_service_configs = self.google_api_interface_region_1.get_global_backend_service_configs( backend_service_name) backend_service_selfLink = operation['targetLink'] ### start migration selfLink_executor = SelfLinkExecutor( self.compute, backend_service_selfLink, self.test_resource_creator_region_1.network_name, target_subnetwork_name) with self.assertRaises(MigrationFailed): migration_handler = selfLink_executor.build_migration_handler() migration_handler.network_migration() ### check migration result new_backend_service_configs = self.google_api_interface_region_1.get_global_backend_service_configs( backend_service_name) # check backend service config self.assertTrue( resource_config_is_unchanged_including_network( original_backend_service_configs, new_backend_service_configs)) # check its backends new_instance_template_1_configs = self.google_api_interface_region_1.get_multi_zone_instance_template_configs( group_name_1) new_instance_template_2_configs = self.google_api_interface_region_2.get_multi_zone_instance_template_configs( group_name_2) # configs are unchanged including network self.assertTrue( instance_template_config_is_unchanged( original_instance_template_1_configs, new_instance_template_1_configs)) self.assertTrue( instance_template_config_is_unchanged( original_instance_template_2_configs, new_instance_template_2_configs)) print('Pass the current test') def tearDown(self) -> None: self.doCleanups() def doCleanups(self) -> None: self.google_api_interface_region_1.clean_all_resources() self.google_api_interface_region_2.clean_all_resources()
class TestInternalSelfManagedForwardingRuleMigration(unittest.TestCase): def setUp(self): print('Initialize test environment.') project = os.environ["PROJECT_ID"] credentials, default_project = google.auth.default() self.compute = discovery.build('compute', 'v1', credentials=credentials) self.google_api_interface = GoogleApiInterface(self.compute, project, 'us-central1', 'us-central1-a') self.test_resource_creator = TestResourceCreator( self.google_api_interface) def testWithTargetHttpProxy(self): ### create test resources forwarding_rule_name = 'end-to-end-test-forwarding-rule' group_name_1 = 'end-to-end-test-managed-instance-group-1' operation = self.test_resource_creator.create_regional_managed_instance_group( self.test_resource_creator.legacy_instance_template_selfLink, group_name_1, 'sample_multi_zone_managed_instance_group.json', ) instance_group_1_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) backend_service_name = 'end-to-end-test-backend-service' original_backend_selfLinks = [instance_group_1_selfLink] operation = self.test_resource_creator.create_global_backend_service( 'sample_internal_self_managed_backend_service.json', backend_service_name, original_backend_selfLinks) backend_service_selfLink = operation['targetLink'] urlmap_selfLink = \ self.test_resource_creator.create_urlmapping(backend_service_name, backend_service_selfLink)[ 'targetLink'] target_proxy_name = forwarding_rule_name proxy_selfLink = self.test_resource_creator.create_http_target_proxy( target_proxy_name, urlmap_selfLink)['targetLink'] forwarding_rule_selfLink = \ self.test_resource_creator.create_global_forwarding_rule_with_target( 'sample_internal_self_managed_forwarding_rule.json', forwarding_rule_name, proxy_selfLink, self.test_resource_creator.legacy_network_selfLink)[ 'targetLink'] original_backend_service_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_name) original_forwarding_rule_config = self.google_api_interface.get_global_forwarding_rule_config( forwarding_rule_name) ### start migration selfLink_executor = SelfLinkExecutor( self.compute, forwarding_rule_selfLink, self.test_resource_creator.network_name, self.test_resource_creator.subnetwork_name, ) migration_handler = selfLink_executor.build_migration_handler() migration_handler.network_migration() ### check migration result # check forwarding rule config new_forwarding_rule_config = self.google_api_interface.get_global_forwarding_rule_config( forwarding_rule_name) self.assertTrue( resource_config_is_unchanged_except_for_network( original_forwarding_rule_config, new_forwarding_rule_config)) self.assertTrue( check_selfLink_equal(new_forwarding_rule_config['network'], self.test_resource_creator.network_selfLink)) # check backend service config new_backend_service_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_name) self.assertTrue( resource_config_is_unchanged_except_for_network( original_backend_service_configs, new_backend_service_configs)) # check its backends new_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) self.assertTrue( instance_template_config_is_unchanged_except_for_network_and_name( original_instance_template_1_configs, new_instance_template_1_configs)) # network changed self.assertTrue( check_instance_template_network( new_instance_template_1_configs, self.test_resource_creator.network_selfLink, self.test_resource_creator.subnetwork_selfLink)) print('Pass the current test') def testWithTargetGrpcProxy(self): ### create test resources forwarding_rule_name = 'end-to-end-test-forwarding-rule' group_name_1 = 'end-to-end-test-managed-instance-group-1' operation = self.test_resource_creator.create_regional_managed_instance_group( self.test_resource_creator.legacy_instance_template_selfLink, group_name_1, 'sample_multi_zone_managed_instance_group.json', ) instance_group_1_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) backend_service_name = 'end-to-end-test-backend-service' original_backend_selfLinks = [instance_group_1_selfLink] operation = self.test_resource_creator.create_global_backend_service( 'sample_internal_self_managed_backend_service.json', backend_service_name, original_backend_selfLinks) backend_service_selfLink = operation['targetLink'] urlmap_selfLink = \ self.test_resource_creator.create_urlmapping(backend_service_name, backend_service_selfLink)[ 'targetLink'] grpc_proxy_name = forwarding_rule_name proxy_selfLink = \ self.google_api_interface.create_grpc_proxy(grpc_proxy_name, urlmap_selfLink)[ 'targetLink'] forwarding_rule_selfLink = \ self.test_resource_creator.create_global_forwarding_rule_with_target( 'sample_internal_self_managed_forwarding_rule.json', forwarding_rule_name, proxy_selfLink, self.test_resource_creator.legacy_network_selfLink)[ 'targetLink'] original_backend_service_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_name) original_forwarding_rule_config = self.google_api_interface.get_global_forwarding_rule_config( forwarding_rule_name) ### start migration selfLink_executor = SelfLinkExecutor( self.compute, forwarding_rule_selfLink, self.test_resource_creator.network_name, self.test_resource_creator.subnetwork_name, ) migration_handler = selfLink_executor.build_migration_handler() migration_handler.network_migration() ### check migration result # check forwarding rule config new_forwarding_rule_config = self.google_api_interface.get_global_forwarding_rule_config( forwarding_rule_name) self.assertTrue( resource_config_is_unchanged_except_for_network( original_forwarding_rule_config, new_forwarding_rule_config)) self.assertTrue( check_selfLink_equal(new_forwarding_rule_config['network'], self.test_resource_creator.network_selfLink)) # check backend service config new_backend_service_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_name) self.assertTrue( resource_config_is_unchanged_except_for_network( original_backend_service_configs, new_backend_service_configs)) # check its backends new_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) self.assertTrue( instance_template_config_is_unchanged_except_for_network_and_name( original_instance_template_1_configs, new_instance_template_1_configs)) # network changed self.assertTrue( check_instance_template_network( new_instance_template_1_configs, self.test_resource_creator.network_selfLink, self.test_resource_creator.subnetwork_selfLink)) print('Pass the current test') def tearDown(self) -> None: pass def doCleanups(self) -> None: self.google_api_interface.clean_all_resources()
class TestExternalForwardingRuleMigration(unittest.TestCase): def setUp(self): print('Initialize test environment.') project = os.environ["PROJECT_ID"] credentials, default_project = google.auth.default() self.compute = discovery.build('compute', 'v1', credentials=credentials) self.google_api_interface = GoogleApiInterface(self.compute, project, 'us-central1', 'us-central1-a') self.test_resource_creator = TestResourceCreator( self.google_api_interface) def testWithTargetHttpProxy(self): ### create test resources forwarding_rule_name = 'end-to-end-test-forwarding-rule' group_name_1 = 'end-to-end-test-managed-instance-group-1' operation = self.test_resource_creator.create_regional_managed_instance_group( self.test_resource_creator.legacy_instance_template_selfLink, group_name_1, 'sample_multi_zone_managed_instance_group.json', ) instance_group_1_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) backend_service_name = 'end-to-end-test-backend-service' original_backend_selfLinks = [instance_group_1_selfLink] operation = self.test_resource_creator.create_global_backend_service( 'sample_http_backend_service.json', backend_service_name, original_backend_selfLinks) original_backend_service_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_name) backend_service_selfLink = operation['targetLink'] urlmap_selfLink = \ self.test_resource_creator.create_urlmapping(backend_service_name, backend_service_selfLink)[ 'targetLink'] proxy_selfLink = self.test_resource_creator.create_http_target_proxy( backend_service_name, urlmap_selfLink)['targetLink'] forwarding_rule_selfLink = \ self.test_resource_creator.create_global_forwarding_rule_with_target( 'sample_global_forwarding_rule.json', forwarding_rule_name, proxy_selfLink)['targetLink'] original_forwarding_rule_config = self.google_api_interface.get_global_forwarding_rule_config( forwarding_rule_name) ### start migration selfLink_executor = SelfLinkExecutor( self.compute, forwarding_rule_selfLink, self.test_resource_creator.network_name, self.test_resource_creator.subnetwork_name, ) migration_handler = selfLink_executor.build_migration_handler() migration_handler.network_migration() ### check migration result # check forwarding rule config new_forwarding_rule_config = self.google_api_interface.get_global_forwarding_rule_config( forwarding_rule_name) self.assertTrue( resource_config_is_unchanged_except_for_network( original_forwarding_rule_config, new_forwarding_rule_config)) # check backend service config new_backend_service_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_name) self.assertTrue( resource_config_is_unchanged_except_for_network( original_backend_service_configs, new_backend_service_configs)) # check its backends new_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) self.assertTrue( instance_template_config_is_unchanged_except_for_network_and_name( original_instance_template_1_configs, new_instance_template_1_configs)) # network changed self.assertTrue( check_instance_template_network( new_instance_template_1_configs, self.test_resource_creator.network_selfLink, self.test_resource_creator.subnetwork_selfLink)) print('Pass the current test') def testWithTargetHttpProxyRelatedToMultipleBackendServices(self): """ The urlMapping is related to two backend service. All the backend services will be migrated. """ ### create test resrouces forwarding_rule_name = 'end-to-end-test-forwarding-rule' group_name_1 = 'end-to-end-test-managed-instance-group-1' operation = self.test_resource_creator.create_regional_managed_instance_group( self.test_resource_creator.legacy_instance_template_selfLink, group_name_1, 'sample_multi_zone_managed_instance_group.json', ) instance_group_1_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) group_name_2 = 'end-to-end-test-managed-instance-group-2' operation = self.test_resource_creator.create_regional_managed_instance_group( self.test_resource_creator.legacy_instance_template_selfLink, group_name_2, 'sample_multi_zone_managed_instance_group.json', ) instance_group_2_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_2_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_2) backend_service_name_1 = 'end-to-end-test-backend-service-1' original_backend_selfLinks_1 = [instance_group_1_selfLink] backend_service_selfLink_1 = \ self.test_resource_creator.create_global_backend_service( 'sample_http_backend_service.json', backend_service_name_1, original_backend_selfLinks_1)[ 'targetLink'] backend_service_name_2 = 'end-to-end-test-backend-service-2' original_backend_selfLinks_2 = [instance_group_2_selfLink] backend_service_selfLink_2 = \ self.test_resource_creator.create_global_backend_service( 'sample_http_backend_service.json', backend_service_name_2, original_backend_selfLinks_2)[ 'targetLink'] backend_service_selfLinks = [ backend_service_selfLink_1, backend_service_selfLink_2 ] urlmap_name = 'end-to-ent-test-urlmapping' urlmap_selfLink = \ self.test_resource_creator.create_urlmapping_using_two_backend_service( urlmap_name, backend_service_selfLinks)[ 'targetLink'] proxy_selfLink = self.test_resource_creator.create_http_target_proxy( urlmap_name, urlmap_selfLink)['targetLink'] forwarding_rule_selfLink = \ self.test_resource_creator.create_global_forwarding_rule_with_target( 'sample_global_forwarding_rule.json', forwarding_rule_name, proxy_selfLink)['targetLink'] original_backend_service_configs_1 = self.google_api_interface.get_global_backend_service_configs( backend_service_name_1) original_backend_service_configs_2 = self.google_api_interface.get_global_backend_service_configs( backend_service_name_2) ### start migration selfLink_executor = SelfLinkExecutor( self.compute, forwarding_rule_selfLink, self.test_resource_creator.network_name, self.test_resource_creator.subnetwork_name, ) migration_handler = selfLink_executor.build_migration_handler() migration_handler.network_migration() ### check migration result new_backend_service_configs_1 = self.google_api_interface.get_global_backend_service_configs( backend_service_name_1) new_backend_service_configs_2 = self.google_api_interface.get_global_backend_service_configs( backend_service_name_2) self.assertTrue( resource_config_is_unchanged_except_for_network( original_backend_service_configs_1, new_backend_service_configs_1)) self.assertTrue( resource_config_is_unchanged_except_for_network( original_backend_service_configs_2, new_backend_service_configs_2)) # check its backends new_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) new_instance_template_2_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_2) self.assertTrue( instance_template_config_is_unchanged_except_for_network_and_name( original_instance_template_1_configs, new_instance_template_1_configs)) self.assertTrue( instance_template_config_is_unchanged_except_for_network_and_name( original_instance_template_2_configs, new_instance_template_2_configs)) # network changed self.assertTrue( check_instance_template_network( new_instance_template_1_configs, self.test_resource_creator.network_selfLink, self.test_resource_creator.subnetwork_selfLink)) self.assertTrue( check_instance_template_network( new_instance_template_2_configs, self.test_resource_creator.network_selfLink, self.test_resource_creator.subnetwork_selfLink)) print('Pass the current test') def testWithTargetTcpProxy(self): ### create test resources forwarding_rule_name = 'end-to-end-test-forwarding-rule' group_name_1 = 'end-to-end-test-managed-instance-group-1' operation = self.test_resource_creator.create_regional_managed_instance_group( self.test_resource_creator.legacy_instance_template_selfLink, group_name_1, 'sample_multi_zone_managed_instance_group.json', ) instance_group_1_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) backend_service_name = 'end-to-end-test-backend-service' backend_service_selfLink = \ self.test_resource_creator.create_global_backend_service( 'sample_tcp_backend_service.json', backend_service_name, [instance_group_1_selfLink])['targetLink'] original_backend_service_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_name) proxy_selfLink = \ self.google_api_interface.create_tcp_proxy(group_name_1, backend_service_selfLink)[ 'targetLink'] forwarding_rule_selfLink = \ self.test_resource_creator.create_global_forwarding_rule_with_target( 'sample_tcp_forwarding_rule_external.json', forwarding_rule_name, proxy_selfLink)['targetLink'] ### start migration selfLink_executor = SelfLinkExecutor( self.compute, forwarding_rule_selfLink, self.test_resource_creator.network_name, self.test_resource_creator.subnetwork_name, ) migration_handler = selfLink_executor.build_migration_handler() migration_handler.network_migration() ### check migration result new_backend_service_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_name) self.assertTrue( resource_config_is_unchanged_except_for_network( original_backend_service_configs, new_backend_service_configs)) # check its backends new_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) self.assertTrue( instance_template_config_is_unchanged_except_for_network_and_name( original_instance_template_1_configs, new_instance_template_1_configs)) # network changed self.assertTrue( check_instance_template_network( new_instance_template_1_configs, self.test_resource_creator.network_selfLink, self.test_resource_creator.subnetwork_selfLink)) print('Pass the current test') def testWithTargetSslProxy(self): ### create test resources forwarding_rule_name = 'end-to-end-test-forwarding-rule' group_name_1 = 'end-to-end-test-managed-instance-group-1' operation = self.test_resource_creator.create_regional_managed_instance_group( self.test_resource_creator.legacy_instance_template_selfLink, group_name_1, 'sample_multi_zone_managed_instance_group.json', ) instance_group_1_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) backend_service_name = 'end-to-end-test-backend-service' backend_service_selfLink = \ self.test_resource_creator.create_global_backend_service( 'sample_tcp_backend_service.json', backend_service_name, [instance_group_1_selfLink])['targetLink'] original_backend_service_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_name) ssl_certificate_name = forwarding_rule_name ssl_certificate_selfLink = \ self.google_api_interface.create_ssl_certificate( ssl_certificate_name)[ 'targetLink'] ssl_proxy_name = forwarding_rule_name proxy_selfLink = \ self.google_api_interface.create_ssl_proxy(ssl_proxy_name, backend_service_selfLink, ssl_certificate_selfLink)[ 'targetLink'] forwarding_rule_selfLink = \ self.test_resource_creator.create_global_forwarding_rule_with_target( 'sample_ssl_forwarding_rule_external.json', forwarding_rule_name, proxy_selfLink)['targetLink'] ### start migration selfLink_executor = SelfLinkExecutor( self.compute, forwarding_rule_selfLink, self.test_resource_creator.network_name, self.test_resource_creator.subnetwork_name, ) migration_handler = selfLink_executor.build_migration_handler() migration_handler.network_migration() ### check migration result new_backend_service_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_name) # check backend service config self.assertTrue( resource_config_is_unchanged_except_for_network( original_backend_service_configs, new_backend_service_configs)) # check its backends new_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) self.assertTrue( instance_template_config_is_unchanged_except_for_network_and_name( original_instance_template_1_configs, new_instance_template_1_configs)) # network changed self.assertTrue( check_instance_template_network( new_instance_template_1_configs, self.test_resource_creator.network_selfLink, self.test_resource_creator.subnetwork_selfLink)) print('Pass the current test') def testWithTargetHttpsProxy(self): ### create test resources forwarding_rule_name = 'end-to-end-test-forwarding-rule' group_name_1 = 'end-to-end-test-managed-instance-group-1' operation = self.test_resource_creator.create_regional_managed_instance_group( self.test_resource_creator.legacy_instance_template_selfLink, group_name_1, 'sample_multi_zone_managed_instance_group.json', ) instance_group_1_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) backend_service_name = 'end-to-end-test-backend-service' operation = self.test_resource_creator.create_global_backend_service( 'sample_https_backend_service.json', backend_service_name, [instance_group_1_selfLink]) original_backend_service_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_name) backend_service_selfLink = operation['targetLink'] urlmap_selfLink = \ self.test_resource_creator.create_urlmapping(backend_service_name, backend_service_selfLink)[ 'targetLink'] ssl_certificate_name = forwarding_rule_name ssl_certificate_selfLink = \ self.google_api_interface.create_ssl_certificate( ssl_certificate_name)[ 'targetLink'] https_proxy_name = forwarding_rule_name proxy_selfLink = \ self.google_api_interface.create_https_proxy(https_proxy_name, urlmap_selfLink, ssl_certificate_selfLink)[ 'targetLink'] forwarding_rule_selfLink = \ self.test_resource_creator.create_global_forwarding_rule_with_target( 'sample_global_https_forwarding_rule.json', forwarding_rule_name, proxy_selfLink)['targetLink'] original_forwarding_rule_config = self.google_api_interface.get_global_forwarding_rule_config( forwarding_rule_name) ### start migration selfLink_executor = SelfLinkExecutor( self.compute, forwarding_rule_selfLink, self.test_resource_creator.network_name, self.test_resource_creator.subnetwork_name, ) migration_handler = selfLink_executor.build_migration_handler() migration_handler.network_migration() ### check migration result new_backend_service_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_name) # check forwarding rule config new_forwarding_rule_config = self.google_api_interface.get_global_forwarding_rule_config( forwarding_rule_name) self.assertTrue( resource_config_is_unchanged_except_for_network( original_forwarding_rule_config, new_forwarding_rule_config)) # check backend service config self.assertTrue( resource_config_is_unchanged_except_for_network( original_backend_service_configs, new_backend_service_configs)) # check its backends new_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) self.assertTrue( instance_template_config_is_unchanged_except_for_network_and_name( original_instance_template_1_configs, new_instance_template_1_configs)) # network changed self.assertTrue( check_instance_template_network( new_instance_template_1_configs, self.test_resource_creator.network_selfLink, self.test_resource_creator.subnetwork_selfLink)) print('Pass the current test') def tearDown(self) -> None: pass def doCleanups(self) -> None: self.google_api_interface.clean_all_resources()
class TestExternalBackendServiceMigration(unittest.TestCase): def setUp(self): print('Initialize test environment.') project = os.environ["PROJECT_ID"] credentials, default_project = google.auth.default() self.compute = discovery.build('compute', 'v1', credentials=credentials) self.google_api_interface = GoogleApiInterface(self.compute, project, 'us-central1', 'us-central1-a') self.test_resource_creator = TestResourceCreator( self.google_api_interface) def testWithMultipleBackendsAttached(self): """ A backend service has multiple backends Expectation: all the backends will be migrated """ ### create test resources group_name_1 = 'end-to-end-test-managed-instance-group-1' operation = self.test_resource_creator.create_regional_managed_instance_group( self.test_resource_creator.legacy_instance_template_selfLink, group_name_1, 'sample_multi_zone_managed_instance_group.json', ) instance_group_1_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) group_name_2 = 'end-to-end-test-managed-instance-group-2' operation = self.test_resource_creator.create_regional_managed_instance_group( self.test_resource_creator.legacy_instance_template_selfLink, group_name_2, 'sample_multi_zone_managed_instance_group.json', ) instance_group_2_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_2_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_2) backend_service_name = 'end-to-end-test-backend-service' original_backend_selfLinks = [ instance_group_2_selfLink, instance_group_1_selfLink ] operation = self.test_resource_creator.create_global_backend_service( 'sample_external_backend_service.json', backend_service_name, original_backend_selfLinks) backend_service_selfLink = operation['targetLink'] ### start migration selfLink_executor = SelfLinkExecutor( self.compute, backend_service_selfLink, self.test_resource_creator.network_name, self.test_resource_creator.subnetwork_name, ) migration_handler = selfLink_executor.build_migration_handler() migration_handler.network_migration() ### check migration result new_backend_service_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_name) new_backend_selfLinks = self.google_api_interface.get_backends_links_from_backend_service_configs( new_backend_service_configs) # check backend service config self.assertTrue( compare_two_list(original_backend_selfLinks, new_backend_selfLinks)) # check its backends new_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) new_instance_template_2_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_2) self.assertTrue( instance_template_config_is_unchanged_except_for_network_and_name( original_instance_template_1_configs, new_instance_template_1_configs)) self.assertTrue( instance_template_config_is_unchanged_except_for_network_and_name( original_instance_template_2_configs, new_instance_template_2_configs)) # network changed self.assertTrue( check_instance_template_network( new_instance_template_1_configs, self.test_resource_creator.network_selfLink, self.test_resource_creator.subnetwork_selfLink)) self.assertTrue( check_instance_template_network( new_instance_template_2_configs, self.test_resource_creator.network_selfLink, self.test_resource_creator.subnetwork_selfLink)) print('Pass the current test') def testWithNoBackendsAttached(self): backend_service_name = 'end-to-end-test-backend-service' operation = self.test_resource_creator.create_global_backend_service( 'sample_external_backend_service.json', backend_service_name, []) original_backend_service_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_name) backend_service_selfLink = operation['targetLink'] # start migration selfLink_executor = SelfLinkExecutor( self.compute, backend_service_selfLink, self.test_resource_creator.network_name, self.test_resource_creator.subnetwork_name, ) migration_handler = selfLink_executor.build_migration_handler() migration_handler.network_migration() ### check migration result new_backend_service_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_name) self.assertTrue( resource_config_is_unchanged_except_for_network( original_backend_service_configs, new_backend_service_configs)) print('Pass the current test') def testAForwardingRuleAttached(self): """ A forwaring rule is using the external backend service. Try to migrate the external backend service directly. Expectation: still can migrate """ group_name_1 = 'end-to-end-test-managed-instance-group-1' operation = self.test_resource_creator.create_regional_managed_instance_group( self.test_resource_creator.legacy_instance_template_selfLink, group_name_1, 'sample_multi_zone_managed_instance_group.json', ) instance_group_1_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) group_name_2 = 'end-to-end-test-managed-instance-group-2' operation = self.test_resource_creator.create_regional_managed_instance_group( self.test_resource_creator.legacy_instance_template_selfLink, group_name_2, 'sample_multi_zone_managed_instance_group.json', ) instance_group_2_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_2_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_2) backend_service_name = 'end-to-end-test-backend-service' original_backend_selfLinks = [ instance_group_2_selfLink, instance_group_1_selfLink ] operation = self.test_resource_creator.create_global_backend_service( 'sample_http_backend_service.json', backend_service_name, original_backend_selfLinks) backend_service_selfLink = operation['targetLink'] urlmap_selfLink = \ self.test_resource_creator.create_urlmapping(backend_service_name, backend_service_selfLink)[ 'targetLink'] proxy_selfLink = self.test_resource_creator.create_http_target_proxy( backend_service_name, urlmap_selfLink)['targetLink'] self.test_resource_creator.create_global_forwarding_rule_with_target( 'sample_global_forwarding_rule.json', backend_service_name, proxy_selfLink) # start migration selfLink_executor = SelfLinkExecutor( self.compute, backend_service_selfLink, self.test_resource_creator.network_name, self.test_resource_creator.subnetwork_name, ) migration_handler = selfLink_executor.build_migration_handler() migration_handler.network_migration() ### check migration result new_backend_service_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_name) new_backend_selfLinks = self.google_api_interface.get_backends_links_from_backend_service_configs( new_backend_service_configs) # check backend service config self.assertTrue( compare_two_list(original_backend_selfLinks, new_backend_selfLinks)) # check its backends new_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) new_instance_template_2_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_2) self.assertTrue( instance_template_config_is_unchanged_except_for_network_and_name( original_instance_template_1_configs, new_instance_template_1_configs)) self.assertTrue( instance_template_config_is_unchanged_except_for_network_and_name( original_instance_template_2_configs, new_instance_template_2_configs)) # network changed self.assertTrue( check_instance_template_network( new_instance_template_1_configs, self.test_resource_creator.network_selfLink, self.test_resource_creator.subnetwork_selfLink)) self.assertTrue( check_instance_template_network( new_instance_template_2_configs, self.test_resource_creator.network_selfLink, self.test_resource_creator.subnetwork_selfLink)) print('Pass the current test') def testSharingBackendWithAnotherBackendService(self): """ backend_service_1 and backend_service_2 share the same instance group. Try to migrate backend_service_1. Expectation: rollback will happen """ group_name_1 = 'end-to-end-test-managed-instance-group-1' operation = self.test_resource_creator.create_regional_managed_instance_group( self.test_resource_creator.legacy_instance_template_selfLink, group_name_1, 'sample_multi_zone_managed_instance_group.json', ) instance_group_1_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) backend_service_1_name = 'end-to-end-test-backend-service-1' backend_service_1_selfLink = \ self.test_resource_creator.create_global_backend_service( 'sample_external_backend_service.json', backend_service_1_name, [instance_group_1_selfLink])[ 'targetLink'] original_backend_service_1_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_1_name) backend_service_2_name = 'end-to-end-test-backend-service-2' backend_service_2_selfLink = \ self.test_resource_creator.create_global_backend_service( 'sample_external_backend_service.json', backend_service_2_name, [instance_group_1_selfLink])[ 'targetLink'] original_backend_service_2_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_2_name) # start migration selfLink_executor = SelfLinkExecutor( self.compute, backend_service_1_selfLink, self.test_resource_creator.network_name, self.test_resource_creator.subnetwork_name, ) with self.assertRaises(MigrationFailed): migration_handler = selfLink_executor.build_migration_handler() migration_handler.network_migration() ### check migration result new_backend_service_1_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_1_name) new_backend_service_2_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_2_name) # check backend service config self.assertTrue( resource_config_is_unchanged_including_network( original_backend_service_1_configs, new_backend_service_1_configs)) self.assertTrue( resource_config_is_unchanged_including_network( original_backend_service_2_configs, new_backend_service_2_configs)) # check its backends new_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) self.assertTrue( instance_template_config_is_unchanged( original_instance_template_1_configs, new_instance_template_1_configs)) print('Pass the current test') def testSharingBackendWithATargetPool(self): """ backend_service_1 share the same backend with a target pool. Try to migrate backend_service_1. Expectation: rollback will happen """ group_name_1 = 'end-to-end-test-managed-instance-group-1' operation = self.test_resource_creator.create_regional_managed_instance_group( self.test_resource_creator.legacy_instance_template_selfLink, group_name_1, 'sample_multi_zone_managed_instance_group.json', ) instance_group_1_selfLink = operation['targetLink'].replace( '/instanceGroupManagers/', '/instanceGroups/') original_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) original_instance_group_config = self.google_api_interface.get_multi_zone_managed_instance_group_configs( group_name_1) backend_service_1_name = 'end-to-end-test-backend-service-1' backend_service_1_selfLink = \ self.test_resource_creator.create_global_backend_service( 'sample_external_backend_service.json', backend_service_1_name, [instance_group_1_selfLink])[ 'targetLink'] original_backend_service_1_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_1_name) target_pool_name = 'end-to-end-test-target-pool' target_pool_selfLink = \ self.test_resource_creator.create_target_pool_with_health_check( 'sample_target_pool_with_no_instance.json', target_pool_name, [group_name_1], [], health_check_selfLink=None)['targetLink'] # start migration selfLink_executor = SelfLinkExecutor( self.compute, backend_service_1_selfLink, self.test_resource_creator.network_name, self.test_resource_creator.subnetwork_name, ) with self.assertRaises(MigrationFailed): migration_handler = selfLink_executor.build_migration_handler() migration_handler.network_migration() ### check migration result new_backend_service_1_configs = self.google_api_interface.get_global_backend_service_configs( backend_service_1_name) # check backend service config self.assertTrue( resource_config_is_unchanged_including_network( original_backend_service_1_configs, new_backend_service_1_configs)) # check its backends new_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs( group_name_1) self.assertTrue( instance_template_config_is_unchanged( original_instance_template_1_configs, new_instance_template_1_configs)) new_instance_group_config = self.google_api_interface.get_multi_zone_managed_instance_group_configs( group_name_1) self.assertTrue( managed_instance_group_config_is_unchanged_except_for_instance_template( original_instance_group_config, new_instance_group_config)) print('Pass the current test') def tearDown(self) -> None: pass def doCleanups(self) -> None: self.google_api_interface.clean_all_resources()