Exemplo n.º 1
0
        public async Task Invoke_CallsFunction()
        {
            var    testFunctions        = new Collection <FunctionDescriptor>();
            string testFunctionName     = "TestFunction";
            string triggerParameterName = "testTrigger";
            string testInput            = Guid.NewGuid().ToString();
            bool   functionInvoked      = false;

            var scriptHostMock = new Mock <IScriptJobHost>();

            scriptHostMock.Setup(p => p.CallAsync(It.IsAny <string>(), It.IsAny <IDictionary <string, object> >(), CancellationToken.None))
            .Callback <string, IDictionary <string, object>, CancellationToken>((name, args, token) =>
            {
                // verify the correct arguments were passed to the invoke
                Assert.Equal(testFunctionName, name);
                Assert.Equal(1, args.Count);
                Assert.Equal(testInput, (string)args[triggerParameterName]);

                functionInvoked = true;
            })
            .Returns(Task.CompletedTask);

            scriptHostMock.Setup(p => p.Functions).Returns(testFunctions);

            // Add a few parameters, with the trigger parameter last
            // to verify parameter order handling
            Collection <ParameterDescriptor> parameters = new Collection <ParameterDescriptor>
            {
                new ParameterDescriptor("context", typeof(ExecutionContext)),
                new ParameterDescriptor("log", typeof(TraceWriter)),
                new ParameterDescriptor(triggerParameterName, typeof(string))
                {
                    IsTrigger = true
                }
            };

            testFunctions.Add(new FunctionDescriptor(testFunctionName, null, null, parameters, null, null, null));

            FunctionInvocation invocation = new FunctionInvocation
            {
                Input = testInput
            };

            var scriptPath             = Path.GetTempPath();
            var applicationHostOptions = new ScriptApplicationHostOptions();

            applicationHostOptions.ScriptPath = scriptPath;
            var           optionsWrapper       = new OptionsWrapper <ScriptApplicationHostOptions>(applicationHostOptions);
            var           functionsManagerMock = new Mock <IWebFunctionsManager>();
            var           mockRouter           = new Mock <IWebJobsRouter>();
            var           testController       = new FunctionsController(functionsManagerMock.Object, mockRouter.Object, new LoggerFactory(), optionsWrapper);
            IActionResult response             = testController.Invoke(testFunctionName, invocation, scriptHostMock.Object);

            Assert.IsType <AcceptedResult>(response);

            // The call is fire-and-forget, so watch for functionInvoked to be set.
            await TestHelpers.Await(() => functionInvoked, timeout : 3000, pollingInterval : 100);

            Assert.True(functionInvoked);
        }
Exemplo n.º 2
0
        public async Task Invoke_CallsFunction()
        {
            string testFunctionName     = "TestFunction";
            string triggerParameterName = "testTrigger";
            string testInput            = Guid.NewGuid().ToString();
            bool   functionInvoked      = false;

            hostMock.Setup(p => p.CallAsync(It.IsAny <string>(), It.IsAny <Dictionary <string, object> >(), CancellationToken.None))
            .Callback <string, Dictionary <string, object>, CancellationToken>((name, args, token) =>
            {
                functionInvoked = true;

                // verify the correct arguments were passed to the invoke
                Assert.Equal(testFunctionName, name);
                Assert.Equal(1, args.Count);
                Assert.Equal(testInput, (string)args[triggerParameterName]);
            })
            .Returns(Task.CompletedTask);

            // Add a few parameters, with the trigger parameter last
            // to verify parameter order handling
            Collection <ParameterDescriptor> parameters = new Collection <ParameterDescriptor>
            {
                new ParameterDescriptor("context", typeof(ExecutionContext)),
                new ParameterDescriptor("log", typeof(TraceWriter)),
                new ParameterDescriptor(triggerParameterName, typeof(string))
                {
                    IsTrigger = true
                }
            };

            testFunctions.Add(new FunctionDescriptor(testFunctionName, null, null, parameters, null, null, null));

            FunctionInvocation invocation = new FunctionInvocation
            {
                Input = testInput
            };
            IActionResult response = testController.Invoke(testFunctionName, invocation);

            Assert.IsType <AcceptedResult>(response);

            // allow the invoke task to run
            await Task.Delay(200);

            Assert.True(functionInvoked);
        }
Exemplo n.º 3
0
        public async Task Invoke_CallsFunctionSession()
        {
            var    testFunctions        = new Collection <FunctionDescriptor>();
            string testFunctionName     = "TestFunction";
            string triggerParameterName = "testTrigger";
            string testInput            = Guid.NewGuid().ToString();
            string sessionId            = Guid.NewGuid().ToString();
            bool   baggageAdded         = false;

            var scriptHostMock = new Mock <IScriptJobHost>();

            scriptHostMock.Setup(p => p.CallAsync(It.IsAny <string>(), It.IsAny <IDictionary <string, object> >(), CancellationToken.None))
            .Callback <string, IDictionary <string, object>, CancellationToken>((name, args, token) =>
            {
                if (string.Equals(Activity.Current?.GetBaggageItem(ScriptConstants.LiveLogsSessionAIKey), sessionId, StringComparison.OrdinalIgnoreCase))
                {
                    baggageAdded = true;
                }
            })
            .Returns(Task.CompletedTask);

            scriptHostMock.Setup(p => p.Functions).Returns(testFunctions);
            Collection <ParameterDescriptor> parameters = new Collection <ParameterDescriptor>
            {
                new ParameterDescriptor("context", typeof(ExecutionContext)),
                new ParameterDescriptor("log", typeof(TraceWriter)),
                new ParameterDescriptor(triggerParameterName, typeof(string))
                {
                    IsTrigger = true
                }
            };

            testFunctions.Add(new FunctionDescriptor(testFunctionName, null, null, parameters, null, null, null));

            FunctionInvocation invocation = new FunctionInvocation
            {
                Input = testInput
            };

            var scriptPath             = Path.GetTempPath();
            var applicationHostOptions = new ScriptApplicationHostOptions();

            applicationHostOptions.ScriptPath = scriptPath;
            var optionsWrapper       = new OptionsWrapper <ScriptApplicationHostOptions>(applicationHostOptions);
            var functionsManagerMock = new Mock <IWebFunctionsManager>();
            var mockRouter           = new Mock <IWebJobsRouter>();
            var testController       = new FunctionsController(functionsManagerMock.Object, mockRouter.Object, new LoggerFactory(), optionsWrapper);
            var httpContext          = new DefaultHttpContext();

            httpContext.Request.Headers.Add(ScriptConstants.LiveLogsSessionAIKey, sessionId);
            testController.ControllerContext.HttpContext = httpContext;

            //Test Code
            IActionResult response = testController.Invoke(testFunctionName, invocation, scriptHostMock.Object);

            Assert.IsType <AcceptedResult>(response);

            // The call is fire-and-forget, so watch for functionInvoked to be set.
            await TestHelpers.Await(() => baggageAdded, timeout : 3000, pollingInterval : 100);

            Assert.True(baggageAdded);
        }