test_run_setup.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # -*- coding: utf-8 -*-
  2. # vim: tabstop=4 shiftwidth=4 softtabstop=4
  3. # Copyright 2013, Red Hat, Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. # not use this file except in compliance with the License. You may obtain
  7. # a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. # License for the specific language governing permissions and limitations
  15. # under the License.
  16. import os
  17. import shutil
  18. import subprocess
  19. import sys
  20. from unittest import TestCase
  21. from packstack.modules import ospluginutils
  22. from packstack.modules import puppet
  23. from packstack.installer import basedefs
  24. from packstack.installer import run_setup
  25. from packstack.installer import validators
  26. from ..test_base import FakePopen
  27. from ..test_base import PackstackTestCaseMixin
  28. def makefile(path, content):
  29. '''Create the named file with the specified content.'''
  30. with open(path, 'w') as fd:
  31. fd.write(content)
  32. class CommandLineTestCase(PackstackTestCaseMixin, TestCase):
  33. def setUp(self):
  34. super(CommandLineTestCase, self).setUp()
  35. self._old_validate_ssh = validators.validate_ssh
  36. validators.validate_ssh = lambda param, options=None: None
  37. def tearDown(self):
  38. super(CommandLineTestCase, self).tearDown()
  39. validators.validate_ssh = self._old_validate_ssh
  40. def test_running_install_hosts(self):
  41. """
  42. Test packstack.installer.run_setup.main
  43. This test effectivly runs all of the python code ran by
  44. packstack --install-hosts=127.0.0.1 --os-swift-install=y \
  45. --nagios-install=y
  46. It is a fairly wide net but boost code coverage of the packstack
  47. python code to about 85%, more finer grained tests should also be
  48. Added to target speficic test cases.
  49. Popen is replaced in PackstackTestCaseMixin so no actual commands get
  50. run on the host running the unit tests
  51. """
  52. subprocess.Popen = FakePopen
  53. FakePopen.register('cat /etc/resolv.conf | grep nameserver',
  54. stdout='nameserver 127.0.0.1')
  55. # required by packstack.plugins.serverprep_949.mangage_rdo
  56. FakePopen.register("rpm -q rdo-release "
  57. "--qf='%{version}-%{release}.%{arch}\n'",
  58. stdout='icehouse-2.noarch\n')
  59. FakePopen.register_as_script('yum-config-manager --enable '
  60. 'openstack-icehouse',
  61. stdout='[openstack-icehouse]\nenabled=1')
  62. FakePopen.register_as_script(
  63. 'facter -p',
  64. stdout='operatingsystem => Fedora\noperatingsystemmajrelease => 21'
  65. )
  66. # required by packstack.plugins.nova_300.gather_host_keys
  67. FakePopen.register('ssh-keyscan 127.0.0.1',
  68. stdout='127.0.0.1 ssh-rsa hostkey-data')
  69. # create a dummy public key
  70. dummy_public_key = os.path.join(self.tempdir, 'id_rsa.pub')
  71. makefile(dummy_public_key, 'ssh-rsa AAAAblablabla')
  72. # create dummy keys for live migration mechanism
  73. makefile(os.path.join(basedefs.VAR_DIR, 'nova_migration_key'),
  74. '-----BEGIN RSA PRIVATE KEY-----\n'
  75. 'keydata\n'
  76. '-----END RSA PRIVATE KEY-----\n')
  77. makefile(os.path.join(basedefs.VAR_DIR, 'nova_migration_key.pub'),
  78. 'ssh-rsa keydata')
  79. # Save sys.argv and replace it with the args we want optparse to use
  80. orig_argv = sys.argv
  81. sys.argv = ['packstack', '--debug',
  82. '--ssh-public-key=%s' % dummy_public_key,
  83. '--install-hosts=127.0.0.1', '--os-swift-install=y',
  84. '--nagios-install=y', '--use-epel=y', '--ssl-cacert-selfsign=y',
  85. '--ssl-cert-dir=%s' % os.path.expanduser('~/')]
  86. # There is no puppet logfile to validate, so replace
  87. # ospluginutils.validate_puppet_logfile with a mock function
  88. orig_validate_logfile = puppet.validate_logfile
  89. puppet.validate_logfile = lambda a: None
  90. puppet.scan_logfile = lambda a: []
  91. # If there is a error in a plugin sys.exit() gets called, this masks
  92. # the actual error that should be reported, so we replace it to
  93. # raise Exception, packstack logging gives a more infomrative error
  94. def raise_(ex):
  95. raise ex
  96. orig_sys_exit = sys.exit
  97. sys.exit = lambda a: raise_(Exception('Error during install-hosts'))
  98. try:
  99. run_setup.main()
  100. finally:
  101. sys.argv = orig_argv
  102. ospluginutils.validate_puppet_logfile = orig_validate_logfile
  103. sys.exit = orig_sys_exit
  104. try:
  105. shutil.rmtree(basedefs.VAR_DIR)
  106. except:
  107. pass