oo_config_tests.py 7.7 KB

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