def test_marathon_delete_leader(marathon_service_name): original_leader = marathon_leader_ip() print('leader: {}'.format(original_leader)) common.abdicate_marathon_leader() wait_for_service_endpoint(marathon_service_name, timedelta(minutes=5).total_seconds(), path="ping") common.assert_marathon_leadership_changed(original_leader)
def test_marathon_delete_leader(marathon_service_name): original_leader = marathon_leader_ip() print('leader: {}'.format(original_leader)) common.abdicate_marathon_leader() wait_for_service_endpoint(marathon_service_name, timedelta(minutes=5).total_seconds(), path="ping") common.assert_marathon_leadership_changed(original_leader)
def __marathon_leadership_changed_in_mesosDNS(original_leader): """ This method uses mesosDNS to verify that the leadership changed. We have to retry because mesosDNS checks for changes only every 30s. """ current_leader = marathon_leader_ip() logger.info(f'Current leader according to MesosDNS: {current_leader}, original leader: {original_leader}') # NOQA E999 assert current_leader, "MesosDNS returned empty string for Marathon leader ip." error = f'Current leader did not change: original={original_leader}, current={current_leader}' # NOQA E999 assert original_leader != current_leader, error return current_leader
def __marathon_leadership_changed_in_mesosDNS(original_leader): """ This method uses mesosDNS to verify that the leadership changed. We have to retry because mesosDNS checks for changes only every 30s. """ current_leader = marathon_leader_ip() logger.info( f'Current leader according to MesosDNS: {current_leader}, original leader: {original_leader}' ) # NOQA E999 assert current_leader, "MesosDNS returned empty string for Marathon leader ip." error = f'Current leader did not change: original={original_leader}, current={current_leader}' # NOQA E999 assert original_leader != current_leader, error return current_leader
def get_marathon_leader_not_on_master_leader_node(): marathon_leader = marathon_leader_ip() master_leader = master_leader_ip() logger.info('marathon leader: {}'.format(marathon_leader)) logger.info('mesos leader: {}'.format(master_leader)) if marathon_leader == master_leader: delete_marathon_path('v2/leader') wait_for_service_endpoint('marathon', timedelta(minutes=5).total_seconds(), path="ping") marathon_leader = assert_marathon_leadership_changed(marathon_leader) logger.info('switched leader to: {}'.format(marathon_leader)) return marathon_leader
def get_marathon_leader_not_on_master_leader_node(): marathon_leader = marathon_leader_ip() master_leader = master_leader_ip() logger.info('marathon leader: {}'.format(marathon_leader)) logger.info('mesos leader: {}'.format(master_leader)) if marathon_leader == master_leader: delete_marathon_path('v2/leader') wait_for_service_endpoint('marathon', timedelta(minutes=5).total_seconds(), path="ping") marathon_leader = assert_marathon_leadership_changed(marathon_leader) logger.info('switched leader to: {}'.format(marathon_leader)) return marathon_leader
def test_marathon_backup_and_restore_leader(marathon_service_name): """Backup and restore meeting is done with only one master since new master has to be able to read the backup file that was created by the previous master and the easiest way to test it is when there is 1 master """ backup_file = 'backup.tar' backup_dir = '/tmp' backup_url = 'file://{}/{}'.format(backup_dir, backup_file) # Deploy a simple test app. It is expected to be there after leader reelection app_def = apps.sleep_app() app_id = app_def['id'] client = marathon.create_client() client.add_app(app_def) common.deployment_wait(service_id=app_id) app = client.get_app(app_id) assert app[ 'tasksRunning'] == 1, "The number of running tasks is {}, but 1 was expected".format( app["tasksRunning"]) task_id = app['tasks'][0]['id'] # Abdicate the leader with backup and restore original_leader = marathon_leader_ip() print('leader: {}'.format(original_leader)) params = '?backup={}&restore={}'.format(backup_url, backup_url) print('DELETE /v2/leader{}'.format(params)) common.abdicate_marathon_leader(params) # Wait for new leader (but same master server) to be up and ready common.wait_for_service_endpoint(marathon_service_name, timedelta(minutes=5).total_seconds(), path="ping") app = client.get_app(app_id) assert app[ 'tasksRunning'] == 1, "The number of running tasks is {}, but 1 was expected".format( app["tasksRunning"]) assert task_id == app['tasks'][0][ 'id'], "Task has a different ID after restore" # Check if the backup file exits and is valid cmd = 'tar -tf {}/{} | wc -l'.format(backup_dir, backup_file) status, data = run_command_on_master(cmd) assert status, 'Failed to validate backup file {}'.format(backup_url) assert int(data.rstrip()) > 0, "Backup file is empty"
def test_marathon_backup_and_restore_leader(marathon_service_name): """Backup and restore meeting is done with only one master since new master has to be able to read the backup file that was created by the previous master and the easiest way to test it is when there is 1 master """ backup_file = 'backup.tar' backup_dir = '/tmp' backup_url = 'file://{}/{}'.format(backup_dir, backup_file) # Deploy a simple test app. It is expected to be there after leader reelection app_def = apps.sleep_app() app_id = app_def['id'] client = marathon.create_client() client.add_app(app_def) deployment_wait(service_id=app_id) app = client.get_app(app_id) assert app['tasksRunning'] == 1, "The number of running tasks is {}, but 1 was expected".format(app["tasksRunning"]) task_id = app['tasks'][0]['id'] # Abdicate the leader with backup and restore original_leader = marathon_leader_ip() print('leader: {}'.format(original_leader)) params = '?backup={}&restore={}'.format(backup_url, backup_url) print('DELETE /v2/leader{}'.format(params)) common.abdicate_marathon_leader(params) # Wait for new leader (but same master server) to be up and ready wait_for_service_endpoint(marathon_service_name, timedelta(minutes=5).total_seconds(), path="ping") app = client.get_app(app_id) assert app['tasksRunning'] == 1, "The number of running tasks is {}, but 1 was expected".format(app["tasksRunning"]) assert task_id == app['tasks'][0]['id'], "Task has a different ID after restore" # Check if the backup file exits and is valid cmd = 'tar -tf {}/{} | wc -l'.format(backup_dir, backup_file) status, data = run_command_on_master(cmd) assert status, 'Failed to validate backup file {}'.format(backup_url) assert int(data.rstrip()) > 0, "Backup file is empty"
def test_marathon_backup_and_check_apps(marathon_service_name): backup_file1 = 'backup1.tar' backup_file2 = 'backup2.tar' backup_dir = '/tmp' for master_ip in get_all_master_ips(): run_command(master_ip, "rm {}/{}".format(backup_dir, backup_file1)) run_command(master_ip, "rm {}/{}".format(backup_dir, backup_file2)) backup_url1 = 'file://{}/{}'.format(backup_dir, backup_file1) backup_url2 = 'file://{}/{}'.format(backup_dir, backup_file2) original_leader = marathon_leader_ip() print('leader: {}'.format(original_leader)) app_def = apps.sleep_app() app_id = app_def['id'] client = marathon.create_client() client.add_app(app_def) common.deployment_wait(service_id=app_id) app = client.get_app(app_id) assert app[ 'tasksRunning'] == 1, "The number of running tasks is {}, but 1 was expected".format( app["tasksRunning"]) # Abdicate the leader with backup original_leader = marathon_leader_ip() params = '?backup={}'.format(backup_url1) common.abdicate_marathon_leader(params) common.wait_for_service_endpoint(marathon_service_name, timedelta(minutes=5).total_seconds(), path="ping") # wait until leader changed common.assert_marathon_leadership_changed(original_leader) @retrying.retry(wait_fixed=1000, stop_max_attempt_number=30, retry_on_exception=common.ignore_exception) def check_app_existence(expected_instances): try: app = client.get_app(app_id) except Exception as e: if expected_instances != 0: raise e else: if expected_instances == 0: assert False, "The application resurrected" else: app['tasksRunning'] == expected_instances, \ "The number of running tasks is {}, but {} was expected".format( app["tasksRunning"], expected_instances) # check if app definition is still there and one instance is still running after new leader was elected check_app_existence(1) # then remove client.remove_app(app_id) common.deployment_wait(service_id=app_id) check_app_existence(0) # Do a second backup. Before MARATHON-7525 we had the problem, that doing a backup after an app was deleted # leads to the state that marathon was not able to re-start, because the second backup failed constantly. # Abdicate the leader with backup original_leader = marathon_leader_ip() print('leader: {}'.format(original_leader)) params = '?backup={}'.format(backup_url2) print('DELETE /v2/leader{}'.format(params)) common.abdicate_marathon_leader(params) common.wait_for_service_endpoint(marathon_service_name, timedelta(minutes=5).total_seconds(), path="ping") # wait until leader changed # if leader changed, this means that marathon was able to start again, which is great :-). common.assert_marathon_leadership_changed(original_leader) # check if app definition is still not there and no instance is running after new leader was elected check_app_existence(0)
def test_marathon_delete_leader_and_check_apps(marathon_service_name): original_leader = marathon_leader_ip() print('leader: {}'.format(original_leader)) app_def = apps.sleep_app() app_id = app_def['id'] client = marathon.create_client() client.add_app(app_def) common.deployment_wait(service_id=app_id) app = client.get_app(app_id) assert app[ 'tasksRunning'] == 1, "The number of running tasks is {}, but 1 was expected".format( app["tasksRunning"]) # abdicate leader after app was started successfully common.abdicate_marathon_leader() common.wait_for_service_endpoint(marathon_service_name, timedelta(minutes=5).total_seconds(), path="ping") # wait until leader changed common.assert_marathon_leadership_changed(original_leader) original_leader = marathon_leader_ip() @retrying.retry(wait_fixed=1000, stop_max_attempt_number=30, retry_on_exception=common.ignore_exception) def check_app_existence(expected_instances): app = client.get_app(app_id) assert app['tasksRunning'] == expected_instances assert app['tasksRunning'] == expected_instances, \ "The number of running tasks is {}, but {} was expected".format(app["tasksRunning"], expected_instances) # check if app definition is still there and one instance is still running after new leader was elected check_app_existence(1) @retrying.retry(wait_fixed=1000, stop_max_attempt_number=30, retry_on_exception=common.ignore_exception) def remove_app(app_id): client.remove_app(app_id) remove_app(app_id) common.deployment_wait(service_id=app_id) try: client.get_app(app_id) except Exception: pass else: assert False, "The application resurrected" # abdicate leader after app was started successfully common.abdicate_marathon_leader() common.wait_for_service_endpoint(marathon_service_name, timedelta(minutes=5).total_seconds(), path="ping") # wait until leader changed common.assert_marathon_leadership_changed(original_leader) # check if app definition is still not there try: client.get_app(app_id) except Exception: pass else: assert False, "The application resurrected"
def test_marathon_delete_leader_and_check_apps(marathon_service_name): original_leader = marathon_leader_ip() print('leader: {}'.format(original_leader)) app_def = apps.sleep_app() app_id = app_def['id'] client = marathon.create_client() client.add_app(app_def) deployment_wait(service_id=app_id) app = client.get_app(app_id) assert app['tasksRunning'] == 1, "The number of running tasks is {}, but 1 was expected".format(app["tasksRunning"]) # abdicate leader after app was started successfully common.abdicate_marathon_leader() wait_for_service_endpoint(marathon_service_name, timedelta(minutes=5).total_seconds(), path="ping") # wait until leader changed common.assert_marathon_leadership_changed(original_leader) original_leader = marathon_leader_ip() @retrying.retry(wait_fixed=1000, stop_max_attempt_number=30, retry_on_exception=common.ignore_exception) def check_app_existence(expected_instances): app = client.get_app(app_id) assert app['tasksRunning'] == expected_instances assert app['tasksRunning'] == expected_instances, \ "The number of running tasks is {}, but {} was expected".format(app["tasksRunning"], expected_instances) # check if app definition is still there and one instance is still running after new leader was elected check_app_existence(1) @retrying.retry(wait_fixed=1000, stop_max_attempt_number=30, retry_on_exception=common.ignore_exception) def remove_app(app_id): client.remove_app(app_id) remove_app(app_id) deployment_wait(service_id=app_id) try: client.get_app(app_id) except Exception: pass else: assert False, "The application resurrected" # abdicate leader after app was started successfully common.abdicate_marathon_leader() wait_for_service_endpoint(marathon_service_name, timedelta(minutes=5).total_seconds(), path="ping") # wait until leader changed common.assert_marathon_leadership_changed(original_leader) # check if app definition is still not there try: client.get_app(app_id) except Exception: pass else: assert False, "The application resurrected"
def test_marathon_backup_and_check_apps(marathon_service_name): backup_file1 = 'backup1.tar' backup_file2 = 'backup2.tar' backup_dir = '/tmp' for master_ip in get_all_master_ips(): run_command(master_ip, "rm {}/{}".format(backup_dir, backup_file1)) run_command(master_ip, "rm {}/{}".format(backup_dir, backup_file2)) backup_url1 = 'file://{}/{}'.format(backup_dir, backup_file1) backup_url2 = 'file://{}/{}'.format(backup_dir, backup_file2) original_leader = marathon_leader_ip() print('leader: {}'.format(original_leader)) app_def = apps.sleep_app() app_id = app_def['id'] client = marathon.create_client() client.add_app(app_def) deployment_wait(service_id=app_id) app = client.get_app(app_id) assert app['tasksRunning'] == 1, "The number of running tasks is {}, but 1 was expected".format(app["tasksRunning"]) # Abdicate the leader with backup original_leader = marathon_leader_ip() params = '?backup={}'.format(backup_url1) common.abdicate_marathon_leader(params) wait_for_service_endpoint(marathon_service_name, timedelta(minutes=5).total_seconds(), path="ping") # wait until leader changed common.assert_marathon_leadership_changed(original_leader) @retrying.retry(wait_fixed=1000, stop_max_attempt_number=30, retry_on_exception=common.ignore_exception) def check_app_existence(expected_instances): try: app = client.get_app(app_id) except Exception as e: if expected_instances != 0: raise e else: if expected_instances == 0: assert False, "The application resurrected" else: app['tasksRunning'] == expected_instances, \ "The number of running tasks is {}, but {} was expected".format( app["tasksRunning"], expected_instances) # check if app definition is still there and one instance is still running after new leader was elected check_app_existence(1) # then remove client.remove_app(app_id) deployment_wait(service_id=app_id) check_app_existence(0) # Do a second backup. Before MARATHON-7525 we had the problem, that doing a backup after an app was deleted # leads to the state that marathon was not able to re-start, because the second backup failed constantly. # Abdicate the leader with backup original_leader = marathon_leader_ip() print('leader: {}'.format(original_leader)) params = '?backup={}'.format(backup_url2) print('DELETE /v2/leader{}'.format(params)) common.abdicate_marathon_leader(params) wait_for_service_endpoint(marathon_service_name, timedelta(minutes=5).total_seconds(), path="ping") # wait until leader changed # if leader changed, this means that marathon was able to start again, which is great :-). common.assert_marathon_leadership_changed(original_leader) # check if app definition is still not there and no instance is running after new leader was elected check_app_existence(0)