blob: b082b1ee1a952a649dcdf2a282d30a992d240e03 [file] [log] [blame]
Joe Gordonc97f5c72013-02-14 01:15:57 +00001# 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 Medvedev13168d02013-06-24 16:13:40 -050018import os
Pavel Sedláka2b757c2013-02-25 18:16:04 +010019import shlex
20import subprocess
Joe Gordonc97f5c72013-02-14 01:15:57 +000021
Matthew Treinish90aedd12013-02-25 17:56:49 -050022from oslo.config import cfg
23
Sean Daguef6825792013-05-08 13:51:26 -040024import tempest.cli.output_parser
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040025from tempest.openstack.common import log as logging
Pavel Sedláka2b757c2013-02-25 18:16:04 +010026import tempest.test
Joe Gordonc97f5c72013-02-14 01:15:57 +000027
Matthew Treinish90aedd12013-02-25 17:56:49 -050028
Joe Gordonc97f5c72013-02-14 01:15:57 +000029LOG = logging.getLogger(__name__)
30
31cli_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 Riedemannab038c92013-08-06 06:56:48 -070038 cfg.IntOpt('timeout',
39 default=15,
40 help="Number of seconds to wait on a CLI timeout"),
Joe Gordonc97f5c72013-02-14 01:15:57 +000041]
42
43CONF = cfg.CONF
44cli_group = cfg.OptGroup(name='cli', title="cli Configuration Options")
45CONF.register_group(cli_group)
46CONF.register_opts(cli_opts, group=cli_group)
Pavel Sedláka2b757c2013-02-25 18:16:04 +010047
48
49class 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 Daguef6825792013-05-08 13:51:26 -040059 self.parser = tempest.cli.output_parser
Pavel Sedláka2b757c2013-02-25 18:16:04 +010060 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 Gordone8b0e152013-03-25 13:37:15 -040067 def nova_manage(self, action, flags='', params='', fail_ok=False,
Joe Gordon0e7cbf82013-03-25 19:49:12 +000068 merge_stderr=False):
Joe Gordon4edb6452013-03-05 21:18:59 +000069 """Executes nova-manage command for the given action."""
70 return self.cmd(
Joe Gordone8b0e152013-03-25 13:37:15 -040071 'nova-manage', action, flags, params, fail_ok, merge_stderr)
Joe Gordon4edb6452013-03-05 21:18:59 +000072
Pavel Sedlák5ce5c032013-02-25 18:41:30 +010073 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
afazekasf35f9402013-03-25 14:51:13 +010078 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
saurabh467c4112013-07-08 17:08:31 +053083 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
saurabh55c29c72013-07-26 21:15:08 +053088 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áka2b757c2013-02-25 18:16:04 +010093 def cmd_with_auth(self, cmd, action, flags='', params='',
94 admin=True, fail_ok=False):
95 """Executes given command with auth attributes appended."""
Attila Fazekasc3a095b2013-08-17 09:15:44 +020096 # TODO(jogo) make admin=False work
Pavel Sedláka2b757c2013-02-25 18:16:04 +010097 creds = ('--os-username %s --os-tenant-name %s --os-password %s '
DennyZhangb432bac2013-09-17 16:24:12 +000098 '--os-auth-url %s ' %
99 (self.identity.admin_username,
100 self.identity.admin_tenant_name,
101 self.identity.admin_password,
102 self.identity.uri))
Pavel Sedláka2b757c2013-02-25 18:16:04 +0100103 flags = creds + ' ' + flags
104 return self.cmd(cmd, action, flags, params, fail_ok)
105
Joe Gordone8b0e152013-03-25 13:37:15 -0400106 def cmd(self, cmd, action, flags='', params='', fail_ok=False,
Joe Gordon0e7cbf82013-03-25 19:49:12 +0000107 merge_stderr=False):
Pavel Sedláka2b757c2013-02-25 18:16:04 +0100108 """Executes specified command for the given action."""
Mikhail S Medvedev13168d02013-06-24 16:13:40 -0500109 cmd = ' '.join([os.path.join(CONF.cli.cli_dir, cmd),
Pavel Sedláka2b757c2013-02-25 18:16:04 +0100110 flags, action, params])
111 LOG.info("running: '%s'" % cmd)
Pavel Sedlák0d9a84f2013-08-27 19:09:26 +0200112 cmd_str = cmd
Pavel Sedláka2b757c2013-02-25 18:16:04 +0100113 cmd = shlex.split(cmd)
Pavel Sedlák0d9a84f2013-08-27 19:09:26 +0200114 result = ''
115 result_err = ''
Pavel Sedláka2b757c2013-02-25 18:16:04 +0100116 try:
Pavel Sedlák0d9a84f2013-08-27 19:09:26 +0200117 stdout = subprocess.PIPE
118 stderr = subprocess.STDOUT if merge_stderr else subprocess.PIPE
119 proc = subprocess.Popen(
120 cmd, stdout=stdout, stderr=stderr)
121 result, result_err = proc.communicate()
122 if not fail_ok and proc.returncode != 0:
123 raise CommandFailed(proc.returncode,
124 cmd,
125 result)
126 finally:
127 LOG.debug('output of %s:\n%s' % (cmd_str, result))
128 if not merge_stderr and result_err:
129 LOG.debug('error output of %s:\n%s' % (cmd_str, result_err))
Pavel Sedláka2b757c2013-02-25 18:16:04 +0100130 return result
Pavel Sedlák5ce5c032013-02-25 18:41:30 +0100131
132 def assertTableStruct(self, items, field_names):
133 """Verify that all items has keys listed in field_names."""
134 for item in items:
135 for field in field_names:
136 self.assertIn(field, item)
Pavel Sedlák1053bd32013-04-16 16:47:40 +0200137
Pavel Sedlák4c18fa12013-08-22 21:29:45 +0200138 def assertFirstLineStartsWith(self, lines, beginning):
139 self.assertTrue(lines[0].startswith(beginning),
140 msg=('Beginning of first line has invalid content: %s'
141 % lines[:3]))
142
Pavel Sedlák1053bd32013-04-16 16:47:40 +0200143
144class CommandFailed(subprocess.CalledProcessError):
145 # adds output attribute for python2.6
146 def __init__(self, returncode, cmd, output):
147 super(CommandFailed, self).__init__(returncode, cmd)
148 self.output = output