| 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 |  | 
 | 18 | import logging | 
| 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 |  | 
| Pavel Sedlák | 5ce5c03 | 2013-02-25 18:41:30 +0100 | [diff] [blame^] | 24 | import cli.output_parser | 
| Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 25 | import tempest.test | 
| Joe Gordon | c97f5c7 | 2013-02-14 01:15:57 +0000 | [diff] [blame] | 26 |  | 
| Matthew Treinish | 90aedd1 | 2013-02-25 17:56:49 -0500 | [diff] [blame] | 27 |  | 
| Joe Gordon | c97f5c7 | 2013-02-14 01:15:57 +0000 | [diff] [blame] | 28 | LOG = logging.getLogger(__name__) | 
 | 29 |  | 
 | 30 | cli_opts = [ | 
 | 31 |     cfg.BoolOpt('enabled', | 
 | 32 |                 default=True, | 
 | 33 |                 help="enable cli tests"), | 
 | 34 |     cfg.StrOpt('cli_dir', | 
 | 35 |                default='/usr/local/bin/', | 
 | 36 |                help="directory where python client binaries are located"), | 
 | 37 | ] | 
 | 38 |  | 
 | 39 | CONF = cfg.CONF | 
 | 40 | cli_group = cfg.OptGroup(name='cli', title="cli Configuration Options") | 
 | 41 | CONF.register_group(cli_group) | 
 | 42 | CONF.register_opts(cli_opts, group=cli_group) | 
| Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 43 |  | 
 | 44 |  | 
 | 45 | class ClientTestBase(tempest.test.BaseTestCase): | 
 | 46 |     @classmethod | 
 | 47 |     def setUpClass(cls): | 
 | 48 |         if not CONF.cli.enabled: | 
 | 49 |             msg = "cli testing disabled" | 
 | 50 |             raise cls.skipException(msg) | 
 | 51 |         cls.identity = cls.config.identity | 
 | 52 |         super(ClientTestBase, cls).setUpClass() | 
 | 53 |  | 
 | 54 |     def __init__(self, *args, **kwargs): | 
| Pavel Sedlák | 5ce5c03 | 2013-02-25 18:41:30 +0100 | [diff] [blame^] | 55 |         self.parser = cli.output_parser | 
| Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 56 |         super(ClientTestBase, self).__init__(*args, **kwargs) | 
 | 57 |  | 
 | 58 |     def nova(self, action, flags='', params='', admin=True, fail_ok=False): | 
 | 59 |         """Executes nova command for the given action.""" | 
 | 60 |         return self.cmd_with_auth( | 
 | 61 |             'nova', action, flags, params, admin, fail_ok) | 
 | 62 |  | 
| Joe Gordon | 4edb645 | 2013-03-05 21:18:59 +0000 | [diff] [blame] | 63 |     def nova_manage(self, action, flags='', params='', fail_ok=False): | 
 | 64 |         """Executes nova-manage command for the given action.""" | 
 | 65 |         return self.cmd( | 
 | 66 |             'nova-manage', action, flags, params, fail_ok) | 
 | 67 |  | 
| Pavel Sedlák | 5ce5c03 | 2013-02-25 18:41:30 +0100 | [diff] [blame^] | 68 |     def keystone(self, action, flags='', params='', admin=True, fail_ok=False): | 
 | 69 |         """Executes keystone command for the given action.""" | 
 | 70 |         return self.cmd_with_auth( | 
 | 71 |             'keystone', action, flags, params, admin, fail_ok) | 
 | 72 |  | 
| Pavel Sedlák | a2b757c | 2013-02-25 18:16:04 +0100 | [diff] [blame] | 73 |     def cmd_with_auth(self, cmd, action, flags='', params='', | 
 | 74 |                       admin=True, fail_ok=False): | 
 | 75 |         """Executes given command with auth attributes appended.""" | 
 | 76 |         #TODO(jogo) make admin=False work | 
 | 77 |         creds = ('--os-username %s --os-tenant-name %s --os-password %s ' | 
 | 78 |                  '--os-auth-url %s ' % (self.identity.admin_username, | 
 | 79 |                  self.identity.admin_tenant_name, self.identity.admin_password, | 
 | 80 |                  self.identity.uri)) | 
 | 81 |         flags = creds + ' ' + flags | 
 | 82 |         return self.cmd(cmd, action, flags, params, fail_ok) | 
 | 83 |  | 
 | 84 |     def cmd(self, cmd, action, flags='', params='', fail_ok=False): | 
 | 85 |         """Executes specified command for the given action.""" | 
 | 86 |         cmd = ' '.join([CONF.cli.cli_dir + cmd, | 
 | 87 |                         flags, action, params]) | 
 | 88 |         LOG.info("running: '%s'" % cmd) | 
 | 89 |         cmd = shlex.split(cmd) | 
 | 90 |         try: | 
 | 91 |             result = subprocess.check_output(cmd, stderr=subprocess.STDOUT) | 
 | 92 |         except subprocess.CalledProcessError, e: | 
 | 93 |             LOG.error("command output:\n%s" % e.output) | 
 | 94 |             raise | 
 | 95 |         return result | 
| Pavel Sedlák | 5ce5c03 | 2013-02-25 18:41:30 +0100 | [diff] [blame^] | 96 |  | 
 | 97 |     def assertTableStruct(self, items, field_names): | 
 | 98 |         """Verify that all items has keys listed in field_names.""" | 
 | 99 |         for item in items: | 
 | 100 |             for field in field_names: | 
 | 101 |                 self.assertIn(field, item) |