test_upgrade.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python3
  2. # encoding: utf-8
  3. import unittest
  4. import inspect
  5. from lib import cluster
  6. from lib.ocboot import \
  7. GROUP_PRIMARY_MASTER_NODE, GROUP_MASTER_NODES, GROUP_WORKER_NODES
  8. class fakeNode(object):
  9. def __init__(self, hostname, ip):
  10. self.hostname = hostname
  11. self.ip = ip
  12. def get_hostname(self):
  13. return self.hostname
  14. def get_ip(self):
  15. return self.ip
  16. class TestAnsibleInventory(unittest.TestCase):
  17. def new_primary_master_node(self, hostname, ip, port=22):
  18. return cluster.AnsiblePrimaryMasterHost(fakeNode(hostname, ip), port=port)
  19. def new_master_node(self, hostname, ip, port=22):
  20. return cluster.AnsibleMasterHost(fakeNode(hostname, ip), port=port)
  21. def new_worker_node(self, hostname, ip, port=22):
  22. return cluster.AnsibleWorkerHost(fakeNode(hostname, ip), port=port)
  23. def test_generate_content(self):
  24. p_m_host = self.new_primary_master_node('p1', '192.168.0.1', port=34)
  25. m_host1 = self.new_master_node('m1', '192.168.0.2', port=34)
  26. m_host2 = self.new_master_node('m2', '192.168.0.3', port=34)
  27. w_host1 = self.new_worker_node('w1', '192.168.0.4', port=36)
  28. w_host2 = self.new_worker_node('w2', '192.168.0.5', port=36)
  29. inventory = cluster.AnsibleInventory()
  30. inventory.add(p_m_host, m_host1, m_host2, w_host1, w_host2)
  31. content = inventory.generate_content()
  32. self.maxDiff = None
  33. self.assertEqual(content, inspect.cleandoc("""[all]
  34. p1 ansible_ssh_host=192.168.0.1 ansible_host=192.168.0.1 ansible_ssh_user=root ansible_user=root ansible_ssh_port=34 ansible_port=34
  35. m1 ansible_ssh_host=192.168.0.2 ansible_host=192.168.0.2 ansible_ssh_user=root ansible_user=root ansible_ssh_port=34 ansible_port=34
  36. m2 ansible_ssh_host=192.168.0.3 ansible_host=192.168.0.3 ansible_ssh_user=root ansible_user=root ansible_ssh_port=34 ansible_port=34
  37. w1 ansible_ssh_host=192.168.0.4 ansible_host=192.168.0.4 ansible_ssh_user=root ansible_user=root ansible_ssh_port=36 ansible_port=36
  38. w2 ansible_ssh_host=192.168.0.5 ansible_host=192.168.0.5 ansible_ssh_user=root ansible_user=root ansible_ssh_port=36 ansible_port=36
  39. [primary_master_node]
  40. p1
  41. [master_nodes]
  42. m1
  43. m2
  44. [worker_nodes]
  45. w1
  46. w2"""))