Exemplo n.º 1
0
    def test_that_representation_works(self):
        version = ccextractor_version.version
        released = ccextractor_version.released
        commit = ccextractor_version.commit
        actual = CCExtractorVersion(version, released, commit)

        expected = '<Version {v}>'.format(v=actual.version)

        self.assertEqual(str(actual), expected)
Exemplo n.º 2
0
def run():
    from mod_home.models import CCExtractorVersion, GeneralData
    from mod_regression.models import Category, RegressionTest, InputType, OutputType, RegressionTestOutput
    from mod_sample.models import Sample
    from mod_upload.models import Upload
    from mod_auth.models import User
    from database import create_session

    db = create_session(sys.argv[1])

    entries = []
    categories = [
        Category('Broken', 'Samples that are broken'),
        Category('DVB', 'Samples that contain DVB subtitles'),
        Category('DVD', 'Samples that contain DVD subtitles'),
        Category('MP4', 'Samples that are stored in the MP4 format'),
        Category('General', 'General regression samples')
    ]
    entries.extend(categories)

    samples = [
        Sample('sample1', 'ts', 'sample1'),
        Sample('sample2', 'ts', 'sample2')
    ]
    entries.extend(samples)

    cc_version = CCExtractorVersion(
        '0.84', '2016-12-16T00:00:00Z',
        '77da2dc873cc25dbf606a3b04172aa9fb1370f32')
    entries.append(cc_version)

    regression_tests = [
        RegressionTest(1, '-autoprogram -out=ttxt -latin1', InputType.file,
                       OutputType.file, 3, 10),
        RegressionTest(2, '-autoprogram -out=ttxt -latin1 -ucla',
                       InputType.file, OutputType.file, 1, 10)
    ]
    entries.extend(regression_tests)

    gen_data = GeneralData('last_commit',
                           '71dffd6eb30c1f4b5cf800307de845072ce33262')
    entries.append(gen_data)

    regression_test_output = [
        RegressionTestOutput(1, "test1", "srt", "test1.srt"),
        RegressionTestOutput(2, "test2", "srt", "test2.srt")
    ]
    entries.extend(regression_test_output)

    for entry in entries:
        try:
            db.add(entry)
            db.commit()
        except IntegrityError:
            print("Entry already exists!", entry, flush=True)
            db.rollback()
Exemplo n.º 3
0
    def test_that_init_works_correctly(self):
        version = ccextractor_version.version
        released = ccextractor_version.released
        released_date = datetime.strptime(released,
                                          '%Y-%m-%dT%H:%M:%SZ').date()
        commit = ccextractor_version.commit
        actual = CCExtractorVersion(version, released, commit)

        self.assertEqual(actual.version, version)
        self.assertEqual(actual.released, released_date)
        self.assertEqual(actual.commit, commit)
Exemplo n.º 4
0
 def test_sample_fail(self, mock_media):
     version = CCExtractorVersion('1.3', '2015-02-27T19:35:32Z', 'abcdefgh')
     g.db.add(version)
     g.db.commit()
     import mod_sample.controllers
     reload(mod_sample.controllers)
     response = self.app.test_client().get('/sample/sample2')
     self.assertEqual(response.status_code, 200)
     self.assert_template_used('sample/sample_info.html')
     sample = Sample.query.filter(Sample.sha == 'sample2').first()
     mock_media.assert_called_with(sample)
     self.assertIn('Repository: Pass', str(response.data))
     self.assertIn('Last release: Fail', str(response.data))
Exemplo n.º 5
0
def run():
    from mod_home.models import CCExtractorVersion, GeneralData
    from mod_regression.models import Category, RegressionTest, InputType, \
        OutputType
    from mod_sample.models import Sample
    from mod_upload.models import Upload
    from mod_auth.models import User
    from database import create_session

    db = create_session(sys.argv[1])

    categories = [
        Category('Broken', 'Samples that are broken'),
        Category('DVB', 'Samples that contain DVB subtitles'),
        Category('DVD', 'Samples that contain DVD subtitles'),
        Category('MP4', 'Samples that are stored in the MP4 format'),
        Category('General', 'General regression samples')
    ]
    db.add_all(categories)
    db.commit()

    samples = [
        Sample('sample1', 'ts', 'sample1'),
        Sample('sample2', 'ts', 'sample2')
    ]
    db.add_all(samples)
    db.commit()

    cc_version = CCExtractorVersion(
        '0.84', '2016-12-16', '77da2dc873cc25dbf606a3b04172aa9fb1370f32'
    )
    db.add(cc_version)
    db.commit()

    regression_tests = [
        RegressionTest(
            1, '-autoprogram -out=ttxt -latin1', InputType.file,
            OutputType.file, 3, 10),
        RegressionTest(
            2, '-autoprogram -out=ttxt -latin1 -ucla', InputType.file,
            OutputType.file, 1, 10)
    ]
    db.add_all(regression_tests)
    db.commit()

    gen_data = GeneralData(
        'last_commit', '71dffd6eb30c1f4b5cf800307de845072ce33262')
    db.add(gen_data)
    db.commit()
 def test_webhook_release_deleted(self, mock_request):
     """
     Check webhook action "delete" removes the specified version.
     """
     with self.app.test_client() as c:
         release = CCExtractorVersion('2.1', '2018-05-30T20:18:44Z', 'abcdefgh')
         g.db.add(release)
         g.db.commit()
         # Delete full release with version with 2.1
         data = {'action': 'deleted',
                 'release': {'prerelease': False, 'published_at': '2018-05-30T20:18:44Z', 'tag_name': 'v2.1'}}
         last_commit = GeneralData.query.filter(GeneralData.key == 'last_commit').first()
         # abcdefgh is the new commit after previous version defined in base.py
         last_commit.value = 'abcdefgh'
         g.db.commit()
         response = c.post(
             '/start-ci', environ_overrides=WSGI_ENVIRONMENT,
             data=json.dumps(data), headers=self.generate_header(data, 'release'))
         last_release = CCExtractorVersion.query.order_by(CCExtractorVersion.released.desc()).first()
         self.assertNotEqual(last_release.version, '2.1')
 def test_webhook_release_edited(self, mock_request):
     """
     Check webhook action "edited" updates the specified version.
     """
     from datetime import datetime
     with self.app.test_client() as c:
         release = CCExtractorVersion('2.1', '2018-05-30T20:18:44Z', 'abcdefgh')
         g.db.add(release)
         g.db.commit()
         # Full Release with version with 2.1
         data = {'action': 'edited',
                 'release': {'prerelease': False, 'published_at': '2018-06-30T20:18:44Z', 'tag_name': 'v2.1'}}
         last_commit = GeneralData.query.filter(GeneralData.key == 'last_commit').first()
         # abcdefgh is the new commit after previous version defined in base.py
         last_commit.value = 'abcdefgh'
         g.db.commit()
         response = c.post(
             '/start-ci', environ_overrides=WSGI_ENVIRONMENT,
             data=json.dumps(data), headers=self.generate_header(data, 'release'))
         last_release = CCExtractorVersion.query.filter_by(version='2.1').first()
         self.assertEqual(last_release.released,
                          datetime.strptime('2018-06-30T20:18:44Z', '%Y-%m-%dT%H:%M:%SZ').date())
Exemplo n.º 8
0
    def setUp(self):
        self.app.preprocess_request()
        g.db = create_session(self.app.config['DATABASE_URI'],
                              drop_tables=True)
        # enable Foreign keys for unit tests
        g.db.execute('pragma foreign_keys=on')

        general_data = [
            GeneralData('last_commit',
                        "1978060bf7d2edd119736ba3ba88341f3bec3323"),
            GeneralData(f'fetch_commit_{TestPlatform.linux.value}',
                        "1978060bf7d2edd119736ba3ba88341f3bec3323"),
            GeneralData(f'fetch_commit_{TestPlatform.windows.value}',
                        "1978060bf7d2edd119736ba3ba88341f3bec3323")
        ]
        g.db.add_all(general_data)

        self.ccextractor_version = CCExtractorVersion(
            "1.2.3", "2013-02-27T19:35:32Z",
            "1978060bf7d2edd119736ba3ba88341f3bec3323")
        g.db.add(self.ccextractor_version)

        fork = Fork(
            f"https://github.com/{g.github['repository_owner']}/{g.github['repository']}.git"
        )
        g.db.add(fork)
        g.db.commit()

        dummy_user = User(signup_information['existing_user_name'],
                          signup_information['existing_user_role'],
                          signup_information['existing_user_email'],
                          signup_information['existing_user_pwd'])
        g.db.add(dummy_user)
        g.db.commit()

        test = [
            Test(TestPlatform.linux, TestType.pull_request, 1, "master",
                 "1978060bf7d2edd119736ba3ba88341f3bec3323", 1),
            Test(TestPlatform.linux, TestType.pull_request, 1, "master",
                 "abcdefgh", 1)
        ]
        g.db.add_all(test)
        g.db.commit()

        categories = [
            Category("Broken", "Samples that are broken"),
            Category("DVB", "Samples that contain DVB subtitles"),
            Category("DVD", "Samples that contain DVD subtitles"),
            Category("MP4", "Samples that are stored in the MP4 format"),
            Category("General", "General regression samples")
        ]
        g.db.add_all(categories)
        g.db.commit()

        samples = [
            Sample("sample1", "ts", "sample1"),
            Sample("sample2", "ts", "sample2")
        ]
        g.db.add_all(samples)
        g.db.commit()

        upload = [
            Upload(1, 1, 1, Platform.windows),
            Upload(1, 2, 1, Platform.linux)
        ]
        g.db.add_all(upload)
        g.db.commit()

        regression_tests = [
            RegressionTest(1, "-autoprogram -out=ttxt -latin1 -2",
                           InputType.file, OutputType.file, 3, 10),
            RegressionTest(2, "-autoprogram -out=ttxt -latin1 -ucla",
                           InputType.file, OutputType.file, 1, 10)
        ]
        g.db.add_all(regression_tests)
        g.db.commit()

        categories[0].regression_tests.append(regression_tests[0])
        categories[2].regression_tests.append(regression_tests[1])
        regression_test_outputs = [
            RegressionTestOutput(1, "sample_out1", ".srt", ""),
            RegressionTestOutput(2, "sample_out2", ".srt", "")
        ]
        g.db.add_all(regression_test_outputs)
        g.db.commit()

        rtof = RegressionTestOutputFiles("bluedabadee", 2)
        g.db.add(rtof)
        g.db.commit()

        test_result_progress = [
            TestProgress(1, TestStatus.preparation, "Test 1 preparation"),
            TestProgress(1, TestStatus.building, "Test 1 building"),
            TestProgress(1, TestStatus.testing, "Test 1 testing"),
            TestProgress(1, TestStatus.completed, "Test 1 completed"),
            TestProgress(2, TestStatus.preparation, "Test 2 preparation"),
            TestProgress(2, TestStatus.building, "Test 2 building"),
            TestProgress(2, TestStatus.testing, "Test 2 testing"),
            TestProgress(2, TestStatus.completed, "Test 2 completed")
        ]
        g.db.add_all(test_result_progress)
        g.db.commit()

        test_results = [
            TestResult(1, 1, 200, 0, 0),
            TestResult(1, 2, 601, 0, 0),
            TestResult(2, 1, 200, 200, 0),
            TestResult(2, 2, 601, 0, 0)
        ]
        g.db.add_all(test_results)
        g.db.commit()

        test_result_files = [
            TestResultFile(1, 1, 1, "sample_out1"),
            TestResultFile(1, 2, 2, "sample_out2"),
            TestResultFile(2, 1, 1, "sample_out1"),
            TestResultFile(2, 2, 2, "sample_out2", "out2")
        ]
        g.db.add_all(test_result_files)
        g.db.commit()

        forbidden_mime = ForbiddenMimeType("application/javascript")
        forbidden_ext = [ForbiddenExtension("js"), ForbiddenExtension("com")]
        g.db.add(forbidden_mime)
        g.db.add_all(forbidden_ext)
        g.db.commit()
Exemplo n.º 9
0
 def setUp(self):
     self.app.preprocess_request()
     g.db = create_session(self.app.config['DATABASE_URI'],
                           drop_tables=True)
     g.db.execute('pragma foreign_keys=on')  # Enable Foreign for unit tests
     commit_name_linux = 'fetch_commit_' + TestPlatform.linux.value
     commit_name_windows = 'fetch_commit_' + TestPlatform.windows.value
     general_data = [
         GeneralData('last_commit',
                     '1978060bf7d2edd119736ba3ba88341f3bec3323'),
         GeneralData(commit_name_linux,
                     '1978060bf7d2edd119736ba3ba88341f3bec3323'),
         GeneralData(commit_name_windows,
                     '1978060bf7d2edd119736ba3ba88341f3bec3323')
     ]
     g.db.add_all(general_data)
     self.ccextractor_version = CCExtractorVersion(
         '1.2.3', '2013-02-27T19:35:32Z',
         '1978060bf7d2edd119736ba3ba88341f3bec3323')
     g.db.add(self.ccextractor_version)
     fork_url = ('https://github.com/{user}/{repo}.git').format(
         user=g.github['repository_owner'], repo=g.github['repository'])
     fork = Fork(fork_url)
     g.db.add(fork)
     g.db.commit()
     dummy_user = User(signup_information['existing_user_name'],
                       signup_information['existing_user_role'],
                       signup_information['existing_user_email'],
                       signup_information['existing_user_pwd'])
     g.db.add(dummy_user)
     g.db.commit()
     test = [
         Test(TestPlatform.linux, TestType.pull_request, 1, 'master',
              '1978060bf7d2edd119736ba3ba88341f3bec3323', 1),
         Test(TestPlatform.linux, TestType.pull_request, 1, 'master',
              'abcdefgh', 1)
     ]
     g.db.add_all(test)
     g.db.commit()
     categories = [
         Category('Broken', 'Samples that are broken'),
         Category('DVB', 'Samples that contain DVB subtitles'),
         Category('DVD', 'Samples that contain DVD subtitles'),
         Category('MP4', 'Samples that are stored in the MP4 format'),
         Category('General', 'General regression samples')
     ]
     g.db.add_all(categories)
     g.db.commit()
     samples = [
         Sample('sample1', 'ts', 'sample1'),
         Sample('sample2', 'ts', 'sample2')
     ]
     g.db.add_all(samples)
     g.db.commit()
     upload = [
         Upload(1, 1, 1, Platform.windows),
         Upload(1, 2, 1, Platform.linux)
     ]
     g.db.add_all(upload)
     g.db.commit()
     regression_tests = [
         RegressionTest(1, '-autoprogram -out=ttxt -latin1 -2',
                        InputType.file, OutputType.file, 3, 10),
         RegressionTest(2, '-autoprogram -out=ttxt -latin1 -ucla',
                        InputType.file, OutputType.file, 1, 10)
     ]
     g.db.add_all(regression_tests)
     g.db.commit()
     categories[0].regression_tests.append(regression_tests[0])
     categories[2].regression_tests.append(regression_tests[1])
     regression_test_outputs = [
         RegressionTestOutput(1, 'sample_out1', '.srt', ''),
         RegressionTestOutput(2, 'sample_out2', '.srt', '')
     ]
     g.db.add_all(regression_test_outputs)
     g.db.commit()
     test_result_progress = [
         TestProgress(1, TestStatus.preparation, "Test 1 preperation"),
         TestProgress(1, TestStatus.building, "Test 1 building"),
         TestProgress(1, TestStatus.testing, "Test 1 testing"),
         TestProgress(1, TestStatus.completed, "Test 1 completed"),
         TestProgress(2, TestStatus.preparation, "Test 2 preperation"),
         TestProgress(2, TestStatus.building, "Test 2 building"),
         TestProgress(2, TestStatus.testing, "Test 2 testing"),
         TestProgress(2, TestStatus.completed, "Test 2 completed")
     ]
     g.db.add_all(test_result_progress)
     g.db.commit()
     test_results = [
         TestResult(1, 1, 200, 0, 0),
         TestResult(1, 2, 601, 0, 0),
         TestResult(2, 1, 200, 200, 0),
         TestResult(2, 2, 601, 0, 0)
     ]
     g.db.add_all(test_results)
     g.db.commit()
     test_result_files = [
         TestResultFile(1, 1, 1, 'sample_out1'),
         TestResultFile(1, 2, 2, 'sample_out2'),
         TestResultFile(2, 1, 1, 'sample_out1'),
         TestResultFile(2, 2, 2, 'sample_out2', 'out2')
     ]
     g.db.add_all(test_result_files)
     g.db.commit()
     forbidden_mime = ForbiddenMimeType('application/javascript')
     forbidden_ext = [ForbiddenExtension('js'), ForbiddenExtension('com')]
     g.db.add(forbidden_mime)
     g.db.add_all(forbidden_ext)
     g.db.commit()
Exemplo n.º 10
0
def start_ci():
    """
    Gets called when the webhook on GitHub is triggered. Reaction to the next events need to be processed
    (after verification):
        - Ping (for fun)
        - Push
        - Pull Request
        - Issues
    """
    if request.method != 'POST':
        return 'OK'
    else:
        abort_code = 418

        event = request.headers.get('X-GitHub-Event')
        if event == "ping":
            return json.dumps({'msg': 'Hi!'})

        x_hub_signature = request.headers.get('X-Hub-Signature')

        if not is_valid_signature(x_hub_signature, request.data,
                                  g.github['ci_key']):
            g.log.warning(
                'CI signature failed: {sig}'.format(sig=x_hub_signature))
            abort(abort_code)

        payload = request.get_json()

        if payload is None:
            g.log.warning(
                'CI payload is empty: {payload}'.format(payload=payload))
            abort(abort_code)

        gh = GitHub(access_token=g.github['bot_token'])
        repository = gh.repos(g.github['repository_owner'])(
            g.github['repository'])

        if event == "push":
            # If it's a push, and the 'after' hash is available, then it's a commit, so run the tests
            if 'after' in payload:
                commit = payload['after']
                gh_commit = repository.statuses(commit)
                # Update the db to the new last commit
                ref = repository.git().refs('heads/master').get()
                last_commit = GeneralData.query.filter(
                    GeneralData.key == 'last_commit').first()
                for platform in TestPlatform.values():
                    commit_name = 'fetch_commit_' + platform
                    fetch_commit = GeneralData.query.filter(
                        GeneralData.key == commit_name).first()

                    if fetch_commit is None:
                        prev_commit = GeneralData(commit_name,
                                                  last_commit.value)
                        g.db.add(prev_commit)

                last_commit.value = ref['object']['sha']
                g.db.commit()
                queue_test(g.db, gh_commit, commit, TestType.commit)
            else:
                g.log.warning(
                    'Unknown push type! Dumping payload for analysis')
                g.log.debug(payload)

        elif event == "pull_request":
            # If it's a valid PR, run the tests
            commit = ''
            gh_commit = None
            pr_nr = payload['pull_request']['number']
            if payload['action'] in ['opened', 'synchronize', 'reopened']:
                try:
                    commit = payload['pull_request']['head']['sha']
                    gh_commit = repository.statuses(commit)
                except KeyError:
                    g.log.critical(
                        "Didn't find a SHA value for a newly opened PR!")
                    g.log.debug(payload)

                # Check if user blacklisted
                user_id = payload['pull_request']['user']['id']
                if BlockedUsers.query.filter(
                        BlockedUsers.user_id == user_id).first() is not None:
                    g.log.critical("User Blacklisted")
                    gh_commit.post(
                        state=Status.ERROR,
                        description=
                        "CI start aborted. You may be blocked from accessing this functionality",
                        target_url=url_for('home.index', _external=True))
                    return 'ERROR'

                queue_test(g.db,
                           gh_commit,
                           commit,
                           TestType.pull_request,
                           pr_nr=pr_nr)

            elif payload['action'] == 'closed':
                g.log.debug('PR was closed, no after hash available')
                # Cancel running queue
                tests = Test.query.filter(Test.pr_nr == pr_nr).all()
                for test in tests:
                    # Add canceled status only if the test hasn't started yet
                    if len(test.progress) > 0:
                        continue
                    progress = TestProgress(test.id,
                                            TestStatus.canceled, "PR closed",
                                            datetime.datetime.now())
                    g.db.add(progress)
                    repository.statuses(test.commit).post(
                        state=Status.FAILURE,
                        description="Tests canceled",
                        context="CI - {name}".format(name=test.platform.value),
                        target_url=url_for('test.by_id',
                                           test_id=test.id,
                                           _external=True))

        elif event == "issues":
            issue_data = payload['issue']
            issue = Issue.query.filter(
                Issue.issue_id == issue_data['number']).first()

            if issue is not None:
                issue.title = issue_data['title']
                issue.status = issue_data['state']
                g.db.commit()

        elif event == "release":
            release_data = payload['release']
            release_type = payload['prerelease']
            # checking whether it is meant for production
            if release_type is False:
                g.log.debug("error, release event meant for pre-release")
                return
            else:
                release_version = payload['tag_name']
                # Github recommends adding v to the version
                if release_version[0] == 'v':
                    release_version = release_version[1:]
                release_date = payload['created_at']
                release_commit = GeneralData.query.filter(
                    GeneralData.key == 'last_commit').first()
                release = CCExtractorVersion(release_data, release_version,
                                             release_commit)
                g.db.add(release)
                g.db.commit()
                # adding test corresponding to last commit to the baseline regression results
                test = Test.query.filter(
                    and_(Test.commit == release_commit,
                         Test.platform == TestPlatform.linux)).first()
                test_result_file = g.db.query(TestResultFile).filter(
                    TestResultFile.test_id == test.id).subquery()
                test_result = g.db.query(TestResult).filter(
                    TestResult.test_id == test.id).subquery()
                g.db.query(RegressionTestOutput.correct).filter(
                    and_(
                        RegressionTestOutput.regression_id ==
                        test_result_file.c.regression_test_id,
                        test_result_file.c.got
                        is not None)).values(test_result_file.c.got)
                g.db.query(RegressionTest.expected_rc).filter(
                    RegressionTest.id ==
                    test_result.c.regression_test_id).values(
                        test_result.c.expected_rc)
                g.db.commit()

        else:
            # Unknown type
            g.log.warning('CI unrecognized event: {event}'.format(event=event))

        return json.dumps({'msg': 'EOL'})