Beispiel #1
0
    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)
Beispiel #2
0
    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)
Beispiel #3
0
    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)
Beispiel #4
0
    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)
Beispiel #5
0
    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)
Beispiel #6
0
    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)
Beispiel #7
0
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,
    )
Beispiel #8
0
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,
    )
Beispiel #9
0
    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")
Beispiel #10
0
    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")
Beispiel #11
0
    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)
Beispiel #12
0
    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)
Beispiel #13
0
    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)
Beispiel #14
0
    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)
Beispiel #15
0
    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%')
Beispiel #16
0
    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%')
Beispiel #17
0
    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')
Beispiel #18
0
    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')
Beispiel #19
0
 def tearDownClass(cls):
     execute('rm -r "{0}"'.format(cls.TEST_DIR))
Beispiel #20
0
 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)
Beispiel #21
0
 def do_run(self, line):
     try:
         execute(line)
     except (BaseExecuteCommandError, OSError) as e:
         print(e)
Beispiel #22
0
 def test_puling_image(self):
     execute('docker rmi busybox')
     self.make_container(image='busybox')
Beispiel #23
0
 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)
Beispiel #24
0
 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)
Beispiel #25
0
 def test_timeout(self):
     command = 'python -c "import time; time.sleep(0.5);"'
     with self.assertRaises(ExecuteCommandTimeoutExpired):
         execute(command, timeout=0.2)
Beispiel #26
0
 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)
Beispiel #27
0
 def test_timeout(self):
     command = 'python -c "import time; time.sleep(0.5);"'
     with self.assertRaises(ExecuteCommandTimeoutExpired):
         execute(command, timeout=0.2)
Beispiel #28
0
 def test_puling_image(self):
     execute('docker rmi busybox')
     self.make_container(image='busybox')
Beispiel #29
0
 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)
Beispiel #30
0
 def do_run(self, line):
     try:
         execute(line)
     except (BaseExecuteCommandError, OSError) as e:
         print(e)