blob: da027c1df4e1e52ffce74dcf614024df16096061 [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
James E. Blairc49e5e72017-03-16 14:56:32 -070018import os
Sean Daguea8311bf2014-09-30 06:28:26 -040019import json
Monty Taylorb934c1a2017-06-16 19:31:47 -050020import urllib
Morgan Fainberg293f7f82016-05-30 14:01:22 -070021
Jan Hruban7083edd2015-08-21 14:00:54 +020022import webob
Sean Daguea8311bf2014-09-30 06:28:26 -040023
James E. Blairc49e5e72017-03-16 14:56:32 -070024from tests.base import ZuulTestCase, FIXTURE_DIR
Sean Daguea8311bf2014-09-30 06:28:26 -040025
26
27class TestWebapp(ZuulTestCase):
Paul Belanger6349d152016-10-30 16:21:17 -040028 tenant_config_file = 'config/single-tenant/main.yaml'
Sean Daguea8311bf2014-09-30 06:28:26 -040029
Sean Daguea8311bf2014-09-30 06:28:26 -040030 def setUp(self):
Sean Daguea8311bf2014-09-30 06:28:26 -040031 super(TestWebapp, self).setUp()
Paul Belanger174a8272017-03-14 13:20:10 -040032 self.executor_server.hold_jobs_in_build = True
Sean Daguea8311bf2014-09-30 06:28:26 -040033 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger6349d152016-10-30 16:21:17 -040034 A.addApproval('code-review', 2)
35 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Sean Daguea8311bf2014-09-30 06:28:26 -040036 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Clint Byrum77c184a2016-12-20 12:27:21 -080037 B.addApproval('code-review', 2)
Paul Belanger6349d152016-10-30 16:21:17 -040038 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Sean Daguea8311bf2014-09-30 06:28:26 -040039 self.waitUntilSettled()
40 self.port = self.webapp.server.socket.getsockname()[1]
41
James E. Blaire18d4602017-01-05 11:17:28 -080042 def tearDown(self):
Paul Belanger174a8272017-03-14 13:20:10 -040043 self.executor_server.hold_jobs_in_build = False
44 self.executor_server.release()
James E. Blaire18d4602017-01-05 11:17:28 -080045 self.waitUntilSettled()
46 super(TestWebapp, self).tearDown()
47
Sean Daguea8311bf2014-09-30 06:28:26 -040048 def test_webapp_status(self):
49 "Test that we can filter to only certain changes in the webapp."
50
Morgan Fainberg293f7f82016-05-30 14:01:22 -070051 req = urllib.request.Request(
Paul Belanger6349d152016-10-30 16:21:17 -040052 "http://localhost:%s/tenant-one/status" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070053 f = urllib.request.urlopen(req)
Clint Byrum1e477c92017-05-10 20:53:54 -070054 data = json.loads(f.read().decode('utf8'))
Sean Daguea8311bf2014-09-30 06:28:26 -040055
56 self.assertIn('pipelines', data)
57
58 def test_webapp_status_compat(self):
59 # testing compat with status.json
Morgan Fainberg293f7f82016-05-30 14:01:22 -070060 req = urllib.request.Request(
Paul Belanger6349d152016-10-30 16:21:17 -040061 "http://localhost:%s/tenant-one/status.json" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070062 f = urllib.request.urlopen(req)
Clint Byrum1e477c92017-05-10 20:53:54 -070063 data = json.loads(f.read().decode('utf8'))
Sean Daguea8311bf2014-09-30 06:28:26 -040064
65 self.assertIn('pipelines', data)
66
67 def test_webapp_bad_url(self):
68 # do we 404 correctly
Morgan Fainberg293f7f82016-05-30 14:01:22 -070069 req = urllib.request.Request(
Sean Daguea8311bf2014-09-30 06:28:26 -040070 "http://localhost:%s/status/foo" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070071 self.assertRaises(urllib.error.HTTPError, urllib.request.urlopen, req)
Sean Daguea8311bf2014-09-30 06:28:26 -040072
73 def test_webapp_find_change(self):
74 # can we filter by change id
Morgan Fainberg293f7f82016-05-30 14:01:22 -070075 req = urllib.request.Request(
Clint Byrum77c184a2016-12-20 12:27:21 -080076 "http://localhost:%s/tenant-one/status/change/1,1" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070077 f = urllib.request.urlopen(req)
Clint Byrum1e477c92017-05-10 20:53:54 -070078 data = json.loads(f.read().decode('utf8'))
Sean Daguea8311bf2014-09-30 06:28:26 -040079
80 self.assertEqual(1, len(data), data)
81 self.assertEqual("org/project", data[0]['project'])
82
Morgan Fainberg293f7f82016-05-30 14:01:22 -070083 req = urllib.request.Request(
Clint Byrum77c184a2016-12-20 12:27:21 -080084 "http://localhost:%s/tenant-one/status/change/2,1" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070085 f = urllib.request.urlopen(req)
Clint Byrum1e477c92017-05-10 20:53:54 -070086 data = json.loads(f.read().decode('utf8'))
Sean Daguea8311bf2014-09-30 06:28:26 -040087
88 self.assertEqual(1, len(data), data)
89 self.assertEqual("org/project1", data[0]['project'], data)
James E. Blairc49e5e72017-03-16 14:56:32 -070090
91 def test_webapp_keys(self):
Clint Byrum1e477c92017-05-10 20:53:54 -070092 with open(os.path.join(FIXTURE_DIR, 'public.pem'), 'rb') as f:
James E. Blairc49e5e72017-03-16 14:56:32 -070093 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 Hruban7083edd2015-08-21 14:00:54 +0200100
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)
Clint Byrum1e477c92017-05-10 20:53:54 -0700109 self.assertEqual(b'ok', f.read())
Jan Hruban7083edd2015-08-21 14:00:54 +0200110
111 self.webapp.unregister_path('/custom')
112 self.assertRaises(urllib.error.HTTPError, urllib.request.urlopen, req)