Esempio n. 1
0
    def __init__(self, test, commits=None, workspace=None, env=None):
        self._test = test
        self._console = None
        if commits is None:
            commits = RepositoryTestState.create_default()
        self._commits = commits
        self.executor = mock.create_autospec(Executor, spec_set=True, instance=True)
        self.executor.check_output.side_effect = self._check_output
        self.executor.read_file.side_effect = self._read_file
        self.executor.write_file.side_effect = self._write_file
        self.reset_console_output()

        if env is None:
            env = dict()
        if workspace:
            env['WORKSPACE'] = workspace
        else:
            env['WORKSPACE'] = '/ws'
        if 'GROMACS_REFSPEC' not in env:
            env['GROMACS_REFSPEC'] = commits.gromacs.refspec
        if 'RELENG_REFSPEC' not in env:
            env['RELENG_REFSPEC'] = commits.releng.refspec
        if 'REGRESSIONTESTS_REFSPEC' not in env \
                and commits.has_project(Project.REGRESSIONTESTS):
            env['REGRESSIONTESTS_REFSPEC'] = commits.regressiontests.refspec

        self.factory = ContextFactory(env=env)
        self.factory.init_executor(instance=self.executor)
        if workspace:
            self.factory.init_workspace_and_projects()
            self.executor.reset_mock()
            self.reset_console_output()
        self._input_files = dict()
        self._output_files = dict()
Esempio n. 2
0
    def __init__(self, test, workspace=None, env=dict()):
        self._test = test
        self._console = None
        self.executor = mock.create_autospec(Executor, spec_set=True, instance=True)
        self.executor.check_output.side_effect = self._check_output
        self.executor.read_file.side_effect = self._read_file
        self.executor.write_file.side_effect = self._write_file
        self.reset_console_output()

        if workspace:
            env['WORKSPACE'] = workspace
            if 'CHECKOUT_PROJECT' not in env:
                env['CHECKOUT_PROJECT'] = Project.GROMACS
            if 'CHECKOUT_REFSPEC' not in env:
                env['CHECKOUT_REFSPEC'] = 'HEAD'
            if 'GROMACS_REFSPEC' not in env:
                env['GROMACS_REFSPEC'] = 'HEAD'
            if 'RELENG_REFSPEC' not in env:
                env['RELENG_REFSPEC'] = 'HEAD'
            if 'REGRESSIONTESTS_REFSPEC' not in env:
                env['REGRESSIONTESTS_REFSPEC'] = 'HEAD'
        self.factory = ContextFactory(env=env)
        self.factory.init_executor(instance=self.executor)
        if workspace:
            self.factory.init_workspace()
            self.executor.reset_mock()
            self.reset_console_output()
        self._input_files = dict()
        self._output_files = dict()
Esempio n. 3
0
    def __init__(self, test, commits=None, workspace=None, env=None):
        self._test = test
        self._console = None
        if commits is None:
            commits = RepositoryTestState.create_default()
        self._commits = commits
        self.executor = mock.create_autospec(Executor,
                                             spec_set=True,
                                             instance=True)
        self.executor.check_output.side_effect = self._check_output
        self.executor.read_file.side_effect = self._read_file
        self.executor.write_file.side_effect = self._write_file
        self.reset_console_output()

        if env is None:
            env = dict()
        if workspace:
            env['WORKSPACE'] = workspace
        else:
            env['WORKSPACE'] = '/ws'
        if 'GROMACS_REFSPEC' not in env:
            env['GROMACS_REFSPEC'] = commits.gromacs.refspec
        if 'RELENG_REFSPEC' not in env:
            env['RELENG_REFSPEC'] = commits.releng.refspec
        if 'REGRESSIONTESTS_REFSPEC' not in env \
                and commits.has_project(Project.REGRESSIONTESTS):
            env['REGRESSIONTESTS_REFSPEC'] = commits.regressiontests.refspec

        self.factory = ContextFactory(env=env)
        self.factory.init_executor(instance=self.executor)
        if workspace:
            self.factory.init_workspace_and_projects()
            self.executor.reset_mock()
            self.reset_console_output()
        self._input_files = dict()
        self._output_files = dict()
Esempio n. 4
0
class TestHelper(object):
    def __init__(self, test, commits=None, workspace=None, env=None):
        self._test = test
        self._console = None
        if commits is None:
            commits = RepositoryTestState.create_default()
        self._commits = commits
        self.executor = mock.create_autospec(Executor, spec_set=True, instance=True)
        self.executor.check_output.side_effect = self._check_output
        self.executor.read_file.side_effect = self._read_file
        self.executor.write_file.side_effect = self._write_file
        self.reset_console_output()

        if env is None:
            env = dict()
        if workspace:
            env['WORKSPACE'] = workspace
        else:
            env['WORKSPACE'] = '/ws'
        if 'GROMACS_REFSPEC' not in env:
            env['GROMACS_REFSPEC'] = commits.gromacs.refspec
        if 'RELENG_REFSPEC' not in env:
            env['RELENG_REFSPEC'] = commits.releng.refspec
        if 'REGRESSIONTESTS_REFSPEC' not in env \
                and commits.has_project(Project.REGRESSIONTESTS):
            env['REGRESSIONTESTS_REFSPEC'] = commits.regressiontests.refspec

        self.factory = ContextFactory(env=env)
        self.factory.init_executor(instance=self.executor)
        if workspace:
            self.factory.init_workspace_and_projects()
            self.executor.reset_mock()
            self.reset_console_output()
        self._input_files = dict()
        self._output_files = dict()

    def reset_console_output(self):
        self._console = StringIO()
        type(self.executor).console = mock.PropertyMock(return_value=self._console)

    def _check_output(self, cmd, **kwargs):
        if cmd[:4] == ['git', 'rev-list', '-n1', '--format=oneline']:
            project = Project.parse(os.path.basename(kwargs['cwd']))
            if cmd[4] == 'HEAD':
                commit = self._commits.get_head(project)
            else:
                commit = self._commits.find_commit(project, sha1=cmd[4])
            if not commit:
                raise CommandError('commit not found: ' + cmd[4])
            return '{0} {1}\n'.format(commit.sha1, commit.title)
        elif cmd[:2] == ['git', 'ls-remote']:
            git_url = urlparse.urlsplit(cmd[2])
            project = Project.parse(os.path.splitext(git_url.path[1:])[0])
            commit = self._commits.find_commit(project, refspec=cmd[3])
            return '{0} {1}\n'.format(commit.sha1, commit.refspec)
        elif cmd[0] == 'ssh' and cmd[4:6] == ['gerrit', 'query']:
            query = cmd[9]
            if query.startswith('commit:'):
                commit = self._commits.find_commit(sha1=query[7:])
            else:
                commit = self._commits.find_commit(change_number=int(query))
            data = {
                    'project': commit.project,
                    'branch': commit.branch,
                    'number': str(commit.change_number),
                    'subject': commit.title,
                    'url': 'URL',
                    'open': True,
                    'currentPatchSet': {
                            'number': str(commit.patch_number),
                            'revision': commit.sha1,
                            'ref': commit.refspec
                        }
                }
            return json.dumps(data) + '\nstats'
        return None

    def _read_file(self, path):
        if path not in self._input_files:
            raise IOError(path + ': not part of test')
        return self._input_files[path]

    def _write_file(self, path, contents):
        self._output_files[path] = contents

    def add_input_file(self, path, contents):
        lines = textwrap.dedent(contents).splitlines(True)
        self._input_files[path] = lines

    def add_input_json_file(self, path, contents):
        lines = json.dumps(contents).splitlines(True)
        self._input_files[path] = lines

    def assertConsoleOutput(self, expected):
        text = textwrap.dedent(expected)
        self._test.assertEqual(text, self._console.getvalue())

    def assertOutputFile(self, path, expected):
        if path not in self._output_files:
            self._test.fail('output file not produced: ' + path)
        text = textwrap.dedent(expected)
        self._test.assertEqual(text, self._output_files[path])

    def assertOutputJsonFile(self, path, expected):
        if path not in self._output_files:
            self._test.fail('output file not produced: ' + path)
        contents = json.loads(self._output_files[path])
        self._test.assertEqual(contents, expected)

    def assertCommandInvoked(self, cmd):
        self.executor.check_call.assert_any_call(cmd, cwd=mock.ANY, env=mock.ANY)
Esempio n. 5
0
class TestHelper(object):
    def __init__(self, test, commits=None, workspace=None, env=None):
        self._test = test
        self._console = None
        if commits is None:
            commits = RepositoryTestState.create_default()
        self._commits = commits
        self.executor = mock.create_autospec(Executor,
                                             spec_set=True,
                                             instance=True)
        self.executor.check_output.side_effect = self._check_output
        self.executor.read_file.side_effect = self._read_file
        self.executor.write_file.side_effect = self._write_file
        self.reset_console_output()

        if env is None:
            env = dict()
        if workspace:
            env['WORKSPACE'] = workspace
        else:
            env['WORKSPACE'] = '/ws'
        if 'GROMACS_REFSPEC' not in env:
            env['GROMACS_REFSPEC'] = commits.gromacs.refspec
        if 'RELENG_REFSPEC' not in env:
            env['RELENG_REFSPEC'] = commits.releng.refspec
        if 'REGRESSIONTESTS_REFSPEC' not in env \
                and commits.has_project(Project.REGRESSIONTESTS):
            env['REGRESSIONTESTS_REFSPEC'] = commits.regressiontests.refspec

        self.factory = ContextFactory(env=env)
        self.factory.init_executor(instance=self.executor)
        if workspace:
            self.factory.init_workspace_and_projects()
            self.executor.reset_mock()
            self.reset_console_output()
        self._input_files = dict()
        self._output_files = dict()

    def reset_console_output(self):
        self._console = StringIO()
        type(self.executor).console = mock.PropertyMock(
            return_value=self._console)

    def _check_output(self, cmd, **kwargs):
        if cmd[:4] == ['git', 'rev-list', '-n1', '--format=oneline']:
            project = Project.parse(os.path.basename(kwargs['cwd']))
            if cmd[4] == 'HEAD':
                commit = self._commits.get_head(project)
            else:
                commit = self._commits.find_commit(project, sha1=cmd[4])
            if not commit:
                raise CommandError('commit not found: ' + cmd[4])
            return '{0} {1}\n'.format(commit.sha1, commit.title)
        elif cmd[:2] == ['git', 'ls-remote']:
            git_url = urlparse.urlsplit(cmd[2])
            project = Project.parse(os.path.splitext(git_url.path[1:])[0])
            commit = self._commits.find_commit(project, refspec=cmd[3])
            return '{0} {1}\n'.format(commit.sha1, commit.refspec)
        elif cmd[0] == 'ssh' and cmd[4:6] == ['gerrit', 'query']:
            query = cmd[9]
            if query.startswith('commit:'):
                commit = self._commits.find_commit(sha1=query[7:])
            else:
                commit = self._commits.find_commit(change_number=int(query))
            data = {
                'project': commit.project,
                'branch': commit.branch,
                'number': str(commit.change_number),
                'subject': commit.title,
                'url': 'URL',
                'open': True,
                'currentPatchSet': {
                    'number': str(commit.patch_number),
                    'revision': commit.sha1,
                    'ref': commit.refspec
                }
            }
            return json.dumps(data) + '\nstats'
        return None

    def _read_file(self, path):
        if path not in self._input_files:
            raise IOError(path + ': not part of test')
        return self._input_files[path]

    def _write_file(self, path, contents):
        self._output_files[path] = contents

    def add_input_file(self, path, contents):
        lines = textwrap.dedent(contents).splitlines(True)
        self._input_files[path] = lines

    def add_input_json_file(self, path, contents):
        lines = json.dumps(contents).splitlines(True)
        self._input_files[path] = lines

    def assertConsoleOutput(self, expected):
        text = textwrap.dedent(expected)
        self._test.assertEqual(text, self._console.getvalue())

    def assertOutputFile(self, path, expected):
        if path not in self._output_files:
            self._test.fail('output file not produced: ' + path)
        text = textwrap.dedent(expected)
        self._test.assertEqual(text, self._output_files[path])

    def assertOutputJsonFile(self, path, expected):
        if path not in self._output_files:
            self._test.fail('output file not produced: ' + path)
        contents = json.loads(self._output_files[path])
        self._test.assertEqual(contents, expected)

    def assertCommandInvoked(self, cmd):
        self.executor.check_call.assert_any_call(cmd,
                                                 cwd=mock.ANY,
                                                 env=mock.ANY)
Esempio n. 6
0
class TestHelper(object):
    def __init__(self, test, workspace=None, env=dict()):
        self._test = test
        self._console = None
        self.executor = mock.create_autospec(Executor, spec_set=True, instance=True)
        self.executor.check_output.side_effect = self._check_output
        self.executor.read_file.side_effect = self._read_file
        self.executor.write_file.side_effect = self._write_file
        self.reset_console_output()

        if workspace:
            env['WORKSPACE'] = workspace
            if 'CHECKOUT_PROJECT' not in env:
                env['CHECKOUT_PROJECT'] = Project.GROMACS
            if 'CHECKOUT_REFSPEC' not in env:
                env['CHECKOUT_REFSPEC'] = 'HEAD'
            if 'GROMACS_REFSPEC' not in env:
                env['GROMACS_REFSPEC'] = 'HEAD'
            if 'RELENG_REFSPEC' not in env:
                env['RELENG_REFSPEC'] = 'HEAD'
            if 'REGRESSIONTESTS_REFSPEC' not in env:
                env['REGRESSIONTESTS_REFSPEC'] = 'HEAD'
        self.factory = ContextFactory(env=env)
        self.factory.init_executor(instance=self.executor)
        if workspace:
            self.factory.init_workspace()
            self.executor.reset_mock()
            self.reset_console_output()
        self._input_files = dict()
        self._output_files = dict()

    def reset_console_output(self):
        self._console = StringIO()
        type(self.executor).console = mock.PropertyMock(return_value=self._console)

    def _check_output(self, cmd, **kwargs):
        if cmd == ['git', 'rev-list', '-n1', '--format=oneline', 'HEAD']:
            sha1 = '1234567890abcdef0123456789abcdef01234567'
            title = 'Mock title'
            return '{0} {1}\n'.format(sha1, title)
        elif cmd[:2] == ['git', 'ls-remote']:
            sha1 = '1234567890abcdef0123456789abcdef01234567'
            refspec = cmd[3]
            return '{0} {1}\n'.format(sha1, refspec)
        elif cmd[0] == 'ssh' and cmd[4:6] == ['gerrit', 'query']:
            data = {
                    'project': 'regressiontests',
                    'branch': 'master',
                    'number': '1234',
                    'url': 'URL',
                    'open': True,
                    'currentPatchSet': {
                            'number': '5',
                            'revision': '1234567890abcdef0123456789abcdef01234567',
                            'ref': 'refs/changes/34/1234/5'
                        }
                }
            return json.dumps(data) + '\nstats'
        return None

    def _read_file(self, path):
        if path not in self._input_files:
            raise IOError(path + ': not part of test')
        return self._input_files[path]

    def _write_file(self, path, contents):
        self._output_files[path] = contents

    def add_input_file(self, path, contents):
        lines = textwrap.dedent(contents).splitlines(True)
        self._input_files[path] = lines

    def add_input_json_file(self, path, contents):
        lines = json.dumps(contents).splitlines(True)
        self._input_files[path] = lines

    def assertConsoleOutput(self, expected):
        text = textwrap.dedent(expected)
        self._test.assertEqual(text, self._console.getvalue())

    def assertOutputFile(self, path, expected):
        if path not in self._output_files:
            self._test.fail('output file not produced: ' + path)
        text = textwrap.dedent(expected)
        self._test.assertEqual(text, self._output_files[path])

    def assertOutputJsonFile(self, path, expected):
        if path not in self._output_files:
            self._test.fail('output file not produced: ' + path)
        contents = json.loads(self._output_files[path])
        self._test.assertEqual(contents, expected)

    def assertCommandInvoked(self, cmd):
        self.executor.check_call.assert_any_call(cmd, cwd=mock.ANY, env=mock.ANY)