def provision(self, args: Dict[str, str]={}): env = {'DEPLOY_OSTREE_%s' % key: value for key, value in args.items()} run( [self.provisioner_path, self.path()], check=True, env=env )
def test_should_raise_exception_if_check_is_true_and_exitcode_is_nonzero(self, mock_run: mock.Mock): args = ['mount', '-o', 'bind', '/', '/subdir'] mock_run.return_value = subprocess.CompletedProcess([], -1) with self.assertRaises(ProcessError) as cm: run(args, check=True) self.assertEqual(str(cm.exception), "'mount -o bind / /subdir' failed with status -1")
def test_should_not_raise_if_check_is_true_and_exitcode_is_zero(self, mock_run: mock.Mock): args = ['mount'] mock_run.return_value = subprocess.CompletedProcess([], 0) result = run(args, check=True) self.assertEqual(0, result.exitcode)
def test_result_should_contain_process_args(self, mock_run: mock.Mock): args = ['mount', '-o', 'bind', '/', '/subdir'] mock_run.return_value = subprocess.CompletedProcess([], 0) result = run(args) self.assertEqual(result.args, args) self.assertEqual(result.args_string, 'mount -o bind / /subdir')
def test_should_extend_os_environ_with_given_environment(self, mock_run: mock.Mock): args = ['/usr/bin/python3', '-m', 'http.server'] run( args, env={'extra1': 'value1', 'extra2': 'value2', 'USER': '******'}, ) mock_run.assert_called_once_with( args, stdout=None, stderr=None, env={ 'HOME': '/home/user', 'USER': '******', 'extra1': 'value1', 'extra2': 'value2', })
def ostree(extra_args: List[str]) -> ProcessResult: try: return run( ['ostree'] + extra_args, capture_output=True, check=True) except ProcessError as exc: print(exc.process_result.stderr) raise
def test_should_invoke_subprocess(self, mock_run: mock.Mock): args = ['ostree', 'remote', 'list'] mock_run.return_value = subprocess.CompletedProcess([], 32) result = run(args) mock_run.assert_called_once_with(args, stdout=None, stderr=None, env=None) self.assertEqual(32, result.exitcode) with self.assertRaises(ValueError): result.stdout with self.assertRaises(ValueError): result.stderr
def test_should_fail_and_clean_up_safely(self): result = deploy_ostree( [os.path.join(TESTS_DIR, 'failing-provisioner.json')], check=False, capture_output=True ) # we expect chpasswd to try to run, but not work self.assertIn('chpasswd', result.stderr) self.assertEqual(result.exitcode, 1) var_mount = os.path.join(self.get_deployment_dir(), 'var') mount = run(['mount'], capture_output=True) self.assertNotIn(' %s ' % var_mount, mount.stdout)
def test_should_capture_and_decode_process_output_using_provided_encoding(self, mock_run: mock.Mock): args = [sys.executable, '-mdeploy_ostree'] mock_run.return_value = subprocess.CompletedProcess( [], 0, 'stdout'.encode('utf-16'), 'stderr'.encode('utf-16')) result = run(args, capture_output=True, encoding='utf-16') mock_run.assert_called_once_with(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=None) self.assertEqual(0, result.exitcode) self.assertEqual('stdout', result.stdout) self.assertEqual('stderr', result.stderr)
def deploy_ostree(extra_args: List[str], check: bool=True, capture_output=False) -> ProcessResult: return run( [sys.executable, '-mdeploy_ostree'] + extra_args, capture_output=capture_output, check=check, )