| 1 | import contextlib |
| 2 | import datetime |
| 3 | import os |
| 4 | import sys |
| 5 | import unittest |
| 6 | |
| 7 | import flask_migrate |
| 8 | import flask_testing |
| 9 | |
| 10 | from ui import config |
| 11 | from ui.app import app |
| 12 | from ui.models import Build, Runner, db |
| 13 | |
| 14 | class UsesApp(flask_testing.TestCase): |
| 15 | def create_app(self): |
| 16 | return app |
| 17 | |
| 18 | class UsesModels(UsesApp): |
| 19 | def setUp(self): |
| 20 | with open(os.devnull, "w") as f, contextlib.redirect_stderr(f): |
| 21 | flask_migrate.upgrade(revision="head") |
| 22 | |
| 23 | def tearDown(self): |
| 24 | with open(os.devnull, "w") as f, contextlib.redirect_stderr(f): |
| 25 | flask_migrate.downgrade(revision="base") |
| 26 | |
| 27 | class TestModels(UsesModels): |
| 28 | |
| 29 | def test_build(self): |
| 30 | build = Build(build_id=1) |
| 31 | db.session.add(build) |
| 32 | db.session.commit() |
| 33 | |
| 34 | assert build in db.session |
| 35 | |
| 36 | def test_get_or_create_build_by_id(self): |
| 37 | build = Build(build_id=1) |
| 38 | db.session.add(build) |
| 39 | db.session.commit() |
| 40 | |
| 41 | assert Build.get_or_create_by_id(1).build_id == 1 |
| 42 | assert Build.get_or_create_by_id(2).build_id == 2 |
| 43 | |
| 44 | def test_runner(self): |
| 45 | runner = Runner(runner_id="foobar") |
| 46 | db.session.add(runner) |
| 47 | db.session.commit() |
| 48 | |
| 49 | assert runner in db.session |
| 50 | |
| 51 | def test_get_or_create_runner_by_id(self): |
| 52 | runner = Runner(runner_id="foobar", runner_name="foobar") |
| 53 | db.session.add(runner) |
| 54 | db.session.commit() |
| 55 | |
| 56 | assert Runner.get_or_create_by_id('foobar').runner_name == "foobar" |
| 57 | assert Runner.get_or_create_by_id("foobaz").runner_name == None |
| 58 | |
| 59 | def test_runner_build_rel(self): |
| 60 | runner = Runner(runner_id="foobar", runner_name="foobar") |
| 61 | build = Build(build_id=1, build_runner=runner) |
| 62 | db.session.add(runner) |
| 63 | db.session.add(build) |
| 64 | db.session.commit() |
| 65 | |
| 66 | assert build.build_runner == runner |
| 67 | assert build in runner.builds |
| 68 | |
| 69 | class TestWebhooks(UsesModels): |
| 70 | |
| 71 | def test_invalid_token(self): |
| 72 | with open("tests/pipeline_webhook.json", "r") as f: |
| 73 | response = self.client.post("/webhook", headers={'X-Gitlab-Event': 'Pipeline Hook', 'X-Gitlab-Token': 'invalidsecret'}, data=f.read(), content_type='application/json') |
| 74 | assert response.status_code == 403 |
| 75 | |
| 76 | def test_pipeline_webhook(self): |
| 77 | with open("tests/pipeline_webhook.json", "r") as f: |
| 78 | response = self.client.post("/webhook", headers={'X-Gitlab-Event': 'Pipeline Hook', 'X-Gitlab-Token': 'secret'}, data=f.read(), content_type='application/json') |
| 79 | assert response.status_code == 200 |
| 80 | build = Build.query.filter_by(build_id=1).first() |
| 81 | assert build.build_id == 1 |
| 82 | assert build.build_runner_id == None |
| 83 | |
| 84 | def test_success(self): |
| 85 | with open("tests/pipeline_success.json") as f: |
| 86 | response = self.client.post("/webhook", headers={'X-Gitlab-Event': 'Pipeline Hook', 'X-Gitlab-Token': 'secret'}, data=f.read(), content_type='application/json') |
| 87 | assert response.status_code == 200 |
| 88 | runner = Runner.query.filter_by(runner_id="530365").first() |
| 89 | build = Build.query.filter_by(build_id=34618162).first() |
| 90 | assert build.build_runner.runner_name == "phenom-test" |
| 91 | assert build.build_version == "lineage-15.1" |
| 92 | assert build.build_device == "FAKE_TEST" |
| 93 | assert build.build_status == "success" |
| 94 | assert build.build_duration == 81 |
| 95 | |
| 96 | |
| 97 | def test_start(self): |
| 98 | with open("tests/pipeline_start.json") as f: |
| 99 | response = self.client.post("/webhook", headers={'X-Gitlab-Event': 'Pipeline Hook', 'X-Gitlab-Token': 'secret'}, data=f.read(), content_type='application/json') |
| 100 | assert response.status_code == 200 |
| 101 | |
| 102 | |
| 103 | class TestWeb(UsesModels): |
| 104 | |
| 105 | def test_home(self): |
| 106 | build1 = Build(build_id=1) |
| 107 | runner = Runner(runner_name="foobar", runner_id="foobar") |
| 108 | build2 = Build(build_id=2, build_status="success", build_device="mako", build_version="cm-14.1", build_type="userdebug", build_date=datetime.datetime.now(), build_runner=runner) |
| 109 | db.session.add(build1) |
| 110 | db.session.add(runner) |
| 111 | db.session.add(build2) |
| 112 | db.session.commit() |
| 113 | response = self.client.get("/") |
| 114 | assert response.status_code == 200 |
| 115 | |
| 116 | if __name__ == "__main__": |
| 117 | unittest.main() |