1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright (c) 2011 Citrix Systems, Inc.
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
17
"""This modules stubs out functions in nova.utils
23
from eventlet import greenthread
25
from nova import exception
26
from nova import log as logging
27
from nova import utils
29
LOG = logging.getLogger('nova.tests.fake_utils')
31
_fake_execute_repliers = []
32
_fake_execute_log = []
35
def fake_execute_get_log():
36
return _fake_execute_log
39
def fake_execute_clear_log():
40
global _fake_execute_log
41
_fake_execute_log = []
44
def fake_execute_set_repliers(repliers):
45
"""Allows the client to configure replies to commands"""
46
global _fake_execute_repliers
47
_fake_execute_repliers = repliers
50
def fake_execute_default_reply_handler(*ignore_args, **ignore_kwargs):
51
"""A reply handler for commands that haven't been added to the reply
52
list. Returns empty strings for stdout and stderr
57
def fake_execute(*cmd_parts, **kwargs):
58
"""This function stubs out execute, optionally executing
59
a preconfigued function to return expected data
61
global _fake_execute_repliers
63
process_input = kwargs.get('process_input', None)
64
addl_env = kwargs.get('addl_env', None)
65
check_exit_code = kwargs.get('check_exit_code', 0)
66
cmd_str = ' '.join(str(part) for part in cmd_parts)
68
LOG.debug(_("Faking execution of cmd (subprocess): %s"), cmd_str)
69
_fake_execute_log.append(cmd_str)
71
reply_handler = fake_execute_default_reply_handler
73
for fake_replier in _fake_execute_repliers:
74
if re.match(fake_replier[0], cmd_str):
75
reply_handler = fake_replier[1]
76
LOG.debug(_('Faked command matched %s') % fake_replier[0])
79
if isinstance(reply_handler, basestring):
80
# If the reply handler is a string, return it as stdout
81
reply = reply_handler, ''
84
# Alternative is a function, so call it
85
reply = reply_handler(cmd_parts,
86
process_input=process_input,
88
check_exit_code=check_exit_code)
89
except exception.ProcessExecutionError as e:
90
LOG.debug(_('Faked command raised an exception %s' % str(e)))
95
LOG.debug(_("Reply to faked command is stdout='%(stdout)s' "
96
"stderr='%(stderr)s'") % locals())
98
# Replicate the sleep call in the real function
103
def stub_out_utils_execute(stubs):
104
fake_execute_set_repliers([])
105
fake_execute_clear_log()
106
stubs.Set(utils, 'execute', fake_execute)