root / qa / qa_job_utils.py @ 343c9ed2
History | View | Annotate | Download (4.4 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2014 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""QA utility functions for testing jobs
|
23 |
|
24 |
"""
|
25 |
|
26 |
import re |
27 |
|
28 |
from ganeti import constants |
29 |
from ganeti import locking |
30 |
from ganeti import utils |
31 |
|
32 |
import qa_config |
33 |
import qa_error |
34 |
|
35 |
from qa_utils import AssertCommand, GetCommandOutput, GetObjectInfo |
36 |
|
37 |
|
38 |
AVAILABLE_LOCKS = [locking.LEVEL_NODE, ] |
39 |
|
40 |
|
41 |
def _GetOutputFromMaster(cmd): |
42 |
""" Gets the output of a command executed on master.
|
43 |
|
44 |
"""
|
45 |
if isinstance(cmd, basestring): |
46 |
cmdstr = cmd |
47 |
else:
|
48 |
cmdstr = utils.ShellQuoteArgs(cmd) |
49 |
|
50 |
# Necessary due to the stderr stream not being captured properly on the
|
51 |
# buildbot
|
52 |
cmdstr += " 2>&1"
|
53 |
|
54 |
return GetCommandOutput(qa_config.GetMasterNode().primary, cmdstr)
|
55 |
|
56 |
|
57 |
def ExecuteJobProducingCommand(cmd): |
58 |
""" Executes a command that contains the --submit flag, and returns a job id.
|
59 |
|
60 |
@type cmd: list of string
|
61 |
@param cmd: The command to execute, broken into constituent components.
|
62 |
|
63 |
"""
|
64 |
job_id_output = _GetOutputFromMaster(cmd) |
65 |
|
66 |
possible_job_ids = re.findall("JobID: ([0-9]+)", job_id_output)
|
67 |
if len(possible_job_ids) != 1: |
68 |
raise qa_error.Error("Cannot parse command output to find job id: output " |
69 |
"is %s" % job_id_output)
|
70 |
|
71 |
return int(possible_job_ids[0]) |
72 |
|
73 |
|
74 |
def _StartDelayFunction(locks, timeout): |
75 |
""" Starts the gnt-debug delay option with the given locks and timeout.
|
76 |
|
77 |
"""
|
78 |
# The interruptible switch must be used
|
79 |
cmd = ["gnt-debug", "delay", "-i", "--submit", "--no-master"] |
80 |
|
81 |
for node in locks.get(locking.LEVEL_NODE, []): |
82 |
cmd.append("-n%s" % node)
|
83 |
|
84 |
cmd.append(str(timeout))
|
85 |
|
86 |
job_id = ExecuteJobProducingCommand(cmd) |
87 |
job_info = GetObjectInfo(["gnt-job", "info", str(job_id)]) |
88 |
execution_logs = job_info[0]["Opcodes"][0]["Execution log"] |
89 |
|
90 |
is_termination_info_fn = \ |
91 |
lambda e: e["Content"][1] == constants.ELOG_DELAY_TEST |
92 |
filtered_logs = filter(is_termination_info_fn, execution_logs)
|
93 |
|
94 |
if len(filtered_logs) != 1: |
95 |
raise qa_error.Error("Failure when trying to retrieve delay termination " |
96 |
"information")
|
97 |
|
98 |
_, _, (socket_path, ) = filtered_logs[0]["Content"] |
99 |
|
100 |
return socket_path
|
101 |
|
102 |
|
103 |
def _TerminateDelayFunction(termination_socket): |
104 |
""" Terminates the delay function by communicating with the domain socket.
|
105 |
|
106 |
"""
|
107 |
AssertCommand("echo a | socat -u stdin UNIX-CLIENT:%s" % termination_socket)
|
108 |
|
109 |
|
110 |
# TODO: Can this be done as a decorator? Implement as needed.
|
111 |
def RunWithLocks(fn, locks, timeout, *args, **kwargs): |
112 |
""" Runs the given function, acquiring a set of locks beforehand.
|
113 |
|
114 |
@type fn: function
|
115 |
@param fn: The function to invoke.
|
116 |
@type locks: dict of string to list of string
|
117 |
@param locks: The locks to acquire, per lock category.
|
118 |
@type timeout: number
|
119 |
@param timeout: The number of seconds the locks should be held before
|
120 |
expiring.
|
121 |
|
122 |
This function allows a set of locks to be acquired in preparation for a QA
|
123 |
test, to try and see if the function can run in parallel with other
|
124 |
operations.
|
125 |
|
126 |
The current version simply creates the locks, which expire after a given
|
127 |
timeout, and attempts to invoke the provided function.
|
128 |
|
129 |
This will probably block the QA, and future versions will address this.
|
130 |
|
131 |
A default timeout is not provided by design - the test creator must make a
|
132 |
good conservative estimate.
|
133 |
|
134 |
"""
|
135 |
if filter(lambda l_type: l_type not in AVAILABLE_LOCKS, locks): |
136 |
raise qa_error.Error("Attempted to acquire locks that cannot yet be " |
137 |
"acquired in the course of a QA test.")
|
138 |
|
139 |
# The watcher may interfere by issuing its own jobs - therefore pause it
|
140 |
AssertCommand(["gnt-cluster", "watcher", "pause", "12h"]) |
141 |
|
142 |
termination_socket = _StartDelayFunction(locks, timeout) |
143 |
|
144 |
fn(*args, **kwargs) |
145 |
|
146 |
_TerminateDelayFunction(termination_socket) |
147 |
|
148 |
# Revive the watcher
|
149 |
AssertCommand(["gnt-cluster", "watcher", "continue"]) |