def test_c2c_no_peer_account(mocker):
    """
    in this scenario, the accepters connection and the accepters cluster
    have no aws infrastructura account available to set up the peering″
    """
    accepter_cluster = build_cluster(
        # no network_mgmt_accounts here
        name="accepter_cluster",
        vpc="accepter_vpc",
        peering_connections=[
            build_accepter_connection(
                # no network_mgmt_accounts here
                name="peername",
                cluster="requester_cluster",
            )
        ],
    )
    requester_cluster = build_cluster(
        name="requester_cluster",
        vpc="requester_vpc",
        network_mgmt_accounts=["acc"],
        peering_connections=[
            build_requester_connection(name="peername", peer_cluster=accepter_cluster)
        ],
    )

    ocm = MockOCM().auto_speced_mock(mocker)
    awsapi = MockAWSAPI().auto_speced_mock(mocker)

    with pytest.raises(sut.BadTerraformPeeringState) as ex:
        sut.build_desired_state_single_cluster(requester_cluster, ocm, awsapi)
    assert str(ex.value).startswith("[no_account_available]")
def test_c2c_no_vpc_in_aws(mocker):
    """
    in this scenario, there are no VPCs found in AWS
    """
    accepter_cluster = build_cluster(
        name="accepter_cluster",
        vpc="accepter_vpc",
        network_mgmt_accounts=["acc"],
        peering_connections=[
            build_accepter_connection(name="peername", cluster="requester_cluster")
        ],
    )
    requester_cluster = build_cluster(
        name="requester_cluster",
        vpc="requester_vpc",
        network_mgmt_accounts=["acc"],
        peering_connections=[
            build_requester_connection(name="peername", peer_cluster=accepter_cluster)
        ],
    )

    ocm = (
        MockOCM()
        .register("requester_cluster", "acc", "terraform", "r")
        .register("accepter_cluster", "acc", "terraform", "a")
        .auto_speced_mock(mocker)
    )

    awsapi = MockAWSAPI().auto_speced_mock(mocker)

    with pytest.raises(sut.BadTerraformPeeringState) as ex:
        sut.build_desired_state_single_cluster(requester_cluster, ocm, awsapi)
    assert str(ex.value).endswith("could not find VPC ID for cluster")
def test_c2c_no_matches(mocker):
    """
    in this scenario, the accepter cluster has no cluster-vpc-accepter
    connection that references back to the requester cluster
    """
    accepter_cluster = build_cluster(
        name="accepter_cluster",
        vpc="accepter_vpc",
        network_mgmt_accounts=["acc"],
        peering_connections=[
            build_accepter_connection(name="peername", cluster="not_a_matching_cluster")
        ],
    )
    requester_cluster = build_cluster(
        name="requester_cluster",
        vpc="requester_vpc",
        network_mgmt_accounts=["acc"],
        peering_connections=[
            build_requester_connection(name="peername", peer_cluster=accepter_cluster)
        ],
    )

    with pytest.raises(sut.BadTerraformPeeringState) as ex:
        sut.build_desired_state_single_cluster(
            requester_cluster,
            MockOCM().auto_speced_mock(mocker),
            MockAWSAPI().auto_speced_mock(mocker),
        )
    assert str(ex.value).startswith("[no_matching_peering]")
def test_c2c_no_peerings(mocker):
    """
    in this scenario, the requester cluster has no peerings defines,
    which results in an empty desired state
    """
    requester_cluster = build_cluster(
        name="requester_cluster",
        vpc="requester_vpc",
        network_mgmt_accounts=["acc"],
        peering_connections=[],
    )
    result = sut.build_desired_state_single_cluster(
        requester_cluster,
        MockOCM().auto_speced_mock(mocker),
        MockAWSAPI().auto_speced_mock(mocker),
    )
    assert not result
def test_c2c_all_clusters(mocker):
    """
    happy path
    """

    accepter_cluster = build_cluster(
        name="accepter_cluster",
        vpc="accepter_vpc",
        network_mgmt_accounts=["acc"],
        peering_connections=[
            build_accepter_connection(name="peername", cluster="requester_cluster")
        ],
    )
    requester_cluster = build_cluster(
        name="requester_cluster",
        vpc="requester_vpc",
        network_mgmt_accounts=["acc"],
        peering_connections=[
            build_requester_connection(name="peername", peer_cluster=accepter_cluster)
        ],
    )

    ocm_map = {
        "requester_cluster": MockOCM()
        .register("requester_cluster", "acc", "terraform", "r")
        .register("accepter_cluster", "acc", "terraform", "a")
        .auto_speced_mock(mocker)
    }

    awsapi = (
        MockAWSAPI()
        .register(
            vpc="accepter_vpc",
            vpc_id="accepter_vpc_id",
            route_tables=["accepter_rt_id"],
        )
        .register(
            vpc="requester_vpc",
            vpc_id="requester_vpc_id",
            route_tables=["requester_rt_id"],
        )
        .auto_speced_mock(mocker)
    )

    result, error = sut.build_desired_state_all_clusters(
        [requester_cluster], ocm_map, awsapi
    )
    expected = [
        {
            "connection_provider": "cluster-vpc-requester",
            "connection_name": "peername",
            "requester": {
                "cidr_block": "requester_vpc",
                "region": "region",
                "vpc_id": "requester_vpc_id",
                "route_table_ids": ["requester_rt_id"],
                "account": {
                    "name": "acc",
                    "uid": "acc",
                    "terraformUsername": "******",
                    "automationToken": {},
                    "assume_role": "arn::::r",
                    "assume_region": "region",
                    "assume_cidr": "requester_vpc",
                },
                "peer_owner_id": "a",
            },
            "accepter": {
                "cidr_block": "accepter_vpc",
                "region": "region",
                "vpc_id": "accepter_vpc_id",
                "route_table_ids": ["accepter_rt_id"],
                "account": {
                    "name": "acc",
                    "uid": "acc",
                    "terraformUsername": "******",
                    "automationToken": {},
                    "assume_role": "arn::::a",
                    "assume_region": "region",
                    "assume_cidr": "accepter_vpc",
                },
            },
            "deleted": False,
        }
    ]
    assert expected == result
    assert not error