You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
8.8 KiB

  1. # Copyright 2016 Mirantis, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. # not use this file except in compliance with the License. You may obtain
  5. # a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations
  13. # under the License.
  14. import mock
  15. import unittest
  16. from collections import OrderedDict
  17. import sys
  18. path = "./contrib/inventory_builder/"
  19. if path not in sys.path:
  20. sys.path.append(path)
  21. import inventory
  22. class TestInventory(unittest.TestCase):
  23. @mock.patch('inventory.sys')
  24. def setUp(self, sys_mock):
  25. sys_mock.exit = mock.Mock()
  26. super(TestInventory, self).setUp()
  27. self.data = ['10.90.3.2', '10.90.3.3', '10.90.3.4']
  28. self.inv = inventory.KargoInventory()
  29. def test_get_ip_from_opts(self):
  30. optstring = "ansible_host=10.90.3.2 ip=10.90.3.2"
  31. expected = "10.90.3.2"
  32. result = self.inv.get_ip_from_opts(optstring)
  33. self.assertEqual(expected, result)
  34. def test_get_ip_from_opts_invalid(self):
  35. optstring = "notanaddr=value something random!chars:D"
  36. self.assertRaisesRegexp(ValueError, "IP parameter not found",
  37. self.inv.get_ip_from_opts, optstring)
  38. def test_ensure_required_groups(self):
  39. groups = ['group1', 'group2']
  40. self.inv.ensure_required_groups(groups)
  41. for group in groups:
  42. self.assertTrue(group in self.inv.config.sections())
  43. def test_get_host_id(self):
  44. hostnames = ['node99', 'no99de01', '01node01', 'node1.domain',
  45. 'node3.xyz123.aaa']
  46. expected = [99, 1, 1, 1, 3]
  47. for hostname, expected in zip(hostnames, expected):
  48. result = self.inv.get_host_id(hostname)
  49. self.assertEqual(expected, result)
  50. def test_get_host_id_invalid(self):
  51. bad_hostnames = ['node', 'no99de', '01node', 'node.111111']
  52. for hostname in bad_hostnames:
  53. self.assertRaisesRegexp(ValueError, "Host name must end in an",
  54. self.inv.get_host_id, hostname)
  55. def test_build_hostnames_add_one(self):
  56. changed_hosts = ['10.90.0.2']
  57. expected = OrderedDict([('node1',
  58. 'ansible_host=10.90.0.2 ip=10.90.0.2')])
  59. result = self.inv.build_hostnames(changed_hosts)
  60. self.assertEqual(expected, result)
  61. def test_build_hostnames_add_duplicate(self):
  62. changed_hosts = ['10.90.0.2']
  63. expected = OrderedDict([('node1',
  64. 'ansible_host=10.90.0.2 ip=10.90.0.2')])
  65. self.inv.config['all'] = expected
  66. result = self.inv.build_hostnames(changed_hosts)
  67. self.assertEqual(expected, result)
  68. def test_build_hostnames_add_two(self):
  69. changed_hosts = ['10.90.0.2', '10.90.0.3']
  70. expected = OrderedDict([
  71. ('node1', 'ansible_host=10.90.0.2 ip=10.90.0.2'),
  72. ('node2', 'ansible_host=10.90.0.3 ip=10.90.0.3')])
  73. self.inv.config['all'] = OrderedDict()
  74. result = self.inv.build_hostnames(changed_hosts)
  75. self.assertEqual(expected, result)
  76. def test_build_hostnames_delete_first(self):
  77. changed_hosts = ['-10.90.0.2']
  78. existing_hosts = OrderedDict([
  79. ('node1', 'ansible_host=10.90.0.2 ip=10.90.0.2'),
  80. ('node2', 'ansible_host=10.90.0.3 ip=10.90.0.3')])
  81. self.inv.config['all'] = existing_hosts
  82. expected = OrderedDict([
  83. ('node2', 'ansible_host=10.90.0.3 ip=10.90.0.3')])
  84. result = self.inv.build_hostnames(changed_hosts)
  85. self.assertEqual(expected, result)
  86. def test_exists_hostname_positive(self):
  87. hostname = 'node1'
  88. expected = True
  89. existing_hosts = OrderedDict([
  90. ('node1', 'ansible_host=10.90.0.2 ip=10.90.0.2'),
  91. ('node2', 'ansible_host=10.90.0.3 ip=10.90.0.3')])
  92. result = self.inv.exists_hostname(existing_hosts, hostname)
  93. self.assertEqual(expected, result)
  94. def test_exists_hostname_negative(self):
  95. hostname = 'node99'
  96. expected = False
  97. existing_hosts = OrderedDict([
  98. ('node1', 'ansible_host=10.90.0.2 ip=10.90.0.2'),
  99. ('node2', 'ansible_host=10.90.0.3 ip=10.90.0.3')])
  100. result = self.inv.exists_hostname(existing_hosts, hostname)
  101. self.assertEqual(expected, result)
  102. def test_exists_ip_positive(self):
  103. ip = '10.90.0.2'
  104. expected = True
  105. existing_hosts = OrderedDict([
  106. ('node1', 'ansible_host=10.90.0.2 ip=10.90.0.2'),
  107. ('node2', 'ansible_host=10.90.0.3 ip=10.90.0.3')])
  108. result = self.inv.exists_ip(existing_hosts, ip)
  109. self.assertEqual(expected, result)
  110. def test_exists_ip_negative(self):
  111. ip = '10.90.0.200'
  112. expected = False
  113. existing_hosts = OrderedDict([
  114. ('node1', 'ansible_host=10.90.0.2 ip=10.90.0.2'),
  115. ('node2', 'ansible_host=10.90.0.3 ip=10.90.0.3')])
  116. result = self.inv.exists_ip(existing_hosts, ip)
  117. self.assertEqual(expected, result)
  118. def test_delete_host_by_ip_positive(self):
  119. ip = '10.90.0.2'
  120. expected = OrderedDict([
  121. ('node2', 'ansible_host=10.90.0.3 ip=10.90.0.3')])
  122. existing_hosts = OrderedDict([
  123. ('node1', 'ansible_host=10.90.0.2 ip=10.90.0.2'),
  124. ('node2', 'ansible_host=10.90.0.3 ip=10.90.0.3')])
  125. self.inv.delete_host_by_ip(existing_hosts, ip)
  126. self.assertEqual(expected, existing_hosts)
  127. def test_delete_host_by_ip_negative(self):
  128. ip = '10.90.0.200'
  129. existing_hosts = OrderedDict([
  130. ('node1', 'ansible_host=10.90.0.2 ip=10.90.0.2'),
  131. ('node2', 'ansible_host=10.90.0.3 ip=10.90.0.3')])
  132. self.assertRaisesRegexp(ValueError, "Unable to find host",
  133. self.inv.delete_host_by_ip, existing_hosts, ip)
  134. def test_purge_invalid_hosts(self):
  135. proper_hostnames = ['node1', 'node2']
  136. bad_host = 'doesnotbelong2'
  137. existing_hosts = OrderedDict([
  138. ('node1', 'ansible_host=10.90.0.2 ip=10.90.0.2'),
  139. ('node2', 'ansible_host=10.90.0.3 ip=10.90.0.3'),
  140. ('doesnotbelong2', 'whateveropts=ilike')])
  141. self.inv.config['all'] = existing_hosts
  142. self.inv.purge_invalid_hosts(proper_hostnames)
  143. self.assertTrue(bad_host not in self.inv.config['all'].keys())
  144. def test_add_host_to_group(self):
  145. group = 'etcd'
  146. host = 'node1'
  147. opts = 'ip=10.90.0.2'
  148. self.inv.add_host_to_group(group, host, opts)
  149. self.assertEqual(self.inv.config[group].get(host), opts)
  150. def test_set_kube_master(self):
  151. group = 'kube-master'
  152. host = 'node1'
  153. self.inv.set_kube_master([host])
  154. self.assertTrue(host in self.inv.config[group])
  155. def test_set_all(self):
  156. group = 'all'
  157. hosts = OrderedDict([
  158. ('node1', 'opt1'),
  159. ('node2', 'opt2')])
  160. self.inv.set_all(hosts)
  161. for host, opt in hosts.items():
  162. self.assertEqual(self.inv.config[group].get(host), opt)
  163. def test_set_k8s_cluster(self):
  164. group = 'k8s-cluster:children'
  165. expected_hosts = ['kube-node', 'kube-master']
  166. self.inv.set_k8s_cluster()
  167. for host in expected_hosts:
  168. self.assertTrue(host in self.inv.config[group])
  169. def test_set_kube_node(self):
  170. group = 'kube-node'
  171. host = 'node1'
  172. self.inv.set_kube_node([host])
  173. self.assertTrue(host in self.inv.config[group])
  174. def test_set_etcd(self):
  175. group = 'etcd'
  176. host = 'node1'
  177. self.inv.set_etcd([host])
  178. self.assertTrue(host in self.inv.config[group])
  179. def test_scale_scenario_one(self):
  180. num_nodes = 50
  181. hosts = OrderedDict()
  182. for hostid in range(1, num_nodes+1):
  183. hosts["node" + str(hostid)] = ""
  184. self.inv.set_all(hosts)
  185. self.inv.set_etcd(hosts.keys()[0:3])
  186. self.inv.set_kube_master(hosts.keys()[0:2])
  187. self.inv.set_kube_node(hosts.keys())
  188. for h in range(3):
  189. self.assertFalse(hosts.keys()[h] in self.inv.config['kube-node'])
  190. def test_scale_scenario_two(self):
  191. num_nodes = 500
  192. hosts = OrderedDict()
  193. for hostid in range(1, num_nodes+1):
  194. hosts["node" + str(hostid)] = ""
  195. self.inv.set_all(hosts)
  196. self.inv.set_etcd(hosts.keys()[0:3])
  197. self.inv.set_kube_master(hosts.keys()[3:5])
  198. self.inv.set_kube_node(hosts.keys())
  199. for h in range(5):
  200. self.assertFalse(hosts.keys()[h] in self.inv.config['kube-node'])