def test_restore_files(self, Command): class temp_glob: def __init__(self, path): self.path = path self.index = 0 self.paths = [ 'anaconda-enterprise-anaconda-platform.yml', 'first.yaml', 'second.yaml', 'third.yaml' ] def __iter__(self): return self def __next__(self): index = self.index if index > 3: raise StopIteration() self.index += 1 return self.paths[index] Command().side_effect = [ 'REPLACE ERROR: (NotFound)', 'File created', sh.ErrorReturnCode_1('kubectl', ''.encode('utf-8'), ''.encode('utf-8')), sh.ErrorReturnCode_1('kubectl', ''.encode('utf-8'), ''.encode('utf-8')), 'file replaced' ] test_class = models.Accord( self.setup_args_restore_default(override=True, no_config=True)) with mock.patch('accord.process.glob.glob', side_effect=temp_glob): process.restoring_files(test_class)
def test_backup_ingress_failure(self, Command): test_class = None with mock.patch('accord.models.Accord.setup_backup_directory'): with mock.patch('accord.models.Accord.remove_signal_restore_file'): with mock.patch('accord.models.Accord.test_sync_to_backup'): test_class = models.Accord( self.setup_args_backup_default()) test_class.backup_directory = '.' Command().side_effect = sh.ErrorReturnCode_1('kubectl', ''.encode('utf-8'), ''.encode('utf-8')) with mock.patch('accord.models.Accord.get_all_ingress') as ingress: ingress.return_value = ['test-master', ''] try: process.backup_ingress_definitions(test_class) assert False, 'An error was not generated when it should have' except exceptions.IngressError: pass if not os.path.exists('ingress'): assert False, 'Did not automatically create the directory' if os.path.exists('ingress/test-master.yaml'): assert False, 'Created the ingress backup when it should not have'
def test_restore_ingress(self, Command): class temp_glob: def __init__(self, path): self.path = path self.index = 0 self.paths = ['master_ingress.yaml', 'failure.yaml'] def __iter__(self): return self def __next__(self): index = self.index if index > 1: raise StopIteration() self.index += 1 return self.paths[index] Command().side_effect = [ 'configured ingress', sh.ErrorReturnCode_1('kubectl', ''.encode('utf-8'), ''.encode('utf-8')) ] test_class = models.Accord( self.setup_args_restore_default(override=True, no_config=True, ingress=True)) with mock.patch('accord.process.glob.glob', side_effect=temp_glob): process.restoring_ingress(test_class)
def test_backup_secrets_cm_failure_configmap(self, Command): test_class = None with mock.patch('accord.models.Accord.setup_backup_directory'): with mock.patch('accord.models.Accord.remove_signal_restore_file'): with mock.patch('accord.models.Accord.test_sync_to_backup'): test_class = models.Accord( self.setup_args_backup_default()) test_class.backup_directory = '.' test_class.secret_files = {} test_class.config_maps = {'default': ['test-cm']} Command().side_effect = sh.ErrorReturnCode_1('kubectl', ''.encode('utf-8'), ''.encode('utf-8')) with mock.patch('accord.models.Accord.get_all_secrets'): try: process.backup_secrets_config_maps(test_class) except exceptions.ConfigMapNotFound: pass except Exception: assert False, 'Exception should have been caught' if not os.path.exists('secrets'): assert False, 'Did not automatically create the directory' if not os.path.exists('secrets/test-cm.yaml'): assert False, 'Did not create the secret'
def test_authenticate_api(self, Command): test_class = models.Accord( self.setup_args_restore_default(override=True)) try: test_class.authenticate_api() except Exception: assert False, "Exception occurred"
def test_cleanup_sessions_none(self, Command, grep, awk): awk().side_effect = [ sh.ErrorReturnCode_1('grep', 'out'.encode('utf-8'), 'error'.encode('utf-8')) ] test_class = models.Accord( self.setup_args_restore_default(override=True)) process.cleanup_sessions_deployments(test_class)
def test_gravity_restore(self, Command): test_class = models.Accord( self.setup_args_restore_default(override=True)) try: test_class.gravity_backup_restore('restore') except Exception: assert False, "Exception occurred"
def test_launch_deployment(self, Command): test_class = models.Accord( self.setup_args_restore_default(override=True)) try: test_class.launch_deployment() except Exception: assert False, "Exception occurred"
def test_gravity_backup(self, Command): with mock.patch('accord.models.Accord.setup_backup_directory'): with mock.patch('accord.models.Accord.remove_signal_restore_file'): test_class = models.Accord(self.setup_args_backup_default()) try: test_class.gravity_backup_restore('backup') except Exception: assert False, "Exception occurred"
def test_cleanup_sessions_success(self, Command, grep, awk): mock_command = mock.Mock() mock_command.return_value = ['deployment_to_grab'] mock_grep = mock.Mock() mock_grep.return_value = mock_command.return_value awk.return_value = mock_grep.return_value test_class = models.Accord( self.setup_args_restore_default(override=True)) process.cleanup_sessions_deployments(test_class)
def test_scale_up_pod_success(self, Command, grep): grep().side_effect = [ 'kubectl', sh.ErrorReturnCode_1('grep', 'out'.encode('utf-8'), 'error'.encode('utf-8')), '' ] test_class = models.Accord( self.setup_args_restore_default(override=True)) process.scale_postgres_pod(test_class, 1)
def test_init_class_restore_exception_file(self): with mock.patch('accord.models.Accord.check_for_restore') as restore: restore.return_value = False try: models.Accord(self.setup_args_restore_default()) except exceptions.RestoreSignal: pass except Exception: assert False, 'Did not catch the proper exception'
def test_file_backup(self, pushd, tar, mv, cp, Command): test_class = None self.setup_temp_file('test_backup.sql') with mock.patch('accord.models.Accord.setup_backup_directory'): with mock.patch('accord.models.Accord.remove_signal_restore_file'): with mock.patch('accord.models.Accord.test_sync_to_backup'): test_class = models.Accord( self.setup_args_backup_default()) process.file_backup_restore(test_class, 'backup')
def test_scale_down_pod_success(self, Command, grep): Command().side_effect = ['', process_returns.RUNNING_PODS] grep().side_effect = [ 'NAME'.encode('utf-8'), sh.ErrorReturnCode_1('grep', 'out'.encode('utf-8'), 'error'.encode('utf-8')) ] test_class = models.Accord( self.setup_args_restore_default(override=True)) process.scale_postgres_pod(test_class, 0)
def test_scale_pod_invalid_count(self, Command): test_class = models.Accord( self.setup_args_restore_default(override=True)) try: process.scale_postgres_pod(test_class, 99) assert False, 'Exception should have been raised' except exceptions.InvalidReplicaCount: pass except Exception: assert False, 'Invalid exception thrown'
def test_file_restore(self, pushd, tar, mv, cp, Command): self.setup_temp_file('test_backup.sql') test_class = models.Accord( self.setup_args_restore_default(override=True)) test_class.storage_backup_name = 'test_backup.sql' process.file_backup_restore(test_class, 'restore') if os.path.isfile('test_backup.sql'): assert False, 'Did not cleanup the original file'
def test_init_class_restore_file_check(self, Command): self.setup_temp_restore_file('restore') test_args = self.setup_args_restore_default(override=True) test_class = models.Accord(test_args) # Change path of file test_class.signal_file = 'restore' check_return = test_class.check_for_restore() self.assertEqual(check_return, True)
def test_su_command_success(self, Command): test_class = models.Accord( self.setup_args_restore_default(override=True)) container = 'test_container' command = 'ls' try: with mock.patch('accord.models.subprocess.run'): test_class.run_su_command(container, command) except Exception: assert False, "Exception occurred"
def test_setup_backup_directory(self, Command): with mock.patch('accord.models.Accord.setup_backup_directory'): with mock.patch('accord.models.Accord.remove_signal_restore_file'): test_class = models.Accord(self.setup_args_backup_default()) test_class.backup_directory = 'anaconda_backup' test_class.setup_backup_directory() if not os.path.exists('anaconda_backup'): assert False, 'Backup directory was not created'
def test_su_command_exception(self, Command): test_class = models.Accord( self.setup_args_restore_default(override=True)) container = 'test_container' command = 'ls' mock_response = mock.Mock() mock_response.side_effect = subprocess.CalledProcessError(1, 'ls') with mock.patch('accord.models.subprocess.run', side_effect=mock_response): with mock.patch('accord.models.sys.exit'): test_class.run_su_command(container, command)
def test_sync_files(self, Command, chown): with mock.patch('accord.models.Accord.setup_backup_directory'): with mock.patch('accord.models.Accord.remove_signal_restore_file'): with mock.patch('accord.models.Accord.test_sync_to_backup'): test_class = models.Accord( self.setup_args_backup_default(sync_user='******', sync_node='1.2.3.4', sync=True)) with mock.patch('accord.process.Accord.run_su_command'): process.sync_files(test_class)
def test_restart_pods(self, Command, grep): Command().side_effect = ['', process_returns.RUNNING_PODS] grep().side_effect = [ 'kubectl', 'NAME'.encode('utf-8'), sh.ErrorReturnCode_1('grep', 'out'.encode('utf-8'), 'error'.encode('utf-8')) ] # grep().return_value = sh.ErrorReturnCode_1 test_class = models.Accord( self.setup_args_restore_default(override=True)) process.restart_pods(test_class)
def test_init_class_backup_sync_no_node(self): with mock.patch('accord.models.Accord.setup_backup_directory'): with mock.patch('accord.models.Accord.remove_signal_restore_file'): try: models.Accord( self.setup_args_backup_default(sync=True, sync_user='******')) except exceptions.MissingSyncNode: pass except Exception: assert False, 'Did not catch expected exception'
def test_cleanup_restore_file(self, Command): self.setup_temp_restore_file('restore') with mock.patch('accord.models.Accord.setup_backup_directory'): with mock.patch('accord.models.Accord.remove_signal_restore_file'): test_class = models.Accord(self.setup_args_backup_default()) test_class.signal_file = 'restore' test_class.remove_signal_restore_file() if os.path.isfile('restore'): assert False, 'restore file was not cleaned up'
def test_restore_database(self, Command, mv, chown): self.setup_temp_file('test_backup.sql') test_class = models.Accord( self.setup_args_restore_default(override=True)) test_class.postgres_system_repo_backup_path = 'test_backup.sql' with mock.patch('accord.process.os.remove'): with mock.patch('accord.models.Accord.run_command_on_container'): process.restore_postgres_database(test_class) if not os.path.isfile('test_backup.sql'): assert False, 'Did not cleanup the original file'
def test_add_restore_file(self, Command): with mock.patch('accord.models.Accord.setup_backup_directory'): with mock.patch('accord.models.Accord.remove_signal_restore_file'): test_class = models.Accord(self.setup_args_backup_default()) test_class.signal_file = 'restore' test_class.add_signal_for_restore() if os.path.isfile('restore'): pass else: assert False, 'Did not find restore file'
def test_init_class_backup_sync_user(self, Command): with mock.patch('accord.models.Accord.setup_backup_directory'): with mock.patch('accord.models.Accord.remove_signal_restore_file'): with mock.patch('accord.models.Accord.test_sync_to_backup'): test = models.Accord( self.setup_args_backup_default(sync=True, sync_user='******', sync_node='1.2.3.4')) self.assertEqual(test.sync_files, True) self.assertEqual(test.sync_user, 'billdo') self.assertEqual(test.sync_node, '1.2.3.4')
def test_extract_tar_archive_success(self, Command): self.setup_valid_tar_archive() os.remove('testing_tar/test.txt') test_class = models.Accord( self.setup_args_restore_default( override=True, restore_file='testing_tar/test.tar.gz', directory='testing_tar')) test_class.extract_tar_archive('.') if os.path.isfile('testing_tar/test.txt'): pass else: assert False, 'Did not extract the archive as expected'
def test_extract_tar_archive_failure(self, Command): self.setup_temp_restore_file('test.tar.gz') test_class = models.Accord( self.setup_args_restore_default(override=True, restore_file='test.tar.gz', directory='.')) try: test_class.extract_tar_archive('.') assert False, 'Exception should have occured' except exceptions.NotValidTarfile: pass except Exception: assert False, 'Exception was not caught'
def test_container_command_success_return(self, Command): test_class = models.Accord( self.setup_args_restore_default(override=True)) container = 'test_container' command = 'ls' try: with mock.patch('accord.models.subprocess.run') as r: r.return_value.stdout = 'Success' results = test_class.run_command_on_container( container, command, True) except Exception: assert False, "Exception occurred" self.assertEqual(results, 'Success', 'Did not receive expected value')