The Gatekeeper, or a project gating system
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

125 lines
4.1 KiB

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