public void CreateClient_Succeeds()
        {
            var opts = new ConsulOptions()
            {
                Host       = "foobar",
                Datacenter = "datacenter",
                Token      = "token",
                Username   = "******",
                Password   = "******",
                Port       = 5555,
                Scheme     = "https",
                WaitTime   = "5s"
            };

            var client = ConsulClientFactory.CreateClient(opts) as ConsulClient;

            Assert.NotNull(client);
            Assert.NotNull(client.Config);
            Assert.Equal(opts.Datacenter, client.Config.Datacenter);
            Assert.Equal(opts.Token, client.Config.Token);
            Assert.Equal(opts.Host, client.Config.Address.Host);
            Assert.Equal(opts.Port, client.Config.Address.Port);
            Assert.Equal(opts.Scheme, client.Config.Address.Scheme);
            Assert.Equal(new TimeSpan(0, 0, 5), client.Config.WaitTime);
        }
Example #2
0
        public void CreateClient_Options_Override()
        {
            var datacenter = "datacenter";
            var address    = new Uri("http://address");
            var token      = "token";
            var waitTime   = TimeSpan.FromSeconds(30);

            var monitor = ConsulOptionsTestHelper.CreateOptionsMonitor(
                new[]
            {
                new ConfigureOptions <ConsulClientConfiguration>(options =>
                {
                    options.Datacenter = datacenter;
                    options.Address    = address;
                    options.Token      = token;
                    options.WaitTime   = waitTime;
                })
            });

            var factory = new ConsulClientFactory(monitor);

            var client = factory.CreateClient() as ConsulClient;

            var configuration = client.Config;

            Assert.Same(monitor.Get(Options.DefaultName), configuration);
            Assert.Equal(datacenter, configuration.Datacenter);
            Assert.Equal(address, configuration.Address);
            Assert.Equal(token, configuration.Token);
            Assert.Equal(waitTime, configuration.WaitTime);
        }
Example #3
0
        private static void AddConsulServices(IServiceCollection services, IConfiguration config, IDiscoveryLifecycle lifecycle)
        {
            services.AddSingleton <IConsulClient>((p) =>
            {
                var consulOptions = p.GetRequiredService <IOptions <ConsulOptions> >();
                return(ConsulClientFactory.CreateClient(consulOptions.Value));
            });

            services.AddSingleton <IScheduler, TtlScheduler>();
            services.AddSingleton <IConsulServiceRegistry, ConsulServiceRegistry>();
            services.AddSingleton <IConsulRegistration>((p) =>
            {
                var opts = p.GetRequiredService <IOptions <ConsulDiscoveryOptions> >();
                return(ConsulRegistration.CreateRegistration(config, opts.Value));
            });
            services.AddSingleton <IConsulServiceRegistrar, ConsulServiceRegistrar>();
            services.AddSingleton <IDiscoveryClient, ConsulDiscoveryClient>();
            services.AddSingleton <IHealthContributor, ConsulHealthContributor>();
        }
        private static void AddConsulServices(ContainerBuilder container, IConfiguration config, IDiscoveryLifecycle lifecycle)
        {
            container.Register(c =>
            {
                var opts = c.Resolve <IOptions <ConsulOptions> >();
                return(ConsulClientFactory.CreateClient(opts.Value));
            }).As <IConsulClient>().SingleInstance();

            container.RegisterType <TtlScheduler>().As <IScheduler>().SingleInstance();
            container.RegisterType <ConsulServiceRegistry>().As <IConsulServiceRegistry>().SingleInstance();
            container.Register(c =>
            {
                var opts = c.Resolve <IOptions <ConsulDiscoveryOptions> >();
                return(ConsulRegistration.CreateRegistration(config, opts.Value));
            }).As <IConsulRegistration>().SingleInstance();

            container.RegisterType <ConsulServiceRegistrar>().As <IConsulServiceRegistrar>().SingleInstance();
            container.RegisterType <ConsulDiscoveryClient>().As <IDiscoveryClient>().SingleInstance();

            container.RegisterType <ConsulHealthContributor>().As <IHealthContributor>().SingleInstance();
        }
 public void CreateClient_ThrowsNullOptions()
 {
     Assert.Throws <ArgumentNullException>(() => ConsulClientFactory.CreateClient(null));
 }