Revision 587f8ff6 qa/qa_utils.py
b/qa/qa_utils.py | ||
---|---|---|
1 | 1 |
# |
2 | 2 |
# |
3 | 3 |
|
4 |
# Copyright (C) 2007, 2011, 2012 Google Inc. |
|
4 |
# Copyright (C) 2007, 2011, 2012, 2013 Google Inc.
|
|
5 | 5 |
# |
6 | 6 |
# This program is free software; you can redistribute it and/or modify |
7 | 7 |
# it under the terms of the GNU General Public License as published by |
... | ... | |
148 | 148 |
return result |
149 | 149 |
|
150 | 150 |
|
151 |
def _AssertRetCode(rcode, fail, cmdstr, nodename): |
|
152 |
"""Check the return value from a command and possibly raise an exception. |
|
153 |
|
|
154 |
""" |
|
155 |
if fail and rcode == 0: |
|
156 |
raise qa_error.Error("Command '%s' on node %s was expected to fail but" |
|
157 |
" didn't" % (cmdstr, nodename)) |
|
158 |
elif not fail and rcode != 0: |
|
159 |
raise qa_error.Error("Command '%s' on node %s failed, exit code %s" % |
|
160 |
(cmdstr, nodename, rcode)) |
|
161 |
|
|
162 |
|
|
151 | 163 |
def AssertCommand(cmd, fail=False, node=None): |
152 | 164 |
"""Checks that a remote command succeeds. |
153 | 165 |
|
... | ... | |
173 | 185 |
cmdstr = utils.ShellQuoteArgs(cmd) |
174 | 186 |
|
175 | 187 |
rcode = StartSSH(nodename, cmdstr).wait() |
176 |
|
|
177 |
if fail: |
|
178 |
if rcode == 0: |
|
179 |
raise qa_error.Error("Command '%s' on node %s was expected to fail but" |
|
180 |
" didn't" % (cmdstr, nodename)) |
|
181 |
else: |
|
182 |
if rcode != 0: |
|
183 |
raise qa_error.Error("Command '%s' on node %s failed, exit code %s" % |
|
184 |
(cmdstr, nodename, rcode)) |
|
188 |
_AssertRetCode(rcode, fail, cmdstr, nodename) |
|
185 | 189 |
|
186 | 190 |
return rcode |
187 | 191 |
|
... | ... | |
278 | 282 |
utils.RemoveFile(sname) |
279 | 283 |
|
280 | 284 |
|
281 |
def GetCommandOutput(node, cmd, tty=None): |
|
285 |
def GetCommandOutput(node, cmd, tty=None, fail=False):
|
|
282 | 286 |
"""Returns the output of a command executed on the given node. |
283 | 287 |
|
288 |
@type node: string |
|
289 |
@param node: node the command should run on |
|
290 |
@type cmd: string |
|
291 |
@param cmd: command to be executed in the node (cannot be empty or None) |
|
292 |
@type tty: bool or None |
|
293 |
@param tty: if we should use tty; if None, it will be auto-detected |
|
294 |
@type fail: bool |
|
295 |
@param fail: whether the command is expected to fail |
|
284 | 296 |
""" |
297 |
assert cmd |
|
285 | 298 |
p = StartLocalCommand(GetSSHCommand(node, cmd, tty=tty), |
286 | 299 |
stdout=subprocess.PIPE) |
287 |
AssertEqual(p.wait(), 0) |
|
300 |
rcode = p.wait() |
|
301 |
_AssertRetCode(rcode, fail, node, cmd) |
|
288 | 302 |
return p.stdout.read() |
289 | 303 |
|
290 | 304 |
|
Also available in: Unified diff