blob: 8791a257eb09ec872788a04eba4cbf96e92f764a [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
Morgan Fainberg293f7f82016-05-30 14:01:22 -070020
21from six.moves import urllib
Sean Daguea8311bf2014-09-30 06:28:26 -040022
James E. Blairc49e5e72017-03-16 14:56:32 -070023from tests.base import ZuulTestCase, FIXTURE_DIR
Sean Daguea8311bf2014-09-30 06:28:26 -040024
25
26class TestWebapp(ZuulTestCase):
Paul Belanger6349d152016-10-30 16:21:17 -040027 tenant_config_file = 'config/single-tenant/main.yaml'
Sean Daguea8311bf2014-09-30 06:28:26 -040028
Sean Daguea8311bf2014-09-30 06:28:26 -040029 def setUp(self):
Sean Daguea8311bf2014-09-30 06:28:26 -040030 super(TestWebapp, self).setUp()
Paul Belanger174a8272017-03-14 13:20:10 -040031 self.executor_server.hold_jobs_in_build = True
Sean Daguea8311bf2014-09-30 06:28:26 -040032 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
Paul Belanger6349d152016-10-30 16:21:17 -040033 A.addApproval('code-review', 2)
34 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
Sean Daguea8311bf2014-09-30 06:28:26 -040035 B = self.fake_gerrit.addFakeChange('org/project1', 'master', 'B')
Clint Byrum77c184a2016-12-20 12:27:21 -080036 B.addApproval('code-review', 2)
Paul Belanger6349d152016-10-30 16:21:17 -040037 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
Sean Daguea8311bf2014-09-30 06:28:26 -040038 self.waitUntilSettled()
39 self.port = self.webapp.server.socket.getsockname()[1]
40
James E. Blaire18d4602017-01-05 11:17:28 -080041 def tearDown(self):
Paul Belanger174a8272017-03-14 13:20:10 -040042 self.executor_server.hold_jobs_in_build = False
43 self.executor_server.release()
James E. Blaire18d4602017-01-05 11:17:28 -080044 self.waitUntilSettled()
45 super(TestWebapp, self).tearDown()
46
Sean Daguea8311bf2014-09-30 06:28:26 -040047 def test_webapp_status(self):
48 "Test that we can filter to only certain changes in the webapp."
49
Morgan Fainberg293f7f82016-05-30 14:01:22 -070050 req = urllib.request.Request(
Paul Belanger6349d152016-10-30 16:21:17 -040051 "http://localhost:%s/tenant-one/status" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070052 f = urllib.request.urlopen(req)
Sean Daguea8311bf2014-09-30 06:28:26 -040053 data = json.loads(f.read())
54
55 self.assertIn('pipelines', data)
56
57 def test_webapp_status_compat(self):
58 # testing compat with status.json
Morgan Fainberg293f7f82016-05-30 14:01:22 -070059 req = urllib.request.Request(
Paul Belanger6349d152016-10-30 16:21:17 -040060 "http://localhost:%s/tenant-one/status.json" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070061 f = urllib.request.urlopen(req)
Sean Daguea8311bf2014-09-30 06:28:26 -040062 data = json.loads(f.read())
63
64 self.assertIn('pipelines', data)
65
66 def test_webapp_bad_url(self):
67 # do we 404 correctly
Morgan Fainberg293f7f82016-05-30 14:01:22 -070068 req = urllib.request.Request(
Sean Daguea8311bf2014-09-30 06:28:26 -040069 "http://localhost:%s/status/foo" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070070 self.assertRaises(urllib.error.HTTPError, urllib.request.urlopen, req)
Sean Daguea8311bf2014-09-30 06:28:26 -040071
72 def test_webapp_find_change(self):
73 # can we filter by change id
Morgan Fainberg293f7f82016-05-30 14:01:22 -070074 req = urllib.request.Request(
Clint Byrum77c184a2016-12-20 12:27:21 -080075 "http://localhost:%s/tenant-one/status/change/1,1" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070076 f = urllib.request.urlopen(req)
Sean Daguea8311bf2014-09-30 06:28:26 -040077 data = json.loads(f.read())
78
79 self.assertEqual(1, len(data), data)
80 self.assertEqual("org/project", data[0]['project'])
81
Morgan Fainberg293f7f82016-05-30 14:01:22 -070082 req = urllib.request.Request(
Clint Byrum77c184a2016-12-20 12:27:21 -080083 "http://localhost:%s/tenant-one/status/change/2,1" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070084 f = urllib.request.urlopen(req)
Sean Daguea8311bf2014-09-30 06:28:26 -040085 data = json.loads(f.read())
86
87 self.assertEqual(1, len(data), data)
88 self.assertEqual("org/project1", data[0]['project'], data)
James E. Blairc49e5e72017-03-16 14:56:32 -070089
90 def test_webapp_keys(self):
91 with open(os.path.join(FIXTURE_DIR, 'public.pem')) as f:
92 public_pem = f.read()
93
94 req = urllib.request.Request(
95 "http://localhost:%s/tenant-one/keys/gerrit/org/project.pub" %
96 self.port)
97 f = urllib.request.urlopen(req)
98 self.assertEqual(f.read(), public_pem)