blob: 2548f24093f5371bced808d052921ab6d9628598 [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
18import logging
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
Pavel Sedláka2b757c2013-02-25 18:16:04 +010024import tempest.test
Joe Gordonc97f5c72013-02-14 01:15:57 +000025
Matthew Treinish90aedd12013-02-25 17:56:49 -050026
Joe Gordonc97f5c72013-02-14 01:15:57 +000027LOG = logging.getLogger(__name__)
28
29cli_opts = [
30 cfg.BoolOpt('enabled',
31 default=True,
32 help="enable cli tests"),
33 cfg.StrOpt('cli_dir',
34 default='/usr/local/bin/',
35 help="directory where python client binaries are located"),
36]
37
38CONF = cfg.CONF
39cli_group = cfg.OptGroup(name='cli', title="cli Configuration Options")
40CONF.register_group(cli_group)
41CONF.register_opts(cli_opts, group=cli_group)
Pavel Sedláka2b757c2013-02-25 18:16:04 +010042
43
44class ClientTestBase(tempest.test.BaseTestCase):
45 @classmethod
46 def setUpClass(cls):
47 if not CONF.cli.enabled:
48 msg = "cli testing disabled"
49 raise cls.skipException(msg)
50 cls.identity = cls.config.identity
51 super(ClientTestBase, cls).setUpClass()
52
53 def __init__(self, *args, **kwargs):
54 super(ClientTestBase, self).__init__(*args, **kwargs)
55
56 def nova(self, action, flags='', params='', admin=True, fail_ok=False):
57 """Executes nova command for the given action."""
58 return self.cmd_with_auth(
59 'nova', action, flags, params, admin, fail_ok)
60
61 def cmd_with_auth(self, cmd, action, flags='', params='',
62 admin=True, fail_ok=False):
63 """Executes given command with auth attributes appended."""
64 #TODO(jogo) make admin=False work
65 creds = ('--os-username %s --os-tenant-name %s --os-password %s '
66 '--os-auth-url %s ' % (self.identity.admin_username,
67 self.identity.admin_tenant_name, self.identity.admin_password,
68 self.identity.uri))
69 flags = creds + ' ' + flags
70 return self.cmd(cmd, action, flags, params, fail_ok)
71
72 def cmd(self, cmd, action, flags='', params='', fail_ok=False):
73 """Executes specified command for the given action."""
74 cmd = ' '.join([CONF.cli.cli_dir + cmd,
75 flags, action, params])
76 LOG.info("running: '%s'" % cmd)
77 cmd = shlex.split(cmd)
78 try:
79 result = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
80 except subprocess.CalledProcessError, e:
81 LOG.error("command output:\n%s" % e.output)
82 raise
83 return result