root / test / import-export_unittest-helper @ 2d76b580
History | View | Annotate | Download (2 kB)
1 |
#!/usr/bin/python |
---|---|
2 |
# |
3 |
|
4 |
# Copyright (C) 2010 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 |
"""Helpers for testing import-export daemon""" |
23 |
|
24 |
import os |
25 |
import sys |
26 |
import errno |
27 |
|
28 |
from ganeti import constants |
29 |
from ganeti import utils |
30 |
from ganeti import objects |
31 |
from ganeti import serializer |
32 |
|
33 |
|
34 |
RETRY_INTERVAL = 0.1 |
35 |
TIMEOUT = int(os.getenv("TIMEOUT", 10)) |
36 |
|
37 |
|
38 |
def _GetImportExportData(filename): |
39 |
try: |
40 |
data = utils.ReadFile(filename) |
41 |
except EnvironmentError, err: |
42 |
if err.errno != errno.ENOENT: |
43 |
raise |
44 |
raise utils.RetryAgain() |
45 |
|
46 |
return objects.ImportExportStatus.FromDict(serializer.LoadJson(data)) |
47 |
|
48 |
|
49 |
def _CheckConnected(filename): |
50 |
if not _GetImportExportData(filename).connected: |
51 |
raise utils.RetryAgain() |
52 |
|
53 |
|
54 |
def WaitForListenPort(filename): |
55 |
return utils.Retry(lambda: _GetImportExportData(filename).listen_port, |
56 |
RETRY_INTERVAL, TIMEOUT) |
57 |
|
58 |
|
59 |
def WaitForConnected(filename): |
60 |
utils.Retry(_CheckConnected, RETRY_INTERVAL, TIMEOUT, args=(filename, )) |
61 |
|
62 |
|
63 |
def main(): |
64 |
(filename, what) = sys.argv[1:] |
65 |
|
66 |
if what == "listen-port": |
67 |
print WaitForListenPort(filename) |
68 |
elif what == "connected": |
69 |
WaitForConnected(filename) |
70 |
elif what == "gencert": |
71 |
utils.GenerateSelfSignedSslCert(filename, validity=1) |
72 |
else: |
73 |
raise Exception("Unknown command '%s'" % what) |
74 |
|
75 |
|
76 |
if __name__ == "__main__": |
77 |
main() |