oo_config_tests.py 7.6 KB

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