def test_cd_without_shell(self): ECHO_CWD_COMMAND = 'python -c "import os; print(os.getcwd())"' tmp = tempfile.gettempdir() COMMAND = 'cd "{0}" && {1}'.format(tmp, ECHO_CWD_COMMAND) with self.assertRaises(ExecuteCommandParseError): execute(COMMAND)
def test_execute_raise_non_zero_error_by_default(self): command = '''python -c "import sys; sys.exit(23);"''' with self.assertRaises(ExecuteCommandNonZeroExitStatus): z = execute(command) self.assertExecuteResult(z, 23, b'', None, None) z = execute(command, check_return_code_and_raise_error=False) self.assertExecuteResult(z, 23, b'', None, None)
def test_set_env_variable2(self): env_name = 'AKAWKFN' command = self.make_print_env_variable_command(env_name) z = execute(command) self.assertExecuteResult(z, 0, b'None', None, None) z = execute(command, env=self.make_python_env({env_name: "SEC!"})) self.assertExecuteResult(z, 0, b'SEC!', None, None)
def run(command, cwd, do_log=lambda x: log.debug(x.decode().strip())): log.debug("run '{0}'".format(command)) execute( command, cwd=cwd, stderr_to_stdout=True, do_on_read_stdout=do_log, check_return_code_and_raise_error=False, )
def test_exit_1_or_echo_1(self): EXIT_1_COMMAND = 'python -c "import sys; sys.exit(1)"' ECHO_1_COMMAND = 'python -c "print(1)"' COMMAND = "{0} || {1}".format(EXIT_1_COMMAND, ECHO_1_COMMAND) with self.assertRaises(ExecuteCommandParseError): execute(COMMAND) r = execute(COMMAND, shell=True) self.assertEqual(r.stdout_text.strip(), "1")
def test_set_env_variable(self): command = 'python -c "import sys, os; ' \ 'os.environ.get(\'SSECRET\') == \'SSEC!\' and ' \ 'sys.exit(24);"' z = execute(command) self.assertExecuteResult(z, 0, b'', None, None) command = 'python -c "import sys, os; ' \ 'os.environ.get(\'SSECRET\') == \'SSEC!\' or ' \ 'sys.exit(24);"' z = execute(command, env=self.make_python_env({'SSECRET': 'SSEC!'})) self.assertExecuteResult(z, 0, b'', None, None)
def test_cd(self): ECHO_CWD_COMMAND = 'python -c "import os; print(os.getcwd())"' tmp = tempfile.gettempdir() COMMAND = 'cd "{0}" && {1}'.format(tmp, ECHO_CWD_COMMAND) r = execute(COMMAND, shell=True) self.assertEqual(r.stdout_text.strip(), tmp)
def test_insecurity_execute_with_shell_true(self): command = '"{0}" -c "import sys; ' \ 'print(\\\"SSS! $SSS %SSS%\\\")"' \ .format(self.get_python_command_path()) z = execute(command, env=self.make_python_env({"SSS": "+"}), shell=True) self.assertEqual(z.returncode, 0) self.assertNotEqual(z.stdout, b'SSS! $SSS %SSS%')
def take_action(self, parsed_args): env = self.env context = get_git_context(parsed_args.git_url, self.log) data_path = env[PLATFORM_DATA_PATH_KEY] secure_repo_dir_name = context[REPO_DIR_NAME_KEY] dns_name = context[DNS_NAME_KEY] repo_local_path = os.path.join(data_path, secure_repo_dir_name) self.log.debug('init new repo {0}'.format(repo_local_path)) self.write_message_for_user("receive {0} project".format(dns_name)) execute( "git init --bare '{name}'".format(name=secure_repo_dir_name), cwd=data_path, stderr_to_stdout=True, do_on_read_stdout=lambda x: self.log.debug(x.decode()), ) self.log.debug('start git-receive-pack') try: subprocess.check_call( 'cd {0} && git-receive-pack {name}' .format(data_path, name=secure_repo_dir_name), shell=True ) except subprocess.CalledProcessError as e: self.log.error("git-receive-pack problem #{0}" .format(e.returncode)) return except OSError as e: self.log.exception(e) raise self.trigger_event('git-receive-pack', env, context) self.log.debug('finish git-receive-pack')
def take_action(self, parsed_args): env = self.env context = get_git_context(parsed_args.git_url, self.log) data_path = env[PLATFORM_DATA_PATH_KEY] secure_repo_dir_name = context[REPO_DIR_NAME_KEY] dns_name = context[DNS_NAME_KEY] repo_local_path = os.path.join(data_path, secure_repo_dir_name) self.log.debug('init new repo {0}'.format(repo_local_path)) self.write_message_for_user("receive {0} project".format(dns_name)) execute( "git init --bare '{name}'".format(name=secure_repo_dir_name), cwd=data_path, stderr_to_stdout=True, do_on_read_stdout=lambda x: self.log.debug(x.decode()), ) self.log.debug('start git-receive-pack') try: subprocess.check_call('cd {0} && git-receive-pack {name}'.format( data_path, name=secure_repo_dir_name), shell=True) except subprocess.CalledProcessError as e: self.log.error("git-receive-pack problem #{0}".format( e.returncode)) return except OSError as e: self.log.exception(e) raise self.trigger_event('git-receive-pack', env, context) self.log.debug('finish git-receive-pack')
def tearDownClass(cls): execute('rm -r "{0}"'.format(cls.TEST_DIR))
def setUpClass(cls): TEST_PROJECTS = ['tests/apps/python-simple'] cls.TEST_DIR = TEST_DIR = tempfile.mkdtemp(prefix='wflow-docker-') cls.TEST_PROJECTS = [os.path.basename(p) for p in TEST_PROJECTS] print('TEST_DIR = {0}; TEST_PROJECTS = {1}'.format( TEST_DIR, cls.TEST_PROJECTS)) for t_project in TEST_PROJECTS: name = os.path.basename(t_project) t_project = os.path.abspath(t_project) t_project_path = os.path.abspath(os.path.join(TEST_DIR, name)) t_project_path_source = os.path.join(t_project_path, 'SOURCE') execute('mkdir {path}'.format(path=t_project_path)) # execute('mkdir {path}'.format(path=t_project_path_source)) execute('cp -r "{0}" "{1}"'.format(t_project, t_project_path_source)) execute('git init --bare', cwd=t_project_path) execute('git init', cwd=t_project_path_source) execute('git add *', cwd=t_project_path_source) execute('git commit -am "init"', cwd=t_project_path_source, check_return_code_and_raise_error=False) execute('git push "{0}" master'.format(t_project_path), cwd=t_project_path_source)
def do_run(self, line): try: execute(line) except (BaseExecuteCommandError, OSError) as e: print(e)
def test_puling_image(self): execute('docker rmi busybox') self.make_container(image='busybox')
def test_security_execute_with_shell_false(self): command = '"{0}" -c "import sys; ' \ 'print(\\\"SSS! $SSS %SSS%\\\")"' \ .format(self.get_python_command_path()) z = execute(command) self.assertExecuteResult(z, 0, b'SSS! $SSS %SSS%', None, None)
def test_execute_result(self): command = '''python -c "print('hi!')"''' z = execute(command) self.assertExecuteResult(z, 0, b'hi!', None, None) self.assertEqual(z.cmd, command)
def test_timeout(self): command = 'python -c "import time; time.sleep(0.5);"' with self.assertRaises(ExecuteCommandTimeoutExpired): execute(command, timeout=0.2)