Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright 2014 Hewlett-Packard Development Company, L.P. |
| 4 | # Copyright 2014 Rackspace Australia |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
James E. Blair | c49e5e7 | 2017-03-16 14:56:32 -0700 | [diff] [blame] | 18 | import os |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 19 | import json |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 20 | |
| 21 | from six.moves import urllib |
Jan Hruban | 7083edd | 2015-08-21 14:00:54 +0200 | [diff] [blame] | 22 | import webob |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 23 | |
James E. Blair | c49e5e7 | 2017-03-16 14:56:32 -0700 | [diff] [blame] | 24 | from tests.base import ZuulTestCase, FIXTURE_DIR |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 25 | |
| 26 | |
| 27 | class TestWebapp(ZuulTestCase): |
Paul Belanger | 6349d15 | 2016-10-30 16:21:17 -0400 | [diff] [blame] | 28 | tenant_config_file = 'config/single-tenant/main.yaml' |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 29 | |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 30 | def setUp(self): |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 31 | super(TestWebapp, self).setUp() |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 32 | self.executor_server.hold_jobs_in_build = True |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 33 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
Paul Belanger | 6349d15 | 2016-10-30 16:21:17 -0400 | [diff] [blame] | 34 | A.addApproval('code-review', 2) |
| 35 | self.fake_gerrit.addEvent(A.addApproval('approved', 1)) |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 36 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
Clint Byrum | 77c184a | 2016-12-20 12:27:21 -0800 | [diff] [blame] | 37 | B.addApproval('code-review', 2) |
Paul Belanger | 6349d15 | 2016-10-30 16:21:17 -0400 | [diff] [blame] | 38 | self.fake_gerrit.addEvent(B.addApproval('approved', 1)) |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 39 | self.waitUntilSettled() |
| 40 | self.port = self.webapp.server.socket.getsockname()[1] |
| 41 | |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 42 | def tearDown(self): |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 43 | self.executor_server.hold_jobs_in_build = False |
| 44 | self.executor_server.release() |
James E. Blair | e18d460 | 2017-01-05 11:17:28 -0800 | [diff] [blame] | 45 | self.waitUntilSettled() |
| 46 | super(TestWebapp, self).tearDown() |
| 47 | |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 48 | def test_webapp_status(self): |
| 49 | "Test that we can filter to only certain changes in the webapp." |
| 50 | |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 51 | req = urllib.request.Request( |
Paul Belanger | 6349d15 | 2016-10-30 16:21:17 -0400 | [diff] [blame] | 52 | "http://localhost:%s/tenant-one/status" % self.port) |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 53 | f = urllib.request.urlopen(req) |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 54 | data = json.loads(f.read()) |
| 55 | |
| 56 | self.assertIn('pipelines', data) |
| 57 | |
| 58 | def test_webapp_status_compat(self): |
| 59 | # testing compat with status.json |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 60 | req = urllib.request.Request( |
Paul Belanger | 6349d15 | 2016-10-30 16:21:17 -0400 | [diff] [blame] | 61 | "http://localhost:%s/tenant-one/status.json" % self.port) |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 62 | f = urllib.request.urlopen(req) |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 63 | data = json.loads(f.read()) |
| 64 | |
| 65 | self.assertIn('pipelines', data) |
| 66 | |
| 67 | def test_webapp_bad_url(self): |
| 68 | # do we 404 correctly |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 69 | req = urllib.request.Request( |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 70 | "http://localhost:%s/status/foo" % self.port) |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 71 | self.assertRaises(urllib.error.HTTPError, urllib.request.urlopen, req) |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 72 | |
| 73 | def test_webapp_find_change(self): |
| 74 | # can we filter by change id |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 75 | req = urllib.request.Request( |
Clint Byrum | 77c184a | 2016-12-20 12:27:21 -0800 | [diff] [blame] | 76 | "http://localhost:%s/tenant-one/status/change/1,1" % self.port) |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 77 | f = urllib.request.urlopen(req) |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 78 | data = json.loads(f.read()) |
| 79 | |
| 80 | self.assertEqual(1, len(data), data) |
| 81 | self.assertEqual("org/project", data[0]['project']) |
| 82 | |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 83 | req = urllib.request.Request( |
Clint Byrum | 77c184a | 2016-12-20 12:27:21 -0800 | [diff] [blame] | 84 | "http://localhost:%s/tenant-one/status/change/2,1" % self.port) |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 85 | f = urllib.request.urlopen(req) |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 86 | data = json.loads(f.read()) |
| 87 | |
| 88 | self.assertEqual(1, len(data), data) |
| 89 | self.assertEqual("org/project1", data[0]['project'], data) |
James E. Blair | c49e5e7 | 2017-03-16 14:56:32 -0700 | [diff] [blame] | 90 | |
| 91 | def test_webapp_keys(self): |
| 92 | with open(os.path.join(FIXTURE_DIR, 'public.pem')) as f: |
| 93 | public_pem = f.read() |
| 94 | |
| 95 | req = urllib.request.Request( |
| 96 | "http://localhost:%s/tenant-one/keys/gerrit/org/project.pub" % |
| 97 | self.port) |
| 98 | f = urllib.request.urlopen(req) |
| 99 | self.assertEqual(f.read(), public_pem) |
Jan Hruban | 7083edd | 2015-08-21 14:00:54 +0200 | [diff] [blame] | 100 | |
| 101 | def test_webapp_custom_handler(self): |
| 102 | def custom_handler(path, tenant_name, request): |
| 103 | return webob.Response(body='ok') |
| 104 | |
| 105 | self.webapp.register_path('/custom', custom_handler) |
| 106 | req = urllib.request.Request( |
| 107 | "http://localhost:%s/custom" % self.port) |
| 108 | f = urllib.request.urlopen(req) |
| 109 | self.assertEqual('ok', f.read()) |
| 110 | |
| 111 | self.webapp.unregister_path('/custom') |
| 112 | self.assertRaises(urllib.error.HTTPError, urllib.request.urlopen, req) |