root / qa / qa_utils.py @ 93029a5b
History | View | Annotate | Download (25.6 kB)
1 | c68d1f43 | Michael Hanselmann | #
|
---|---|---|---|
2 | c68d1f43 | Michael Hanselmann | #
|
3 | c68d1f43 | Michael Hanselmann | |
4 | 587f8ff6 | Bernardo Dal Seno | # Copyright (C) 2007, 2011, 2012, 2013 Google Inc.
|
5 | cec9845c | Michael Hanselmann | #
|
6 | cec9845c | Michael Hanselmann | # This program is free software; you can redistribute it and/or modify
|
7 | cec9845c | Michael Hanselmann | # it under the terms of the GNU General Public License as published by
|
8 | cec9845c | Michael Hanselmann | # the Free Software Foundation; either version 2 of the License, or
|
9 | cec9845c | Michael Hanselmann | # (at your option) any later version.
|
10 | cec9845c | Michael Hanselmann | #
|
11 | cec9845c | Michael Hanselmann | # This program is distributed in the hope that it will be useful, but
|
12 | cec9845c | Michael Hanselmann | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | cec9845c | Michael Hanselmann | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | cec9845c | Michael Hanselmann | # General Public License for more details.
|
15 | cec9845c | Michael Hanselmann | #
|
16 | cec9845c | Michael Hanselmann | # You should have received a copy of the GNU General Public License
|
17 | cec9845c | Michael Hanselmann | # along with this program; if not, write to the Free Software
|
18 | cec9845c | Michael Hanselmann | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | cec9845c | Michael Hanselmann | # 02110-1301, USA.
|
20 | cec9845c | Michael Hanselmann | |
21 | cec9845c | Michael Hanselmann | |
22 | cec9845c | Michael Hanselmann | """Utilities for QA tests.
|
23 | cec9845c | Michael Hanselmann |
|
24 | cec9845c | Michael Hanselmann | """
|
25 | cec9845c | Michael Hanselmann | |
26 | ec996117 | Bernardo Dal Seno | import copy |
27 | 0e79564a | Bernardo Dal Seno | import operator |
28 | cec9845c | Michael Hanselmann | import os |
29 | 0e79564a | Bernardo Dal Seno | import random |
30 | e6ce18ac | René Nussbaumer | import re |
31 | a02dbfca | Petr Pudlak | import socket |
32 | cec9845c | Michael Hanselmann | import subprocess |
33 | 0e79564a | Bernardo Dal Seno | import sys |
34 | f7e6f3c8 | Iustin Pop | import tempfile |
35 | 0e79564a | Bernardo Dal Seno | import yaml |
36 | cec9845c | Michael Hanselmann | |
37 | c9e05005 | Michael Hanselmann | try:
|
38 | c9e05005 | Michael Hanselmann | import functools |
39 | c9e05005 | Michael Hanselmann | except ImportError, err: |
40 | c9e05005 | Michael Hanselmann | raise ImportError("Python 2.5 or higher is required: %s" % err) |
41 | c9e05005 | Michael Hanselmann | |
42 | cec9845c | Michael Hanselmann | from ganeti import utils |
43 | 288d6440 | Michael Hanselmann | from ganeti import compat |
44 | 2214cf14 | Michael Hanselmann | from ganeti import constants |
45 | c9e05005 | Michael Hanselmann | from ganeti import ht |
46 | 48967eb0 | Michael Hanselmann | from ganeti import pathutils |
47 | 50eaa5da | Michael Hanselmann | from ganeti import vcluster |
48 | cec9845c | Michael Hanselmann | |
49 | d5a9b556 | Petr Pudlak | import colors |
50 | cec9845c | Michael Hanselmann | import qa_config |
51 | cec9845c | Michael Hanselmann | import qa_error |
52 | cec9845c | Michael Hanselmann | |
53 | cec9845c | Michael Hanselmann | |
54 | 23269c5b | Michael Hanselmann | _INFO_SEQ = None
|
55 | 23269c5b | Michael Hanselmann | _WARNING_SEQ = None
|
56 | 23269c5b | Michael Hanselmann | _ERROR_SEQ = None
|
57 | 23269c5b | Michael Hanselmann | _RESET_SEQ = None
|
58 | 23269c5b | Michael Hanselmann | |
59 | f7e6f3c8 | Iustin Pop | _MULTIPLEXERS = {} |
60 | f7e6f3c8 | Iustin Pop | |
61 | c9e05005 | Michael Hanselmann | #: Unique ID per QA run
|
62 | c9e05005 | Michael Hanselmann | _RUN_UUID = utils.NewUUID() |
63 | c9e05005 | Michael Hanselmann | |
64 | afd5ca04 | Iustin Pop | #: Path to the QA query output log file
|
65 | afd5ca04 | Iustin Pop | _QA_OUTPUT = pathutils.GetLogFilename("qa-output")
|
66 | afd5ca04 | Iustin Pop | |
67 | c9e05005 | Michael Hanselmann | |
68 | c9e05005 | Michael Hanselmann | (INST_DOWN, |
69 | c9e05005 | Michael Hanselmann | INST_UP) = range(500, 502) |
70 | c9e05005 | Michael Hanselmann | |
71 | c9e05005 | Michael Hanselmann | (FIRST_ARG, |
72 | c9e05005 | Michael Hanselmann | RETURN_VALUE) = range(1000, 1002) |
73 | c9e05005 | Michael Hanselmann | |
74 | 23269c5b | Michael Hanselmann | |
75 | 23269c5b | Michael Hanselmann | def _SetupColours(): |
76 | 23269c5b | Michael Hanselmann | """Initializes the colour constants.
|
77 | 23269c5b | Michael Hanselmann |
|
78 | 23269c5b | Michael Hanselmann | """
|
79 | b459a848 | Andrea Spadaccini | # pylint: disable=W0603
|
80 | 3582eef6 | Iustin Pop | # due to global usage
|
81 | 23269c5b | Michael Hanselmann | global _INFO_SEQ, _WARNING_SEQ, _ERROR_SEQ, _RESET_SEQ
|
82 | 23269c5b | Michael Hanselmann | |
83 | dfe11bad | Michael Hanselmann | # Don't use colours if stdout isn't a terminal
|
84 | dfe11bad | Michael Hanselmann | if not sys.stdout.isatty(): |
85 | dfe11bad | Michael Hanselmann | return
|
86 | dfe11bad | Michael Hanselmann | |
87 | 23269c5b | Michael Hanselmann | try:
|
88 | 23269c5b | Michael Hanselmann | import curses |
89 | 23269c5b | Michael Hanselmann | except ImportError: |
90 | 23269c5b | Michael Hanselmann | # Don't use colours if curses module can't be imported
|
91 | 23269c5b | Michael Hanselmann | return
|
92 | 23269c5b | Michael Hanselmann | |
93 | d3737aca | Petr Pudlak | try:
|
94 | d3737aca | Petr Pudlak | curses.setupterm() |
95 | d3737aca | Petr Pudlak | except curses.error:
|
96 | d3737aca | Petr Pudlak | # Probably a non-standard terminal, don't use colours then
|
97 | d3737aca | Petr Pudlak | return
|
98 | 23269c5b | Michael Hanselmann | |
99 | 23269c5b | Michael Hanselmann | _RESET_SEQ = curses.tigetstr("op")
|
100 | 23269c5b | Michael Hanselmann | |
101 | 23269c5b | Michael Hanselmann | setaf = curses.tigetstr("setaf")
|
102 | 23269c5b | Michael Hanselmann | _INFO_SEQ = curses.tparm(setaf, curses.COLOR_GREEN) |
103 | 23269c5b | Michael Hanselmann | _WARNING_SEQ = curses.tparm(setaf, curses.COLOR_YELLOW) |
104 | 23269c5b | Michael Hanselmann | _ERROR_SEQ = curses.tparm(setaf, curses.COLOR_RED) |
105 | 23269c5b | Michael Hanselmann | |
106 | 23269c5b | Michael Hanselmann | |
107 | 23269c5b | Michael Hanselmann | _SetupColours() |
108 | 23269c5b | Michael Hanselmann | |
109 | 23269c5b | Michael Hanselmann | |
110 | 93029a5b | Jose A. Lopes | def AssertIn(item, sequence, msg=""): |
111 | eaef8a05 | Michael Hanselmann | """Raises an error when item is not in sequence.
|
112 | eaef8a05 | Michael Hanselmann |
|
113 | eaef8a05 | Michael Hanselmann | """
|
114 | eaef8a05 | Michael Hanselmann | if item not in sequence: |
115 | 93029a5b | Jose A. Lopes | if msg:
|
116 | 93029a5b | Jose A. Lopes | raise qa_error.Error("%s: %r not in %r" % (msg, item, sequence)) |
117 | 93029a5b | Jose A. Lopes | else:
|
118 | 93029a5b | Jose A. Lopes | raise qa_error.Error("%r not in %r" % (item, sequence)) |
119 | eaef8a05 | Michael Hanselmann | |
120 | eaef8a05 | Michael Hanselmann | |
121 | 79eac09b | Michael Hanselmann | def AssertNotIn(item, sequence): |
122 | 79eac09b | Michael Hanselmann | """Raises an error when item is in sequence.
|
123 | 79eac09b | Michael Hanselmann |
|
124 | 79eac09b | Michael Hanselmann | """
|
125 | 79eac09b | Michael Hanselmann | if item in sequence: |
126 | d0c8c01d | Iustin Pop | raise qa_error.Error("%r in %r" % (item, sequence)) |
127 | 79eac09b | Michael Hanselmann | |
128 | 79eac09b | Michael Hanselmann | |
129 | f985ecbd | Jose A. Lopes | def AssertEqual(first, second, msg=""): |
130 | cec9845c | Michael Hanselmann | """Raises an error when values aren't equal.
|
131 | cec9845c | Michael Hanselmann |
|
132 | cec9845c | Michael Hanselmann | """
|
133 | cec9845c | Michael Hanselmann | if not first == second: |
134 | f985ecbd | Jose A. Lopes | if msg:
|
135 | f985ecbd | Jose A. Lopes | raise qa_error.Error("%s: %r == %r" % (msg, first, second)) |
136 | f985ecbd | Jose A. Lopes | else:
|
137 | f985ecbd | Jose A. Lopes | raise qa_error.Error("%r == %r" % (first, second)) |
138 | e8ae0c20 | Michael Hanselmann | |
139 | e8ae0c20 | Michael Hanselmann | |
140 | e6ce18ac | René Nussbaumer | def AssertMatch(string, pattern): |
141 | e6ce18ac | René Nussbaumer | """Raises an error when string doesn't match regexp pattern.
|
142 | e6ce18ac | René Nussbaumer |
|
143 | e6ce18ac | René Nussbaumer | """
|
144 | e6ce18ac | René Nussbaumer | if not re.match(pattern, string): |
145 | e6ce18ac | René Nussbaumer | raise qa_error.Error("%r doesn't match /%r/" % (string, pattern)) |
146 | e6ce18ac | René Nussbaumer | |
147 | e6ce18ac | René Nussbaumer | |
148 | 6998aefe | Michael Hanselmann | def _GetName(entity, fn): |
149 | 889bed16 | Michael Hanselmann | """Tries to get name of an entity.
|
150 | 889bed16 | Michael Hanselmann |
|
151 | 889bed16 | Michael Hanselmann | @type entity: string or dict
|
152 | 6998aefe | Michael Hanselmann | @param fn: Function retrieving name from entity
|
153 | 889bed16 | Michael Hanselmann |
|
154 | 889bed16 | Michael Hanselmann | """
|
155 | 889bed16 | Michael Hanselmann | if isinstance(entity, basestring): |
156 | 889bed16 | Michael Hanselmann | result = entity |
157 | 889bed16 | Michael Hanselmann | else:
|
158 | 6998aefe | Michael Hanselmann | result = fn(entity) |
159 | 889bed16 | Michael Hanselmann | |
160 | 889bed16 | Michael Hanselmann | if not ht.TNonEmptyString(result): |
161 | 889bed16 | Michael Hanselmann | raise Exception("Invalid name '%s'" % result) |
162 | 889bed16 | Michael Hanselmann | |
163 | 889bed16 | Michael Hanselmann | return result
|
164 | 889bed16 | Michael Hanselmann | |
165 | 889bed16 | Michael Hanselmann | |
166 | 587f8ff6 | Bernardo Dal Seno | def _AssertRetCode(rcode, fail, cmdstr, nodename): |
167 | 587f8ff6 | Bernardo Dal Seno | """Check the return value from a command and possibly raise an exception.
|
168 | 587f8ff6 | Bernardo Dal Seno |
|
169 | 587f8ff6 | Bernardo Dal Seno | """
|
170 | 587f8ff6 | Bernardo Dal Seno | if fail and rcode == 0: |
171 | 587f8ff6 | Bernardo Dal Seno | raise qa_error.Error("Command '%s' on node %s was expected to fail but" |
172 | 587f8ff6 | Bernardo Dal Seno | " didn't" % (cmdstr, nodename))
|
173 | 587f8ff6 | Bernardo Dal Seno | elif not fail and rcode != 0: |
174 | 587f8ff6 | Bernardo Dal Seno | raise qa_error.Error("Command '%s' on node %s failed, exit code %s" % |
175 | 587f8ff6 | Bernardo Dal Seno | (cmdstr, nodename, rcode)) |
176 | 587f8ff6 | Bernardo Dal Seno | |
177 | 587f8ff6 | Bernardo Dal Seno | |
178 | 56b9f2db | Iustin Pop | def AssertCommand(cmd, fail=False, node=None, log_cmd=True): |
179 | 2f4b4f78 | Iustin Pop | """Checks that a remote command succeeds.
|
180 | 2f4b4f78 | Iustin Pop |
|
181 | 2f4b4f78 | Iustin Pop | @param cmd: either a string (the command to execute) or a list (to
|
182 | 2f4b4f78 | Iustin Pop | be converted using L{utils.ShellQuoteArgs} into a string)
|
183 | 2f4b4f78 | Iustin Pop | @type fail: boolean
|
184 | 2f4b4f78 | Iustin Pop | @param fail: if the command is expected to fail instead of succeeding
|
185 | 2f4b4f78 | Iustin Pop | @param node: if passed, it should be the node on which the command
|
186 | 2f4b4f78 | Iustin Pop | should be executed, instead of the master node (can be either a
|
187 | 2f4b4f78 | Iustin Pop | dict or a string)
|
188 | 56b9f2db | Iustin Pop | @param log_cmd: if False, the command won't be logged (simply passed to
|
189 | 56b9f2db | Iustin Pop | StartSSH)
|
190 | 05325a35 | Bernardo Dal Seno | @return: the return code of the command
|
191 | 05325a35 | Bernardo Dal Seno | @raise qa_error.Error: if the command fails when it shouldn't or vice versa
|
192 | 2f4b4f78 | Iustin Pop |
|
193 | 2f4b4f78 | Iustin Pop | """
|
194 | 2f4b4f78 | Iustin Pop | if node is None: |
195 | 2f4b4f78 | Iustin Pop | node = qa_config.GetMasterNode() |
196 | 2f4b4f78 | Iustin Pop | |
197 | 6998aefe | Michael Hanselmann | nodename = _GetName(node, operator.attrgetter("primary"))
|
198 | 2f4b4f78 | Iustin Pop | |
199 | 2f4b4f78 | Iustin Pop | if isinstance(cmd, basestring): |
200 | 2f4b4f78 | Iustin Pop | cmdstr = cmd |
201 | 2f4b4f78 | Iustin Pop | else:
|
202 | 2f4b4f78 | Iustin Pop | cmdstr = utils.ShellQuoteArgs(cmd) |
203 | 2f4b4f78 | Iustin Pop | |
204 | 56b9f2db | Iustin Pop | rcode = StartSSH(nodename, cmdstr, log_cmd=log_cmd).wait() |
205 | 587f8ff6 | Bernardo Dal Seno | _AssertRetCode(rcode, fail, cmdstr, nodename) |
206 | 2f4b4f78 | Iustin Pop | |
207 | 2214cf14 | Michael Hanselmann | return rcode
|
208 | 2214cf14 | Michael Hanselmann | |
209 | 2f4b4f78 | Iustin Pop | |
210 | afd5ca04 | Iustin Pop | def AssertRedirectedCommand(cmd, fail=False, node=None, log_cmd=True): |
211 | afd5ca04 | Iustin Pop | """Executes a command with redirected output.
|
212 | afd5ca04 | Iustin Pop |
|
213 | afd5ca04 | Iustin Pop | The log will go to the qa-output log file in the ganeti log
|
214 | afd5ca04 | Iustin Pop | directory on the node where the command is executed. The fail and
|
215 | afd5ca04 | Iustin Pop | node parameters are passed unchanged to AssertCommand.
|
216 | afd5ca04 | Iustin Pop |
|
217 | afd5ca04 | Iustin Pop | @param cmd: the command to be executed, as a list; a string is not
|
218 | afd5ca04 | Iustin Pop | supported
|
219 | afd5ca04 | Iustin Pop |
|
220 | afd5ca04 | Iustin Pop | """
|
221 | afd5ca04 | Iustin Pop | if not isinstance(cmd, list): |
222 | afd5ca04 | Iustin Pop | raise qa_error.Error("Non-list passed to AssertRedirectedCommand") |
223 | afd5ca04 | Iustin Pop | ofile = utils.ShellQuote(_QA_OUTPUT) |
224 | afd5ca04 | Iustin Pop | cmdstr = utils.ShellQuoteArgs(cmd) |
225 | afd5ca04 | Iustin Pop | AssertCommand("echo ---- $(date) %s ---- >> %s" % (cmdstr, ofile),
|
226 | afd5ca04 | Iustin Pop | fail=False, node=node, log_cmd=False) |
227 | afd5ca04 | Iustin Pop | return AssertCommand(cmdstr + " >> %s" % ofile, |
228 | afd5ca04 | Iustin Pop | fail=fail, node=node, log_cmd=log_cmd) |
229 | afd5ca04 | Iustin Pop | |
230 | afd5ca04 | Iustin Pop | |
231 | f14a8b15 | Iustin Pop | def GetSSHCommand(node, cmd, strict=True, opts=None, tty=None): |
232 | cec9845c | Michael Hanselmann | """Builds SSH command to be executed.
|
233 | cec9845c | Michael Hanselmann |
|
234 | 0a05f959 | Adeodato Simo | @type node: string
|
235 | 0a05f959 | Adeodato Simo | @param node: node the command should run on
|
236 | 0a05f959 | Adeodato Simo | @type cmd: string
|
237 | f7e6f3c8 | Iustin Pop | @param cmd: command to be executed in the node; if None or empty
|
238 | f7e6f3c8 | Iustin Pop | string, no command will be executed
|
239 | 0a05f959 | Adeodato Simo | @type strict: boolean
|
240 | 0a05f959 | Adeodato Simo | @param strict: whether to enable strict host key checking
|
241 | f7e6f3c8 | Iustin Pop | @type opts: list
|
242 | f7e6f3c8 | Iustin Pop | @param opts: list of additional options
|
243 | f14a8b15 | Iustin Pop | @type tty: boolean or None
|
244 | f14a8b15 | Iustin Pop | @param tty: if we should use tty; if None, will be auto-detected
|
245 | c68d1f43 | Michael Hanselmann |
|
246 | cec9845c | Michael Hanselmann | """
|
247 | 710bc88c | Iustin Pop | args = ["ssh", "-oEscapeChar=none", "-oBatchMode=yes", "-lroot"] |
248 | 50265802 | René Nussbaumer | |
249 | f14a8b15 | Iustin Pop | if tty is None: |
250 | f14a8b15 | Iustin Pop | tty = sys.stdout.isatty() |
251 | f14a8b15 | Iustin Pop | |
252 | 50265802 | René Nussbaumer | if tty:
|
253 | 50265802 | René Nussbaumer | args.append("-t")
|
254 | cec9845c | Michael Hanselmann | |
255 | cec9845c | Michael Hanselmann | if strict:
|
256 | d0c8c01d | Iustin Pop | tmp = "yes"
|
257 | cec9845c | Michael Hanselmann | else:
|
258 | d0c8c01d | Iustin Pop | tmp = "no"
|
259 | d0c8c01d | Iustin Pop | args.append("-oStrictHostKeyChecking=%s" % tmp)
|
260 | d0c8c01d | Iustin Pop | args.append("-oClearAllForwardings=yes")
|
261 | d0c8c01d | Iustin Pop | args.append("-oForwardAgent=yes")
|
262 | f7e6f3c8 | Iustin Pop | if opts:
|
263 | f7e6f3c8 | Iustin Pop | args.extend(opts) |
264 | f7e6f3c8 | Iustin Pop | if node in _MULTIPLEXERS: |
265 | f7e6f3c8 | Iustin Pop | spath = _MULTIPLEXERS[node][0]
|
266 | d0c8c01d | Iustin Pop | args.append("-oControlPath=%s" % spath)
|
267 | d0c8c01d | Iustin Pop | args.append("-oControlMaster=no")
|
268 | 50eaa5da | Michael Hanselmann | |
269 | 50eaa5da | Michael Hanselmann | (vcluster_master, vcluster_basedir) = \ |
270 | 50eaa5da | Michael Hanselmann | qa_config.GetVclusterSettings() |
271 | 50eaa5da | Michael Hanselmann | |
272 | 50eaa5da | Michael Hanselmann | if vcluster_master:
|
273 | 50eaa5da | Michael Hanselmann | args.append(vcluster_master) |
274 | 50eaa5da | Michael Hanselmann | args.append("%s/%s/cmd" % (vcluster_basedir, node))
|
275 | 50eaa5da | Michael Hanselmann | |
276 | 50eaa5da | Michael Hanselmann | if cmd:
|
277 | 50eaa5da | Michael Hanselmann | # For virtual clusters the whole command must be wrapped using the "cmd"
|
278 | 50eaa5da | Michael Hanselmann | # script, as that script sets a number of environment variables. If the
|
279 | 50eaa5da | Michael Hanselmann | # command contains shell meta characters the whole command needs to be
|
280 | 50eaa5da | Michael Hanselmann | # quoted.
|
281 | 50eaa5da | Michael Hanselmann | args.append(utils.ShellQuote(cmd)) |
282 | 50eaa5da | Michael Hanselmann | else:
|
283 | 50eaa5da | Michael Hanselmann | args.append(node) |
284 | 50eaa5da | Michael Hanselmann | |
285 | 50eaa5da | Michael Hanselmann | if cmd:
|
286 | 50eaa5da | Michael Hanselmann | args.append(cmd) |
287 | cec9845c | Michael Hanselmann | |
288 | cec9845c | Michael Hanselmann | return args
|
289 | cec9845c | Michael Hanselmann | |
290 | cec9845c | Michael Hanselmann | |
291 | 56b9f2db | Iustin Pop | def StartLocalCommand(cmd, _nolog_opts=False, log_cmd=True, **kwargs): |
292 | 5d831182 | Michael Hanselmann | """Starts a local command.
|
293 | 5d831182 | Michael Hanselmann |
|
294 | 5d831182 | Michael Hanselmann | """
|
295 | 56b9f2db | Iustin Pop | if log_cmd:
|
296 | 56b9f2db | Iustin Pop | if _nolog_opts:
|
297 | 56b9f2db | Iustin Pop | pcmd = [i for i in cmd if not i.startswith("-")] |
298 | 56b9f2db | Iustin Pop | else:
|
299 | 56b9f2db | Iustin Pop | pcmd = cmd |
300 | d5a9b556 | Petr Pudlak | print "%s %s" % (colors.colorize("Command:", colors.CYAN), |
301 | d5a9b556 | Petr Pudlak | utils.ShellQuoteArgs(pcmd)) |
302 | 5d831182 | Michael Hanselmann | return subprocess.Popen(cmd, shell=False, **kwargs) |
303 | 5d831182 | Michael Hanselmann | |
304 | 5d831182 | Michael Hanselmann | |
305 | 56b9f2db | Iustin Pop | def StartSSH(node, cmd, strict=True, log_cmd=True): |
306 | cec9845c | Michael Hanselmann | """Starts SSH.
|
307 | cec9845c | Michael Hanselmann |
|
308 | cec9845c | Michael Hanselmann | """
|
309 | 710bc88c | Iustin Pop | return StartLocalCommand(GetSSHCommand(node, cmd, strict=strict),
|
310 | 56b9f2db | Iustin Pop | _nolog_opts=True, log_cmd=log_cmd)
|
311 | 4b62db14 | Michael Hanselmann | |
312 | 4b62db14 | Michael Hanselmann | |
313 | f7e6f3c8 | Iustin Pop | def StartMultiplexer(node): |
314 | f7e6f3c8 | Iustin Pop | """Starts a multiplexer command.
|
315 | f7e6f3c8 | Iustin Pop |
|
316 | f7e6f3c8 | Iustin Pop | @param node: the node for which to open the multiplexer
|
317 | f7e6f3c8 | Iustin Pop |
|
318 | f7e6f3c8 | Iustin Pop | """
|
319 | f7e6f3c8 | Iustin Pop | if node in _MULTIPLEXERS: |
320 | f7e6f3c8 | Iustin Pop | return
|
321 | f7e6f3c8 | Iustin Pop | |
322 | f7e6f3c8 | Iustin Pop | # Note: yes, we only need mktemp, since we'll remove the file anyway
|
323 | f7e6f3c8 | Iustin Pop | sname = tempfile.mktemp(prefix="ganeti-qa-multiplexer.")
|
324 | f7e6f3c8 | Iustin Pop | utils.RemoveFile(sname) |
325 | f7e6f3c8 | Iustin Pop | opts = ["-N", "-oControlPath=%s" % sname, "-oControlMaster=yes"] |
326 | f7e6f3c8 | Iustin Pop | print "Created socket at %s" % sname |
327 | f7e6f3c8 | Iustin Pop | child = StartLocalCommand(GetSSHCommand(node, None, opts=opts))
|
328 | f7e6f3c8 | Iustin Pop | _MULTIPLEXERS[node] = (sname, child) |
329 | f7e6f3c8 | Iustin Pop | |
330 | f7e6f3c8 | Iustin Pop | |
331 | f7e6f3c8 | Iustin Pop | def CloseMultiplexers(): |
332 | f7e6f3c8 | Iustin Pop | """Closes all current multiplexers and cleans up.
|
333 | f7e6f3c8 | Iustin Pop |
|
334 | f7e6f3c8 | Iustin Pop | """
|
335 | f7e6f3c8 | Iustin Pop | for node in _MULTIPLEXERS.keys(): |
336 | f7e6f3c8 | Iustin Pop | (sname, child) = _MULTIPLEXERS.pop(node) |
337 | f7e6f3c8 | Iustin Pop | utils.KillProcess(child.pid, timeout=10, waitpid=True) |
338 | f7e6f3c8 | Iustin Pop | utils.RemoveFile(sname) |
339 | f7e6f3c8 | Iustin Pop | |
340 | f7e6f3c8 | Iustin Pop | |
341 | 587f8ff6 | Bernardo Dal Seno | def GetCommandOutput(node, cmd, tty=None, fail=False): |
342 | 4b62db14 | Michael Hanselmann | """Returns the output of a command executed on the given node.
|
343 | 4b62db14 | Michael Hanselmann |
|
344 | 587f8ff6 | Bernardo Dal Seno | @type node: string
|
345 | 587f8ff6 | Bernardo Dal Seno | @param node: node the command should run on
|
346 | 587f8ff6 | Bernardo Dal Seno | @type cmd: string
|
347 | 587f8ff6 | Bernardo Dal Seno | @param cmd: command to be executed in the node (cannot be empty or None)
|
348 | 587f8ff6 | Bernardo Dal Seno | @type tty: bool or None
|
349 | 587f8ff6 | Bernardo Dal Seno | @param tty: if we should use tty; if None, it will be auto-detected
|
350 | 587f8ff6 | Bernardo Dal Seno | @type fail: bool
|
351 | 587f8ff6 | Bernardo Dal Seno | @param fail: whether the command is expected to fail
|
352 | 4b62db14 | Michael Hanselmann | """
|
353 | 587f8ff6 | Bernardo Dal Seno | assert cmd
|
354 | 50265802 | René Nussbaumer | p = StartLocalCommand(GetSSHCommand(node, cmd, tty=tty), |
355 | 50265802 | René Nussbaumer | stdout=subprocess.PIPE) |
356 | 587f8ff6 | Bernardo Dal Seno | rcode = p.wait() |
357 | 3b0db9e3 | Michael Hanselmann | _AssertRetCode(rcode, fail, cmd, node) |
358 | 4b62db14 | Michael Hanselmann | return p.stdout.read()
|
359 | cec9845c | Michael Hanselmann | |
360 | cec9845c | Michael Hanselmann | |
361 | 0e79564a | Bernardo Dal Seno | def GetObjectInfo(infocmd): |
362 | 0e79564a | Bernardo Dal Seno | """Get and parse information about a Ganeti object.
|
363 | 0e79564a | Bernardo Dal Seno |
|
364 | 0e79564a | Bernardo Dal Seno | @type infocmd: list of strings
|
365 | 0e79564a | Bernardo Dal Seno | @param infocmd: command to be executed, e.g. ["gnt-cluster", "info"]
|
366 | 0e79564a | Bernardo Dal Seno | @return: the information parsed, appropriately stored in dictionaries,
|
367 | 0e79564a | Bernardo Dal Seno | lists...
|
368 | 0e79564a | Bernardo Dal Seno |
|
369 | 0e79564a | Bernardo Dal Seno | """
|
370 | 0e79564a | Bernardo Dal Seno | master = qa_config.GetMasterNode() |
371 | 0e79564a | Bernardo Dal Seno | cmdline = utils.ShellQuoteArgs(infocmd) |
372 | 0e79564a | Bernardo Dal Seno | info_out = GetCommandOutput(master.primary, cmdline) |
373 | 0e79564a | Bernardo Dal Seno | return yaml.load(info_out)
|
374 | 0e79564a | Bernardo Dal Seno | |
375 | 0e79564a | Bernardo Dal Seno | |
376 | cec9845c | Michael Hanselmann | def UploadFile(node, src): |
377 | cec9845c | Michael Hanselmann | """Uploads a file to a node and returns the filename.
|
378 | cec9845c | Michael Hanselmann |
|
379 | cec9845c | Michael Hanselmann | Caller needs to remove the returned file on the node when it's not needed
|
380 | cec9845c | Michael Hanselmann | anymore.
|
381 | 49d50e52 | Michael Hanselmann |
|
382 | cec9845c | Michael Hanselmann | """
|
383 | cec9845c | Michael Hanselmann | # Make sure nobody else has access to it while preserving local permissions
|
384 | cec9845c | Michael Hanselmann | mode = os.stat(src).st_mode & 0700
|
385 | cec9845c | Michael Hanselmann | |
386 | f90a2d0c | Thomas Thrainer | cmd = ('tmp=$(mktemp --tmpdir gnt.XXXXXX) && '
|
387 | f90a2d0c | Thomas Thrainer | 'chmod %o "${tmp}" && '
|
388 | cec9845c | Michael Hanselmann | '[[ -f "${tmp}" ]] && '
|
389 | cec9845c | Michael Hanselmann | 'cat > "${tmp}" && '
|
390 | cec9845c | Michael Hanselmann | 'echo "${tmp}"') % mode
|
391 | cec9845c | Michael Hanselmann | |
392 | d0c8c01d | Iustin Pop | f = open(src, "r") |
393 | cec9845c | Michael Hanselmann | try:
|
394 | cec9845c | Michael Hanselmann | p = subprocess.Popen(GetSSHCommand(node, cmd), shell=False, stdin=f,
|
395 | cec9845c | Michael Hanselmann | stdout=subprocess.PIPE) |
396 | cec9845c | Michael Hanselmann | AssertEqual(p.wait(), 0)
|
397 | cec9845c | Michael Hanselmann | |
398 | cec9845c | Michael Hanselmann | # Return temporary filename
|
399 | cec9845c | Michael Hanselmann | return p.stdout.read().strip()
|
400 | cec9845c | Michael Hanselmann | finally:
|
401 | cec9845c | Michael Hanselmann | f.close() |
402 | 5d640672 | Michael Hanselmann | |
403 | 5d640672 | Michael Hanselmann | |
404 | b9955569 | René Nussbaumer | def UploadData(node, data, mode=0600, filename=None): |
405 | b9955569 | René Nussbaumer | """Uploads data to a node and returns the filename.
|
406 | b9955569 | René Nussbaumer |
|
407 | b9955569 | René Nussbaumer | Caller needs to remove the returned file on the node when it's not needed
|
408 | b9955569 | René Nussbaumer | anymore.
|
409 | b9955569 | René Nussbaumer |
|
410 | b9955569 | René Nussbaumer | """
|
411 | b9955569 | René Nussbaumer | if filename:
|
412 | b9955569 | René Nussbaumer | tmp = "tmp=%s" % utils.ShellQuote(filename)
|
413 | b9955569 | René Nussbaumer | else:
|
414 | f90a2d0c | Thomas Thrainer | tmp = ('tmp=$(mktemp --tmpdir gnt.XXXXXX) && '
|
415 | f90a2d0c | Thomas Thrainer | 'chmod %o "${tmp}"') % mode
|
416 | b9955569 | René Nussbaumer | cmd = ("%s && "
|
417 | b9955569 | René Nussbaumer | "[[ -f \"${tmp}\" ]] && "
|
418 | b9955569 | René Nussbaumer | "cat > \"${tmp}\" && "
|
419 | b9955569 | René Nussbaumer | "echo \"${tmp}\"") % tmp
|
420 | b9955569 | René Nussbaumer | |
421 | b9955569 | René Nussbaumer | p = subprocess.Popen(GetSSHCommand(node, cmd), shell=False,
|
422 | b9955569 | René Nussbaumer | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
423 | b9955569 | René Nussbaumer | p.stdin.write(data) |
424 | b9955569 | René Nussbaumer | p.stdin.close() |
425 | b9955569 | René Nussbaumer | AssertEqual(p.wait(), 0)
|
426 | b9955569 | René Nussbaumer | |
427 | b9955569 | René Nussbaumer | # Return temporary filename
|
428 | b9955569 | René Nussbaumer | return p.stdout.read().strip()
|
429 | b9955569 | René Nussbaumer | |
430 | b9955569 | René Nussbaumer | |
431 | 49d50e52 | Michael Hanselmann | def BackupFile(node, path): |
432 | 49d50e52 | Michael Hanselmann | """Creates a backup of a file on the node and returns the filename.
|
433 | 49d50e52 | Michael Hanselmann |
|
434 | 49d50e52 | Michael Hanselmann | Caller needs to remove the returned file on the node when it's not needed
|
435 | 49d50e52 | Michael Hanselmann | anymore.
|
436 | 49d50e52 | Michael Hanselmann |
|
437 | 49d50e52 | Michael Hanselmann | """
|
438 | 7160f14a | Michael Hanselmann | vpath = MakeNodePath(node, path) |
439 | 7160f14a | Michael Hanselmann | |
440 | f90a2d0c | Thomas Thrainer | cmd = ("tmp=$(mktemp .gnt.XXXXXX --tmpdir=$(dirname %s)) && "
|
441 | 49d50e52 | Michael Hanselmann | "[[ -f \"$tmp\" ]] && "
|
442 | 49d50e52 | Michael Hanselmann | "cp %s $tmp && "
|
443 | 7160f14a | Michael Hanselmann | "echo $tmp") % (utils.ShellQuote(vpath), utils.ShellQuote(vpath))
|
444 | 49d50e52 | Michael Hanselmann | |
445 | 49d50e52 | Michael Hanselmann | # Return temporary filename
|
446 | 7160f14a | Michael Hanselmann | result = GetCommandOutput(node, cmd).strip() |
447 | 7160f14a | Michael Hanselmann | |
448 | 7160f14a | Michael Hanselmann | print "Backup filename: %s" % result |
449 | 7160f14a | Michael Hanselmann | |
450 | 7160f14a | Michael Hanselmann | return result
|
451 | 49d50e52 | Michael Hanselmann | |
452 | 49d50e52 | Michael Hanselmann | |
453 | 5d640672 | Michael Hanselmann | def ResolveInstanceName(instance): |
454 | 5d640672 | Michael Hanselmann | """Gets the full name of an instance.
|
455 | 5d640672 | Michael Hanselmann |
|
456 | 46f9a948 | Michael Hanselmann | @type instance: string
|
457 | 46f9a948 | Michael Hanselmann | @param instance: Instance name
|
458 | 46f9a948 | Michael Hanselmann |
|
459 | 5d640672 | Michael Hanselmann | """
|
460 | 2cbcf95d | Bernardo Dal Seno | info = GetObjectInfo(["gnt-instance", "info", instance]) |
461 | 2cbcf95d | Bernardo Dal Seno | return info[0]["Instance name"] |
462 | 4b62db14 | Michael Hanselmann | |
463 | 4b62db14 | Michael Hanselmann | |
464 | 4b62db14 | Michael Hanselmann | def ResolveNodeName(node): |
465 | 4b62db14 | Michael Hanselmann | """Gets the full name of a node.
|
466 | 4b62db14 | Michael Hanselmann |
|
467 | 4b62db14 | Michael Hanselmann | """
|
468 | 5f6d1b42 | Bernardo Dal Seno | info = GetObjectInfo(["gnt-node", "info", node.primary]) |
469 | 5f6d1b42 | Bernardo Dal Seno | return info[0]["Node name"] |
470 | 4b62db14 | Michael Hanselmann | |
471 | 4b62db14 | Michael Hanselmann | |
472 | 4b62db14 | Michael Hanselmann | def GetNodeInstances(node, secondaries=False): |
473 | 4b62db14 | Michael Hanselmann | """Gets a list of instances on a node.
|
474 | 4b62db14 | Michael Hanselmann |
|
475 | 4b62db14 | Michael Hanselmann | """
|
476 | 5d640672 | Michael Hanselmann | master = qa_config.GetMasterNode() |
477 | 4b62db14 | Michael Hanselmann | node_name = ResolveNodeName(node) |
478 | 5d640672 | Michael Hanselmann | |
479 | 4b62db14 | Michael Hanselmann | # Get list of all instances
|
480 | d0c8c01d | Iustin Pop | cmd = ["gnt-instance", "list", "--separator=:", "--no-headers", |
481 | d0c8c01d | Iustin Pop | "--output=name,pnode,snodes"]
|
482 | aecba21e | Michael Hanselmann | output = GetCommandOutput(master.primary, utils.ShellQuoteArgs(cmd)) |
483 | 4b62db14 | Michael Hanselmann | |
484 | 4b62db14 | Michael Hanselmann | instances = [] |
485 | 4b62db14 | Michael Hanselmann | for line in output.splitlines(): |
486 | d0c8c01d | Iustin Pop | (name, pnode, snodes) = line.split(":", 2) |
487 | 4b62db14 | Michael Hanselmann | if ((not secondaries and pnode == node_name) or |
488 | d0c8c01d | Iustin Pop | (secondaries and node_name in snodes.split(","))): |
489 | 4b62db14 | Michael Hanselmann | instances.append(name) |
490 | 5d640672 | Michael Hanselmann | |
491 | 4b62db14 | Michael Hanselmann | return instances
|
492 | 23269c5b | Michael Hanselmann | |
493 | 23269c5b | Michael Hanselmann | |
494 | 288d6440 | Michael Hanselmann | def _SelectQueryFields(rnd, fields): |
495 | 288d6440 | Michael Hanselmann | """Generates a list of fields for query tests.
|
496 | 288d6440 | Michael Hanselmann |
|
497 | 288d6440 | Michael Hanselmann | """
|
498 | 288d6440 | Michael Hanselmann | # Create copy for shuffling
|
499 | 288d6440 | Michael Hanselmann | fields = list(fields)
|
500 | 288d6440 | Michael Hanselmann | rnd.shuffle(fields) |
501 | 288d6440 | Michael Hanselmann | |
502 | 288d6440 | Michael Hanselmann | # Check all fields
|
503 | 288d6440 | Michael Hanselmann | yield fields
|
504 | 288d6440 | Michael Hanselmann | yield sorted(fields) |
505 | 288d6440 | Michael Hanselmann | |
506 | 288d6440 | Michael Hanselmann | # Duplicate fields
|
507 | 288d6440 | Michael Hanselmann | yield fields + fields
|
508 | 288d6440 | Michael Hanselmann | |
509 | 288d6440 | Michael Hanselmann | # Check small groups of fields
|
510 | 288d6440 | Michael Hanselmann | while fields:
|
511 | 288d6440 | Michael Hanselmann | yield [fields.pop() for _ in range(rnd.randint(2, 10)) if fields] |
512 | 288d6440 | Michael Hanselmann | |
513 | 288d6440 | Michael Hanselmann | |
514 | 288d6440 | Michael Hanselmann | def _List(listcmd, fields, names): |
515 | 288d6440 | Michael Hanselmann | """Runs a list command.
|
516 | 288d6440 | Michael Hanselmann |
|
517 | 288d6440 | Michael Hanselmann | """
|
518 | 288d6440 | Michael Hanselmann | master = qa_config.GetMasterNode() |
519 | 288d6440 | Michael Hanselmann | |
520 | 58ea8d17 | Michael Hanselmann | cmd = [listcmd, "list", "--separator=|", "--no-headers", |
521 | 288d6440 | Michael Hanselmann | "--output", ",".join(fields)] |
522 | 288d6440 | Michael Hanselmann | |
523 | 288d6440 | Michael Hanselmann | if names:
|
524 | 288d6440 | Michael Hanselmann | cmd.extend(names) |
525 | 288d6440 | Michael Hanselmann | |
526 | aecba21e | Michael Hanselmann | return GetCommandOutput(master.primary,
|
527 | 288d6440 | Michael Hanselmann | utils.ShellQuoteArgs(cmd)).splitlines() |
528 | 288d6440 | Michael Hanselmann | |
529 | 288d6440 | Michael Hanselmann | |
530 | d352b796 | Helga Velroyen | def GenericQueryTest(cmd, fields, namefield="name", test_unknown=True): |
531 | 288d6440 | Michael Hanselmann | """Runs a number of tests on query commands.
|
532 | 288d6440 | Michael Hanselmann |
|
533 | 288d6440 | Michael Hanselmann | @param cmd: Command name
|
534 | 288d6440 | Michael Hanselmann | @param fields: List of field names
|
535 | 288d6440 | Michael Hanselmann |
|
536 | 288d6440 | Michael Hanselmann | """
|
537 | 288d6440 | Michael Hanselmann | rnd = random.Random(hash(cmd))
|
538 | 288d6440 | Michael Hanselmann | |
539 | 3582eef6 | Iustin Pop | fields = list(fields)
|
540 | 288d6440 | Michael Hanselmann | rnd.shuffle(fields) |
541 | 288d6440 | Michael Hanselmann | |
542 | 288d6440 | Michael Hanselmann | # Test a number of field combinations
|
543 | 288d6440 | Michael Hanselmann | for testfields in _SelectQueryFields(rnd, fields): |
544 | 93146c8c | Iustin Pop | AssertRedirectedCommand([cmd, "list", "--output", ",".join(testfields)]) |
545 | 288d6440 | Michael Hanselmann | |
546 | 0fdf247d | Michael Hanselmann | if namefield is not None: |
547 | 0fdf247d | Michael Hanselmann | namelist_fn = compat.partial(_List, cmd, [namefield]) |
548 | 288d6440 | Michael Hanselmann | |
549 | 0fdf247d | Michael Hanselmann | # When no names were requested, the list must be sorted
|
550 | 0fdf247d | Michael Hanselmann | names = namelist_fn(None)
|
551 | 0fdf247d | Michael Hanselmann | AssertEqual(names, utils.NiceSort(names)) |
552 | 288d6440 | Michael Hanselmann | |
553 | 0fdf247d | Michael Hanselmann | # When requesting specific names, the order must be kept
|
554 | 0fdf247d | Michael Hanselmann | revnames = list(reversed(names)) |
555 | 0fdf247d | Michael Hanselmann | AssertEqual(namelist_fn(revnames), revnames) |
556 | 288d6440 | Michael Hanselmann | |
557 | 0fdf247d | Michael Hanselmann | randnames = list(names)
|
558 | 0fdf247d | Michael Hanselmann | rnd.shuffle(randnames) |
559 | 0fdf247d | Michael Hanselmann | AssertEqual(namelist_fn(randnames), randnames) |
560 | 288d6440 | Michael Hanselmann | |
561 | 6d1e4845 | Michael Hanselmann | if test_unknown:
|
562 | 6d1e4845 | Michael Hanselmann | # Listing unknown items must fail
|
563 | 6d1e4845 | Michael Hanselmann | AssertCommand([cmd, "list", "this.name.certainly.does.not.exist"], |
564 | 6d1e4845 | Michael Hanselmann | fail=True)
|
565 | 2214cf14 | Michael Hanselmann | |
566 | 2214cf14 | Michael Hanselmann | # Check exit code for listing unknown field
|
567 | 93146c8c | Iustin Pop | AssertEqual(AssertRedirectedCommand([cmd, "list",
|
568 | 93146c8c | Iustin Pop | "--output=field/does/not/exist"],
|
569 | 93146c8c | Iustin Pop | fail=True),
|
570 | 2214cf14 | Michael Hanselmann | constants.EXIT_UNKNOWN_FIELD) |
571 | 2214cf14 | Michael Hanselmann | |
572 | 2214cf14 | Michael Hanselmann | |
573 | 2214cf14 | Michael Hanselmann | def GenericQueryFieldsTest(cmd, fields): |
574 | 2214cf14 | Michael Hanselmann | master = qa_config.GetMasterNode() |
575 | 2214cf14 | Michael Hanselmann | |
576 | 2214cf14 | Michael Hanselmann | # Listing fields
|
577 | 93146c8c | Iustin Pop | AssertRedirectedCommand([cmd, "list-fields"])
|
578 | 93146c8c | Iustin Pop | AssertRedirectedCommand([cmd, "list-fields"] + fields)
|
579 | 2214cf14 | Michael Hanselmann | |
580 | 2214cf14 | Michael Hanselmann | # Check listed fields (all, must be sorted)
|
581 | 2214cf14 | Michael Hanselmann | realcmd = [cmd, "list-fields", "--separator=|", "--no-headers"] |
582 | aecba21e | Michael Hanselmann | output = GetCommandOutput(master.primary, |
583 | 2214cf14 | Michael Hanselmann | utils.ShellQuoteArgs(realcmd)).splitlines() |
584 | 2214cf14 | Michael Hanselmann | AssertEqual([line.split("|", 1)[0] for line in output], |
585 | c694367b | Michael Hanselmann | utils.NiceSort(fields)) |
586 | 2214cf14 | Michael Hanselmann | |
587 | 2214cf14 | Michael Hanselmann | # Check exit code for listing unknown field
|
588 | 2214cf14 | Michael Hanselmann | AssertEqual(AssertCommand([cmd, "list-fields", "field/does/not/exist"], |
589 | 2214cf14 | Michael Hanselmann | fail=True),
|
590 | 2214cf14 | Michael Hanselmann | constants.EXIT_UNKNOWN_FIELD) |
591 | 2214cf14 | Michael Hanselmann | |
592 | 288d6440 | Michael Hanselmann | |
593 | dfe11bad | Michael Hanselmann | def _FormatWithColor(text, seq): |
594 | dfe11bad | Michael Hanselmann | if not seq: |
595 | dfe11bad | Michael Hanselmann | return text
|
596 | dfe11bad | Michael Hanselmann | return "%s%s%s" % (seq, text, _RESET_SEQ) |
597 | 23269c5b | Michael Hanselmann | |
598 | 23269c5b | Michael Hanselmann | |
599 | dfe11bad | Michael Hanselmann | FormatWarning = lambda text: _FormatWithColor(text, _WARNING_SEQ)
|
600 | dfe11bad | Michael Hanselmann | FormatError = lambda text: _FormatWithColor(text, _ERROR_SEQ)
|
601 | dfe11bad | Michael Hanselmann | FormatInfo = lambda text: _FormatWithColor(text, _INFO_SEQ)
|
602 | 31fe5102 | René Nussbaumer | |
603 | 31fe5102 | René Nussbaumer | |
604 | 31fe5102 | René Nussbaumer | def AddToEtcHosts(hostnames): |
605 | 31fe5102 | René Nussbaumer | """Adds hostnames to /etc/hosts.
|
606 | 31fe5102 | René Nussbaumer |
|
607 | 31fe5102 | René Nussbaumer | @param hostnames: List of hostnames first used A records, all other CNAMEs
|
608 | 31fe5102 | René Nussbaumer |
|
609 | 31fe5102 | René Nussbaumer | """
|
610 | 31fe5102 | René Nussbaumer | master = qa_config.GetMasterNode() |
611 | aecba21e | Michael Hanselmann | tmp_hosts = UploadData(master.primary, "", mode=0644) |
612 | 31fe5102 | René Nussbaumer | |
613 | 31fe5102 | René Nussbaumer | data = [] |
614 | 31fe5102 | René Nussbaumer | for localhost in ("::1", "127.0.0.1"): |
615 | 31fe5102 | René Nussbaumer | data.append("%s %s" % (localhost, " ".join(hostnames))) |
616 | 31fe5102 | René Nussbaumer | |
617 | 31fe5102 | René Nussbaumer | try:
|
618 | 48967eb0 | Michael Hanselmann | AssertCommand("{ cat %s && echo -e '%s'; } > %s && mv %s %s" %
|
619 | 48967eb0 | Michael Hanselmann | (utils.ShellQuote(pathutils.ETC_HOSTS), |
620 | 48967eb0 | Michael Hanselmann | "\\n".join(data),
|
621 | 48967eb0 | Michael Hanselmann | utils.ShellQuote(tmp_hosts), |
622 | 48967eb0 | Michael Hanselmann | utils.ShellQuote(tmp_hosts), |
623 | 48967eb0 | Michael Hanselmann | utils.ShellQuote(pathutils.ETC_HOSTS))) |
624 | 48967eb0 | Michael Hanselmann | except Exception: |
625 | 48967eb0 | Michael Hanselmann | AssertCommand(["rm", "-f", tmp_hosts]) |
626 | 48967eb0 | Michael Hanselmann | raise
|
627 | 31fe5102 | René Nussbaumer | |
628 | 31fe5102 | René Nussbaumer | |
629 | 31fe5102 | René Nussbaumer | def RemoveFromEtcHosts(hostnames): |
630 | 31fe5102 | René Nussbaumer | """Remove hostnames from /etc/hosts.
|
631 | 31fe5102 | René Nussbaumer |
|
632 | 31fe5102 | René Nussbaumer | @param hostnames: List of hostnames first used A records, all other CNAMEs
|
633 | 31fe5102 | René Nussbaumer |
|
634 | 31fe5102 | René Nussbaumer | """
|
635 | 31fe5102 | René Nussbaumer | master = qa_config.GetMasterNode() |
636 | aecba21e | Michael Hanselmann | tmp_hosts = UploadData(master.primary, "", mode=0644) |
637 | 31fe5102 | René Nussbaumer | quoted_tmp_hosts = utils.ShellQuote(tmp_hosts) |
638 | 31fe5102 | René Nussbaumer | |
639 | 31fe5102 | René Nussbaumer | sed_data = " ".join(hostnames)
|
640 | 31fe5102 | René Nussbaumer | try:
|
641 | fa0a1365 | Michele Tartara | AssertCommand((r"sed -e '/^\(::1\|127\.0\.0\.1\)\s\+%s/d' %s > %s"
|
642 | fa0a1365 | Michele Tartara | r" && mv %s %s") %
|
643 | 48967eb0 | Michael Hanselmann | (sed_data, utils.ShellQuote(pathutils.ETC_HOSTS), |
644 | 48967eb0 | Michael Hanselmann | quoted_tmp_hosts, quoted_tmp_hosts, |
645 | 48967eb0 | Michael Hanselmann | utils.ShellQuote(pathutils.ETC_HOSTS))) |
646 | 48967eb0 | Michael Hanselmann | except Exception: |
647 | 48967eb0 | Michael Hanselmann | AssertCommand(["rm", "-f", tmp_hosts]) |
648 | 48967eb0 | Michael Hanselmann | raise
|
649 | c9e05005 | Michael Hanselmann | |
650 | c9e05005 | Michael Hanselmann | |
651 | c9e05005 | Michael Hanselmann | def RunInstanceCheck(instance, running): |
652 | c9e05005 | Michael Hanselmann | """Check if instance is running or not.
|
653 | c9e05005 | Michael Hanselmann |
|
654 | c9e05005 | Michael Hanselmann | """
|
655 | 6998aefe | Michael Hanselmann | instance_name = _GetName(instance, operator.attrgetter("name"))
|
656 | 2ac35588 | Michael Hanselmann | |
657 | c9e05005 | Michael Hanselmann | script = qa_config.GetInstanceCheckScript() |
658 | c9e05005 | Michael Hanselmann | if not script: |
659 | c9e05005 | Michael Hanselmann | return
|
660 | c9e05005 | Michael Hanselmann | |
661 | c9e05005 | Michael Hanselmann | master_node = qa_config.GetMasterNode() |
662 | c9e05005 | Michael Hanselmann | |
663 | c9e05005 | Michael Hanselmann | # Build command to connect to master node
|
664 | aecba21e | Michael Hanselmann | master_ssh = GetSSHCommand(master_node.primary, "--")
|
665 | c9e05005 | Michael Hanselmann | |
666 | c9e05005 | Michael Hanselmann | if running:
|
667 | c9e05005 | Michael Hanselmann | running_shellval = "1"
|
668 | c9e05005 | Michael Hanselmann | running_text = ""
|
669 | c9e05005 | Michael Hanselmann | else:
|
670 | c9e05005 | Michael Hanselmann | running_shellval = ""
|
671 | c9e05005 | Michael Hanselmann | running_text = "not "
|
672 | c9e05005 | Michael Hanselmann | |
673 | c9e05005 | Michael Hanselmann | print FormatInfo("Checking if instance '%s' is %srunning" % |
674 | c9e05005 | Michael Hanselmann | (instance_name, running_text)) |
675 | c9e05005 | Michael Hanselmann | |
676 | c9e05005 | Michael Hanselmann | args = [script, instance_name] |
677 | c9e05005 | Michael Hanselmann | env = { |
678 | c9e05005 | Michael Hanselmann | "PATH": constants.HOOKS_PATH,
|
679 | c9e05005 | Michael Hanselmann | "RUN_UUID": _RUN_UUID,
|
680 | c9e05005 | Michael Hanselmann | "MASTER_SSH": utils.ShellQuoteArgs(master_ssh),
|
681 | c9e05005 | Michael Hanselmann | "INSTANCE_NAME": instance_name,
|
682 | c9e05005 | Michael Hanselmann | "INSTANCE_RUNNING": running_shellval,
|
683 | c9e05005 | Michael Hanselmann | } |
684 | c9e05005 | Michael Hanselmann | |
685 | c9e05005 | Michael Hanselmann | result = os.spawnve(os.P_WAIT, script, args, env) |
686 | c9e05005 | Michael Hanselmann | if result != 0: |
687 | c9e05005 | Michael Hanselmann | raise qa_error.Error("Instance check failed with result %s" % result) |
688 | c9e05005 | Michael Hanselmann | |
689 | c9e05005 | Michael Hanselmann | |
690 | c9e05005 | Michael Hanselmann | def _InstanceCheckInner(expected, instarg, args, result): |
691 | c9e05005 | Michael Hanselmann | """Helper function used by L{InstanceCheck}.
|
692 | c9e05005 | Michael Hanselmann |
|
693 | c9e05005 | Michael Hanselmann | """
|
694 | c9e05005 | Michael Hanselmann | if instarg == FIRST_ARG:
|
695 | c9e05005 | Michael Hanselmann | instance = args[0]
|
696 | c9e05005 | Michael Hanselmann | elif instarg == RETURN_VALUE:
|
697 | c9e05005 | Michael Hanselmann | instance = result |
698 | c9e05005 | Michael Hanselmann | else:
|
699 | c9e05005 | Michael Hanselmann | raise Exception("Invalid value '%s' for instance argument" % instarg) |
700 | c9e05005 | Michael Hanselmann | |
701 | c9e05005 | Michael Hanselmann | if expected in (INST_DOWN, INST_UP): |
702 | c9e05005 | Michael Hanselmann | RunInstanceCheck(instance, (expected == INST_UP)) |
703 | c9e05005 | Michael Hanselmann | elif expected is not None: |
704 | c9e05005 | Michael Hanselmann | raise Exception("Invalid value '%s'" % expected) |
705 | c9e05005 | Michael Hanselmann | |
706 | c9e05005 | Michael Hanselmann | |
707 | c9e05005 | Michael Hanselmann | def InstanceCheck(before, after, instarg): |
708 | c9e05005 | Michael Hanselmann | """Decorator to check instance status before and after test.
|
709 | c9e05005 | Michael Hanselmann |
|
710 | c9e05005 | Michael Hanselmann | @param before: L{INST_DOWN} if instance must be stopped before test,
|
711 | c9e05005 | Michael Hanselmann | L{INST_UP} if instance must be running before test, L{None} to not check.
|
712 | c9e05005 | Michael Hanselmann | @param after: L{INST_DOWN} if instance must be stopped after test,
|
713 | c9e05005 | Michael Hanselmann | L{INST_UP} if instance must be running after test, L{None} to not check.
|
714 | c9e05005 | Michael Hanselmann | @param instarg: L{FIRST_ARG} to use first argument to test as instance (a
|
715 | c9e05005 | Michael Hanselmann | dictionary), L{RETURN_VALUE} to use return value (disallows pre-checks)
|
716 | c9e05005 | Michael Hanselmann |
|
717 | c9e05005 | Michael Hanselmann | """
|
718 | c9e05005 | Michael Hanselmann | def decorator(fn): |
719 | c9e05005 | Michael Hanselmann | @functools.wraps(fn)
|
720 | c9e05005 | Michael Hanselmann | def wrapper(*args, **kwargs): |
721 | c9e05005 | Michael Hanselmann | _InstanceCheckInner(before, instarg, args, NotImplemented)
|
722 | c9e05005 | Michael Hanselmann | |
723 | c9e05005 | Michael Hanselmann | result = fn(*args, **kwargs) |
724 | c9e05005 | Michael Hanselmann | |
725 | c9e05005 | Michael Hanselmann | _InstanceCheckInner(after, instarg, args, result) |
726 | c9e05005 | Michael Hanselmann | |
727 | c9e05005 | Michael Hanselmann | return result
|
728 | c9e05005 | Michael Hanselmann | return wrapper
|
729 | c9e05005 | Michael Hanselmann | return decorator
|
730 | b4d2d2cb | Michael Hanselmann | |
731 | b4d2d2cb | Michael Hanselmann | |
732 | b4d2d2cb | Michael Hanselmann | def GetNonexistentGroups(count): |
733 | b4d2d2cb | Michael Hanselmann | """Gets group names which shouldn't exist on the cluster.
|
734 | b4d2d2cb | Michael Hanselmann |
|
735 | b4d2d2cb | Michael Hanselmann | @param count: Number of groups to get
|
736 | ea7693c1 | Helga Velroyen | @rtype: integer
|
737 | b4d2d2cb | Michael Hanselmann |
|
738 | b4d2d2cb | Michael Hanselmann | """
|
739 | ea7693c1 | Helga Velroyen | return GetNonexistentEntityNames(count, "groups", "group") |
740 | b4d2d2cb | Michael Hanselmann | |
741 | ea7693c1 | Helga Velroyen | |
742 | ea7693c1 | Helga Velroyen | def GetNonexistentEntityNames(count, name_config, name_prefix): |
743 | ea7693c1 | Helga Velroyen | """Gets entity names which shouldn't exist on the cluster.
|
744 | ea7693c1 | Helga Velroyen |
|
745 | ea7693c1 | Helga Velroyen | The actualy names can refer to arbitrary entities (for example
|
746 | ea7693c1 | Helga Velroyen | groups, networks).
|
747 | ea7693c1 | Helga Velroyen |
|
748 | ea7693c1 | Helga Velroyen | @param count: Number of names to get
|
749 | ea7693c1 | Helga Velroyen | @rtype: integer
|
750 | ea7693c1 | Helga Velroyen | @param name_config: name of the leaf in the config containing
|
751 | ea7693c1 | Helga Velroyen | this entity's configuration, including a 'inexistent-'
|
752 | ea7693c1 | Helga Velroyen | element
|
753 | ea7693c1 | Helga Velroyen | @rtype: string
|
754 | ea7693c1 | Helga Velroyen | @param name_prefix: prefix of the entity's names, used to compose
|
755 | ea7693c1 | Helga Velroyen | the default values; for example for groups, the prefix is
|
756 | ea7693c1 | Helga Velroyen | 'group' and the generated names are then group1, group2, ...
|
757 | ea7693c1 | Helga Velroyen | @rtype: string
|
758 | ea7693c1 | Helga Velroyen |
|
759 | ea7693c1 | Helga Velroyen | """
|
760 | ea7693c1 | Helga Velroyen | entities = qa_config.get(name_config, {}) |
761 | ea7693c1 | Helga Velroyen | |
762 | ea7693c1 | Helga Velroyen | default = [name_prefix + str(i) for i in range(count)] |
763 | b4d2d2cb | Michael Hanselmann | assert count <= len(default) |
764 | b4d2d2cb | Michael Hanselmann | |
765 | ea7693c1 | Helga Velroyen | name_config_inexistent = "inexistent-" + name_config
|
766 | ea7693c1 | Helga Velroyen | candidates = entities.get(name_config_inexistent, default)[:count] |
767 | b4d2d2cb | Michael Hanselmann | |
768 | b4d2d2cb | Michael Hanselmann | if len(candidates) < count: |
769 | ea7693c1 | Helga Velroyen | raise Exception("At least %s non-existent %s are needed" % |
770 | ea7693c1 | Helga Velroyen | (count, name_config)) |
771 | b4d2d2cb | Michael Hanselmann | |
772 | b4d2d2cb | Michael Hanselmann | return candidates
|
773 | 7160f14a | Michael Hanselmann | |
774 | 7160f14a | Michael Hanselmann | |
775 | 7160f14a | Michael Hanselmann | def MakeNodePath(node, path): |
776 | 7160f14a | Michael Hanselmann | """Builds an absolute path for a virtual node.
|
777 | 7160f14a | Michael Hanselmann |
|
778 | 7160f14a | Michael Hanselmann | @type node: string or L{qa_config._QaNode}
|
779 | 7160f14a | Michael Hanselmann | @param node: Node
|
780 | 7160f14a | Michael Hanselmann | @type path: string
|
781 | 7160f14a | Michael Hanselmann | @param path: Path without node-specific prefix
|
782 | 7160f14a | Michael Hanselmann |
|
783 | 7160f14a | Michael Hanselmann | """
|
784 | 7160f14a | Michael Hanselmann | (_, basedir) = qa_config.GetVclusterSettings() |
785 | 7160f14a | Michael Hanselmann | |
786 | 7160f14a | Michael Hanselmann | if isinstance(node, basestring): |
787 | 7160f14a | Michael Hanselmann | name = node |
788 | 7160f14a | Michael Hanselmann | else:
|
789 | 7160f14a | Michael Hanselmann | name = node.primary |
790 | 7160f14a | Michael Hanselmann | |
791 | 7160f14a | Michael Hanselmann | if basedir:
|
792 | 7160f14a | Michael Hanselmann | assert path.startswith("/") |
793 | 7160f14a | Michael Hanselmann | return "%s%s" % (vcluster.MakeNodeRoot(basedir, name), path) |
794 | 7160f14a | Michael Hanselmann | else:
|
795 | 7160f14a | Michael Hanselmann | return path
|
796 | 63e08b25 | Bernardo Dal Seno | |
797 | 63e08b25 | Bernardo Dal Seno | |
798 | ec996117 | Bernardo Dal Seno | def _GetParameterOptions(specs): |
799 | 63e08b25 | Bernardo Dal Seno | """Helper to build policy options."""
|
800 | ec996117 | Bernardo Dal Seno | values = ["%s=%s" % (par, val)
|
801 | ec996117 | Bernardo Dal Seno | for (par, val) in specs.items()] |
802 | 63e08b25 | Bernardo Dal Seno | return ",".join(values) |
803 | 63e08b25 | Bernardo Dal Seno | |
804 | 63e08b25 | Bernardo Dal Seno | |
805 | ec996117 | Bernardo Dal Seno | def TestSetISpecs(new_specs=None, diff_specs=None, get_policy_fn=None, |
806 | ec996117 | Bernardo Dal Seno | build_cmd_fn=None, fail=False, old_values=None): |
807 | 63e08b25 | Bernardo Dal Seno | """Change instance specs for an object.
|
808 | 63e08b25 | Bernardo Dal Seno |
|
809 | ec996117 | Bernardo Dal Seno | At most one of new_specs or diff_specs can be specified.
|
810 | ec996117 | Bernardo Dal Seno |
|
811 | ec996117 | Bernardo Dal Seno | @type new_specs: dict
|
812 | ec996117 | Bernardo Dal Seno | @param new_specs: new complete specs, in the same format returned by
|
813 | ec996117 | Bernardo Dal Seno | L{ParseIPolicy}.
|
814 | ec996117 | Bernardo Dal Seno | @type diff_specs: dict
|
815 | 7c8ae421 | Bernardo Dal Seno | @param diff_specs: partial specs, it can be an incomplete specifications, but
|
816 | 7c8ae421 | Bernardo Dal Seno | if min/max specs are specified, their number must match the number of the
|
817 | 7c8ae421 | Bernardo Dal Seno | existing specs
|
818 | 63e08b25 | Bernardo Dal Seno | @type get_policy_fn: function
|
819 | 63e08b25 | Bernardo Dal Seno | @param get_policy_fn: function that returns the current policy as in
|
820 | ec996117 | Bernardo Dal Seno | L{ParseIPolicy}
|
821 | 63e08b25 | Bernardo Dal Seno | @type build_cmd_fn: function
|
822 | 63e08b25 | Bernardo Dal Seno | @param build_cmd_fn: function that return the full command line from the
|
823 | 63e08b25 | Bernardo Dal Seno | options alone
|
824 | 63e08b25 | Bernardo Dal Seno | @type fail: bool
|
825 | 63e08b25 | Bernardo Dal Seno | @param fail: if the change is expected to fail
|
826 | 63e08b25 | Bernardo Dal Seno | @type old_values: tuple
|
827 | 63e08b25 | Bernardo Dal Seno | @param old_values: (old_policy, old_specs), as returned by
|
828 | ec996117 | Bernardo Dal Seno | L{ParseIPolicy}
|
829 | ec996117 | Bernardo Dal Seno | @return: same as L{ParseIPolicy}
|
830 | 63e08b25 | Bernardo Dal Seno |
|
831 | 63e08b25 | Bernardo Dal Seno | """
|
832 | 63e08b25 | Bernardo Dal Seno | assert get_policy_fn is not None |
833 | 63e08b25 | Bernardo Dal Seno | assert build_cmd_fn is not None |
834 | ec996117 | Bernardo Dal Seno | assert new_specs is None or diff_specs is None |
835 | 63e08b25 | Bernardo Dal Seno | |
836 | 63e08b25 | Bernardo Dal Seno | if old_values:
|
837 | 63e08b25 | Bernardo Dal Seno | (old_policy, old_specs) = old_values |
838 | 63e08b25 | Bernardo Dal Seno | else:
|
839 | 63e08b25 | Bernardo Dal Seno | (old_policy, old_specs) = get_policy_fn() |
840 | ec996117 | Bernardo Dal Seno | |
841 | ec996117 | Bernardo Dal Seno | if diff_specs:
|
842 | ec996117 | Bernardo Dal Seno | new_specs = copy.deepcopy(old_specs) |
843 | 7c8ae421 | Bernardo Dal Seno | if constants.ISPECS_MINMAX in diff_specs: |
844 | 7c8ae421 | Bernardo Dal Seno | AssertEqual(len(new_specs[constants.ISPECS_MINMAX]),
|
845 | 7c8ae421 | Bernardo Dal Seno | len(diff_specs[constants.ISPECS_MINMAX]))
|
846 | 7c8ae421 | Bernardo Dal Seno | for (new_minmax, diff_minmax) in zip(new_specs[constants.ISPECS_MINMAX], |
847 | 7c8ae421 | Bernardo Dal Seno | diff_specs[constants.ISPECS_MINMAX]): |
848 | 7c8ae421 | Bernardo Dal Seno | for (key, parvals) in diff_minmax.items(): |
849 | 7c8ae421 | Bernardo Dal Seno | for (par, val) in parvals.items(): |
850 | 7c8ae421 | Bernardo Dal Seno | new_minmax[key][par] = val |
851 | 7c8ae421 | Bernardo Dal Seno | for (par, val) in diff_specs.get(constants.ISPECS_STD, {}).items(): |
852 | 7c8ae421 | Bernardo Dal Seno | new_specs[constants.ISPECS_STD][par] = val |
853 | ec996117 | Bernardo Dal Seno | |
854 | 63e08b25 | Bernardo Dal Seno | if new_specs:
|
855 | 63e08b25 | Bernardo Dal Seno | cmd = [] |
856 | 7c8ae421 | Bernardo Dal Seno | if (diff_specs is None or constants.ISPECS_MINMAX in diff_specs): |
857 | 63e08b25 | Bernardo Dal Seno | minmax_opt_items = [] |
858 | 7c8ae421 | Bernardo Dal Seno | for minmax in new_specs[constants.ISPECS_MINMAX]: |
859 | 7c8ae421 | Bernardo Dal Seno | minmax_opts = [] |
860 | 7c8ae421 | Bernardo Dal Seno | for key in ["min", "max"]: |
861 | 7c8ae421 | Bernardo Dal Seno | keyopt = _GetParameterOptions(minmax[key]) |
862 | 7c8ae421 | Bernardo Dal Seno | minmax_opts.append("%s:%s" % (key, keyopt))
|
863 | 7c8ae421 | Bernardo Dal Seno | minmax_opt_items.append("/".join(minmax_opts))
|
864 | 63e08b25 | Bernardo Dal Seno | cmd.extend([ |
865 | 63e08b25 | Bernardo Dal Seno | "--ipolicy-bounds-specs",
|
866 | 7c8ae421 | Bernardo Dal Seno | "//".join(minmax_opt_items)
|
867 | 63e08b25 | Bernardo Dal Seno | ]) |
868 | 7c8ae421 | Bernardo Dal Seno | if diff_specs is None: |
869 | ec996117 | Bernardo Dal Seno | std_source = new_specs |
870 | 7c8ae421 | Bernardo Dal Seno | else:
|
871 | 7c8ae421 | Bernardo Dal Seno | std_source = diff_specs |
872 | ec996117 | Bernardo Dal Seno | std_opt = _GetParameterOptions(std_source.get("std", {}))
|
873 | 63e08b25 | Bernardo Dal Seno | if std_opt:
|
874 | 63e08b25 | Bernardo Dal Seno | cmd.extend(["--ipolicy-std-specs", std_opt])
|
875 | 63e08b25 | Bernardo Dal Seno | AssertCommand(build_cmd_fn(cmd), fail=fail) |
876 | 63e08b25 | Bernardo Dal Seno | |
877 | ec996117 | Bernardo Dal Seno | # Check the new state
|
878 | ec996117 | Bernardo Dal Seno | (eff_policy, eff_specs) = get_policy_fn() |
879 | ec996117 | Bernardo Dal Seno | AssertEqual(eff_policy, old_policy) |
880 | ec996117 | Bernardo Dal Seno | if fail:
|
881 | ec996117 | Bernardo Dal Seno | AssertEqual(eff_specs, old_specs) |
882 | ec996117 | Bernardo Dal Seno | else:
|
883 | ec996117 | Bernardo Dal Seno | AssertEqual(eff_specs, new_specs) |
884 | ec996117 | Bernardo Dal Seno | |
885 | 63e08b25 | Bernardo Dal Seno | else:
|
886 | ec996117 | Bernardo Dal Seno | (eff_policy, eff_specs) = (old_policy, old_specs) |
887 | ec996117 | Bernardo Dal Seno | |
888 | 63e08b25 | Bernardo Dal Seno | return (eff_policy, eff_specs)
|
889 | 63e08b25 | Bernardo Dal Seno | |
890 | 63e08b25 | Bernardo Dal Seno | |
891 | 63e08b25 | Bernardo Dal Seno | def ParseIPolicy(policy): |
892 | 63e08b25 | Bernardo Dal Seno | """Parse and split instance an instance policy.
|
893 | 63e08b25 | Bernardo Dal Seno |
|
894 | 63e08b25 | Bernardo Dal Seno | @type policy: dict
|
895 | 63e08b25 | Bernardo Dal Seno | @param policy: policy, as returned by L{GetObjectInfo}
|
896 | 63e08b25 | Bernardo Dal Seno | @rtype: tuple
|
897 | 63e08b25 | Bernardo Dal Seno | @return: (policy, specs), where:
|
898 | 63e08b25 | Bernardo Dal Seno | - policy is a dictionary of the policy values, instance specs excluded
|
899 | 7c8ae421 | Bernardo Dal Seno | - specs is a dictionary containing only the specs, using the internal
|
900 | 7c8ae421 | Bernardo Dal Seno | format (see L{constants.IPOLICY_DEFAULTS} for an example)
|
901 | 63e08b25 | Bernardo Dal Seno |
|
902 | 63e08b25 | Bernardo Dal Seno | """
|
903 | 63e08b25 | Bernardo Dal Seno | ret_specs = {} |
904 | 63e08b25 | Bernardo Dal Seno | ret_policy = {} |
905 | 63e08b25 | Bernardo Dal Seno | for (key, val) in policy.items(): |
906 | 7c8ae421 | Bernardo Dal Seno | if key == "bounds specs": |
907 | 7c8ae421 | Bernardo Dal Seno | ret_specs[constants.ISPECS_MINMAX] = [] |
908 | 7c8ae421 | Bernardo Dal Seno | for minmax in val: |
909 | 7c8ae421 | Bernardo Dal Seno | ret_minmax = {} |
910 | 7c8ae421 | Bernardo Dal Seno | for key in minmax: |
911 | 7c8ae421 | Bernardo Dal Seno | keyparts = key.split("/", 1) |
912 | 7c8ae421 | Bernardo Dal Seno | assert len(keyparts) > 1 |
913 | 7c8ae421 | Bernardo Dal Seno | ret_minmax[keyparts[0]] = minmax[key]
|
914 | 7c8ae421 | Bernardo Dal Seno | ret_specs[constants.ISPECS_MINMAX].append(ret_minmax) |
915 | 7c8ae421 | Bernardo Dal Seno | elif key == constants.ISPECS_STD:
|
916 | ec996117 | Bernardo Dal Seno | ret_specs[key] = val |
917 | 63e08b25 | Bernardo Dal Seno | else:
|
918 | 63e08b25 | Bernardo Dal Seno | ret_policy[key] = val |
919 | 63e08b25 | Bernardo Dal Seno | return (ret_policy, ret_specs)
|
920 | a02dbfca | Petr Pudlak | |
921 | a02dbfca | Petr Pudlak | |
922 | a02dbfca | Petr Pudlak | def UsesIPv6Connection(host, port): |
923 | a02dbfca | Petr Pudlak | """Returns True if the connection to a given host/port could go through IPv6.
|
924 | a02dbfca | Petr Pudlak |
|
925 | a02dbfca | Petr Pudlak | """
|
926 | a02dbfca | Petr Pudlak | return any(t[0] == socket.AF_INET6 for t in socket.getaddrinfo(host, port)) |