Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
added test for maintenance-mode subcommand and removed old testes rel…
Browse files Browse the repository at this point in the history
…ated to maintenance-mode
  • Loading branch information
Jameer Pathan committed Jun 20, 2019
1 parent d3a6a9b commit ec7236b
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 63 deletions.
1 change: 1 addition & 0 deletions testfm/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
74 changes: 74 additions & 0 deletions testfm/maintenance_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# -*- 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


16 changes: 12 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
from testfm.constants import (
DOGFOOD_ACTIVATIONKEY,
DOGFOOD_ORG,
foreman_maintain_yml,
katello_ca_consumer,
RHN_PASSWORD,
RHN_USERNAME,
RHN_POOLID,
HOTFIX_URL,
upstream_url,
)
from testfm.maintenance_mode import MaintenanceMode
from testfm.log import logger


Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -181,16 +187,18 @@ 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']:
ansible_module.file(
path=path,
state='absent',
)
ansible_module.lineinfile(
dest=foreman_maintain_yml,
state='absent',
line=":manage_crond: true")
return sync_plan


Expand Down
59 changes: 0 additions & 59 deletions tests/test_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
115 changes: 115 additions & 0 deletions tests/test_maintenance_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
from testfm.maintenance_mode import MaintenanceMode
from testfm.log import logger
import yaml


def test_positive_maintenance_mode(setup_sync_plan, ansible_module):
"""
:param setup_sync_plan:
:param ansible_module:
:return:
"""
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
for i in maintenance_mode_off:
assert i in result['stdout']
# 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()
assert 'stopped' in contacted.values()[0]['ansible_facts']['services']['crond.service']['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 in rules['stdout'] # Assert FOREMAN_MAINTAIN not listed in iptables
# Assert crond.service is running
contacted = ansible_module.service_facts()
assert 'running' in contacted.values()[0]['ansible_facts']['services']['crond.service']['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']

0 comments on commit ec7236b

Please sign in to comment.