Joe Gordon | c97f5c7 | 2013-02-14 01:15:57 +0000 | [diff] [blame] | 1 | # Copyright 2013 OpenStack Foundation |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
Mikhail S Medvedev | 13168d0 | 2013-06-24 16:13:40 -0500 | [diff] [blame] | 16 | import os |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 17 | import shlex |
| 18 | import subprocess |
Joe Gordon | c97f5c7 | 2013-02-14 01:15:57 +0000 | [diff] [blame] | 19 | |
Sean Dague | f682579 | 2013-05-08 13:51:26 -0400 | [diff] [blame] | 20 | import tempest.cli.output_parser |
Matthew Treinish | e2b56b5 | 2014-01-29 19:25:50 +0000 | [diff] [blame] | 21 | from tempest import config |
Matthew Treinish | f4a9b0f | 2013-07-26 16:58:26 -0400 | [diff] [blame] | 22 | from tempest.openstack.common import log as logging |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 23 | import tempest.test |
Joe Gordon | c97f5c7 | 2013-02-14 01:15:57 +0000 | [diff] [blame] | 24 | |
Matthew Treinish | 90aedd1 | 2013-02-25 17:56:49 -0500 | [diff] [blame] | 25 | |
Joe Gordon | c97f5c7 | 2013-02-14 01:15:57 +0000 | [diff] [blame] | 26 | LOG = logging.getLogger(__name__) |
| 27 | |
Matthew Treinish | e2b56b5 | 2014-01-29 19:25:50 +0000 | [diff] [blame] | 28 | CONF = config.CONF |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 29 | |
| 30 | |
| 31 | class ClientTestBase(tempest.test.BaseTestCase): |
| 32 | @classmethod |
| 33 | def setUpClass(cls): |
| 34 | if not CONF.cli.enabled: |
| 35 | msg = "cli testing disabled" |
| 36 | raise cls.skipException(msg) |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 37 | super(ClientTestBase, cls).setUpClass() |
| 38 | |
| 39 | def __init__(self, *args, **kwargs): |
Sean Dague | f682579 | 2013-05-08 13:51:26 -0400 | [diff] [blame] | 40 | self.parser = tempest.cli.output_parser |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 41 | super(ClientTestBase, self).__init__(*args, **kwargs) |
| 42 | |
| 43 | def nova(self, action, flags='', params='', admin=True, fail_ok=False): |
| 44 | """Executes nova command for the given action.""" |
| 45 | return self.cmd_with_auth( |
| 46 | 'nova', action, flags, params, admin, fail_ok) |
| 47 | |
Joe Gordon | e8b0e15 | 2013-03-25 13:37:15 -0400 | [diff] [blame] | 48 | def nova_manage(self, action, flags='', params='', fail_ok=False, |
Joe Gordon | 0e7cbf8 | 2013-03-25 19:49:12 +0000 | [diff] [blame] | 49 | merge_stderr=False): |
Joe Gordon | 4edb645 | 2013-03-05 21:18:59 +0000 | [diff] [blame] | 50 | """Executes nova-manage command for the given action.""" |
| 51 | return self.cmd( |
Joe Gordon | e8b0e15 | 2013-03-25 13:37:15 -0400 | [diff] [blame] | 52 | 'nova-manage', action, flags, params, fail_ok, merge_stderr) |
Joe Gordon | 4edb645 | 2013-03-05 21:18:59 +0000 | [diff] [blame] | 53 | |
Pavel Sedlák | 5ce5c03 | 2013-02-25 18:41:30 +0100 | [diff] [blame] | 54 | def keystone(self, action, flags='', params='', admin=True, fail_ok=False): |
| 55 | """Executes keystone command for the given action.""" |
| 56 | return self.cmd_with_auth( |
| 57 | 'keystone', action, flags, params, admin, fail_ok) |
| 58 | |
afazekas | f35f940 | 2013-03-25 14:51:13 +0100 | [diff] [blame] | 59 | def glance(self, action, flags='', params='', admin=True, fail_ok=False): |
| 60 | """Executes glance command for the given action.""" |
| 61 | return self.cmd_with_auth( |
| 62 | 'glance', action, flags, params, admin, fail_ok) |
| 63 | |
Mehdi Abaakouk | 8581c0b | 2013-10-04 10:45:42 +0200 | [diff] [blame] | 64 | def ceilometer(self, action, flags='', params='', admin=True, |
| 65 | fail_ok=False): |
| 66 | """Executes ceilometer command for the given action.""" |
| 67 | return self.cmd_with_auth( |
| 68 | 'ceilometer', action, flags, params, admin, fail_ok) |
| 69 | |
Steven Hardy | 5de54ee | 2013-12-31 15:58:30 +0000 | [diff] [blame] | 70 | def heat(self, action, flags='', params='', admin=True, |
| 71 | fail_ok=False): |
| 72 | """Executes heat command for the given action.""" |
| 73 | return self.cmd_with_auth( |
| 74 | 'heat', action, flags, params, admin, fail_ok) |
| 75 | |
saurabh | 467c411 | 2013-07-08 17:08:31 +0530 | [diff] [blame] | 76 | def cinder(self, action, flags='', params='', admin=True, fail_ok=False): |
| 77 | """Executes cinder command for the given action.""" |
| 78 | return self.cmd_with_auth( |
| 79 | 'cinder', action, flags, params, admin, fail_ok) |
| 80 | |
saurabh | 55c29c7 | 2013-07-26 21:15:08 +0530 | [diff] [blame] | 81 | def neutron(self, action, flags='', params='', admin=True, fail_ok=False): |
| 82 | """Executes neutron command for the given action.""" |
| 83 | return self.cmd_with_auth( |
| 84 | 'neutron', action, flags, params, admin, fail_ok) |
| 85 | |
Sergey Lukjanov | 582d1c6 | 2014-01-21 19:51:21 +0400 | [diff] [blame] | 86 | def savanna(self, action, flags='', params='', admin=True, fail_ok=False): |
| 87 | """Executes savanna command for the given action.""" |
| 88 | return self.cmd_with_auth( |
| 89 | 'savanna', action, flags, params, admin, fail_ok) |
| 90 | |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 91 | def cmd_with_auth(self, cmd, action, flags='', params='', |
| 92 | admin=True, fail_ok=False): |
| 93 | """Executes given command with auth attributes appended.""" |
Attila Fazekas | c3a095b | 2013-08-17 09:15:44 +0200 | [diff] [blame] | 94 | # TODO(jogo) make admin=False work |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 95 | creds = ('--os-username %s --os-tenant-name %s --os-password %s ' |
DennyZhang | b432bac | 2013-09-17 16:24:12 +0000 | [diff] [blame] | 96 | '--os-auth-url %s ' % |
Matthew Treinish | e2b56b5 | 2014-01-29 19:25:50 +0000 | [diff] [blame] | 97 | (CONF.identity.admin_username, |
| 98 | CONF.identity.admin_tenant_name, |
| 99 | CONF.identity.admin_password, |
| 100 | CONF.identity.uri)) |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 101 | flags = creds + ' ' + flags |
| 102 | return self.cmd(cmd, action, flags, params, fail_ok) |
| 103 | |
Joe Gordon | e8b0e15 | 2013-03-25 13:37:15 -0400 | [diff] [blame] | 104 | def cmd(self, cmd, action, flags='', params='', fail_ok=False, |
Joe Gordon | 0e7cbf8 | 2013-03-25 19:49:12 +0000 | [diff] [blame] | 105 | merge_stderr=False): |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 106 | """Executes specified command for the given action.""" |
Mikhail S Medvedev | 13168d0 | 2013-06-24 16:13:40 -0500 | [diff] [blame] | 107 | cmd = ' '.join([os.path.join(CONF.cli.cli_dir, cmd), |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 108 | flags, action, params]) |
| 109 | LOG.info("running: '%s'" % cmd) |
Pavel Sedlák | 0d9a84f | 2013-08-27 19:09:26 +0200 | [diff] [blame] | 110 | cmd_str = cmd |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 111 | cmd = shlex.split(cmd) |
Pavel Sedlák | 0d9a84f | 2013-08-27 19:09:26 +0200 | [diff] [blame] | 112 | result = '' |
| 113 | result_err = '' |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 114 | try: |
Pavel Sedlák | 0d9a84f | 2013-08-27 19:09:26 +0200 | [diff] [blame] | 115 | stdout = subprocess.PIPE |
| 116 | stderr = subprocess.STDOUT if merge_stderr else subprocess.PIPE |
| 117 | proc = subprocess.Popen( |
| 118 | cmd, stdout=stdout, stderr=stderr) |
| 119 | result, result_err = proc.communicate() |
| 120 | if not fail_ok and proc.returncode != 0: |
| 121 | raise CommandFailed(proc.returncode, |
| 122 | cmd, |
Cyril Roelandt | b18d5fb | 2013-10-09 20:23:39 +0000 | [diff] [blame] | 123 | result, |
| 124 | stderr=result_err) |
Pavel Sedlák | 0d9a84f | 2013-08-27 19:09:26 +0200 | [diff] [blame] | 125 | finally: |
| 126 | LOG.debug('output of %s:\n%s' % (cmd_str, result)) |
| 127 | if not merge_stderr and result_err: |
| 128 | LOG.debug('error output of %s:\n%s' % (cmd_str, result_err)) |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 129 | return result |
Pavel Sedlák | 5ce5c03 | 2013-02-25 18:41:30 +0100 | [diff] [blame] | 130 | |
| 131 | def assertTableStruct(self, items, field_names): |
| 132 | """Verify that all items has keys listed in field_names.""" |
| 133 | for item in items: |
| 134 | for field in field_names: |
| 135 | self.assertIn(field, item) |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 136 | |
Pavel Sedlák | 4c18fa1 | 2013-08-22 21:29:45 +0200 | [diff] [blame] | 137 | def assertFirstLineStartsWith(self, lines, beginning): |
| 138 | self.assertTrue(lines[0].startswith(beginning), |
| 139 | msg=('Beginning of first line has invalid content: %s' |
| 140 | % lines[:3])) |
| 141 | |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 142 | |
| 143 | class CommandFailed(subprocess.CalledProcessError): |
| 144 | # adds output attribute for python2.6 |
Cyril Roelandt | b18d5fb | 2013-10-09 20:23:39 +0000 | [diff] [blame] | 145 | def __init__(self, returncode, cmd, output, stderr=""): |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 146 | super(CommandFailed, self).__init__(returncode, cmd) |
| 147 | self.output = output |
Cyril Roelandt | b18d5fb | 2013-10-09 20:23:39 +0000 | [diff] [blame] | 148 | self.stderr = stderr |