cli_installer_tests.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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. from six.moves 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. self.fail("Unexpected CLI return. Exit code: %s" % result.exit_code)
  393. # unattended with config file and all installed hosts (with --force)
  394. @patch('ooinstall.openshift_ansible.run_main_playbook')
  395. @patch('ooinstall.openshift_ansible.load_system_facts')
  396. def test_get_hosts_to_run_on2(self, load_facts_mock, run_playbook_mock):
  397. mock_facts = copy.deepcopy(MOCK_FACTS)
  398. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  399. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  400. mock_facts['10.0.0.3']['common']['version'] = "3.0.0"
  401. self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
  402. cli_input=None,
  403. exp_hosts_len=3,
  404. exp_hosts_to_run_on_len=3,
  405. force=True)
  406. # unattended with config file and no installed hosts (without --force)
  407. @patch('ooinstall.openshift_ansible.run_main_playbook')
  408. @patch('ooinstall.openshift_ansible.load_system_facts')
  409. def test_get_hosts_to_run_on3(self, load_facts_mock, run_playbook_mock):
  410. load_facts_mock.return_value = (MOCK_FACTS, 0)
  411. run_playbook_mock.return_value = 0
  412. self._verify_get_hosts_to_run_on(MOCK_FACTS, load_facts_mock, run_playbook_mock,
  413. cli_input=None,
  414. exp_hosts_len=3,
  415. exp_hosts_to_run_on_len=3,
  416. force=False)
  417. # unattended with config file and no installed hosts (with --force)
  418. @patch('ooinstall.openshift_ansible.run_main_playbook')
  419. @patch('ooinstall.openshift_ansible.load_system_facts')
  420. def test_get_hosts_to_run_on4(self, load_facts_mock, run_playbook_mock):
  421. load_facts_mock.return_value = (MOCK_FACTS, 0)
  422. run_playbook_mock.return_value = 0
  423. self._verify_get_hosts_to_run_on(MOCK_FACTS, load_facts_mock, run_playbook_mock,
  424. cli_input=None,
  425. exp_hosts_len=3,
  426. exp_hosts_to_run_on_len=3,
  427. force=True)
  428. # unattended with config file and some installed some uninstalled hosts (without --force)
  429. @patch('ooinstall.openshift_ansible.run_main_playbook')
  430. @patch('ooinstall.openshift_ansible.load_system_facts')
  431. def test_get_hosts_to_run_on5(self, load_facts_mock, run_playbook_mock):
  432. mock_facts = copy.deepcopy(MOCK_FACTS)
  433. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  434. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  435. self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
  436. cli_input=None,
  437. exp_hosts_len=3,
  438. exp_hosts_to_run_on_len=2,
  439. force=False)
  440. # unattended with config file and some installed some uninstalled hosts (with --force)
  441. @patch('ooinstall.openshift_ansible.run_main_playbook')
  442. @patch('ooinstall.openshift_ansible.load_system_facts')
  443. def test_get_hosts_to_run_on6(self, load_facts_mock, run_playbook_mock):
  444. mock_facts = copy.deepcopy(MOCK_FACTS)
  445. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  446. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  447. self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
  448. cli_input=None,
  449. exp_hosts_len=3,
  450. exp_hosts_to_run_on_len=3,
  451. force=True)
  452. @patch('ooinstall.openshift_ansible.run_main_playbook')
  453. @patch('ooinstall.openshift_ansible.load_system_facts')
  454. def test_cfg_full_run(self, load_facts_mock, run_playbook_mock):
  455. load_facts_mock.return_value = (MOCK_FACTS, 0)
  456. run_playbook_mock.return_value = 0
  457. config_file = self.write_config(os.path.join(self.work_dir,
  458. 'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')
  459. self.cli_args.extend(["-c", config_file, "install"])
  460. result = self.runner.invoke(cli.cli, self.cli_args)
  461. self.assert_result(result, 0)
  462. load_facts_args = load_facts_mock.call_args[0]
  463. self.assertEquals(os.path.join(self.work_dir, "hosts"),
  464. load_facts_args[0])
  465. self.assertEquals(os.path.join(self.work_dir,
  466. "playbooks/byo/openshift_facts.yml"), load_facts_args[1])
  467. env_vars = load_facts_args[2]
  468. self.assertEquals(os.path.join(self.work_dir,
  469. '.ansible/callback_facts.yaml'),
  470. env_vars['OO_INSTALL_CALLBACK_FACTS_YAML'])
  471. self.assertEqual('/tmp/ansible.log', env_vars['ANSIBLE_LOG_PATH'])
  472. # If user running test has rpm installed, this might be set to default:
  473. self.assertTrue('ANSIBLE_CONFIG' not in env_vars or
  474. env_vars['ANSIBLE_CONFIG'] == cli.DEFAULT_ANSIBLE_CONFIG)
  475. # Make sure we ran on the expected masters and nodes:
  476. hosts = run_playbook_mock.call_args[0][1]
  477. hosts_to_run_on = run_playbook_mock.call_args[0][2]
  478. self.assertEquals(3, len(hosts))
  479. self.assertEquals(3, len(hosts_to_run_on))
  480. @patch('ooinstall.openshift_ansible.run_main_playbook')
  481. @patch('ooinstall.openshift_ansible.load_system_facts')
  482. def test_inventory_write(self, load_facts_mock, run_playbook_mock):
  483. merged_config = SAMPLE_CONFIG % 'openshift-enterprise'
  484. load_facts_mock.return_value = (MOCK_FACTS, 0)
  485. run_playbook_mock.return_value = 0
  486. config_file = self.write_config(os.path.join(self.work_dir,
  487. 'ooinstall.conf'), merged_config)
  488. self.cli_args.extend(["-c", config_file, "install"])
  489. result = self.runner.invoke(cli.cli, self.cli_args)
  490. self.assert_result(result, 0)
  491. # Check the inventory file looks as we would expect:
  492. inventory = configparser.ConfigParser(allow_no_value=True)
  493. inventory.read(os.path.join(self.work_dir, 'hosts'))
  494. self.assertEquals('root',
  495. inventory.get('OSEv3:vars', 'ansible_ssh_user'))
  496. self.assertEquals('openshift-enterprise',
  497. inventory.get('OSEv3:vars', 'deployment_type'))
  498. # Check the masters:
  499. self.assertEquals(1, len(inventory.items('masters')))
  500. self.assertEquals(3, len(inventory.items('nodes')))
  501. for item in inventory.items('masters'):
  502. # ansible host lines do NOT parse nicely:
  503. master_line = item[0]
  504. if item[1] is not None:
  505. master_line = "%s=%s" % (master_line, item[1])
  506. self.assertTrue('openshift_ip' in master_line)
  507. self.assertTrue('openshift_public_ip' in master_line)
  508. self.assertTrue('openshift_hostname' in master_line)
  509. self.assertTrue('openshift_public_hostname' in master_line)
  510. @patch('ooinstall.openshift_ansible.run_main_playbook')
  511. @patch('ooinstall.openshift_ansible.load_system_facts')
  512. def test_variant_version_latest_assumed(self, load_facts_mock,
  513. run_playbook_mock):
  514. load_facts_mock.return_value = (MOCK_FACTS, 0)
  515. run_playbook_mock.return_value = 0
  516. config_file = self.write_config(os.path.join(self.work_dir,
  517. 'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')
  518. self.cli_args.extend(["-c", config_file, "install"])
  519. result = self.runner.invoke(cli.cli, self.cli_args)
  520. self.assert_result(result, 0)
  521. written_config = read_yaml(config_file)
  522. self.assertEquals('openshift-enterprise', written_config['variant'])
  523. # We didn't specify a version so the latest should have been assumed,
  524. # and written to disk:
  525. self.assertEquals('3.3', written_config['variant_version'])
  526. # Make sure the correct value was passed to ansible:
  527. inventory = configparser.ConfigParser(allow_no_value=True)
  528. inventory.read(os.path.join(self.work_dir, 'hosts'))
  529. self.assertEquals('openshift-enterprise',
  530. inventory.get('OSEv3:vars', 'deployment_type'))
  531. @patch('ooinstall.openshift_ansible.run_main_playbook')
  532. @patch('ooinstall.openshift_ansible.load_system_facts')
  533. def test_variant_version_preserved(self, load_facts_mock,
  534. run_playbook_mock):
  535. load_facts_mock.return_value = (MOCK_FACTS, 0)
  536. run_playbook_mock.return_value = 0
  537. config = SAMPLE_CONFIG % 'openshift-enterprise'
  538. config = '%s\n%s' % (config, 'variant_version: 3.3')
  539. config_file = self.write_config(os.path.join(self.work_dir,
  540. 'ooinstall.conf'), config)
  541. self.cli_args.extend(["-c", config_file, "install"])
  542. result = self.runner.invoke(cli.cli, self.cli_args)
  543. self.assert_result(result, 0)
  544. written_config = read_yaml(config_file)
  545. self.assertEquals('openshift-enterprise', written_config['variant'])
  546. # Make sure our older version was preserved:
  547. # and written to disk:
  548. self.assertEquals('3.3', written_config['variant_version'])
  549. inventory = configparser.ConfigParser(allow_no_value=True)
  550. inventory.read(os.path.join(self.work_dir, 'hosts'))
  551. self.assertEquals('openshift-enterprise',
  552. inventory.get('OSEv3:vars', 'deployment_type'))
  553. # unattended with bad config file and no installed hosts (without --force)
  554. @patch('ooinstall.openshift_ansible.run_main_playbook')
  555. @patch('ooinstall.openshift_ansible.load_system_facts')
  556. def test_bad_config(self, load_facts_mock, run_playbook_mock):
  557. load_facts_mock.return_value = (MOCK_FACTS, 0)
  558. run_playbook_mock.return_value = 0
  559. config_file = self.write_config(os.path.join(self.work_dir,
  560. 'ooinstall.conf'), BAD_CONFIG % 'openshift-enterprise')
  561. self.cli_args.extend(["-c", config_file, "install"])
  562. result = self.runner.invoke(cli.cli, self.cli_args)
  563. self.assertEquals(1, result.exit_code)
  564. self.assertTrue("You must specify either an ip or hostname"
  565. in result.output)
  566. #unattended with three masters, one node, and haproxy
  567. @patch('ooinstall.openshift_ansible.run_main_playbook')
  568. @patch('ooinstall.openshift_ansible.load_system_facts')
  569. def test_quick_ha_full_run(self, load_facts_mock, run_playbook_mock):
  570. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  571. run_playbook_mock.return_value = 0
  572. config_file = self.write_config(os.path.join(self.work_dir,
  573. 'ooinstall.conf'), QUICKHA_CONFIG % 'openshift-enterprise')
  574. self.cli_args.extend(["-c", config_file, "install"])
  575. result = self.runner.invoke(cli.cli, self.cli_args)
  576. self.assert_result(result, 0)
  577. # Make sure we ran on the expected masters and nodes:
  578. hosts = run_playbook_mock.call_args[0][1]
  579. hosts_to_run_on = run_playbook_mock.call_args[0][2]
  580. self.assertEquals(6, len(hosts))
  581. self.assertEquals(6, len(hosts_to_run_on))
  582. #unattended with two masters, one node, and haproxy
  583. @patch('ooinstall.openshift_ansible.run_main_playbook')
  584. @patch('ooinstall.openshift_ansible.load_system_facts')
  585. def test_quick_ha_only_2_masters(self, load_facts_mock, run_playbook_mock):
  586. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  587. run_playbook_mock.return_value = 0
  588. config_file = self.write_config(os.path.join(self.work_dir,
  589. 'ooinstall.conf'), QUICKHA_2_MASTER_CONFIG % 'openshift-enterprise')
  590. self.cli_args.extend(["-c", config_file, "install"])
  591. result = self.runner.invoke(cli.cli, self.cli_args)
  592. # This is an invalid config:
  593. self.assert_result(result, 1)
  594. self.assertTrue("A minimum of 3 masters are required" in result.output)
  595. #unattended with three masters, one node, but no load balancer specified:
  596. @patch('ooinstall.openshift_ansible.run_main_playbook')
  597. @patch('ooinstall.openshift_ansible.load_system_facts')
  598. def test_quick_ha_no_lb(self, load_facts_mock, run_playbook_mock):
  599. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  600. run_playbook_mock.return_value = 0
  601. config_file = self.write_config(os.path.join(self.work_dir,
  602. 'ooinstall.conf'), QUICKHA_CONFIG_NO_LB % 'openshift-enterprise')
  603. self.cli_args.extend(["-c", config_file, "install"])
  604. result = self.runner.invoke(cli.cli, self.cli_args)
  605. # This is not a valid input:
  606. self.assert_result(result, 1)
  607. self.assertTrue('No master load balancer specified in config' in result.output)
  608. #unattended with three masters, one node, and one of the masters reused as load balancer:
  609. @patch('ooinstall.openshift_ansible.run_main_playbook')
  610. @patch('ooinstall.openshift_ansible.load_system_facts')
  611. def test_quick_ha_reused_lb(self, load_facts_mock, run_playbook_mock):
  612. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  613. run_playbook_mock.return_value = 0
  614. config_file = self.write_config(os.path.join(self.work_dir,
  615. 'ooinstall.conf'), QUICKHA_CONFIG_REUSED_LB % 'openshift-enterprise')
  616. self.cli_args.extend(["-c", config_file, "install"])
  617. result = self.runner.invoke(cli.cli, self.cli_args)
  618. # This is not a valid configuration:
  619. self.assert_result(result, 1)
  620. #unattended with preconfigured lb
  621. @patch('ooinstall.openshift_ansible.run_main_playbook')
  622. @patch('ooinstall.openshift_ansible.load_system_facts')
  623. def test_quick_ha_preconfigured_lb(self, load_facts_mock, run_playbook_mock):
  624. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  625. run_playbook_mock.return_value = 0
  626. config_file = self.write_config(os.path.join(self.work_dir,
  627. 'ooinstall.conf'), QUICKHA_CONFIG_PRECONFIGURED_LB % 'openshift-enterprise')
  628. self.cli_args.extend(["-c", config_file, "install"])
  629. result = self.runner.invoke(cli.cli, self.cli_args)
  630. self.assert_result(result, 0)
  631. # Make sure we ran on the expected masters and nodes:
  632. hosts = run_playbook_mock.call_args[0][1]
  633. hosts_to_run_on = run_playbook_mock.call_args[0][2]
  634. self.assertEquals(6, len(hosts))
  635. self.assertEquals(6, len(hosts_to_run_on))
  636. class AttendedCliTests(OOCliFixture):
  637. def setUp(self):
  638. OOCliFixture.setUp(self)
  639. # Doesn't exist but keeps us from reading the local users config:
  640. self.config_file = os.path.join(self.work_dir, 'config.yml')
  641. self.cli_args.extend(["-c", self.config_file])
  642. @patch('ooinstall.openshift_ansible.run_main_playbook')
  643. @patch('ooinstall.openshift_ansible.load_system_facts')
  644. def test_full_run(self, load_facts_mock, run_playbook_mock):
  645. load_facts_mock.return_value = (MOCK_FACTS, 0)
  646. run_playbook_mock.return_value = 0
  647. cli_input = build_input(hosts=[
  648. ('10.0.0.1', True, False),
  649. ('10.0.0.2', False, False),
  650. ('10.0.0.3', False, False)],
  651. ssh_user='root',
  652. variant_num=1,
  653. confirm_facts='y',
  654. storage='10.1.0.1',)
  655. self.cli_args.append("install")
  656. result = self.runner.invoke(cli.cli, self.cli_args,
  657. input=cli_input)
  658. self.assert_result(result, 0)
  659. self._verify_load_facts(load_facts_mock)
  660. self._verify_run_playbook(run_playbook_mock, 4, 4)
  661. written_config = read_yaml(self.config_file)
  662. self._verify_config_hosts(written_config, 4)
  663. inventory = configparser.ConfigParser(allow_no_value=True)
  664. inventory.read(os.path.join(self.work_dir, 'hosts'))
  665. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.1',
  666. 'openshift_schedulable=False')
  667. self.assert_inventory_host_var_unset(inventory, 'nodes', '10.0.0.2',
  668. 'openshift_schedulable=True')
  669. self.assert_inventory_host_var_unset(inventory, 'nodes', '10.0.0.3',
  670. 'openshift_schedulable=True')
  671. # interactive with config file and some installed some uninstalled hosts
  672. @patch('ooinstall.openshift_ansible.run_main_playbook')
  673. @patch('ooinstall.openshift_ansible.load_system_facts')
  674. def test_scaleup_hint(self, load_facts_mock, run_playbook_mock):
  675. # Modify the mock facts to return a version indicating OpenShift
  676. # is already installed on our master, and the first node.
  677. mock_facts = copy.deepcopy(MOCK_FACTS)
  678. mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  679. mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  680. load_facts_mock.return_value = (mock_facts, 0)
  681. run_playbook_mock.return_value = 0
  682. cli_input = build_input(hosts=[
  683. ('10.0.0.1', True, False),
  684. ('10.0.0.2', False, False),
  685. ],
  686. add_nodes=[('10.0.0.3', False, False)],
  687. ssh_user='root',
  688. variant_num=1,
  689. confirm_facts='y',
  690. storage='10.0.0.1',)
  691. self.cli_args.append("install")
  692. result = self.runner.invoke(cli.cli,
  693. self.cli_args,
  694. input=cli_input)
  695. # This is testing the install workflow so we want to make sure we
  696. # exit with the appropriate hint.
  697. self.assertTrue('scaleup' in result.output)
  698. self.assert_result(result, 1)
  699. @patch('ooinstall.openshift_ansible.run_main_playbook')
  700. @patch('ooinstall.openshift_ansible.load_system_facts')
  701. def test_fresh_install_with_config(self, load_facts_mock, run_playbook_mock):
  702. load_facts_mock.return_value = (MOCK_FACTS, 0)
  703. run_playbook_mock.return_value = 0
  704. config_file = self.write_config(os.path.join(self.work_dir,
  705. 'ooinstall.conf'),
  706. SAMPLE_CONFIG % 'openshift-enterprise')
  707. cli_input = build_input(confirm_facts='y')
  708. self.cli_args.extend(["-c", config_file])
  709. self.cli_args.append("install")
  710. result = self.runner.invoke(cli.cli,
  711. self.cli_args,
  712. input=cli_input)
  713. self.assert_result(result, 0)
  714. self._verify_load_facts(load_facts_mock)
  715. self._verify_run_playbook(run_playbook_mock, 3, 3)
  716. written_config = read_yaml(config_file)
  717. self._verify_config_hosts(written_config, 3)
  718. # #interactive with config file and all installed hosts
  719. # @patch('ooinstall.openshift_ansible.run_main_playbook')
  720. # @patch('ooinstall.openshift_ansible.load_system_facts')
  721. # def test_get_hosts_to_run_on(self, load_facts_mock, run_playbook_mock):
  722. # mock_facts = copy.deepcopy(MOCK_FACTS)
  723. # mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
  724. # mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
  725. #
  726. # cli_input = build_input(hosts=[
  727. # ('10.0.0.1', True, False),
  728. # ],
  729. # add_nodes=[('10.0.0.2', False, False)],
  730. # ssh_user='root',
  731. # variant_num=1,
  732. # schedulable_masters_ok=True,
  733. # confirm_facts='y',
  734. # storage='10.0.0.1',)
  735. #
  736. # self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock,
  737. # run_playbook_mock,
  738. # cli_input,
  739. # exp_hosts_len=2,
  740. # exp_hosts_to_run_on_len=2,
  741. # force=False)
  742. #interactive multimaster: one more node than master
  743. @patch('ooinstall.openshift_ansible.run_main_playbook')
  744. @patch('ooinstall.openshift_ansible.load_system_facts')
  745. def test_ha_dedicated_node(self, load_facts_mock, run_playbook_mock):
  746. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  747. run_playbook_mock.return_value = 0
  748. cli_input = build_input(hosts=[
  749. ('10.0.0.1', True, False),
  750. ('10.0.0.2', True, False),
  751. ('10.0.0.3', True, False),
  752. ('10.0.0.4', False, False)],
  753. ssh_user='root',
  754. variant_num=1,
  755. confirm_facts='y',
  756. master_lb=('10.0.0.5', False),
  757. storage='10.1.0.1',)
  758. self.cli_args.append("install")
  759. result = self.runner.invoke(cli.cli, self.cli_args,
  760. input=cli_input)
  761. self.assert_result(result, 0)
  762. self._verify_load_facts(load_facts_mock)
  763. self._verify_run_playbook(run_playbook_mock, 6, 6)
  764. written_config = read_yaml(self.config_file)
  765. self._verify_config_hosts(written_config, 6)
  766. inventory = configparser.ConfigParser(allow_no_value=True)
  767. inventory.read(os.path.join(self.work_dir, 'hosts'))
  768. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.1',
  769. 'openshift_schedulable=False')
  770. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.2',
  771. 'openshift_schedulable=False')
  772. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.3',
  773. 'openshift_schedulable=False')
  774. self.assert_inventory_host_var_unset(inventory, 'nodes', '10.0.0.4',
  775. 'openshift_schedulable=True')
  776. self.assertTrue(inventory.has_section('etcd'))
  777. self.assertEquals(3, len(inventory.items('etcd')))
  778. #interactive multimaster: identical masters and nodes
  779. @patch('ooinstall.openshift_ansible.run_main_playbook')
  780. @patch('ooinstall.openshift_ansible.load_system_facts')
  781. def test_ha_no_dedicated_nodes(self, load_facts_mock, run_playbook_mock):
  782. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  783. run_playbook_mock.return_value = 0
  784. cli_input = build_input(hosts=[
  785. ('10.0.0.1', True, False),
  786. ('10.0.0.2', True, False),
  787. ('10.0.0.3', True, False)],
  788. ssh_user='root',
  789. variant_num=1,
  790. confirm_facts='y',
  791. master_lb=('10.0.0.5', False),
  792. storage='10.1.0.1',)
  793. self.cli_args.append("install")
  794. result = self.runner.invoke(cli.cli, self.cli_args,
  795. input=cli_input)
  796. self.assert_result(result, 0)
  797. self._verify_load_facts(load_facts_mock)
  798. self._verify_run_playbook(run_playbook_mock, 5, 5)
  799. written_config = read_yaml(self.config_file)
  800. self._verify_config_hosts(written_config, 5)
  801. inventory = configparser.ConfigParser(allow_no_value=True)
  802. inventory.read(os.path.join(self.work_dir, 'hosts'))
  803. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.1',
  804. 'openshift_schedulable=True')
  805. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.2',
  806. 'openshift_schedulable=True')
  807. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.3',
  808. 'openshift_schedulable=True')
  809. # Checks the inventory (as a ConfigParser) for the given host, host
  810. # variable, and expected value.
  811. def assert_inventory_host_var(self, inventory, section, host, variable):
  812. # Config parser splits on the first "=", so we end up with:
  813. # 'hostname key1' -> 'val1 key2=val2 key3=val3'
  814. #
  815. # Convert to something easier to test:
  816. for (a, b) in inventory.items(section):
  817. full_line = "%s=%s" % (a, b)
  818. tokens = full_line.split()
  819. if tokens[0] == host:
  820. self.assertTrue(variable in tokens[1:], "Unable to find %s in line: %s" % (variable, full_line))
  821. return
  822. self.fail("unable to find host %s in inventory" % host)
  823. def assert_inventory_host_var_unset(self, inventory, section, host, variable):
  824. # Config parser splits on the first "=", so we end up with:
  825. # 'hostname key1' -> 'val1 key2=val2 key3=val3'
  826. #
  827. # Convert to something easier to test:
  828. for (a, b) in inventory.items(section):
  829. full_line = "%s=%s" % (a, b)
  830. tokens = full_line.split()
  831. if tokens[0] == host:
  832. self.assertFalse(("%s=" % variable) in full_line,
  833. msg='%s host variable was set: %s' %
  834. (variable, full_line))
  835. return
  836. self.fail("unable to find host %s in inventory" % host)
  837. #interactive multimaster: attempting to use a master as the load balancer should fail:
  838. @patch('ooinstall.openshift_ansible.run_main_playbook')
  839. @patch('ooinstall.openshift_ansible.load_system_facts')
  840. def test_ha_reuse_master_as_lb(self, load_facts_mock, run_playbook_mock):
  841. load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
  842. run_playbook_mock.return_value = 0
  843. cli_input = build_input(hosts=[
  844. ('10.0.0.1', True, False),
  845. ('10.0.0.2', True, False),
  846. ('10.0.0.3', False, False),
  847. ('10.0.0.4', True, False)],
  848. ssh_user='root',
  849. variant_num=1,
  850. confirm_facts='y',
  851. master_lb=(['10.0.0.2', '10.0.0.5'], False),
  852. storage='10.1.0.1')
  853. self.cli_args.append("install")
  854. result = self.runner.invoke(cli.cli, self.cli_args,
  855. input=cli_input)
  856. self.assert_result(result, 0)
  857. #interactive all-in-one
  858. @patch('ooinstall.openshift_ansible.run_main_playbook')
  859. @patch('ooinstall.openshift_ansible.load_system_facts')
  860. def test_all_in_one(self, load_facts_mock, run_playbook_mock):
  861. load_facts_mock.return_value = (MOCK_FACTS, 0)
  862. run_playbook_mock.return_value = 0
  863. cli_input = build_input(hosts=[
  864. ('10.0.0.1', True, False)],
  865. ssh_user='root',
  866. variant_num=1,
  867. confirm_facts='y',
  868. storage='10.0.0.1')
  869. self.cli_args.append("install")
  870. result = self.runner.invoke(cli.cli, self.cli_args,
  871. input=cli_input)
  872. self.assert_result(result, 0)
  873. self._verify_load_facts(load_facts_mock)
  874. self._verify_run_playbook(run_playbook_mock, 1, 1)
  875. written_config = read_yaml(self.config_file)
  876. self._verify_config_hosts(written_config, 1)
  877. inventory = configparser.ConfigParser(allow_no_value=True)
  878. inventory.read(os.path.join(self.work_dir, 'hosts'))
  879. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.1',
  880. 'openshift_schedulable=True')
  881. @patch('ooinstall.openshift_ansible.run_main_playbook')
  882. @patch('ooinstall.openshift_ansible.load_system_facts')
  883. def test_gen_inventory(self, load_facts_mock, run_playbook_mock):
  884. load_facts_mock.return_value = (MOCK_FACTS, 0)
  885. run_playbook_mock.return_value = 0
  886. cli_input = build_input(hosts=[
  887. ('10.0.0.1', True, False),
  888. ('10.0.0.2', False, False),
  889. ('10.0.0.3', False, False)],
  890. ssh_user='root',
  891. variant_num=1,
  892. confirm_facts='y',
  893. storage='10.1.0.1',)
  894. self.cli_args.append("install")
  895. self.cli_args.append("--gen-inventory")
  896. result = self.runner.invoke(cli.cli, self.cli_args,
  897. input=cli_input)
  898. self.assert_result(result, 0)
  899. self._verify_load_facts(load_facts_mock)
  900. # Make sure run playbook wasn't called:
  901. self.assertEquals(0, len(run_playbook_mock.mock_calls))
  902. written_config = read_yaml(self.config_file)
  903. self._verify_config_hosts(written_config, 4)
  904. inventory = configparser.ConfigParser(allow_no_value=True)
  905. inventory.read(os.path.join(self.work_dir, 'hosts'))
  906. self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.1',
  907. 'openshift_schedulable=False')
  908. self.assert_inventory_host_var_unset(inventory, 'nodes', '10.0.0.2',
  909. 'openshift_schedulable=True')
  910. self.assert_inventory_host_var_unset(inventory, 'nodes', '10.0.0.3',
  911. 'openshift_schedulable=True')
  912. # TODO: test with config file, attended add node
  913. # TODO: test with config file, attended new node already in config file
  914. # TODO: test with config file, attended new node already in config file, plus manually added nodes
  915. # TODO: test with config file, attended reject facts