blob: 94f097a772eae7823f22ee88333dd8d204af08a7 [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):
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 Fainberg293f7f82016-05-30 14:01:22 -070048 req = urllib.request.Request(
Sean Daguea8311bf2014-09-30 06:28:26 -040049 "http://localhost:%s/status" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070050 f = urllib.request.urlopen(req)
Sean Daguea8311bf2014-09-30 06:28:26 -040051 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 Fainberg293f7f82016-05-30 14:01:22 -070057 req = urllib.request.Request(
Sean Daguea8311bf2014-09-30 06:28:26 -040058 "http://localhost:%s/status.json" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070059 f = urllib.request.urlopen(req)
Sean Daguea8311bf2014-09-30 06:28:26 -040060 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 Fainberg293f7f82016-05-30 14:01:22 -070066 req = urllib.request.Request(
Sean Daguea8311bf2014-09-30 06:28:26 -040067 "http://localhost:%s/status/foo" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070068 self.assertRaises(urllib.error.HTTPError, urllib.request.urlopen, req)
Sean Daguea8311bf2014-09-30 06:28:26 -040069
70 def test_webapp_find_change(self):
71 # can we filter by change id
Morgan Fainberg293f7f82016-05-30 14:01:22 -070072 req = urllib.request.Request(
Sean Daguea8311bf2014-09-30 06:28:26 -040073 "http://localhost:%s/status/change/1,1" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070074 f = urllib.request.urlopen(req)
Sean Daguea8311bf2014-09-30 06:28:26 -040075 data = json.loads(f.read())
76
77 self.assertEqual(1, len(data), data)
78 self.assertEqual("org/project", data[0]['project'])
79
Morgan Fainberg293f7f82016-05-30 14:01:22 -070080 req = urllib.request.Request(
Sean Daguea8311bf2014-09-30 06:28:26 -040081 "http://localhost:%s/status/change/2,1" % self.port)
Morgan Fainberg293f7f82016-05-30 14:01:22 -070082 f = urllib.request.urlopen(req)
Sean Daguea8311bf2014-09-30 06:28:26 -040083 data = json.loads(f.read())
84
85 self.assertEqual(1, len(data), data)
86 self.assertEqual("org/project1", data[0]['project'], data)