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.

346 lines
13 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 inventory
  15. import mock
  16. import unittest
  17. from collections import OrderedDict
  18. import sys
  19. path = "./contrib/inventory_builder/"
  20. if path not in sys.path:
  21. sys.path.append(path)
  22. import inventory # noqa
  23. class TestInventory(unittest.TestCase):
  24. @mock.patch('inventory.sys')
  25. def setUp(self, sys_mock):
  26. sys_mock.exit = mock.Mock()
  27. super(TestInventory, self).setUp()
  28. self.data = ['10.90.3.2', '10.90.3.3', '10.90.3.4']
  29. self.inv = inventory.KubesprayInventory()
  30. def test_get_ip_from_opts(self):
  31. optstring = {'ansible_host': '10.90.3.2',
  32. 'ip': '10.90.3.2',
  33. 'access_ip': '10.90.3.2'}
  34. expected = "10.90.3.2"
  35. result = self.inv.get_ip_from_opts(optstring)
  36. self.assertEqual(expected, result)
  37. def test_get_ip_from_opts_invalid(self):
  38. optstring = "notanaddr=value something random!chars:D"
  39. self.assertRaisesRegex(ValueError, "IP parameter not found",
  40. self.inv.get_ip_from_opts, optstring)
  41. def test_ensure_required_groups(self):
  42. groups = ['group1', 'group2']
  43. self.inv.ensure_required_groups(groups)
  44. for group in groups:
  45. self.assertTrue(group in self.inv.yaml_config['all']['children'])
  46. def test_get_host_id(self):
  47. hostnames = ['node99', 'no99de01', '01node01', 'node1.domain',
  48. 'node3.xyz123.aaa']
  49. expected = [99, 1, 1, 1, 3]
  50. for hostname, expected in zip(hostnames, expected):
  51. result = self.inv.get_host_id(hostname)
  52. self.assertEqual(expected, result)
  53. def test_get_host_id_invalid(self):
  54. bad_hostnames = ['node', 'no99de', '01node', 'node.111111']
  55. for hostname in bad_hostnames:
  56. self.assertRaisesRegex(ValueError, "Host name must end in an",
  57. self.inv.get_host_id, hostname)
  58. def test_build_hostnames_add_one(self):
  59. changed_hosts = ['10.90.0.2']
  60. expected = OrderedDict([('node1',
  61. {'ansible_host': '10.90.0.2',
  62. 'ip': '10.90.0.2',
  63. 'access_ip': '10.90.0.2'})])
  64. result = self.inv.build_hostnames(changed_hosts)
  65. self.assertEqual(expected, result)
  66. def test_build_hostnames_add_duplicate(self):
  67. changed_hosts = ['10.90.0.2']
  68. expected = OrderedDict([('node1',
  69. {'ansible_host': '10.90.0.2',
  70. 'ip': '10.90.0.2',
  71. 'access_ip': '10.90.0.2'})])
  72. self.inv.yaml_config['all']['hosts'] = expected
  73. result = self.inv.build_hostnames(changed_hosts)
  74. self.assertEqual(expected, result)
  75. def test_build_hostnames_add_two(self):
  76. changed_hosts = ['10.90.0.2', '10.90.0.3']
  77. expected = OrderedDict([
  78. ('node1', {'ansible_host': '10.90.0.2',
  79. 'ip': '10.90.0.2',
  80. 'access_ip': '10.90.0.2'}),
  81. ('node2', {'ansible_host': '10.90.0.3',
  82. 'ip': '10.90.0.3',
  83. 'access_ip': '10.90.0.3'})])
  84. self.inv.yaml_config['all']['hosts'] = OrderedDict()
  85. result = self.inv.build_hostnames(changed_hosts)
  86. self.assertEqual(expected, result)
  87. def test_build_hostnames_delete_first(self):
  88. changed_hosts = ['-10.90.0.2']
  89. existing_hosts = OrderedDict([
  90. ('node1', {'ansible_host': '10.90.0.2',
  91. 'ip': '10.90.0.2',
  92. 'access_ip': '10.90.0.2'}),
  93. ('node2', {'ansible_host': '10.90.0.3',
  94. 'ip': '10.90.0.3',
  95. 'access_ip': '10.90.0.3'})])
  96. self.inv.yaml_config['all']['hosts'] = existing_hosts
  97. expected = OrderedDict([
  98. ('node2', {'ansible_host': '10.90.0.3',
  99. 'ip': '10.90.0.3',
  100. 'access_ip': '10.90.0.3'})])
  101. result = self.inv.build_hostnames(changed_hosts)
  102. self.assertEqual(expected, result)
  103. def test_exists_hostname_positive(self):
  104. hostname = 'node1'
  105. expected = True
  106. existing_hosts = OrderedDict([
  107. ('node1', {'ansible_host': '10.90.0.2',
  108. 'ip': '10.90.0.2',
  109. 'access_ip': '10.90.0.2'}),
  110. ('node2', {'ansible_host': '10.90.0.3',
  111. 'ip': '10.90.0.3',
  112. 'access_ip': '10.90.0.3'})])
  113. result = self.inv.exists_hostname(existing_hosts, hostname)
  114. self.assertEqual(expected, result)
  115. def test_exists_hostname_negative(self):
  116. hostname = 'node99'
  117. expected = False
  118. existing_hosts = OrderedDict([
  119. ('node1', {'ansible_host': '10.90.0.2',
  120. 'ip': '10.90.0.2',
  121. 'access_ip': '10.90.0.2'}),
  122. ('node2', {'ansible_host': '10.90.0.3',
  123. 'ip': '10.90.0.3',
  124. 'access_ip': '10.90.0.3'})])
  125. result = self.inv.exists_hostname(existing_hosts, hostname)
  126. self.assertEqual(expected, result)
  127. def test_exists_ip_positive(self):
  128. ip = '10.90.0.2'
  129. expected = True
  130. existing_hosts = OrderedDict([
  131. ('node1', {'ansible_host': '10.90.0.2',
  132. 'ip': '10.90.0.2',
  133. 'access_ip': '10.90.0.2'}),
  134. ('node2', {'ansible_host': '10.90.0.3',
  135. 'ip': '10.90.0.3',
  136. 'access_ip': '10.90.0.3'})])
  137. result = self.inv.exists_ip(existing_hosts, ip)
  138. self.assertEqual(expected, result)
  139. def test_exists_ip_negative(self):
  140. ip = '10.90.0.200'
  141. expected = False
  142. existing_hosts = OrderedDict([
  143. ('node1', {'ansible_host': '10.90.0.2',
  144. 'ip': '10.90.0.2',
  145. 'access_ip': '10.90.0.2'}),
  146. ('node2', {'ansible_host': '10.90.0.3',
  147. 'ip': '10.90.0.3',
  148. 'access_ip': '10.90.0.3'})])
  149. result = self.inv.exists_ip(existing_hosts, ip)
  150. self.assertEqual(expected, result)
  151. def test_delete_host_by_ip_positive(self):
  152. ip = '10.90.0.2'
  153. expected = OrderedDict([
  154. ('node2', {'ansible_host': '10.90.0.3',
  155. 'ip': '10.90.0.3',
  156. 'access_ip': '10.90.0.3'})])
  157. existing_hosts = OrderedDict([
  158. ('node1', {'ansible_host': '10.90.0.2',
  159. 'ip': '10.90.0.2',
  160. 'access_ip': '10.90.0.2'}),
  161. ('node2', {'ansible_host': '10.90.0.3',
  162. 'ip': '10.90.0.3',
  163. 'access_ip': '10.90.0.3'})])
  164. self.inv.delete_host_by_ip(existing_hosts, ip)
  165. self.assertEqual(expected, existing_hosts)
  166. def test_delete_host_by_ip_negative(self):
  167. ip = '10.90.0.200'
  168. existing_hosts = OrderedDict([
  169. ('node1', {'ansible_host': '10.90.0.2',
  170. 'ip': '10.90.0.2',
  171. 'access_ip': '10.90.0.2'}),
  172. ('node2', {'ansible_host': '10.90.0.3',
  173. 'ip': '10.90.0.3',
  174. 'access_ip': '10.90.0.3'})])
  175. self.assertRaisesRegex(ValueError, "Unable to find host",
  176. self.inv.delete_host_by_ip, existing_hosts, ip)
  177. def test_purge_invalid_hosts(self):
  178. proper_hostnames = ['node1', 'node2']
  179. bad_host = 'doesnotbelong2'
  180. existing_hosts = OrderedDict([
  181. ('node1', {'ansible_host': '10.90.0.2',
  182. 'ip': '10.90.0.2',
  183. 'access_ip': '10.90.0.2'}),
  184. ('node2', {'ansible_host': '10.90.0.3',
  185. 'ip': '10.90.0.3',
  186. 'access_ip': '10.90.0.3'}),
  187. ('doesnotbelong2', {'whateveropts=ilike'})])
  188. self.inv.yaml_config['all']['hosts'] = existing_hosts
  189. self.inv.purge_invalid_hosts(proper_hostnames)
  190. self.assertTrue(
  191. bad_host not in self.inv.yaml_config['all']['hosts'].keys())
  192. def test_add_host_to_group(self):
  193. group = 'etcd'
  194. host = 'node1'
  195. opts = {'ip': '10.90.0.2'}
  196. self.inv.add_host_to_group(group, host, opts)
  197. self.assertEqual(
  198. self.inv.yaml_config['all']['children'][group]['hosts'].get(host),
  199. None)
  200. def test_set_kube_master(self):
  201. group = 'kube-master'
  202. host = 'node1'
  203. self.inv.set_kube_master([host])
  204. self.assertTrue(
  205. host in self.inv.yaml_config['all']['children'][group]['hosts'])
  206. def test_set_all(self):
  207. hosts = OrderedDict([
  208. ('node1', 'opt1'),
  209. ('node2', 'opt2')])
  210. self.inv.set_all(hosts)
  211. for host, opt in hosts.items():
  212. self.assertEqual(
  213. self.inv.yaml_config['all']['hosts'].get(host), opt)
  214. def test_set_k8s_cluster(self):
  215. group = 'k8s-cluster'
  216. expected_hosts = ['kube-node', 'kube-master']
  217. self.inv.set_k8s_cluster()
  218. for host in expected_hosts:
  219. self.assertTrue(
  220. host in
  221. self.inv.yaml_config['all']['children'][group]['children'])
  222. def test_set_kube_node(self):
  223. group = 'kube-node'
  224. host = 'node1'
  225. self.inv.set_kube_node([host])
  226. self.assertTrue(
  227. host in self.inv.yaml_config['all']['children'][group]['hosts'])
  228. def test_set_etcd(self):
  229. group = 'etcd'
  230. host = 'node1'
  231. self.inv.set_etcd([host])
  232. self.assertTrue(
  233. host in self.inv.yaml_config['all']['children'][group]['hosts'])
  234. def test_scale_scenario_one(self):
  235. num_nodes = 50
  236. hosts = OrderedDict()
  237. for hostid in range(1, num_nodes+1):
  238. hosts["node" + str(hostid)] = ""
  239. self.inv.set_all(hosts)
  240. self.inv.set_etcd(list(hosts.keys())[0:3])
  241. self.inv.set_kube_master(list(hosts.keys())[0:2])
  242. self.inv.set_kube_node(hosts.keys())
  243. for h in range(3):
  244. self.assertFalse(
  245. list(hosts.keys())[h] in
  246. self.inv.yaml_config['all']['children']['kube-node']['hosts'])
  247. def test_scale_scenario_two(self):
  248. num_nodes = 500
  249. hosts = OrderedDict()
  250. for hostid in range(1, num_nodes+1):
  251. hosts["node" + str(hostid)] = ""
  252. self.inv.set_all(hosts)
  253. self.inv.set_etcd(list(hosts.keys())[0:3])
  254. self.inv.set_kube_master(list(hosts.keys())[3:5])
  255. self.inv.set_kube_node(hosts.keys())
  256. for h in range(5):
  257. self.assertFalse(
  258. list(hosts.keys())[h] in
  259. self.inv.yaml_config['all']['children']['kube-node']['hosts'])
  260. def test_range2ips_range(self):
  261. changed_hosts = ['10.90.0.2', '10.90.0.4-10.90.0.6', '10.90.0.8']
  262. expected = ['10.90.0.2',
  263. '10.90.0.4',
  264. '10.90.0.5',
  265. '10.90.0.6',
  266. '10.90.0.8']
  267. result = self.inv.range2ips(changed_hosts)
  268. self.assertEqual(expected, result)
  269. def test_range2ips_incorrect_range(self):
  270. host_range = ['10.90.0.4-a.9b.c.e']
  271. self.assertRaisesRegex(Exception, "Range of ip_addresses isn't valid",
  272. self.inv.range2ips, host_range)
  273. def test_build_hostnames_different_ips_add_one(self):
  274. changed_hosts = ['10.90.0.2,192.168.0.2']
  275. expected = OrderedDict([('node1',
  276. {'ansible_host': '192.168.0.2',
  277. 'ip': '10.90.0.2',
  278. 'access_ip': '192.168.0.2'})])
  279. result = self.inv.build_hostnames(changed_hosts)
  280. self.assertEqual(expected, result)
  281. def test_build_hostnames_different_ips_add_duplicate(self):
  282. changed_hosts = ['10.90.0.2,192.168.0.2']
  283. expected = OrderedDict([('node1',
  284. {'ansible_host': '192.168.0.2',
  285. 'ip': '10.90.0.2',
  286. 'access_ip': '192.168.0.2'})])
  287. self.inv.yaml_config['all']['hosts'] = expected
  288. result = self.inv.build_hostnames(changed_hosts)
  289. self.assertEqual(expected, result)
  290. def test_build_hostnames_different_ips_add_two(self):
  291. changed_hosts = ['10.90.0.2,192.168.0.2', '10.90.0.3,192.168.0.3']
  292. expected = OrderedDict([
  293. ('node1', {'ansible_host': '192.168.0.2',
  294. 'ip': '10.90.0.2',
  295. 'access_ip': '192.168.0.2'}),
  296. ('node2', {'ansible_host': '192.168.0.3',
  297. 'ip': '10.90.0.3',
  298. 'access_ip': '192.168.0.3'})])
  299. self.inv.yaml_config['all']['hosts'] = OrderedDict()
  300. result = self.inv.build_hostnames(changed_hosts)
  301. self.assertEqual(expected, result)