blob: 932b1515b4ab02cb4f9b67be095750f5043b5180 [file] [log] [blame]
Joe Gordonc97f5c72013-02-14 01:15:57 +00001# 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 Medvedev13168d02013-06-24 16:13:40 -050016import os
Pavel Sedláka2b757c2013-02-25 18:16:04 +010017import shlex
18import subprocess
Joe Gordonc97f5c72013-02-14 01:15:57 +000019
Sean Daguef6825792013-05-08 13:51:26 -040020import tempest.cli.output_parser
Matthew Treinishe2b56b52014-01-29 19:25:50 +000021from tempest import config
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040022from tempest.openstack.common import log as logging
Pavel Sedláka2b757c2013-02-25 18:16:04 +010023import tempest.test
Joe Gordonc97f5c72013-02-14 01:15:57 +000024
Matthew Treinish90aedd12013-02-25 17:56:49 -050025
Joe Gordonc97f5c72013-02-14 01:15:57 +000026LOG = logging.getLogger(__name__)
27
Matthew Treinishe2b56b52014-01-29 19:25:50 +000028CONF = config.CONF
Pavel Sedláka2b757c2013-02-25 18:16:04 +010029
30
31class 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áka2b757c2013-02-25 18:16:04 +010037 super(ClientTestBase, cls).setUpClass()
38
39 def __init__(self, *args, **kwargs):
Sean Daguef6825792013-05-08 13:51:26 -040040 self.parser = tempest.cli.output_parser
Pavel Sedláka2b757c2013-02-25 18:16:04 +010041 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."""
JordanP71c85f62014-02-26 15:53:43 +000045 flags += ' --endpoint-type %s' % CONF.compute.endpoint_type
Pavel Sedláka2b757c2013-02-25 18:16:04 +010046 return self.cmd_with_auth(
47 'nova', action, flags, params, admin, fail_ok)
48
Joe Gordone8b0e152013-03-25 13:37:15 -040049 def nova_manage(self, action, flags='', params='', fail_ok=False,
Joe Gordon0e7cbf82013-03-25 19:49:12 +000050 merge_stderr=False):
Joe Gordon4edb6452013-03-05 21:18:59 +000051 """Executes nova-manage command for the given action."""
52 return self.cmd(
Joe Gordone8b0e152013-03-25 13:37:15 -040053 'nova-manage', action, flags, params, fail_ok, merge_stderr)
Joe Gordon4edb6452013-03-05 21:18:59 +000054
Pavel Sedlák5ce5c032013-02-25 18:41:30 +010055 def keystone(self, action, flags='', params='', admin=True, fail_ok=False):
56 """Executes keystone command for the given action."""
57 return self.cmd_with_auth(
58 'keystone', action, flags, params, admin, fail_ok)
59
afazekasf35f9402013-03-25 14:51:13 +010060 def glance(self, action, flags='', params='', admin=True, fail_ok=False):
61 """Executes glance command for the given action."""
JordanP71c85f62014-02-26 15:53:43 +000062 flags += ' --os-endpoint-type %s' % CONF.image.endpoint_type
afazekasf35f9402013-03-25 14:51:13 +010063 return self.cmd_with_auth(
64 'glance', action, flags, params, admin, fail_ok)
65
Mehdi Abaakouk8581c0b2013-10-04 10:45:42 +020066 def ceilometer(self, action, flags='', params='', admin=True,
67 fail_ok=False):
68 """Executes ceilometer command for the given action."""
JordanP71c85f62014-02-26 15:53:43 +000069 flags += ' --os-endpoint-type %s' % CONF.telemetry.endpoint_type
Mehdi Abaakouk8581c0b2013-10-04 10:45:42 +020070 return self.cmd_with_auth(
71 'ceilometer', action, flags, params, admin, fail_ok)
72
Steven Hardy5de54ee2013-12-31 15:58:30 +000073 def heat(self, action, flags='', params='', admin=True,
74 fail_ok=False):
75 """Executes heat command for the given action."""
JordanP71c85f62014-02-26 15:53:43 +000076 flags += ' --os-endpoint-type %s' % CONF.orchestration.endpoint_type
Steven Hardy5de54ee2013-12-31 15:58:30 +000077 return self.cmd_with_auth(
78 'heat', action, flags, params, admin, fail_ok)
79
saurabh467c4112013-07-08 17:08:31 +053080 def cinder(self, action, flags='', params='', admin=True, fail_ok=False):
81 """Executes cinder command for the given action."""
JordanP71c85f62014-02-26 15:53:43 +000082 flags += ' --endpoint-type %s' % CONF.volume.endpoint_type
saurabh467c4112013-07-08 17:08:31 +053083 return self.cmd_with_auth(
84 'cinder', action, flags, params, admin, fail_ok)
85
saurabh55c29c72013-07-26 21:15:08 +053086 def neutron(self, action, flags='', params='', admin=True, fail_ok=False):
87 """Executes neutron command for the given action."""
JordanP71c85f62014-02-26 15:53:43 +000088 flags += ' --endpoint-type %s' % CONF.network.endpoint_type
saurabh55c29c72013-07-26 21:15:08 +053089 return self.cmd_with_auth(
90 'neutron', action, flags, params, admin, fail_ok)
91
Sergey Lukjanov9c95a252014-03-13 23:59:22 +040092 def sahara(self, action, flags='', params='', admin=True, fail_ok=False):
93 """Executes sahara command for the given action."""
JordanP71c85f62014-02-26 15:53:43 +000094 flags += ' --endpoint-type %s' % CONF.data_processing.endpoint_type
Sergey Lukjanov582d1c62014-01-21 19:51:21 +040095 return self.cmd_with_auth(
Sergey Lukjanov9c95a252014-03-13 23:59:22 +040096 # TODO (slukjanov): replace with sahara when new client released
Sergey Lukjanov582d1c62014-01-21 19:51:21 +040097 'savanna', action, flags, params, admin, fail_ok)
98
Pavel Sedláka2b757c2013-02-25 18:16:04 +010099 def cmd_with_auth(self, cmd, action, flags='', params='',
100 admin=True, fail_ok=False):
101 """Executes given command with auth attributes appended."""
Attila Fazekasc3a095b2013-08-17 09:15:44 +0200102 # TODO(jogo) make admin=False work
Pavel Sedláka2b757c2013-02-25 18:16:04 +0100103 creds = ('--os-username %s --os-tenant-name %s --os-password %s '
JordanP71c85f62014-02-26 15:53:43 +0000104 '--os-auth-url %s' %
Matthew Treinishe2b56b52014-01-29 19:25:50 +0000105 (CONF.identity.admin_username,
106 CONF.identity.admin_tenant_name,
107 CONF.identity.admin_password,
108 CONF.identity.uri))
Pavel Sedláka2b757c2013-02-25 18:16:04 +0100109 flags = creds + ' ' + flags
110 return self.cmd(cmd, action, flags, params, fail_ok)
111
Joe Gordone8b0e152013-03-25 13:37:15 -0400112 def cmd(self, cmd, action, flags='', params='', fail_ok=False,
Joe Gordon0e7cbf82013-03-25 19:49:12 +0000113 merge_stderr=False):
Pavel Sedláka2b757c2013-02-25 18:16:04 +0100114 """Executes specified command for the given action."""
Mikhail S Medvedev13168d02013-06-24 16:13:40 -0500115 cmd = ' '.join([os.path.join(CONF.cli.cli_dir, cmd),
Pavel Sedláka2b757c2013-02-25 18:16:04 +0100116 flags, action, params])
117 LOG.info("running: '%s'" % cmd)
Pavel Sedlák0d9a84f2013-08-27 19:09:26 +0200118 cmd_str = cmd
Pavel Sedláka2b757c2013-02-25 18:16:04 +0100119 cmd = shlex.split(cmd)
Pavel Sedlák0d9a84f2013-08-27 19:09:26 +0200120 result = ''
121 result_err = ''
Pavel Sedláka2b757c2013-02-25 18:16:04 +0100122 try:
Pavel Sedlák0d9a84f2013-08-27 19:09:26 +0200123 stdout = subprocess.PIPE
124 stderr = subprocess.STDOUT if merge_stderr else subprocess.PIPE
125 proc = subprocess.Popen(
126 cmd, stdout=stdout, stderr=stderr)
127 result, result_err = proc.communicate()
128 if not fail_ok and proc.returncode != 0:
129 raise CommandFailed(proc.returncode,
130 cmd,
Cyril Roelandtb18d5fb2013-10-09 20:23:39 +0000131 result,
132 stderr=result_err)
Pavel Sedlák0d9a84f2013-08-27 19:09:26 +0200133 finally:
134 LOG.debug('output of %s:\n%s' % (cmd_str, result))
135 if not merge_stderr and result_err:
136 LOG.debug('error output of %s:\n%s' % (cmd_str, result_err))
Pavel Sedláka2b757c2013-02-25 18:16:04 +0100137 return result
Pavel Sedlák5ce5c032013-02-25 18:41:30 +0100138
139 def assertTableStruct(self, items, field_names):
140 """Verify that all items has keys listed in field_names."""
141 for item in items:
142 for field in field_names:
143 self.assertIn(field, item)
Pavel Sedlák1053bd32013-04-16 16:47:40 +0200144
Pavel Sedlák4c18fa12013-08-22 21:29:45 +0200145 def assertFirstLineStartsWith(self, lines, beginning):
146 self.assertTrue(lines[0].startswith(beginning),
147 msg=('Beginning of first line has invalid content: %s'
148 % lines[:3]))
149
Pavel Sedlák1053bd32013-04-16 16:47:40 +0200150
151class CommandFailed(subprocess.CalledProcessError):
152 # adds output attribute for python2.6
Cyril Roelandtb18d5fb2013-10-09 20:23:39 +0000153 def __init__(self, returncode, cmd, output, stderr=""):
Pavel Sedlák1053bd32013-04-16 16:47:40 +0200154 super(CommandFailed, self).__init__(returncode, cmd)
155 self.output = output
Cyril Roelandtb18d5fb2013-10-09 20:23:39 +0000156 self.stderr = stderr