def test_githublistener_post_return(self):
        """
        Test the overall response of the GithubListener post endpoint
        """

        # Without tag
        commit = Commit(repository='git-repo', commit_hash='git-hash')
        with mock.patch('mc.views.GithubListener') as gh_mock, \
                mock.patch('mc.tasks.build_docker') as bd_mock:

            gh_mock.parse_github_payload.return_value = commit
            bd_mock.delay.return_value = None

            url = url_for('GithubListener'.lower())
            r = self.client.post(url)
        self.assertEqual(r.json['received'], 'git-repo@git-hash, tag:None')

        # With tag
        commit.tag = 'v1.0.0'
        with mock.patch('mc.views.GithubListener') as gh_mock, \
                mock.patch('mc.tasks.build_docker') as bd_mock:

            gh_mock.parse_github_payload.return_value = commit
            bd_mock.delay.return_value = None

            url = url_for('GithubListener'.lower())
            r = self.client.post(url)
        self.assertEqual(r.json['received'], 'git-repo@git-hash, tag:v1.0.0')
    def test_build_model(self):
        """
        CRUD on models.Build
        """

        # Create
        commit = Commit(commit_hash='test-hash')
        db.session.add(commit)
        build = Build(commit=commit, built=False, pushed=False)
        db.session.commit()

        # Read
        b = build.query.first()
        self.assertEqual(b, build)
        self.assertEqual(b.commit, commit)

        # Update
        b.no_cache = True
        b.commit = Commit(commit_hash='mutated')
        db.session.commit()
        self.assertEqual(Build.query.first().no_cache, True)
        self.assertEqual(Build.query.first().commit.commit_hash, 'mutated')

        # Delete
        b = Build.query.first()
        db.session.delete(b)
        db.session.commit()
        self.assertIsNone(Build.query.first())
    def test_githublistener_post_return(self):
        """
        Test the overall response of the GithubListener post endpoint
        """

        # Without tag
        commit = Commit(
            repository='git-repo',
            commit_hash='git-hash'
        )
        with mock.patch('mc.views.GithubListener') as gh_mock, \
                mock.patch('mc.tasks.build_docker') as bd_mock:

            gh_mock.parse_github_payload.return_value = commit
            bd_mock.delay.return_value = None

            url = url_for('GithubListener'.lower())
            r = self.client.post(url)
        self.assertEqual(r.json['received'], 'git-repo@git-hash, tag:None')

        # With tag
        commit.tag = 'v1.0.0'
        with mock.patch('mc.views.GithubListener') as gh_mock, \
                mock.patch('mc.tasks.build_docker') as bd_mock:

            gh_mock.parse_github_payload.return_value = commit
            bd_mock.delay.return_value = None

            url = url_for('GithubListener'.lower())
            r = self.client.post(url)
        self.assertEqual(r.json['received'], 'git-repo@git-hash, tag:v1.0.0')
Beispiel #4
0
    def setUp(self):
        db.create_all()
        self.commit = Commit(commit_hash='master', repository="adsws")
        self.build = Build(commit=self.commit)

        # Add some unrelated commit/builds to ensure that these are *not*
        # rendered
        c = Commit(commit_hash="_hash_", repository="_repo_")
        b = Build(commit=c)

        db.session.add(self.commit)
        db.session.add(self.build)
        db.session.add(c)
        db.session.add(b)
        db.session.commit()
Beispiel #5
0
    def test_docker_build_task(self, mocked):
        """
        Tests that the docker_build_task adds a Build entry. Assumes the
        underlying builder.run command does not Raise
        """
        commit = Commit(
            repository='adsws',
            commit_hash='test-hash',
        )
        db.session.add(commit)
        db.session.commit()
        commit_id = commit.id

        instance = mocked.return_value
        instance.build.return_value = ['Successfully built']
        instance.push.return_value = ['pushing tag']

        build_docker(commit_id)

        build = db.session.query(Build).first()
        self.assertEqual(build.commit_id, commit_id)
        self.assertAlmostEqual(build.timestamp,
                               datetime.datetime.now(),
                               delta=datetime.timedelta(seconds=1))
        self.assertTrue(build.built)
        self.assertTrue(build.pushed)
    def test_commit_model(self):
        """
        CRUD on models.Commit
        """

        # Create
        commit = Commit(
            commit_hash='test-hash',
            timestamp=datetime.datetime(2015,
                                        6,
                                        3,
                                        12,
                                        26,
                                        57,
                                        tzinfo=tzlocal()),
            repository="test-repo",
            message="test-message",
        )
        db.session.add(commit)
        db.session.commit()

        # commit_hash should be unique
        _ = Commit(commit_hash='test-hash')
        with self.assertRaises(IntegrityError):
            db.session.add(_)
            db.session.commit()
        db.session.remove()

        # Read
        c = Commit.query.first()
        self.assertEqual(c.commit_hash, 'test-hash')
        self.assertEqual(c.timestamp,
                         datetime.datetime(2015, 6, 3, 12, 26, 57))

        # Update
        c.commit_hash = 'mutated'
        db.session.commit()
        self.assertEqual(Commit.query.first().commit_hash, 'mutated')

        # Delete
        c = Commit.query.first()
        db.session.delete(c)
        db.session.commit()
        self.assertIsNone(Commit.query.first())
    def run(self, repo, commit_hash=None, tag=None, app=app):
        with app.app_context():

            if tag:
                # Using the tag, obtain the sha for the relevant tag
                url = current_app.config['GITHUB_TAG_FIND_API'].format(
                    repo=repo, tag=tag)
                r = requests.get(url)
                r.raise_for_status()
                payload_find_tag = r.json()
                try:
                    tag_commit_hash = payload_find_tag['object']['sha']
                except KeyError:
                    raise KeyError(
                        'tag supplied does not exist: {0}'.format(tag))

                # Obtain the commit hash for this tag
                url = current_app.config['GITHUB_TAG_GET_API'].format(
                    repo=repo, hash=tag_commit_hash)
                r = requests.get(url)
                r.raise_for_status()
                payload_get_tag = r.json()
                commit_hash = payload_get_tag['object']['sha']

                current_app.logger.info(
                    'user supplied a tag: {0}, sha: {1}'.format(
                        tag, commit_hash))

            url = current_app.config['GITHUB_COMMIT_API'].format(
                repo=repo, hash=commit_hash)
            r = requests.get(url)
            r.raise_for_status()
            payload = r.json()
            try:
                c = Commit.query.filter_by(commit_hash=commit_hash,
                                           repository=repo).one()

                if not c.tag and tag:
                    c.tag = tag

            except NoResultFound:
                c = Commit(commit_hash=commit_hash,
                           timestamp=parser.parse(payload['author']['date']),
                           author=payload['author']['name'],
                           repository=repo,
                           message=payload['message'],
                           tag=tag if tag else None)
            db.session.add(c)
            db.session.commit()
            build_docker.delay(c.id)
            current_app.logger.info("user-received: {}@{}".format(
                c.repository, c.commit_hash))
Beispiel #8
0
 def setUp(self):
     self.commit = Commit(commit_hash='master', repository="adsws")
     self.build = Build(commit=self.commit)
     self.containers = [
         ECSBuilder.DockerContainer(self.build,
                                    environment="staging",
                                    memory=m,
                                    portmappings=[{
                                        "hostPort": 8080,
                                        "containerPort": 80
                                    }] if m == 10 else None)
         for m in range(10, 50, 10)
     ]
     self.builder = ECSBuilder(self.containers, family="unittest")
    def parse_github_payload(request=None):
        """
        parses a github webhook message to create a models.Commit instance
        If that commit is already in the database, it instead returns that
        commit
        :param request: request containing the header and body
        :return: models.Commit based on the incoming payload
        """

        if request is None:
            raise ValueError("No request object given")

        payload = request.get_json()
        repo = payload['repository']['name']
        commit_hash = payload['head_commit']['id']
        tag = payload['ref'].replace('refs/tags/', '') \
            if 'tags' in payload['ref'] else None

        if repo not in current_app.config.get('WATCHED_REPOS'):
            raise UnknownRepoError("{}".format(repo))

        try:
            c = Commit.query.filter_by(commit_hash=commit_hash,
                                       repository=repo).one()

            if not c.tag and tag:
                c.tag = tag
                db.session.add(c)
                db.session.commit()

            return c
        except NoResultFound:
            return Commit(
                commit_hash=commit_hash,
                tag=tag,
                timestamp=parser.parse(payload['head_commit']['timestamp']),
                author=payload['head_commit']['author']['username'],
                repository=repo,
                message=payload['head_commit']['message'],
            )
Beispiel #10
0
 def setUp(self):
     self.commit = Commit(commit_hash='master', repository="adsws")
     self.builder = DockerImageBuilder(self.commit)