diff --git a/zaza/openstack/charm_tests/openstack_dashboard/tests.py b/zaza/openstack/charm_tests/openstack_dashboard/tests.py index df39b65..4986f7b 100644 --- a/zaza/openstack/charm_tests/openstack_dashboard/tests.py +++ b/zaza/openstack/charm_tests/openstack_dashboard/tests.py @@ -26,7 +26,6 @@ import zaza.model as zaza_model import zaza.openstack.utilities.openstack as openstack_utils import zaza.openstack.charm_tests.test_utils as test_utils -import zaza.openstack.utilities.juju as openstack_juju import zaza.openstack.utilities.generic as generic_utils import zaza.openstack.charm_tests.policyd.tests as policyd @@ -43,21 +42,6 @@ class FailedAuth(AuthExceptions): pass -def _get_dashboard_ip(): - """Get the IP of the dashboard. - - :returns: The IP of the dashboard - :rtype: str - """ - unit_name = zaza_model.get_lead_unit_name('openstack-dashboard') - keystone_unit = zaza_model.get_lead_unit_name('keystone') - dashboard_relation = openstack_juju.get_relation_from_unit( - keystone_unit, unit_name, 'identity-service') - dashboard_ip = dashboard_relation['private-address'] - logging.debug("dashboard_ip is: {}".format(dashboard_ip)) - return dashboard_ip - - # NOTE: intermittent authentication fails. Wrap in a retry loop. @tenacity.retry(wait=tenacity.wait_exponential(multiplier=1, min=5, max=10), @@ -156,6 +140,17 @@ def _login(dashboard_ip, domain, username, password): return client, response +# NOTE(ajkavanagh): it seems that apache2 doesn't start quickly enough +# for the test, and so it gets reset errors; repeat until either that +# stops or there is a failure +@tenacity.retry(wait=tenacity.wait_exponential(multiplier=1, min=5, max=10), + retry=tenacity.retry_if_exception_type( + http.client.RemoteDisconnected), + reraise=True) +def _do_request(request): + return urllib.request.urlopen(request) + + class OpenStackDashboardTests(test_utils.OpenStackBaseTest): """Encapsulate openstack dashboard charm tests.""" @@ -211,14 +206,9 @@ class OpenStackDashboardTests(test_utils.OpenStackBaseTest): def test_200_haproxy_stats_config(self): """Verify that the HAProxy stats are properly setup.""" logging.info('Checking dashboard HAProxy settings...') - application_name = 'openstack-dashboard' unit = zaza_model.get_unit_from_name( - zaza_model.get_lead_unit_name(application_name)) - keystone_unit = zaza_model.get_lead_unit_name('keystone') - dashboard_relation = openstack_juju.get_relation_from_unit( - keystone_unit, unit.entity_id, 'identity-service') - dashboard_ip = dashboard_relation['private-address'] - logging.debug("... dashboard_ip is:{}".format(dashboard_ip)) + zaza_model.get_lead_unit_name(self.application_name)) + logging.debug("... dashboard_ip is:{}".format(unit.public_address)) conf = '/etc/haproxy/haproxy.cfg' port = '8888' set_alternate = { @@ -226,26 +216,15 @@ class OpenStackDashboardTests(test_utils.OpenStackBaseTest): } request = urllib.request.Request( - 'http://{}:{}'.format(dashboard_ip, port)) - - # NOTE(ajkavanagh): it seems that apache2 doesn't start quickly enough - # for the test, and so it gets reset errors; repeat until either that - # stops or there is a failure - @tenacity.retry(wait=tenacity.wait_exponential(multiplier=1, - min=5, max=10), - retry=tenacity.retry_if_exception_type( - http.client.RemoteDisconnected), - reraise=True) - def _do_request(): - return urllib.request.urlopen(request) + 'http://{}:{}'.format(unit.public_address, port)) output = str(generic_utils.get_file_contents(unit, conf)) for line in output.split('\n'): if "stats auth" in line: password = line.split(':')[1] - base64string = base64.b64encode(bytes('%s:%s' % ('admin', password), - 'ascii')) + base64string = base64.b64encode( + bytes('{}:{}'.format('admin', password), 'ascii')) request.add_header( "Authorization", "Basic %s" % base64string.decode('utf-8')) @@ -253,15 +232,15 @@ class OpenStackDashboardTests(test_utils.OpenStackBaseTest): expected = 'bind 127.0.0.1:{}'.format(port) self.assertIn(expected, output) with self.assertRaises(urllib.error.URLError): - _do_request() + _do_request(request) - zaza_model.set_application_config(application_name, set_alternate) + zaza_model.set_application_config(self.application_name, set_alternate) zaza_model.block_until_all_units_idle(model_name=self.model_name) # Once exposed, expect HAProxy stats to be available externally output = str(generic_utils.get_file_contents(unit, conf)) expected = 'bind 0.0.0.0:{}'.format(port) - html = _do_request().read().decode(encoding='utf-8') + html = _do_request(request).read().decode(encoding='utf-8') self.assertIn(expected, output) self.assertIn('Statistics Report for HAProxy', html, "HAProxy stats check failed") @@ -311,33 +290,20 @@ class OpenStackDashboardTests(test_utils.OpenStackBaseTest): """ logging.info('Checking dashboard http response...') - unit_name = zaza_model.get_lead_unit_name('openstack-dashboard') - keystone_unit = zaza_model.get_lead_unit_name('keystone') - dashboard_relation = openstack_juju.get_relation_from_unit( - keystone_unit, unit_name, 'identity-service') - dashboard_ip = dashboard_relation['private-address'] - logging.debug("... dashboard_ip is:{}".format(dashboard_ip)) + unit = zaza_model.get_unit_from_name( + zaza_model.get_lead_unit_name(self.application_name)) + logging.debug("... dashboard_ip is:{}".format(unit.public_address)) - # NOTE(fnordahl) there is a eluding issue that currently makes the - # first request to the OpenStack Dashboard error out - # with 500 Internal Server Error in CI. Temporarilly - # add retry logic to unwedge the gate. This issue - # should be revisited and root caused properly when time - # allows. - @tenacity.retry(wait=tenacity.wait_exponential(multiplier=1, - min=5, max=10), - reraise=True) - def do_request(): + request = urllib.request.Request( + 'http://{}/horizon'.format(unit.public_address)) + try: logging.info("... trying to fetch the page") - try: - response = urllib.request.urlopen('http://{}/horizon' - .format(dashboard_ip)) - logging.info("... fetched page") - except Exception as e: - logging.info("... exception raised was {}".format(str(e))) - raise - return response.read().decode('utf-8') - html = do_request() + html = _do_request(request) + logging.info("... fetched page") + except Exception as e: + logging.info("... exception raised was {}".format(str(e))) + raise + return html.read().decode('utf-8') self.assertIn('OpenStack Dashboard', html, "Dashboard frontpage check failed") @@ -345,7 +311,8 @@ class OpenStackDashboardTests(test_utils.OpenStackBaseTest): """Validate that authentication succeeds for client log in.""" logging.info('Checking authentication through dashboard...') - dashboard_ip = _get_dashboard_ip() + unit = zaza_model.get_unit_from_name( + zaza_model.get_lead_unit_name(self.application_name)) overcloud_auth = openstack_utils.get_overcloud_auth() password = overcloud_auth['OS_PASSWORD'], logging.info("admin password is {}".format(password)) @@ -354,7 +321,7 @@ class OpenStackDashboardTests(test_utils.OpenStackBaseTest): domain = 'admin_domain', username = 'admin', password = overcloud_auth['OS_PASSWORD'], - _login(dashboard_ip, domain, username, password) + _login(unit.public_address, domain, username, password) logging.info('OK') def test_404_connection(self): @@ -364,34 +331,21 @@ class OpenStackDashboardTests(test_utils.OpenStackBaseTest): """ logging.info('Checking apache mod_status gets disabled.') - unit_name = zaza_model.get_lead_unit_name('openstack-dashboard') - keystone_unit = zaza_model.get_lead_unit_name('keystone') - dashboard_relation = openstack_juju.get_relation_from_unit( - keystone_unit, unit_name, 'identity-service') - dashboard_ip = dashboard_relation['private-address'] - logging.debug("... dashboard_ip is:{}".format(dashboard_ip)) + unit = zaza_model.get_unit_from_name( + zaza_model.get_lead_unit_name(self.application_name)) + logging.debug("... dashboard_ip is: {}".format(unit.public_address)) logging.debug('Maybe enabling hardening for apache...') _app_config = zaza_model.get_application_config(self.application_name) logging.info(_app_config['harden']) - # NOTE(ajkavanagh): it seems that apache2 doesn't start quickly enough - # for the test, and so it gets reset errors; repeat until either that - # stops or there is a failure - @tenacity.retry(wait=tenacity.wait_exponential(multiplier=1, - min=5, max=10), - retry=tenacity.retry_if_exception_type( - http.client.RemoteDisconnected), - reraise=True) - def _do_request(): - return urllib.request.urlopen('http://{}/server-status' - .format(dashboard_ip)) - + request = urllib.request.Request( + 'http://{}/server-status'.format(unit.public_address)) with self.config_change( {'harden': _app_config['harden'].get('value', '')}, {'harden': 'apache'}): try: - _do_request() + _do_request(request) except urllib.request.HTTPError as e: # test failed if it didn't return 404 msg = "Apache mod_status check failed." @@ -491,8 +445,9 @@ class OpenStackDashboardPolicydTests(policyd.BasePolicydSpecialization): :type ip: str :raises: PolicydOperationFailedException if operation fails. """ - dashboard_ip = _get_dashboard_ip() - logging.info("Dashboard is at {}".format(dashboard_ip)) + unit = zaza_model.get_unit_from_name( + zaza_model.get_lead_unit_name(self.application_name)) + logging.info("Dashboard is at {}".format(unit.public_address)) overcloud_auth = openstack_utils.get_overcloud_auth() password = overcloud_auth['OS_PASSWORD'], logging.info("admin password is {}".format(password)) @@ -501,9 +456,10 @@ class OpenStackDashboardPolicydTests(policyd.BasePolicydSpecialization): domain = 'admin_domain', username = 'admin', password = overcloud_auth['OS_PASSWORD'], - client, response = _login(dashboard_ip, domain, username, password) + client, response = _login( + unit.public_address, domain, username, password) # now attempt to get the domains page - _url = self.url.format(dashboard_ip) + _url = self.url.format(unit.public_address) result = client.get(_url) if result.status_code == 403: raise policyd.PolicydOperationFailedException("Not authenticated")