From a51f517f8d3b1ffae7f702faa7beafac45195e19 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Wed, 18 Jul 2018 11:19:41 -0400 Subject: [PATCH] Move prepare trigger tests to pytest --- buildtrigger/test/test_prepare_trigger.py | 519 ++++++++++++++++++ .../test}/triggerjson/bitbucket_commit.json | 0 .../bitbucket_customer_example_noauthor.json | 0 .../bitbucket_customer_example_tag.json | 0 .../triggerjson/bitbucket_knownissue.json | 0 .../test}/triggerjson/bitbucket_webhook.json | 0 .../test}/triggerjson/custom_webhook.json | 0 .../test}/triggerjson/github_webhook.json | 0 .../github_webhook_deletedcommit.json | 0 .../github_webhook_knownissue.json | 0 .../triggerjson/github_webhook_missing.json | 0 .../triggerjson/github_webhook_noname.json | 0 .../github_webhook_slash_branch.json | 0 .../test}/triggerjson/gitlab_webhook.json | 0 .../gitlab_webhook_multicommit.json | 0 .../triggerjson/gitlab_webhook_nocommit.json | 0 .../triggerjson/gitlab_webhook_other.json | 0 .../test}/triggerjson/gitlab_webhook_tag.json | 0 test/test_prepare_trigger.py | 519 ------------------ 19 files changed, 519 insertions(+), 519 deletions(-) create mode 100644 buildtrigger/test/test_prepare_trigger.py rename {test => buildtrigger/test}/triggerjson/bitbucket_commit.json (100%) rename {test => buildtrigger/test}/triggerjson/bitbucket_customer_example_noauthor.json (100%) rename {test => buildtrigger/test}/triggerjson/bitbucket_customer_example_tag.json (100%) rename {test => buildtrigger/test}/triggerjson/bitbucket_knownissue.json (100%) rename {test => buildtrigger/test}/triggerjson/bitbucket_webhook.json (100%) rename {test => buildtrigger/test}/triggerjson/custom_webhook.json (100%) rename {test => buildtrigger/test}/triggerjson/github_webhook.json (100%) rename {test => buildtrigger/test}/triggerjson/github_webhook_deletedcommit.json (100%) rename {test => buildtrigger/test}/triggerjson/github_webhook_knownissue.json (100%) rename {test => buildtrigger/test}/triggerjson/github_webhook_missing.json (100%) rename {test => buildtrigger/test}/triggerjson/github_webhook_noname.json (100%) rename {test => buildtrigger/test}/triggerjson/github_webhook_slash_branch.json (100%) rename {test => buildtrigger/test}/triggerjson/gitlab_webhook.json (100%) rename {test => buildtrigger/test}/triggerjson/gitlab_webhook_multicommit.json (100%) rename {test => buildtrigger/test}/triggerjson/gitlab_webhook_nocommit.json (100%) rename {test => buildtrigger/test}/triggerjson/gitlab_webhook_other.json (100%) rename {test => buildtrigger/test}/triggerjson/gitlab_webhook_tag.json (100%) delete mode 100644 test/test_prepare_trigger.py diff --git a/buildtrigger/test/test_prepare_trigger.py b/buildtrigger/test/test_prepare_trigger.py new file mode 100644 index 000000000..165507fd8 --- /dev/null +++ b/buildtrigger/test/test_prepare_trigger.py @@ -0,0 +1,519 @@ +import json + +import pytest + +from jsonschema import validate + +from buildtrigger.customhandler import custom_trigger_payload +from buildtrigger.basehandler import METADATA_SCHEMA +from buildtrigger.bitbuckethandler import get_transformed_webhook_payload as bb_webhook +from buildtrigger.bitbuckethandler import get_transformed_commit_info as bb_commit +from buildtrigger.githubhandler import get_transformed_webhook_payload as gh_webhook +from buildtrigger.gitlabhandler import get_transformed_webhook_payload as gl_webhook +from buildtrigger.triggerutil import SkipRequestException + +def assertSkipped(filename, processor, *args, **kwargs): + with open('buildtrigger/test/triggerjson/%s.json' % filename) as f: + payload = json.loads(f.read()) + + nargs = [payload] + nargs.extend(args) + + with pytest.raises(SkipRequestException): + processor(*nargs, **kwargs) + + +def assertSchema(filename, expected, processor, *args, **kwargs): + with open('buildtrigger/test/triggerjson/%s.json' % filename) as f: + payload = json.loads(f.read()) + + nargs = [payload] + nargs.extend(args) + + created = processor(*nargs, **kwargs) + assert created == expected + validate(created, METADATA_SCHEMA) + + +def test_custom_custom(): + expected = { + u'commit':u'1c002dd', + u'commit_info': { + u'url': u'gitsoftware.com/repository/commits/1234567', + u'date': u'timestamp', + u'message': u'initial commit', + u'committer': { + u'username': u'user', + u'url': u'gitsoftware.com/users/user', + u'avatar_url': u'gravatar.com/user.png' + }, + u'author': { + u'username': u'user', + u'url': u'gitsoftware.com/users/user', + u'avatar_url': u'gravatar.com/user.png' + } + }, + u'ref': u'refs/heads/master', + u'default_branch': u'master', + u'git_url': u'foobar', + } + + assertSchema('custom_webhook', expected, custom_trigger_payload, git_url='foobar') + + +def test_custom_gitlab(): + expected = { + 'commit': u'fb88379ee45de28a0a4590fddcbd8eff8b36026e', + 'ref': u'refs/heads/master', + 'git_url': u'git@gitlab.com:jzelinskie/www-gitlab-com.git', + 'commit_info': { + 'url': u'https://gitlab.com/jzelinskie/www-gitlab-com/commit/fb88379ee45de28a0a4590fddcbd8eff8b36026e', + 'date': u'2015-08-13T19:33:18+00:00', + 'message': u'Fix link\n', + }, + } + + assertSchema('gitlab_webhook', expected, custom_trigger_payload, git_url='git@gitlab.com:jzelinskie/www-gitlab-com.git') + + +def test_custom_github(): + expected = { + 'commit': u'410f4cdf8ff09b87f245b13845e8497f90b90a4c', + 'ref': u'refs/heads/master', + 'git_url': u'git@github.com:josephschorr/anothertest.git', + 'commit_info': { + 'url': u'https://github.com/josephschorr/anothertest/commit/410f4cdf8ff09b87f245b13845e8497f90b90a4c', + 'date': u'2015-09-11T14:26:16-04:00', + 'message': u'Update Dockerfile', + 'committer': { + 'username': u'josephschorr', + }, + 'author': { + 'username': u'josephschorr', + }, + }, + } + + assertSchema('github_webhook', expected, custom_trigger_payload, + git_url='git@github.com:josephschorr/anothertest.git') + + +def test_custom_bitbucket(): + expected = { + "commit": u"af64ae7188685f8424040b4735ad12941b980d75", + "ref": u"refs/heads/master", + "git_url": u"git@bitbucket.org:jscoreos/another-repo.git", + "commit_info": { + "url": u"https://bitbucket.org/jscoreos/another-repo/commits/af64ae7188685f8424040b4735ad12941b980d75", + "date": u"2015-09-10T20:40:54+00:00", + "message": u"Dockerfile edited online with Bitbucket", + "author": { + "username": u"jscoreos", + "url": u"https://bitbucket.org/jscoreos/", + "avatar_url": u"https://bitbucket.org/account/jscoreos/avatar/32/", + }, + "committer": { + "username": u"jscoreos", + "url": u"https://bitbucket.org/jscoreos/", + "avatar_url": u"https://bitbucket.org/account/jscoreos/avatar/32/", + }, + }, + } + + assertSchema('bitbucket_webhook', expected, custom_trigger_payload, git_url='git@bitbucket.org:jscoreos/another-repo.git') + + +def test_bitbucket_customer_payload_noauthor(): + expected = { + "commit": "a0ec139843b2bb281ab21a433266ddc498e605dc", + "ref": "refs/heads/master", + "git_url": "git@bitbucket.org:lightsidelabs/svc-identity.git", + "commit_info": { + "url": "https://bitbucket.org/lightsidelabs/svc-identity/commits/a0ec139843b2bb281ab21a433266ddc498e605dc", + "date": "2015-09-25T00:55:08+00:00", + "message": "Update version.py to 0.1.2 [skip ci]\n\n(by utilitybelt/scripts/autotag_version.py)\n", + "committer": { + "username": "LightSide_CodeShip", + "url": "https://bitbucket.org/LightSide_CodeShip/", + "avatar_url": "https://bitbucket.org/account/LightSide_CodeShip/avatar/32/", + }, + }, + } + + assertSchema('bitbucket_customer_example_noauthor', expected, bb_webhook) + + +def test_bitbucket_customer_payload_tag(): + expected = { + "commit": "a0ec139843b2bb281ab21a433266ddc498e605dc", + "ref": "refs/tags/0.1.2", + "git_url": "git@bitbucket.org:lightsidelabs/svc-identity.git", + "commit_info": { + "url": "https://bitbucket.org/lightsidelabs/svc-identity/commits/a0ec139843b2bb281ab21a433266ddc498e605dc", + "date": "2015-09-25T00:55:08+00:00", + "message": "Update version.py to 0.1.2 [skip ci]\n\n(by utilitybelt/scripts/autotag_version.py)\n", + "committer": { + "username": "LightSide_CodeShip", + "url": "https://bitbucket.org/LightSide_CodeShip/", + "avatar_url": "https://bitbucket.org/account/LightSide_CodeShip/avatar/32/", + }, + }, + } + + assertSchema('bitbucket_customer_example_tag', expected, bb_webhook) + + +def test_bitbucket_commit(): + ref = 'refs/heads/somebranch' + default_branch = 'somebranch' + repository_name = 'foo/bar' + + def lookup_author(_): + return { + 'user': { + 'username': 'cooluser', + 'avatar': 'http://some/avatar/url' + } + } + + expected = { + "commit": u"abdeaf1b2b4a6b9ddf742c1e1754236380435a62", + "ref": u"refs/heads/somebranch", + "git_url": u"git@bitbucket.org:foo/bar.git", + "default_branch": u"somebranch", + "commit_info": { + "url": u"https://bitbucket.org/foo/bar/commits/abdeaf1b2b4a6b9ddf742c1e1754236380435a62", + "date": u"2012-07-24 00:26:36", + "message": u"making some changes\n", + "author": { + "url": u"https://bitbucket.org/cooluser/", + "avatar_url": u"http://some/avatar/url", + "username": u"cooluser", + } + } + } + + assertSchema('bitbucket_commit', expected, bb_commit, ref, default_branch, + repository_name, lookup_author) + +def test_bitbucket_webhook_payload(): + expected = { + "commit": u"af64ae7188685f8424040b4735ad12941b980d75", + "ref": u"refs/heads/master", + "git_url": u"git@bitbucket.org:jscoreos/another-repo.git", + "commit_info": { + "url": u"https://bitbucket.org/jscoreos/another-repo/commits/af64ae7188685f8424040b4735ad12941b980d75", + "date": u"2015-09-10T20:40:54+00:00", + "message": u"Dockerfile edited online with Bitbucket", + "author": { + "username": u"jscoreos", + "url": u"https://bitbucket.org/jscoreos/", + "avatar_url": u"https://bitbucket.org/account/jscoreos/avatar/32/", + }, + "committer": { + "username": u"jscoreos", + "url": u"https://bitbucket.org/jscoreos/", + "avatar_url": u"https://bitbucket.org/account/jscoreos/avatar/32/", + }, + }, + } + + assertSchema('bitbucket_webhook', expected, bb_webhook) + + +def test_github_webhook_payload_slash_branch(): + expected = { + 'commit': u'410f4cdf8ff09b87f245b13845e8497f90b90a4c', + 'ref': u'refs/heads/slash/branch', + 'git_url': u'git@github.com:josephschorr/anothertest.git', + 'commit_info': { + 'url': u'https://github.com/josephschorr/anothertest/commit/410f4cdf8ff09b87f245b13845e8497f90b90a4c', + 'date': u'2015-09-11T14:26:16-04:00', + 'message': u'Update Dockerfile', + 'committer': { + 'username': u'josephschorr', + }, + 'author': { + 'username': u'josephschorr', + }, + }, + } + + assertSchema('github_webhook_slash_branch', expected, gh_webhook) + + +def test_github_webhook_payload(): + expected = { + 'commit': u'410f4cdf8ff09b87f245b13845e8497f90b90a4c', + 'ref': u'refs/heads/master', + 'git_url': u'git@github.com:josephschorr/anothertest.git', + 'commit_info': { + 'url': u'https://github.com/josephschorr/anothertest/commit/410f4cdf8ff09b87f245b13845e8497f90b90a4c', + 'date': u'2015-09-11T14:26:16-04:00', + 'message': u'Update Dockerfile', + 'committer': { + 'username': u'josephschorr', + }, + 'author': { + 'username': u'josephschorr', + }, + }, + } + + assertSchema('github_webhook', expected, gh_webhook) + + +def test_github_webhook_payload_with_lookup(): + expected = { + 'commit': u'410f4cdf8ff09b87f245b13845e8497f90b90a4c', + 'ref': u'refs/heads/master', + 'git_url': u'git@github.com:josephschorr/anothertest.git', + 'commit_info': { + 'url': u'https://github.com/josephschorr/anothertest/commit/410f4cdf8ff09b87f245b13845e8497f90b90a4c', + 'date': u'2015-09-11T14:26:16-04:00', + 'message': u'Update Dockerfile', + 'committer': { + 'username': u'josephschorr', + 'url': u'http://github.com/josephschorr', + 'avatar_url': u'http://some/avatar/url', + }, + 'author': { + 'username': u'josephschorr', + 'url': u'http://github.com/josephschorr', + 'avatar_url': u'http://some/avatar/url', + }, + }, + } + + def lookup_user(_): + return { + 'html_url': 'http://github.com/josephschorr', + 'avatar_url': 'http://some/avatar/url' + } + + assertSchema('github_webhook', expected, gh_webhook, lookup_user=lookup_user) + + +def test_github_webhook_payload_missing_fields_with_lookup(): + expected = { + 'commit': u'410f4cdf8ff09b87f245b13845e8497f90b90a4c', + 'ref': u'refs/heads/master', + 'git_url': u'git@github.com:josephschorr/anothertest.git', + 'commit_info': { + 'url': u'https://github.com/josephschorr/anothertest/commit/410f4cdf8ff09b87f245b13845e8497f90b90a4c', + 'date': u'2015-09-11T14:26:16-04:00', + 'message': u'Update Dockerfile' + }, + } + + def lookup_user(username): + if not username: + raise Exception('Fail!') + + return { + 'html_url': 'http://github.com/josephschorr', + 'avatar_url': 'http://some/avatar/url' + } + + assertSchema('github_webhook_missing', expected, gh_webhook, lookup_user=lookup_user) + + +def test_gitlab_webhook_payload(): + expected = { + 'commit': u'fb88379ee45de28a0a4590fddcbd8eff8b36026e', + 'ref': u'refs/heads/master', + 'git_url': u'git@gitlab.com:jzelinskie/www-gitlab-com.git', + 'commit_info': { + 'url': u'https://gitlab.com/jzelinskie/www-gitlab-com/commit/fb88379ee45de28a0a4590fddcbd8eff8b36026e', + 'date': u'2015-08-13T19:33:18+00:00', + 'message': u'Fix link\n', + }, + } + + assertSchema('gitlab_webhook', expected, gl_webhook) + + +def test_github_webhook_payload_known_issue(): + expected = { + "commit": "118b07121695d9f2e40a5ff264fdcc2917680870", + "ref": "refs/heads/master", + "git_url": "git@github.com:silas/docker-test.git", + "commit_info": { + "url": "https://github.com/silas/docker-test/commit/118b07121695d9f2e40a5ff264fdcc2917680870", + "date": "2015-09-25T14:55:11-04:00", + "message": "Fail", + }, + } + + assertSchema('github_webhook_noname', expected, gh_webhook) + + +def test_github_webhook_payload_missing_fields(): + expected = { + 'commit': u'410f4cdf8ff09b87f245b13845e8497f90b90a4c', + 'ref': u'refs/heads/master', + 'git_url': u'git@github.com:josephschorr/anothertest.git', + 'commit_info': { + 'url': u'https://github.com/josephschorr/anothertest/commit/410f4cdf8ff09b87f245b13845e8497f90b90a4c', + 'date': u'2015-09-11T14:26:16-04:00', + 'message': u'Update Dockerfile' + }, + } + + assertSchema('github_webhook_missing', expected, gh_webhook) + + +def test_gitlab_webhook_nocommit_payload(): + assertSkipped('gitlab_webhook_nocommit', gl_webhook) + + +def test_gitlab_webhook_multiple_commits(): + expected = { + 'commit': u'9a052a0b2fbe01d4a1a88638dd9fe31c1c56ef53', + 'ref': u'refs/heads/master', + 'git_url': u'git@gitlab.com:joseph.schorr/some-test-project.git', + 'commit_info': { + 'url': u'https://gitlab.com/joseph.schorr/some-test-project/commit/9a052a0b2fbe01d4a1a88638dd9fe31c1c56ef53', + 'date': u'2016-09-29T15:02:41+00:00', + 'message': u"Merge branch 'foobar' into 'master'\r\n\r\nAdd changelog\r\n\r\nSome merge thing\r\n\r\nSee merge request !1", + 'author': { + 'username': 'josephschorr', + 'url': 'http://gitlab.com/josephschorr', + 'avatar_url': 'http://some/avatar/url' + }, + }, + } + + def lookup_user(_): + return { + 'username': 'josephschorr', + 'html_url': 'http://gitlab.com/josephschorr', + 'avatar_url': 'http://some/avatar/url', + } + + assertSchema('gitlab_webhook_multicommit', expected, gl_webhook, lookup_user=lookup_user) + + +def test_gitlab_webhook_for_tag(): + expected = { + 'commit': u'82b3d5ae55f7080f1e6022629cdb57bfae7cccc7', + 'commit_info': { + 'author': { + 'avatar_url': 'http://some/avatar/url', + 'url': 'http://gitlab.com/jzelinskie', + 'username': 'jzelinskie' + }, + 'date': '2015-08-13T19:33:18+00:00', + 'message': 'Fix link\n', + 'url': 'https://some/url', + }, + 'git_url': u'git@example.com:jsmith/example.git', + 'ref': u'refs/tags/v1.0.0', + } + + def lookup_user(_): + return { + 'username': 'jzelinskie', + 'html_url': 'http://gitlab.com/jzelinskie', + 'avatar_url': 'http://some/avatar/url', + } + + def lookup_commit(repo_id, commit_sha): + if commit_sha == '82b3d5ae55f7080f1e6022629cdb57bfae7cccc7': + return { + "id": "82b3d5ae55f7080f1e6022629cdb57bfae7cccc7", + "message": "Fix link\n", + "timestamp": "2015-08-13T19:33:18+00:00", + "url": "https://some/url", + "author_name": "Foo Guy", + "author_email": "foo@bar.com", + } + + return None + + assertSchema('gitlab_webhook_tag', expected, gl_webhook, lookup_user=lookup_user, + lookup_commit=lookup_commit) + + +def test_gitlab_webhook_for_tag_nocommit(): + expected = { + 'commit': u'82b3d5ae55f7080f1e6022629cdb57bfae7cccc7', + 'git_url': u'git@example.com:jsmith/example.git', + 'ref': u'refs/tags/v1.0.0', + } + + def lookup_user(_): + return { + 'username': 'jzelinskie', + 'html_url': 'http://gitlab.com/jzelinskie', + 'avatar_url': 'http://some/avatar/url', + } + + assertSchema('gitlab_webhook_tag', expected, gl_webhook, lookup_user=lookup_user) + + +def test_gitlab_webhook_for_other(): + assertSkipped('gitlab_webhook_other', gl_webhook) + + +def test_gitlab_webhook_payload_with_lookup(): + expected = { + 'commit': u'fb88379ee45de28a0a4590fddcbd8eff8b36026e', + 'ref': u'refs/heads/master', + 'git_url': u'git@gitlab.com:jzelinskie/www-gitlab-com.git', + 'commit_info': { + 'url': u'https://gitlab.com/jzelinskie/www-gitlab-com/commit/fb88379ee45de28a0a4590fddcbd8eff8b36026e', + 'date': u'2015-08-13T19:33:18+00:00', + 'message': u'Fix link\n', + 'author': { + 'username': 'jzelinskie', + 'url': 'http://gitlab.com/jzelinskie', + 'avatar_url': 'http://some/avatar/url', + }, + }, + } + + def lookup_user(_): + return { + 'username': 'jzelinskie', + 'html_url': 'http://gitlab.com/jzelinskie', + 'avatar_url': 'http://some/avatar/url', + } + + assertSchema('gitlab_webhook', expected, gl_webhook, lookup_user=lookup_user) + + +def test_github_webhook_payload_deleted_commit(): + expected = { + 'commit': u'456806b662cb903a0febbaed8344f3ed42f27bab', + 'commit_info': { + 'author': { + 'username': u'jakedt' + }, + 'committer': { + 'username': u'jakedt' + }, + 'date': u'2015-12-08T18:07:03-05:00', + 'message': (u'Merge pull request #1044 from jakedt/errerror\n\n' + + 'Assign the exception to a variable to log it'), + 'url': u'https://github.com/coreos-inc/quay/commit/456806b662cb903a0febbaed8344f3ed42f27bab' + }, + 'git_url': u'git@github.com:coreos-inc/quay.git', + 'ref': u'refs/heads/master', + } + + def lookup_user(_): + return None + + assertSchema('github_webhook_deletedcommit', expected, gh_webhook, lookup_user=lookup_user) + + +def test_github_webhook_known_issue(): + def lookup_user(_): + return None + + assertSkipped('github_webhook_knownissue', gh_webhook, lookup_user=lookup_user) + + +def test_bitbucket_webhook_known_issue(): + assertSkipped('bitbucket_knownissue', bb_webhook) diff --git a/test/triggerjson/bitbucket_commit.json b/buildtrigger/test/triggerjson/bitbucket_commit.json similarity index 100% rename from test/triggerjson/bitbucket_commit.json rename to buildtrigger/test/triggerjson/bitbucket_commit.json diff --git a/test/triggerjson/bitbucket_customer_example_noauthor.json b/buildtrigger/test/triggerjson/bitbucket_customer_example_noauthor.json similarity index 100% rename from test/triggerjson/bitbucket_customer_example_noauthor.json rename to buildtrigger/test/triggerjson/bitbucket_customer_example_noauthor.json diff --git a/test/triggerjson/bitbucket_customer_example_tag.json b/buildtrigger/test/triggerjson/bitbucket_customer_example_tag.json similarity index 100% rename from test/triggerjson/bitbucket_customer_example_tag.json rename to buildtrigger/test/triggerjson/bitbucket_customer_example_tag.json diff --git a/test/triggerjson/bitbucket_knownissue.json b/buildtrigger/test/triggerjson/bitbucket_knownissue.json similarity index 100% rename from test/triggerjson/bitbucket_knownissue.json rename to buildtrigger/test/triggerjson/bitbucket_knownissue.json diff --git a/test/triggerjson/bitbucket_webhook.json b/buildtrigger/test/triggerjson/bitbucket_webhook.json similarity index 100% rename from test/triggerjson/bitbucket_webhook.json rename to buildtrigger/test/triggerjson/bitbucket_webhook.json diff --git a/test/triggerjson/custom_webhook.json b/buildtrigger/test/triggerjson/custom_webhook.json similarity index 100% rename from test/triggerjson/custom_webhook.json rename to buildtrigger/test/triggerjson/custom_webhook.json diff --git a/test/triggerjson/github_webhook.json b/buildtrigger/test/triggerjson/github_webhook.json similarity index 100% rename from test/triggerjson/github_webhook.json rename to buildtrigger/test/triggerjson/github_webhook.json diff --git a/test/triggerjson/github_webhook_deletedcommit.json b/buildtrigger/test/triggerjson/github_webhook_deletedcommit.json similarity index 100% rename from test/triggerjson/github_webhook_deletedcommit.json rename to buildtrigger/test/triggerjson/github_webhook_deletedcommit.json diff --git a/test/triggerjson/github_webhook_knownissue.json b/buildtrigger/test/triggerjson/github_webhook_knownissue.json similarity index 100% rename from test/triggerjson/github_webhook_knownissue.json rename to buildtrigger/test/triggerjson/github_webhook_knownissue.json diff --git a/test/triggerjson/github_webhook_missing.json b/buildtrigger/test/triggerjson/github_webhook_missing.json similarity index 100% rename from test/triggerjson/github_webhook_missing.json rename to buildtrigger/test/triggerjson/github_webhook_missing.json diff --git a/test/triggerjson/github_webhook_noname.json b/buildtrigger/test/triggerjson/github_webhook_noname.json similarity index 100% rename from test/triggerjson/github_webhook_noname.json rename to buildtrigger/test/triggerjson/github_webhook_noname.json diff --git a/test/triggerjson/github_webhook_slash_branch.json b/buildtrigger/test/triggerjson/github_webhook_slash_branch.json similarity index 100% rename from test/triggerjson/github_webhook_slash_branch.json rename to buildtrigger/test/triggerjson/github_webhook_slash_branch.json diff --git a/test/triggerjson/gitlab_webhook.json b/buildtrigger/test/triggerjson/gitlab_webhook.json similarity index 100% rename from test/triggerjson/gitlab_webhook.json rename to buildtrigger/test/triggerjson/gitlab_webhook.json diff --git a/test/triggerjson/gitlab_webhook_multicommit.json b/buildtrigger/test/triggerjson/gitlab_webhook_multicommit.json similarity index 100% rename from test/triggerjson/gitlab_webhook_multicommit.json rename to buildtrigger/test/triggerjson/gitlab_webhook_multicommit.json diff --git a/test/triggerjson/gitlab_webhook_nocommit.json b/buildtrigger/test/triggerjson/gitlab_webhook_nocommit.json similarity index 100% rename from test/triggerjson/gitlab_webhook_nocommit.json rename to buildtrigger/test/triggerjson/gitlab_webhook_nocommit.json diff --git a/test/triggerjson/gitlab_webhook_other.json b/buildtrigger/test/triggerjson/gitlab_webhook_other.json similarity index 100% rename from test/triggerjson/gitlab_webhook_other.json rename to buildtrigger/test/triggerjson/gitlab_webhook_other.json diff --git a/test/triggerjson/gitlab_webhook_tag.json b/buildtrigger/test/triggerjson/gitlab_webhook_tag.json similarity index 100% rename from test/triggerjson/gitlab_webhook_tag.json rename to buildtrigger/test/triggerjson/gitlab_webhook_tag.json diff --git a/test/test_prepare_trigger.py b/test/test_prepare_trigger.py deleted file mode 100644 index 975847533..000000000 --- a/test/test_prepare_trigger.py +++ /dev/null @@ -1,519 +0,0 @@ -import unittest -import json - -from jsonschema import validate -from buildtrigger.customhandler import custom_trigger_payload -from buildtrigger.basehandler import METADATA_SCHEMA -from buildtrigger.bitbuckethandler import get_transformed_webhook_payload as bb_webhook -from buildtrigger.bitbuckethandler import get_transformed_commit_info as bb_commit -from buildtrigger.githubhandler import get_transformed_webhook_payload as gh_webhook -from buildtrigger.gitlabhandler import get_transformed_webhook_payload as gl_webhook -from buildtrigger.triggerutil import SkipRequestException - -class TestPrepareTrigger(unittest.TestCase): - def assertSkipped(self, filename, processor, *args, **kwargs): - with open('test/triggerjson/%s.json' % filename) as f: - payload = json.loads(f.read()) - - nargs = [payload] - nargs.extend(args) - - with self.assertRaises(SkipRequestException): - processor(*nargs, **kwargs) - - - def assertSchema(self, filename, expected, processor, *args, **kwargs): - with open('test/triggerjson/%s.json' % filename) as f: - payload = json.loads(f.read()) - - nargs = [payload] - nargs.extend(args) - - created = processor(*nargs, **kwargs) - self.assertEquals(expected, created) - validate(created, METADATA_SCHEMA) - - - def test_custom_custom(self): - expected = { - u'commit':u'1c002dd', - u'commit_info': { - u'url': u'gitsoftware.com/repository/commits/1234567', - u'date': u'timestamp', - u'message': u'initial commit', - u'committer': { - u'username': u'user', - u'url': u'gitsoftware.com/users/user', - u'avatar_url': u'gravatar.com/user.png' - }, - u'author': { - u'username': u'user', - u'url': u'gitsoftware.com/users/user', - u'avatar_url': u'gravatar.com/user.png' - } - }, - u'ref': u'refs/heads/master', - u'default_branch': u'master', - u'git_url': u'foobar', - } - - self.assertSchema('custom_webhook', expected, custom_trigger_payload, git_url='foobar') - - - def test_custom_gitlab(self): - expected = { - 'commit': u'fb88379ee45de28a0a4590fddcbd8eff8b36026e', - 'ref': u'refs/heads/master', - 'git_url': u'git@gitlab.com:jzelinskie/www-gitlab-com.git', - 'commit_info': { - 'url': u'https://gitlab.com/jzelinskie/www-gitlab-com/commit/fb88379ee45de28a0a4590fddcbd8eff8b36026e', - 'date': u'2015-08-13T19:33:18+00:00', - 'message': u'Fix link\n', - }, - } - - self.assertSchema('gitlab_webhook', expected, custom_trigger_payload, git_url='git@gitlab.com:jzelinskie/www-gitlab-com.git') - - - def test_custom_github(self): - expected = { - 'commit': u'410f4cdf8ff09b87f245b13845e8497f90b90a4c', - 'ref': u'refs/heads/master', - 'git_url': u'git@github.com:josephschorr/anothertest.git', - 'commit_info': { - 'url': u'https://github.com/josephschorr/anothertest/commit/410f4cdf8ff09b87f245b13845e8497f90b90a4c', - 'date': u'2015-09-11T14:26:16-04:00', - 'message': u'Update Dockerfile', - 'committer': { - 'username': u'josephschorr', - }, - 'author': { - 'username': u'josephschorr', - }, - }, - } - - self.assertSchema('github_webhook', expected, custom_trigger_payload, git_url='git@github.com:josephschorr/anothertest.git') - - - def test_custom_bitbucket(self): - expected = { - "commit": u"af64ae7188685f8424040b4735ad12941b980d75", - "ref": u"refs/heads/master", - "git_url": u"git@bitbucket.org:jscoreos/another-repo.git", - "commit_info": { - "url": u"https://bitbucket.org/jscoreos/another-repo/commits/af64ae7188685f8424040b4735ad12941b980d75", - "date": u"2015-09-10T20:40:54+00:00", - "message": u"Dockerfile edited online with Bitbucket", - "author": { - "username": u"jscoreos", - "url": u"https://bitbucket.org/jscoreos/", - "avatar_url": u"https://bitbucket.org/account/jscoreos/avatar/32/", - }, - "committer": { - "username": u"jscoreos", - "url": u"https://bitbucket.org/jscoreos/", - "avatar_url": u"https://bitbucket.org/account/jscoreos/avatar/32/", - }, - }, - } - - self.assertSchema('bitbucket_webhook', expected, custom_trigger_payload, git_url='git@bitbucket.org:jscoreos/another-repo.git') - - - def test_bitbucket_customer_payload_noauthor(self): - expected = { - "commit": "a0ec139843b2bb281ab21a433266ddc498e605dc", - "ref": "refs/heads/master", - "git_url": "git@bitbucket.org:lightsidelabs/svc-identity.git", - "commit_info": { - "url": "https://bitbucket.org/lightsidelabs/svc-identity/commits/a0ec139843b2bb281ab21a433266ddc498e605dc", - "date": "2015-09-25T00:55:08+00:00", - "message": "Update version.py to 0.1.2 [skip ci]\n\n(by utilitybelt/scripts/autotag_version.py)\n", - "committer": { - "username": "LightSide_CodeShip", - "url": "https://bitbucket.org/LightSide_CodeShip/", - "avatar_url": "https://bitbucket.org/account/LightSide_CodeShip/avatar/32/", - }, - }, - } - - self.assertSchema('bitbucket_customer_example_noauthor', expected, bb_webhook) - - - def test_bitbucket_customer_payload_tag(self): - expected = { - "commit": "a0ec139843b2bb281ab21a433266ddc498e605dc", - "ref": "refs/tags/0.1.2", - "git_url": "git@bitbucket.org:lightsidelabs/svc-identity.git", - "commit_info": { - "url": "https://bitbucket.org/lightsidelabs/svc-identity/commits/a0ec139843b2bb281ab21a433266ddc498e605dc", - "date": "2015-09-25T00:55:08+00:00", - "message": "Update version.py to 0.1.2 [skip ci]\n\n(by utilitybelt/scripts/autotag_version.py)\n", - "committer": { - "username": "LightSide_CodeShip", - "url": "https://bitbucket.org/LightSide_CodeShip/", - "avatar_url": "https://bitbucket.org/account/LightSide_CodeShip/avatar/32/", - }, - }, - } - - self.assertSchema('bitbucket_customer_example_tag', expected, bb_webhook) - - - def test_bitbucket_commit(self): - ref = 'refs/heads/somebranch' - default_branch = 'somebranch' - repository_name = 'foo/bar' - - def lookup_author(_): - return { - 'user': { - 'username': 'cooluser', - 'avatar': 'http://some/avatar/url' - } - } - - expected = { - "commit": u"abdeaf1b2b4a6b9ddf742c1e1754236380435a62", - "ref": u"refs/heads/somebranch", - "git_url": u"git@bitbucket.org:foo/bar.git", - "default_branch": u"somebranch", - "commit_info": { - "url": u"https://bitbucket.org/foo/bar/commits/abdeaf1b2b4a6b9ddf742c1e1754236380435a62", - "date": u"2012-07-24 00:26:36", - "message": u"making some changes\n", - "author": { - "url": u"https://bitbucket.org/cooluser/", - "avatar_url": u"http://some/avatar/url", - "username": u"cooluser", - } - } - } - - self.assertSchema('bitbucket_commit', expected, bb_commit, ref, default_branch, - repository_name, lookup_author) - - def test_bitbucket_webhook_payload(self): - expected = { - "commit": u"af64ae7188685f8424040b4735ad12941b980d75", - "ref": u"refs/heads/master", - "git_url": u"git@bitbucket.org:jscoreos/another-repo.git", - "commit_info": { - "url": u"https://bitbucket.org/jscoreos/another-repo/commits/af64ae7188685f8424040b4735ad12941b980d75", - "date": u"2015-09-10T20:40:54+00:00", - "message": u"Dockerfile edited online with Bitbucket", - "author": { - "username": u"jscoreos", - "url": u"https://bitbucket.org/jscoreos/", - "avatar_url": u"https://bitbucket.org/account/jscoreos/avatar/32/", - }, - "committer": { - "username": u"jscoreos", - "url": u"https://bitbucket.org/jscoreos/", - "avatar_url": u"https://bitbucket.org/account/jscoreos/avatar/32/", - }, - }, - } - - self.assertSchema('bitbucket_webhook', expected, bb_webhook) - - - def test_github_webhook_payload_slash_branch(self): - expected = { - 'commit': u'410f4cdf8ff09b87f245b13845e8497f90b90a4c', - 'ref': u'refs/heads/slash/branch', - 'git_url': u'git@github.com:josephschorr/anothertest.git', - 'commit_info': { - 'url': u'https://github.com/josephschorr/anothertest/commit/410f4cdf8ff09b87f245b13845e8497f90b90a4c', - 'date': u'2015-09-11T14:26:16-04:00', - 'message': u'Update Dockerfile', - 'committer': { - 'username': u'josephschorr', - }, - 'author': { - 'username': u'josephschorr', - }, - }, - } - - self.assertSchema('github_webhook_slash_branch', expected, gh_webhook) - - - def test_github_webhook_payload(self): - expected = { - 'commit': u'410f4cdf8ff09b87f245b13845e8497f90b90a4c', - 'ref': u'refs/heads/master', - 'git_url': u'git@github.com:josephschorr/anothertest.git', - 'commit_info': { - 'url': u'https://github.com/josephschorr/anothertest/commit/410f4cdf8ff09b87f245b13845e8497f90b90a4c', - 'date': u'2015-09-11T14:26:16-04:00', - 'message': u'Update Dockerfile', - 'committer': { - 'username': u'josephschorr', - }, - 'author': { - 'username': u'josephschorr', - }, - }, - } - - self.assertSchema('github_webhook', expected, gh_webhook) - - - def test_github_webhook_payload_with_lookup(self): - expected = { - 'commit': u'410f4cdf8ff09b87f245b13845e8497f90b90a4c', - 'ref': u'refs/heads/master', - 'git_url': u'git@github.com:josephschorr/anothertest.git', - 'commit_info': { - 'url': u'https://github.com/josephschorr/anothertest/commit/410f4cdf8ff09b87f245b13845e8497f90b90a4c', - 'date': u'2015-09-11T14:26:16-04:00', - 'message': u'Update Dockerfile', - 'committer': { - 'username': u'josephschorr', - 'url': u'http://github.com/josephschorr', - 'avatar_url': u'http://some/avatar/url', - }, - 'author': { - 'username': u'josephschorr', - 'url': u'http://github.com/josephschorr', - 'avatar_url': u'http://some/avatar/url', - }, - }, - } - - def lookup_user(_): - return { - 'html_url': 'http://github.com/josephschorr', - 'avatar_url': 'http://some/avatar/url' - } - - self.assertSchema('github_webhook', expected, gh_webhook, lookup_user=lookup_user) - - - def test_github_webhook_payload_missing_fields_with_lookup(self): - expected = { - 'commit': u'410f4cdf8ff09b87f245b13845e8497f90b90a4c', - 'ref': u'refs/heads/master', - 'git_url': u'git@github.com:josephschorr/anothertest.git', - 'commit_info': { - 'url': u'https://github.com/josephschorr/anothertest/commit/410f4cdf8ff09b87f245b13845e8497f90b90a4c', - 'date': u'2015-09-11T14:26:16-04:00', - 'message': u'Update Dockerfile' - }, - } - - def lookup_user(username): - if not username: - raise Exception('Fail!') - - return { - 'html_url': 'http://github.com/josephschorr', - 'avatar_url': 'http://some/avatar/url' - } - - self.assertSchema('github_webhook_missing', expected, gh_webhook, lookup_user=lookup_user) - - - def test_gitlab_webhook_payload(self): - expected = { - 'commit': u'fb88379ee45de28a0a4590fddcbd8eff8b36026e', - 'ref': u'refs/heads/master', - 'git_url': u'git@gitlab.com:jzelinskie/www-gitlab-com.git', - 'commit_info': { - 'url': u'https://gitlab.com/jzelinskie/www-gitlab-com/commit/fb88379ee45de28a0a4590fddcbd8eff8b36026e', - 'date': u'2015-08-13T19:33:18+00:00', - 'message': u'Fix link\n', - }, - } - - self.assertSchema('gitlab_webhook', expected, gl_webhook) - - - def test_github_webhook_payload_known_issue(self): - expected = { - "commit": "118b07121695d9f2e40a5ff264fdcc2917680870", - "ref": "refs/heads/master", - "git_url": "git@github.com:silas/docker-test.git", - "commit_info": { - "url": "https://github.com/silas/docker-test/commit/118b07121695d9f2e40a5ff264fdcc2917680870", - "date": "2015-09-25T14:55:11-04:00", - "message": "Fail", - }, - } - - self.assertSchema('github_webhook_noname', expected, gh_webhook) - - - def test_github_webhook_payload_missing_fields(self): - expected = { - 'commit': u'410f4cdf8ff09b87f245b13845e8497f90b90a4c', - 'ref': u'refs/heads/master', - 'git_url': u'git@github.com:josephschorr/anothertest.git', - 'commit_info': { - 'url': u'https://github.com/josephschorr/anothertest/commit/410f4cdf8ff09b87f245b13845e8497f90b90a4c', - 'date': u'2015-09-11T14:26:16-04:00', - 'message': u'Update Dockerfile' - }, - } - - self.assertSchema('github_webhook_missing', expected, gh_webhook) - - - def test_gitlab_webhook_nocommit_payload(self): - self.assertSkipped('gitlab_webhook_nocommit', gl_webhook) - - - def test_gitlab_webhook_multiple_commits(self): - expected = { - 'commit': u'9a052a0b2fbe01d4a1a88638dd9fe31c1c56ef53', - 'ref': u'refs/heads/master', - 'git_url': u'git@gitlab.com:joseph.schorr/some-test-project.git', - 'commit_info': { - 'url': u'https://gitlab.com/joseph.schorr/some-test-project/commit/9a052a0b2fbe01d4a1a88638dd9fe31c1c56ef53', - 'date': u'2016-09-29T15:02:41+00:00', - 'message': u"Merge branch 'foobar' into 'master'\r\n\r\nAdd changelog\r\n\r\nSome merge thing\r\n\r\nSee merge request !1", - 'author': { - 'username': 'josephschorr', - 'url': 'http://gitlab.com/josephschorr', - 'avatar_url': 'http://some/avatar/url' - }, - }, - } - - def lookup_user(_): - return { - 'username': 'josephschorr', - 'html_url': 'http://gitlab.com/josephschorr', - 'avatar_url': 'http://some/avatar/url', - } - - self.assertSchema('gitlab_webhook_multicommit', expected, gl_webhook, lookup_user=lookup_user) - - - def test_gitlab_webhook_for_tag(self): - expected = { - 'commit': u'82b3d5ae55f7080f1e6022629cdb57bfae7cccc7', - 'commit_info': { - 'author': { - 'avatar_url': 'http://some/avatar/url', - 'url': 'http://gitlab.com/jzelinskie', - 'username': 'jzelinskie' - }, - 'date': '2015-08-13T19:33:18+00:00', - 'message': 'Fix link\n', - 'url': 'https://some/url', - }, - 'git_url': u'git@example.com:jsmith/example.git', - 'ref': u'refs/tags/v1.0.0', - } - - def lookup_user(_): - return { - 'username': 'jzelinskie', - 'html_url': 'http://gitlab.com/jzelinskie', - 'avatar_url': 'http://some/avatar/url', - } - - def lookup_commit(repo_id, commit_sha): - if commit_sha == '82b3d5ae55f7080f1e6022629cdb57bfae7cccc7': - return { - "id": "82b3d5ae55f7080f1e6022629cdb57bfae7cccc7", - "message": "Fix link\n", - "timestamp": "2015-08-13T19:33:18+00:00", - "url": "https://some/url", - "author_name": "Foo Guy", - "author_email": "foo@bar.com", - } - - return None - - self.assertSchema('gitlab_webhook_tag', expected, gl_webhook, lookup_user=lookup_user, - lookup_commit=lookup_commit) - - - def test_gitlab_webhook_for_tag_nocommit(self): - expected = { - 'commit': u'82b3d5ae55f7080f1e6022629cdb57bfae7cccc7', - 'git_url': u'git@example.com:jsmith/example.git', - 'ref': u'refs/tags/v1.0.0', - } - - def lookup_user(_): - return { - 'username': 'jzelinskie', - 'html_url': 'http://gitlab.com/jzelinskie', - 'avatar_url': 'http://some/avatar/url', - } - - self.assertSchema('gitlab_webhook_tag', expected, gl_webhook, lookup_user=lookup_user) - - - def test_gitlab_webhook_for_other(self): - self.assertSkipped('gitlab_webhook_other', gl_webhook) - - - def test_gitlab_webhook_payload_with_lookup(self): - expected = { - 'commit': u'fb88379ee45de28a0a4590fddcbd8eff8b36026e', - 'ref': u'refs/heads/master', - 'git_url': u'git@gitlab.com:jzelinskie/www-gitlab-com.git', - 'commit_info': { - 'url': u'https://gitlab.com/jzelinskie/www-gitlab-com/commit/fb88379ee45de28a0a4590fddcbd8eff8b36026e', - 'date': u'2015-08-13T19:33:18+00:00', - 'message': u'Fix link\n', - 'author': { - 'username': 'jzelinskie', - 'url': 'http://gitlab.com/jzelinskie', - 'avatar_url': 'http://some/avatar/url', - }, - }, - } - - def lookup_user(_): - return { - 'username': 'jzelinskie', - 'html_url': 'http://gitlab.com/jzelinskie', - 'avatar_url': 'http://some/avatar/url', - } - - self.assertSchema('gitlab_webhook', expected, gl_webhook, lookup_user=lookup_user) - - - def test_github_webhook_payload_deleted_commit(self): - expected = { - 'commit': u'456806b662cb903a0febbaed8344f3ed42f27bab', - 'commit_info': { - 'author': { - 'username': u'jakedt' - }, - 'committer': { - 'username': u'jakedt' - }, - 'date': u'2015-12-08T18:07:03-05:00', - 'message': (u'Merge pull request #1044 from jakedt/errerror\n\n' + - 'Assign the exception to a variable to log it'), - 'url': u'https://github.com/coreos-inc/quay/commit/456806b662cb903a0febbaed8344f3ed42f27bab' - }, - 'git_url': u'git@github.com:coreos-inc/quay.git', - 'ref': u'refs/heads/master', - } - - def lookup_user(_): - return None - - self.assertSchema('github_webhook_deletedcommit', expected, gh_webhook, lookup_user=lookup_user) - - def test_github_webhook_known_issue(self): - def lookup_user(_): - return None - - self.assertSkipped('github_webhook_knownissue', gh_webhook, lookup_user=lookup_user) - - - def test_bitbucket_webhook_known_issue(self): - self.assertSkipped('bitbucket_knownissue', bb_webhook) - -if __name__ == '__main__': - unittest.main()