Exemplo n.º 1
0
        public void InvalidOutputEvent()
        {
            Func <EventGridAttribute, IAsyncCollector <object> > eventConverter = (attr =>
            {
                var mockClient = new Mock <EventGridPublisherClient>();
                return(new EventGridAsyncCollector(mockClient.Object));
            });

            ILoggerFactory loggerFactory = new LoggerFactory();

            loggerFactory.AddProvider(new TestLoggerProvider());
            // use moq eventgridclient for test extension
            var customExtension = new EventGridExtensionConfigProvider(eventConverter, new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), loggerFactory);

            var configuration = new Dictionary <string, string>
            {
                { "eventGridUri", "https://pccode.westus2-1.eventgrid.azure.net/api/events" },
                { "eventgridKey", "thisismagic" }
            };

            var host = TestHelpers.NewHost <OutputCloudEventBindingParams>(customExtension, configuration: configuration);

            Assert.That(
                async() => await host.GetJobHost().CallAsync($"OutputCloudEventBindingParams.InvalidEvent"),
                Throws.InstanceOf <FunctionInvocationException>().And.InnerException.InnerException.InstanceOf <InvalidOperationException>()
                .And.InnerException.InnerException.Message.Contains(nameof(TestEvent)));
        }
Exemplo n.º 2
0
        public async Task TestMultipleCloudEventsWithTracingMultiDispatchError()
        {
            // individual elements
            var ext = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance);

            using var host = TestHelpers.NewHost <MyProg3>(ext);
            await host.StartAsync(); // add listener

            using var testListener = new ClientDiagnosticListener("Azure.Messaging.EventGrid");
            const string functionName = "EventGridThrowsExceptionMultiple";
            const string traceparent1 = "00-0123456789abcdef0123456789abcdef-0123456789abcdef-01";
            const string traceparent2 = "00-1123456789abcdef0123456789abcdef-1123456789abcdef-01";

            var request = CreateDispatchRequest(functionName,
                                                JObject.Parse($"{{'subject':'one','data':{{'prop':'alpha'}},'traceparent':'{traceparent1}'}}"),
                                                JObject.Parse($"{{'subject':'two','data':{{'prop':'alpha'}},'traceparent':'{traceparent2}'}}"));

            var response = await ext.ConvertAsync(request, CancellationToken.None);

            Assert.AreEqual(1, testListener.Scopes.Count);
            var executionScope = testListener.AssertScope("EventGrid.Process",
                                                          new KeyValuePair <string, string>("az.namespace", "Microsoft.EventGrid"));

            var expectedLinks = new[] { new ClientDiagnosticListener.ProducedLink(traceparent1, null),
                                        new ClientDiagnosticListener.ProducedLink(traceparent2, null) };

            Assert.That(executionScope.Links, Is.EquivalentTo(expectedLinks));
            Assert.NotNull(executionScope.Exception);
            Assert.True(_log.TryGetValue(executionScope.Activity.Id, out var activityName));
            Assert.AreEqual("EventGrid.Process", activityName);
        }
Exemplo n.º 3
0
        public async Task TestEventGridEventWithTracingSingleDispatch(string functionName)
        {
            // individual elements
            var ext = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance);

            using var host = TestHelpers.NewHost <MyProg1>(ext);
            await host.StartAsync(); // add listener

            using var testListener = new ClientDiagnosticListener("Azure.Messaging.EventGrid");
            var request = CreateDispatchRequest(functionName,
                                                JObject.Parse(@"{'subject':'one','eventType':'1','id':'1','dataVersion':'0','data':{'prop':'alpha'}}"),
                                                JObject.Parse(@"{'subject':'one','eventType':'2','id':'1','dataVersion':'0','data':{'prop':'alpha'}}"));
            var response = await ext.ConvertAsync(request, CancellationToken.None);

            Assert.AreEqual(2, testListener.Scopes.Count);

            // one of executions will fail because of dup subject
            Assert.AreEqual(1, testListener.Scopes.Count(s => s.Exception != null));

            for (int i = 0; i < 2; i++)
            {
                var executionScope = testListener.AssertAndRemoveScope("EventGrid.Process", new KeyValuePair <string, string>("az.namespace", "Microsoft.EventGrid"));
                Assert.IsEmpty(executionScope.Links);
                Assert.True(_log.TryGetValue(executionScope.Activity.Id, out var activityName));
                Assert.AreEqual("EventGrid.Process", activityName);
            }
        }
Exemplo n.º 4
0
        public static IHost NewHost <T>(EventGridExtensionConfigProvider ext = null, Dictionary <string, string> configuration = null)
        {
            var builder = new HostBuilder()
                          .ConfigureServices(services =>
            {
                services.AddSingleton <ITypeLocator>(new FakeTypeLocator <T>());
                if (ext != null)
                {
                    services.AddSingleton <IExtensionConfigProvider>(ext);
                }
                services.AddSingleton <IExtensionConfigProvider>(new TestExtensionConfig());
            })
                          .ConfigureWebJobs(webJobsBuilder =>
            {
                webJobsBuilder.AddEventGrid();
                webJobsBuilder.UseHostId(Guid.NewGuid().ToString("n"));
            })
                          .ConfigureLogging(logging =>
            {
                logging.ClearProviders();
                logging.AddProvider(new TestLoggerProvider());
            });

            if (configuration != null)
            {
                builder.ConfigureAppConfiguration(b =>
                {
                    b.AddInMemoryCollection(configuration);
                });
            }

            return(builder.Build());
        }
Exemplo n.º 5
0
        public async Task TestUnsubscribe()
        {
            var ext  = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance);
            var host = TestHelpers.NewHost <MyProg1>(ext);
            await host.StartAsync(); // add listener

            var request  = CreateUnsubscribeRequest("TestEventGrid");
            var response = await ext.ConvertAsync(request, CancellationToken.None);

            Assert.AreEqual(HttpStatusCode.Accepted, response.StatusCode);
        }
Exemplo n.º 6
0
        public async Task TestMultipleCloudEventsWithTracingSingleDispatch()
        {
            // individual elements
            var ext = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance);

            using var host = TestHelpers.NewHost <MyProg1>(ext);
            await host.StartAsync(); // add listener

            using var testListener = new ClientDiagnosticListener("Azure.Messaging.EventGrid");
            const string functionName = "TestCloudEvent";
            const string traceparent1 = "00-0123456789abcdef0123456789abcdef-0123456789abcdef-01";
            const string tracestate1  = "foo1=bar1";
            const string traceparent2 = "00-1123456789abcdef0123456789abcdef-1123456789abcdef-01";

            var request = CreateDispatchRequest(functionName,
                                                JObject.Parse($"{{{CloudEventRequiredFields},'data':{{'prop':'1'}},'traceparent':'{traceparent1}','tracestate':'{tracestate1}'}}"),
                                                JObject.Parse($"{{{CloudEventRequiredFields},'data':{{'prop':'2'}},'traceparent':'{traceparent2}'}}"));

            var response = await ext.ConvertAsync(request, CancellationToken.None);

            Assert.AreEqual(2, testListener.Scopes.Count);

            // one of executions will fail because of dup subject
            Assert.AreEqual(1, testListener.Scopes.Count(s => s.Exception != null));

            bool fullFound = false, parentOnlyFound = false;

            for (int i = 0; i < 2; i++)
            {
                var executionScope = testListener.AssertAndRemoveScope("EventGrid.Process", new KeyValuePair <string, string>("az.namespace", "Microsoft.EventGrid"));
                Assert.AreEqual(1, executionScope.Links.Count);
                Assert.True(_log.TryGetValue(executionScope.Activity.Id, out var activityName));
                Assert.AreEqual("EventGrid.Process", activityName);

                var link = executionScope.Links.Single();
                if (link.Traceparent == traceparent1)
                {
                    Assert.AreEqual(tracestate1, link.Tracestate);
                    Assert.IsFalse(fullFound);
                    fullFound = true;
                }
                else
                {
                    Assert.IsNull(link.Tracestate);
                    Assert.IsFalse(parentOnlyFound);
                    parentOnlyFound = true;
                }
            }

            Assert.IsTrue(fullFound);
            Assert.IsTrue(parentOnlyFound);
        }
Exemplo n.º 7
0
        public async Task TestSubscribeOptions(string functionName)
        {
            var ext = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance);

            using var host = TestHelpers.NewHost <MyProg1>(ext);
            await host.StartAsync(); // add listener

            var request  = new HttpRequestMessage(HttpMethod.Options, $"http://localhost/?functionName={functionName}");
            var response = await ext.ConvertAsync(request, CancellationToken.None);

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual("eventgrid.azure.net", response.Headers.GetValues("Webhook-Allowed-Origin").First());
        }
Exemplo n.º 8
0
        public async Task TestSubscribe(string evt)
        {
            var ext  = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance);
            var host = TestHelpers.NewHost <MyProg1>(ext);
            await host.StartAsync(); // add listener

            var request  = CreateEventSubscribeRequest("TestEventGrid", evt);
            var response = await ext.ConvertAsync(request, CancellationToken.None);

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            var content = await response.Content.ReadAsStringAsync();

            Assert.AreEqual("validation-code", JObject.Parse(content)["validationResponse"].ToString());
        }
Exemplo n.º 9
0
        public async Task OutputCloudEventBindingParamsTests(string functionName, string expectedCollection)
        {
            List <CloudEvent> cloudEvents = new List <CloudEvent>();

            Func <EventGridAttribute, IAsyncCollector <object> > eventConverter = (attr =>
            {
                var mockClient = new Mock <EventGridPublisherClient>();
                mockClient.Setup(x => x.SendEventsAsync(It.IsAny <IEnumerable <CloudEvent> >(), It.IsAny <CancellationToken>()))
                .Returns((IEnumerable <CloudEvent> events, CancellationToken cancel) =>
                {
                    foreach (CloudEvent eve in events)
                    {
                        cloudEvents.Add(eve);
                    }

                    return(Task.FromResult <Response>(new MockResponse(200)));
                });
                return(new EventGridAsyncCollector(mockClient.Object));
            });

            ILoggerFactory loggerFactory = new LoggerFactory();
            var            provider      = new TestLoggerProvider();

            loggerFactory.AddProvider(provider);
            // use moq eventgridclient for test extension
            var customExtension = new EventGridExtensionConfigProvider(eventConverter, new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), loggerFactory);

            var configuration = new Dictionary <string, string>
            {
                { "eventGridUri", "https://pccode.westus2-1.eventgrid.azure.net/api/events" },
                { "eventgridKey", "thisismagic" }
            };

            var host = TestHelpers.NewHost <OutputCloudEventBindingParams>(customExtension, configuration: configuration);

            await host.GetJobHost().CallAsync($"OutputCloudEventBindingParams.{functionName}");

            var categories = provider.GetAllLogMessages().Select(p => p.Category);

            CollectionAssert.Contains(categories, "Microsoft.Azure.WebJobs.Extensions.EventGrid.EventGridExtensionConfigProvider");

            var expectedEvents = new HashSet <string>(expectedCollection.Split(' '));

            foreach (CloudEvent eve in cloudEvents)
            {
                Assert.True(expectedEvents.Remove(eve.Data.ToObjectFromJson <string>()));
            }
            Assert.AreEqual(0, expectedEvents.Count);
        }
Exemplo n.º 10
0
        public async Task ExecutionFailureMultipleEventsTest()
        {
            var ext  = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance);
            var host = TestHelpers.NewHost <MyProg3>(ext);
            await host.StartAsync(); // add listener

            JObject dummyPayload = JObject.Parse("{}");
            var     request      = CreateDispatchRequest("EventGridThrowsExceptionMultiple", dummyPayload);
            var     response     = await ext.ConvertAsync(request, CancellationToken.None);

            string responseContent = await response.Content.ReadAsStringAsync();

            Assert.AreEqual("Exception while executing function: EventGridThrowsExceptionMultiple", responseContent);
            Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode);
        }
Exemplo n.º 11
0
        public async Task WrongFunctionNameTest()
        {
            var ext  = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance);
            var host = TestHelpers.NewHost <MyProg2>(ext);
            await host.StartAsync(); // add listener

            JObject dummyPayload = JObject.Parse("{}");
            var     request      = CreateDispatchRequest("RandomFunctionName", dummyPayload);
            var     response     = await ext.ConvertAsync(request, CancellationToken.None);

            string responseContent = await response.Content.ReadAsStringAsync();

            Assert.AreEqual("cannot find function: 'RandomFunctionName'", responseContent);
            Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
        }
Exemplo n.º 12
0
        public async Task TestCloudEvent()
        {
            // individual elements
            var ext  = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance);
            var host = TestHelpers.NewHost <MyProg1>(ext);
            await host.StartAsync(); // add listener

            var request = CreateSingleRequest("TestEventGrid",
                                              JObject.Parse(@"{'subject':'one','data':{'prop':'alpha'}}"));
            var response = await ext.ConvertAsync(request, CancellationToken.None);

            // verifies each instance gets its own proper binding data (from FakePayload.Prop)
            _log.TryGetValue("one", out string alpha);
            Assert.AreEqual("alpha", alpha);
            Assert.AreEqual(HttpStatusCode.Accepted, response.StatusCode);
        }
Exemplo n.º 13
0
        public async Task OutputBindingParamsTests(string functionName, string expectedCollection)
        {
            List <EventGridEvent> output = new List <EventGridEvent>();

            Func <EventGridAttribute, IAsyncCollector <EventGridEvent> > customConverter = (attr =>
            {
                var mockClient = new Mock <EventGridPublisherClient>();
                mockClient.Setup(x => x.SendEventsAsync(It.IsAny <IEnumerable <EventGridEvent> >(), It.IsAny <CancellationToken>()))
                .Returns((IEnumerable <EventGridEvent> events, CancellationToken cancel) =>
                {
                    foreach (EventGridEvent eve in events)
                    {
                        output.Add(eve);
                    }

                    return(Task.FromResult <Response>(new MockResponse(200)));
                });
                return(new EventGridAsyncCollector(mockClient.Object));
            });

            ILoggerFactory loggerFactory = new LoggerFactory();

            loggerFactory.AddProvider(new TestLoggerProvider());
            // use moq eventgridclient for test extension
            var customExtension = new EventGridExtensionConfigProvider(customConverter, loggerFactory);

            var configuration = new Dictionary <string, string>
            {
                { "eventGridUri", "https://pccode.westus2-1.eventgrid.azure.net/api/events" },
                { "eventgridKey", "thisismagic" }
            };

            var host = TestHelpers.NewHost <OutputBindingParams>(customExtension, configuration: configuration);

            await host.GetJobHost().CallAsync($"OutputBindingParams.{functionName}");

            var expectedEvents = new HashSet <string>(expectedCollection.Split(' '));

            foreach (EventGridEvent eve in output)
            {
                Assert.True(expectedEvents.Remove(eve.GetData <string>()));
            }
            Assert.True(expectedEvents.Count == 0);
        }
Exemplo n.º 14
0
        public async Task TestDispatch()
        {
            var ext  = new EventGridExtensionConfigProvider(new HttpRequestProcessor(NullLoggerFactory.Instance.CreateLogger <HttpRequestProcessor>()), NullLoggerFactory.Instance);
            var host = TestHelpers.NewHost <MyProg1>(ext);
            await host.StartAsync(); // add listener

            var request = CreateDispatchRequest("TestEventGrid",
                                                JObject.Parse(@"{'subject':'one','data':{'prop':'alpha'}}"),
                                                JObject.Parse(@"{'subject':'two','data':{'prop':'beta'}}"));
            var response = await ext.ConvertAsync(request, CancellationToken.None);

            // Verify that the user function was dispatched twice, NOT necessarily in order
            // Also verifies each instance gets its own proper binding data (from FakePayload.Prop)
            _log.TryGetValue("one", out string alpha);
            _log.TryGetValue("two", out string beta);
            Assert.AreEqual("alpha", alpha);
            Assert.AreEqual("beta", beta);
            // TODO - Verify that we return from webhook before the dispatch is finished
            // https://github.com/Azure/azure-functions-eventgrid-extension/issues/10
            Assert.AreEqual(HttpStatusCode.Accepted, response.StatusCode);
        }