Beispiel #1
0
    async def _verify_environment_reloaded(
            self,
            test_env: typing.Dict[str, str] = {},
            test_cwd: str = os.getcwd()):
        request = protos.FunctionEnvironmentReloadRequest(
            environment_variables=test_env,
            function_app_directory=test_cwd)

        request_msg = protos.StreamingMessage(
            request_id='0',
            function_environment_reload_request=request)

        disp = testutils.create_dummy_dispatcher()

        try:
            r = await disp._handle__function_environment_reload_request(
                request_msg)

            environ_dict = os.environ.copy()
            self.assertDictEqual(environ_dict, test_env)
            self.assertEqual(os.getcwd(), test_cwd)
            status = r.function_environment_reload_response.result.status
            self.assertEqual(status, protos.StatusResult.Success)
        finally:
            self._reset_environ()
async def vertify_nested_namespace_import():
    test_env = {}
    request = protos.FunctionEnvironmentReloadRequest(
        environment_variables=test_env)

    request_msg = protos.StreamingMessage(
        request_id='0', function_environment_reload_request=request)

    disp = testutils.create_dummy_dispatcher()

    # Mock intepreter starts in placeholder mode
    import azure.module_a as mod_a  # noqa: F401

    # Mock function specialization, load customer's libraries and functionapps
    ns_root = os.path.join(testutils.UNIT_TESTS_ROOT, 'azure_namespace_import',
                           'namespace_location_b')
    test_path = os.path.join(ns_root, 'azure', 'namespace_b', 'module_b')
    test_mod_path = os.path.join(test_path, 'test_module.py')

    os.makedirs(test_path)
    with open(test_mod_path, 'w') as f:
        f.write('MESSAGE = "module_b is imported"')

    try:
        # Mock a customer uses test_module
        if sys.argv[1].lower() == 'true':
            await disp._handle__function_environment_reload_request(request_msg
                                                                    )
        from azure.namespace_b.module_b import test_module
        print(test_module.MESSAGE)
    except ModuleNotFoundError:
        print('module_b fails to import')
    finally:
        # Cleanup
        shutil.rmtree(ns_root)
Beispiel #3
0
async def verify_path_imports():
    test_env = {}
    request = protos.FunctionEnvironmentReloadRequest(
        environment_variables=test_env)

    request_msg = protos.StreamingMessage(
        request_id='0', function_environment_reload_request=request)

    disp = testutils.create_dummy_dispatcher()

    test_path = 'test_module_dir'
    test_mod_path = os.path.join(test_path, 'test_module.py')

    os.mkdir(test_path)
    with open(test_mod_path, 'w') as f:
        f.write('CONSTANT = "This module was imported!"')

    if (sys.argv[1] == 'success'):
        await disp._handle__function_environment_reload_request(request_msg)

    try:
        import test_module
        print(test_module.CONSTANT)
    finally:
        # Cleanup
        shutil.rmtree(test_path)
Beispiel #4
0
    async def reload_environment(
        self,
        environment: typing.Dict[str, str],
        function_project_path: str = '/home/site/wwwroot'
    ) -> protos.FunctionEnvironmentReloadResponse:

        request_content = protos.FunctionEnvironmentReloadRequest(
            function_app_directory=function_project_path,
            environment_variables={
                k.encode(): v.encode()
                for k, v in environment.items()
            })

        r = await self.communicate(
            protos.StreamingMessage(
                function_environment_reload_request=request_content),
            wait_for='function_environment_reload_response')

        return r