Skip to content
Snippets Groups Projects
import-export_unittest-helper 2.55 KiB
Newer Older
#!/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

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


RETRY_INTERVAL = (0.1, 1.1, 1)
TIMEOUT = int(os.getenv("TIMEOUT", 30))
VALIDITY = int(os.getenv("VALIDITY", 1))
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()


def _GetImportExportData(filename):
  try:
    data = utils.ReadFile(filename)
  except EnvironmentError, err:
    if err.errno != errno.ENOENT:
      raise
    raise utils.RetryAgain()

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

  return objects.ImportExportStatus.FromDict(serializer.LoadJson(data))


def _CheckConnected(filename):
  if not _GetImportExportData(filename).connected:
    Log("Not connected")
    raise utils.RetryAgain()

  Log("Connected")


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


def WaitForListenPort(filename):
  return utils.Retry(_CheckListenPort, RETRY_INTERVAL, TIMEOUT,
                     args=(filename, ))


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


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

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

  if what == "listen-port":
    print WaitForListenPort(filename)
  elif what == "connected":
    WaitForConnected(filename)
  elif what == "gencert":
    utils.GenerateSelfSignedSslCert(filename, validity=VALIDITY)
  else:
    raise Exception("Unknown command '%s'" % what)


if __name__ == "__main__":
  main()