Esempio n. 1
0
        public void Create_WithEmptyOptionalConfigItems_SetsDefaultValues()
        {
            // Arrange
            var healthReporter = new Mock <IHealthReporter>();
            var builder        = new ConfigurationBuilder();

            builder
            .AddInMemoryCollection(new Dictionary <string, string>()
            {
                { "type", "SplunkOutput" },
                { "serviceBaseAddress", "https://hec.mysplunkserver.com:8088" },
                { "authenticationToken", "B5A79AAD-D822-46CC-80D1-819F80D7BFB0" },
                { "host", "" },
                { "index", "" },
                { "source", "" },
                { "sourceType", "" }
            });
            var configuration = builder.Build();

            // Act
            var factory = new SplunkOutputConfigurationFactory();
            var splunkOutputConfiguration = factory.Create(configuration, healthReporter.Object);

            // Assert
            Assert.NotNull(splunkOutputConfiguration);
            Assert.Equal(Environment.MachineName, splunkOutputConfiguration.Host);
            Assert.Null(splunkOutputConfiguration.Index);
            Assert.Null(splunkOutputConfiguration.Source);
            Assert.Null(splunkOutputConfiguration.SourceType);
        }
Esempio n. 2
0
        public void Create_WithMissingRequiredAuthenticationToken_ReportsProblemAndThrows()
        {
            // Arrange
            var healthReporter = new Mock <IHealthReporter>();
            var builder        = new ConfigurationBuilder();

            builder
            .AddInMemoryCollection(new Dictionary <string, string>()
            {
                { "type", "SplunkOutput" },
                { "serviceBaseAddress", "https://hec.mysplunkserver.com:8088" }
            });
            var configuration = builder.Build();

            healthReporter
            .Setup(r => r.ReportProblem(It.IsAny <string>(), It.IsAny <string>()))
            .Verifiable();

            // Act
            var factory = new SplunkOutputConfigurationFactory();

            Assert.ThrowsAny <Exception>(() => factory.Create(configuration, healthReporter.Object));

            // Assert
            healthReporter.Verify();
        }
Esempio n. 3
0
        public void Create_WithInvalidConfigItems_ReportsProblemAndThrows()
        {
            // Arrange
            var healthReporter = new Mock <IHealthReporter>();
            var builder        = new ConfigurationBuilder();

            builder
            .AddInMemoryCollection(new Dictionary <string, string>()
            {
                { "type", "SplunkOutput" },
                { "serviceBaseAddress", "https://hec.mysplunkserver.com:8088" },
                { "authenticationToken", "B5A79AAD-D822-46CC-80D1-819F80D7BFB0" },
                { "maxRetryAttempts", "ABC" }
            });
            var configuration = builder.Build();

            healthReporter
            .Setup(r => r.ReportProblem(It.IsAny <string>(), It.IsAny <string>()))
            .Verifiable();

            // Act
            var factory = new SplunkOutputConfigurationFactory();

            Assert.ThrowsAny <Exception>(() => factory.Create(configuration, healthReporter.Object));

            // Assert
            healthReporter.Verify();
        }
Esempio n. 4
0
        public void Create_WithRequiredConfigItems_DoesNotThrow()
        {
            // Arrange
            var healthReporter = new Mock <IHealthReporter>();
            var builder        = new ConfigurationBuilder();

            builder
            .AddInMemoryCollection(new Dictionary <string, string>()
            {
                { "type", "SplunkOutput" },
                { "serviceBaseAddress", "https://hec.mysplunkserver.com:8088" },
                { "authenticationToken", "B5A79AAD-D822-46CC-80D1-819F80D7BFB0" }
            });
            var configuration = builder.Build();

            // Act
            var factory = new SplunkOutputConfigurationFactory();
            var splunkOutputConfiguration = factory.Create(configuration, healthReporter.Object);

            // Assert
            Assert.NotNull(splunkOutputConfiguration);
        }
        public SplunkOutput CreateItem(IConfiguration configuration, IHealthReporter healthReporter)
        {
            Requires.NotNull(configuration, nameof(configuration));
            Requires.NotNull(healthReporter, nameof(healthReporter));

            //// TODO also target .NET Core 2.1 and use IHttpClientFactory and Polly for exponential backoff etc.
            var services = new ServiceCollection();

            services.AddSingleton <SplunkOutputConfiguration>(s =>
            {
                var configFactory = new SplunkOutputConfigurationFactory();
                return(configFactory.Create(configuration, healthReporter));
            });
            services.AddSingleton <ISplunkHttpMessageHandlerFactory, SplunkHttpMessageHandlerFactory>();
            services.AddSingleton <ISplunkHttpClientFactory, SplunkHttpClientFactory>();
            services.AddTransient <HttpClient>(s => s.GetRequiredService <ISplunkHttpClientFactory>().Create());
            services.AddSingleton <ISplunkHttpEventCollectorClient, SplunkHttpEventCollectorClient>();

            var serviceProvider = services.BuildServiceProvider();
            var splunkHttpEventCollectorClient = serviceProvider.GetRequiredService <ISplunkHttpEventCollectorClient>();

            return(new SplunkOutput(splunkHttpEventCollectorClient, healthReporter));
        }
Esempio n. 6
0
        public void Create_WithAllConfigItems_SetsAllProperties()
        {
            // Arrange
            var healthReporter = new Mock <IHealthReporter>();
            var builder        = new ConfigurationBuilder();

            builder
            .AddInMemoryCollection(new Dictionary <string, string>()
            {
                { "type", "SplunkOutput" },
                { "serviceBaseAddress", "https://hec.mysplunkserver.com:8088" },
                { "authenticationToken", "B5A79AAD-D822-46CC-80D1-819F80D7BFB0" },
                { "host", "localhost" },
                { "index", "main" },
                { "source", "my source" },
                { "sourceType", "_json" },
                { "ignoreSslCertificateErrors", "true" },
                { "maxRetryAttempts", "5" }
            });
            var configuration = builder.Build();

            // Act
            var factory = new SplunkOutputConfigurationFactory();
            var splunkOutputConfiguration = factory.Create(configuration, healthReporter.Object);

            // Assert
            Assert.NotNull(splunkOutputConfiguration);
            Assert.Equal("SplunkOutput", splunkOutputConfiguration.Type);
            Assert.Equal("https://hec.mysplunkserver.com:8088", splunkOutputConfiguration.ServiceBaseAddress);
            Assert.Equal("B5A79AAD-D822-46CC-80D1-819F80D7BFB0", splunkOutputConfiguration.AuthenticationToken);
            Assert.Equal("localhost", splunkOutputConfiguration.Host);
            Assert.Equal("main", splunkOutputConfiguration.Index);
            Assert.Equal("my source", splunkOutputConfiguration.Source);
            Assert.Equal("_json", splunkOutputConfiguration.SourceType);
            Assert.True(splunkOutputConfiguration.IgnoreSslCertificateErrors);
            Assert.Equal(5, splunkOutputConfiguration.MaxRetryAttempts);
        }