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 | |
| 18 | import json |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 19 | |
| 20 | from six.moves import urllib |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 21 | |
| 22 | from tests.base import ZuulTestCase |
| 23 | |
| 24 | |
| 25 | class TestWebapp(ZuulTestCase): |
| 26 | |
| 27 | def _cleanup(self): |
| 28 | self.worker.hold_jobs_in_build = False |
| 29 | self.worker.release() |
| 30 | self.waitUntilSettled() |
| 31 | |
| 32 | def setUp(self): |
| 33 | super(TestWebapp, self).setUp() |
| 34 | self.addCleanup(self._cleanup) |
| 35 | self.worker.hold_jobs_in_build = True |
| 36 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 37 | A.addApproval('CRVW', 2) |
| 38 | self.fake_gerrit.addEvent(A.addApproval('APRV', 1)) |
| 39 | B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B') |
| 40 | B.addApproval('CRVW', 2) |
| 41 | self.fake_gerrit.addEvent(B.addApproval('APRV', 1)) |
| 42 | self.waitUntilSettled() |
| 43 | self.port = self.webapp.server.socket.getsockname()[1] |
| 44 | |
| 45 | def test_webapp_status(self): |
| 46 | "Test that we can filter to only certain changes in the webapp." |
| 47 | |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 48 | req = urllib.request.Request( |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 49 | "http://localhost:%s/status" % self.port) |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 50 | f = urllib.request.urlopen(req) |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 51 | data = json.loads(f.read()) |
| 52 | |
| 53 | self.assertIn('pipelines', data) |
| 54 | |
| 55 | def test_webapp_status_compat(self): |
| 56 | # testing compat with status.json |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 57 | req = urllib.request.Request( |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 58 | "http://localhost:%s/status.json" % self.port) |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 59 | f = urllib.request.urlopen(req) |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 60 | data = json.loads(f.read()) |
| 61 | |
| 62 | self.assertIn('pipelines', data) |
| 63 | |
| 64 | def test_webapp_bad_url(self): |
| 65 | # do we 404 correctly |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 66 | req = urllib.request.Request( |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 67 | "http://localhost:%s/status/foo" % self.port) |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 68 | self.assertRaises(urllib.error.HTTPError, urllib.request.urlopen, req) |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 69 | |
| 70 | def test_webapp_find_change(self): |
| 71 | # can we filter by change id |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 72 | req = urllib.request.Request( |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 73 | "http://localhost:%s/status/change/1,1" % self.port) |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 74 | f = urllib.request.urlopen(req) |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 75 | data = json.loads(f.read()) |
| 76 | |
| 77 | self.assertEqual(1, len(data), data) |
| 78 | self.assertEqual("org/project", data[0]['project']) |
| 79 | |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 80 | req = urllib.request.Request( |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 81 | "http://localhost:%s/status/change/2,1" % self.port) |
Morgan Fainberg | 293f7f8 | 2016-05-30 14:01:22 -0700 | [diff] [blame] | 82 | f = urllib.request.urlopen(req) |
Sean Dague | a8311bf | 2014-09-30 06:28:26 -0400 | [diff] [blame] | 83 | data = json.loads(f.read()) |
| 84 | |
| 85 | self.assertEqual(1, len(data), data) |
| 86 | self.assertEqual("org/project1", data[0]['project'], data) |