def test_kubernetes_mesh_status_error( mock_mesh_status, mock_load_service_namespace_config, include_mesh, inst_type, service_ns_conf, expected_msg, ): mock_load_service_namespace_config.return_value = service_ns_conf mock_settings = mock.Mock() with pytest.raises(RuntimeError) as excinfo: pik.kubernetes_mesh_status( service="fake_service", instance="fake_instance", instance_type=inst_type, settings=mock_settings, include_smartstack=include_mesh, include_envoy=include_mesh, ) assert expected_msg in excinfo.value.args[0] assert mock_mesh_status.call_args_list == []
def test_kubernetes_mesh_status( include_smartstack, include_envoy, expected, ): with asynctest.patch( "paasta_tools.kubernetes_tools.load_service_namespace_config", autospec=True ) as mock_load_service_namespace_config, asynctest.patch( "paasta_tools.instance.kubernetes.mesh_status", autospec=True ) as mock_mesh_status, asynctest.patch( "paasta_tools.kubernetes_tools.pods_for_service_instance", autospec=True ) as mock_pods_for_service_instance, asynctest.patch( "paasta_tools.instance.kubernetes.LONG_RUNNING_INSTANCE_TYPE_HANDLERS", {"flink": mock.Mock()}, autospec=False, ): mock_load_service_namespace_config.return_value = {"proxy_port": 1234} mock_pods_for_service_instance.return_value = ["pod_1"] mock_job_config = pik.LONG_RUNNING_INSTANCE_TYPE_HANDLERS[ "flink" ].loader.return_value mock_settings = mock.Mock() kmesh = pik.kubernetes_mesh_status( service="fake_service", instance="fake_instance", instance_type="flink", settings=mock_settings, include_smartstack=include_smartstack, include_envoy=include_envoy, ) assert len(kmesh) == len(expected) for i in range(len(expected)): mesh_type = expected[i] assert kmesh.get(mesh_type) == mock_mesh_status.return_value assert mock_mesh_status.call_args_list[i] == mock.call( service="fake_service", instance=mock_job_config.get_nerve_namespace.return_value, job_config=mock_job_config, service_namespace_config={"proxy_port": 1234}, pods_task=mock.ANY, should_return_individual_backends=True, settings=mock_settings, service_mesh=getattr(pik.ServiceMesh, mesh_type.upper()), ) _, kwargs = mock_mesh_status.call_args_list[i] assert kwargs["pods_task"].result() == ["pod_1"]
def instance_mesh_status(request): service = request.swagger_data.get("service") instance = request.swagger_data.get("instance") include_smartstack = request.swagger_data.get("include_smartstack") include_envoy = request.swagger_data.get("include_envoy") instance_mesh: Dict[str, Any] = {} instance_mesh["service"] = service instance_mesh["instance"] = instance try: instance_type = validate_service_instance(service, instance, settings.cluster, settings.soa_dir) except NoConfigurationForServiceError: error_message = ( f"No instance named '{compose_job_id(service, instance)}' has been " f"configured to run in the {settings.cluster} cluster") raise ApiFailure(error_message, 404) except Exception: error_message = traceback.format_exc() raise ApiFailure(error_message, 500) try: instance_mesh.update( pik.kubernetes_mesh_status( service=service, instance=instance, instance_type=instance_type, settings=settings, include_smartstack=include_smartstack, include_envoy=include_envoy, )) except RuntimeError as e: raise ApiFailure(str(e), 405) except Exception: error_message = traceback.format_exc() raise ApiFailure(error_message, 500) return instance_mesh
def test_kubernetes_mesh_status( mock_pods_for_service_instance, mock_mesh_status, mock_load_service_namespace_config, include_smartstack, include_envoy, expected, ): mock_load_service_namespace_config.return_value = {"proxy_port": 1234} mock_pods_for_service_instance.return_value = ["pod_1"] mock_job_config = pik.LONG_RUNNING_INSTANCE_TYPE_HANDLERS[ "flink"].loader.return_value mock_settings = mock.Mock() kmesh = pik.kubernetes_mesh_status( service="fake_service", instance="fake_instance", instance_type="flink", settings=mock_settings, include_smartstack=include_smartstack, include_envoy=include_envoy, ) assert len(kmesh) == len(expected) for i in range(len(expected)): mesh_type = expected[i] assert kmesh.get(mesh_type) == mock_mesh_status.return_value assert mock_mesh_status.call_args_list[i] == mock.call( service="fake_service", instance=mock_job_config.get_nerve_namespace.return_value, job_config=mock_job_config, service_namespace_config={"proxy_port": 1234}, pods=["pod_1"], should_return_individual_backends=True, settings=mock_settings, service_mesh=getattr(pik.ServiceMesh, mesh_type.upper()), )