cli_installer_tests.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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('hostname' in h)
  198. self.assertTrue('public_hostname' in h)
  199. if 'preconfigured' not in h:
  200. self.assertTrue(h['node'])
  201. self.assertTrue('ip' in h)
  202. self.assertTrue('public_ip' in h)
  203. #pylint: disable=too-many-arguments
  204. def _verify_get_hosts_to_run_on(self, mock_facts, load_facts_mock,
  205. run_playbook_mock, cli_input,
  206. exp_hosts_len=None, exp_hosts_to_run_on_len=None,
  207. force=None):
  208. """
  209. Tests cli_installer.py:get_hosts_to_run_on. That method has quite a
  210. few subtle branches in the logic. The goal with this method is simply
  211. to handle all the messy stuff here and allow the main test cases to be
  212. easily read. The basic idea is to modify mock_facts to return a
  213. version indicating OpenShift is already installed on particular hosts.
  214. """
  215. load_facts_mock.return_value = (mock_facts, 0)
  216. run_playbook_mock.return_value = 0
  217. if cli_input:
  218. self.cli_args.append("install")
  219. result = self.runner.invoke(cli.cli,
  220. self.cli_args,
  221. input=cli_input)
  222. else:
  223. config_file = self.write_config(os.path.join(self.work_dir,
  224. 'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')
  225. self.cli_args.extend(["-c", config_file, "install"])
  226. if force:
  227. self.cli_args.append("--force")
  228. result = self.runner.invoke(cli.cli, self.cli_args)
  229. written_config = self._read_yaml(config_file)
  230. self._verify_config_hosts(written_config, exp_hosts_len)
  231. self.assert_result(result, 0)
  232. self._verify_load_facts(load_facts_mock)
  233. self._verify_run_playbook(run_playbook_mock, exp_hosts_len, exp_hosts_to_run_on_len)
  234. # Make sure we ran on the expected masters and nodes:
  235. hosts = run_playbook_mock.call_args[0][0]
  236. hosts_to_run_on = run_playbook_mock.call_args[0][1]
  237. self.assertEquals(exp_hosts_len, len(hosts))
  238. self.assertEquals(exp_hosts_to_run_on_len, len(hosts_to_run_on))
  239. class UnattendedCliTests(OOCliFixture):
  240. def setUp(self):
  241. OOCliFixture.setUp(self)
  242. self.cli_args.append("-u")
  243. # unattended with config file and all installed hosts (without --force)
  244. @patch('ooinstall.openshift_ansible.run_main_playbook')
  245. @patch('ooinstall.openshift_ansible.load_system_facts')
  246. def test_get_hosts_to_run_on1(self, load_facts_mock, run_playbook_mock):
  247. mock_facts = copy.deepcopy(MOCK_FACTS)
  248. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  249. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  250. mock_facts['10.0.0.3']['common']['version'] = "3.0.0"
  251. load_facts_mock.return_value = (mock_facts, 0)
  252. run_playbook_mock.return_value = 0
  253. config_file = self.write_config(os.path.join(self.work_dir,
  254. 'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')
  255. self.cli_args.extend(["-c", config_file, "install"])
  256. result = self.runner.invoke(cli.cli, self.cli_args)
  257. if result.exception is None or result.exit_code != 1:
  258. print "Exit code: %s" % result.exit_code
  259. self.fail("Unexpected CLI return")
  260. # unattended with config file and all installed hosts (with --force)
  261. @patch('ooinstall.openshift_ansible.run_main_playbook')
  262. @patch('ooinstall.openshift_ansible.load_system_facts')
  263. def test_get_hosts_to_run_on2(self, load_facts_mock, run_playbook_mock):
  264. mock_facts = copy.deepcopy(MOCK_FACTS)
  265. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  266. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  267. mock_facts['10.0.0.3']['common']['version'] = "3.0.0"
  268. self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
  269. cli_input=None,
  270. exp_hosts_len=3,
  271. exp_hosts_to_run_on_len=3,
  272. force=True)
  273. # unattended with config file and no installed hosts (without --force)
  274. @patch('ooinstall.openshift_ansible.run_main_playbook')
  275. @patch('ooinstall.openshift_ansible.load_system_facts')
  276. def test_get_hosts_to_run_on3(self, load_facts_mock, run_playbook_mock):
  277. load_facts_mock.return_value = (MOCK_FACTS, 0)
  278. run_playbook_mock.return_value = 0
  279. self._verify_get_hosts_to_run_on(MOCK_FACTS, load_facts_mock, run_playbook_mock,
  280. cli_input=None,
  281. exp_hosts_len=3,
  282. exp_hosts_to_run_on_len=3,
  283. force=False)
  284. # unattended with config file and no installed hosts (with --force)
  285. @patch('ooinstall.openshift_ansible.run_main_playbook')
  286. @patch('ooinstall.openshift_ansible.load_system_facts')
  287. def test_get_hosts_to_run_on4(self, load_facts_mock, run_playbook_mock):
  288. load_facts_mock.return_value = (MOCK_FACTS, 0)
  289. run_playbook_mock.return_value = 0
  290. self._verify_get_hosts_to_run_on(MOCK_FACTS, load_facts_mock, run_playbook_mock,
  291. cli_input=None,
  292. exp_hosts_len=3,
  293. exp_hosts_to_run_on_len=3,
  294. force=True)
  295. # unattended with config file and some installed some uninstalled hosts (without --force)
  296. @patch('ooinstall.openshift_ansible.run_main_playbook')
  297. @patch('ooinstall.openshift_ansible.load_system_facts')
  298. def test_get_hosts_to_run_on5(self, load_facts_mock, run_playbook_mock):
  299. mock_facts = copy.deepcopy(MOCK_FACTS)
  300. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  301. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  302. self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
  303. cli_input=None,
  304. exp_hosts_len=3,
  305. exp_hosts_to_run_on_len=2,
  306. force=False)
  307. # unattended with config file and some installed some uninstalled hosts (with --force)
  308. @patch('ooinstall.openshift_ansible.run_main_playbook')
  309. @patch('ooinstall.openshift_ansible.load_system_facts')
  310. def test_get_hosts_to_run_on6(self, load_facts_mock, run_playbook_mock):
  311. mock_facts = copy.deepcopy(MOCK_FACTS)
  312. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  313. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  314. self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
  315. cli_input=None,
  316. exp_hosts_len=3,
  317. exp_hosts_to_run_on_len=3,
  318. force=True)
  319. @patch('ooinstall.openshift_ansible.run_main_playbook')
  320. @patch('ooinstall.openshift_ansible.load_system_facts')
  321. def test_cfg_full_run(self, load_facts_mock, run_playbook_mock):
  322. load_facts_mock.return_value = (MOCK_FACTS, 0)
  323. run_playbook_mock.return_value = 0
  324. config_file = self.write_config(os.path.join(self.work_dir,
  325. 'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')
  326. self.cli_args.extend(["-c", config_file, "install"])
  327. result = self.runner.invoke(cli.cli, self.cli_args)
  328. self.assert_result(result, 0)
  329. load_facts_args = load_facts_mock.call_args[0]
  330. self.assertEquals(os.path.join(self.work_dir, ".ansible/hosts"),
  331. load_facts_args[0])
  332. self.assertEquals(os.path.join(self.work_dir,
  333. "playbooks/byo/openshift_facts.yml"), load_facts_args[1])
  334. env_vars = load_facts_args[2]
  335. self.assertEquals(os.path.join(self.work_dir,
  336. '.ansible/callback_facts.yaml'),
  337. env_vars['OO_INSTALL_CALLBACK_FACTS_YAML'])
  338. self.assertEqual('/tmp/ansible.log', env_vars['ANSIBLE_LOG_PATH'])
  339. # If user running test has rpm installed, this might be set to default:
  340. self.assertTrue('ANSIBLE_CONFIG' not in env_vars or
  341. env_vars['ANSIBLE_CONFIG'] == cli.DEFAULT_ANSIBLE_CONFIG)
  342. # Make sure we ran on the expected masters and nodes:
  343. hosts = run_playbook_mock.call_args[0][0]
  344. hosts_to_run_on = run_playbook_mock.call_args[0][1]
  345. self.assertEquals(3, len(hosts))
  346. self.assertEquals(3, len(hosts_to_run_on))
  347. @patch('ooinstall.openshift_ansible.run_main_playbook')
  348. @patch('ooinstall.openshift_ansible.load_system_facts')
  349. def test_inventory_write(self, load_facts_mock, run_playbook_mock):
  350. # Add an ssh user so we can verify it makes it to the inventory file:
  351. merged_config = "%s\n%s" % (SAMPLE_CONFIG % 'openshift-enterprise',
  352. "ansible_ssh_user: bob")
  353. load_facts_mock.return_value = (MOCK_FACTS, 0)
  354. run_playbook_mock.return_value = 0
  355. config_file = self.write_config(os.path.join(self.work_dir,
  356. 'ooinstall.conf'), merged_config)
  357. self.cli_args.extend(["-c", config_file, "install"])
  358. result = self.runner.invoke(cli.cli, self.cli_args)
  359. self.assert_result(result, 0)
  360. # Check the inventory file looks as we would expect:
  361. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  362. inventory.read(os.path.join(self.work_dir, '.ansible/hosts'))
  363. self.assertEquals('bob',
  364. inventory.get('OSEv3:vars', 'ansible_ssh_user'))
  365. self.assertEquals('openshift-enterprise',
  366. inventory.get('OSEv3:vars', 'deployment_type'))
  367. # Check the masters:
  368. self.assertEquals(1, len(inventory.items('masters')))
  369. self.assertEquals(3, len(inventory.items('nodes')))
  370. for item in inventory.items('masters'):
  371. # ansible host lines do NOT parse nicely:
  372. master_line = item[0]
  373. if item[1] is not None:
  374. master_line = "%s=%s" % (master_line, item[1])
  375. self.assertTrue('openshift_ip' in master_line)
  376. self.assertTrue('openshift_public_ip' in master_line)
  377. self.assertTrue('openshift_hostname' in master_line)
  378. self.assertTrue('openshift_public_hostname' in master_line)
  379. @patch('ooinstall.openshift_ansible.run_main_playbook')
  380. @patch('ooinstall.openshift_ansible.load_system_facts')
  381. def test_variant_version_latest_assumed(self, load_facts_mock,
  382. run_playbook_mock):
  383. load_facts_mock.return_value = (MOCK_FACTS, 0)
  384. run_playbook_mock.return_value = 0
  385. config_file = self.write_config(os.path.join(self.work_dir,
  386. 'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')
  387. self.cli_args.extend(["-c", config_file, "install"])
  388. result = self.runner.invoke(cli.cli, self.cli_args)
  389. self.assert_result(result, 0)
  390. written_config = self._read_yaml(config_file)
  391. self.assertEquals('openshift-enterprise', written_config['variant'])
  392. # We didn't specify a version so the latest should have been assumed,
  393. # and written to disk:
  394. self.assertEquals('3.1', written_config['variant_version'])
  395. # Make sure the correct value was passed to ansible:
  396. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  397. inventory.read(os.path.join(self.work_dir, '.ansible/hosts'))
  398. self.assertEquals('openshift-enterprise',
  399. inventory.get('OSEv3:vars', 'deployment_type'))
  400. @patch('ooinstall.openshift_ansible.run_main_playbook')
  401. @patch('ooinstall.openshift_ansible.load_system_facts')
  402. def test_variant_version_preserved(self, load_facts_mock,
  403. run_playbook_mock):
  404. load_facts_mock.return_value = (MOCK_FACTS, 0)
  405. run_playbook_mock.return_value = 0
  406. config = SAMPLE_CONFIG % 'openshift-enterprise'
  407. config = '%s\n%s' % (config, 'variant_version: 3.0')
  408. config_file = self.write_config(os.path.join(self.work_dir,
  409. 'ooinstall.conf'), config)
  410. self.cli_args.extend(["-c", config_file, "install"])
  411. result = self.runner.invoke(cli.cli, self.cli_args)
  412. self.assert_result(result, 0)
  413. written_config = self._read_yaml(config_file)
  414. self.assertEquals('openshift-enterprise', written_config['variant'])
  415. # Make sure our older version was preserved:
  416. # and written to disk:
  417. self.assertEquals('3.0', written_config['variant_version'])
  418. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  419. inventory.read(os.path.join(self.work_dir, '.ansible/hosts'))
  420. self.assertEquals('enterprise',
  421. inventory.get('OSEv3:vars', 'deployment_type'))
  422. @patch('ooinstall.openshift_ansible.run_ansible')
  423. @patch('ooinstall.openshift_ansible.load_system_facts')
  424. def test_no_ansible_config_specified(self, load_facts_mock, run_ansible_mock):
  425. load_facts_mock.return_value = (MOCK_FACTS, 0)
  426. run_ansible_mock.return_value = 0
  427. config = SAMPLE_CONFIG % 'openshift-enterprise'
  428. self._ansible_config_test(load_facts_mock, run_ansible_mock,
  429. config, None, None)
  430. @patch('ooinstall.openshift_ansible.run_ansible')
  431. @patch('ooinstall.openshift_ansible.load_system_facts')
  432. def test_ansible_config_specified_cli(self, load_facts_mock, run_ansible_mock):
  433. load_facts_mock.return_value = (MOCK_FACTS, 0)
  434. run_ansible_mock.return_value = 0
  435. config = SAMPLE_CONFIG % 'openshift-enterprise'
  436. ansible_config = os.path.join(self.work_dir, 'ansible.cfg')
  437. self._ansible_config_test(load_facts_mock, run_ansible_mock,
  438. config, ansible_config, ansible_config)
  439. @patch('ooinstall.openshift_ansible.run_ansible')
  440. @patch('ooinstall.openshift_ansible.load_system_facts')
  441. def test_ansible_config_specified_in_installer_config(self,
  442. load_facts_mock, run_ansible_mock):
  443. load_facts_mock.return_value = (MOCK_FACTS, 0)
  444. run_ansible_mock.return_value = 0
  445. ansible_config = os.path.join(self.work_dir, 'ansible.cfg')
  446. config = SAMPLE_CONFIG % 'openshift-enterprise'
  447. config = "%s\nansible_config: %s" % (config, ansible_config)
  448. self._ansible_config_test(load_facts_mock, run_ansible_mock,
  449. config, None, ansible_config)
  450. #pylint: disable=too-many-arguments
  451. # This method allows for drastically simpler tests to write, and the args
  452. # are all useful.
  453. def _ansible_config_test(self, load_facts_mock, run_ansible_mock,
  454. installer_config, ansible_config_cli=None, expected_result=None):
  455. """
  456. Utility method for testing the ways you can specify the ansible config.
  457. """
  458. load_facts_mock.return_value = (MOCK_FACTS, 0)
  459. run_ansible_mock.return_value = 0
  460. config_file = self.write_config(os.path.join(self.work_dir,
  461. 'ooinstall.conf'), installer_config)
  462. self.cli_args.extend(["-c", config_file])
  463. if ansible_config_cli:
  464. self.cli_args.extend(["--ansible-config", ansible_config_cli])
  465. self.cli_args.append("install")
  466. result = self.runner.invoke(cli.cli, self.cli_args)
  467. self.assert_result(result, 0)
  468. # Test the env vars for facts playbook:
  469. facts_env_vars = load_facts_mock.call_args[0][2]
  470. if expected_result:
  471. self.assertEquals(expected_result, facts_env_vars['ANSIBLE_CONFIG'])
  472. else:
  473. # If user running test has rpm installed, this might be set to default:
  474. self.assertTrue('ANSIBLE_CONFIG' not in facts_env_vars or
  475. facts_env_vars['ANSIBLE_CONFIG'] == cli.DEFAULT_ANSIBLE_CONFIG)
  476. # Test the env vars for main playbook:
  477. env_vars = run_ansible_mock.call_args[0][2]
  478. if expected_result:
  479. self.assertEquals(expected_result, env_vars['ANSIBLE_CONFIG'])
  480. else:
  481. # If user running test has rpm installed, this might be set to default:
  482. self.assertTrue('ANSIBLE_CONFIG' not in env_vars or
  483. env_vars['ANSIBLE_CONFIG'] == cli.DEFAULT_ANSIBLE_CONFIG)
  484. # unattended with bad config file and no installed hosts (without --force)
  485. @patch('ooinstall.openshift_ansible.run_main_playbook')
  486. @patch('ooinstall.openshift_ansible.load_system_facts')
  487. def test_bad_config(self, load_facts_mock, run_playbook_mock):
  488. load_facts_mock.return_value = (MOCK_FACTS, 0)
  489. run_playbook_mock.return_value = 0
  490. config_file = self.write_config(os.path.join(self.work_dir,
  491. 'ooinstall.conf'), BAD_CONFIG % 'openshift-enterprise')
  492. self.cli_args.extend(["-c", config_file, "install"])
  493. result = self.runner.invoke(cli.cli, self.cli_args)
  494. assert result.exit_code == 1
  495. assert result.output == "You must specify either and 'ip' or 'hostname' to connect to.\n"
  496. #unattended with two masters, one node, and haproxy
  497. @patch('ooinstall.openshift_ansible.run_main_playbook')
  498. @patch('ooinstall.openshift_ansible.load_system_facts')
  499. def test_quick_ha_full_run(self, load_facts_mock, run_playbook_mock):
  500. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  501. run_playbook_mock.return_value = 0
  502. config_file = self.write_config(os.path.join(self.work_dir,
  503. 'ooinstall.conf'), QUICKHA_CONFIG % 'openshift-enterprise')
  504. self.cli_args.extend(["-c", config_file, "install"])
  505. result = self.runner.invoke(cli.cli, self.cli_args)
  506. self.assert_result(result, 0)
  507. load_facts_args = load_facts_mock.call_args[0]
  508. self.assertEquals(os.path.join(self.work_dir, ".ansible/hosts"),
  509. load_facts_args[0])
  510. self.assertEquals(os.path.join(self.work_dir,
  511. "playbooks/byo/openshift_facts.yml"), load_facts_args[1])
  512. env_vars = load_facts_args[2]
  513. self.assertEquals(os.path.join(self.work_dir,
  514. '.ansible/callback_facts.yaml'),
  515. env_vars['OO_INSTALL_CALLBACK_FACTS_YAML'])
  516. self.assertEqual('/tmp/ansible.log', env_vars['ANSIBLE_LOG_PATH'])
  517. # If user running test has rpm installed, this might be set to default:
  518. self.assertTrue('ANSIBLE_CONFIG' not in env_vars or
  519. env_vars['ANSIBLE_CONFIG'] == cli.DEFAULT_ANSIBLE_CONFIG)
  520. # Make sure we ran on the expected masters and nodes:
  521. hosts = run_playbook_mock.call_args[0][0]
  522. hosts_to_run_on = run_playbook_mock.call_args[0][1]
  523. self.assertEquals(4, len(hosts))
  524. self.assertEquals(4, len(hosts_to_run_on))
  525. class AttendedCliTests(OOCliFixture):
  526. def setUp(self):
  527. OOCliFixture.setUp(self)
  528. # Doesn't exist but keeps us from reading the local users config:
  529. self.config_file = os.path.join(self.work_dir, 'config.yml')
  530. self.cli_args.extend(["-c", self.config_file])
  531. #pylint: disable=too-many-arguments,too-many-branches
  532. def _build_input(self, ssh_user=None, hosts=None, variant_num=None,
  533. add_nodes=None, confirm_facts=None, schedulable_masters_ok=None,
  534. master_lb=None):
  535. """
  536. Builds a CLI input string with newline characters to simulate
  537. the full run.
  538. This gives us only one place to update when the input prompts change.
  539. """
  540. inputs = [
  541. 'y', # let's proceed
  542. ]
  543. if ssh_user:
  544. inputs.append(ssh_user)
  545. if variant_num:
  546. inputs.append(str(variant_num)) # Choose variant + version
  547. num_masters = 0
  548. if hosts:
  549. i = 0
  550. min_masters_for_ha = 3
  551. for (host, is_master) in hosts:
  552. inputs.append(host)
  553. if is_master:
  554. inputs.append('y')
  555. num_masters += 1
  556. else:
  557. inputs.append('n')
  558. #inputs.append('rpm')
  559. if i < len(hosts) - 1:
  560. if num_masters <= 1 or num_masters >= min_masters_for_ha:
  561. inputs.append('y') # Add more hosts
  562. else:
  563. inputs.append('n') # Done adding hosts
  564. i += 1
  565. if master_lb:
  566. inputs.append(master_lb[0])
  567. inputs.append('y' if master_lb[1] else 'n')
  568. # TODO: support option 2, fresh install
  569. if add_nodes:
  570. if schedulable_masters_ok:
  571. inputs.append('y')
  572. inputs.append('1') # Add more nodes
  573. i = 0
  574. for (host, is_master) in add_nodes:
  575. inputs.append(host)
  576. #inputs.append('rpm')
  577. if i < len(add_nodes) - 1:
  578. inputs.append('y') # Add more hosts
  579. else:
  580. inputs.append('n') # Done adding hosts
  581. i += 1
  582. if add_nodes is None:
  583. total_hosts = hosts
  584. else:
  585. total_hosts = hosts + add_nodes
  586. if total_hosts is not None and num_masters == len(total_hosts):
  587. inputs.append('y')
  588. inputs.extend([
  589. confirm_facts,
  590. 'y', # lets do this
  591. ])
  592. return '\n'.join(inputs)
  593. @patch('ooinstall.openshift_ansible.run_main_playbook')
  594. @patch('ooinstall.openshift_ansible.load_system_facts')
  595. def test_full_run(self, load_facts_mock, run_playbook_mock):
  596. load_facts_mock.return_value = (MOCK_FACTS, 0)
  597. run_playbook_mock.return_value = 0
  598. cli_input = self._build_input(hosts=[
  599. ('10.0.0.1', True),
  600. ('10.0.0.2', False),
  601. ('10.0.0.3', False)],
  602. ssh_user='root',
  603. variant_num=1,
  604. confirm_facts='y')
  605. self.cli_args.append("install")
  606. result = self.runner.invoke(cli.cli, self.cli_args,
  607. input=cli_input)
  608. self.assert_result(result, 0)
  609. self._verify_load_facts(load_facts_mock)
  610. self._verify_run_playbook(run_playbook_mock, 3, 3)
  611. written_config = self._read_yaml(self.config_file)
  612. self._verify_config_hosts(written_config, 3)
  613. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  614. inventory.read(os.path.join(self.work_dir, '.ansible/hosts'))
  615. self.assertEquals('False',
  616. inventory.get('nodes', '10.0.0.1 openshift_schedulable'))
  617. self.assertEquals(None,
  618. inventory.get('nodes', '10.0.0.2'))
  619. self.assertEquals(None,
  620. inventory.get('nodes', '10.0.0.3'))
  621. # interactive with config file and some installed some uninstalled hosts
  622. @patch('ooinstall.openshift_ansible.run_main_playbook')
  623. @patch('ooinstall.openshift_ansible.load_system_facts')
  624. def test_add_nodes(self, load_facts_mock, run_playbook_mock):
  625. # Modify the mock facts to return a version indicating OpenShift
  626. # is already installed on our master, and the first node.
  627. mock_facts = copy.deepcopy(MOCK_FACTS)
  628. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  629. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  630. load_facts_mock.return_value = (mock_facts, 0)
  631. run_playbook_mock.return_value = 0
  632. cli_input = self._build_input(hosts=[
  633. ('10.0.0.1', True),
  634. ('10.0.0.2', False),
  635. ],
  636. add_nodes=[('10.0.0.3', False)],
  637. ssh_user='root',
  638. variant_num=1,
  639. confirm_facts='y')
  640. self.cli_args.append("install")
  641. result = self.runner.invoke(cli.cli,
  642. self.cli_args,
  643. input=cli_input)
  644. print result
  645. self.assert_result(result, 0)
  646. self._verify_load_facts(load_facts_mock)
  647. self._verify_run_playbook(run_playbook_mock, 3, 2)
  648. written_config = self._read_yaml(self.config_file)
  649. self._verify_config_hosts(written_config, 3)
  650. @patch('ooinstall.openshift_ansible.run_main_playbook')
  651. @patch('ooinstall.openshift_ansible.load_system_facts')
  652. def test_fresh_install_with_config(self, load_facts_mock, run_playbook_mock):
  653. load_facts_mock.return_value = (MOCK_FACTS, 0)
  654. run_playbook_mock.return_value = 0
  655. config_file = self.write_config(os.path.join(self.work_dir,
  656. 'ooinstall.conf'),
  657. SAMPLE_CONFIG % 'openshift-enterprise')
  658. cli_input = self._build_input(confirm_facts='y')
  659. self.cli_args.extend(["-c", config_file])
  660. self.cli_args.append("install")
  661. result = self.runner.invoke(cli.cli,
  662. self.cli_args,
  663. input=cli_input)
  664. self.assert_result(result, 0)
  665. self._verify_load_facts(load_facts_mock)
  666. self._verify_run_playbook(run_playbook_mock, 3, 3)
  667. written_config = self._read_yaml(config_file)
  668. self._verify_config_hosts(written_config, 3)
  669. #interactive with config file and all installed hosts
  670. @patch('ooinstall.openshift_ansible.run_main_playbook')
  671. @patch('ooinstall.openshift_ansible.load_system_facts')
  672. def test_get_hosts_to_run_on(self, load_facts_mock, run_playbook_mock):
  673. mock_facts = copy.deepcopy(MOCK_FACTS)
  674. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  675. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  676. cli_input = self._build_input(hosts=[
  677. ('10.0.0.1', True),
  678. ],
  679. add_nodes=[('10.0.0.2', False)],
  680. ssh_user='root',
  681. variant_num=1,
  682. schedulable_masters_ok=True,
  683. confirm_facts='y')
  684. self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock,
  685. run_playbook_mock,
  686. cli_input,
  687. exp_hosts_len=2,
  688. exp_hosts_to_run_on_len=2,
  689. force=False)
  690. #interactive multimaster: one more node than master
  691. @patch('ooinstall.openshift_ansible.run_main_playbook')
  692. @patch('ooinstall.openshift_ansible.load_system_facts')
  693. def test_quick_ha1(self, load_facts_mock, run_playbook_mock):
  694. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  695. run_playbook_mock.return_value = 0
  696. cli_input = self._build_input(hosts=[
  697. ('10.0.0.1', True),
  698. ('10.0.0.2', True),
  699. ('10.0.0.3', False),
  700. ('10.0.0.4', True)],
  701. ssh_user='root',
  702. variant_num=1,
  703. confirm_facts='y',
  704. master_lb=('10.0.0.5', False))
  705. self.cli_args.append("install")
  706. result = self.runner.invoke(cli.cli, self.cli_args,
  707. input=cli_input)
  708. self.assert_result(result, 0)
  709. self._verify_load_facts(load_facts_mock)
  710. self._verify_run_playbook(run_playbook_mock, 5, 5)
  711. written_config = self._read_yaml(self.config_file)
  712. self._verify_config_hosts(written_config, 5)
  713. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  714. inventory.read(os.path.join(self.work_dir, '.ansible/hosts'))
  715. self.assertEquals('False',
  716. inventory.get('nodes', '10.0.0.1 openshift_schedulable'))
  717. self.assertEquals('False',
  718. inventory.get('nodes', '10.0.0.2 openshift_schedulable'))
  719. self.assertEquals(None,
  720. inventory.get('nodes', '10.0.0.3'))
  721. self.assertEquals('False',
  722. inventory.get('nodes', '10.0.0.4 openshift_schedulable'))
  723. return
  724. #interactive multimaster: equal number masters and nodes
  725. @patch('ooinstall.openshift_ansible.run_main_playbook')
  726. @patch('ooinstall.openshift_ansible.load_system_facts')
  727. def test_quick_ha2(self, load_facts_mock, run_playbook_mock):
  728. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  729. run_playbook_mock.return_value = 0
  730. cli_input = self._build_input(hosts=[
  731. ('10.0.0.1', True),
  732. ('10.0.0.2', True),
  733. ('10.0.0.3', True)],
  734. ssh_user='root',
  735. variant_num=1,
  736. confirm_facts='y',
  737. master_lb=('10.0.0.5', False))
  738. self.cli_args.append("install")
  739. result = self.runner.invoke(cli.cli, self.cli_args,
  740. input=cli_input)
  741. self.assert_result(result, 0)
  742. self._verify_load_facts(load_facts_mock)
  743. self._verify_run_playbook(run_playbook_mock, 4, 4)
  744. written_config = self._read_yaml(self.config_file)
  745. self._verify_config_hosts(written_config, 4)
  746. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  747. inventory.read(os.path.join(self.work_dir, '.ansible/hosts'))
  748. self.assertEquals(None,
  749. inventory.get('nodes', '10.0.0.1'))
  750. self.assertEquals(None,
  751. inventory.get('nodes', '10.0.0.2'))
  752. self.assertEquals(None,
  753. inventory.get('nodes', '10.0.0.3'))
  754. return
  755. #interactive all-in-one
  756. @patch('ooinstall.openshift_ansible.run_main_playbook')
  757. @patch('ooinstall.openshift_ansible.load_system_facts')
  758. def test_all_in_one(self, load_facts_mock, run_playbook_mock):
  759. load_facts_mock.return_value = (MOCK_FACTS, 0)
  760. run_playbook_mock.return_value = 0
  761. cli_input = self._build_input(hosts=[
  762. ('10.0.0.1', True)],
  763. ssh_user='root',
  764. variant_num=1,
  765. confirm_facts='y')
  766. self.cli_args.append("install")
  767. result = self.runner.invoke(cli.cli, self.cli_args,
  768. input=cli_input)
  769. self.assert_result(result, 0)
  770. self._verify_load_facts(load_facts_mock)
  771. self._verify_run_playbook(run_playbook_mock, 1, 1)
  772. written_config = self._read_yaml(self.config_file)
  773. self._verify_config_hosts(written_config, 1)
  774. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  775. inventory.read(os.path.join(self.work_dir, '.ansible/hosts'))
  776. self.assertEquals(None,
  777. inventory.get('nodes', '10.0.0.1'))
  778. return
  779. # TODO: test with config file, attended add node
  780. # TODO: test with config file, attended new node already in config file
  781. # TODO: test with config file, attended new node already in config file, plus manually added nodes
  782. # TODO: test with config file, attended reject facts