Ejemplo n.º 1
0
        public async Task Should_combine_secret_sources_in_order()
        {
            application = new Application(
                env =>
            {
                env.ConfigurationProvider.Get <int[]>(env.SecretConfigurationSource.ScopeTo("Array"))
                .Should().BeEquivalentTo(new[] { 1, 2, 3 }, options => options.WithStrictOrdering());
                env.ConfigurationProvider.Get <int[]>(env.ConfigurationSource.ScopeTo("Array"))
                .Should().BeEmpty();
            });

            host = new VostokHost(new TestHostSettings(application, setup =>
            {
                SetupEnvironment(setup);

                setup.SetupConfiguration(c => c.AddSecretSource(new ObjectSource(new { Array = new[] { 1 } })));
                setup.SetupConfiguration(c => c.AddSecretSource(new ObjectSource(new { Array = new[] { 2 } })));
                setup.SetupConfiguration(c => c.CustomizeSecretConfigurationSource(s => s.CombineWith(new ObjectSource(new { Array = new[] { 3 } }), new SettingsMergeOptions {
                    ArrayMergeStyle = ArrayMergeStyle.Concat
                })));

                setup.SetupConfiguration(c => c.CustomizeSecretSettingsMerging(s => s.ArrayMergeStyle = ArrayMergeStyle.Concat));

                setup.SetupConfiguration(c => c.GetIntermediateSecretConfiguration <int[]>("Array")
                                         .Should().BeEquivalentTo(new[] { 1, 2, 3 }, options => options.WithStrictOrdering()));

                setup.SetupConfiguration(c => c.CustomizeConfigurationContext(env =>
                                                                              env.ConfigurationProvider.Get <int[]>(env.SecretConfigurationSource.ScopeTo("Array"))
                                                                              .Should().BeEquivalentTo(new[] { 1, 2, 3 }, options => options.WithStrictOrdering())));
            }));

            var result = await host.RunAsync();

            result.State.Should().Be(VostokApplicationState.Exited);
        }
Ejemplo n.º 2
0
        public async Task Should_allow_to_use_datacenters_during_clusterconfig_setup()
        {
            application = new Application();

            host = new VostokHost(new TestHostSettings(application,
                                                       builder =>
            {
                builder.SetupApplicationIdentity(
                    id => id
                    .SetProject("infra")
                    .SetSubproject("vostok")
                    .SetApplication("app")
                    .SetInstance("1"));

                builder.SetupApplicationIdentity((id, ctx) => id.SetEnvironment("env"));

                builder.SetupLog(b => b.SetupConsoleLog());

                builder.SetupConfiguration(
                    (config, context) => { context.Datacenters.GetLocalDatacenter().Should().BeNull(); });
            }));

            var result = await host.RunAsync();

            result.State.Should().Be(VostokApplicationState.Exited);
        }
Ejemplo n.º 3
0
        public async Task Should_allow_to_use_configuration_during_configuration_setup()
        {
            application = new Application();

            host = new VostokHost(new TestHostSettings(application,
                                                       builder =>
            {
                builder.SetupApplicationIdentity(
                    id => id
                    .SetProject("infra")
                    .SetSubproject("vostok")
                    .SetApplication("app")
                    .SetInstance("1"));

                builder.SetupApplicationIdentity((id, ctx) => id.SetEnvironment("env"));

                builder.SetupConfiguration(
                    config =>
                {
                    config.AddSource(new ObjectSource(new
                    {
                        A = "hello"
                    }));
                });

                builder.SetupConfiguration(
                    config =>
                    config.GetIntermediateConfiguration <ApplicationSettings>().A.Should().Be("hello"));
            }));

            var result = await host.RunAsync();

            result.State.Should().Be(VostokApplicationState.Exited);
        }
Ejemplo n.º 4
0
        public async Task Should_allow_to_setup_sources_in_an_idempotent_way()
        {
            application = new Application(
                env =>
            {
                env.ConfigurationProvider.SetupSourceFor <ApplicationSettings>(env.ConfigurationSource);
                env.SecretConfigurationProvider.SetupSourceFor <ApplicationSecretSettings>(env.SecretConfigurationSource);
            });

            host = new VostokHost(new TestHostSettings(application, SetupEnvironment));

            var result = await host.RunAsync();

            result.State.Should().Be(VostokApplicationState.Exited);
        }
Ejemplo n.º 5
0
        public async Task Should_allow_to_use_statically_configured_app_identity_properties_during_configuration_setup()
        {
            application = new Application();

            host = new VostokHost(new TestHostSettings(application,
                                                       builder =>
            {
                builder.SetupApplicationIdentity(
                    id => id
                    .SetProject("infra")
                    .SetSubproject("vostok")
                    .SetApplication("app")
                    .SetInstance("1"));

                builder.SetupApplicationIdentity((id, ctx) => id.SetEnvironment("env"));

                builder.SetupLog(log => log.SetupConsoleLog());

                builder.SetupConfiguration(
                    (config, ctx) =>
                {
                    ctx.Log.Info(ctx.ApplicationIdentity.ToString());

                    ctx.ApplicationIdentity.Project.Should().Be("infra");
                    ctx.ApplicationIdentity.Subproject.Should().Be("vostok");
                    ctx.ApplicationIdentity.Application.Should().Be("app");
                    ctx.ApplicationIdentity.Instance.Should().Be("1");

                    // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
                    Action action = () => ctx.ApplicationIdentity.Environment.GetHashCode();

                    var exception = action.Should().Throw <InvalidOperationException>().Which;

                    Console.Out.WriteLine(exception);
                });
            }));

            var result = await host.RunAsync();

            result.State.Should().Be(VostokApplicationState.Exited);
        }
Ejemplo n.º 6
0
        public async Task Should_allow_to_use_configuration_during_logging_setup(LogLevel logLevel)
        {
            application = new Application();

            host = new VostokHost(new TestHostSettings(application,
                                                       builder =>
            {
                builder.SetupApplicationIdentity(
                    id => id
                    .SetProject("infra")
                    .SetSubproject("vostok")
                    .SetApplication("app")
                    .SetInstance("1"));

                builder.SetupApplicationIdentity((id, ctx) => id.SetEnvironment("env"));

                builder.SetupLog((b, ctx) =>
                {
                    b.SetupConsoleLog(
                        c => c.SetupMinimumLevelProvider(
                            () => ctx.ConfigurationProvider.Get <ApplicationSettings>().LogLevel));
                });

                builder.SetupConfiguration(
                    config =>
                {
                    config.AddSource(new ObjectSource(new
                    {
                        LogLevel = logLevel
                    }));
                });
            }));

            var result = await host.RunAsync();

            result.State.Should().Be(VostokApplicationState.Exited);
        }
Ejemplo n.º 7
0
        private Task <VostokApplicationRunResult> CreateAndRunAsync(
            [NotNull] SimpleApplicationSettings settings,
            [CanBeNull] Action <IVostokHostingEnvironmentBuilder> additionalEnvSetup = null,
            [CanBeNull] Action <VostokHostSettings> additionalHostSetup = null)
        {
            observer = new TestObserver <VostokApplicationState>();

            settings.CrashError = crashError;
            application         = new SimpleApplication(settings);

            void EnvironmentSetup(IVostokHostingEnvironmentBuilder builder)
            {
                builder.SetupApplicationIdentity(
                    (applicationIdentitySetup, setupContext) => applicationIdentitySetup.SetProject("Infrastructure")
                    .SetSubproject("vostok")
                    .SetEnvironment("dev")
                    .SetApplication("simple-application")
                    .SetInstance("1"))
                .SetupLog(logSetup => logSetup.SetupConsoleLog(consoleLogSetup => consoleLogSetup.UseSynchronous()));

                additionalEnvSetup?.Invoke(builder);
            }

            var hostSettings = new TestHostSettings(application, EnvironmentSetup)
            {
                ShutdownTimeout = shutdownTimeout
            };

            additionalHostSetup?.Invoke(hostSettings);

            host = new VostokHost(hostSettings);

            host.OnApplicationStateChanged.Subscribe(observer);

            return(host.RunAsync());
        }
Ejemplo n.º 8
0
        public async Task Should_support_well_known_substitutions()
        {
            application = new Application(
                env =>
            {
                Action assertion = () =>
                {
                    var settings = env.ConfigurationProvider.Get <ApplicationSettings>();
                    var secrets  = env.SecretConfigurationProvider.Get <ApplicationSecretSettings>();

                    settings.A.Should().Be("infra");
                    settings.B.Should().Be("vostok");
                    settings.C.Should().Be("app");
                    settings.D.Should().Be("dev");
                    settings.E.Should().Be("1");

                    secrets.F.Should().Be("sd-app");
                    secrets.G.Should().Be("sd-env");
                };

                assertion.ShouldPassIn(10.Seconds(), 100.Milliseconds());
            });

            host = new VostokHost(new TestHostSettings(application,
                                                       builder =>
            {
                builder.SetupApplicationIdentity(
                    id => id
                    .SetProject("infra")
                    .SetSubproject("vostok")
                    .SetEnvironment("dev")
                    .SetApplication("app")
                    .SetInstance("1"));

                builder.SetupLog(log => log.SetupConsoleLog());

                builder.SetBeaconApplication("sd-app");
                builder.SetBeaconEnvironment("sd-env");

                builder.SetupConfiguration(
                    config =>
                {
                    config.AddSource(new ObjectSource(new
                    {
                        A = $"#{{{VostokConfigurationPlaceholders.IdentityProject}}}",
                        B = $"#{{{VostokConfigurationPlaceholders.IdentitySubproject}}}",
                        C = $"#{{{VostokConfigurationPlaceholders.IdentityApplication}}}",
                        D = $"#{{{VostokConfigurationPlaceholders.IdentityEnvironment}}}",
                        E = $"#{{{VostokConfigurationPlaceholders.IdentityInstance}}}"
                    }));

                    config.AddSecretSource(new ObjectSource(new
                    {
                        F = $"#{{{VostokConfigurationPlaceholders.ServiceDiscoveryApplication}}}",
                        G = $"#{{{VostokConfigurationPlaceholders.ServiceDiscoveryEnvironment}}}",
                    }));
                });

                builder.SetupHerculesSink(
                    (sink, context) =>
                {
                    context.ConfigurationProvider.Get <ApplicationSettings>();
                    context.SecretConfigurationProvider.Get <ApplicationSecretSettings>();
                });
            }));

            var result = await host.RunAsync();

            result.State.Should().Be(VostokApplicationState.Exited);
        }