"""
A Testcase to remove mon from
when I/O's are happening.

Polarion-ID- OCS-355

"""

import logging
import pytest
from ocs_ci.ocs import ocp, constants
from ocs_ci.framework.testlib import tier4, ManageTest
from ocs_ci.framework import config
from ocs_ci.ocs.resources import pod
from tests.helpers import run_io_with_rados_bench, delete_cephblockpool
from ocs_ci.ocs.cluster import CephCluster
from ocs_ci.utility.retry import retry
from ocs_ci.ocs.exceptions import CephHealthException


log = logging.getLogger(__name__)


@retry(CephHealthException, 8, 3, 1)
def verify_mon_pod_up(ceph_cluster, pods):
    """
    Verify mon pods are in Running state.

    Returns:
        bool: True for wait for the resource, False otherwise

    """
    log.info(f"Verifying all mons pods are up and Running")
    ceph_cluster.cluster_health_check(timeout=3)
    ret = pods.wait_for_resource(
        condition=constants.STATUS_RUNNING, selector='app=rook-ceph-mon',
        resource_count=3, timeout=700)
    log.info(f"waited for all mon pod to come up and running {ret}")
    return ret


def run_io_on_pool():
    """
    Runs the I/O on the pool and delete the pool

    Returns: A thread of I/O
    """
    tools_pod = pod.get_ceph_tools_pod()
    tools_pod.add_role(role='client')

    return run_io_with_rados_bench(
        ceph_pods=[tools_pod],
        config={'time': 45, 'cleanup': False,
                'pool': 'test-pool'
                }
    )


@tier4
@pytest.mark.polarion_id("OCS-355")
class TestRemoveMonFromCluster(ManageTest):

    def test_remove_mon_pod_from_cluster(self):
        """
        To remove mon pod from the cluster
        after the I/O is performed on the pool
        and waiting for the operator to create a
        new mon pod on its own

        """
        ceph_cluster = CephCluster()
        pods = ocp.OCP(
            kind=constants.POD, namespace=config.ENV_DATA['cluster_namespace']
        )
        list_mons = ceph_cluster.get_mons_from_cluster()
        assert len(list_mons) > 1, pytest.skip(
            "INVALID: Mon count should be more than one to delete."
        )
        assert run_io_on_pool(), 'Failed to run I/O on the pool'
        assert delete_cephblockpool('test-pool'), 'Failed to delete pool'
        ceph_cluster.cluster_health_check(timeout=0)
        ceph_cluster.remove_mon_from_cluster()
        assert verify_mon_pod_up(ceph_cluster, pods), f"Mon pods are not up and running state"
        ceph_cluster.cluster_health_check(timeout=60)

[((21, 6, 21, 33), 'logging.getLogger', 'logging.getLogger', ({(21, 24, 21, 32): '__name__'}, {}), '(__name__)', False, 'import logging\n'), ((24, 1, 24, 36), 'ocs_ci.utility.retry.retry', 'retry', ({(24, 7, 24, 26): 'CephHealthException', (24, 28, 24, 29): '(8)', (24, 31, 24, 32): '(3)', (24, 34, 24, 35): '(1)'}, {}), '(CephHealthException, 8, 3, 1)', False, 'from ocs_ci.utility.retry import retry\n'), ((60, 1, 60, 35), 'pytest.mark.polarion_id', 'pytest.mark.polarion_id', ({(60, 25, 60, 34): '"""OCS-355"""'}, {}), "('OCS-355')", False, 'import pytest\n'), ((48, 16, 48, 40), 'ocs_ci.ocs.resources.pod.get_ceph_tools_pod', 'pod.get_ceph_tools_pod', ({}, {}), '()', False, 'from ocs_ci.ocs.resources import pod\n'), ((51, 11, 56, 5), 'tests.helpers.run_io_with_rados_bench', 'run_io_with_rados_bench', (), '', False, 'from tests.helpers import run_io_with_rados_bench, delete_cephblockpool\n'), ((71, 23, 71, 36), 'ocs_ci.ocs.cluster.CephCluster', 'CephCluster', ({}, {}), '()', False, 'from ocs_ci.ocs.cluster import CephCluster\n'), ((72, 15, 74, 9), 'ocs_ci.ocs.ocp.OCP', 'ocp.OCP', (), '', False, 'from ocs_ci.ocs import ocp, constants\n'), ((76, 35, 78, 9), 'pytest.skip', 'pytest.skip', ({(77, 12, 77, 67): '"""INVALID: Mon count should be more than one to delete."""'}, {}), "('INVALID: Mon count should be more than one to delete.')", False, 'import pytest\n'), ((80, 15, 80, 48), 'tests.helpers.delete_cephblockpool', 'delete_cephblockpool', ({(80, 36, 80, 47): '"""test-pool"""'}, {}), "('test-pool')", False, 'from tests.helpers import run_io_with_rados_bench, delete_cephblockpool\n')]