Skip to content

Commit 9850b18

Browse files
authored
Merge pull request #77 from negz/its-happening-dot-gif
Return a response, not a request
2 parents f8f842d + 39b1128 commit 9850b18

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

crossplane/function/runtime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ async def RunFunction( # noqa: N802 # gRPC requires this name.
141141
gareq.ParseFromString(req.SerializeToString())
142142

143143
garsp = await self.wrapped.RunFunction(gareq, context)
144-
rsp = fnv1beta1.RunFunctionRequest()
144+
rsp = fnv1beta1.RunFunctionResponse()
145145
rsp.ParseFromString(garsp.SerializeToString())
146146

147147
return rsp

tests/test_runtime.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright 2023 The Crossplane Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import dataclasses
16+
import unittest
17+
18+
import grpc
19+
20+
import crossplane.function.proto.v1.run_function_pb2 as fnv1
21+
import crossplane.function.proto.v1.run_function_pb2_grpc as grpcv1
22+
import crossplane.function.proto.v1beta1.run_function_pb2 as fnv1beta1
23+
from crossplane.function import logging, runtime
24+
25+
26+
class TestRuntime(unittest.IsolatedAsyncioTestCase):
27+
def setUp(self) -> None:
28+
logging.configure(level=logging.Level.DISABLED)
29+
30+
async def test_run_function(self) -> None:
31+
@dataclasses.dataclass
32+
class TestCase:
33+
reason: str
34+
runner: grpcv1.FunctionRunnerService
35+
req: fnv1beta1.RunFunctionRequest
36+
want: fnv1beta1.RunFunctionResponse
37+
38+
cases = [
39+
TestCase(
40+
reason="The v1 response should be returned as a v1beta1 response.",
41+
runner=EchoRunner(),
42+
req=fnv1beta1.RunFunctionRequest(meta=fnv1beta1.RequestMeta(tag="hi")),
43+
want=fnv1beta1.RunFunctionResponse(
44+
meta=fnv1beta1.ResponseMeta(tag="hi")
45+
),
46+
),
47+
]
48+
49+
for case in cases:
50+
beta_runner = runtime.BetaFunctionRunner(wrapped=case.runner)
51+
rsp = await beta_runner.RunFunction(case.req, None)
52+
53+
self.assertEqual(rsp, case.want, "-want, +got")
54+
55+
56+
class EchoRunner(grpcv1.FunctionRunnerService):
57+
def __init__(self):
58+
self.log = logging.get_logger()
59+
60+
async def RunFunction( # noqa: N802 # gRPC requires this name.
61+
self,
62+
req: fnv1.RunFunctionRequest,
63+
_: grpc.aio.ServicerContext,
64+
) -> fnv1beta1.RunFunctionResponse:
65+
return fnv1beta1.RunFunctionResponse(
66+
meta=fnv1beta1.ResponseMeta(tag=req.meta.tag)
67+
)
68+
69+
70+
if __name__ == "__main__":
71+
unittest.main()

0 commit comments

Comments
 (0)