Exemple #1
0
        public void when_there_is_no_base_configuration_then_the_resulting_config_is_not_combined()
        {
            var resolver      = new ConfigurationResolver();
            var configuration = resolver.Resolve();

            Assert.That(configuration, Is.Not.TypeOf <CombinedSpectatorConfiguration>());
        }
Exemple #2
0
        public void when_there_is_an_existing_base_configuration_then_the_resulting_config_is_combined()
        {
            File.WriteAllText(ConfigurationResolver.BaseSpectatorConfigFile, @"{
                      ""statsdHost"": ""basehost"",
                      ""statsdPort"": 8125,
                      ""metricPrefix"": ""base.prefix"",
                      ""interval"":  ""00:00:01"",
                      ""metrics"": [
                        {
                          ""source"": ""performanceCounter"",
                          ""path"": ""\\Processor(_Total)\\Interrupts/sec"",
                          ""type"": ""gauge"",
                          ""template"": ""base.performance.counter""
                        },
                        {
                          ""source"": ""performanceCounter"",
                          ""path"": ""\\Processor(_Total)\\Interrupts/sec"",
                          ""type"": ""gauge"",
                          ""template"": ""overriden.performance.counter""
                        }
                      ]
                    }", Encoding.UTF8);

            var resolver      = new ConfigurationResolver();
            var configuration = resolver.Resolve();

            Assert.That(configuration, Is.TypeOf <ExpiringConfigurationDecorator>());

            var expiringConfig     = (ExpiringConfigurationDecorator)configuration;
            var innerConfiguration = expiringConfig.InnerConfiguration;

            Assert.That(innerConfiguration, Is.TypeOf <CombinedSpectatorConfiguration>());
        }
        public void Given_EnvironmentVariables_When_Instantiated_Then_It_Should_Return_Result(string hideSwaggerUI, bool expectedHideSwaggerUI,
                                                                                              string hideDocument, bool expectedHideDocument,
                                                                                              string apiKey,
                                                                                              string authLevelDoc, AuthorizationLevel expectedAuthLevelDoc,
                                                                                              string authLevelUI, AuthorizationLevel expectedAuthLevelUI,
                                                                                              string proxyUrl, string hostnames)
        {
            Environment.SetEnvironmentVariable("OpenApi__HideSwaggerUI", hideSwaggerUI);
            Environment.SetEnvironmentVariable("OpenApi__HideDocument", hideDocument);
            Environment.SetEnvironmentVariable("OpenApi__ApiKey", apiKey);
            Environment.SetEnvironmentVariable("OpenApi__AuthLevel__Document", authLevelDoc);
            Environment.SetEnvironmentVariable("OpenApi__AuthLevel__UI", authLevelUI);
            Environment.SetEnvironmentVariable("OpenApi__BackendProxyUrl", proxyUrl);
            Environment.SetEnvironmentVariable("OpenApi__HostNames", hostnames);

            var config   = ConfigurationResolver.Resolve();
            var settings = config.Get <OpenApiSettings>("OpenApi");

            settings.HideSwaggerUI.Should().Be(expectedHideSwaggerUI);
            settings.HideDocument.Should().Be(expectedHideDocument);
            settings.ApiKey.Should().Be(apiKey);
            settings.AuthLevel.Document.Should().Be(expectedAuthLevelDoc);
            settings.AuthLevel.UI.Should().Be(expectedAuthLevelUI);
            settings.BackendProxyUrl.Should().Be(proxyUrl);
            settings.HostNames.Should().Be(hostnames);
        }
        /// <summary>
        /// Gets the <see cref="IConfiguration"/> instance from openapisettings.json
        /// </summary>
        /// <param name="config"><see cref="IConfiguration"/> instance from the environment variables - either local.settings.json or App Settings blade.</param>
        /// <param name="basePath">Base path of the executing Azure Functions assembly.</param>
        public static IConfiguration Resolve(IConfiguration config = null, string basePath = null)
        {
            if (config.IsNullOrDefault())
            {
                config = ConfigurationResolver.Resolve();
            }

            if (basePath.IsNullOrWhiteSpace())
            {
                basePath = ConfigurationResolver.GetBasePath(config);
            }

            var builder = new ConfigurationBuilder();

            if (!File.Exists($"{basePath.TrimEnd('/')}/openapisettings.json"))
            {
                return(builder.Build());
            }

            var openapi = builder.SetBasePath(basePath)
                          .AddJsonFile("openapisettings.json")
                          .Build();

            return(openapi);
        }
        /// <inheritdoc />
        public void Configure(IWebJobsBuilder builder)
        {
            var config   = ConfigurationResolver.Resolve();
            var settings = config.Get <OpenApiSettings>(OpenApiSettingsKey);

            builder.Services.AddSingleton(settings);
            builder.Services.AddSingleton <IFunctionProvider, OpenApiTriggerFunctionProvider>();
        }
        /// <summary>
        /// Gets the <see cref="IConfiguration"/> instance from host.json
        /// </summary>
        /// <param name="config"><see cref="IConfiguration"/> instance from the environment variables - either local.settings.json or App Settings blade.</param>
        /// <param name="basePath">Base path of the executing Azure Functions assembly.</param>
        public static IConfiguration Resolve(IConfiguration config = null, string basePath = null)
        {
            if (config.IsNullOrDefault())
            {
                config = ConfigurationResolver.Resolve();
            }

            if (basePath.IsNullOrWhiteSpace())
            {
                basePath = ConfigurationResolver.GetBasePath(config);
            }

            var host = new ConfigurationBuilder()
                       .SetBasePath(basePath)
                       .AddJsonFile("host.json")
                       .Build();

            return(host);
        }
        /// <summary>
        /// Creates mocked instance of the <see cref="AppSettings"/> class.
        /// </summary>
        /// <returns>Returns the <see cref="Mock{AppSettings}"/> instance.</returns>
        public Mock <AppSettings> CreateAppSettingsInstance()
        {
            var config = ConfigurationResolver.Resolve();

            var settings = new Mock <AppSettings>();

            settings.Protected()
            .As <IAppSettingsProtected>()
            .Setup(p => p.Config)
            .Returns(config);

            var shortenUrlSettings = new Mock <ShortenUrlSettings>();
            var cosmosDbSettings   = new Mock <CosmosDbSettings>();

            settings.SetupGet(p => p.ShortenUrl).Returns(shortenUrlSettings.Object);
            settings.SetupGet(p => p.CosmosDb).Returns(cosmosDbSettings.Object);

            return(settings);
        }
Exemple #8
0
        public static void Main(string[] args)
        {
            System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);

            var configurationResolver = new ConfigurationResolver();

            Log.Info("Starting spectator topshelf host");

            HostFactory.Run(hostConfigurator =>
            {
                hostConfigurator.Service <SpectatorService>(serviceConfigurator =>
                {
                    var configuration = configurationResolver.Resolve();
                    serviceConfigurator.ConstructUsing(() =>
                                                       new SpectatorService(
                                                           configuration,
                                                           new QueryableSourceFactory(),
                                                           new StatsdPublisher(
                                                               new Statsd(
                                                                   new StatsdUDP(
                                                                       configuration.StatsdHost,
                                                                       configuration.StatsdPort
                                                                       )
                                                                   )
                                                               ),
                                                           new MetricFormatter()
                                                           )
                                                       );
                    serviceConfigurator.WhenStarted(myService => myService.Start());
                    serviceConfigurator.WhenStopped(myService => myService.Stop());
                });

                hostConfigurator.RunAsLocalSystem();

                hostConfigurator.SetDisplayName(@"Spectator Agent");
                hostConfigurator.SetDescription(@"Monitors system metrics and sends them to a statsd-compatible server.");
                hostConfigurator.SetServiceName(@"Spectator");
            });
        }
        /// <summary>
        /// Gets the <see cref="OpenApiInfo"/> instance from one of host.json, openapisettings.json and environment variables.
        /// </summary>
        /// <param name="host"><see cref="IConfiguration"/> instance representing host.json.</param>
        /// <param name="openapi"><see cref="IConfiguration"/> instance representing openapisettings.json.</param>
        /// <param name="appsettings"><see cref="IConfiguration"/> instance representing environment variables.</param>
        /// <returns>Returns <see cref="OpenApiInfo"/> instance resolved.</returns>
        public static OpenApiInfo Resolve(IConfiguration host = null, IConfiguration openapi = null, IConfiguration appsettings = null)
        {
            if (host.IsNullOrDefault())
            {
                host = HostJsonResolver.Resolve();
            }

            var info = host.Get <OpenApiInfo>("openApi:info");

            if (info.IsValid())
            {
                return(info);
            }

            if (openapi.IsNullOrDefault())
            {
                openapi = OpenApiSettingsJsonResolver.Resolve();
            }

            info = openapi.Get <OpenApiInfo>("info");
            if (info.IsValid())
            {
                return(info);
            }

            if (appsettings.IsNullOrDefault())
            {
                appsettings = ConfigurationResolver.Resolve();
            }

            info = appsettings.Get <OpenApiInfo>("OpenApi:Info");
            if (info.IsValid())
            {
                return(info);
            }

            throw new InvalidOperationException("Open API metadata not found");
        }
Exemple #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AppSettingsBase"/> class.
 /// </summary>
 protected AppSettingsBase()
 {
     this.Config = ConfigurationResolver.Resolve();
 }