cli_installer_tests.py 43 KB

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