Joe Gordon | c97f5c7 | 2013-02-14 01:15:57 +0000 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2013 OpenStack Foundation |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
Mikhail S Medvedev | 13168d0 | 2013-06-24 16:13:40 -0500 | [diff] [blame] | 18 | import os |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 19 | import shlex |
| 20 | import subprocess |
Joe Gordon | c97f5c7 | 2013-02-14 01:15:57 +0000 | [diff] [blame] | 21 | |
Matthew Treinish | 90aedd1 | 2013-02-25 17:56:49 -0500 | [diff] [blame] | 22 | from oslo.config import cfg |
| 23 | |
Sean Dague | f682579 | 2013-05-08 13:51:26 -0400 | [diff] [blame] | 24 | import tempest.cli.output_parser |
Matthew Treinish | f4a9b0f | 2013-07-26 16:58:26 -0400 | [diff] [blame] | 25 | from tempest.openstack.common import log as logging |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 26 | import tempest.test |
Joe Gordon | c97f5c7 | 2013-02-14 01:15:57 +0000 | [diff] [blame] | 27 | |
Matthew Treinish | 90aedd1 | 2013-02-25 17:56:49 -0500 | [diff] [blame] | 28 | |
Joe Gordon | c97f5c7 | 2013-02-14 01:15:57 +0000 | [diff] [blame] | 29 | LOG = logging.getLogger(__name__) |
| 30 | |
| 31 | cli_opts = [ |
| 32 | cfg.BoolOpt('enabled', |
| 33 | default=True, |
| 34 | help="enable cli tests"), |
| 35 | cfg.StrOpt('cli_dir', |
| 36 | default='/usr/local/bin/', |
| 37 | help="directory where python client binaries are located"), |
Matt Riedemann | ab038c9 | 2013-08-06 06:56:48 -0700 | [diff] [blame] | 38 | cfg.IntOpt('timeout', |
| 39 | default=15, |
| 40 | help="Number of seconds to wait on a CLI timeout"), |
Joe Gordon | c97f5c7 | 2013-02-14 01:15:57 +0000 | [diff] [blame] | 41 | ] |
| 42 | |
| 43 | CONF = cfg.CONF |
| 44 | cli_group = cfg.OptGroup(name='cli', title="cli Configuration Options") |
| 45 | CONF.register_group(cli_group) |
| 46 | CONF.register_opts(cli_opts, group=cli_group) |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 47 | |
| 48 | |
| 49 | class ClientTestBase(tempest.test.BaseTestCase): |
| 50 | @classmethod |
| 51 | def setUpClass(cls): |
| 52 | if not CONF.cli.enabled: |
| 53 | msg = "cli testing disabled" |
| 54 | raise cls.skipException(msg) |
| 55 | cls.identity = cls.config.identity |
| 56 | super(ClientTestBase, cls).setUpClass() |
| 57 | |
| 58 | def __init__(self, *args, **kwargs): |
Sean Dague | f682579 | 2013-05-08 13:51:26 -0400 | [diff] [blame] | 59 | self.parser = tempest.cli.output_parser |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 60 | super(ClientTestBase, self).__init__(*args, **kwargs) |
| 61 | |
| 62 | def nova(self, action, flags='', params='', admin=True, fail_ok=False): |
| 63 | """Executes nova command for the given action.""" |
| 64 | return self.cmd_with_auth( |
| 65 | 'nova', action, flags, params, admin, fail_ok) |
| 66 | |
Joe Gordon | e8b0e15 | 2013-03-25 13:37:15 -0400 | [diff] [blame] | 67 | def nova_manage(self, action, flags='', params='', fail_ok=False, |
Joe Gordon | 0e7cbf8 | 2013-03-25 19:49:12 +0000 | [diff] [blame] | 68 | merge_stderr=False): |
Joe Gordon | 4edb645 | 2013-03-05 21:18:59 +0000 | [diff] [blame] | 69 | """Executes nova-manage command for the given action.""" |
| 70 | return self.cmd( |
Joe Gordon | e8b0e15 | 2013-03-25 13:37:15 -0400 | [diff] [blame] | 71 | 'nova-manage', action, flags, params, fail_ok, merge_stderr) |
Joe Gordon | 4edb645 | 2013-03-05 21:18:59 +0000 | [diff] [blame] | 72 | |
Pavel Sedlák | 5ce5c03 | 2013-02-25 18:41:30 +0100 | [diff] [blame] | 73 | def keystone(self, action, flags='', params='', admin=True, fail_ok=False): |
| 74 | """Executes keystone command for the given action.""" |
| 75 | return self.cmd_with_auth( |
| 76 | 'keystone', action, flags, params, admin, fail_ok) |
| 77 | |
afazekas | f35f940 | 2013-03-25 14:51:13 +0100 | [diff] [blame] | 78 | def glance(self, action, flags='', params='', admin=True, fail_ok=False): |
| 79 | """Executes glance command for the given action.""" |
| 80 | return self.cmd_with_auth( |
| 81 | 'glance', action, flags, params, admin, fail_ok) |
| 82 | |
saurabh | 467c411 | 2013-07-08 17:08:31 +0530 | [diff] [blame] | 83 | def cinder(self, action, flags='', params='', admin=True, fail_ok=False): |
| 84 | """Executes cinder command for the given action.""" |
| 85 | return self.cmd_with_auth( |
| 86 | 'cinder', action, flags, params, admin, fail_ok) |
| 87 | |
saurabh | 55c29c7 | 2013-07-26 21:15:08 +0530 | [diff] [blame] | 88 | def neutron(self, action, flags='', params='', admin=True, fail_ok=False): |
| 89 | """Executes neutron command for the given action.""" |
| 90 | return self.cmd_with_auth( |
| 91 | 'neutron', action, flags, params, admin, fail_ok) |
| 92 | |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 93 | def cmd_with_auth(self, cmd, action, flags='', params='', |
| 94 | admin=True, fail_ok=False): |
| 95 | """Executes given command with auth attributes appended.""" |
Attila Fazekas | c3a095b | 2013-08-17 09:15:44 +0200 | [diff] [blame] | 96 | # TODO(jogo) make admin=False work |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 97 | creds = ('--os-username %s --os-tenant-name %s --os-password %s ' |
| 98 | '--os-auth-url %s ' % (self.identity.admin_username, |
| 99 | self.identity.admin_tenant_name, self.identity.admin_password, |
| 100 | self.identity.uri)) |
| 101 | flags = creds + ' ' + flags |
| 102 | return self.cmd(cmd, action, flags, params, fail_ok) |
| 103 | |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 104 | def check_output(self, cmd, **kwargs): |
| 105 | # substitutes subprocess.check_output which is not in python2.6 |
| 106 | kwargs['stdout'] = subprocess.PIPE |
| 107 | proc = subprocess.Popen(cmd, **kwargs) |
| 108 | output = proc.communicate()[0] |
| 109 | if proc.returncode != 0: |
| 110 | raise CommandFailed(proc.returncode, cmd, output) |
| 111 | return output |
| 112 | |
Joe Gordon | e8b0e15 | 2013-03-25 13:37:15 -0400 | [diff] [blame] | 113 | def cmd(self, cmd, action, flags='', params='', fail_ok=False, |
Joe Gordon | 0e7cbf8 | 2013-03-25 19:49:12 +0000 | [diff] [blame] | 114 | merge_stderr=False): |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 115 | """Executes specified command for the given action.""" |
Mikhail S Medvedev | 13168d0 | 2013-06-24 16:13:40 -0500 | [diff] [blame] | 116 | cmd = ' '.join([os.path.join(CONF.cli.cli_dir, cmd), |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 117 | flags, action, params]) |
| 118 | LOG.info("running: '%s'" % cmd) |
| 119 | cmd = shlex.split(cmd) |
| 120 | try: |
Joe Gordon | e8b0e15 | 2013-03-25 13:37:15 -0400 | [diff] [blame] | 121 | if merge_stderr: |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 122 | result = self.check_output(cmd, stderr=subprocess.STDOUT) |
Joe Gordon | e8b0e15 | 2013-03-25 13:37:15 -0400 | [diff] [blame] | 123 | else: |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 124 | with open('/dev/null', 'w') as devnull: |
| 125 | result = self.check_output(cmd, stderr=devnull) |
Dirk Mueller | 1db5db2 | 2013-06-23 20:21:32 +0200 | [diff] [blame] | 126 | except subprocess.CalledProcessError as e: |
Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 127 | LOG.error("command output:\n%s" % e.output) |
| 128 | raise |
| 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 | |
| 137 | |
| 138 | class CommandFailed(subprocess.CalledProcessError): |
| 139 | # adds output attribute for python2.6 |
| 140 | def __init__(self, returncode, cmd, output): |
| 141 | super(CommandFailed, self).__init__(returncode, cmd) |
| 142 | self.output = output |