示例#1
0
        public async Task Agent_Services_MultipleChecks()
        {
            var svcID        = KVTest.GenerateTestKeyName();
            var registration = new AgentServiceRegistration
            {
                Name   = svcID,
                Tags   = new[] { "bar", "baz" },
                Port   = 8000,
                Checks = new[]
                {
                    new AgentServiceCheck
                    {
                        TTL = TimeSpan.FromSeconds(15)
                    },
                    new AgentServiceCheck
                    {
                        TTL = TimeSpan.FromSeconds(15)
                    }
                }
            };
            await _client.Agent.ServiceRegister(registration);

            var services = await _client.Agent.Services();

            Assert.True(services.Response.ContainsKey(svcID));

            var checks = await _client.Agent.Checks();

            Assert.True(checks.Response.ContainsKey("service:" + svcID + ":1"));
            Assert.True(checks.Response.ContainsKey("service:" + svcID + ":2"));

            await _client.Agent.ServiceDeregister(svcID);
        }
示例#2
0
        public async Task Health_Checks()
        {
            var client       = new ConsulClient();
            var svcID        = KVTest.GenerateTestKeyName();
            var registration = new AgentServiceRegistration()
            {
                Name  = svcID,
                Tags  = new[] { "bar", "baz" },
                Port  = 8000,
                Check = new AgentServiceCheck
                {
                    TTL = TimeSpan.FromSeconds(15)
                }
            };

            try
            {
                await client.Agent.ServiceRegister(registration);

                var checks = await client.Health.Checks(svcID);

                Assert.NotEqual((ulong)0, checks.LastIndex);
                Assert.NotEqual(0, checks.Response.Length);
            }
            finally
            {
                await client.Agent.ServiceDeregister(svcID);
            }
        }
示例#3
0
        public async Task Agent_FilterServices()
        {
            var svcID1     = KVTest.GenerateTestKeyName();
            var svcID2     = KVTest.GenerateTestKeyName();
            var uniqueMeta = KVTest.GenerateTestKeyName();

            var reg1 = new AgentServiceRegistration
            {
                Name = svcID1,
                Meta = new Dictionary <string, string> {
                    { uniqueMeta, "bar1" }
                },
            };

            var reg2 = new AgentServiceRegistration
            {
                Name = svcID2,
                Meta = new Dictionary <string, string> {
                    { uniqueMeta, "bar2" }
                },
            };

            await _client.Agent.ServiceRegister(reg1);

            await _client.Agent.ServiceRegister(reg2);

            var idSelector   = new StringFieldSelector("ID");
            var metaSelector = new MetaSelector();

            Assert.Equal(svcID1, (await _client.Agent.Services(idSelector == svcID1)).Response.Keys.Single());
            Assert.Equal(svcID1, (await _client.Agent.Services(idSelector == svcID1)).Response.Keys.Single());
            Assert.Equal(svcID1, (await _client.Agent.Services(metaSelector[uniqueMeta] == "bar1")).Response.Keys.Single());
            Assert.Equal(svcID2, (await _client.Agent.Services(metaSelector[uniqueMeta] == "bar2")).Response.Keys.Single());
        }
示例#4
0
        public async Task Agent_Services_CheckPassing()
        {
            var svcID        = KVTest.GenerateTestKeyName();
            var registration = new AgentServiceRegistration
            {
                Name  = svcID,
                Tags  = new[] { "bar", "baz" },
                Port  = 8000,
                Check = new AgentServiceCheck
                {
                    TTL    = TimeSpan.FromSeconds(15),
                    Status = HealthStatus.Passing
                }
            };

            await _client.Agent.ServiceRegister(registration);

            var services = await _client.Agent.Services();

            Assert.True(services.Response.ContainsKey(svcID));

            var checks = await _client.Agent.Checks();

            Assert.True(checks.Response.ContainsKey("service:" + svcID));

            Assert.Equal(HealthStatus.Passing, checks.Response["service:" + svcID].Status);

            await _client.Agent.ServiceDeregister(svcID);
        }
示例#5
0
        public async Task Agent_Checks_Docker()
        {
            var svcID      = KVTest.GenerateTestKeyName();
            var serviceReg = new AgentServiceRegistration
            {
                Name = svcID
            };
            await _client.Agent.ServiceRegister(serviceReg);

            var reg = new AgentCheckRegistration
            {
                Name              = "redischeck",
                ServiceID         = svcID,
                DockerContainerID = "f972c95ebf0e",
                Args              = new string[] { "/bin/true" },
                Shell             = "/bin/bash",
                Interval          = TimeSpan.FromSeconds(10)
            };
            await _client.Agent.CheckRegister(reg);

            var checks = await _client.Agent.Checks();

            Assert.True(checks.Response.ContainsKey("redischeck"));
            Assert.Equal(svcID, checks.Response["redischeck"].ServiceID);

            await _client.Agent.CheckDeregister("redischeck");

            await _client.Agent.ServiceDeregister(svcID);
        }
示例#6
0
        public async Task Agent_Checks_ServiceBound()
        {
            var svcID      = KVTest.GenerateTestKeyName();
            var serviceReg = new AgentServiceRegistration
            {
                Name = svcID
            };
            await _client.Agent.ServiceRegister(serviceReg);

            var reg = new AgentCheckRegistration
            {
                Name      = "redischeck",
                ServiceID = svcID,
                TTL       = TimeSpan.FromSeconds(15),
                DeregisterCriticalServiceAfter = TimeSpan.FromMinutes(90)
            };
            await _client.Agent.CheckRegister(reg);

            var checks = await _client.Agent.Checks();

            Assert.True(checks.Response.ContainsKey("redischeck"));
            Assert.Equal(svcID, checks.Response["redischeck"].ServiceID);

            await _client.Agent.CheckDeregister("redischeck");

            await _client.Agent.ServiceDeregister(svcID);
        }
示例#7
0
        public async Task Agent_EnableTagOverride()
        {
            var svcID1 = KVTest.GenerateTestKeyName();
            var svcID2 = KVTest.GenerateTestKeyName();
            var reg1   = new AgentServiceRegistration
            {
                Name              = svcID1,
                Port              = 8000,
                Address           = "192.168.0.42",
                EnableTagOverride = true
            };

            var reg2 = new AgentServiceRegistration
            {
                Name = svcID2,
                Port = 8000
            };

            await _client.Agent.ServiceRegister(reg1);

            await _client.Agent.ServiceRegister(reg2);

            var services = await _client.Agent.Services();

            Assert.Contains(svcID1, services.Response.Keys);
            Assert.True(services.Response[svcID1].EnableTagOverride);

            Assert.Contains(svcID2, services.Response.Keys);
            Assert.False(services.Response[svcID2].EnableTagOverride);
        }
示例#8
0
        public async Task Agent_ServiceAddress()
        {
            var svcID1        = KVTest.GenerateTestKeyName();
            var svcID2        = KVTest.GenerateTestKeyName();
            var registration1 = new AgentServiceRegistration
            {
                Name    = svcID1,
                Port    = 8000,
                Address = "192.168.0.42"
            };
            var registration2 = new AgentServiceRegistration
            {
                Name = svcID2,
                Port = 8000
            };

            await _client.Agent.ServiceRegister(registration1);

            await _client.Agent.ServiceRegister(registration2);

            var services = await _client.Agent.Services();

            Assert.True(services.Response.ContainsKey(svcID1));
            Assert.True(services.Response.ContainsKey(svcID2));
            Assert.Equal("192.168.0.42", services.Response[svcID1].Address);
            Assert.True(string.IsNullOrEmpty(services.Response[svcID2].Address));

            await _client.Agent.ServiceDeregister(svcID1);

            await _client.Agent.ServiceDeregister(svcID2);
        }
示例#9
0
        public async Task Agent_ServiceTaggedAddresses()
        {
            var svcID        = KVTest.GenerateTestKeyName();
            var registration = new AgentServiceRegistration
            {
                Name            = svcID,
                Port            = 8000,
                TaggedAddresses = new Dictionary <string, ServiceTaggedAddress>
                {
                    { "lan", new ServiceTaggedAddress {
                          Address = "127.0.0.1", Port = 80
                      } },
                    { "wan", new ServiceTaggedAddress {
                          Address = "192.168.10.10", Port = 8000
                      } }
                }
            };

            await _client.Agent.ServiceRegister(registration);

            var services = await _client.Agent.Services();

            Assert.True(services.Response.ContainsKey(svcID));
            Assert.True(services.Response[svcID].TaggedAddresses.Count > 0);
            Assert.True(services.Response[svcID].TaggedAddresses.ContainsKey("wan"));
            Assert.True(services.Response[svcID].TaggedAddresses.ContainsKey("lan"));


            await _client.Agent.ServiceDeregister(svcID);
        }
示例#10
0
        public async Task Snapshot_TakeRestore()
        {
            var keyName = KVTest.GenerateTestKeyName();

            var key = new KVPair(keyName)
            {
                Value = Encoding.UTF8.GetBytes("hello")
            };

            var putResponse = await _client.KV.Put(key);

            Assert.True(putResponse.Response);

            Assert.Equal(Encoding.UTF8.GetBytes("hello"), (await _client.KV.Get(keyName)).Response.Value);

            var snap = await _client.Snapshot.Save();

            Assert.NotEqual <ulong>(0, snap.LastIndex);
            Assert.True(snap.KnownLeader);

            key.Value = Encoding.UTF8.GetBytes("goodbye");

            putResponse = await _client.KV.Put(key);

            Assert.True(putResponse.Response);

            Assert.Equal(Encoding.UTF8.GetBytes("goodbye"), (await _client.KV.Get(keyName)).Response.Value);

            await _client.Snapshot.Restore(snap.Response);

            Assert.Equal(Encoding.UTF8.GetBytes("hello"), (await _client.KV.Get(keyName)).Response.Value);
        }
示例#11
0
        public async Task Snapshot_TakeRestore()
        {
            using (var client = new ConsulClient((c) => { c.Token = ACLTest.ConsulRoot; }))
            {
                var keyName = KVTest.GenerateTestKeyName();

                var key = new KVPair(keyName)
                {
                    Value = Encoding.UTF8.GetBytes("hello")
                };

                Assert.True((await client.KV.Put(key)).Response);

                Assert.Equal(Encoding.UTF8.GetBytes("hello"), (await client.KV.Get(keyName)).Response.Value);

                var snap = await client.Snapshot.Save();

                Assert.NotEqual <ulong>(0, snap.LastIndex);
                Assert.True(snap.KnownLeader);

                key.Value = Encoding.UTF8.GetBytes("goodbye");

                Assert.True((await client.KV.Put(key)).Response);

                Assert.Equal(Encoding.UTF8.GetBytes("goodbye"), (await client.KV.Get(keyName)).Response.Value);

                await client.Snapshot.Restore(snap.Response);

                Assert.Equal(Encoding.UTF8.GetBytes("hello"), (await client.KV.Get(keyName)).Response.Value);
            }
        }
示例#12
0
        public async Task Catalog_GetTaggedAddressesService()
        {
            var svcID        = KVTest.GenerateTestKeyName();
            var registration = new CatalogRegistration
            {
                Datacenter = "dc1",
                Node       = "foobar",
                Address    = "192.168.10.10",
                Service    = new AgentService
                {
                    ID              = svcID,
                    Service         = "redis",
                    Tags            = new[] { "master", "v1" },
                    Port            = 8000,
                    TaggedAddresses = new Dictionary <string, ServiceTaggedAddress>
                    {
                        { "lan", new ServiceTaggedAddress {
                              Address = "127.0.0.1", Port = 80
                          } },
                        { "wan", new ServiceTaggedAddress {
                              Address = "192.168.10.10", Port = 8000
                          } }
                    }
                }
            };

            await _client.Catalog.Register(registration);

            var services = await _client.Catalog.Service("redis");

            Assert.True(services.Response.Length > 0);
            Assert.True(services.Response[0].ServiceTaggedAddresses.Count > 0);
            Assert.True(services.Response[0].ServiceTaggedAddresses.ContainsKey("wan"));
            Assert.True(services.Response[0].ServiceTaggedAddresses.ContainsKey("lan"));
        }
示例#13
0
        public async Task Health_GetServiceWithTaggedAddresses()
        {
            var svcID        = KVTest.GenerateTestKeyName();
            var registration = new AgentServiceRegistration
            {
                Name            = svcID,
                Port            = 8000,
                TaggedAddresses = new Dictionary <string, ServiceTaggedAddress>
                {
                    { "lan", new ServiceTaggedAddress {
                          Address = "127.0.0.1", Port = 80
                      } },
                    { "wan", new ServiceTaggedAddress {
                          Address = "192.168.10.10", Port = 8000
                      } }
                }
            };

            await _client.Agent.ServiceRegister(registration);

            var checks = await _client.Health.Service(svcID, "", false);

            Assert.NotEqual((ulong)0, checks.LastIndex);
            Assert.NotEmpty(checks.Response);

            Assert.True(checks.Response[0].Service.TaggedAddresses.Count > 0);
            Assert.True(checks.Response[0].Service.TaggedAddresses.ContainsKey("wan"));
            Assert.True(checks.Response[0].Service.TaggedAddresses.ContainsKey("lan"));

            await _client.Agent.ServiceDeregister(svcID);
        }
示例#14
0
        public async Task Catalog_EnableTagOverride()
        {
            var svcID   = KVTest.GenerateTestKeyName();
            var service = new AgentService()
            {
                ID      = svcID,
                Service = svcID,
                Tags    = new[] { "master", "v1" },
                Port    = 8000
            };

            var registration = new CatalogRegistration()
            {
                Datacenter = "dc1",
                Node       = "foobar",
                Address    = "192.168.10.10",
                Service    = service
            };

            using (IConsulClient client = new ConsulClient())
            {
                await client.Catalog.Register(registration);

                var node = await client.Catalog.Node("foobar");

                Assert.Contains(svcID, node.Response.Services.Keys);
                Assert.False(node.Response.Services[svcID].EnableTagOverride);

                var services = await client.Catalog.Service(svcID);

                Assert.NotEmpty(services.Response);
                Assert.Equal(svcID, services.Response[0].ServiceName);

                Assert.False(services.Response[0].ServiceEnableTagOverride);
            }

            // Use a new scope
            using (IConsulClient client = new ConsulClient())
            {
                service.EnableTagOverride = true;

                await client.Catalog.Register(registration);

                var node = await client.Catalog.Node("foobar");

                Assert.Contains(svcID, node.Response.Services.Keys);
                Assert.True(node.Response.Services[svcID].EnableTagOverride);

                var services = await client.Catalog.Service(svcID);

                Assert.NotEmpty(services.Response);
                Assert.Equal(svcID, services.Response[0].ServiceName);

                Assert.True(services.Response[0].ServiceEnableTagOverride);
            }
        }
示例#15
0
        public async Task Agent_Checks()
        {
            var svcID        = KVTest.GenerateTestKeyName();
            var registration = new AgentCheckRegistration
            {
                Name = svcID,
                TTL  = TimeSpan.FromSeconds(15)
            };
            await _client.Agent.CheckRegister(registration);

            var checks = await _client.Agent.Checks();

            Assert.True(checks.Response.ContainsKey(svcID));
            Assert.Equal(HealthStatus.Critical, checks.Response[svcID].Status);

            await _client.Agent.CheckDeregister(svcID);
        }
示例#16
0
        public async Task Agent_CheckStartPassing()
        {
            var client       = new ConsulClient();
            var svcID        = KVTest.GenerateTestKeyName();
            var registration = new AgentCheckRegistration()
            {
                Name   = svcID,
                Status = CheckStatus.Passing,
                TTL    = TimeSpan.FromSeconds(15)
            };
            await client.Agent.CheckRegister(registration);

            var checks = await client.Agent.Checks();

            Assert.True(checks.Response.ContainsKey(svcID));
            Assert.Equal(CheckStatus.Passing, checks.Response[svcID].Status);

            await client.Agent.CheckDeregister(svcID);
        }
示例#17
0
        public async Task Agent_ServiceMaintenance()
        {
            var client     = new ConsulClient();
            var svcID      = KVTest.GenerateTestKeyName();
            var serviceReg = new AgentServiceRegistration()
            {
                Name = svcID
            };
            await client.Agent.ServiceRegister(serviceReg);

            await client.Agent.EnableServiceMaintenance(svcID, "broken");

            var checks = await client.Agent.Checks();

            var found = false;

            foreach (var check in checks.Response)
            {
                if (check.Value.CheckID.Contains("maintenance"))
                {
                    found = true;
                    Assert.Equal(CheckStatus.Critical, check.Value.Status);
                    Assert.Equal("broken", check.Value.Notes);
                }
            }
            Assert.True(found);

            await client.Agent.DisableServiceMaintenance(svcID);

            checks = await client.Agent.Checks();

            foreach (var check in checks.Response)
            {
                Assert.False(check.Value.CheckID.Contains("maintenance"));
            }

            await client.Agent.ServiceDeregister(svcID);
        }
示例#18
0
        public async Task Agent_Services_CheckTTLNote()
        {
            var client       = new ConsulClient();
            var svcID        = KVTest.GenerateTestKeyName();
            var registration = new AgentServiceRegistration()
            {
                Name  = svcID,
                Tags  = new[] { "bar", "baz" },
                Port  = 8000,
                Check = new AgentServiceCheck
                {
                    TTL    = TimeSpan.FromSeconds(15),
                    Status = CheckStatus.Critical
                }
            };

            await client.Agent.ServiceRegister(registration);

            var services = await client.Agent.Services();

            Assert.True(services.Response.ContainsKey(svcID));

            var checks = await client.Agent.Checks();

            Assert.True(checks.Response.ContainsKey("service:" + svcID));

            Assert.Equal(CheckStatus.Critical, checks.Response["service:" + svcID].Status);

            await client.Agent.PassTTL("service:" + svcID, "test is ok");

            checks = await client.Agent.Checks();

            Assert.True(checks.Response.ContainsKey("service:" + svcID));
            Assert.Equal(CheckStatus.Passing, checks.Response["service:" + svcID].Status);
            Assert.Equal("test is ok", checks.Response["service:" + svcID].Output);

            await client.Agent.ServiceDeregister(svcID);
        }
示例#19
0
        public async Task Agent_SetTTLStatus()
        {
            var svcID        = KVTest.GenerateTestKeyName();
            var registration = new AgentServiceRegistration
            {
                Name  = svcID,
                Check = new AgentServiceCheck
                {
                    TTL = TimeSpan.FromSeconds(15)
                }
            };
            await _client.Agent.ServiceRegister(registration);

            await _client.Agent.WarnTTL("service:" + svcID, "warning");

            var checks = await _client.Agent.Checks();

            Assert.Contains("service:" + svcID, checks.Response.Keys);
            Assert.Equal(HealthStatus.Warning, checks.Response["service:" + svcID].Status);
            Assert.Equal("warning", checks.Response["service:" + svcID].Output);

            await _client.Agent.PassTTL("service:" + svcID, "passing");

            checks = await _client.Agent.Checks();

            Assert.Contains("service:" + svcID, checks.Response.Keys);
            Assert.Equal(HealthStatus.Passing, checks.Response["service:" + svcID].Status);
            Assert.Equal("passing", checks.Response["service:" + svcID].Output);

            await _client.Agent.FailTTL("service:" + svcID, "failing");

            checks = await _client.Agent.Checks();

            Assert.Contains("service:" + svcID, checks.Response.Keys);
            Assert.Equal(HealthStatus.Critical, checks.Response["service:" + svcID].Status);
            Assert.Equal("failing", checks.Response["service:" + svcID].Output);

            await _client.Agent.UpdateTTL("service:" + svcID, svcID, TTLStatus.Pass);

            checks = await _client.Agent.Checks();

            Assert.Contains("service:" + svcID, checks.Response.Keys);
            Assert.Equal(HealthStatus.Passing, checks.Response["service:" + svcID].Status);
            Assert.Equal(svcID, checks.Response["service:" + svcID].Output);

            await _client.Agent.UpdateTTL("service:" + svcID, "foo warning", TTLStatus.Warn);

            checks = await _client.Agent.Checks();

            Assert.Contains("service:" + svcID, checks.Response.Keys);
            Assert.Equal(HealthStatus.Warning, checks.Response["service:" + svcID].Status);
            Assert.Equal("foo warning", checks.Response["service:" + svcID].Output);

            await _client.Agent.UpdateTTL("service:" + svcID, "foo failing", TTLStatus.Critical);

            checks = await _client.Agent.Checks();

            Assert.Contains("service:" + svcID, checks.Response.Keys);
            Assert.Equal(HealthStatus.Critical, checks.Response["service:" + svcID].Status);
            Assert.Equal("foo failing", checks.Response["service:" + svcID].Output);

            await _client.Agent.ServiceDeregister(svcID);
        }
示例#20
0
        public async Task Catalog_RegistrationDeregistration()
        {
            var svcID   = KVTest.GenerateTestKeyName();
            var service = new AgentService
            {
                ID      = svcID,
                Service = "redis",
                Tags    = new[] { "master", "v1" },
                Port    = 8000
            };

            var check = new AgentCheck
            {
                Node      = "foobar",
                CheckID   = "service:" + svcID,
                Name      = "Redis health check",
                Notes     = "Script based health check",
                Status    = HealthStatus.Passing,
                ServiceID = svcID
            };

            var registration = new CatalogRegistration
            {
                Datacenter = "dc1",
                Node       = "foobar",
                Address    = "192.168.10.10",
                Service    = service,
                Check      = check
            };

            await _client.Catalog.Register(registration);

            var node = await _client.Catalog.Node("foobar");

            Assert.True(node.Response.Services.ContainsKey(svcID));

            var health = await _client.Health.Node("foobar");

            Assert.Equal("service:" + svcID, health.Response[0].CheckID);

            var dereg = new CatalogDeregistration
            {
                Datacenter = "dc1",
                Node       = "foobar",
                Address    = "192.168.10.10",
                CheckID    = "service:" + svcID
            };

            await _client.Catalog.Deregister(dereg);

            health = await _client.Health.Node("foobar");

            Assert.Empty(health.Response);

            dereg = new CatalogDeregistration
            {
                Datacenter = "dc1",
                Node       = "foobar",
                Address    = "192.168.10.10"
            };

            await _client.Catalog.Deregister(dereg);

            node = await _client.Catalog.Node("foobar");

            Assert.Null(node.Response);
        }