def test_redis_unavailable(self): self._servicer._redis_dict = MockUnavailableRedis("localhost", 6380) req = UpdateRecordRequest() req.id = "IMSI557" req.fields["mac_addr"] = "aa:bb:aa:bb:aa:bb" with self.assertRaises(grpc.RpcError) as err: self._stub.UpdateRecord(req) self.assertEqual(err.exception.code(), grpc.StatusCode.UNAVAILABLE) get_req = GetDirectoryFieldRequest() get_req.id = "IMSI557" get_req.field_key = "mac_addr" with self.assertRaises(grpc.RpcError) as err: self._stub.GetDirectoryField(get_req) self.assertEqual(err.exception.code(), grpc.StatusCode.UNAVAILABLE) del_req = DeleteRecordRequest() del_req.id = "IMSI557" with self.assertRaises(grpc.RpcError) as err: self._stub.DeleteRecord(del_req) self.assertEqual(err.exception.code(), grpc.StatusCode.UNAVAILABLE) void_req = Void() with self.assertRaises(grpc.RpcError) as err: self._stub.GetAllDirectoryRecords(void_req) self.assertEqual(err.exception.code(), grpc.StatusCode.UNAVAILABLE)
def test_get_field(self): self._servicer._redis_dict.clear() req = UpdateRecordRequest() req.id = "IMSI557" req.fields["mac_addr"] = "aa:bb:aa:bb:aa:bb" self._stub.UpdateRecord(req) self.assertTrue(req.id in self._servicer._redis_dict) get_req = GetDirectoryFieldRequest() get_req.id = "IMSI557" get_req.field_key = "mac_addr" ret = self._stub.GetDirectoryField(get_req) self.assertEqual("aa:bb:aa:bb:aa:bb", ret.value) with self.assertRaises(grpc.RpcError) as err: get_req.field_key = "ipv4_addr" self._stub.GetDirectoryField(get_req) self.assertEqual(err.exception.code(), grpc.StatusCode.NOT_FOUND)
def get_record_field_handler(client, args): get_request = GetDirectoryFieldRequest() get_request.id = args.id get_request.field_key = args.field_key try: res = client.GetDirectoryField(get_request) print("Successfully got field: (%s, %s) for ID: %s" % (res.key, res.value, args.id)) except grpc.RpcError as e: print("gRPC failed with %s: %s" % (e.code(), e.details()))