openshift_ansible_tests.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import os
  2. import unittest
  3. import tempfile
  4. import shutil
  5. from six.moves import configparser
  6. from ooinstall import openshift_ansible
  7. from ooinstall.oo_config import Host, OOConfig
  8. BASE_CONFIG = """
  9. ---
  10. variant: openshift-enterprise
  11. variant_version: 3.3
  12. version: v2
  13. deployment:
  14. ansible_ssh_user: cloud-user
  15. hosts: []
  16. roles:
  17. master:
  18. node:
  19. """
  20. class TestOpenShiftAnsible(unittest.TestCase):
  21. def setUp(self):
  22. self.tempfiles = []
  23. self.work_dir = tempfile.mkdtemp(prefix='openshift_ansible_tests')
  24. self.configfile = os.path.join(self.work_dir, 'ooinstall.config')
  25. with open(self.configfile, 'w') as config_file:
  26. config_file.write(BASE_CONFIG)
  27. self.inventory = os.path.join(self.work_dir, 'hosts')
  28. config = OOConfig(self.configfile)
  29. config.settings['ansible_inventory_path'] = self.inventory
  30. openshift_ansible.set_config(config)
  31. def tearDown(self):
  32. shutil.rmtree(self.work_dir)
  33. def test_generate_inventory_new_nodes(self):
  34. hosts = generate_hosts(1, 'master', roles=(['master', 'etcd']))
  35. hosts.extend(generate_hosts(1, 'node', roles=['node']))
  36. hosts.extend(generate_hosts(1, 'new_node', roles=['node'], new_host=True))
  37. openshift_ansible.generate_inventory(hosts)
  38. inventory = configparser.ConfigParser(allow_no_value=True)
  39. inventory.read(self.inventory)
  40. self.assertTrue(inventory.has_section('new_nodes'))
  41. self.assertTrue(inventory.has_option('new_nodes', 'new_node1'))
  42. def test_write_inventory_vars_role_vars(self):
  43. with open(self.inventory, 'w') as inv:
  44. openshift_ansible.CFG.deployment.roles['master'].variables = {'color': 'blue'}
  45. openshift_ansible.CFG.deployment.roles['node'].variables = {'color': 'green'}
  46. openshift_ansible.write_inventory_vars(inv, None)
  47. inventory = configparser.ConfigParser(allow_no_value=True)
  48. inventory.read(self.inventory)
  49. self.assertTrue(inventory.has_section('masters:vars'))
  50. self.assertEquals('blue', inventory.get('masters:vars', 'color'))
  51. self.assertTrue(inventory.has_section('nodes:vars'))
  52. self.assertEquals('green', inventory.get('nodes:vars', 'color'))
  53. def generate_hosts(num_hosts, name_prefix, roles=None, new_host=False):
  54. hosts = []
  55. for num in range(1, num_hosts + 1):
  56. hosts.append(Host(connect_to=name_prefix + str(num),
  57. roles=roles, new_host=new_host))
  58. return hosts