oo_config_tests.py 6.7 KB

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