root / test / import-export_unittest-helper @ c6ccba7e
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 |
VALIDITY = int(os.getenv("VALIDITY", 1)) |
37 |
|
38 |
|
39 |
def _GetImportExportData(filename): |
40 |
try: |
41 |
data = utils.ReadFile(filename) |
42 |
except EnvironmentError, err: |
43 |
if err.errno != errno.ENOENT: |
44 |
raise |
45 |
raise utils.RetryAgain() |
46 |
|
47 |
return objects.ImportExportStatus.FromDict(serializer.LoadJson(data)) |
48 |
|
49 |
|
50 |
def _CheckConnected(filename): |
51 |
if not _GetImportExportData(filename).connected: |
52 |
raise utils.RetryAgain() |
53 |
|
54 |
|
55 |
def WaitForListenPort(filename): |
56 |
return utils.Retry(lambda: _GetImportExportData(filename).listen_port, |
57 |
RETRY_INTERVAL, TIMEOUT) |
58 |
|
59 |
|
60 |
def WaitForConnected(filename): |
61 |
utils.Retry(_CheckConnected, RETRY_INTERVAL, TIMEOUT, args=(filename, )) |
62 |
|
63 |
|
64 |
def main(): |
65 |
(filename, what) = sys.argv[1:] |
66 |
|
67 |
if what == "listen-port": |
68 |
print WaitForListenPort(filename) |
69 |
elif what == "connected": |
70 |
WaitForConnected(filename) |
71 |
elif what == "gencert": |
72 |
utils.GenerateSelfSignedSslCert(filename, validity=VALIDITY) |
73 |
else: |
74 |
raise Exception("Unknown command '%s'" % what) |
75 |
|
76 |
|
77 |
if __name__ == "__main__": |
78 |
main() |