oo_config_tests.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. CONFIG_BAD = """
  70. variant: openshift-enterprise
  71. ansible_ssh_user: root
  72. hosts:
  73. - connect_to: master-private.example.com
  74. ip: 10.0.0.1
  75. hostname: master-private.example.com
  76. public_ip: 24.222.0.1
  77. public_hostname: master.example.com
  78. master: true
  79. node: true
  80. - ip: 10.0.0.2
  81. hostname: node1-private.example.com
  82. public_ip: 24.222.0.2
  83. public_hostname: node1.example.com
  84. node: true
  85. - connect_to: node2-private.example.com
  86. ip: 10.0.0.3
  87. hostname: node2-private.example.com
  88. public_ip: 24.222.0.3
  89. public_hostname: node2.example.com
  90. node: true
  91. """
  92. class OOInstallFixture(unittest.TestCase):
  93. def setUp(self):
  94. self.tempfiles = []
  95. self.work_dir = tempfile.mkdtemp(prefix='ooconfigtests')
  96. self.tempfiles.append(self.work_dir)
  97. def tearDown(self):
  98. for path in self.tempfiles:
  99. if os.path.isdir(path):
  100. shutil.rmtree(path)
  101. else:
  102. os.remove(path)
  103. def write_config(self, path, config_str):
  104. """
  105. Write given config to a temporary file which will be cleaned
  106. up in teardown.
  107. Returns full path to the file.
  108. """
  109. cfg_file = open(path, 'w')
  110. cfg_file.write(config_str)
  111. cfg_file.close()
  112. return path
  113. class LegacyOOConfigTests(OOInstallFixture):
  114. def setUp(self):
  115. OOInstallFixture.setUp(self)
  116. self.cfg_path = self.write_config(os.path.join(self.work_dir,
  117. 'ooinstall.conf'), LEGACY_CONFIG)
  118. self.cfg = OOConfig(self.cfg_path)
  119. def test_load_config_memory(self):
  120. self.assertEquals('openshift-enterprise', self.cfg.settings['variant'])
  121. self.assertEquals('3.0', self.cfg.settings['variant_version'])
  122. self.assertEquals('v1', self.cfg.settings['version'])
  123. self.assertEquals(3, len(self.cfg.hosts))
  124. h1 = self.cfg.get_host('10.0.0.1')
  125. self.assertEquals('10.0.0.1', h1.ip)
  126. self.assertEquals('24.222.0.1', h1.public_ip)
  127. self.assertEquals('master-private.example.com', h1.hostname)
  128. self.assertEquals('master.example.com', h1.public_hostname)
  129. h2 = self.cfg.get_host('10.0.0.2')
  130. self.assertEquals('10.0.0.2', h2.ip)
  131. self.assertEquals('24.222.0.2', h2.public_ip)
  132. self.assertEquals('node1-private.example.com', h2.hostname)
  133. self.assertEquals('node1.example.com', h2.public_hostname)
  134. h3 = self.cfg.get_host('10.0.0.3')
  135. self.assertEquals('10.0.0.3', h3.ip)
  136. self.assertEquals('24.222.0.3', h3.public_ip)
  137. self.assertEquals('node2-private.example.com', h3.hostname)
  138. self.assertEquals('node2.example.com', h3.public_hostname)
  139. self.assertFalse('masters' in self.cfg.settings)
  140. self.assertFalse('nodes' in self.cfg.settings)
  141. self.assertFalse('Description' in self.cfg.settings)
  142. self.assertFalse('Name' in self.cfg.settings)
  143. self.assertFalse('Subscription' in self.cfg.settings)
  144. self.assertFalse('Vendor' in self.cfg.settings)
  145. self.assertFalse('Version' in self.cfg.settings)
  146. self.assertFalse('validates_facts' in self.cfg.settings)
  147. class OOConfigTests(OOInstallFixture):
  148. def test_load_config(self):
  149. cfg_path = self.write_config(os.path.join(self.work_dir,
  150. 'ooinstall.conf'), SAMPLE_CONFIG)
  151. ooconfig = OOConfig(cfg_path)
  152. self.assertEquals(3, len(ooconfig.hosts))
  153. self.assertEquals("master-private.example.com", ooconfig.hosts[0].connect_to)
  154. self.assertEquals("10.0.0.1", ooconfig.hosts[0].ip)
  155. self.assertEquals("master-private.example.com", ooconfig.hosts[0].hostname)
  156. self.assertEquals(["10.0.0.1", "10.0.0.2", "10.0.0.3"],
  157. [host['ip'] for host in ooconfig.settings['hosts']])
  158. self.assertEquals('openshift-enterprise', ooconfig.settings['variant'])
  159. self.assertEquals('v1', ooconfig.settings['version'])
  160. def test_load_bad_config(self):
  161. cfg_path = self.write_config(os.path.join(self.work_dir,
  162. 'ooinstall.conf'), CONFIG_BAD)
  163. try:
  164. OOConfig(cfg_path)
  165. assert False
  166. except OOConfigInvalidHostError:
  167. assert True
  168. def test_load_complete_facts(self):
  169. cfg_path = self.write_config(os.path.join(self.work_dir,
  170. 'ooinstall.conf'), SAMPLE_CONFIG)
  171. ooconfig = OOConfig(cfg_path)
  172. missing_host_facts = ooconfig.calc_missing_facts()
  173. self.assertEquals(0, len(missing_host_facts))
  174. # Test missing optional facts the user must confirm:
  175. def test_load_host_incomplete_facts(self):
  176. cfg_path = self.write_config(os.path.join(self.work_dir,
  177. 'ooinstall.conf'), CONFIG_INCOMPLETE_FACTS)
  178. ooconfig = OOConfig(cfg_path)
  179. missing_host_facts = ooconfig.calc_missing_facts()
  180. self.assertEquals(2, len(missing_host_facts))
  181. self.assertEquals(1, len(missing_host_facts['10.0.0.2']))
  182. self.assertEquals(3, len(missing_host_facts['10.0.0.3']))
  183. def test_write_config(self):
  184. cfg_path = self.write_config(os.path.join(self.work_dir,
  185. 'ooinstall.conf'), SAMPLE_CONFIG)
  186. ooconfig = OOConfig(cfg_path)
  187. ooconfig.save_to_disk()
  188. f = open(cfg_path, 'r')
  189. written_config = yaml.safe_load(f.read())
  190. f.close()
  191. self.assertEquals(3, len(written_config['hosts']))
  192. for h in written_config['hosts']:
  193. self.assertTrue('ip' in h)
  194. self.assertTrue('public_ip' in h)
  195. self.assertTrue('hostname' in h)
  196. self.assertTrue('public_hostname' in h)
  197. self.assertTrue('ansible_ssh_user' in written_config)
  198. self.assertTrue('variant' in written_config)
  199. self.assertEquals('v1', written_config['version'])
  200. # Some advanced settings should not get written out if they
  201. # were not specified by the user:
  202. self.assertFalse('ansible_inventory_directory' in written_config)
  203. class HostTests(OOInstallFixture):
  204. def test_load_host_no_ip_or_hostname(self):
  205. yaml_props = {
  206. 'public_ip': '192.168.0.1',
  207. 'public_hostname': 'a.example.com',
  208. 'master': True
  209. }
  210. self.assertRaises(OOConfigInvalidHostError, Host, **yaml_props)
  211. def test_load_host_no_master_or_node_specified(self):
  212. yaml_props = {
  213. 'ip': '192.168.0.1',
  214. 'hostname': 'a.example.com',
  215. 'public_ip': '192.168.0.1',
  216. 'public_hostname': 'a.example.com',
  217. }
  218. self.assertRaises(OOConfigInvalidHostError, Host, **yaml_props)