oo_config_tests.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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: /home/dgoodwin/.python-eggs/ooinstall-3.0.0-py2.7.egg-tmp/ooinstall/ansible.cfg
  39. ansible_inventory_directory: /home/dgoodwin/.config/openshift/.ansible
  40. ansible_log_path: /tmp/ansible.log
  41. ansible_plugins_directory: /home/dgoodwin/.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(3, len(self.cfg.hosts))
  94. h1 = self.cfg.get_host('10.0.0.1')
  95. self.assertEquals('10.0.0.1', h1.ip)
  96. self.assertEquals('24.222.0.1', h1.public_ip)
  97. self.assertEquals('master-private.example.com', h1.hostname)
  98. self.assertEquals('master.example.com', h1.public_hostname)
  99. h2 = self.cfg.get_host('10.0.0.2')
  100. self.assertEquals('10.0.0.2', h2.ip)
  101. self.assertEquals('24.222.0.2', h2.public_ip)
  102. self.assertEquals('node1-private.example.com', h2.hostname)
  103. self.assertEquals('node1.example.com', h2.public_hostname)
  104. h3 = self.cfg.get_host('10.0.0.3')
  105. self.assertEquals('10.0.0.3', h3.ip)
  106. self.assertEquals('24.222.0.3', h3.public_ip)
  107. self.assertEquals('node2-private.example.com', h3.hostname)
  108. self.assertEquals('node2.example.com', h3.public_hostname)
  109. self.assertFalse('masters' in self.cfg.settings)
  110. self.assertFalse('nodes' in self.cfg.settings)
  111. self.assertFalse('Description' in self.cfg.settings)
  112. self.assertFalse('Name' in self.cfg.settings)
  113. self.assertFalse('Subscription' in self.cfg.settings)
  114. self.assertFalse('Vendor' in self.cfg.settings)
  115. self.assertFalse('Version' in self.cfg.settings)
  116. self.assertFalse('validates_facts' in self.cfg.settings)
  117. class OOConfigTests(OOInstallFixture):
  118. def test_load_config(self):
  119. cfg_path = self.write_config(os.path.join(self.work_dir,
  120. 'ooinstall.conf'), SAMPLE_CONFIG)
  121. ooconfig = OOConfig(cfg_path)
  122. self.assertEquals(3, len(ooconfig.hosts))
  123. self.assertEquals("10.0.0.1", ooconfig.hosts[0].name)
  124. self.assertEquals("10.0.0.1", ooconfig.hosts[0].ip)
  125. self.assertEquals("master-private.example.com", ooconfig.hosts[0].hostname)
  126. self.assertEquals(["10.0.0.1", "10.0.0.2", "10.0.0.3"],
  127. [host['ip'] for host in ooconfig.settings['hosts']])
  128. self.assertEquals('openshift-enterprise', ooconfig.settings['variant'])
  129. def test_load_complete_facts(self):
  130. cfg_path = self.write_config(os.path.join(self.work_dir,
  131. 'ooinstall.conf'), SAMPLE_CONFIG)
  132. ooconfig = OOConfig(cfg_path)
  133. missing_host_facts = ooconfig.calc_missing_facts()
  134. self.assertEquals(0, len(missing_host_facts))
  135. # Test missing optional facts the user must confirm:
  136. def test_load_host_incomplete_facts(self):
  137. cfg_path = self.write_config(os.path.join(self.work_dir,
  138. 'ooinstall.conf'), CONFIG_INCOMPLETE_FACTS)
  139. ooconfig = OOConfig(cfg_path)
  140. missing_host_facts = ooconfig.calc_missing_facts()
  141. self.assertEquals(2, len(missing_host_facts))
  142. self.assertEquals(1, len(missing_host_facts['10.0.0.2']))
  143. self.assertEquals(3, len(missing_host_facts['10.0.0.3']))
  144. def test_write_config(self):
  145. cfg_path = self.write_config(os.path.join(self.work_dir,
  146. 'ooinstall.conf'), SAMPLE_CONFIG)
  147. ooconfig = OOConfig(cfg_path)
  148. ooconfig.save_to_disk()
  149. f = open(cfg_path, 'r')
  150. written_config = yaml.safe_load(f.read())
  151. f.close()
  152. self.assertEquals(3, len(written_config['hosts']))
  153. for h in written_config['hosts']:
  154. self.assertTrue('ip' in h)
  155. self.assertTrue('public_ip' in h)
  156. self.assertTrue('hostname' in h)
  157. self.assertTrue('public_hostname' in h)
  158. self.assertTrue('ansible_ssh_user' in written_config)
  159. self.assertTrue('variant' in written_config)
  160. # Some advanced settings should not get written out if they
  161. # were not specified by the user:
  162. self.assertFalse('ansible_inventory_directory' in written_config)
  163. class HostTests(OOInstallFixture):
  164. def test_load_host_no_ip_or_hostname(self):
  165. yaml_props = {
  166. 'public_ip': '192.168.0.1',
  167. 'public_hostname': 'a.example.com',
  168. 'master': True
  169. }
  170. self.assertRaises(OOConfigInvalidHostError, Host, **yaml_props)
  171. def test_load_host_no_master_or_node_specified(self):
  172. yaml_props = {
  173. 'ip': '192.168.0.1',
  174. 'hostname': 'a.example.com',
  175. 'public_ip': '192.168.0.1',
  176. 'public_hostname': 'a.example.com',
  177. }
  178. self.assertRaises(OOConfigInvalidHostError, Host, **yaml_props)