oo_config_tests.py 7.5 KB

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