def instance_set_state(request, ) -> None: service = request.swagger_data.get("service") instance = request.swagger_data.get("instance") desired_state = request.swagger_data.get("desired_state") try: instance_type = validate_service_instance(service, instance, settings.cluster, settings.soa_dir) except NoConfigurationForServiceError: error_message = "deployment key %s not found" % ".".join( [settings.cluster, instance]) raise ApiFailure(error_message, 404) except Exception: error_message = traceback.format_exc() raise ApiFailure(error_message, 500) if pik.can_set_state(instance_type): try: pik.set_cr_desired_state( kube_client=settings.kubernetes_client, service=service, instance=instance, instance_type=instance_type, desired_state=desired_state, ) except RuntimeError as e: raise ApiFailure(e, 500) else: error_message = ( f"instance_type {instance_type} of {service}.{instance} doesn't " "support set_state") raise ApiFailure(error_message, 500)
def test_set_cr_desired_state_calls_k8s_tools(mock_set_cr_desired_state): pik.set_cr_desired_state( kube_client=mock.Mock(), service=mock.Mock(), instance=mock.Mock(), instance_type="flink", desired_state=mock.Mock(), ) assert len(mock_set_cr_desired_state.mock_calls) == 1
def test_set_cr_desired_state_invalid_instance_type(): with pytest.raises(RuntimeError) as excinfo: pik.set_cr_desired_state( kube_client=mock.Mock(), service=mock.Mock(), instance=mock.Mock(), instance_type="marathon", desired_state=mock.Mock(), ) assert "Unknown instance type" in str(excinfo.value)