Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 1 | # All Rights Reserved. |
| 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 | |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 15 | from six.moves import html_parser as HTMLParser |
| 16 | from six.moves.urllib import parse |
| 17 | from six.moves.urllib import request |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 18 | |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 19 | from tempest import config |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 20 | from tempest.scenario import manager |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 21 | from tempest import test |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 22 | |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 23 | CONF = config.CONF |
| 24 | |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 25 | |
Sean Dague | 8fa6c03 | 2014-11-25 10:54:38 -0500 | [diff] [blame] | 26 | class HorizonHTMLParser(HTMLParser.HTMLParser): |
| 27 | csrf_token = None |
| 28 | region = None |
David Lyle | f0b070c | 2015-08-12 19:27:18 -0600 | [diff] [blame] | 29 | login = None |
Sean Dague | 8fa6c03 | 2014-11-25 10:54:38 -0500 | [diff] [blame] | 30 | |
| 31 | def _find_name(self, attrs, name): |
| 32 | for attrpair in attrs: |
| 33 | if attrpair[0] == 'name' and attrpair[1] == name: |
| 34 | return True |
| 35 | return False |
| 36 | |
| 37 | def _find_value(self, attrs): |
| 38 | for attrpair in attrs: |
| 39 | if attrpair[0] == 'value': |
| 40 | return attrpair[1] |
| 41 | return None |
| 42 | |
David Lyle | f0b070c | 2015-08-12 19:27:18 -0600 | [diff] [blame] | 43 | def _find_attr_value(self, attrs, attr_name): |
| 44 | for attrpair in attrs: |
| 45 | if attrpair[0] == attr_name: |
| 46 | return attrpair[1] |
| 47 | return None |
| 48 | |
Sean Dague | 8fa6c03 | 2014-11-25 10:54:38 -0500 | [diff] [blame] | 49 | def handle_starttag(self, tag, attrs): |
| 50 | if tag == 'input': |
| 51 | if self._find_name(attrs, 'csrfmiddlewaretoken'): |
| 52 | self.csrf_token = self._find_value(attrs) |
| 53 | if self._find_name(attrs, 'region'): |
| 54 | self.region = self._find_value(attrs) |
David Lyle | f0b070c | 2015-08-12 19:27:18 -0600 | [diff] [blame] | 55 | if tag == 'form': |
| 56 | self.login = self._find_attr_value(attrs, 'action') |
Sean Dague | 8fa6c03 | 2014-11-25 10:54:38 -0500 | [diff] [blame] | 57 | |
| 58 | |
Masayuki Igawa | 2675f8f | 2014-07-17 13:46:26 +0200 | [diff] [blame] | 59 | class TestDashboardBasicOps(manager.ScenarioTest): |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 60 | |
| 61 | """ |
| 62 | This is a basic scenario test: |
| 63 | * checks that the login page is available |
| 64 | * logs in as a regular user |
| 65 | * checks that the user home page loads without error |
| 66 | """ |
| 67 | |
| 68 | @classmethod |
Emily Hugenbruch | 5e2d2a2 | 2015-02-25 21:35:45 +0000 | [diff] [blame] | 69 | def skip_checks(cls): |
| 70 | super(TestDashboardBasicOps, cls).skip_checks() |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 71 | if not CONF.service_available.horizon: |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 72 | raise cls.skipException("Horizon support is required") |
Emily Hugenbruch | 5e2d2a2 | 2015-02-25 21:35:45 +0000 | [diff] [blame] | 73 | |
| 74 | @classmethod |
| 75 | def setup_credentials(cls): |
Masayuki Igawa | 60ea6c5 | 2014-10-15 17:32:14 +0900 | [diff] [blame] | 76 | cls.set_network_resources() |
Emily Hugenbruch | 5e2d2a2 | 2015-02-25 21:35:45 +0000 | [diff] [blame] | 77 | super(TestDashboardBasicOps, cls).setup_credentials() |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 78 | |
| 79 | def check_login_page(self): |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 80 | response = request.urlopen(CONF.dashboard.dashboard_url) |
Thomas Bechtold | 4458749 | 2015-02-09 10:04:03 +0100 | [diff] [blame] | 81 | self.assertIn("id_username", response.read()) |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 82 | |
Matthew Treinish | 643f4c6 | 2014-12-12 19:54:34 -0500 | [diff] [blame] | 83 | def user_login(self, username, password): |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 84 | self.opener = request.build_opener(request.HTTPCookieProcessor()) |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 85 | response = self.opener.open(CONF.dashboard.dashboard_url).read() |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 86 | |
| 87 | # Grab the CSRF token and default region |
Sean Dague | 8fa6c03 | 2014-11-25 10:54:38 -0500 | [diff] [blame] | 88 | parser = HorizonHTMLParser() |
| 89 | parser.feed(response) |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 90 | |
David Lyle | f0b070c | 2015-08-12 19:27:18 -0600 | [diff] [blame] | 91 | # construct login url for dashboard, discovery accomodates non-/ web |
| 92 | # root for dashboard |
| 93 | login_url = CONF.dashboard.dashboard_url + parser.login[1:] |
| 94 | |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 95 | # Prepare login form request |
David Lyle | f0b070c | 2015-08-12 19:27:18 -0600 | [diff] [blame] | 96 | req = request.Request(login_url) |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 97 | req.add_header('Content-type', 'application/x-www-form-urlencoded') |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 98 | req.add_header('Referer', CONF.dashboard.dashboard_url) |
Matthew Treinish | 643f4c6 | 2014-12-12 19:54:34 -0500 | [diff] [blame] | 99 | params = {'username': username, |
| 100 | 'password': password, |
Sean Dague | 8fa6c03 | 2014-11-25 10:54:38 -0500 | [diff] [blame] | 101 | 'region': parser.region, |
| 102 | 'csrfmiddlewaretoken': parser.csrf_token} |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 103 | self.opener.open(req, parse.urlencode(params)) |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 104 | |
| 105 | def check_home_page(self): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 106 | response = self.opener.open(CONF.dashboard.dashboard_url) |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 107 | self.assertIn('Overview', response.read()) |
| 108 | |
Chris Hoge | 7579c1a | 2015-02-26 14:12:15 -0800 | [diff] [blame] | 109 | @test.idempotent_id('4f8851b1-0e69-482b-b63b-84c6e76f6c80') |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 110 | @test.services('dashboard') |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 111 | def test_basic_scenario(self): |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 112 | creds = self.os.credentials |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 113 | self.check_login_page() |
Matthew Treinish | 643f4c6 | 2014-12-12 19:54:34 -0500 | [diff] [blame] | 114 | self.user_login(creds.username, creds.password) |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 115 | self.check_home_page() |