oo_config_tests.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. - connect_to: master-private.example.com
  15. ip: 10.0.0.1
  16. hostname: master-private.example.com
  17. public_ip: 24.222.0.1
  18. public_hostname: master.example.com
  19. master: true
  20. node: true
  21. - connect_to: node1-private.example.com
  22. ip: 10.0.0.2
  23. hostname: node1-private.example.com
  24. public_ip: 24.222.0.2
  25. public_hostname: node1.example.com
  26. node: true
  27. - connect_to: node2-private.example.com
  28. ip: 10.0.0.3
  29. hostname: node2-private.example.com
  30. public_ip: 24.222.0.3
  31. public_hostname: node2.example.com
  32. node: true
  33. """
  34. # Used to test automatic upgrading of config:
  35. LEGACY_CONFIG = """
  36. Description: This is the configuration file for the OpenShift Ansible-Based Installer.
  37. Name: OpenShift Ansible-Based Installer Configuration
  38. Subscription: {type: none}
  39. Vendor: OpenShift Community
  40. Version: 0.0.1
  41. ansible_config: /tmp/notreal/ansible.cfg
  42. ansible_inventory_directory: /tmp/notreal/.config/openshift/.ansible
  43. ansible_log_path: /tmp/ansible.log
  44. ansible_plugins_directory: /tmp/notreal/.python-eggs/ooinstall-3.0.0-py2.7.egg-tmp/ooinstall/ansible_plugins
  45. masters: [10.0.0.1]
  46. nodes: [10.0.0.2, 10.0.0.3]
  47. validated_facts:
  48. 10.0.0.1: {hostname: master-private.example.com, ip: 10.0.0.1, public_hostname: master.example.com, public_ip: 24.222.0.1}
  49. 10.0.0.2: {hostname: node1-private.example.com, ip: 10.0.0.2, public_hostname: node1.example.com, public_ip: 24.222.0.2}
  50. 10.0.0.3: {hostname: node2-private.example.com, ip: 10.0.0.3, public_hostname: node2.example.com, public_ip: 24.222.0.3}
  51. """
  52. CONFIG_INCOMPLETE_FACTS = """
  53. hosts:
  54. - connect_to: 10.0.0.1
  55. ip: 10.0.0.1
  56. hostname: master-private.example.com
  57. public_ip: 24.222.0.1
  58. public_hostname: master.example.com
  59. master: true
  60. - connect_to: 10.0.0.2
  61. ip: 10.0.0.2
  62. hostname: 24.222.0.2
  63. public_ip: 24.222.0.2
  64. node: true
  65. - connect_to: 10.0.0.3
  66. ip: 10.0.0.3
  67. node: true
  68. """
  69. class OOInstallFixture(unittest.TestCase):
  70. def setUp(self):
  71. self.tempfiles = []
  72. self.work_dir = tempfile.mkdtemp(prefix='ooconfigtests')
  73. self.tempfiles.append(self.work_dir)
  74. def tearDown(self):
  75. for path in self.tempfiles:
  76. if os.path.isdir(path):
  77. shutil.rmtree(path)
  78. else:
  79. os.remove(path)
  80. def write_config(self, path, config_str):
  81. """
  82. Write given config to a temporary file which will be cleaned
  83. up in teardown.
  84. Returns full path to the file.
  85. """
  86. cfg_file = open(path, 'w')
  87. cfg_file.write(config_str)
  88. cfg_file.close()
  89. return path
  90. class LegacyOOConfigTests(OOInstallFixture):
  91. def setUp(self):
  92. OOInstallFixture.setUp(self)
  93. self.cfg_path = self.write_config(os.path.join(self.work_dir,
  94. 'ooinstall.conf'), LEGACY_CONFIG)
  95. self.cfg = OOConfig(self.cfg_path)
  96. def test_load_config_memory(self):
  97. self.assertEquals('openshift-enterprise', self.cfg.settings['variant'])
  98. self.assertEquals('3.0', self.cfg.settings['variant_version'])
  99. self.assertEquals('v1', self.cfg.settings['version'])
  100. self.assertEquals(3, len(self.cfg.hosts))
  101. h1 = self.cfg.get_host('10.0.0.1')
  102. self.assertEquals('10.0.0.1', h1.ip)
  103. self.assertEquals('24.222.0.1', h1.public_ip)
  104. self.assertEquals('master-private.example.com', h1.hostname)
  105. self.assertEquals('master.example.com', h1.public_hostname)
  106. h2 = self.cfg.get_host('10.0.0.2')
  107. self.assertEquals('10.0.0.2', h2.ip)
  108. self.assertEquals('24.222.0.2', h2.public_ip)
  109. self.assertEquals('node1-private.example.com', h2.hostname)
  110. self.assertEquals('node1.example.com', h2.public_hostname)
  111. h3 = self.cfg.get_host('10.0.0.3')
  112. self.assertEquals('10.0.0.3', h3.ip)
  113. self.assertEquals('24.222.0.3', h3.public_ip)
  114. self.assertEquals('node2-private.example.com', h3.hostname)
  115. self.assertEquals('node2.example.com', h3.public_hostname)
  116. self.assertFalse('masters' in self.cfg.settings)
  117. self.assertFalse('nodes' in self.cfg.settings)
  118. self.assertFalse('Description' in self.cfg.settings)
  119. self.assertFalse('Name' in self.cfg.settings)
  120. self.assertFalse('Subscription' in self.cfg.settings)
  121. self.assertFalse('Vendor' in self.cfg.settings)
  122. self.assertFalse('Version' in self.cfg.settings)
  123. self.assertFalse('validates_facts' in self.cfg.settings)
  124. class OOConfigTests(OOInstallFixture):
  125. def test_load_config(self):
  126. cfg_path = self.write_config(os.path.join(self.work_dir,
  127. 'ooinstall.conf'), SAMPLE_CONFIG)
  128. ooconfig = OOConfig(cfg_path)
  129. self.assertEquals(3, len(ooconfig.hosts))
  130. self.assertEquals("master-private.example.com", ooconfig.hosts[0].connect_to)
  131. self.assertEquals("10.0.0.1", ooconfig.hosts[0].ip)
  132. self.assertEquals("master-private.example.com", ooconfig.hosts[0].hostname)
  133. self.assertEquals(["10.0.0.1", "10.0.0.2", "10.0.0.3"],
  134. [host['ip'] for host in ooconfig.settings['hosts']])
  135. self.assertEquals('openshift-enterprise', ooconfig.settings['variant'])
  136. self.assertEquals('v1', ooconfig.settings['version'])
  137. def test_load_complete_facts(self):
  138. cfg_path = self.write_config(os.path.join(self.work_dir,
  139. 'ooinstall.conf'), SAMPLE_CONFIG)
  140. ooconfig = OOConfig(cfg_path)
  141. missing_host_facts = ooconfig.calc_missing_facts()
  142. self.assertEquals(0, len(missing_host_facts))
  143. # Test missing optional facts the user must confirm:
  144. def test_load_host_incomplete_facts(self):
  145. cfg_path = self.write_config(os.path.join(self.work_dir,
  146. 'ooinstall.conf'), CONFIG_INCOMPLETE_FACTS)
  147. ooconfig = OOConfig(cfg_path)
  148. missing_host_facts = ooconfig.calc_missing_facts()
  149. self.assertEquals(2, len(missing_host_facts))
  150. self.assertEquals(1, len(missing_host_facts['10.0.0.2']))
  151. self.assertEquals(3, len(missing_host_facts['10.0.0.3']))
  152. def test_write_config(self):
  153. cfg_path = self.write_config(os.path.join(self.work_dir,
  154. 'ooinstall.conf'), SAMPLE_CONFIG)
  155. ooconfig = OOConfig(cfg_path)
  156. ooconfig.save_to_disk()
  157. f = open(cfg_path, 'r')
  158. written_config = yaml.safe_load(f.read())
  159. f.close()
  160. self.assertEquals(3, len(written_config['hosts']))
  161. for h in written_config['hosts']:
  162. self.assertTrue('ip' in h)
  163. self.assertTrue('public_ip' in h)
  164. self.assertTrue('hostname' in h)
  165. self.assertTrue('public_hostname' in h)
  166. self.assertTrue('ansible_ssh_user' in written_config)
  167. self.assertTrue('variant' in written_config)
  168. self.assertEquals('v1', written_config['version'])
  169. # Some advanced settings should not get written out if they
  170. # were not specified by the user:
  171. self.assertFalse('ansible_inventory_directory' in written_config)
  172. class HostTests(OOInstallFixture):
  173. def test_load_host_no_ip_or_hostname(self):
  174. yaml_props = {
  175. 'public_ip': '192.168.0.1',
  176. 'public_hostname': 'a.example.com',
  177. 'master': True
  178. }
  179. self.assertRaises(OOConfigInvalidHostError, Host, **yaml_props)
  180. def test_load_host_no_master_or_node_specified(self):
  181. yaml_props = {
  182. 'ip': '192.168.0.1',
  183. 'hostname': 'a.example.com',
  184. 'public_ip': '192.168.0.1',
  185. 'public_hostname': 'a.example.com',
  186. }
  187. self.assertRaises(OOConfigInvalidHostError, Host, **yaml_props)