Move CommandFailed exception to tempest.exceptions

When the CommandFailed exception was added it was only being used by
the CLI tests. However, it is generally useful for anything that is
using subprocess to make external calls. This patch moves it to
tempest.exceptions to make using it simpler for non-cli tests to use
the exception.

Change-Id: Ibf5f1cbbb847d32976b54c4484acfc3c0e3b4f48
This commit is contained in:
Matthew Treinish 2014-07-25 18:38:56 -04:00
parent 26cd43d157
commit aeb5274fd4
11 changed files with 43 additions and 34 deletions

View File

@ -19,6 +19,7 @@ import subprocess
import tempest.cli.output_parser
from tempest import config
from tempest import exceptions
from tempest.openstack.common import log as logging
import tempest.test
@ -130,10 +131,10 @@ class ClientTestBase(tempest.test.BaseTestCase):
cmd, stdout=stdout, stderr=stderr)
result, result_err = proc.communicate()
if not fail_ok and proc.returncode != 0:
raise CommandFailed(proc.returncode,
cmd,
result,
result_err)
raise exceptions.CommandFailed(proc.returncode,
cmd,
result,
result_err)
return result
def assertTableStruct(self, items, field_names):
@ -146,17 +147,3 @@ class ClientTestBase(tempest.test.BaseTestCase):
self.assertTrue(lines[0].startswith(beginning),
msg=('Beginning of first line has invalid content: %s'
% lines[:3]))
class CommandFailed(Exception):
def __init__(self, returncode, cmd, output, stderr):
super(CommandFailed, self).__init__()
self.returncode = returncode
self.cmd = cmd
self.stdout = output
self.stderr = stderr
def __str__(self):
return ("Command '%s' returned non-zero exit status %d.\n"
"stdout:\n%s\n"
"stderr:\n%s" % (self.cmd, self.returncode, self.stdout, self.stderr))

View File

@ -19,6 +19,7 @@ import testtools
from tempest import cli
from tempest import config
from tempest import exceptions
CONF = config.CONF
LOG = logging.getLogger(__name__)
@ -40,7 +41,7 @@ class SimpleReadOnlyCinderClientTest(cli.ClientTestBase):
super(SimpleReadOnlyCinderClientTest, cls).setUpClass()
def test_cinder_fake_action(self):
self.assertRaises(cli.CommandFailed,
self.assertRaises(exceptions.CommandFailed,
self.cinder,
'this-does-not-exist')
@ -65,7 +66,7 @@ class SimpleReadOnlyCinderClientTest(cli.ClientTestBase):
'Attached to'])
self.cinder('list', params='--all-tenants 1')
self.cinder('list', params='--all-tenants 0')
self.assertRaises(cli.CommandFailed,
self.assertRaises(exceptions.CommandFailed,
self.cinder,
'list',
params='--all-tenants bad')

View File

@ -17,6 +17,7 @@ import re
from tempest import cli
from tempest import config
from tempest import exceptions
from tempest.openstack.common import log as logging
CONF = config.CONF
@ -40,7 +41,7 @@ class SimpleReadOnlyGlanceClientTest(cli.ClientTestBase):
super(SimpleReadOnlyGlanceClientTest, cls).setUpClass()
def test_glance_fake_action(self):
self.assertRaises(cli.CommandFailed,
self.assertRaises(exceptions.CommandFailed,
self.glance,
'this-does-not-exist')

View File

@ -17,6 +17,7 @@ import re
from tempest import cli
from tempest import config
from tempest import exceptions
from tempest.openstack.common import log as logging
CONF = config.CONF
@ -34,7 +35,7 @@ class SimpleReadOnlyKeystoneClientTest(cli.ClientTestBase):
"""
def test_admin_fake_action(self):
self.assertRaises(cli.CommandFailed,
self.assertRaises(exceptions.CommandFailed,
self.keystone,
'this-does-not-exist')

View File

@ -17,6 +17,7 @@ import re
from tempest import cli
from tempest import config
from tempest import exceptions
from tempest.openstack.common import log as logging
from tempest import test
@ -42,7 +43,7 @@ class SimpleReadOnlyNeutronClientTest(cli.ClientTestBase):
@test.attr(type='smoke')
def test_neutron_fake_action(self):
self.assertRaises(cli.CommandFailed,
self.assertRaises(exceptions.CommandFailed,
self.neutron,
'this-does-not-exist')
@ -88,7 +89,7 @@ class SimpleReadOnlyNeutronClientTest(cli.ClientTestBase):
def _test_neutron_lbaas_command(self, command):
try:
self.neutron(command)
except cli.CommandFailed as e:
except exceptions.CommandFailed as e:
if '404 Not Found' not in e.stderr:
self.fail('%s: Unexpected failure.' % command)

View File

@ -17,6 +17,7 @@ import testtools
from tempest import cli
from tempest import config
from tempest import exceptions
from tempest.openstack.common import log as logging
import tempest.test
@ -47,7 +48,7 @@ class SimpleReadOnlyNovaClientTest(cli.ClientTestBase):
super(SimpleReadOnlyNovaClientTest, cls).setUpClass()
def test_admin_fake_action(self):
self.assertRaises(cli.CommandFailed,
self.assertRaises(exceptions.CommandFailed,
self.nova,
'this-does-nova-exist')
@ -84,11 +85,11 @@ class SimpleReadOnlyNovaClientTest(cli.ClientTestBase):
self.nova('endpoints')
def test_admin_flavor_acces_list(self):
self.assertRaises(cli.CommandFailed,
self.assertRaises(exceptions.CommandFailed,
self.nova,
'flavor-access-list')
# Failed to get access list for public flavor type
self.assertRaises(cli.CommandFailed,
self.assertRaises(exceptions.CommandFailed,
self.nova,
'flavor-access-list',
params='--flavor m1.tiny')
@ -125,7 +126,7 @@ class SimpleReadOnlyNovaClientTest(cli.ClientTestBase):
self.nova('list')
self.nova('list', params='--all-tenants 1')
self.nova('list', params='--all-tenants 0')
self.assertRaises(cli.CommandFailed,
self.assertRaises(exceptions.CommandFailed,
self.nova,
'list',
params='--all-tenants bad')

View File

@ -15,6 +15,7 @@
from tempest import cli
from tempest import config
from tempest import exceptions
from tempest.openstack.common import log as logging
@ -46,7 +47,7 @@ class SimpleReadOnlyNovaManageTest(cli.ClientTestBase):
super(SimpleReadOnlyNovaManageTest, cls).setUpClass()
def test_admin_fake_action(self):
self.assertRaises(cli.CommandFailed,
self.assertRaises(exceptions.CommandFailed,
self.nova_manage,
'this-does-nova-exist')

View File

@ -17,6 +17,7 @@ import re
from tempest import cli
from tempest import config
from tempest import exceptions
from tempest import test
CONF = config.CONF
@ -41,7 +42,7 @@ class SimpleReadOnlySaharaClientTest(cli.ClientTestBase):
@test.attr(type='negative')
def test_sahara_fake_action(self):
self.assertRaises(cli.CommandFailed,
self.assertRaises(exceptions.CommandFailed,
self.sahara,
'this-does-not-exist')

View File

@ -17,6 +17,7 @@ import re
from tempest import cli
from tempest import config
from tempest import exceptions
CONF = config.CONF
@ -37,7 +38,7 @@ class SimpleReadOnlySwiftClientTest(cli.ClientTestBase):
super(SimpleReadOnlySwiftClientTest, cls).setUpClass()
def test_swift_fake_action(self):
self.assertRaises(cli.CommandFailed,
self.assertRaises(exceptions.CommandFailed,
self.swift,
'this-does-not-exist')

View File

@ -211,3 +211,17 @@ class UnexpectedResponseCode(RestClientException):
class InvalidStructure(TempestException):
message = "Invalid structure of table with details"
class CommandFailed(Exception):
def __init__(self, returncode, cmd, output, stderr):
super(CommandFailed, self).__init__()
self.returncode = returncode
self.cmd = cmd
self.stdout = output
self.stderr = stderr
def __str__(self):
return ("Command '%s' returned non-zero exit status %d.\n"
"stdout:\n%s\n"
"stderr:\n%s" % (self.cmd, self.returncode, self.stdout, self.stderr))

View File

@ -10,7 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from tempest import cli
from tempest import exceptions
from tempest.tests import base
@ -22,8 +22,8 @@ class TestOutputParser(base.TestCase):
stdout = "output"
stderr = "error"
try:
raise cli.CommandFailed(returncode, cmd, stdout, stderr)
except cli.CommandFailed as e:
raise exceptions.CommandFailed(returncode, cmd, stdout, stderr)
except exceptions.CommandFailed as e:
self.assertIn(str(returncode), str(e))
self.assertIn(cmd, str(e))
self.assertIn(stdout, str(e))