bash_completion: Enable extglob while parsing file
[ganeti-local] / test / import-export_unittest-helper
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 import time
28
29 from ganeti import constants
30 from ganeti import utils
31 from ganeti import objects
32 from ganeti import serializer
33
34
35 RETRY_INTERVAL = (0.1, 1.1, 1)
36 TIMEOUT = int(os.getenv("TIMEOUT", 30))
37 VALIDITY = int(os.getenv("VALIDITY", 1))
38
39
40 def Log(msg, *args):
41   if args:
42     line = msg % args
43   else:
44     line = msg
45
46   sys.stderr.write("%0.6f, pid %s: %s\n" % (time.time(), os.getpid(), line))
47   sys.stderr.flush()
48
49
50 def _GetImportExportData(filename):
51   try:
52     data = utils.ReadFile(filename)
53   except EnvironmentError, err:
54     Log("%s = %s", filename, err)
55     if err.errno != errno.ENOENT:
56       raise
57     raise utils.RetryAgain()
58
59   Log("%s = %s", filename, data.strip())
60
61   return objects.ImportExportStatus.FromDict(serializer.LoadJson(data))
62
63
64 def _CheckConnected(filename):
65   if not _GetImportExportData(filename).connected:
66     Log("Not connected")
67     raise utils.RetryAgain()
68
69   Log("Connected")
70
71
72 def _CheckListenPort(filename):
73   port = _GetImportExportData(filename).listen_port
74   if not port:
75     Log("No port")
76     raise utils.RetryAgain()
77   Log("Listening on %s", port)
78   return port
79
80
81 def WaitForListenPort(filename):
82   return utils.Retry(_CheckListenPort, RETRY_INTERVAL, TIMEOUT,
83                      args=(filename, ))
84
85
86 def WaitForConnected(filename):
87   utils.Retry(_CheckConnected, RETRY_INTERVAL, TIMEOUT, args=(filename, ))
88
89
90 def main():
91   (filename, what) = sys.argv[1:]
92
93   Log("Running helper for %s %s", filename, what)
94
95   if what == "listen-port":
96     print WaitForListenPort(filename)
97   elif what == "connected":
98     WaitForConnected(filename)
99   elif what == "gencert":
100     utils.GenerateSelfSignedSslCert(filename, validity=VALIDITY)
101   else:
102     raise Exception("Unknown command '%s'" % what)
103
104
105 if __name__ == "__main__":
106   main()