import-export_unittest-helper 2.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/python
#

# Copyright (C) 2010 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


"""Helpers for testing import-export daemon"""

import os
import sys
import errno
27
import time
28
29
30
31
32
33
34
35
36

from ganeti import constants
from ganeti import utils
from ganeti import objects
from ganeti import serializer


RETRY_INTERVAL = 0.1
TIMEOUT = int(os.getenv("TIMEOUT", 10))
37
VALIDITY = int(os.getenv("VALIDITY", 1))
38
39


40
41
42
43
44
45
46
47
48
49
def Log(msg, *args):
  if args:
    line = msg % args
  else:
    line = msg

  sys.stderr.write("%0.6f, pid %s: %s\n" % (time.time(), os.getpid(), line))
  sys.stderr.flush()


50
51
52
53
def _GetImportExportData(filename):
  try:
    data = utils.ReadFile(filename)
  except EnvironmentError, err:
54
    Log("%s = %s", filename, err)
55
56
57
58
    if err.errno != errno.ENOENT:
      raise
    raise utils.RetryAgain()

59
60
  Log("%s = %s", filename, data.strip())

61
62
63
64
65
  return objects.ImportExportStatus.FromDict(serializer.LoadJson(data))


def _CheckConnected(filename):
  if not _GetImportExportData(filename).connected:
66
67
68
69
70
71
72
73
74
75
    Log("Not connected")
    raise utils.RetryAgain()

  Log("Connected")


def _CheckListenPort(filename):
  port = _GetImportExportData(filename).listen_port
  if not port:
    Log("No port")
76
    raise utils.RetryAgain()
77
78
  Log("Listening on %s", port)
  return port
79
80
81


def WaitForListenPort(filename):
82
83
  return utils.Retry(_CheckListenPort, RETRY_INTERVAL, TIMEOUT,
                     args=(filename, ))
84
85
86
87
88
89
90
91
92


def WaitForConnected(filename):
  utils.Retry(_CheckConnected, RETRY_INTERVAL, TIMEOUT, args=(filename, ))


def main():
  (filename, what) = sys.argv[1:]

93
94
  Log("Running helper for %s %s", filename, what)

95
96
97
98
99
  if what == "listen-port":
    print WaitForListenPort(filename)
  elif what == "connected":
    WaitForConnected(filename)
  elif what == "gencert":
100
    utils.GenerateSelfSignedSslCert(filename, validity=VALIDITY)
101
102
103
104
105
106
  else:
    raise Exception("Unknown command '%s'" % what)


if __name__ == "__main__":
  main()