From 1a11790f2d26ec92858a3ffa100388310912fae1 Mon Sep 17 00:00:00 2001 From: Michael Still Date: Fri, 3 May 2013 10:47:39 +1000 Subject: Convert to using oslo's execute() method. There are some small warts (mostly around other execute() like methods which reuse the exception). I will fix these warts in later reviews. Change-Id: Ice9cdbdc5f3e5a9f8365f5d99acf1863a9fe3e7a --- nova/tests/baremetal/test_virtual_power_driver.py | 7 +- nova/tests/fake_utils.py | 4 +- nova/tests/network/test_manager.py | 3 +- nova/tests/test_crypto.py | 3 +- nova/tests/test_powervm.py | 4 +- nova/tests/test_utils.py | 86 +---------------------- 6 files changed, 14 insertions(+), 93 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/baremetal/test_virtual_power_driver.py b/nova/tests/baremetal/test_virtual_power_driver.py index b98ef5182..4cac0a3ba 100644 --- a/nova/tests/baremetal/test_virtual_power_driver.py +++ b/nova/tests/baremetal/test_virtual_power_driver.py @@ -22,6 +22,7 @@ import mox from oslo.config import cfg from nova import exception +from nova.openstack.common import processutils from nova.tests.baremetal.db import base as bm_db_base from nova.tests.baremetal.db import utils as bm_db_utils from nova.tests.image import fake as fake_image @@ -371,7 +372,7 @@ class VPDClassMethodsTestCase(BareMetalVPDTestCase): self.pm._set_connection().AndReturn(True) nutils.ssh_execute(None, '/usr/bin/VBoxManage test return', check_exit_code=True).\ - AndRaise(exception.ProcessExecutionError) + AndRaise(processutils.ProcessExecutionError) self.mox.ReplayAll() result = self.pm._run_command("test return") @@ -389,10 +390,10 @@ class VPDClassMethodsTestCase(BareMetalVPDTestCase): self.pm._check_for_node().AndReturn(['"testNode"']) nutils.ssh_execute('test', '/usr/bin/VBoxManage startvm ', check_exit_code=True).\ - AndRaise(exception.ProcessExecutionError) + AndRaise(processutils.ProcessExecutionError) nutils.ssh_execute('test', '/usr/bin/VBoxManage list runningvms', check_exit_code=True).\ - AndRaise(exception.ProcessExecutionError) + AndRaise(processutils.ProcessExecutionError) self.mox.ReplayAll() self.pm._connection = 'test' diff --git a/nova/tests/fake_utils.py b/nova/tests/fake_utils.py index 08a84e73f..6a295b43b 100644 --- a/nova/tests/fake_utils.py +++ b/nova/tests/fake_utils.py @@ -20,8 +20,8 @@ import re from eventlet import greenthread -from nova import exception from nova.openstack.common import log as logging +from nova.openstack.common import processutils from nova import utils LOG = logging.getLogger(__name__) @@ -92,7 +92,7 @@ def fake_execute(*cmd_parts, **kwargs): attempts=attempts, run_as_root=run_as_root, check_exit_code=check_exit_code) - except exception.ProcessExecutionError as e: + except processutils.ProcessExecutionError as e: LOG.debug(_('Faked command raised an exception %s'), e) raise diff --git a/nova/tests/network/test_manager.py b/nova/tests/network/test_manager.py index def993f62..b8d7115a9 100644 --- a/nova/tests/network/test_manager.py +++ b/nova/tests/network/test_manager.py @@ -32,6 +32,7 @@ from nova.network import model as net_model from nova.openstack.common.db import exception as db_exc from nova.openstack.common import importutils from nova.openstack.common import log as logging +from nova.openstack.common import processutils from nova.openstack.common import rpc from nova.openstack.common.rpc import common as rpc_common from nova import quota @@ -763,7 +764,7 @@ class VlanNetworkTestCase(test.TestCase): self.local = True def fake8(*args, **kwargs): - raise exception.ProcessExecutionError('', + raise processutils.ProcessExecutionError('', 'Cannot find device "em0"\n') def fake9(*args, **kwargs): diff --git a/nova/tests/test_crypto.py b/nova/tests/test_crypto.py index e328babfe..2264273d7 100644 --- a/nova/tests/test_crypto.py +++ b/nova/tests/test_crypto.py @@ -23,6 +23,7 @@ import mox from nova import crypto from nova import db from nova import exception +from nova.openstack.common import processutils from nova import test from nova import utils @@ -200,7 +201,7 @@ e6fCXWECgYEAqgpGvva5kJ1ISgNwnJbwiNw0sOT9BMOsdNZBElf0kJIIy6FMPvap '-inkey', sshkey, process_input=text) return dec - except exception.ProcessExecutionError as exc: + except processutils.ProcessExecutionError as exc: raise exception.DecryptionFailure(reason=exc.stderr) def test_ssh_encrypt_decrypt_text(self): diff --git a/nova/tests/test_powervm.py b/nova/tests/test_powervm.py index 96954269e..73299f23a 100644 --- a/nova/tests/test_powervm.py +++ b/nova/tests/test_powervm.py @@ -22,13 +22,13 @@ import contextlib from nova import context from nova import db -from nova import exception as nova_exception from nova import test from nova.compute import instance_types from nova.compute import power_state from nova.compute import task_states from nova.network import model as network_model +from nova.openstack.common import processutils from nova.tests import fake_network_cache_model from nova.tests.image import fake from nova.virt import images @@ -227,7 +227,7 @@ class PowerVMDriverTestCase(test.TestCase): self.powervm_connection._powervm, 'get_host_stats', lambda *x, **y: raise_( - (nova_exception.ProcessExecutionError('instance_name')))) + (processutils.ProcessExecutionError('instance_name')))) fake_net_info = network_model.NetworkInfo([ fake_network_cache_model.new_vif()]) self.assertRaises(exception.PowerVMLPARCreationFailed, diff --git a/nova/tests/test_utils.py b/nova/tests/test_utils.py index af6a9b9aa..6b7af48fc 100644 --- a/nova/tests/test_utils.py +++ b/nova/tests/test_utils.py @@ -30,6 +30,7 @@ from oslo.config import cfg import nova from nova import exception +from nova.openstack.common import processutils from nova.openstack.common import timeutils from nova import test from nova import utils @@ -72,89 +73,6 @@ class ByteConversionTest(test.TestCase): self.assertRaises(TypeError, utils.to_bytes, v) -class ExecuteTestCase(test.TestCase): - - def test_retry_on_failure(self): - fd, tmpfilename = tempfile.mkstemp() - _, tmpfilename2 = tempfile.mkstemp() - try: - fp = os.fdopen(fd, 'w+') - fp.write('''#!/bin/sh -# If stdin fails to get passed during one of the runs, make a note. -if ! grep -q foo -then - echo 'failure' > "$1" -fi -# If stdin has failed to get passed during this or a previous run, exit early. -if grep failure "$1" -then - exit 1 -fi -runs="$(cat $1)" -if [ -z "$runs" ] -then - runs=0 -fi -runs=$(($runs + 1)) -echo $runs > "$1" -exit 1 -''') - fp.close() - os.chmod(tmpfilename, 0755) - self.assertRaises(exception.ProcessExecutionError, - utils.execute, - tmpfilename, tmpfilename2, attempts=10, - process_input='foo', - delay_on_retry=False) - fp = open(tmpfilename2, 'r') - runs = fp.read() - fp.close() - self.assertNotEquals(runs.strip(), 'failure', 'stdin did not ' - 'always get passed ' - 'correctly') - runs = int(runs.strip()) - self.assertEquals(runs, 10, - 'Ran %d times instead of 10.' % (runs,)) - finally: - os.unlink(tmpfilename) - os.unlink(tmpfilename2) - - def test_unknown_kwargs_raises_error(self): - self.assertRaises(exception.NovaException, - utils.execute, - '/usr/bin/env', 'true', - this_is_not_a_valid_kwarg=True) - - def test_check_exit_code_boolean(self): - utils.execute('/usr/bin/env', 'false', check_exit_code=False) - self.assertRaises(exception.ProcessExecutionError, - utils.execute, - '/usr/bin/env', 'false', check_exit_code=True) - - def test_no_retry_on_success(self): - fd, tmpfilename = tempfile.mkstemp() - _, tmpfilename2 = tempfile.mkstemp() - try: - fp = os.fdopen(fd, 'w+') - fp.write('''#!/bin/sh -# If we've already run, bail out. -grep -q foo "$1" && exit 1 -# Mark that we've run before. -echo foo > "$1" -# Check that stdin gets passed correctly. -grep foo -''') - fp.close() - os.chmod(tmpfilename, 0755) - utils.execute(tmpfilename, - tmpfilename2, - process_input='foo', - attempts=2) - finally: - os.unlink(tmpfilename) - os.unlink(tmpfilename2) - - class GetFromPathTestCase(test.TestCase): def test_tolerates_nones(self): f = utils.get_from_path @@ -429,7 +347,7 @@ class GenericUtilsTestCase(test.TestCase): def test_read_file_as_root(self): def fake_execute(*args, **kwargs): if args[1] == 'bad': - raise exception.ProcessExecutionError + raise processutils.ProcessExecutionError return 'fakecontents', None self.stubs.Set(utils, 'execute', fake_execute) -- cgit