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.

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