Exemplo n.º 1
0
def function_env(module, handler):
    return {
        'FUNCTION_URI':
        'file://%s/tests/functions/%s?handler=%s' %
        (os.getcwd(), module, handler),
        'GRPC_PORT':
        testutils.find_free_port()
    }
Exemplo n.º 2
0
    def test_json_processing(self):
        port = testutils.find_free_port()
        self.process = run_function(port=port,
                                    module="concat.py",
                                    handler="concat")

        response = call_http(port=port,
                             message='{"foo":"bar","hello":"world"}',
                             content_type="application/json")

        self.assertEqual(b'{"result": "foobarhelloworld"}', response)
    def test_upper(self):
        port = testutils.find_free_port()
        env = {
            'PYTHONPATH':
            self.PYTHONPATH,
            'GRPC_PORT':
            str(port),
            'FUNCTION_URI':
            'file://%s/tests/functions/upper.py?handler=handle' % os.getcwd()
        }

        self.process = subprocess.Popen(
            self.command,
            cwd=self.workingdir,
            shell=True,
            env=env,
            preexec_fn=os.setsid,
        )

        channel = grpc.insecure_channel('localhost:%s' % port)
        testutils.wait_until_channel_ready(channel)

        self.stub = function.MessageFunctionStub(channel)

        def generate_messages():
            headers = {
                'Content-Type':
                message.Message.HeaderValue(values=['text/plain']),
                'correlationId':
                message.Message.HeaderValue(values=[str(uuid.uuid4())])
            }

            messages = [
                message.Message(payload=bytes("hello", 'UTF-8'),
                                headers=headers),
                message.Message(payload=bytes("world", 'UTF-8'),
                                headers=headers),
                message.Message(payload=bytes("foo", 'UTF-8'),
                                headers=headers),
                message.Message(payload=bytes("bar", 'UTF-8'),
                                headers=headers),
            ]
            for msg in messages:
                yield msg

        responses = self.stub.Call(generate_messages())
        expected = [b'HELLO', b'WORLD', b'FOO', b'BAR']

        for response in responses:
            self.assertTrue(response.payload in expected)
            expected.remove(response.payload)

        self.assertEqual(0, len(expected))
Exemplo n.º 4
0
 def test_zip(self):
     env = {
         'FUNCTION_URI':
         'file://%s/tests/zip/myfunc.zip?handler=func.handler' %
         os.getcwd(),
         'GRPC_PORT':
         testutils.find_free_port()
     }
     func, interaction_model = invoker.function_invoker.install_function(
         env)
     self.assertEqual('HELLO', func('hello'))
     os.remove('func.py')
     os.remove('helpers.py')
     self.assertEqual('handler', func.__name__)
    def test_accepts_not_supported(self):
        port = testutils.find_free_port()
        env = {
            'PYTHONPATH':
            self.PYTHONPATH,
            'GRPC_PORT':
            str(port),
            'FUNCTION_URI':
            'file://%s/tests/functions/concat.py?handler=concat' % os.getcwd()
        }

        self.process = subprocess.Popen(
            self.command,
            cwd=self.workingdir,
            shell=True,
            env=env,
            preexec_fn=os.setsid,
        )

        channel = grpc.insecure_channel('localhost:%s' % port)
        testutils.wait_until_channel_ready(channel)

        self.stub = function.MessageFunctionStub(channel)

        def generate_messages():
            headers = {
                'Content-Type':
                message.Message.HeaderValue(values=['application/json']),
                'Accept':
                message.Message.HeaderValue(values=['application/xml']),
                'correlationId':
                message.Message.HeaderValue(values=[str(uuid.uuid4())])
            }

            messages = [
                message.Message(payload=b'{"foo":"bar","hello":"world"}',
                                headers=headers),
            ]
            for msg in messages:
                yield msg

        try:
            responses = self.stub.Call(generate_messages())
            self.assertEqual(grpc._channel._Rendezvous, type(responses))
            # TODO: Investigate error handling
            # https://github.com/projectriff/python2-function-invoker/issues/5
        except RuntimeError:
            pass
    def test_accepts_text_plain(self):
        port = testutils.find_free_port()
        env = {
            'PYTHONPATH':
            self.PYTHONPATH,
            'GRPC_PORT':
            str(port),
            'FUNCTION_URI':
            'file://%s/tests/functions/concat.py?handler=concat' % os.getcwd()
        }

        self.process = subprocess.Popen(
            self.command,
            cwd=self.workingdir,
            shell=True,
            env=env,
            preexec_fn=os.setsid,
        )

        channel = grpc.insecure_channel('localhost:%d' % port)
        testutils.wait_until_channel_ready(channel)

        self.stub = function.MessageFunctionStub(channel)

        def generate_messages():
            headers = {
                'Content-Type':
                message.Message.HeaderValue(values=['application/json']),
                'Accept':
                message.Message.HeaderValue(values=['text/plain']),
                'correlationId':
                message.Message.HeaderValue(values=[str(uuid.uuid4())])
            }

            messages = [
                message.Message(payload=b'{"foo":"bar","hello":"world"}',
                                headers=headers),
            ]
            for msg in messages:
                yield msg

        responses = self.stub.Call(generate_messages())

        for response in responses:
            self.assertEqual(b'{"result": "foobarhelloworld"}',
                             response.payload)
Exemplo n.º 7
0
    def test_upper(self):
        port = testutils.find_free_port()
        self.process = run_function(port=port,
                                    module="upper.py",
                                    handler="handle")

        def generate_messages():
            messages = [
                "hello",
                "world",
                "foo",
                "bar",
            ]
            for msg in messages:
                yield msg

        responses = call_multiple_http_messages(port, generate_messages())
        expected = [b'HELLO', b'WORLD', b'FOO', b'BAR']

        for response in responses:
            self.assertTrue(response in expected)

        self.assertEqual(len(responses), len(expected))
 def setUp(self):
     self.port = testutils.find_free_port()