Exemple #1
0
def test_capture():
    cmd = [
        'git', '--no-pager', 'show', '-s', "--format=%an",
        '1548a62d2db12b9b2afcd996aac015d3c373bae8'
    ]
    author_name = subprocess.check_output(cmd)
    assert author_name == b'Gabor Szabo\n'

    code, auth_name = capture2(cmd)
    assert code == 0
    assert auth_name == 'Gabor Szabo\n'

    code, output = capture2([sys.executable, "test/exceptional.py"])
    assert code == 1
    assert output == "Parameters are 'raise', 'good', N\n"

    code, output = capture2([sys.executable, "test/exceptional.py", 'over'])
    assert code == 1
    print(output)
    assert 'Traceback (most recent call last):' in output
    assert 'IndexError: list index out of range' in output

    code, output = capture2([sys.executable, "test/exceptional.py", "42"])
    assert code == 42
    assert output == ''

    code, output = capture2([sys.executable, "test/exceptional.py", "raise"])
    assert code == 1
    assert 'Traceback (most recent call last):' in output
    assert 'Exception: Something bad happened.' in output
Exemple #2
0
    def test_run_tests(self, tmpdir):
        temp_dir = str(tmpdir)
        print(temp_dir)
        self.setup_repos(temp_dir,
                         {'steps': ["cli: python repo0/selftest.py"]}, 1)

        # update the repository
        with cwd(self.client[0]):
            with open('selftest.py', 'w') as fh:
                fh.write("exit(13)\n")
            _system("git add .")
            _system("git commit -m 'first' --author 'Foo Bar <*****@*****.**>'")
            _system("git push")
        code, out = capture2(
            "python check.py --server {} --config {} {}".format(
                self.server_file, self.config_file, debug),
            shell=True)
        print(out)
        assert code == 1, "One test failure repored"
        #assert out == "" # TODO: this will fail if --debug is on, but also becaues there is some output from the git commands.
        assert os.listdir(os.path.join(self.repositories,
                                       'repo0/')) == ['selftest.py', '.git']
        assert os.path.exists(os.path.join(self.workdir, '1', 'repo0/'))
        assert os.listdir(os.path.join(self.workdir, '1',
                                       'repo0/')) == ['selftest.py', '.git']

        with cwd(self.client[0]):
            with open('selftest.py', 'w') as fh:
                fh.write("exit(0)\n")
            _system("git add .")
            _system("git commit -m 'second' --author 'Foo Bar <*****@*****.**>'")
            _system("git push")
        code, out = capture2(
            "python check.py --server {} --config {} {}".format(
                self.server_file, self.config_file, debug),
            shell=True)
        print(out)
        assert code == 0, "test sucess repored: "
        #assert out == "" # TODO: this will fail if --debug is on, but also becaues there is some output from the git commands.
        assert os.listdir(os.path.join(self.repositories,
                                       'repo0/')) == ['selftest.py', '.git']
        assert os.path.exists(os.path.join(self.workdir, '2', 'repo0/'))
        assert os.listdir(os.path.join(self.workdir, '2',
                                       'repo0/')) == ['selftest.py', '.git']
Exemple #3
0
def _system(cmd):
    logger = logging.getLogger(__name__)

    if type(cmd).__name__ == 'list':
        cmd_list = cmd
        cmd_str = ' '.join(cmd)
    elif type(cmd).__name__ == 'str':
        cmd_list = shlex.split(cmd)
        cmd_str = cmd
    else:
        raise Exception("Invalid paramerer type: " + type(cmd).__name__)

    logger.debug(cmd_str)
    code, out = capture2(cmd_list)
    logger.debug("Exit code for '{}' is '{}'. Output is {}.".format(
        cmd_str, code, out))
    return code, out
Exemple #4
0
    def test_repo(self, tmpdir):
        temp_dir = str(tmpdir)
        print(temp_dir)
        self.setup_repos(temp_dir, {}, 1)

        _system("python check.py --server {} --config {} {}".format(
            self.server_file, self.config_file, debug))
        assert os.path.exists(os.path.join(self.repositories, 'repo0'))
        assert os.listdir(os.path.join(self.repositories,
                                       'repo0/')) == ['.git']
        assert not os.path.exists(os.path.join(self.workdir, '1', 'repo0/'))

        # update the repository
        with cwd(self.client[0]):
            with open('README.txt', 'w') as fh:
                fh.write("first line\n")
            _system("git add .")
            _system("git commit -m 'first' --author 'Foo Bar <*****@*****.**>'")
            _system("git push")
        _system("python check.py --server {} --config {} {}".format(
            self.server_file, self.config_file, debug))
        assert os.listdir(os.path.join(self.repositories,
                                       'repo0/')) == ['README.txt', '.git']
        assert os.path.exists(os.path.join(self.workdir, '1', 'repo0/'))
        assert os.listdir(os.path.join(self.workdir, '1',
                                       'repo0/')) == ['README.txt', '.git']
        # check if the sha change was noticed git rev-parse HEAD

        # create a branch, see if the new branch is noticed
        with cwd(self.client[0]):
            with open('TODO', 'w') as fh:
                fh.write("Some TODO text\n")
            _system("git checkout -b todo")
            _system("git add .")
            _system(
                "git commit -m 'add test' --author 'Foo Bar <*****@*****.**>'")
            _system("git push --set-upstream origin todo")

            _system("git checkout master")
            with open('MASTER', 'w') as fh:
                fh.write("Some MASTER text\n")
            _system("git add .")
            _system(
                "git commit -m 'add master' --author 'Foo Bar <*****@*****.**>'")
            _system("git push")
        code, out = capture2(
            "python check.py --server {} --config {} {}".format(
                self.server_file, self.config_file, debug),
            shell=True)
        print(out)
        assert code == 0

        # first workdir did not change
        assert os.path.exists(os.path.join(self.workdir, '1', 'repo0/'))
        assert os.listdir(os.path.join(self.workdir, '1',
                                       'repo0/')) == ['README.txt', '.git']

        # second workdir has the new file as well
        assert os.path.exists(os.path.join(self.workdir, '2', 'repo0/'))
        assert os.listdir(os.path.join(
            self.workdir, '2', 'repo0/')) == ['MASTER', 'README.txt', '.git']

        # second workdir has the new file as well
        assert os.path.exists(os.path.join(self.workdir, '3', 'repo0/'))
        assert os.listdir(os.path.join(
            self.workdir, '3', 'repo0/')) == ['TODO', 'README.txt', '.git']
Exemple #5
0
    def test_run_matrix(self, tmpdir):
        temp_dir = str(tmpdir)
        print(temp_dir)
        self.setup_repos(
            temp_dir, {
                'matrix': [
                    {
                        'agent': 'master',
                        'exe': 'python repo0/code.py Foo',
                    },
                    {
                        'agent': 'master',
                        'exe': 'python repo0/code.py',
                    },
                    {
                        'agent': 'master',
                        'exe': 'python repo0/code.py Bar',
                    },
                    {
                        'agent': 'master',
                        'exe': 'python repo0/code.py crash',
                    },
                ]
            }, 1)

        # update the repository
        with cwd(self.client[0]):
            with open('code.py', 'w') as fh:
                fh.write("""
import sys
if len(sys.argv) < 2:
    exit("Missing parameter")
print("hello " + sys.argv[1])
if sys.argv[1] == "crash":
    v = 0
    print(42/v)
""")

            _system("git add .")
            _system("git commit -m 'first' --author 'Foo Bar <*****@*****.**>'")
            _system("git push")
        code, out = capture2(
            "python check.py --server {} --config {} {}".format(
                self.server_file, self.config_file, debug),
            shell=True)
        print(out)
        assert code == 1, "some tests failed in the matrix"
        #assert out == "" # TODO: this will fail if --debug is on, but also becaues there is some output from the git commands.
        assert os.listdir(os.path.join(self.repositories,
                                       'repo0/')) == ['code.py', '.git']
        assert os.path.exists(os.path.join(self.workdir, '1/1', 'repo0/'))
        assert os.listdir(os.path.join(self.workdir, '1/1',
                                       'repo0/')) == ['code.py', '.git']
        assert os.path.exists(os.path.join(self.workdir, '1/2', 'repo0/'))
        assert os.listdir(os.path.join(self.workdir, '1/2',
                                       'repo0/')) == ['code.py', '.git']
        results_file = os.path.join(self.db, '1.json')
        assert os.path.exists(results_file)
        with open(results_file) as fh:
            results = json.load(fh)
        last = results['matrix'].pop('4')
        assert results == {
            'status': 'failure',
            'matrix': {
                '1': {
                    'exit': 0,
                    'agent': 'master',
                    'exe': 'python repo0/code.py Foo',
                    'out': 'hello Foo\n'
                },
                '2': {
                    'exit': 1,
                    'agent': 'master',
                    'exe': 'python repo0/code.py',
                    'out': 'Missing parameter\n'
                },
                '3': {
                    'exit': 0,
                    'agent': 'master',
                    'exe': 'python repo0/code.py Bar',
                    'out': 'hello Bar\n'
                },
            },
        }
        out_of_last = last.pop('out')
        assert last == {
            'exit': 1,
            'agent': 'master',
            'exe': 'python repo0/code.py crash',
        }
        # Then end of the error message has changed so we don't test the specifics
        # In Python 2: integer division or modulo by zero
        # In Python 3: division by zero
        assert 'hello crash\nTraceback (most recent call last):\n  File "repo0/code.py", line 8, in <module>\n    print(42/v)\nZeroDivisionError:' in out_of_last