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

added test for maintenance-mode subcommand and removed old tests related to maintenance-mode #77

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
72 changes: 72 additions & 0 deletions testfm/maintenance_mode.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 14 additions & 5 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 @@ -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="./"
Expand All @@ -181,16 +188,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
140 changes: 140 additions & 0 deletions tests/test_maintenance_mode.py
Original file line number Diff line number Diff line change
@@ -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']