cli_installer_tests.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. # TODO: Temporarily disabled due to importing old code into openshift-ansible
  2. # repo. We will work on these over time.
  3. # pylint: disable=bad-continuation,missing-docstring,no-self-use,invalid-name
  4. import copy
  5. import os
  6. import ConfigParser
  7. import yaml
  8. import ooinstall.cli_installer as cli
  9. from click.testing import CliRunner
  10. from test.oo_config_tests import OOInstallFixture
  11. from mock import patch
  12. MOCK_FACTS = {
  13. '10.0.0.1': {
  14. 'common': {
  15. 'ip': '10.0.0.1',
  16. 'public_ip': '10.0.0.1',
  17. 'hostname': 'master-private.example.com',
  18. 'public_hostname': 'master.example.com'
  19. }
  20. },
  21. '10.0.0.2': {
  22. 'common': {
  23. 'ip': '10.0.0.2',
  24. 'public_ip': '10.0.0.2',
  25. 'hostname': 'node1-private.example.com',
  26. 'public_hostname': 'node1.example.com'
  27. }
  28. },
  29. '10.0.0.3': {
  30. 'common': {
  31. 'ip': '10.0.0.3',
  32. 'public_ip': '10.0.0.3',
  33. 'hostname': 'node2-private.example.com',
  34. 'public_hostname': 'node2.example.com'
  35. }
  36. },
  37. }
  38. MOCK_FACTS_QUICKHA = {
  39. '10.0.0.1': {
  40. 'common': {
  41. 'ip': '10.0.0.1',
  42. 'public_ip': '10.0.0.1',
  43. 'hostname': 'master-private.example.com',
  44. 'public_hostname': 'master.example.com'
  45. }
  46. },
  47. '10.0.0.2': {
  48. 'common': {
  49. 'ip': '10.0.0.2',
  50. 'public_ip': '10.0.0.2',
  51. 'hostname': 'node1-private.example.com',
  52. 'public_hostname': 'node1.example.com'
  53. }
  54. },
  55. '10.0.0.3': {
  56. 'common': {
  57. 'ip': '10.0.0.3',
  58. 'public_ip': '10.0.0.3',
  59. 'hostname': 'node2-private.example.com',
  60. 'public_hostname': 'node2.example.com'
  61. }
  62. },
  63. '10.0.0.4': {
  64. 'common': {
  65. 'ip': '10.0.0.4',
  66. 'public_ip': '10.0.0.4',
  67. 'hostname': 'proxy-private.example.com',
  68. 'public_hostname': 'proxy.example.com'
  69. }
  70. },
  71. }
  72. # Substitute in a product name before use:
  73. SAMPLE_CONFIG = """
  74. variant: %s
  75. ansible_ssh_user: root
  76. hosts:
  77. - connect_to: 10.0.0.1
  78. ip: 10.0.0.1
  79. hostname: master-private.example.com
  80. public_ip: 24.222.0.1
  81. public_hostname: master.example.com
  82. master: true
  83. node: true
  84. - connect_to: 10.0.0.2
  85. ip: 10.0.0.2
  86. hostname: node1-private.example.com
  87. public_ip: 24.222.0.2
  88. public_hostname: node1.example.com
  89. node: true
  90. - connect_to: 10.0.0.3
  91. ip: 10.0.0.3
  92. hostname: node2-private.example.com
  93. public_ip: 24.222.0.3
  94. public_hostname: node2.example.com
  95. node: true
  96. """
  97. BAD_CONFIG = """
  98. variant: %s
  99. ansible_ssh_user: root
  100. hosts:
  101. - connect_to: 10.0.0.1
  102. ip: 10.0.0.1
  103. hostname: master-private.example.com
  104. public_ip: 24.222.0.1
  105. public_hostname: master.example.com
  106. master: true
  107. node: true
  108. - ip: 10.0.0.2
  109. hostname: node1-private.example.com
  110. public_ip: 24.222.0.2
  111. public_hostname: node1.example.com
  112. node: true
  113. - connect_to: 10.0.0.3
  114. ip: 10.0.0.3
  115. hostname: node2-private.example.com
  116. public_ip: 24.222.0.3
  117. public_hostname: node2.example.com
  118. node: true
  119. """
  120. QUICKHA_CONFIG = """
  121. variant: %s
  122. ansible_ssh_user: root
  123. hosts:
  124. - connect_to: 10.0.0.1
  125. ip: 10.0.0.1
  126. hostname: master-private.example.com
  127. public_ip: 24.222.0.1
  128. public_hostname: master.example.com
  129. master: true
  130. node: true
  131. - connect_to: 10.0.0.2
  132. ip: 10.0.0.2
  133. hostname: node1-private.example.com
  134. public_ip: 24.222.0.2
  135. public_hostname: node1.example.com
  136. master: true
  137. node: true
  138. - connect_to: 10.0.0.3
  139. ip: 10.0.0.3
  140. hostname: node2-private.example.com
  141. public_ip: 24.222.0.3
  142. public_hostname: node2.example.com
  143. node: true
  144. - connect_to: 10.0.0.4
  145. ip: 10.0.0.4
  146. hostname: proxy-private.example.com
  147. public_ip: 24.222.0.4
  148. public_hostname: proxy.example.com
  149. master_lb: true
  150. """
  151. class OOCliFixture(OOInstallFixture):
  152. def setUp(self):
  153. OOInstallFixture.setUp(self)
  154. self.runner = CliRunner()
  155. # Add any arguments you would like to test here, the defaults ensure
  156. # we only do unattended invocations here, and using temporary files/dirs.
  157. self.cli_args = ["-a", self.work_dir]
  158. def run_cli(self):
  159. return self.runner.invoke(cli.cli, self.cli_args)
  160. def assert_result(self, result, exit_code):
  161. if result.exception is not None or result.exit_code != exit_code:
  162. print "Unexpected result from CLI execution"
  163. print "Exit code: %s" % result.exit_code
  164. print "Exception: %s" % result.exception
  165. print result.exc_info
  166. import traceback
  167. traceback.print_exception(*result.exc_info)
  168. print "Output:\n%s" % result.output
  169. self.fail("Exception during CLI execution")
  170. def _read_yaml(self, config_file_path):
  171. f = open(config_file_path, 'r')
  172. config = yaml.safe_load(f.read())
  173. f.close()
  174. return config
  175. def _verify_load_facts(self, load_facts_mock):
  176. """ Check that we ran load facts with expected inputs. """
  177. load_facts_args = load_facts_mock.call_args[0]
  178. self.assertEquals(os.path.join(self.work_dir, ".ansible/hosts"),
  179. load_facts_args[0])
  180. self.assertEquals(os.path.join(self.work_dir,
  181. "playbooks/byo/openshift_facts.yml"), load_facts_args[1])
  182. env_vars = load_facts_args[2]
  183. self.assertEquals(os.path.join(self.work_dir,
  184. '.ansible/callback_facts.yaml'),
  185. env_vars['OO_INSTALL_CALLBACK_FACTS_YAML'])
  186. self.assertEqual('/tmp/ansible.log', env_vars['ANSIBLE_LOG_PATH'])
  187. def _verify_run_playbook(self, run_playbook_mock, exp_hosts_len, exp_hosts_to_run_on_len):
  188. """ Check that we ran playbook with expected inputs. """
  189. hosts = run_playbook_mock.call_args[0][0]
  190. hosts_to_run_on = run_playbook_mock.call_args[0][1]
  191. self.assertEquals(exp_hosts_len, len(hosts))
  192. self.assertEquals(exp_hosts_to_run_on_len, len(hosts_to_run_on))
  193. def _verify_config_hosts(self, written_config, host_count):
  194. print written_config['hosts']
  195. self.assertEquals(host_count, len(written_config['hosts']))
  196. for h in written_config['hosts']:
  197. self.assertTrue(h['node'])
  198. self.assertTrue('ip' in h)
  199. self.assertTrue('hostname' in h)
  200. self.assertTrue('public_ip' in h)
  201. self.assertTrue('public_hostname' in h)
  202. #pylint: disable=too-many-arguments
  203. def _verify_get_hosts_to_run_on(self, mock_facts, load_facts_mock,
  204. run_playbook_mock, cli_input,
  205. exp_hosts_len=None, exp_hosts_to_run_on_len=None,
  206. force=None):
  207. """
  208. Tests cli_installer.py:get_hosts_to_run_on. That method has quite a
  209. few subtle branches in the logic. The goal with this method is simply
  210. to handle all the messy stuff here and allow the main test cases to be
  211. easily read. The basic idea is to modify mock_facts to return a
  212. version indicating OpenShift is already installed on particular hosts.
  213. """
  214. load_facts_mock.return_value = (mock_facts, 0)
  215. run_playbook_mock.return_value = 0
  216. if cli_input:
  217. self.cli_args.append("install")
  218. result = self.runner.invoke(cli.cli,
  219. self.cli_args,
  220. input=cli_input)
  221. else:
  222. config_file = self.write_config(os.path.join(self.work_dir,
  223. 'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')
  224. self.cli_args.extend(["-c", config_file, "install"])
  225. if force:
  226. self.cli_args.append("--force")
  227. result = self.runner.invoke(cli.cli, self.cli_args)
  228. written_config = self._read_yaml(config_file)
  229. self._verify_config_hosts(written_config, exp_hosts_len)
  230. self.assert_result(result, 0)
  231. self._verify_load_facts(load_facts_mock)
  232. self._verify_run_playbook(run_playbook_mock, exp_hosts_len, exp_hosts_to_run_on_len)
  233. # Make sure we ran on the expected masters and nodes:
  234. hosts = run_playbook_mock.call_args[0][0]
  235. hosts_to_run_on = run_playbook_mock.call_args[0][1]
  236. self.assertEquals(exp_hosts_len, len(hosts))
  237. self.assertEquals(exp_hosts_to_run_on_len, len(hosts_to_run_on))
  238. class UnattendedCliTests(OOCliFixture):
  239. def setUp(self):
  240. OOCliFixture.setUp(self)
  241. self.cli_args.append("-u")
  242. # unattended with config file and all installed hosts (without --force)
  243. @patch('ooinstall.openshift_ansible.run_main_playbook')
  244. @patch('ooinstall.openshift_ansible.load_system_facts')
  245. def test_get_hosts_to_run_on1(self, load_facts_mock, run_playbook_mock):
  246. mock_facts = copy.deepcopy(MOCK_FACTS)
  247. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  248. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  249. mock_facts['10.0.0.3']['common']['version'] = "3.0.0"
  250. load_facts_mock.return_value = (mock_facts, 0)
  251. run_playbook_mock.return_value = 0
  252. config_file = self.write_config(os.path.join(self.work_dir,
  253. 'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')
  254. self.cli_args.extend(["-c", config_file, "install"])
  255. result = self.runner.invoke(cli.cli, self.cli_args)
  256. if result.exception is None or result.exit_code != 1:
  257. print "Exit code: %s" % result.exit_code
  258. self.fail("Unexpected CLI return")
  259. # unattended with config file and all installed hosts (with --force)
  260. @patch('ooinstall.openshift_ansible.run_main_playbook')
  261. @patch('ooinstall.openshift_ansible.load_system_facts')
  262. def test_get_hosts_to_run_on2(self, load_facts_mock, run_playbook_mock):
  263. mock_facts = copy.deepcopy(MOCK_FACTS)
  264. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  265. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  266. mock_facts['10.0.0.3']['common']['version'] = "3.0.0"
  267. self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
  268. cli_input=None,
  269. exp_hosts_len=3,
  270. exp_hosts_to_run_on_len=3,
  271. force=True)
  272. # unattended with config file and no installed hosts (without --force)
  273. @patch('ooinstall.openshift_ansible.run_main_playbook')
  274. @patch('ooinstall.openshift_ansible.load_system_facts')
  275. def test_get_hosts_to_run_on3(self, load_facts_mock, run_playbook_mock):
  276. load_facts_mock.return_value = (MOCK_FACTS, 0)
  277. run_playbook_mock.return_value = 0
  278. self._verify_get_hosts_to_run_on(MOCK_FACTS, load_facts_mock, run_playbook_mock,
  279. cli_input=None,
  280. exp_hosts_len=3,
  281. exp_hosts_to_run_on_len=3,
  282. force=False)
  283. # unattended with config file and no installed hosts (with --force)
  284. @patch('ooinstall.openshift_ansible.run_main_playbook')
  285. @patch('ooinstall.openshift_ansible.load_system_facts')
  286. def test_get_hosts_to_run_on4(self, load_facts_mock, run_playbook_mock):
  287. load_facts_mock.return_value = (MOCK_FACTS, 0)
  288. run_playbook_mock.return_value = 0
  289. self._verify_get_hosts_to_run_on(MOCK_FACTS, load_facts_mock, run_playbook_mock,
  290. cli_input=None,
  291. exp_hosts_len=3,
  292. exp_hosts_to_run_on_len=3,
  293. force=True)
  294. # unattended with config file and some installed some uninstalled hosts (without --force)
  295. @patch('ooinstall.openshift_ansible.run_main_playbook')
  296. @patch('ooinstall.openshift_ansible.load_system_facts')
  297. def test_get_hosts_to_run_on5(self, load_facts_mock, run_playbook_mock):
  298. mock_facts = copy.deepcopy(MOCK_FACTS)
  299. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  300. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  301. self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
  302. cli_input=None,
  303. exp_hosts_len=3,
  304. exp_hosts_to_run_on_len=2,
  305. force=False)
  306. # unattended with config file and some installed some uninstalled hosts (with --force)
  307. @patch('ooinstall.openshift_ansible.run_main_playbook')
  308. @patch('ooinstall.openshift_ansible.load_system_facts')
  309. def test_get_hosts_to_run_on6(self, load_facts_mock, run_playbook_mock):
  310. mock_facts = copy.deepcopy(MOCK_FACTS)
  311. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  312. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  313. self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
  314. cli_input=None,
  315. exp_hosts_len=3,
  316. exp_hosts_to_run_on_len=3,
  317. force=True)
  318. @patch('ooinstall.openshift_ansible.run_main_playbook')
  319. @patch('ooinstall.openshift_ansible.load_system_facts')
  320. def test_cfg_full_run(self, load_facts_mock, run_playbook_mock):
  321. load_facts_mock.return_value = (MOCK_FACTS, 0)
  322. run_playbook_mock.return_value = 0
  323. config_file = self.write_config(os.path.join(self.work_dir,
  324. 'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')
  325. self.cli_args.extend(["-c", config_file, "install"])
  326. result = self.runner.invoke(cli.cli, self.cli_args)
  327. self.assert_result(result, 0)
  328. load_facts_args = load_facts_mock.call_args[0]
  329. self.assertEquals(os.path.join(self.work_dir, ".ansible/hosts"),
  330. load_facts_args[0])
  331. self.assertEquals(os.path.join(self.work_dir,
  332. "playbooks/byo/openshift_facts.yml"), load_facts_args[1])
  333. env_vars = load_facts_args[2]
  334. self.assertEquals(os.path.join(self.work_dir,
  335. '.ansible/callback_facts.yaml'),
  336. env_vars['OO_INSTALL_CALLBACK_FACTS_YAML'])
  337. self.assertEqual('/tmp/ansible.log', env_vars['ANSIBLE_LOG_PATH'])
  338. # If user running test has rpm installed, this might be set to default:
  339. self.assertTrue('ANSIBLE_CONFIG' not in env_vars or
  340. env_vars['ANSIBLE_CONFIG'] == cli.DEFAULT_ANSIBLE_CONFIG)
  341. # Make sure we ran on the expected masters and nodes:
  342. hosts = run_playbook_mock.call_args[0][0]
  343. hosts_to_run_on = run_playbook_mock.call_args[0][1]
  344. self.assertEquals(3, len(hosts))
  345. self.assertEquals(3, len(hosts_to_run_on))
  346. @patch('ooinstall.openshift_ansible.run_main_playbook')
  347. @patch('ooinstall.openshift_ansible.load_system_facts')
  348. def test_inventory_write(self, load_facts_mock, run_playbook_mock):
  349. # Add an ssh user so we can verify it makes it to the inventory file:
  350. merged_config = "%s\n%s" % (SAMPLE_CONFIG % 'openshift-enterprise',
  351. "ansible_ssh_user: bob")
  352. load_facts_mock.return_value = (MOCK_FACTS, 0)
  353. run_playbook_mock.return_value = 0
  354. config_file = self.write_config(os.path.join(self.work_dir,
  355. 'ooinstall.conf'), merged_config)
  356. self.cli_args.extend(["-c", config_file, "install"])
  357. result = self.runner.invoke(cli.cli, self.cli_args)
  358. self.assert_result(result, 0)
  359. # Check the inventory file looks as we would expect:
  360. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  361. inventory.read(os.path.join(self.work_dir, '.ansible/hosts'))
  362. self.assertEquals('bob',
  363. inventory.get('OSEv3:vars', 'ansible_ssh_user'))
  364. self.assertEquals('openshift-enterprise',
  365. inventory.get('OSEv3:vars', 'deployment_type'))
  366. # Check the masters:
  367. self.assertEquals(1, len(inventory.items('masters')))
  368. self.assertEquals(3, len(inventory.items('nodes')))
  369. for item in inventory.items('masters'):
  370. # ansible host lines do NOT parse nicely:
  371. master_line = item[0]
  372. if item[1] is not None:
  373. master_line = "%s=%s" % (master_line, item[1])
  374. self.assertTrue('openshift_ip' in master_line)
  375. self.assertTrue('openshift_public_ip' in master_line)
  376. self.assertTrue('openshift_hostname' in master_line)
  377. self.assertTrue('openshift_public_hostname' in master_line)
  378. @patch('ooinstall.openshift_ansible.run_main_playbook')
  379. @patch('ooinstall.openshift_ansible.load_system_facts')
  380. def test_variant_version_latest_assumed(self, load_facts_mock,
  381. run_playbook_mock):
  382. load_facts_mock.return_value = (MOCK_FACTS, 0)
  383. run_playbook_mock.return_value = 0
  384. config_file = self.write_config(os.path.join(self.work_dir,
  385. 'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')
  386. self.cli_args.extend(["-c", config_file, "install"])
  387. result = self.runner.invoke(cli.cli, self.cli_args)
  388. self.assert_result(result, 0)
  389. written_config = self._read_yaml(config_file)
  390. self.assertEquals('openshift-enterprise', written_config['variant'])
  391. # We didn't specify a version so the latest should have been assumed,
  392. # and written to disk:
  393. self.assertEquals('3.1', written_config['variant_version'])
  394. # Make sure the correct value was passed to ansible:
  395. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  396. inventory.read(os.path.join(self.work_dir, '.ansible/hosts'))
  397. self.assertEquals('openshift-enterprise',
  398. inventory.get('OSEv3:vars', 'deployment_type'))
  399. @patch('ooinstall.openshift_ansible.run_main_playbook')
  400. @patch('ooinstall.openshift_ansible.load_system_facts')
  401. def test_variant_version_preserved(self, load_facts_mock,
  402. run_playbook_mock):
  403. load_facts_mock.return_value = (MOCK_FACTS, 0)
  404. run_playbook_mock.return_value = 0
  405. config = SAMPLE_CONFIG % 'openshift-enterprise'
  406. config = '%s\n%s' % (config, 'variant_version: 3.0')
  407. config_file = self.write_config(os.path.join(self.work_dir,
  408. 'ooinstall.conf'), config)
  409. self.cli_args.extend(["-c", config_file, "install"])
  410. result = self.runner.invoke(cli.cli, self.cli_args)
  411. self.assert_result(result, 0)
  412. written_config = self._read_yaml(config_file)
  413. self.assertEquals('openshift-enterprise', written_config['variant'])
  414. # Make sure our older version was preserved:
  415. # and written to disk:
  416. self.assertEquals('3.0', written_config['variant_version'])
  417. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  418. inventory.read(os.path.join(self.work_dir, '.ansible/hosts'))
  419. self.assertEquals('enterprise',
  420. inventory.get('OSEv3:vars', 'deployment_type'))
  421. @patch('ooinstall.openshift_ansible.run_ansible')
  422. @patch('ooinstall.openshift_ansible.load_system_facts')
  423. def test_no_ansible_config_specified(self, load_facts_mock, run_ansible_mock):
  424. load_facts_mock.return_value = (MOCK_FACTS, 0)
  425. run_ansible_mock.return_value = 0
  426. config = SAMPLE_CONFIG % 'openshift-enterprise'
  427. self._ansible_config_test(load_facts_mock, run_ansible_mock,
  428. config, None, None)
  429. @patch('ooinstall.openshift_ansible.run_ansible')
  430. @patch('ooinstall.openshift_ansible.load_system_facts')
  431. def test_ansible_config_specified_cli(self, load_facts_mock, run_ansible_mock):
  432. load_facts_mock.return_value = (MOCK_FACTS, 0)
  433. run_ansible_mock.return_value = 0
  434. config = SAMPLE_CONFIG % 'openshift-enterprise'
  435. ansible_config = os.path.join(self.work_dir, 'ansible.cfg')
  436. self._ansible_config_test(load_facts_mock, run_ansible_mock,
  437. config, ansible_config, ansible_config)
  438. @patch('ooinstall.openshift_ansible.run_ansible')
  439. @patch('ooinstall.openshift_ansible.load_system_facts')
  440. def test_ansible_config_specified_in_installer_config(self,
  441. load_facts_mock, run_ansible_mock):
  442. load_facts_mock.return_value = (MOCK_FACTS, 0)
  443. run_ansible_mock.return_value = 0
  444. ansible_config = os.path.join(self.work_dir, 'ansible.cfg')
  445. config = SAMPLE_CONFIG % 'openshift-enterprise'
  446. config = "%s\nansible_config: %s" % (config, ansible_config)
  447. self._ansible_config_test(load_facts_mock, run_ansible_mock,
  448. config, None, ansible_config)
  449. #pylint: disable=too-many-arguments
  450. # This method allows for drastically simpler tests to write, and the args
  451. # are all useful.
  452. def _ansible_config_test(self, load_facts_mock, run_ansible_mock,
  453. installer_config, ansible_config_cli=None, expected_result=None):
  454. """
  455. Utility method for testing the ways you can specify the ansible config.
  456. """
  457. load_facts_mock.return_value = (MOCK_FACTS, 0)
  458. run_ansible_mock.return_value = 0
  459. config_file = self.write_config(os.path.join(self.work_dir,
  460. 'ooinstall.conf'), installer_config)
  461. self.cli_args.extend(["-c", config_file])
  462. if ansible_config_cli:
  463. self.cli_args.extend(["--ansible-config", ansible_config_cli])
  464. self.cli_args.append("install")
  465. result = self.runner.invoke(cli.cli, self.cli_args)
  466. self.assert_result(result, 0)
  467. # Test the env vars for facts playbook:
  468. facts_env_vars = load_facts_mock.call_args[0][2]
  469. if expected_result:
  470. self.assertEquals(expected_result, facts_env_vars['ANSIBLE_CONFIG'])
  471. else:
  472. # If user running test has rpm installed, this might be set to default:
  473. self.assertTrue('ANSIBLE_CONFIG' not in facts_env_vars or
  474. facts_env_vars['ANSIBLE_CONFIG'] == cli.DEFAULT_ANSIBLE_CONFIG)
  475. # Test the env vars for main playbook:
  476. env_vars = run_ansible_mock.call_args[0][2]
  477. if expected_result:
  478. self.assertEquals(expected_result, env_vars['ANSIBLE_CONFIG'])
  479. else:
  480. # If user running test has rpm installed, this might be set to default:
  481. self.assertTrue('ANSIBLE_CONFIG' not in env_vars or
  482. env_vars['ANSIBLE_CONFIG'] == cli.DEFAULT_ANSIBLE_CONFIG)
  483. # unattended with bad config file and no installed hosts (without --force)
  484. @patch('ooinstall.openshift_ansible.run_main_playbook')
  485. @patch('ooinstall.openshift_ansible.load_system_facts')
  486. def test_bad_config(self, load_facts_mock, run_playbook_mock):
  487. load_facts_mock.return_value = (MOCK_FACTS, 0)
  488. run_playbook_mock.return_value = 0
  489. config_file = self.write_config(os.path.join(self.work_dir,
  490. 'ooinstall.conf'), BAD_CONFIG % 'openshift-enterprise')
  491. self.cli_args.extend(["-c", config_file, "install"])
  492. result = self.runner.invoke(cli.cli, self.cli_args)
  493. assert result.exit_code == 1
  494. assert result.output == "You must specify either and 'ip' or 'hostname' to connect to.\n"
  495. #unattended with two masters, one node, and haproxy
  496. @patch('ooinstall.openshift_ansible.run_main_playbook')
  497. @patch('ooinstall.openshift_ansible.load_system_facts')
  498. def test_quick_ha_full_run(self, load_facts_mock, run_playbook_mock):
  499. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  500. run_playbook_mock.return_value = 0
  501. config_file = self.write_config(os.path.join(self.work_dir,
  502. 'ooinstall.conf'), QUICKHA_CONFIG % 'openshift-enterprise')
  503. self.cli_args.extend(["-c", config_file, "install"])
  504. result = self.runner.invoke(cli.cli, self.cli_args)
  505. self.assert_result(result, 0)
  506. load_facts_args = load_facts_mock.call_args[0]
  507. self.assertEquals(os.path.join(self.work_dir, ".ansible/hosts"),
  508. load_facts_args[0])
  509. self.assertEquals(os.path.join(self.work_dir,
  510. "playbooks/byo/openshift_facts.yml"), load_facts_args[1])
  511. env_vars = load_facts_args[2]
  512. self.assertEquals(os.path.join(self.work_dir,
  513. '.ansible/callback_facts.yaml'),
  514. env_vars['OO_INSTALL_CALLBACK_FACTS_YAML'])
  515. self.assertEqual('/tmp/ansible.log', env_vars['ANSIBLE_LOG_PATH'])
  516. # If user running test has rpm installed, this might be set to default:
  517. self.assertTrue('ANSIBLE_CONFIG' not in env_vars or
  518. env_vars['ANSIBLE_CONFIG'] == cli.DEFAULT_ANSIBLE_CONFIG)
  519. # Make sure we ran on the expected masters and nodes:
  520. hosts = run_playbook_mock.call_args[0][0]
  521. hosts_to_run_on = run_playbook_mock.call_args[0][1]
  522. self.assertEquals(4, len(hosts))
  523. self.assertEquals(4, len(hosts_to_run_on))
  524. class AttendedCliTests(OOCliFixture):
  525. def setUp(self):
  526. OOCliFixture.setUp(self)
  527. # Doesn't exist but keeps us from reading the local users config:
  528. self.config_file = os.path.join(self.work_dir, 'config.yml')
  529. self.cli_args.extend(["-c", self.config_file])
  530. #pylint: disable=too-many-arguments
  531. def _build_input(self, ssh_user=None, hosts=None, variant_num=None,
  532. add_nodes=None, confirm_facts=None):
  533. """
  534. Builds a CLI input string with newline characters to simulate
  535. the full run.
  536. This gives us only one place to update when the input prompts change.
  537. """
  538. inputs = [
  539. 'y', # let's proceed
  540. ]
  541. if ssh_user:
  542. inputs.append(ssh_user)
  543. if variant_num:
  544. inputs.append(str(variant_num)) # Choose variant + version
  545. if hosts:
  546. i = 0
  547. for (host, is_master) in hosts:
  548. inputs.append(host)
  549. inputs.append('y' if is_master else 'n')
  550. #inputs.append('rpm')
  551. if i < len(hosts) - 1:
  552. inputs.append('y') # Add more hosts
  553. else:
  554. inputs.append('n') # Done adding hosts
  555. i += 1
  556. # TODO: support option 2, fresh install
  557. if add_nodes:
  558. inputs.append('1') # Add more nodes
  559. i = 0
  560. for (host, is_master) in add_nodes:
  561. inputs.append(host)
  562. inputs.append('y' if is_master else 'n')
  563. #inputs.append('rpm')
  564. if i < len(add_nodes) - 1:
  565. inputs.append('y') # Add more hosts
  566. else:
  567. inputs.append('n') # Done adding hosts
  568. i += 1
  569. inputs.extend([
  570. confirm_facts,
  571. 'y', # lets do this
  572. ])
  573. return '\n'.join(inputs)
  574. @patch('ooinstall.openshift_ansible.run_main_playbook')
  575. @patch('ooinstall.openshift_ansible.load_system_facts')
  576. def test_full_run(self, load_facts_mock, run_playbook_mock):
  577. load_facts_mock.return_value = (MOCK_FACTS, 0)
  578. run_playbook_mock.return_value = 0
  579. cli_input = self._build_input(hosts=[
  580. ('10.0.0.1', True),
  581. ('10.0.0.2', False),
  582. ('10.0.0.3', False)],
  583. ssh_user='root',
  584. variant_num=1,
  585. confirm_facts='y')
  586. self.cli_args.append("install")
  587. result = self.runner.invoke(cli.cli, self.cli_args,
  588. input=cli_input)
  589. self.assert_result(result, 0)
  590. self._verify_load_facts(load_facts_mock)
  591. self._verify_run_playbook(run_playbook_mock, 3, 3)
  592. written_config = self._read_yaml(self.config_file)
  593. self._verify_config_hosts(written_config, 3)
  594. # interactive with config file and some installed some uninstalled hosts
  595. @patch('ooinstall.openshift_ansible.run_main_playbook')
  596. @patch('ooinstall.openshift_ansible.load_system_facts')
  597. def test_add_nodes(self, load_facts_mock, run_playbook_mock):
  598. # Modify the mock facts to return a version indicating OpenShift
  599. # is already installed on our master, and the first node.
  600. mock_facts = copy.deepcopy(MOCK_FACTS)
  601. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  602. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  603. load_facts_mock.return_value = (mock_facts, 0)
  604. run_playbook_mock.return_value = 0
  605. cli_input = self._build_input(hosts=[
  606. ('10.0.0.1', True),
  607. ('10.0.0.2', False),
  608. ],
  609. add_nodes=[('10.0.0.3', False)],
  610. ssh_user='root',
  611. variant_num=1,
  612. confirm_facts='y')
  613. self.cli_args.append("install")
  614. result = self.runner.invoke(cli.cli,
  615. self.cli_args,
  616. input=cli_input)
  617. print result
  618. self.assert_result(result, 0)
  619. self._verify_load_facts(load_facts_mock)
  620. self._verify_run_playbook(run_playbook_mock, 3, 2)
  621. written_config = self._read_yaml(self.config_file)
  622. self._verify_config_hosts(written_config, 3)
  623. @patch('ooinstall.openshift_ansible.run_main_playbook')
  624. @patch('ooinstall.openshift_ansible.load_system_facts')
  625. def test_fresh_install_with_config(self, load_facts_mock, run_playbook_mock):
  626. load_facts_mock.return_value = (MOCK_FACTS, 0)
  627. run_playbook_mock.return_value = 0
  628. config_file = self.write_config(os.path.join(self.work_dir,
  629. 'ooinstall.conf'),
  630. SAMPLE_CONFIG % 'openshift-enterprise')
  631. cli_input = self._build_input(confirm_facts='y')
  632. self.cli_args.extend(["-c", config_file])
  633. self.cli_args.append("install")
  634. result = self.runner.invoke(cli.cli,
  635. self.cli_args,
  636. input=cli_input)
  637. self.assert_result(result, 0)
  638. self._verify_load_facts(load_facts_mock)
  639. self._verify_run_playbook(run_playbook_mock, 3, 3)
  640. written_config = self._read_yaml(config_file)
  641. self._verify_config_hosts(written_config, 3)
  642. #interactive with config file and all installed hosts
  643. @patch('ooinstall.openshift_ansible.run_main_playbook')
  644. @patch('ooinstall.openshift_ansible.load_system_facts')
  645. def test_get_hosts_to_run_on(self, load_facts_mock, run_playbook_mock):
  646. mock_facts = copy.deepcopy(MOCK_FACTS)
  647. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  648. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  649. cli_input = self._build_input(hosts=[
  650. ('10.0.0.1', True),
  651. ],
  652. add_nodes=[('10.0.0.2', False)],
  653. ssh_user='root',
  654. variant_num=1,
  655. confirm_facts='y')
  656. self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock,
  657. run_playbook_mock,
  658. cli_input,
  659. exp_hosts_len=2,
  660. exp_hosts_to_run_on_len=2,
  661. force=False)
  662. #interactive multimaster
  663. @patch('ooinstall.openshift_ansible.run_main_playbook')
  664. @patch('ooinstall.openshift_ansible.load_system_facts')
  665. def test_quick_ha(self, load_facts_mock, run_playbook_mock):
  666. load_facts_mock.return_value = (MOCK_FACTS, 0)
  667. run_playbook_mock.return_value = 0
  668. cli_input = self._build_input(hosts=[
  669. ('10.0.0.1', True),
  670. ('10.0.0.2', True),
  671. ('10.0.0.3', False),
  672. ('10.0.0.4', True)],
  673. ssh_user='root',
  674. variant_num=1,
  675. confirm_facts='y')
  676. self.cli_args.append("install")
  677. result = self.runner.invoke(cli.cli, self.cli_args,
  678. input=cli_input)
  679. self.assert_result(result, 0)
  680. self._verify_load_facts(load_facts_mock)
  681. self._verify_run_playbook(run_playbook_mock, 3, 3)
  682. written_config = self._read_yaml(self.config_file)
  683. self._verify_config_hosts(written_config, 3)
  684. return
  685. # TODO: test with config file, attended add node
  686. # TODO: test with config file, attended new node already in config file
  687. # TODO: test with config file, attended new node already in config file, plus manually added nodes
  688. # TODO: test with config file, attended reject facts