oo_config_tests.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # TODO: Temporarily disabled due to importing old code into openshift-ansible
  2. # repo. We will work on these over time.
  3. # pylint: disable=bad-continuation,missing-docstring,no-self-use,invalid-name
  4. import os
  5. import unittest
  6. import tempfile
  7. import shutil
  8. import yaml
  9. from ooinstall.oo_config import OOConfig, Host, OOConfigInvalidHostError
  10. SAMPLE_CONFIG = """
  11. variant: openshift-enterprise
  12. ansible_ssh_user: root
  13. hosts:
  14. - ip: 10.0.0.1
  15. hostname: master-private.example.com
  16. public_ip: 24.222.0.1
  17. public_hostname: master.example.com
  18. master: true
  19. node: true
  20. - ip: 10.0.0.2
  21. hostname: node1-private.example.com
  22. public_ip: 24.222.0.2
  23. public_hostname: node1.example.com
  24. node: true
  25. - ip: 10.0.0.3
  26. hostname: node2-private.example.com
  27. public_ip: 24.222.0.3
  28. public_hostname: node2.example.com
  29. node: true
  30. """
  31. CONFIG_INCOMPLETE_FACTS = """
  32. hosts:
  33. - ip: 10.0.0.1
  34. hostname: master-private.example.com
  35. public_ip: 24.222.0.1
  36. public_hostname: master.example.com
  37. master: true
  38. - ip: 10.0.0.2
  39. hostname: node1-private.example.com
  40. public_ip: 24.222.0.2
  41. node: true
  42. - ip: 10.0.0.3
  43. node: true
  44. """
  45. class OOInstallFixture(unittest.TestCase):
  46. def setUp(self):
  47. self.tempfiles = []
  48. self.work_dir = tempfile.mkdtemp(prefix='ooconfigtests')
  49. self.tempfiles.append(self.work_dir)
  50. def tearDown(self):
  51. for path in self.tempfiles:
  52. if os.path.isdir(path):
  53. shutil.rmtree(path)
  54. else:
  55. os.remove(path)
  56. def write_config(self, path, config_str):
  57. """
  58. Write given config to a temporary file which will be cleaned
  59. up in teardown.
  60. Returns full path to the file.
  61. """
  62. cfg_file = open(path, 'w')
  63. cfg_file.write(config_str)
  64. cfg_file.close()
  65. return path
  66. class OOConfigTests(OOInstallFixture):
  67. def test_load_config(self):
  68. cfg_path = self.write_config(os.path.join(self.work_dir,
  69. 'ooinstall.conf'), SAMPLE_CONFIG)
  70. ooconfig = OOConfig(cfg_path)
  71. self.assertEquals(3, len(ooconfig.hosts))
  72. self.assertEquals("10.0.0.1", ooconfig.hosts[0].name)
  73. self.assertEquals("10.0.0.1", ooconfig.hosts[0].ip)
  74. self.assertEquals("master-private.example.com", ooconfig.hosts[0].hostname)
  75. self.assertEquals(["10.0.0.1", "10.0.0.2", "10.0.0.3"],
  76. [host['ip'] for host in ooconfig.settings['hosts']])
  77. self.assertEquals('openshift-enterprise', ooconfig.settings['variant'])
  78. def test_load_complete_facts(self):
  79. cfg_path = self.write_config(os.path.join(self.work_dir,
  80. 'ooinstall.conf'), SAMPLE_CONFIG)
  81. ooconfig = OOConfig(cfg_path)
  82. missing_host_facts = ooconfig.calc_missing_facts()
  83. self.assertEquals(0, len(missing_host_facts))
  84. # Test missing optional facts the user must confirm:
  85. def test_load_host_incomplete_facts(self):
  86. cfg_path = self.write_config(os.path.join(self.work_dir,
  87. 'ooinstall.conf'), CONFIG_INCOMPLETE_FACTS)
  88. ooconfig = OOConfig(cfg_path)
  89. missing_host_facts = ooconfig.calc_missing_facts()
  90. self.assertEquals(2, len(missing_host_facts))
  91. self.assertEquals(1, len(missing_host_facts['10.0.0.2']))
  92. self.assertEquals(3, len(missing_host_facts['10.0.0.3']))
  93. def test_write_config(self):
  94. cfg_path = self.write_config(os.path.join(self.work_dir,
  95. 'ooinstall.conf'), SAMPLE_CONFIG)
  96. ooconfig = OOConfig(cfg_path)
  97. ooconfig.save_to_disk()
  98. f = open(cfg_path, 'r')
  99. written_config = yaml.safe_load(f.read())
  100. f.close()
  101. self.assertEquals(3, len(written_config['hosts']))
  102. for h in written_config['hosts']:
  103. self.assertTrue('ip' in h)
  104. self.assertTrue('public_ip' in h)
  105. self.assertTrue('hostname' in h)
  106. self.assertTrue('public_hostname' in h)
  107. self.assertTrue('ansible_ssh_user' in written_config)
  108. self.assertTrue('variant' in written_config)
  109. # Some advanced settings should not get written out if they
  110. # were not specified by the user:
  111. self.assertFalse('ansible_inventory_directory' in written_config)
  112. class HostTests(OOInstallFixture):
  113. def test_load_host_no_ip_or_hostname(self):
  114. yaml_props = {
  115. 'public_ip': '192.168.0.1',
  116. 'public_hostname': 'a.example.com',
  117. 'master': True
  118. }
  119. self.assertRaises(OOConfigInvalidHostError, Host, **yaml_props)
  120. def test_load_host_no_master_or_node_specified(self):
  121. yaml_props = {
  122. 'ip': '192.168.0.1',
  123. 'hostname': 'a.example.com',
  124. 'public_ip': '192.168.0.1',
  125. 'public_hostname': 'a.example.com',
  126. }
  127. self.assertRaises(OOConfigInvalidHostError, Host, **yaml_props)