cli_installer_tests.py 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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,too-many-lines
  4. import copy
  5. import os
  6. import ConfigParser
  7. import ooinstall.cli_installer as cli
  8. from test.fixture import OOCliFixture, SAMPLE_CONFIG, build_input, read_yaml
  9. from mock import patch
  10. MOCK_FACTS = {
  11. '10.0.0.1': {
  12. 'common': {
  13. 'ip': '10.0.0.1',
  14. 'public_ip': '10.0.0.1',
  15. 'hostname': 'master-private.example.com',
  16. 'public_hostname': 'master.example.com'
  17. }
  18. },
  19. '10.0.0.2': {
  20. 'common': {
  21. 'ip': '10.0.0.2',
  22. 'public_ip': '10.0.0.2',
  23. 'hostname': 'node1-private.example.com',
  24. 'public_hostname': 'node1.example.com'
  25. }
  26. },
  27. '10.0.0.3': {
  28. 'common': {
  29. 'ip': '10.0.0.3',
  30. 'public_ip': '10.0.0.3',
  31. 'hostname': 'node2-private.example.com',
  32. 'public_hostname': 'node2.example.com'
  33. }
  34. },
  35. '10.1.0.1': {
  36. 'common': {
  37. 'ip': '10.1.0.1',
  38. 'public_ip': '10.1.0.1',
  39. 'hostname': 'storage-private.example.com',
  40. 'public_hostname': 'storage.example.com'
  41. }
  42. },
  43. }
  44. MOCK_FACTS_QUICKHA = {
  45. '10.0.0.1': {
  46. 'common': {
  47. 'ip': '10.0.0.1',
  48. 'public_ip': '10.0.0.1',
  49. 'hostname': 'master-private.example.com',
  50. 'public_hostname': 'master.example.com'
  51. }
  52. },
  53. '10.0.0.2': {
  54. 'common': {
  55. 'ip': '10.0.0.2',
  56. 'public_ip': '10.0.0.2',
  57. 'hostname': 'node1-private.example.com',
  58. 'public_hostname': 'node1.example.com'
  59. }
  60. },
  61. '10.0.0.3': {
  62. 'common': {
  63. 'ip': '10.0.0.3',
  64. 'public_ip': '10.0.0.3',
  65. 'hostname': 'node2-private.example.com',
  66. 'public_hostname': 'node2.example.com'
  67. }
  68. },
  69. '10.0.0.4': {
  70. 'common': {
  71. 'ip': '10.0.0.4',
  72. 'public_ip': '10.0.0.4',
  73. 'hostname': 'node3-private.example.com',
  74. 'public_hostname': 'node3.example.com'
  75. }
  76. },
  77. '10.0.0.5': {
  78. 'common': {
  79. 'ip': '10.0.0.5',
  80. 'public_ip': '10.0.0.5',
  81. 'hostname': 'proxy-private.example.com',
  82. 'public_hostname': 'proxy.example.com'
  83. }
  84. },
  85. '10.1.0.1': {
  86. 'common': {
  87. 'ip': '10.1.0.1',
  88. 'public_ip': '10.1.0.1',
  89. 'hostname': 'storage-private.example.com',
  90. 'public_hostname': 'storage.example.com'
  91. }
  92. },
  93. }
  94. # Missing connect_to on some hosts:
  95. BAD_CONFIG = """
  96. variant: %s
  97. version: v2
  98. deployment:
  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. roles:
  107. - master
  108. - node
  109. - ip: 10.0.0.2
  110. hostname: node1-private.example.com
  111. public_ip: 24.222.0.2
  112. public_hostname: node1.example.com
  113. roles:
  114. - node
  115. - connect_to: 10.0.0.3
  116. ip: 10.0.0.3
  117. hostname: node2-private.example.com
  118. public_ip: 24.222.0.3
  119. public_hostname: node2.example.com
  120. roles:
  121. - node
  122. roles:
  123. master:
  124. node:
  125. """
  126. QUICKHA_CONFIG = """
  127. variant: %s
  128. version: v2
  129. deployment:
  130. ansible_ssh_user: root
  131. hosts:
  132. - connect_to: 10.0.0.1
  133. ip: 10.0.0.1
  134. hostname: master-private.example.com
  135. public_ip: 24.222.0.1
  136. public_hostname: master.example.com
  137. roles:
  138. - master
  139. - node
  140. - connect_to: 10.0.0.2
  141. ip: 10.0.0.2
  142. hostname: node1-private.example.com
  143. public_ip: 24.222.0.2
  144. public_hostname: node1.example.com
  145. roles:
  146. - master
  147. - node
  148. - connect_to: 10.0.0.3
  149. ip: 10.0.0.3
  150. hostname: node2-private.example.com
  151. public_ip: 24.222.0.3
  152. public_hostname: node2.example.com
  153. roles:
  154. - master
  155. - node
  156. - connect_to: 10.0.0.4
  157. ip: 10.0.0.4
  158. hostname: node3-private.example.com
  159. public_ip: 24.222.0.4
  160. public_hostname: node3.example.com
  161. roles:
  162. - node
  163. - connect_to: 10.0.0.5
  164. ip: 10.0.0.5
  165. hostname: proxy-private.example.com
  166. public_ip: 24.222.0.5
  167. public_hostname: proxy.example.com
  168. roles:
  169. - master_lb
  170. - connect_to: 10.1.0.1
  171. ip: 10.1.0.1
  172. hostname: storage-private.example.com
  173. public_ip: 24.222.0.6
  174. public_hostname: storage.example.com
  175. roles:
  176. - storage
  177. roles:
  178. master:
  179. master_lb:
  180. node:
  181. storage:
  182. """
  183. QUICKHA_2_MASTER_CONFIG = """
  184. variant: %s
  185. version: v2
  186. deployment:
  187. ansible_ssh_user: root
  188. hosts:
  189. - connect_to: 10.0.0.1
  190. ip: 10.0.0.1
  191. hostname: master-private.example.com
  192. public_ip: 24.222.0.1
  193. public_hostname: master.example.com
  194. roles:
  195. - master
  196. - node
  197. - connect_to: 10.0.0.2
  198. ip: 10.0.0.2
  199. hostname: node1-private.example.com
  200. public_ip: 24.222.0.2
  201. public_hostname: node1.example.com
  202. roles:
  203. - master
  204. - node
  205. - connect_to: 10.0.0.4
  206. ip: 10.0.0.4
  207. hostname: node3-private.example.com
  208. public_ip: 24.222.0.4
  209. public_hostname: node3.example.com
  210. roles:
  211. - node
  212. - connect_to: 10.0.0.5
  213. ip: 10.0.0.5
  214. hostname: proxy-private.example.com
  215. public_ip: 24.222.0.5
  216. public_hostname: proxy.example.com
  217. roles:
  218. - master_lb
  219. - connect_to: 10.1.0.1
  220. ip: 10.1.0.1
  221. hostname: storage-private.example.com
  222. public_ip: 24.222.0.6
  223. public_hostname: storage.example.com
  224. roles:
  225. - storage
  226. roles:
  227. master:
  228. master_lb:
  229. node:
  230. storage:
  231. """
  232. QUICKHA_CONFIG_REUSED_LB = """
  233. variant: %s
  234. version: v2
  235. deployment:
  236. ansible_ssh_user: root
  237. hosts:
  238. - connect_to: 10.0.0.1
  239. ip: 10.0.0.1
  240. hostname: master-private.example.com
  241. public_ip: 24.222.0.1
  242. public_hostname: master.example.com
  243. roles:
  244. - master
  245. - node
  246. - connect_to: 10.0.0.2
  247. ip: 10.0.0.2
  248. hostname: node1-private.example.com
  249. public_ip: 24.222.0.2
  250. public_hostname: node1.example.com
  251. roles:
  252. - master
  253. - node
  254. - master_lb
  255. - connect_to: 10.0.0.3
  256. ip: 10.0.0.3
  257. hostname: node2-private.example.com
  258. public_ip: 24.222.0.3
  259. public_hostname: node2.example.com
  260. roles:
  261. - master
  262. - node
  263. - connect_to: 10.1.0.1
  264. ip: 10.1.0.1
  265. hostname: storage-private.example.com
  266. public_ip: 24.222.0.6
  267. public_hostname: storage.example.com
  268. roles:
  269. - storage
  270. roles:
  271. master:
  272. node:
  273. storage:
  274. """
  275. QUICKHA_CONFIG_NO_LB = """
  276. variant: %s
  277. version: v2
  278. deployment:
  279. ansible_ssh_user: root
  280. hosts:
  281. - connect_to: 10.0.0.1
  282. ip: 10.0.0.1
  283. hostname: master-private.example.com
  284. public_ip: 24.222.0.1
  285. public_hostname: master.example.com
  286. roles:
  287. - master
  288. - node
  289. - connect_to: 10.0.0.2
  290. ip: 10.0.0.2
  291. hostname: node1-private.example.com
  292. public_ip: 24.222.0.2
  293. public_hostname: node1.example.com
  294. roles:
  295. - master
  296. - node
  297. - connect_to: 10.0.0.3
  298. ip: 10.0.0.3
  299. hostname: node2-private.example.com
  300. public_ip: 24.222.0.3
  301. public_hostname: node2.example.com
  302. roles:
  303. - master
  304. - node
  305. - connect_to: 10.1.0.1
  306. ip: 10.1.0.1
  307. hostname: storage-private.example.com
  308. public_ip: 24.222.0.6
  309. public_hostname: storage.example.com
  310. roles:
  311. - storage
  312. roles:
  313. master:
  314. node:
  315. storage:
  316. """
  317. QUICKHA_CONFIG_PRECONFIGURED_LB = """
  318. variant: %s
  319. version: v2
  320. deployment:
  321. ansible_ssh_user: root
  322. hosts:
  323. - connect_to: 10.0.0.1
  324. ip: 10.0.0.1
  325. hostname: master-private.example.com
  326. public_ip: 24.222.0.1
  327. public_hostname: master.example.com
  328. roles:
  329. - master
  330. - node
  331. - connect_to: 10.0.0.2
  332. ip: 10.0.0.2
  333. hostname: node1-private.example.com
  334. public_ip: 24.222.0.2
  335. public_hostname: node1.example.com
  336. roles:
  337. - master
  338. - node
  339. - connect_to: 10.0.0.3
  340. ip: 10.0.0.3
  341. hostname: node2-private.example.com
  342. public_ip: 24.222.0.3
  343. public_hostname: node2.example.com
  344. roles:
  345. - master
  346. - node
  347. - connect_to: 10.0.0.4
  348. ip: 10.0.0.4
  349. hostname: node3-private.example.com
  350. public_ip: 24.222.0.4
  351. public_hostname: node3.example.com
  352. roles:
  353. - node
  354. - connect_to: proxy-private.example.com
  355. hostname: proxy-private.example.com
  356. public_hostname: proxy.example.com
  357. preconfigured: true
  358. roles:
  359. - master_lb
  360. - connect_to: 10.1.0.1
  361. ip: 10.1.0.1
  362. hostname: storage-private.example.com
  363. public_ip: 24.222.0.6
  364. public_hostname: storage.example.com
  365. roles:
  366. - storage
  367. roles:
  368. master:
  369. master_lb:
  370. node:
  371. storage:
  372. """
  373. class UnattendedCliTests(OOCliFixture):
  374. def setUp(self):
  375. OOCliFixture.setUp(self)
  376. self.cli_args.append("-u")
  377. # unattended with config file and all installed hosts (without --force)
  378. @patch('ooinstall.openshift_ansible.run_main_playbook')
  379. @patch('ooinstall.openshift_ansible.load_system_facts')
  380. def test_get_hosts_to_run_on1(self, load_facts_mock, run_playbook_mock):
  381. mock_facts = copy.deepcopy(MOCK_FACTS)
  382. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  383. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  384. mock_facts['10.0.0.3']['common']['version'] = "3.0.0"
  385. load_facts_mock.return_value = (mock_facts, 0)
  386. run_playbook_mock.return_value = 0
  387. config_file = self.write_config(os.path.join(self.work_dir,
  388. 'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')
  389. self.cli_args.extend(["-c", config_file, "install"])
  390. result = self.runner.invoke(cli.cli, self.cli_args)
  391. if result.exception is None or result.exit_code != 1:
  392. print "Exit code: %s" % result.exit_code
  393. self.fail("Unexpected CLI return")
  394. # unattended with config file and all installed hosts (with --force)
  395. @patch('ooinstall.openshift_ansible.run_main_playbook')
  396. @patch('ooinstall.openshift_ansible.load_system_facts')
  397. def test_get_hosts_to_run_on2(self, load_facts_mock, run_playbook_mock):
  398. mock_facts = copy.deepcopy(MOCK_FACTS)
  399. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  400. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  401. mock_facts['10.0.0.3']['common']['version'] = "3.0.0"
  402. self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
  403. cli_input=None,
  404. exp_hosts_len=3,
  405. exp_hosts_to_run_on_len=3,
  406. force=True)
  407. # unattended with config file and no installed hosts (without --force)
  408. @patch('ooinstall.openshift_ansible.run_main_playbook')
  409. @patch('ooinstall.openshift_ansible.load_system_facts')
  410. def test_get_hosts_to_run_on3(self, load_facts_mock, run_playbook_mock):
  411. load_facts_mock.return_value = (MOCK_FACTS, 0)
  412. run_playbook_mock.return_value = 0
  413. self._verify_get_hosts_to_run_on(MOCK_FACTS, load_facts_mock, run_playbook_mock,
  414. cli_input=None,
  415. exp_hosts_len=3,
  416. exp_hosts_to_run_on_len=3,
  417. force=False)
  418. # unattended with config file and no installed hosts (with --force)
  419. @patch('ooinstall.openshift_ansible.run_main_playbook')
  420. @patch('ooinstall.openshift_ansible.load_system_facts')
  421. def test_get_hosts_to_run_on4(self, load_facts_mock, run_playbook_mock):
  422. load_facts_mock.return_value = (MOCK_FACTS, 0)
  423. run_playbook_mock.return_value = 0
  424. self._verify_get_hosts_to_run_on(MOCK_FACTS, load_facts_mock, run_playbook_mock,
  425. cli_input=None,
  426. exp_hosts_len=3,
  427. exp_hosts_to_run_on_len=3,
  428. force=True)
  429. # unattended with config file and some installed some uninstalled hosts (without --force)
  430. @patch('ooinstall.openshift_ansible.run_main_playbook')
  431. @patch('ooinstall.openshift_ansible.load_system_facts')
  432. def test_get_hosts_to_run_on5(self, load_facts_mock, run_playbook_mock):
  433. mock_facts = copy.deepcopy(MOCK_FACTS)
  434. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  435. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  436. self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
  437. cli_input=None,
  438. exp_hosts_len=3,
  439. exp_hosts_to_run_on_len=2,
  440. force=False)
  441. # unattended with config file and some installed some uninstalled hosts (with --force)
  442. @patch('ooinstall.openshift_ansible.run_main_playbook')
  443. @patch('ooinstall.openshift_ansible.load_system_facts')
  444. def test_get_hosts_to_run_on6(self, load_facts_mock, run_playbook_mock):
  445. mock_facts = copy.deepcopy(MOCK_FACTS)
  446. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  447. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  448. self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
  449. cli_input=None,
  450. exp_hosts_len=3,
  451. exp_hosts_to_run_on_len=3,
  452. force=True)
  453. @patch('ooinstall.openshift_ansible.run_main_playbook')
  454. @patch('ooinstall.openshift_ansible.load_system_facts')
  455. def test_cfg_full_run(self, load_facts_mock, run_playbook_mock):
  456. load_facts_mock.return_value = (MOCK_FACTS, 0)
  457. run_playbook_mock.return_value = 0
  458. config_file = self.write_config(os.path.join(self.work_dir,
  459. 'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')
  460. self.cli_args.extend(["-c", config_file, "install"])
  461. result = self.runner.invoke(cli.cli, self.cli_args)
  462. self.assert_result(result, 0)
  463. load_facts_args = load_facts_mock.call_args[0]
  464. self.assertEquals(os.path.join(self.work_dir, "hosts"),
  465. load_facts_args[0])
  466. self.assertEquals(os.path.join(self.work_dir,
  467. "playbooks/byo/openshift_facts.yml"), load_facts_args[1])
  468. env_vars = load_facts_args[2]
  469. self.assertEquals(os.path.join(self.work_dir,
  470. '.ansible/callback_facts.yaml'),
  471. env_vars['OO_INSTALL_CALLBACK_FACTS_YAML'])
  472. self.assertEqual('/tmp/ansible.log', env_vars['ANSIBLE_LOG_PATH'])
  473. # If user running test has rpm installed, this might be set to default:
  474. self.assertTrue('ANSIBLE_CONFIG' not in env_vars or
  475. env_vars['ANSIBLE_CONFIG'] == cli.DEFAULT_ANSIBLE_CONFIG)
  476. # Make sure we ran on the expected masters and nodes:
  477. hosts = run_playbook_mock.call_args[0][1]
  478. hosts_to_run_on = run_playbook_mock.call_args[0][2]
  479. self.assertEquals(3, len(hosts))
  480. self.assertEquals(3, len(hosts_to_run_on))
  481. @patch('ooinstall.openshift_ansible.run_main_playbook')
  482. @patch('ooinstall.openshift_ansible.load_system_facts')
  483. def test_inventory_write(self, load_facts_mock, run_playbook_mock):
  484. merged_config = SAMPLE_CONFIG % 'openshift-enterprise'
  485. load_facts_mock.return_value = (MOCK_FACTS, 0)
  486. run_playbook_mock.return_value = 0
  487. config_file = self.write_config(os.path.join(self.work_dir,
  488. 'ooinstall.conf'), merged_config)
  489. self.cli_args.extend(["-c", config_file, "install"])
  490. result = self.runner.invoke(cli.cli, self.cli_args)
  491. self.assert_result(result, 0)
  492. # Check the inventory file looks as we would expect:
  493. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  494. inventory.read(os.path.join(self.work_dir, 'hosts'))
  495. self.assertEquals('root',
  496. inventory.get('OSEv3:vars', 'ansible_ssh_user'))
  497. self.assertEquals('openshift-enterprise',
  498. inventory.get('OSEv3:vars', 'deployment_type'))
  499. # Check the masters:
  500. self.assertEquals(1, len(inventory.items('masters')))
  501. self.assertEquals(3, len(inventory.items('nodes')))
  502. for item in inventory.items('masters'):
  503. # ansible host lines do NOT parse nicely:
  504. master_line = item[0]
  505. if item[1] is not None:
  506. master_line = "%s=%s" % (master_line, item[1])
  507. self.assertTrue('openshift_ip' in master_line)
  508. self.assertTrue('openshift_public_ip' in master_line)
  509. self.assertTrue('openshift_hostname' in master_line)
  510. self.assertTrue('openshift_public_hostname' in master_line)
  511. @patch('ooinstall.openshift_ansible.run_main_playbook')
  512. @patch('ooinstall.openshift_ansible.load_system_facts')
  513. def test_variant_version_latest_assumed(self, load_facts_mock,
  514. run_playbook_mock):
  515. load_facts_mock.return_value = (MOCK_FACTS, 0)
  516. run_playbook_mock.return_value = 0
  517. config_file = self.write_config(os.path.join(self.work_dir,
  518. 'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')
  519. self.cli_args.extend(["-c", config_file, "install"])
  520. result = self.runner.invoke(cli.cli, self.cli_args)
  521. self.assert_result(result, 0)
  522. written_config = read_yaml(config_file)
  523. self.assertEquals('openshift-enterprise', written_config['variant'])
  524. # We didn't specify a version so the latest should have been assumed,
  525. # and written to disk:
  526. self.assertEquals('3.3', written_config['variant_version'])
  527. # Make sure the correct value was passed to ansible:
  528. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  529. inventory.read(os.path.join(self.work_dir, 'hosts'))
  530. self.assertEquals('openshift-enterprise',
  531. inventory.get('OSEv3:vars', 'deployment_type'))
  532. @patch('ooinstall.openshift_ansible.run_main_playbook')
  533. @patch('ooinstall.openshift_ansible.load_system_facts')
  534. def test_variant_version_preserved(self, load_facts_mock,
  535. run_playbook_mock):
  536. load_facts_mock.return_value = (MOCK_FACTS, 0)
  537. run_playbook_mock.return_value = 0
  538. config = SAMPLE_CONFIG % 'openshift-enterprise'
  539. config = '%s\n%s' % (config, 'variant_version: 3.3')
  540. config_file = self.write_config(os.path.join(self.work_dir,
  541. 'ooinstall.conf'), config)
  542. self.cli_args.extend(["-c", config_file, "install"])
  543. result = self.runner.invoke(cli.cli, self.cli_args)
  544. self.assert_result(result, 0)
  545. written_config = read_yaml(config_file)
  546. self.assertEquals('openshift-enterprise', written_config['variant'])
  547. # Make sure our older version was preserved:
  548. # and written to disk:
  549. self.assertEquals('3.3', written_config['variant_version'])
  550. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  551. inventory.read(os.path.join(self.work_dir, 'hosts'))
  552. self.assertEquals('openshift-enterprise',
  553. inventory.get('OSEv3:vars', 'deployment_type'))
  554. @patch('ooinstall.openshift_ansible.run_ansible')
  555. @patch('ooinstall.openshift_ansible.load_system_facts')
  556. def test_no_ansible_config_specified(self, load_facts_mock, run_ansible_mock):
  557. load_facts_mock.return_value = (MOCK_FACTS, 0)
  558. run_ansible_mock.return_value = 0
  559. config = SAMPLE_CONFIG % 'openshift-enterprise'
  560. self._ansible_config_test(load_facts_mock, run_ansible_mock,
  561. config, None, None)
  562. @patch('ooinstall.openshift_ansible.run_ansible')
  563. @patch('ooinstall.openshift_ansible.load_system_facts')
  564. def test_ansible_config_specified_cli(self, load_facts_mock, run_ansible_mock):
  565. load_facts_mock.return_value = (MOCK_FACTS, 0)
  566. run_ansible_mock.return_value = 0
  567. config = SAMPLE_CONFIG % 'openshift-enterprise'
  568. ansible_config = os.path.join(self.work_dir, 'ansible.cfg')
  569. self._ansible_config_test(load_facts_mock, run_ansible_mock,
  570. config, ansible_config, ansible_config)
  571. @patch('ooinstall.openshift_ansible.run_ansible')
  572. @patch('ooinstall.openshift_ansible.load_system_facts')
  573. def test_ansible_config_specified_in_installer_config(self,
  574. load_facts_mock, run_ansible_mock):
  575. load_facts_mock.return_value = (MOCK_FACTS, 0)
  576. run_ansible_mock.return_value = 0
  577. ansible_config = os.path.join(self.work_dir, 'ansible.cfg')
  578. config = SAMPLE_CONFIG % 'openshift-enterprise'
  579. config = "%s\nansible_config: %s" % (config, ansible_config)
  580. self._ansible_config_test(load_facts_mock, run_ansible_mock,
  581. config, None, ansible_config)
  582. #pylint: disable=too-many-arguments
  583. # This method allows for drastically simpler tests to write, and the args
  584. # are all useful.
  585. def _ansible_config_test(self, load_facts_mock, run_ansible_mock,
  586. installer_config, ansible_config_cli=None, expected_result=None):
  587. """
  588. Utility method for testing the ways you can specify the ansible config.
  589. """
  590. load_facts_mock.return_value = (MOCK_FACTS, 0)
  591. run_ansible_mock.return_value = 0
  592. config_file = self.write_config(os.path.join(self.work_dir,
  593. 'ooinstall.conf'), installer_config)
  594. self.cli_args.extend(["-c", config_file])
  595. if ansible_config_cli:
  596. self.cli_args.extend(["--ansible-config", ansible_config_cli])
  597. self.cli_args.append("install")
  598. result = self.runner.invoke(cli.cli, self.cli_args)
  599. self.assert_result(result, 0)
  600. # Test the env vars for facts playbook:
  601. facts_env_vars = load_facts_mock.call_args[0][2]
  602. if expected_result:
  603. self.assertEquals(expected_result, facts_env_vars['ANSIBLE_CONFIG'])
  604. else:
  605. # If user running test has rpm installed, this might be set to default:
  606. self.assertTrue('ANSIBLE_CONFIG' not in facts_env_vars or
  607. facts_env_vars['ANSIBLE_CONFIG'] == cli.DEFAULT_ANSIBLE_CONFIG)
  608. # Test the env vars for main playbook:
  609. env_vars = run_ansible_mock.call_args[0][2]
  610. if expected_result:
  611. self.assertEquals(expected_result, env_vars['ANSIBLE_CONFIG'])
  612. else:
  613. # If user running test has rpm installed, this might be set to default:
  614. self.assertTrue('ANSIBLE_CONFIG' not in env_vars or
  615. env_vars['ANSIBLE_CONFIG'] == cli.DEFAULT_ANSIBLE_CONFIG)
  616. # unattended with bad config file and no installed hosts (without --force)
  617. @patch('ooinstall.openshift_ansible.run_main_playbook')
  618. @patch('ooinstall.openshift_ansible.load_system_facts')
  619. def test_bad_config(self, load_facts_mock, run_playbook_mock):
  620. load_facts_mock.return_value = (MOCK_FACTS, 0)
  621. run_playbook_mock.return_value = 0
  622. config_file = self.write_config(os.path.join(self.work_dir,
  623. 'ooinstall.conf'), BAD_CONFIG % 'openshift-enterprise')
  624. self.cli_args.extend(["-c", config_file, "install"])
  625. result = self.runner.invoke(cli.cli, self.cli_args)
  626. self.assertEquals(1, result.exit_code)
  627. self.assertTrue("You must specify either an ip or hostname"
  628. in result.output)
  629. #unattended with three masters, one node, and haproxy
  630. @patch('ooinstall.openshift_ansible.run_main_playbook')
  631. @patch('ooinstall.openshift_ansible.load_system_facts')
  632. def test_quick_ha_full_run(self, load_facts_mock, run_playbook_mock):
  633. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  634. run_playbook_mock.return_value = 0
  635. config_file = self.write_config(os.path.join(self.work_dir,
  636. 'ooinstall.conf'), QUICKHA_CONFIG % 'openshift-enterprise')
  637. self.cli_args.extend(["-c", config_file, "install"])
  638. result = self.runner.invoke(cli.cli, self.cli_args)
  639. self.assert_result(result, 0)
  640. # Make sure we ran on the expected masters and nodes:
  641. hosts = run_playbook_mock.call_args[0][1]
  642. hosts_to_run_on = run_playbook_mock.call_args[0][2]
  643. self.assertEquals(6, len(hosts))
  644. self.assertEquals(6, len(hosts_to_run_on))
  645. #unattended with two masters, one node, and haproxy
  646. @patch('ooinstall.openshift_ansible.run_main_playbook')
  647. @patch('ooinstall.openshift_ansible.load_system_facts')
  648. def test_quick_ha_only_2_masters(self, load_facts_mock, run_playbook_mock):
  649. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  650. run_playbook_mock.return_value = 0
  651. config_file = self.write_config(os.path.join(self.work_dir,
  652. 'ooinstall.conf'), QUICKHA_2_MASTER_CONFIG % 'openshift-enterprise')
  653. self.cli_args.extend(["-c", config_file, "install"])
  654. result = self.runner.invoke(cli.cli, self.cli_args)
  655. # This is an invalid config:
  656. self.assert_result(result, 1)
  657. self.assertTrue("A minimum of 3 masters are required" in result.output)
  658. #unattended with three masters, one node, but no load balancer specified:
  659. @patch('ooinstall.openshift_ansible.run_main_playbook')
  660. @patch('ooinstall.openshift_ansible.load_system_facts')
  661. def test_quick_ha_no_lb(self, load_facts_mock, run_playbook_mock):
  662. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  663. run_playbook_mock.return_value = 0
  664. config_file = self.write_config(os.path.join(self.work_dir,
  665. 'ooinstall.conf'), QUICKHA_CONFIG_NO_LB % 'openshift-enterprise')
  666. self.cli_args.extend(["-c", config_file, "install"])
  667. result = self.runner.invoke(cli.cli, self.cli_args)
  668. # This is not a valid input:
  669. self.assert_result(result, 1)
  670. self.assertTrue('No master load balancer specified in config' in result.output)
  671. #unattended with three masters, one node, and one of the masters reused as load balancer:
  672. @patch('ooinstall.openshift_ansible.run_main_playbook')
  673. @patch('ooinstall.openshift_ansible.load_system_facts')
  674. def test_quick_ha_reused_lb(self, load_facts_mock, run_playbook_mock):
  675. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  676. run_playbook_mock.return_value = 0
  677. config_file = self.write_config(os.path.join(self.work_dir,
  678. 'ooinstall.conf'), QUICKHA_CONFIG_REUSED_LB % 'openshift-enterprise')
  679. self.cli_args.extend(["-c", config_file, "install"])
  680. result = self.runner.invoke(cli.cli, self.cli_args)
  681. # This is not a valid configuration:
  682. self.assert_result(result, 1)
  683. #unattended with preconfigured lb
  684. @patch('ooinstall.openshift_ansible.run_main_playbook')
  685. @patch('ooinstall.openshift_ansible.load_system_facts')
  686. def test_quick_ha_preconfigured_lb(self, load_facts_mock, run_playbook_mock):
  687. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  688. run_playbook_mock.return_value = 0
  689. config_file = self.write_config(os.path.join(self.work_dir,
  690. 'ooinstall.conf'), QUICKHA_CONFIG_PRECONFIGURED_LB % 'openshift-enterprise')
  691. self.cli_args.extend(["-c", config_file, "install"])
  692. result = self.runner.invoke(cli.cli, self.cli_args)
  693. self.assert_result(result, 0)
  694. # Make sure we ran on the expected masters and nodes:
  695. hosts = run_playbook_mock.call_args[0][1]
  696. hosts_to_run_on = run_playbook_mock.call_args[0][2]
  697. self.assertEquals(6, len(hosts))
  698. self.assertEquals(6, len(hosts_to_run_on))
  699. class AttendedCliTests(OOCliFixture):
  700. def setUp(self):
  701. OOCliFixture.setUp(self)
  702. # Doesn't exist but keeps us from reading the local users config:
  703. self.config_file = os.path.join(self.work_dir, 'config.yml')
  704. self.cli_args.extend(["-c", self.config_file])
  705. @patch('ooinstall.openshift_ansible.run_main_playbook')
  706. @patch('ooinstall.openshift_ansible.load_system_facts')
  707. def test_full_run(self, load_facts_mock, run_playbook_mock):
  708. load_facts_mock.return_value = (MOCK_FACTS, 0)
  709. run_playbook_mock.return_value = 0
  710. cli_input = build_input(hosts=[
  711. ('10.0.0.1', True, False),
  712. ('10.0.0.2', False, False),
  713. ('10.0.0.3', False, False)],
  714. ssh_user='root',
  715. variant_num=1,
  716. confirm_facts='y',
  717. storage='10.1.0.1',)
  718. self.cli_args.append("install")
  719. result = self.runner.invoke(cli.cli, self.cli_args,
  720. input=cli_input)
  721. self.assert_result(result, 0)
  722. self._verify_load_facts(load_facts_mock)
  723. self._verify_run_playbook(run_playbook_mock, 4, 4)
  724. written_config = read_yaml(self.config_file)
  725. self._verify_config_hosts(written_config, 4)
  726. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  727. inventory.read(os.path.join(self.work_dir, 'hosts'))
  728. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.1',
  729. 'openshift_schedulable=False')
  730. self.assert_inventory_host_var_unset(inventory, 'nodes', '10.0.0.2',
  731. 'openshift_schedulable=True')
  732. self.assert_inventory_host_var_unset(inventory, 'nodes', '10.0.0.3',
  733. 'openshift_schedulable=True')
  734. # interactive with config file and some installed some uninstalled hosts
  735. @patch('ooinstall.openshift_ansible.run_main_playbook')
  736. @patch('ooinstall.openshift_ansible.load_system_facts')
  737. def test_add_nodes(self, load_facts_mock, run_playbook_mock):
  738. # Modify the mock facts to return a version indicating OpenShift
  739. # is already installed on our master, and the first node.
  740. mock_facts = copy.deepcopy(MOCK_FACTS)
  741. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  742. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  743. load_facts_mock.return_value = (mock_facts, 0)
  744. run_playbook_mock.return_value = 0
  745. cli_input = build_input(hosts=[
  746. ('10.0.0.1', True, False),
  747. ('10.0.0.2', False, False),
  748. ],
  749. add_nodes=[('10.0.0.3', False, False)],
  750. ssh_user='root',
  751. variant_num=1,
  752. confirm_facts='y',
  753. storage='10.0.0.1',)
  754. self.cli_args.append("install")
  755. result = self.runner.invoke(cli.cli,
  756. self.cli_args,
  757. input=cli_input)
  758. self.assert_result(result, 0)
  759. self._verify_load_facts(load_facts_mock)
  760. self._verify_run_playbook(run_playbook_mock, 3, 2)
  761. written_config = read_yaml(self.config_file)
  762. self._verify_config_hosts(written_config, 3)
  763. @patch('ooinstall.openshift_ansible.run_main_playbook')
  764. @patch('ooinstall.openshift_ansible.load_system_facts')
  765. def test_fresh_install_with_config(self, load_facts_mock, run_playbook_mock):
  766. load_facts_mock.return_value = (MOCK_FACTS, 0)
  767. run_playbook_mock.return_value = 0
  768. config_file = self.write_config(os.path.join(self.work_dir,
  769. 'ooinstall.conf'),
  770. SAMPLE_CONFIG % 'openshift-enterprise')
  771. cli_input = build_input(confirm_facts='y')
  772. self.cli_args.extend(["-c", config_file])
  773. self.cli_args.append("install")
  774. result = self.runner.invoke(cli.cli,
  775. self.cli_args,
  776. input=cli_input)
  777. self.assert_result(result, 0)
  778. self._verify_load_facts(load_facts_mock)
  779. self._verify_run_playbook(run_playbook_mock, 3, 3)
  780. written_config = read_yaml(config_file)
  781. self._verify_config_hosts(written_config, 3)
  782. #interactive with config file and all installed hosts
  783. @patch('ooinstall.openshift_ansible.run_main_playbook')
  784. @patch('ooinstall.openshift_ansible.load_system_facts')
  785. def test_get_hosts_to_run_on(self, load_facts_mock, run_playbook_mock):
  786. mock_facts = copy.deepcopy(MOCK_FACTS)
  787. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  788. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  789. cli_input = build_input(hosts=[
  790. ('10.0.0.1', True, False),
  791. ],
  792. add_nodes=[('10.0.0.2', False, False)],
  793. ssh_user='root',
  794. variant_num=1,
  795. schedulable_masters_ok=True,
  796. confirm_facts='y',
  797. storage='10.0.0.1',)
  798. self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock,
  799. run_playbook_mock,
  800. cli_input,
  801. exp_hosts_len=2,
  802. exp_hosts_to_run_on_len=2,
  803. force=False)
  804. #interactive multimaster: one more node than master
  805. @patch('ooinstall.openshift_ansible.run_main_playbook')
  806. @patch('ooinstall.openshift_ansible.load_system_facts')
  807. def test_ha_dedicated_node(self, load_facts_mock, run_playbook_mock):
  808. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  809. run_playbook_mock.return_value = 0
  810. cli_input = build_input(hosts=[
  811. ('10.0.0.1', True, False),
  812. ('10.0.0.2', True, False),
  813. ('10.0.0.3', True, False),
  814. ('10.0.0.4', False, False)],
  815. ssh_user='root',
  816. variant_num=1,
  817. confirm_facts='y',
  818. master_lb=('10.0.0.5', False),
  819. storage='10.1.0.1',)
  820. self.cli_args.append("install")
  821. result = self.runner.invoke(cli.cli, self.cli_args,
  822. input=cli_input)
  823. self.assert_result(result, 0)
  824. self._verify_load_facts(load_facts_mock)
  825. self._verify_run_playbook(run_playbook_mock, 6, 6)
  826. written_config = read_yaml(self.config_file)
  827. self._verify_config_hosts(written_config, 6)
  828. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  829. inventory.read(os.path.join(self.work_dir, 'hosts'))
  830. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.1',
  831. 'openshift_schedulable=False')
  832. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.2',
  833. 'openshift_schedulable=False')
  834. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.3',
  835. 'openshift_schedulable=False')
  836. self.assert_inventory_host_var_unset(inventory, 'nodes', '10.0.0.4',
  837. 'openshift_schedulable=True')
  838. self.assertTrue(inventory.has_section('etcd'))
  839. self.assertEquals(3, len(inventory.items('etcd')))
  840. #interactive multimaster: identical masters and nodes
  841. @patch('ooinstall.openshift_ansible.run_main_playbook')
  842. @patch('ooinstall.openshift_ansible.load_system_facts')
  843. def test_ha_no_dedicated_nodes(self, load_facts_mock, run_playbook_mock):
  844. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  845. run_playbook_mock.return_value = 0
  846. cli_input = build_input(hosts=[
  847. ('10.0.0.1', True, False),
  848. ('10.0.0.2', True, False),
  849. ('10.0.0.3', True, False)],
  850. ssh_user='root',
  851. variant_num=1,
  852. confirm_facts='y',
  853. master_lb=('10.0.0.5', False),
  854. storage='10.1.0.1',)
  855. self.cli_args.append("install")
  856. result = self.runner.invoke(cli.cli, self.cli_args,
  857. input=cli_input)
  858. self.assert_result(result, 0)
  859. self._verify_load_facts(load_facts_mock)
  860. self._verify_run_playbook(run_playbook_mock, 5, 5)
  861. written_config = read_yaml(self.config_file)
  862. self._verify_config_hosts(written_config, 5)
  863. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  864. inventory.read(os.path.join(self.work_dir, 'hosts'))
  865. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.1',
  866. 'openshift_schedulable=True')
  867. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.2',
  868. 'openshift_schedulable=True')
  869. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.3',
  870. 'openshift_schedulable=True')
  871. # Checks the inventory (as a ConfigParser) for the given host, host
  872. # variable, and expected value.
  873. def assert_inventory_host_var(self, inventory, section, host, variable):
  874. # Config parser splits on the first "=", so we end up with:
  875. # 'hostname key1' -> 'val1 key2=val2 key3=val3'
  876. #
  877. # Convert to something easier to test:
  878. for (a, b) in inventory.items(section):
  879. full_line = "%s=%s" % (a, b)
  880. tokens = full_line.split()
  881. if tokens[0] == host:
  882. found = False
  883. for token in tokens:
  884. if token == variable:
  885. found = True
  886. continue
  887. self.assertTrue("Unable to find %s in line: %s" %
  888. (variable, full_line), found)
  889. return
  890. self.fail("unable to find host %s in inventory" % host)
  891. def assert_inventory_host_var_unset(self, inventory, section, host, variable):
  892. # Config parser splits on the first "=", so we end up with:
  893. # 'hostname key1' -> 'val1 key2=val2 key3=val3'
  894. #
  895. # Convert to something easier to test:
  896. for (a, b) in inventory.items(section):
  897. full_line = "%s=%s" % (a, b)
  898. tokens = full_line.split()
  899. if tokens[0] == host:
  900. self.assertFalse(("%s=" % variable) in full_line,
  901. msg='%s host variable was set: %s' %
  902. (variable, full_line))
  903. return
  904. self.fail("unable to find host %s in inventory" % host)
  905. #interactive multimaster: attempting to use a master as the load balancer should fail:
  906. @patch('ooinstall.openshift_ansible.run_main_playbook')
  907. @patch('ooinstall.openshift_ansible.load_system_facts')
  908. def test_ha_reuse_master_as_lb(self, load_facts_mock, run_playbook_mock):
  909. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  910. run_playbook_mock.return_value = 0
  911. cli_input = build_input(hosts=[
  912. ('10.0.0.1', True, False),
  913. ('10.0.0.2', True, False),
  914. ('10.0.0.3', False, False),
  915. ('10.0.0.4', True, False)],
  916. ssh_user='root',
  917. variant_num=1,
  918. confirm_facts='y',
  919. master_lb=(['10.0.0.2', '10.0.0.5'], False),
  920. storage='10.1.0.1')
  921. self.cli_args.append("install")
  922. result = self.runner.invoke(cli.cli, self.cli_args,
  923. input=cli_input)
  924. self.assert_result(result, 0)
  925. #interactive all-in-one
  926. @patch('ooinstall.openshift_ansible.run_main_playbook')
  927. @patch('ooinstall.openshift_ansible.load_system_facts')
  928. def test_all_in_one(self, load_facts_mock, run_playbook_mock):
  929. load_facts_mock.return_value = (MOCK_FACTS, 0)
  930. run_playbook_mock.return_value = 0
  931. cli_input = build_input(hosts=[
  932. ('10.0.0.1', True, False)],
  933. ssh_user='root',
  934. variant_num=1,
  935. confirm_facts='y',
  936. storage='10.0.0.1')
  937. self.cli_args.append("install")
  938. result = self.runner.invoke(cli.cli, self.cli_args,
  939. input=cli_input)
  940. self.assert_result(result, 0)
  941. self._verify_load_facts(load_facts_mock)
  942. self._verify_run_playbook(run_playbook_mock, 1, 1)
  943. written_config = read_yaml(self.config_file)
  944. self._verify_config_hosts(written_config, 1)
  945. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  946. inventory.read(os.path.join(self.work_dir, 'hosts'))
  947. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.1',
  948. 'openshift_schedulable=True')
  949. @patch('ooinstall.openshift_ansible.run_main_playbook')
  950. @patch('ooinstall.openshift_ansible.load_system_facts')
  951. def test_gen_inventory(self, load_facts_mock, run_playbook_mock):
  952. load_facts_mock.return_value = (MOCK_FACTS, 0)
  953. run_playbook_mock.return_value = 0
  954. cli_input = build_input(hosts=[
  955. ('10.0.0.1', True, False),
  956. ('10.0.0.2', False, False),
  957. ('10.0.0.3', False, False)],
  958. ssh_user='root',
  959. variant_num=1,
  960. confirm_facts='y',
  961. storage='10.1.0.1',)
  962. self.cli_args.append("install")
  963. self.cli_args.append("--gen-inventory")
  964. result = self.runner.invoke(cli.cli, self.cli_args,
  965. input=cli_input)
  966. self.assert_result(result, 0)
  967. self._verify_load_facts(load_facts_mock)
  968. # Make sure run playbook wasn't called:
  969. self.assertEquals(0, len(run_playbook_mock.mock_calls))
  970. written_config = read_yaml(self.config_file)
  971. self._verify_config_hosts(written_config, 4)
  972. inventory = ConfigParser.ConfigParser(allow_no_value=True)
  973. inventory.read(os.path.join(self.work_dir, 'hosts'))
  974. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.1',
  975. 'openshift_schedulable=False')
  976. self.assert_inventory_host_var_unset(inventory, 'nodes', '10.0.0.2',
  977. 'openshift_schedulable=True')
  978. self.assert_inventory_host_var_unset(inventory, 'nodes', '10.0.0.3',
  979. 'openshift_schedulable=True')
  980. # TODO: test with config file, attended add node
  981. # TODO: test with config file, attended new node already in config file
  982. # TODO: test with config file, attended new node already in config file, plus manually added nodes
  983. # TODO: test with config file, attended reject facts