blob: 2211d1be4a0fe6693ffb4bbb7fcf69a08108310a [file] [log] [blame]
Sean Daguea8311bf2014-09-30 06:28:26 -04001#!/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
18import json
Morgan Fainberg293f7f82016-05-30 14:01:22 -070019
20from six.moves import urllib
Sean Daguea8311bf2014-09-30 06:28:26 -040021
22from tests.base import ZuulTestCase
23
24
25class TestWebapp(ZuulTestCase):
Paul Belanger6349d152016-10-30 16:21:17 -040026 tenant_config_file = 'config/single-tenant/main.yaml'
Sean Daguea8311bf2014-09-30 06:28:26 -040027
Sean Daguea8311bf2014-09-30 06:28:26 -040028 def setUp(self):
Sean Daguea8311bf2014-09-30 06:28:26 -040029 super(TestWebapp, self).setUp()
Paul Belanger6349d152016-10-30 16:21:17 -040030 self.launch_server.hold_jobs_in_build = True
Sean Daguea8311bf2014-09-30 06:28:26 -040031 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger6349d152016-10-30 16:21:17 -040032 A.addApproval('code-review', 2)
33 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Sean Daguea8311bf2014-09-30 06:28:26 -040034 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Clint Byrum77c184a2016-12-20 12:27:21 -080035 B.addApproval('code-review', 2)
Paul Belanger6349d152016-10-30 16:21:17 -040036 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Sean Daguea8311bf2014-09-30 06:28:26 -040037 self.waitUntilSettled()
38 self.port = self.webapp.server.socket.getsockname()[1]
39
James E. Blaire18d4602017-01-05 11:17:28 -080040 def tearDown(self):
41 self.launch_server.hold_jobs_in_build = False
42 self.launch_server.release()
43 self.waitUntilSettled()
44 super(TestWebapp, self).tearDown()
45
Sean Daguea8311bf2014-09-30 06:28:26 -040046 def test_webapp_status(self):
47 "Test that we can filter to only certain changes in the webapp."
48
Morgan Fainberg293f7f82016-05-30 14:01:22 -070049 req = urllib.request.Request(
Paul Belanger6349d152016-10-30 16:21:17 -040050 "http://localhost:%s/tenant-one/status" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070051 f = urllib.request.urlopen(req)
Sean Daguea8311bf2014-09-30 06:28:26 -040052 data = json.loads(f.read())
53
54 self.assertIn('pipelines', data)
55
56 def test_webapp_status_compat(self):
57 # testing compat with status.json
Morgan Fainberg293f7f82016-05-30 14:01:22 -070058 req = urllib.request.Request(
Paul Belanger6349d152016-10-30 16:21:17 -040059 "http://localhost:%s/tenant-one/status.json" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070060 f = urllib.request.urlopen(req)
Sean Daguea8311bf2014-09-30 06:28:26 -040061 data = json.loads(f.read())
62
63 self.assertIn('pipelines', data)
64
65 def test_webapp_bad_url(self):
66 # do we 404 correctly
Morgan Fainberg293f7f82016-05-30 14:01:22 -070067 req = urllib.request.Request(
Sean Daguea8311bf2014-09-30 06:28:26 -040068 "http://localhost:%s/status/foo" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070069 self.assertRaises(urllib.error.HTTPError, urllib.request.urlopen, req)
Sean Daguea8311bf2014-09-30 06:28:26 -040070
71 def test_webapp_find_change(self):
72 # can we filter by change id
Morgan Fainberg293f7f82016-05-30 14:01:22 -070073 req = urllib.request.Request(
Clint Byrum77c184a2016-12-20 12:27:21 -080074 "http://localhost:%s/tenant-one/status/change/1,1" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070075 f = urllib.request.urlopen(req)
Sean Daguea8311bf2014-09-30 06:28:26 -040076 data = json.loads(f.read())
77
78 self.assertEqual(1, len(data), data)
79 self.assertEqual("org/project", data[0]['project'])
80
Morgan Fainberg293f7f82016-05-30 14:01:22 -070081 req = urllib.request.Request(
Clint Byrum77c184a2016-12-20 12:27:21 -080082 "http://localhost:%s/tenant-one/status/change/2,1" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070083 f = urllib.request.urlopen(req)
Sean Daguea8311bf2014-09-30 06:28:26 -040084 data = json.loads(f.read())
85
86 self.assertEqual(1, len(data), data)
87 self.assertEqual("org/project1", data[0]['project'], data)