From 77e6f75515a8a46bb7252845c7d29f62198fc5a3 Mon Sep 17 00:00:00 2001 From: Jameer Pathan Date: Thu, 20 Jun 2019 16:51:27 +0530 Subject: [PATCH] added test for maintenance-mode subcommand and removed old testes related to maintenance-mode --- testfm/constants.py | 1 + testfm/maintenance_mode.py | 72 +++++++++++++++++ tests/conftest.py | 19 +++-- tests/test_advanced.py | 59 -------------- tests/test_maintenance_mode.py | 140 +++++++++++++++++++++++++++++++++ 5 files changed, 227 insertions(+), 64 deletions(-) create mode 100644 testfm/maintenance_mode.py create mode 100644 tests/test_maintenance_mode.py diff --git a/testfm/constants.py b/testfm/constants.py index 854df5e..e7399c6 100644 --- a/testfm/constants.py +++ b/testfm/constants.py @@ -32,3 +32,4 @@ sat_65_repo = ['rhel-7-server-ansible-2.6-rpms', 'rhel-7-server-rpms', 'rhel-7-server-satellite-6.5-rpms', 'rhel-7-server-satellite-maintenance-6-rpms', 'rhel-7-server-satellite-tools-6.5-rpms', 'rhel-server-rhscl-7-rpms'] +foreman_maintain_yml = '/etc/foreman-maintain/foreman_maintain.yml' diff --git a/testfm/maintenance_mode.py b/testfm/maintenance_mode.py new file mode 100644 index 0000000..5fe7dca --- /dev/null +++ b/testfm/maintenance_mode.py @@ -0,0 +1,72 @@ +# -*- encoding: utf-8 -*- +""" +Usage: + foreman-maintain maintenance-mode [OPTIONS] SUBCOMMAND [ARG] ... + +Parameters: + SUBCOMMAND subcommand + [ARG] ... subcommand arguments + +Subcommands: + start Start maintenance-mode + stop Stop maintenance-mode + status Get maintenance-mode status + is-enabled Get maintenance-mode status code + +Options: + -h, --help print help +""" +from testfm.base import Base + + +class MaintenanceMode(Base): + """Manipulates Foreman-maintain's maintenance-mode command""" + command_base = 'maintenance-mode' + + @classmethod + def start(cls, options=None): + """foreman-maintain maintenance-mode start [OPTIONS]""" + cls.command_sub = 'start' + + if options is None: + options = {} + + result = cls._construct_command(options) + + return result + + @classmethod + def stop(cls, options=None): + """foreman-maintain maintenance-mode stop [OPTIONS]""" + cls.command_sub = 'stop' + + if options is None: + options = {} + + result = cls._construct_command(options) + + return result + + @classmethod + def status(cls, options=None): + """foreman-maintain maintenance-mode status [OPTIONS]""" + cls.command_sub = 'status' + + if options is None: + options = {} + + result = cls._construct_command(options) + + return result + + @classmethod + def is_enabled(cls, options=None): + """foreman-maintain maintenance-mode is-enabled [OPTIONS]""" + cls.command_sub = 'is-enabled' + + if options is None: + options = {} + + result = cls._construct_command(options) + + return result diff --git a/tests/conftest.py b/tests/conftest.py index 60b25c4..6826036 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,6 +6,7 @@ from testfm.constants import ( DOGFOOD_ACTIVATIONKEY, DOGFOOD_ORG, + foreman_maintain_yml, katello_ca_consumer, RHN_PASSWORD, RHN_USERNAME, @@ -13,6 +14,7 @@ HOTFIX_URL, upstream_url, ) +from testfm.maintenance_mode import MaintenanceMode from testfm.log import logger @@ -137,9 +139,13 @@ def setup_install_pexpect(ansible_module): @pytest.fixture(scope='function') def setup_sync_plan(request, ansible_module): """This fixture is used to create/delete sync-plan. - It is used by test test_positive_sync_plan_disable_enable of test_advanced.py. + It is used by tests test_positive_sync_plan_disable_enable and test_positive_maintenance_mode. """ sync_plan_name = gen_string('alpha') + ansible_module.lineinfile( + dest=foreman_maintain_yml, + insertafter='EOF', + line=":manage_crond: true") def sync_plan(): org_ids = [] @@ -167,7 +173,8 @@ def sync_plan(): org_ids.append(id['Id']) for id in org_ids: ansible_module.shell( - "hammer --output yaml sync-plan list --organization-id {} | sed -n '1!p' >> /tmp/sync_id.yaml".format(id)) + "hammer --output yaml sync-plan list --organization-id {} | " + "sed -n '1!p' >> /tmp/sync_id.yaml".format(id)) ansible_module.fetch( src="/tmp/sync_id.yaml", dest="./" @@ -181,9 +188,7 @@ def sync_plan(): return list(set(sync_ids)), sat_hostname def teardown_sync_plan(): - teardown = ansible_module.command( - 'hammer sync-plan delete --name {0} --organization-id 1'.format( - sync_plan_name)) + teardown = ansible_module.command(MaintenanceMode.stop()) for result in teardown.values(): assert result["rc"] == 0 for path in ['/tmp/sync_id.yaml', '/tmp/orgs.yaml']: @@ -191,6 +196,10 @@ def teardown_sync_plan(): path=path, state='absent', ) + ansible_module.lineinfile( + dest=foreman_maintain_yml, + state='absent', + line=":manage_crond: true") return sync_plan diff --git a/tests/test_advanced.py b/tests/test_advanced.py index 6df3a2f..8279ede 100644 --- a/tests/test_advanced.py +++ b/tests/test_advanced.py @@ -95,65 +95,6 @@ def test_positive_foreman_maintain_packages_update(ansible_module): assert "FAIL" not in result['stdout'] -def test_positive_foreman_maintain_disable_maintenance_mode(ansible_module): - """Disable maintenance mode using advanced procedure run - - :id: 3a58ce93-631e-42b6-9b41-1cb620f351e6 - - :setup: - 1. foreman-maintain should be installed. - 2. maintenance mode should be enabled. - - :steps: - 1. Run foreman-maintain advanced procedure run maintenance-mode-disable - - :expectedresults: iptables rules should remove. - - :CaseImportance: Critical - """ - setup = ansible_module.command(Advanced.run_enable_maintenance_mode()) - for result in setup.values(): - logger.info(result['stdout']) - assert "FAIL" not in result['stdout'] - contacted = ansible_module.command(Advanced.run_disable_maintenance_mode()) - for result in contacted.values(): - logger.info(result['stdout']) - assert "FAIL" not in result['stdout'] - check_iptables = ansible_module.command("iptables -L") - for rules in check_iptables.values(): - logger.info(rules['stdout']) - assert "FOREMAN_MAINTAIN" not in rules['stdout'] - - -def test_positive_foreman_maintain_enable_maintenance_mode(ansible_module): - """Enable maintenance mode using advanced procedure run - - :id: a37c78da-430d-48be-b94a-c25699bddb02 - - :setup: - 1. foreman-maintain should be installed. - - :steps: - 1. Run foreman-maintain advanced procedure run maintenance-mode-enable - - :expectedresults: iptables rules should add. - - :CaseImportance: Critical - """ - contacted = ansible_module.command(Advanced.run_enable_maintenance_mode()) - for result in contacted.values(): - logger.info(result['stdout']) - assert "FAIL" not in result['stdout'] - check_iptables = ansible_module.command("iptables -L") - for rules in check_iptables.values(): - logger.info(rules['stdout']) - assert "FOREMAN_MAINTAIN" in rules['stdout'] - teardown = ansible_module.command(Advanced.run_disable_maintenance_mode()) - for result in teardown.values(): - logger.info(result['stdout']) - assert "FAIL" not in result['stdout'] - - def test_positive_foreman_taks_delete_old(ansible_module): """Delete old foreman-tasks using advanced procedure run diff --git a/tests/test_maintenance_mode.py b/tests/test_maintenance_mode.py new file mode 100644 index 0000000..b2159e7 --- /dev/null +++ b/tests/test_maintenance_mode.py @@ -0,0 +1,140 @@ +from testfm.maintenance_mode import MaintenanceMode +from testfm.log import logger +import yaml + + +def test_positive_maintenance_mode(setup_sync_plan, ansible_module): + """Test foreman-maintain maintenance-mode subcommand + + :id: 51d76219-d3cc-43c0-9894-7bcb75c163c3 + + :setup: + 1. foreman-maintain should be installed. + 2. active sync-plans + 3. set :manage_crond: true in /etc/foreman-maintain/foreman_maintain.yml' + + :steps: + 1. Verify that maintenance-mode is Off. + 2. Start maintenance-mode. + 3. Verify that active sync-plans got disabled or not. + 4. Verify that FOREMAN_MAINTAIN is mentioned in iptables list. + 5. Verify that crond.service is stopped. + 6. Validate maintenance-mode status command's output. + 7. Validate maintenance-mode is-enabled command's output. + 8. Stop maintenance-mode. + 9. Verify that disabled sync-plans got re-enabled or not. + 10. Verify that FOREMAN_MAINTAIN is not mentioned in iptables list. + 11. Verify that crond.service is running. + 12. Validate maintenance-mode status command's output. + 13. Validate maintenance-mode is-enabled command's output. + + :expectedresults: foreman-maintain maintenance-mode start/stop able + to disable/enable sync-plan, stop/start crond.service and is able to add + FOREMAN_MAINTAIN chain rule in iptables. + + :CaseImportance: Critical + """ + sync_ids, sat_hostname = setup_sync_plan() + maintenance_mode_off = ['Status of maintenance-mode: Off', 'Iptables chain: absent', + 'sync plans: enabled', 'cron jobs: running'] + maintenance_mode_on = ['Status of maintenance-mode: On', 'Iptables chain: present', + 'sync plans: disabled', 'cron jobs: not running'] + + # Verify maintenance-mode status + setup = ansible_module.command(MaintenanceMode.status()) + for result in setup.values(): + logger.info(result['stdout']) + assert "FAIL" not in result['stdout'] + assert "OK" in result['stdout'] + assert result["rc"] == 0 + # Verify maintenance-mode is-enabled + setup = ansible_module.command(MaintenanceMode.is_enabled()) + for result in setup.values(): + logger.info(result['stdout']) + assert "FAIL" not in result['stdout'] + assert "OK" in result['stdout'] + assert result["rc"] == 1 + assert 'Maintenance mode is Off' in result['stdout'] + + # Verify maintenance-mode start + contacted = ansible_module.command(MaintenanceMode.start()) + for result in contacted.values(): + logger.info(result['stdout']) + assert "FAIL" not in result['stdout'] + assert result["rc"] == 0 + assert "Total {} sync plans are now disabled.".format(len(sync_ids)) in result['stdout'] + ansible_module.fetch( + src="/var/lib/foreman-maintain/data.yml", + dest="./" + ) + with open('./{0}/var/lib/foreman-maintain/data.yml'.format(sat_hostname)) as f: + data_yml = yaml.safe_load(f) + assert len(sync_ids) == len(data_yml[':default'][':sync_plans'][':disabled']) + assert sorted(sync_ids) == sorted(data_yml[':default'][':sync_plans'][':disabled']) + check_iptables = ansible_module.command("iptables -L") + for rules in check_iptables.values(): + logger.info(rules['stdout']) + assert "FOREMAN_MAINTAIN" in rules['stdout'] # Assert FOREMAN_MAINTAIN is listed iptables + # Assert crond.service is stopped + contacted = ansible_module.service_facts() + state = contacted.values()[0]['ansible_facts']['services']['crond.service']['state'] + assert 'stopped' in state + # Verify maintenance-mode status + contacted = ansible_module.command(MaintenanceMode.status()) + for result in contacted.values(): + logger.info(result['stdout']) + assert "FAIL" not in result['stdout'] + assert "OK" in result['stdout'] + assert result["rc"] == 0 + for i in maintenance_mode_on: + assert i in result['stdout'] + # Verify maintenance-mode is-enabled + contacted = ansible_module.command(MaintenanceMode.is_enabled()) + for result in contacted.values(): + logger.info(result['stdout']) + assert "FAIL" not in result['stdout'] + assert "OK" in result['stdout'] + assert result["rc"] == 0 + assert 'Maintenance mode is On' in result['stdout'] + + # Verify maintenance-mode stop + contacted = ansible_module.command(MaintenanceMode.stop()) + for result in contacted.values(): + logger.info(result['stdout']) + assert result["rc"] == 0 + assert "FAIL" not in result['stdout'] + assert "Total {} sync plans are now enabled.".format(len(sync_ids)) in result['stdout'] + ansible_module.fetch( + src="/var/lib/foreman-maintain/data.yml", + dest="./" + ) + with open('./{0}/var/lib/foreman-maintain/data.yml'.format(sat_hostname)) as f: + data_yml = yaml.safe_load(f) + assert len(sync_ids) == len(data_yml[':default'][':sync_plans'][':enabled']) + assert sorted(sync_ids) == sorted(data_yml[':default'][':sync_plans'][':enabled']) + check_iptables = ansible_module.command("iptables -L") + for rules in check_iptables.values(): + logger.info(rules['stdout']) + # Assert FOREMAN_MAINTAIN not listed in iptables + assert "FOREMAN_MAINTAIN" not in rules['stdout'] + # Assert crond.service is running + contacted = ansible_module.service_facts() + state = contacted.values()[0]['ansible_facts']['services']['crond.service']['state'] + assert 'running' in state + # Verify maintenance-mode status + contacted = ansible_module.command(MaintenanceMode.status()) + for result in contacted.values(): + logger.info(result['stdout']) + assert "FAIL" not in result['stdout'] + assert "OK" in result['stdout'] + assert result["rc"] == 0 + for i in maintenance_mode_off: + assert i in result['stdout'] + # Verify maintenance-mode is-enabled + contacted = ansible_module.command(MaintenanceMode.is_enabled()) + for result in contacted.values(): + logger.info(result['stdout']) + assert "FAIL" not in result['stdout'] + assert "OK" in result['stdout'] + assert result["rc"] == 1 + assert 'Maintenance mode is Off' in result['stdout']