def _get_cluster_phase(cluster_name,
                       test_runner_username,
                       org_name=None,
                       vdc_name=None):  # noqa: E501
    if not org_name and not vdc_name:
        org_name = env.TEST_ORG
        vdc_name = env.TEST_VDC
    cmd_list = [
        testutils.CMD_BINDER(
            cmd=env.USERNAME_TO_LOGIN_CMD[test_runner_username],
            exit_code=0,
            validate_output_func=None,
            test_user=test_runner_username),
        testutils.CMD_BINDER(
            cmd=f"cse cluster info {cluster_name} -o {org_name} -v {vdc_name}",
            exit_code=0,
            validate_output_func=None,
            test_user=test_runner_username),
    ]
    result = testutils.execute_commands(cmd_list=cmd_list,
                                        logger=PYTEST_LOGGER)[-1]  # noqa: E501
    if result.exit_code != 0:
        raise Exception("Cluster {cluster_name} not found.")
    match = re.search(r'phase: (\w+:\w+)', result.output)
    return match[1]
def test_0060_vcd_cse_cluster_list(test_runner_username):
    cmd_list = [
        testutils.CMD_BINDER(
            cmd=env.USERNAME_TO_LOGIN_CMD[test_runner_username],  # noqa: E501
            exit_code=0,
            validate_output_func=None,
            test_user=test_runner_username),
        testutils.CMD_BINDER(cmd="cse cluster list",
                             exit_code=0,
                             validate_output_func=None,
                             test_user=test_runner_username),
        testutils.CMD_BINDER(cmd=env.USER_LOGOUT_CMD,
                             exit_code=0,
                             validate_output_func=None,
                             test_user=test_runner_username)
    ]

    testutils.execute_commands(cmd_list, logger=PYTEST_LOGGER)
def test_0090_vcd_cse_cluster_delete(
        cluster_delete_param: CLUSTER_DELETE_TEST_PARAM):  # noqa: E501
    """Test 'vcd cse cluster delete ...' command for various cse users.

    Cluster delete operation on the above create clusters operations-
    cluster Author can only delete self created clusters.
    cluster admin can delete all cluster in the organization.

    :param config: cse config file for vcd configuration
    """
    cmd_list = [
        testutils.CMD_BINDER(
            cmd=env.USER_LOGOUT_CMD[cluster_delete_param.user],  # noqa: E501
            exit_code=0,
            validate_output_func=None,
            test_user=cluster_delete_param.user),
        testutils.CMD_BINDER(cmd=f"org use {cluster_delete_param.org}",
                             exit_code=0,
                             validate_output_func=None,
                             test_user=env.CLUSTER_ADMIN_NAME),
        testutils.CMD_BINDER(
            cmd=
            f"cse cluster delete {cluster_delete_param.cluster_name}",  # noqa: E501
            exit_code=2
            if cluster_delete_param.expect_failure else 0,  # noqa: E501
            validate_output_func=_follow_apply_output(
                expect_failure=cluster_delete_param.expect_failure
            ),  # noqa: E501
            test_user=cluster_delete_param.user),
        testutils.CMD_BINDER(cmd=env.USER_LOGOUT_CMD,
                             exit_code=0,
                             validate_output_func=None,
                             test_user=env.CLUSTER_AUTHOR_NAME),
    ]

    testutils.execute_commands(cmd_list, logger=PYTEST_LOGGER)

    if not cluster_delete_param.expect_failure:
        assert not env.vapp_exists(
            cluster_delete_param.cluster_name,
            vdc_href=env.TEST_VDC_HREF,
            logger=PYTEST_LOGGER), \
            f"Cluster {cluster_delete_param.cluster_name} exists when it should not"  # noqa: E501
def test_0080_vcd_cse_cluster_config(test_runner_username):
    cmd_list = [
        testutils.CMD_BINDER(
            cmd=env.USERNAME_TO_LOGIN_CMD[test_runner_username],  # noqa: E501
            exit_code=0,
            validate_output_func=None,
            test_user=test_runner_username),
        testutils.CMD_BINDER(
            cmd=
            f"cse cluster config {env.USERNAME_TO_CLUSTER_NAME[test_runner_username]}",  # noqa: E501
            exit_code=0,
            validate_output_func=testutils.validate_yaml_output(
            ),  # noqa: E501
            test_user=test_runner_username),
        testutils.CMD_BINDER(cmd=env.USER_LOGOUT_CMD,
                             exit_code=0,
                             validate_output_func=None,
                             test_user=test_runner_username)
    ]

    testutils.execute_commands(cmd_list, logger=PYTEST_LOGGER)
def test_0030_vcd_cse_system_toggle(
        system_toggle_test_case: SYSTEM_TOGGLE_TEST_PARAM):  # noqa: E501
    """Test `vcd cse system ...` commands.

    Test that on disabling CSE, cluster deployments are no longer
    allowed, and on enabling CSE, cluster deployments are allowed again.
    These commands are combined into 1 test function because only sys admin
    can modify the state of CSE server, org admin/tenant can test cluster
    deployment to ensure that CSE is disabled/enabled. Also, this avoids
    cases such as running the system disable test, and then running the
    cluster operations test, which would fail due to CSE server being
    disabled).
    :param config: cse config file for vcd configuration
    :param test_runner_username: parameterized persona to run tests with
    different users
    """
    cmd_list = [
        testutils.CMD_BINDER(cmd="cse system disable",
                             exit_code=0,
                             validate_output_func=None,
                             test_user=system_toggle_test_case.user),
        testutils.CMD_BINDER(cmd=f"cse cluster apply {env.APPLY_SPEC_PATH}",
                             exit_code=2,
                             validate_output_func=None,
                             test_user=system_toggle_test_case.user),
        testutils.CMD_BINDER(cmd="cse system enable",
                             exit_code=0,
                             validate_output_func=None,
                             test_user=system_toggle_test_case.user),
        testutils.CMD_BINDER(
            cmd=f"cse cluster apply {env.APPLY_SPEC_PATH} ",
            exit_code=0,
            validate_output_func=_follow_apply_output(
                expect_failure=True),  # noqa: E501
            test_user=system_toggle_test_case.user)
    ]

    testutils.execute_commands(cmd_list, logger=PYTEST_LOGGER)
def test_0050_vcd_cse_delete_nfs(test_runner_username):
    """Test delete nfs node command."""
    cluster_name = env.USERNAME_TO_CLUSTER_NAME[test_runner_username]

    cmd_list = [
        testutils.CMD_BINDER(
            cmd=env.USERNAME_TO_LOGIN_CMD[test_runner_username],  # noqa: E501
            exit_code=0,
            validate_output_func=None,
            test_user=test_runner_username),
        testutils.CMD_BINDER(cmd=f"org use {env.TEST_ORG}",
                             exit_code=0,
                             validate_output_func=None,
                             test_user=test_runner_username),
        testutils.CMD_BINDER(cmd=f"vdc use {env.TEST_VDC}",
                             exit_code=0,
                             validate_output_func=None,
                             test_user=test_runner_username)
    ]
    testutils.execute_commands(cmd_list, logger=PYTEST_LOGGER)

    cmd_list = [
        testutils.CMD_BINDER(
            cmd=
            f"cse cluster info {env.USERNAME_TO_CLUSTER_NAME[test_runner_username]}",  # noqa: E501
            exit_code=0,
            validate_output_func=None,
            test_user=test_runner_username)
    ]
    cmd_results = testutils.execute_commands(cmd_list, logger=PYTEST_LOGGER)

    nfs_node = get_nfs_node(cmd_results[0].output)

    cmd_list = [
        testutils.CMD_BINDER(
            cmd=
            f"cse cluster delete-nfs {cluster_name} {nfs_node}",  # noqa: E501
            exit_code=0,
            validate_output_func=None,
            test_user=test_runner_username),
        testutils.CMD_BINDER(
            cmd=f"cse cluster info {cluster_name}",
            exit_code=0,
            validate_output_func=validate_if_node_not_present(
                nfs_node),  # noqa: E501
            test_user=test_runner_username)
    ]
    testutils.execute_commands(cmd_list, logger=PYTEST_LOGGER)
def test_0040_vcd_cse_cluster_apply(
        cluster_apply_param: CLUSTER_APPLY_TEST_PARAM):  # noqa: E501
    """Test 'vcd cse cluster create ...' command for various cse users.

    Test cluster creation from different persona's- sys_admin, org_admin
    and k8_author. Created clusters will remain in the system for further
    command tests - list, resize and delete.
    :param config: cse config file for vcd configuration
    :param test_runner_username: parameterized persona to run tests with
    different users
    """
    print(f"Running cluster create operation for {cluster_apply_param.user}")

    exit_code = 0
    if cluster_apply_param.expected_phase == 'UPDATE:FAILED':
        # Validation failure during cluster update will fail the command
        # execution before task is generated.
        exit_code = 2
    expect_failure = "FAILED" in cluster_apply_param.expected_phase

    cmd_list = [
        testutils.CMD_BINDER(
            cmd=f"cse cluster apply {env.APPLY_SPEC_PATH} ",
            exit_code=exit_code,
            validate_output_func=_follow_apply_output(
                expect_failure=expect_failure),  # noqa: E501
            test_user=cluster_apply_param.user)
    ]
    testutils.execute_commands(cmd_list, logger=PYTEST_LOGGER)

    created_cluster_name = cluster_apply_param.cluster_name
    rollback = cluster_apply_param.rollback

    if "CREATE" in cluster_apply_param.expected_phase:
        if "SUCCEEDED" in cluster_apply_param.expected_phase:
            assert env.vapp_exists(created_cluster_name, vdc_href=env.TEST_VDC_HREF), \
                f"Expected VApp to be present for cluster {created_cluster_name}"  # noqa: E501
            assert env.rde_exists(created_cluster_name), \
                f"Expected RDE to be present for cluster {created_cluster_name}"  # noqa: E501
            assert _get_cluster_phase(created_cluster_name, cluster_apply_param.user) == 'CREATE:SUCCEEDED', \
                "Expected RDE phase to be 'CREATE:SUCCEEDED'"  # noqa: E501
        if "FAILED" in cluster_apply_param.expected_phase:
            if rollback:
                assert not env.vapp_exists(created_cluster_name, vdc_href=env.TEST_VDC_HREF), \
                    f"Expected VApp to be present for cluster {created_cluster_name}"  # noqa: E501
                assert not env.rde_exists(created_cluster_name), \
                    f"Expected RDE to be present for cluster {created_cluster_name}"  # noqa: E501
            else:
                # During failure, cannot garauntee vapp creation
                assert env.rde_exists(created_cluster_name), \
                    f"Expected RDE for the cluster {created_cluster_name} to be present"  # noqa: E501
                assert _get_cluster_phase(created_cluster_name, cluster_apply_param.user) == 'CREATE:FAILED', \
                    "Expected RDE phase to be 'CREATE:FAILED'"  # noqa: E501
    if "UPDATE" in cluster_apply_param.expected_phase:
        if "SUCCEEDED" in cluster_apply_param.expected_phase:
            cmd_list = [
                testutils.CMD_BINDER(
                    cmd=f"cse cluster info {created_cluster_name}",  # noqa
                    exit_code=0,
                    validate_output_func=testutils.
                    generate_validate_node_count_func(  # noqa: E501
                        expected_nodes=cluster_apply_param.
                        worker_count,  # noqa: E501
                        rde_version=get_runtime_rde_version_by_vcd_api_version(
                            env.VCD_API_VERSION_TO_USE),  # noqa: E501
                        logger=PYTEST_LOGGER),  # noqa: E501
                    test_user=cluster_apply_param.user)
            ]
            testutils.execute_commands(cmd_list, logger=PYTEST_LOGGER)
        # common for both succeeded and failed conditions
        assert _get_cluster_phase(created_cluster_name, cluster_apply_param.user) == cluster_apply_param.expected_phase, \
            f"Expected RDE phase to be {cluster_apply_param.expected_phase}"  # noqa: E501

    # logout user
    env.CLI_RUNNER.invoke(vcd, env.USER_LOGOUT_CMD, catch_exceptions=False)