blob: 3e7f69e20684c72384e6eb32f16eaafb57504558 [file] [log] [blame]
Monty Taylor4a781a72017-07-25 07:28:04 -04001#!/usr/bin/env python
2
3# Copyright 2017 Red Hat, Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
17import urllib
18
19from bs4 import BeautifulSoup
20
21from tests.base import ZuulTestCase, WebProxyFixture
22from tests.base import ZuulWebFixture
23
24
25class TestWebURLs(object):
26 tenant_config_file = 'config/single-tenant/main.yaml'
27
28 def setUp(self):
29 super(TestWebURLs, self).setUp()
30 self.web = self.useFixture(
31 ZuulWebFixture(self.gearman_server.port))
32
33 def _get(self, port, uri):
34 url = "http://localhost:{}{}".format(port, uri)
35 self.log.debug("GET {}".format(url))
36 req = urllib.request.Request(url)
37 try:
38 f = urllib.request.urlopen(req)
39 except urllib.error.HTTPError as e:
40 raise Exception("Error on URL {}".format(url))
41 return f.read()
42
43 def _crawl(self, url):
44 page = self._get(self.port, url)
45 page = BeautifulSoup(page, 'html.parser')
46 for (tag, attr) in [
47 ('script', 'src'),
48 ('link', 'href'),
49 ('a', 'href'),
50 ('img', 'src'),
51 ]:
52 for item in page.find_all(tag):
53 suburl = item.get(attr)
54 # Skip empty urls. Also skip the navbar relative link for now.
55 # TODO(mordred) Remove when we have the top navbar link sorted.
56 if suburl is None or suburl == "../":
57 continue
58 link = urllib.parse.urljoin(url, suburl)
59 self._get(self.port, link)
60
61
62class TestDirect(TestWebURLs, ZuulTestCase):
63 # Test directly accessing the zuul-web server with no proxy
64 def setUp(self):
65 super(TestDirect, self).setUp()
66 self.port = self.web.port
67
68 def test_status_page(self):
69 self._crawl('/tenant-one/status.html')
70
71
72class TestWhiteLabel(TestWebURLs, ZuulTestCase):
73 # Test a zuul-web behind a whitelabel proxy (i.e., what
74 # zuul.openstack.org does).
75 def setUp(self):
76 super(TestWhiteLabel, self).setUp()
77 rules = [
78 ('^/(.*)$', 'http://localhost:{}/tenant-one/\\1'.format(
79 self.web.port)),
80 ]
81 self.proxy = self.useFixture(WebProxyFixture(rules))
82 self.port = self.proxy.port
83
84 def test_status_page(self):
85 self._crawl('/status.html')
86
87
88class TestSuburl(TestWebURLs, ZuulTestCase):
89 # Test a zuul-web mounted on a suburl (i.e., what software factory
90 # does).
91 def setUp(self):
92 super(TestSuburl, self).setUp()
93 rules = [
94 ('^/zuul3/(.*)$', 'http://localhost:{}/\\1'.format(
95 self.web.port)),
96 ]
97 self.proxy = self.useFixture(WebProxyFixture(rules))
98 self.port = self.proxy.port
99
100 def test_status_page(self):
101 self._crawl('/zuul3/tenant-one/status.html')