def test_kubernetes_smartstack_status(
    mock_get_expected_instance_count_for_namespace,
    mock_get_all_nodes,
    mock_kube_smartstack_replication_checker,
    mock_get_backends,
    mock_match_backends_and_pods,
):
    mock_get_all_nodes.return_value = [{
        "hostname": "host1.paasta.party",
        "attributes": {
            "region": "us-north-3"
        }
    }]

    mock_kube_smartstack_replication_checker.return_value.get_allowed_locations_and_hosts.return_value = {
        "us-north-3":
        [SmartstackHost(hostname="host1.paasta.party", pool="default")]
    }

    mock_get_expected_instance_count_for_namespace.return_value = 2
    mock_backend = HaproxyBackend(
        status="UP",
        svname="host1_1.2.3.4:123",
        check_status="L7OK",
        check_code="0",
        check_duration="1",
        lastchg="9876",
    )
    mock_pod = mock.create_autospec(V1Pod)
    mock_match_backends_and_pods.return_value = [(mock_backend, mock_pod)]

    mock_job_config = kubernetes_tools.KubernetesDeploymentConfig(
        service="fake_service",
        cluster="fake_cluster",
        instance="fake_instance",
        config_dict={"bounce_method": "fake_bounce"},
        branch_dict=None,
    )
    mock_service_namespace_config = ServiceNamespaceConfig()

    smartstack_status = instance.kubernetes_smartstack_status(
        "fake_service",
        "fake_instance",
        mock_job_config,
        mock_service_namespace_config,
        pods=[mock_pod],
        should_return_individual_backends=True,
    )
    assert smartstack_status == {
        "registration":
        "fake_service.fake_instance",
        "expected_backends_per_location":
        2,
        "locations": [{
            "name":
            "us-north-3",
            "running_backends_count":
            1,
            "backends": [{
                "hostname": "host1:1.2.3.4",
                "port": 123,
                "status": "UP",
                "check_status": "L7OK",
                "check_code": "0",
                "last_change": 9876,
                "has_associated_task": True,
                "check_duration": 1,
            }],
        }],
    }
Ejemplo n.º 2
0
async def test_kubernetes_smartstack_status():
    with asynctest.patch(
        "paasta_tools.api.views.instance.pik.match_backends_and_pods", autospec=True
    ) as mock_match_backends_and_pods, asynctest.patch(
        "paasta_tools.api.views.instance.pik.smartstack_tools.get_backends",
        autospec=True,
    ), asynctest.patch(
        "paasta_tools.api.views.instance.pik.KubeSmartstackEnvoyReplicationChecker",
        autospec=True,
    ) as mock_kube_smartstack_replication_checker, asynctest.patch(
        "paasta_tools.api.views.instance.pik.kubernetes_tools.get_all_nodes",
        autospec=True,
    ) as mock_get_all_nodes, asynctest.patch(
        "paasta_tools.api.views.instance.marathon_tools.get_expected_instance_count_for_namespace",
        autospec=True,
    ) as mock_get_expected_instance_count_for_namespace:
        mock_get_all_nodes.return_value = [
            {"hostname": "host1.paasta.party", "attributes": {"region": "us-north-3"}}
        ]

        mock_kube_smartstack_replication_checker.return_value.get_allowed_locations_and_hosts.return_value = {
            "us-north-3": [
                DiscoveredHost(hostname="host1.paasta.party", pool="default")
            ]
        }

        mock_get_expected_instance_count_for_namespace.return_value = 2
        mock_backend = HaproxyBackend(
            status="UP",
            svname="host1_1.2.3.4:123",
            check_status="L7OK",
            check_code="0",
            check_duration="1",
            lastchg="9876",
        )
        mock_pod = mock.create_autospec(V1Pod)
        mock_match_backends_and_pods.return_value = [(mock_backend, mock_pod)]

        mock_job_config = kubernetes_tools.KubernetesDeploymentConfig(
            service="fake_service",
            cluster="fake_cluster",
            instance="fake_instance",
            config_dict={"bounce_method": "fake_bounce"},
            branch_dict=None,
        )
        mock_service_namespace_config = ServiceNamespaceConfig()
        mock_settings = mock.Mock()

        smartstack_status = await instance.pik.mesh_status(
            service="fake_service",
            service_mesh=ServiceMesh.SMARTSTACK,
            instance="fake_instance",
            job_config=mock_job_config,
            service_namespace_config=mock_service_namespace_config,
            pods_task=wrap_value_in_task([mock_pod]),
            should_return_individual_backends=True,
            settings=mock_settings,
        )
        assert smartstack_status == {
            "registration": "fake_service.fake_instance",
            "expected_backends_per_location": 2,
            "locations": [
                {
                    "name": "us-north-3",
                    "running_backends_count": 1,
                    "backends": [
                        {
                            "hostname": "host1:1.2.3.4",
                            "port": 123,
                            "status": "UP",
                            "check_status": "L7OK",
                            "check_code": "0",
                            "last_change": 9876,
                            "has_associated_task": True,
                            "check_duration": 1,
                        }
                    ],
                }
            ],
        }