12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133 |
- # TODO: Temporarily disabled due to importing old code into openshift-ansible
- # repo. We will work on these over time.
- # pylint: disable=bad-continuation,missing-docstring,no-self-use,invalid-name,too-many-lines
- import copy
- import os
- import ConfigParser
- import ooinstall.cli_installer as cli
- from test.fixture import OOCliFixture, SAMPLE_CONFIG, build_input, read_yaml
- from mock import patch
- MOCK_FACTS = {
- '10.0.0.1': {
- 'common': {
- 'ip': '10.0.0.1',
- 'public_ip': '10.0.0.1',
- 'hostname': 'master-private.example.com',
- 'public_hostname': 'master.example.com'
- }
- },
- '10.0.0.2': {
- 'common': {
- 'ip': '10.0.0.2',
- 'public_ip': '10.0.0.2',
- 'hostname': 'node1-private.example.com',
- 'public_hostname': 'node1.example.com'
- }
- },
- '10.0.0.3': {
- 'common': {
- 'ip': '10.0.0.3',
- 'public_ip': '10.0.0.3',
- 'hostname': 'node2-private.example.com',
- 'public_hostname': 'node2.example.com'
- }
- },
- '10.1.0.1': {
- 'common': {
- 'ip': '10.1.0.1',
- 'public_ip': '10.1.0.1',
- 'hostname': 'storage-private.example.com',
- 'public_hostname': 'storage.example.com'
- }
- },
- }
- MOCK_FACTS_QUICKHA = {
- '10.0.0.1': {
- 'common': {
- 'ip': '10.0.0.1',
- 'public_ip': '10.0.0.1',
- 'hostname': 'master-private.example.com',
- 'public_hostname': 'master.example.com'
- }
- },
- '10.0.0.2': {
- 'common': {
- 'ip': '10.0.0.2',
- 'public_ip': '10.0.0.2',
- 'hostname': 'node1-private.example.com',
- 'public_hostname': 'node1.example.com'
- }
- },
- '10.0.0.3': {
- 'common': {
- 'ip': '10.0.0.3',
- 'public_ip': '10.0.0.3',
- 'hostname': 'node2-private.example.com',
- 'public_hostname': 'node2.example.com'
- }
- },
- '10.0.0.4': {
- 'common': {
- 'ip': '10.0.0.4',
- 'public_ip': '10.0.0.4',
- 'hostname': 'node3-private.example.com',
- 'public_hostname': 'node3.example.com'
- }
- },
- '10.0.0.5': {
- 'common': {
- 'ip': '10.0.0.5',
- 'public_ip': '10.0.0.5',
- 'hostname': 'proxy-private.example.com',
- 'public_hostname': 'proxy.example.com'
- }
- },
- '10.1.0.1': {
- 'common': {
- 'ip': '10.1.0.1',
- 'public_ip': '10.1.0.1',
- 'hostname': 'storage-private.example.com',
- 'public_hostname': 'storage.example.com'
- }
- },
- }
- # Missing connect_to on some hosts:
- BAD_CONFIG = """
- variant: %s
- version: v2
- deployment:
- ansible_ssh_user: root
- hosts:
- - connect_to: 10.0.0.1
- ip: 10.0.0.1
- hostname: master-private.example.com
- public_ip: 24.222.0.1
- public_hostname: master.example.com
- roles:
- - master
- - node
- - ip: 10.0.0.2
- hostname: node1-private.example.com
- public_ip: 24.222.0.2
- public_hostname: node1.example.com
- roles:
- - node
- - connect_to: 10.0.0.3
- ip: 10.0.0.3
- hostname: node2-private.example.com
- public_ip: 24.222.0.3
- public_hostname: node2.example.com
- roles:
- - node
- roles:
- master:
- node:
- """
- QUICKHA_CONFIG = """
- variant: %s
- version: v2
- deployment:
- ansible_ssh_user: root
- hosts:
- - connect_to: 10.0.0.1
- ip: 10.0.0.1
- hostname: master-private.example.com
- public_ip: 24.222.0.1
- public_hostname: master.example.com
- roles:
- - master
- - node
- - connect_to: 10.0.0.2
- ip: 10.0.0.2
- hostname: node1-private.example.com
- public_ip: 24.222.0.2
- public_hostname: node1.example.com
- roles:
- - master
- - node
- - connect_to: 10.0.0.3
- ip: 10.0.0.3
- hostname: node2-private.example.com
- public_ip: 24.222.0.3
- public_hostname: node2.example.com
- roles:
- - master
- - node
- - connect_to: 10.0.0.4
- ip: 10.0.0.4
- hostname: node3-private.example.com
- public_ip: 24.222.0.4
- public_hostname: node3.example.com
- roles:
- - node
- - connect_to: 10.0.0.5
- ip: 10.0.0.5
- hostname: proxy-private.example.com
- public_ip: 24.222.0.5
- public_hostname: proxy.example.com
- roles:
- - master_lb
- - connect_to: 10.1.0.1
- ip: 10.1.0.1
- hostname: storage-private.example.com
- public_ip: 24.222.0.6
- public_hostname: storage.example.com
- roles:
- - storage
- roles:
- master:
- master_lb:
- node:
- storage:
- """
- QUICKHA_2_MASTER_CONFIG = """
- variant: %s
- version: v2
- deployment:
- ansible_ssh_user: root
- hosts:
- - connect_to: 10.0.0.1
- ip: 10.0.0.1
- hostname: master-private.example.com
- public_ip: 24.222.0.1
- public_hostname: master.example.com
- roles:
- - master
- - node
- - connect_to: 10.0.0.2
- ip: 10.0.0.2
- hostname: node1-private.example.com
- public_ip: 24.222.0.2
- public_hostname: node1.example.com
- roles:
- - master
- - node
- - connect_to: 10.0.0.4
- ip: 10.0.0.4
- hostname: node3-private.example.com
- public_ip: 24.222.0.4
- public_hostname: node3.example.com
- roles:
- - node
- - connect_to: 10.0.0.5
- ip: 10.0.0.5
- hostname: proxy-private.example.com
- public_ip: 24.222.0.5
- public_hostname: proxy.example.com
- roles:
- - master_lb
- - connect_to: 10.1.0.1
- ip: 10.1.0.1
- hostname: storage-private.example.com
- public_ip: 24.222.0.6
- public_hostname: storage.example.com
- roles:
- - storage
- roles:
- master:
- master_lb:
- node:
- storage:
- """
- QUICKHA_CONFIG_REUSED_LB = """
- variant: %s
- version: v2
- deployment:
- ansible_ssh_user: root
- hosts:
- - connect_to: 10.0.0.1
- ip: 10.0.0.1
- hostname: master-private.example.com
- public_ip: 24.222.0.1
- public_hostname: master.example.com
- roles:
- - master
- - node
- - connect_to: 10.0.0.2
- ip: 10.0.0.2
- hostname: node1-private.example.com
- public_ip: 24.222.0.2
- public_hostname: node1.example.com
- roles:
- - master
- - node
- - master_lb
- - connect_to: 10.0.0.3
- ip: 10.0.0.3
- hostname: node2-private.example.com
- public_ip: 24.222.0.3
- public_hostname: node2.example.com
- roles:
- - master
- - node
- - connect_to: 10.1.0.1
- ip: 10.1.0.1
- hostname: storage-private.example.com
- public_ip: 24.222.0.6
- public_hostname: storage.example.com
- roles:
- - storage
- roles:
- master:
- node:
- storage:
- """
- QUICKHA_CONFIG_NO_LB = """
- variant: %s
- version: v2
- deployment:
- ansible_ssh_user: root
- hosts:
- - connect_to: 10.0.0.1
- ip: 10.0.0.1
- hostname: master-private.example.com
- public_ip: 24.222.0.1
- public_hostname: master.example.com
- roles:
- - master
- - node
- - connect_to: 10.0.0.2
- ip: 10.0.0.2
- hostname: node1-private.example.com
- public_ip: 24.222.0.2
- public_hostname: node1.example.com
- roles:
- - master
- - node
- - connect_to: 10.0.0.3
- ip: 10.0.0.3
- hostname: node2-private.example.com
- public_ip: 24.222.0.3
- public_hostname: node2.example.com
- roles:
- - master
- - node
- - connect_to: 10.1.0.1
- ip: 10.1.0.1
- hostname: storage-private.example.com
- public_ip: 24.222.0.6
- public_hostname: storage.example.com
- roles:
- - storage
- roles:
- master:
- node:
- storage:
- """
- QUICKHA_CONFIG_PRECONFIGURED_LB = """
- variant: %s
- version: v2
- deployment:
- ansible_ssh_user: root
- hosts:
- - connect_to: 10.0.0.1
- ip: 10.0.0.1
- hostname: master-private.example.com
- public_ip: 24.222.0.1
- public_hostname: master.example.com
- roles:
- - master
- - node
- - connect_to: 10.0.0.2
- ip: 10.0.0.2
- hostname: node1-private.example.com
- public_ip: 24.222.0.2
- public_hostname: node1.example.com
- roles:
- - master
- - node
- - connect_to: 10.0.0.3
- ip: 10.0.0.3
- hostname: node2-private.example.com
- public_ip: 24.222.0.3
- public_hostname: node2.example.com
- roles:
- - master
- - node
- - connect_to: 10.0.0.4
- ip: 10.0.0.4
- hostname: node3-private.example.com
- public_ip: 24.222.0.4
- public_hostname: node3.example.com
- roles:
- - node
- - connect_to: proxy-private.example.com
- hostname: proxy-private.example.com
- public_hostname: proxy.example.com
- preconfigured: true
- roles:
- - master_lb
- - connect_to: 10.1.0.1
- ip: 10.1.0.1
- hostname: storage-private.example.com
- public_ip: 24.222.0.6
- public_hostname: storage.example.com
- roles:
- - storage
- roles:
- master:
- master_lb:
- node:
- storage:
- """
- class UnattendedCliTests(OOCliFixture):
- def setUp(self):
- OOCliFixture.setUp(self)
- self.cli_args.append("-u")
- # unattended with config file and all installed hosts (without --force)
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_get_hosts_to_run_on1(self, load_facts_mock, run_playbook_mock):
- mock_facts = copy.deepcopy(MOCK_FACTS)
- mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
- mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
- mock_facts['10.0.0.3']['common']['version'] = "3.0.0"
- load_facts_mock.return_value = (mock_facts, 0)
- run_playbook_mock.return_value = 0
- config_file = self.write_config(os.path.join(self.work_dir,
- 'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')
- self.cli_args.extend(["-c", config_file, "install"])
- result = self.runner.invoke(cli.cli, self.cli_args)
- if result.exception is None or result.exit_code != 1:
- print "Exit code: %s" % result.exit_code
- self.fail("Unexpected CLI return")
- # unattended with config file and all installed hosts (with --force)
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_get_hosts_to_run_on2(self, load_facts_mock, run_playbook_mock):
- mock_facts = copy.deepcopy(MOCK_FACTS)
- mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
- mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
- mock_facts['10.0.0.3']['common']['version'] = "3.0.0"
- self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
- cli_input=None,
- exp_hosts_len=3,
- exp_hosts_to_run_on_len=3,
- force=True)
- # unattended with config file and no installed hosts (without --force)
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_get_hosts_to_run_on3(self, load_facts_mock, run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS, 0)
- run_playbook_mock.return_value = 0
- self._verify_get_hosts_to_run_on(MOCK_FACTS, load_facts_mock, run_playbook_mock,
- cli_input=None,
- exp_hosts_len=3,
- exp_hosts_to_run_on_len=3,
- force=False)
- # unattended with config file and no installed hosts (with --force)
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_get_hosts_to_run_on4(self, load_facts_mock, run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS, 0)
- run_playbook_mock.return_value = 0
- self._verify_get_hosts_to_run_on(MOCK_FACTS, load_facts_mock, run_playbook_mock,
- cli_input=None,
- exp_hosts_len=3,
- exp_hosts_to_run_on_len=3,
- force=True)
- # unattended with config file and some installed some uninstalled hosts (without --force)
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_get_hosts_to_run_on5(self, load_facts_mock, run_playbook_mock):
- mock_facts = copy.deepcopy(MOCK_FACTS)
- mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
- mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
- self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
- cli_input=None,
- exp_hosts_len=3,
- exp_hosts_to_run_on_len=2,
- force=False)
- # unattended with config file and some installed some uninstalled hosts (with --force)
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_get_hosts_to_run_on6(self, load_facts_mock, run_playbook_mock):
- mock_facts = copy.deepcopy(MOCK_FACTS)
- mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
- mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
- self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock, run_playbook_mock,
- cli_input=None,
- exp_hosts_len=3,
- exp_hosts_to_run_on_len=3,
- force=True)
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_cfg_full_run(self, load_facts_mock, run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS, 0)
- run_playbook_mock.return_value = 0
- config_file = self.write_config(os.path.join(self.work_dir,
- 'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')
- self.cli_args.extend(["-c", config_file, "install"])
- result = self.runner.invoke(cli.cli, self.cli_args)
- self.assert_result(result, 0)
- load_facts_args = load_facts_mock.call_args[0]
- self.assertEquals(os.path.join(self.work_dir, "hosts"),
- load_facts_args[0])
- self.assertEquals(os.path.join(self.work_dir,
- "playbooks/byo/openshift_facts.yml"), load_facts_args[1])
- env_vars = load_facts_args[2]
- self.assertEquals(os.path.join(self.work_dir,
- '.ansible/callback_facts.yaml'),
- env_vars['OO_INSTALL_CALLBACK_FACTS_YAML'])
- self.assertEqual('/tmp/ansible.log', env_vars['ANSIBLE_LOG_PATH'])
- # If user running test has rpm installed, this might be set to default:
- self.assertTrue('ANSIBLE_CONFIG' not in env_vars or
- env_vars['ANSIBLE_CONFIG'] == cli.DEFAULT_ANSIBLE_CONFIG)
- # Make sure we ran on the expected masters and nodes:
- hosts = run_playbook_mock.call_args[0][1]
- hosts_to_run_on = run_playbook_mock.call_args[0][2]
- self.assertEquals(3, len(hosts))
- self.assertEquals(3, len(hosts_to_run_on))
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_inventory_write(self, load_facts_mock, run_playbook_mock):
- merged_config = SAMPLE_CONFIG % 'openshift-enterprise'
- load_facts_mock.return_value = (MOCK_FACTS, 0)
- run_playbook_mock.return_value = 0
- config_file = self.write_config(os.path.join(self.work_dir,
- 'ooinstall.conf'), merged_config)
- self.cli_args.extend(["-c", config_file, "install"])
- result = self.runner.invoke(cli.cli, self.cli_args)
- self.assert_result(result, 0)
- # Check the inventory file looks as we would expect:
- inventory = ConfigParser.ConfigParser(allow_no_value=True)
- inventory.read(os.path.join(self.work_dir, 'hosts'))
- self.assertEquals('root',
- inventory.get('OSEv3:vars', 'ansible_ssh_user'))
- self.assertEquals('openshift-enterprise',
- inventory.get('OSEv3:vars', 'deployment_type'))
- # Check the masters:
- self.assertEquals(1, len(inventory.items('masters')))
- self.assertEquals(3, len(inventory.items('nodes')))
- for item in inventory.items('masters'):
- # ansible host lines do NOT parse nicely:
- master_line = item[0]
- if item[1] is not None:
- master_line = "%s=%s" % (master_line, item[1])
- self.assertTrue('openshift_ip' in master_line)
- self.assertTrue('openshift_public_ip' in master_line)
- self.assertTrue('openshift_hostname' in master_line)
- self.assertTrue('openshift_public_hostname' in master_line)
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_variant_version_latest_assumed(self, load_facts_mock,
- run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS, 0)
- run_playbook_mock.return_value = 0
- config_file = self.write_config(os.path.join(self.work_dir,
- 'ooinstall.conf'), SAMPLE_CONFIG % 'openshift-enterprise')
- self.cli_args.extend(["-c", config_file, "install"])
- result = self.runner.invoke(cli.cli, self.cli_args)
- self.assert_result(result, 0)
- written_config = read_yaml(config_file)
- self.assertEquals('openshift-enterprise', written_config['variant'])
- # We didn't specify a version so the latest should have been assumed,
- # and written to disk:
- self.assertEquals('3.3', written_config['variant_version'])
- # Make sure the correct value was passed to ansible:
- inventory = ConfigParser.ConfigParser(allow_no_value=True)
- inventory.read(os.path.join(self.work_dir, 'hosts'))
- self.assertEquals('openshift-enterprise',
- inventory.get('OSEv3:vars', 'deployment_type'))
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_variant_version_preserved(self, load_facts_mock,
- run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS, 0)
- run_playbook_mock.return_value = 0
- config = SAMPLE_CONFIG % 'openshift-enterprise'
- config = '%s\n%s' % (config, 'variant_version: 3.3')
- config_file = self.write_config(os.path.join(self.work_dir,
- 'ooinstall.conf'), config)
- self.cli_args.extend(["-c", config_file, "install"])
- result = self.runner.invoke(cli.cli, self.cli_args)
- self.assert_result(result, 0)
- written_config = read_yaml(config_file)
- self.assertEquals('openshift-enterprise', written_config['variant'])
- # Make sure our older version was preserved:
- # and written to disk:
- self.assertEquals('3.3', written_config['variant_version'])
- inventory = ConfigParser.ConfigParser(allow_no_value=True)
- inventory.read(os.path.join(self.work_dir, 'hosts'))
- self.assertEquals('openshift-enterprise',
- inventory.get('OSEv3:vars', 'deployment_type'))
- # 2016-09-26 - tbielawa - COMMENTING OUT these tests FOR NOW while
- # we wait to see if anyone notices that we took away their ability
- # to set the ansible_config parameter in the command line options
- # and in the installer config file.
- #
- # We have removed the ability to set the ansible config file
- # manually so that our new quieter output mode is the default and
- # only output mode.
- #
- # RE: https://trello.com/c/DSwwizwP - atomic-openshift-install
- # should only output relevant information.
- # @patch('ooinstall.openshift_ansible.run_ansible')
- # @patch('ooinstall.openshift_ansible.load_system_facts')
- # def test_no_ansible_config_specified(self, load_facts_mock, run_ansible_mock):
- # load_facts_mock.return_value = (MOCK_FACTS, 0)
- # run_ansible_mock.return_value = 0
- # config = SAMPLE_CONFIG % 'openshift-enterprise'
- # self._ansible_config_test(load_facts_mock, run_ansible_mock,
- # config, None, None)
- # @patch('ooinstall.openshift_ansible.run_ansible')
- # @patch('ooinstall.openshift_ansible.load_system_facts')
- # def test_ansible_config_specified_cli(self, load_facts_mock, run_ansible_mock):
- # load_facts_mock.return_value = (MOCK_FACTS, 0)
- # run_ansible_mock.return_value = 0
- # config = SAMPLE_CONFIG % 'openshift-enterprise'
- # ansible_config = os.path.join(self.work_dir, 'ansible.cfg')
- # self._ansible_config_test(load_facts_mock, run_ansible_mock,
- # config, ansible_config, ansible_config)
- # @patch('ooinstall.openshift_ansible.run_ansible')
- # @patch('ooinstall.openshift_ansible.load_system_facts')
- # def test_ansible_config_specified_in_installer_config(self,
- # load_facts_mock, run_ansible_mock):
- # load_facts_mock.return_value = (MOCK_FACTS, 0)
- # run_ansible_mock.return_value = 0
- # ansible_config = os.path.join(self.work_dir, 'ansible.cfg')
- # config = SAMPLE_CONFIG % 'openshift-enterprise'
- # config = "%s\nansible_config: %s" % (config, ansible_config)
- # self._ansible_config_test(load_facts_mock, run_ansible_mock,
- # config, None, ansible_config)
- # #pylint: disable=too-many-arguments
- # # This method allows for drastically simpler tests to write, and the args
- # # are all useful.
- # def _ansible_config_test(self, load_facts_mock, run_ansible_mock,
- # installer_config, ansible_config_cli=None, expected_result=None):
- # """
- # Utility method for testing the ways you can specify the ansible config.
- # """
- # load_facts_mock.return_value = (MOCK_FACTS, 0)
- # run_ansible_mock.return_value = 0
- # config_file = self.write_config(os.path.join(self.work_dir,
- # 'ooinstall.conf'), installer_config)
- # self.cli_args.extend(["-c", config_file])
- # if ansible_config_cli:
- # self.cli_args.extend(["--ansible-config", ansible_config_cli])
- # self.cli_args.append("install")
- # result = self.runner.invoke(cli.cli, self.cli_args)
- # self.assert_result(result, 0)
- # # Test the env vars for facts playbook:
- # facts_env_vars = load_facts_mock.call_args[0][2]
- # if expected_result:
- # self.assertEquals(expected_result, facts_env_vars['ANSIBLE_CONFIG'])
- # else:
- # # If user running test has rpm installed, this might be set to default:
- # self.assertTrue('ANSIBLE_CONFIG' not in facts_env_vars or
- # facts_env_vars['ANSIBLE_CONFIG'] == cli.DEFAULT_ANSIBLE_CONFIG)
- # # Test the env vars for main playbook:
- # env_vars = run_ansible_mock.call_args[0][2]
- # if expected_result:
- # self.assertEquals(expected_result, env_vars['ANSIBLE_CONFIG'])
- # else:
- # # If user running test has rpm installed, this might be set to default:
- # #
- # # By default we will use the quiet config
- # self.assertTrue('ANSIBLE_CONFIG' not in env_vars or
- # env_vars['ANSIBLE_CONFIG'] == cli.QUIET_ANSIBLE_CONFIG)
- # unattended with bad config file and no installed hosts (without --force)
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_bad_config(self, load_facts_mock, run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS, 0)
- run_playbook_mock.return_value = 0
- config_file = self.write_config(os.path.join(self.work_dir,
- 'ooinstall.conf'), BAD_CONFIG % 'openshift-enterprise')
- self.cli_args.extend(["-c", config_file, "install"])
- result = self.runner.invoke(cli.cli, self.cli_args)
- self.assertEquals(1, result.exit_code)
- self.assertTrue("You must specify either an ip or hostname"
- in result.output)
- #unattended with three masters, one node, and haproxy
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_quick_ha_full_run(self, load_facts_mock, run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
- run_playbook_mock.return_value = 0
- config_file = self.write_config(os.path.join(self.work_dir,
- 'ooinstall.conf'), QUICKHA_CONFIG % 'openshift-enterprise')
- self.cli_args.extend(["-c", config_file, "install"])
- result = self.runner.invoke(cli.cli, self.cli_args)
- self.assert_result(result, 0)
- # Make sure we ran on the expected masters and nodes:
- hosts = run_playbook_mock.call_args[0][1]
- hosts_to_run_on = run_playbook_mock.call_args[0][2]
- self.assertEquals(6, len(hosts))
- self.assertEquals(6, len(hosts_to_run_on))
- #unattended with two masters, one node, and haproxy
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_quick_ha_only_2_masters(self, load_facts_mock, run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
- run_playbook_mock.return_value = 0
- config_file = self.write_config(os.path.join(self.work_dir,
- 'ooinstall.conf'), QUICKHA_2_MASTER_CONFIG % 'openshift-enterprise')
- self.cli_args.extend(["-c", config_file, "install"])
- result = self.runner.invoke(cli.cli, self.cli_args)
- # This is an invalid config:
- self.assert_result(result, 1)
- self.assertTrue("A minimum of 3 masters are required" in result.output)
- #unattended with three masters, one node, but no load balancer specified:
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_quick_ha_no_lb(self, load_facts_mock, run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
- run_playbook_mock.return_value = 0
- config_file = self.write_config(os.path.join(self.work_dir,
- 'ooinstall.conf'), QUICKHA_CONFIG_NO_LB % 'openshift-enterprise')
- self.cli_args.extend(["-c", config_file, "install"])
- result = self.runner.invoke(cli.cli, self.cli_args)
- # This is not a valid input:
- self.assert_result(result, 1)
- self.assertTrue('No master load balancer specified in config' in result.output)
- #unattended with three masters, one node, and one of the masters reused as load balancer:
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_quick_ha_reused_lb(self, load_facts_mock, run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
- run_playbook_mock.return_value = 0
- config_file = self.write_config(os.path.join(self.work_dir,
- 'ooinstall.conf'), QUICKHA_CONFIG_REUSED_LB % 'openshift-enterprise')
- self.cli_args.extend(["-c", config_file, "install"])
- result = self.runner.invoke(cli.cli, self.cli_args)
- # This is not a valid configuration:
- self.assert_result(result, 1)
- #unattended with preconfigured lb
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_quick_ha_preconfigured_lb(self, load_facts_mock, run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
- run_playbook_mock.return_value = 0
- config_file = self.write_config(os.path.join(self.work_dir,
- 'ooinstall.conf'), QUICKHA_CONFIG_PRECONFIGURED_LB % 'openshift-enterprise')
- self.cli_args.extend(["-c", config_file, "install"])
- result = self.runner.invoke(cli.cli, self.cli_args)
- self.assert_result(result, 0)
- # Make sure we ran on the expected masters and nodes:
- hosts = run_playbook_mock.call_args[0][1]
- hosts_to_run_on = run_playbook_mock.call_args[0][2]
- self.assertEquals(6, len(hosts))
- self.assertEquals(6, len(hosts_to_run_on))
- class AttendedCliTests(OOCliFixture):
- def setUp(self):
- OOCliFixture.setUp(self)
- # Doesn't exist but keeps us from reading the local users config:
- self.config_file = os.path.join(self.work_dir, 'config.yml')
- self.cli_args.extend(["-c", self.config_file])
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_full_run(self, load_facts_mock, run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS, 0)
- run_playbook_mock.return_value = 0
- cli_input = build_input(hosts=[
- ('10.0.0.1', True, False),
- ('10.0.0.2', False, False),
- ('10.0.0.3', False, False)],
- ssh_user='root',
- variant_num=1,
- confirm_facts='y',
- storage='10.1.0.1',)
- self.cli_args.append("install")
- result = self.runner.invoke(cli.cli, self.cli_args,
- input=cli_input)
- self.assert_result(result, 0)
- self._verify_load_facts(load_facts_mock)
- self._verify_run_playbook(run_playbook_mock, 4, 4)
- written_config = read_yaml(self.config_file)
- self._verify_config_hosts(written_config, 4)
- inventory = ConfigParser.ConfigParser(allow_no_value=True)
- inventory.read(os.path.join(self.work_dir, 'hosts'))
- self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.1',
- 'openshift_schedulable=False')
- self.assert_inventory_host_var_unset(inventory, 'nodes', '10.0.0.2',
- 'openshift_schedulable=True')
- self.assert_inventory_host_var_unset(inventory, 'nodes', '10.0.0.3',
- 'openshift_schedulable=True')
- # interactive with config file and some installed some uninstalled hosts
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_add_nodes(self, load_facts_mock, run_playbook_mock):
- # Modify the mock facts to return a version indicating OpenShift
- # is already installed on our master, and the first node.
- mock_facts = copy.deepcopy(MOCK_FACTS)
- mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
- mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
- load_facts_mock.return_value = (mock_facts, 0)
- run_playbook_mock.return_value = 0
- cli_input = build_input(hosts=[
- ('10.0.0.1', True, False),
- ('10.0.0.2', False, False),
- ],
- add_nodes=[('10.0.0.3', False, False)],
- ssh_user='root',
- variant_num=1,
- confirm_facts='y',
- storage='10.0.0.1',)
- self.cli_args.append("install")
- result = self.runner.invoke(cli.cli,
- self.cli_args,
- input=cli_input)
- self.assert_result(result, 0)
- self._verify_load_facts(load_facts_mock)
- self._verify_run_playbook(run_playbook_mock, 3, 2)
- written_config = read_yaml(self.config_file)
- self._verify_config_hosts(written_config, 3)
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_fresh_install_with_config(self, load_facts_mock, run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS, 0)
- run_playbook_mock.return_value = 0
- config_file = self.write_config(os.path.join(self.work_dir,
- 'ooinstall.conf'),
- SAMPLE_CONFIG % 'openshift-enterprise')
- cli_input = build_input(confirm_facts='y')
- self.cli_args.extend(["-c", config_file])
- self.cli_args.append("install")
- result = self.runner.invoke(cli.cli,
- self.cli_args,
- input=cli_input)
- self.assert_result(result, 0)
- self._verify_load_facts(load_facts_mock)
- self._verify_run_playbook(run_playbook_mock, 3, 3)
- written_config = read_yaml(config_file)
- self._verify_config_hosts(written_config, 3)
- #interactive with config file and all installed hosts
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_get_hosts_to_run_on(self, load_facts_mock, run_playbook_mock):
- mock_facts = copy.deepcopy(MOCK_FACTS)
- mock_facts['10.0.0.1']['common']['version'] = "3.0.0"
- mock_facts['10.0.0.2']['common']['version'] = "3.0.0"
- cli_input = build_input(hosts=[
- ('10.0.0.1', True, False),
- ],
- add_nodes=[('10.0.0.2', False, False)],
- ssh_user='root',
- variant_num=1,
- schedulable_masters_ok=True,
- confirm_facts='y',
- storage='10.0.0.1',)
- self._verify_get_hosts_to_run_on(mock_facts, load_facts_mock,
- run_playbook_mock,
- cli_input,
- exp_hosts_len=2,
- exp_hosts_to_run_on_len=2,
- force=False)
- #interactive multimaster: one more node than master
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_ha_dedicated_node(self, load_facts_mock, run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
- run_playbook_mock.return_value = 0
- cli_input = build_input(hosts=[
- ('10.0.0.1', True, False),
- ('10.0.0.2', True, False),
- ('10.0.0.3', True, False),
- ('10.0.0.4', False, False)],
- ssh_user='root',
- variant_num=1,
- confirm_facts='y',
- master_lb=('10.0.0.5', False),
- storage='10.1.0.1',)
- self.cli_args.append("install")
- result = self.runner.invoke(cli.cli, self.cli_args,
- input=cli_input)
- self.assert_result(result, 0)
- self._verify_load_facts(load_facts_mock)
- self._verify_run_playbook(run_playbook_mock, 6, 6)
- written_config = read_yaml(self.config_file)
- self._verify_config_hosts(written_config, 6)
- inventory = ConfigParser.ConfigParser(allow_no_value=True)
- inventory.read(os.path.join(self.work_dir, 'hosts'))
- self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.1',
- 'openshift_schedulable=False')
- self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.2',
- 'openshift_schedulable=False')
- self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.3',
- 'openshift_schedulable=False')
- self.assert_inventory_host_var_unset(inventory, 'nodes', '10.0.0.4',
- 'openshift_schedulable=True')
- self.assertTrue(inventory.has_section('etcd'))
- self.assertEquals(3, len(inventory.items('etcd')))
- #interactive multimaster: identical masters and nodes
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_ha_no_dedicated_nodes(self, load_facts_mock, run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
- run_playbook_mock.return_value = 0
- cli_input = build_input(hosts=[
- ('10.0.0.1', True, False),
- ('10.0.0.2', True, False),
- ('10.0.0.3', True, False)],
- ssh_user='root',
- variant_num=1,
- confirm_facts='y',
- master_lb=('10.0.0.5', False),
- storage='10.1.0.1',)
- self.cli_args.append("install")
- result = self.runner.invoke(cli.cli, self.cli_args,
- input=cli_input)
- self.assert_result(result, 0)
- self._verify_load_facts(load_facts_mock)
- self._verify_run_playbook(run_playbook_mock, 5, 5)
- written_config = read_yaml(self.config_file)
- self._verify_config_hosts(written_config, 5)
- inventory = ConfigParser.ConfigParser(allow_no_value=True)
- inventory.read(os.path.join(self.work_dir, 'hosts'))
- self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.1',
- 'openshift_schedulable=True')
- self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.2',
- 'openshift_schedulable=True')
- self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.3',
- 'openshift_schedulable=True')
- # Checks the inventory (as a ConfigParser) for the given host, host
- # variable, and expected value.
- def assert_inventory_host_var(self, inventory, section, host, variable):
- # Config parser splits on the first "=", so we end up with:
- # 'hostname key1' -> 'val1 key2=val2 key3=val3'
- #
- # Convert to something easier to test:
- for (a, b) in inventory.items(section):
- full_line = "%s=%s" % (a, b)
- tokens = full_line.split()
- if tokens[0] == host:
- found = False
- for token in tokens:
- if token == variable:
- found = True
- continue
- self.assertTrue("Unable to find %s in line: %s" %
- (variable, full_line), found)
- return
- self.fail("unable to find host %s in inventory" % host)
- def assert_inventory_host_var_unset(self, inventory, section, host, variable):
- # Config parser splits on the first "=", so we end up with:
- # 'hostname key1' -> 'val1 key2=val2 key3=val3'
- #
- # Convert to something easier to test:
- for (a, b) in inventory.items(section):
- full_line = "%s=%s" % (a, b)
- tokens = full_line.split()
- if tokens[0] == host:
- self.assertFalse(("%s=" % variable) in full_line,
- msg='%s host variable was set: %s' %
- (variable, full_line))
- return
- self.fail("unable to find host %s in inventory" % host)
- #interactive multimaster: attempting to use a master as the load balancer should fail:
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_ha_reuse_master_as_lb(self, load_facts_mock, run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS_QUICKHA, 0)
- run_playbook_mock.return_value = 0
- cli_input = build_input(hosts=[
- ('10.0.0.1', True, False),
- ('10.0.0.2', True, False),
- ('10.0.0.3', False, False),
- ('10.0.0.4', True, False)],
- ssh_user='root',
- variant_num=1,
- confirm_facts='y',
- master_lb=(['10.0.0.2', '10.0.0.5'], False),
- storage='10.1.0.1')
- self.cli_args.append("install")
- result = self.runner.invoke(cli.cli, self.cli_args,
- input=cli_input)
- self.assert_result(result, 0)
- #interactive all-in-one
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_all_in_one(self, load_facts_mock, run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS, 0)
- run_playbook_mock.return_value = 0
- cli_input = build_input(hosts=[
- ('10.0.0.1', True, False)],
- ssh_user='root',
- variant_num=1,
- confirm_facts='y',
- storage='10.0.0.1')
- self.cli_args.append("install")
- result = self.runner.invoke(cli.cli, self.cli_args,
- input=cli_input)
- self.assert_result(result, 0)
- self._verify_load_facts(load_facts_mock)
- self._verify_run_playbook(run_playbook_mock, 1, 1)
- written_config = read_yaml(self.config_file)
- self._verify_config_hosts(written_config, 1)
- inventory = ConfigParser.ConfigParser(allow_no_value=True)
- inventory.read(os.path.join(self.work_dir, 'hosts'))
- self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.1',
- 'openshift_schedulable=True')
- @patch('ooinstall.openshift_ansible.run_main_playbook')
- @patch('ooinstall.openshift_ansible.load_system_facts')
- def test_gen_inventory(self, load_facts_mock, run_playbook_mock):
- load_facts_mock.return_value = (MOCK_FACTS, 0)
- run_playbook_mock.return_value = 0
- cli_input = build_input(hosts=[
- ('10.0.0.1', True, False),
- ('10.0.0.2', False, False),
- ('10.0.0.3', False, False)],
- ssh_user='root',
- variant_num=1,
- confirm_facts='y',
- storage='10.1.0.1',)
- self.cli_args.append("install")
- self.cli_args.append("--gen-inventory")
- result = self.runner.invoke(cli.cli, self.cli_args,
- input=cli_input)
- self.assert_result(result, 0)
- self._verify_load_facts(load_facts_mock)
- # Make sure run playbook wasn't called:
- self.assertEquals(0, len(run_playbook_mock.mock_calls))
- written_config = read_yaml(self.config_file)
- self._verify_config_hosts(written_config, 4)
- inventory = ConfigParser.ConfigParser(allow_no_value=True)
- inventory.read(os.path.join(self.work_dir, 'hosts'))
- self.assert_inventory_host_var(inventory, 'nodes', '10.0.0.1',
- 'openshift_schedulable=False')
- self.assert_inventory_host_var_unset(inventory, 'nodes', '10.0.0.2',
- 'openshift_schedulable=True')
- self.assert_inventory_host_var_unset(inventory, 'nodes', '10.0.0.3',
- 'openshift_schedulable=True')
- # TODO: test with config file, attended add node
- # TODO: test with config file, attended new node already in config file
- # TODO: test with config file, attended new node already in config file, plus manually added nodes
- # TODO: test with config file, attended reject facts
|