def test_fetch_jwt_bundles_raise_grpc_call(mocker): WORKLOAD_API_CLIENT._spiffe_workload_api_stub.FetchJWTBundles = mocker.Mock( side_effect=FakeCall()) with pytest.raises(FetchJwtBundleError) as exc_info: WORKLOAD_API_CLIENT.fetch_jwt_bundles() assert (str(exc_info.value) == 'Error fetching JWT Bundle: Error details from Workload API.')
def test_fetch_x509_svid_raise_grpc_error_call(mocker): WORKLOAD_API_CLIENT._spiffe_workload_api_stub.FetchX509SVID = mocker.Mock( side_effect=FakeCall()) with (pytest.raises(FetchX509SvidError)) as exception: WORKLOAD_API_CLIENT.fetch_x509_svid() assert (str(exception.value) == 'Error fetching X.509 SVID: Error details from Workload API.')
def test_watch_jwt_bundle_no_retry_on_grpc_error(mocker): grpc_error = FakeCall() grpc_error._code = grpc.StatusCode.INVALID_ARGUMENT WORKLOAD_API_CLIENT._spiffe_workload_api_stub.FetchJWTBundles = mocker.Mock( side_effect=[ grpc_error, ]) expected_error = FetchJwtBundleError(grpc_error.details()) event = threading.Event() response_holder = ResponseHolder() WORKLOAD_API_CLIENT.watch_jwt_bundles( on_success=lambda r: handle_success(r, response_holder, event), on_error=lambda e: handle_error(e, response_holder, event), ) event.wait(3) # add timeout to prevent test from hanging assert not response_holder.success assert response_holder.error assert_error(response_holder.error, expected_error)
def test_watch_jwt_bundle_retry_on_grpc_error(mocker): grpc_error = FakeCall() jwt_bundles = { 'example.org': JWKS_1_EC_KEY, 'domain.prod': JWKS_2_EC_1_RSA_KEYS } WORKLOAD_API_CLIENT._spiffe_workload_api_stub.FetchJWTBundles = mocker.Mock( side_effect=[ grpc_error, delayed_responses( [workload_pb2.JWTBundlesResponse(bundles=jwt_bundles)]), ]) expected_error = FetchJwtBundleError(grpc_error.details()) event = threading.Event() response_holder = ResponseHolder() WORKLOAD_API_CLIENT.watch_jwt_bundles( on_success=lambda r: handle_success(r, response_holder, event), on_error=lambda e: assert_error(e, expected_error), ) event.wait(3) # add timeout to prevent test from hanging # Wait to receive the response from delayed_responses() time.sleep(1) jwt_bundle_set = response_holder.success assert jwt_bundle_set jwt_bundle_1 = jwt_bundle_set.get(TrustDomain.parse('example.org')) assert jwt_bundle_1 assert len(jwt_bundle_1.jwt_authorities()) == 1 jwt_bundle_2 = jwt_bundle_set.get(TrustDomain.parse('domain.prod')) assert jwt_bundle_2 assert len(jwt_bundle_2.jwt_authorities()) == 3
def func_that_raises_grpc_call_error(): raise FakeCall()