2015-01-06 18:40:00 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-01-07 00:18:50 +00:00
|
|
|
"fmt"
|
2015-01-06 18:40:00 +00:00
|
|
|
"testing"
|
2015-01-07 00:18:50 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/testutil"
|
2017-04-29 16:34:02 +00:00
|
|
|
"github.com/hashicorp/consul/testutil/retry"
|
2017-04-27 23:03:05 +00:00
|
|
|
"github.com/pascaldekloe/goe/verify"
|
2015-01-06 18:40:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHealth_Node(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
health := c.Health()
|
|
|
|
|
|
|
|
info, err := agent.Self()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
name := info["Config"]["NodeName"].(string)
|
2017-04-29 16:34:02 +00:00
|
|
|
retry.Run("", t, func(r *retry.R) {
|
2015-01-13 19:25:19 +00:00
|
|
|
checks, meta, err := health.Node(name, nil)
|
|
|
|
if err != nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatal(err)
|
2015-01-13 19:25:19 +00:00
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %v", meta)
|
2015-01-13 19:25:19 +00:00
|
|
|
}
|
|
|
|
if len(checks) == 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %v", checks)
|
2015-01-13 19:25:19 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
2016-11-29 21:15:20 +00:00
|
|
|
func TestHealthChecks_AggregatedStatus(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
checks HealthChecks
|
|
|
|
exp string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"empty",
|
|
|
|
nil,
|
|
|
|
HealthPassing,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"passing",
|
|
|
|
HealthChecks{
|
|
|
|
&HealthCheck{
|
|
|
|
Status: HealthPassing,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
HealthPassing,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"warning",
|
|
|
|
HealthChecks{
|
|
|
|
&HealthCheck{
|
|
|
|
Status: HealthWarning,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
HealthWarning,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"critical",
|
|
|
|
HealthChecks{
|
|
|
|
&HealthCheck{
|
|
|
|
Status: HealthCritical,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
HealthCritical,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"node_maintenance",
|
|
|
|
HealthChecks{
|
|
|
|
&HealthCheck{
|
|
|
|
CheckID: NodeMaint,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
HealthMaint,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"service_maintenance",
|
|
|
|
HealthChecks{
|
|
|
|
&HealthCheck{
|
|
|
|
CheckID: ServiceMaintPrefix + "service",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
HealthMaint,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"unknown",
|
|
|
|
HealthChecks{
|
|
|
|
&HealthCheck{
|
|
|
|
Status: "nope-nope-noper",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"maintenance_over_critical",
|
|
|
|
HealthChecks{
|
|
|
|
&HealthCheck{
|
|
|
|
CheckID: NodeMaint,
|
|
|
|
},
|
|
|
|
&HealthCheck{
|
|
|
|
Status: HealthCritical,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
HealthMaint,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"critical_over_warning",
|
|
|
|
HealthChecks{
|
|
|
|
&HealthCheck{
|
|
|
|
Status: HealthCritical,
|
|
|
|
},
|
|
|
|
&HealthCheck{
|
|
|
|
Status: HealthWarning,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
HealthCritical,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"warning_over_passing",
|
|
|
|
HealthChecks{
|
|
|
|
&HealthCheck{
|
|
|
|
Status: HealthWarning,
|
|
|
|
},
|
|
|
|
&HealthCheck{
|
|
|
|
Status: HealthPassing,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
HealthWarning,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"lots",
|
|
|
|
HealthChecks{
|
|
|
|
&HealthCheck{
|
|
|
|
Status: HealthPassing,
|
|
|
|
},
|
|
|
|
&HealthCheck{
|
|
|
|
Status: HealthPassing,
|
|
|
|
},
|
|
|
|
&HealthCheck{
|
|
|
|
Status: HealthPassing,
|
|
|
|
},
|
|
|
|
&HealthCheck{
|
|
|
|
Status: HealthWarning,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
HealthWarning,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) {
|
|
|
|
act := tc.checks.AggregatedStatus()
|
|
|
|
if tc.exp != act {
|
|
|
|
t.Errorf("\nexp: %#v\nact: %#v", tc.exp, act)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
func TestHealth_Checks(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2017-04-27 23:03:05 +00:00
|
|
|
c, s := makeClientWithConfig(t, nil, func(conf *testutil.TestServerConfig) {
|
|
|
|
conf.NodeName = "node123"
|
|
|
|
})
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
agent := c.Agent()
|
|
|
|
health := c.Health()
|
|
|
|
|
|
|
|
// Make a service with a check
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
2017-04-27 23:03:05 +00:00
|
|
|
Tags: []string{"bar"},
|
2015-01-06 18:40:00 +00:00
|
|
|
Check: &AgentServiceCheck{
|
|
|
|
TTL: "15s",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer agent.ServiceDeregister("foo")
|
|
|
|
|
2017-04-29 16:34:02 +00:00
|
|
|
retry.Run("", t, func(r *retry.R) {
|
2017-04-27 23:03:05 +00:00
|
|
|
checks := HealthChecks{
|
|
|
|
&HealthCheck{
|
|
|
|
Node: "node123",
|
|
|
|
CheckID: "service:foo",
|
|
|
|
Name: "Service 'foo' check",
|
|
|
|
Status: "critical",
|
|
|
|
ServiceID: "foo",
|
|
|
|
ServiceName: "foo",
|
|
|
|
ServiceTags: []string{"bar"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
out, meta, err := health.Checks("foo", nil)
|
2015-01-07 00:18:50 +00:00
|
|
|
if err != nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatal(err)
|
2015-01-07 00:18:50 +00:00
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %v", meta)
|
2015-01-07 00:18:50 +00:00
|
|
|
}
|
2017-04-27 23:03:05 +00:00
|
|
|
if got, want := out, checks; !verify.Values(t, "checks", got, want) {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatal("health.Checks failed")
|
2015-01-07 00:18:50 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
2017-01-14 01:08:43 +00:00
|
|
|
func TestHealth_Checks_NodeMetaFilter(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
meta := map[string]string{"somekey": "somevalue"}
|
|
|
|
c, s := makeClientWithConfig(t, nil, func(conf *testutil.TestServerConfig) {
|
|
|
|
conf.NodeMeta = meta
|
|
|
|
})
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
health := c.Health()
|
|
|
|
|
|
|
|
// Make a service with a check
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
Check: &AgentServiceCheck{
|
|
|
|
TTL: "15s",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := agent.ServiceRegister(reg); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer agent.ServiceDeregister("foo")
|
|
|
|
|
2017-04-29 16:34:02 +00:00
|
|
|
retry.Run("", t, func(r *retry.R) {
|
2017-01-14 01:08:43 +00:00
|
|
|
checks, meta, err := health.Checks("foo", &QueryOptions{NodeMeta: meta})
|
|
|
|
if err != nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatal(err)
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %v", meta)
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
|
|
|
if len(checks) == 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("Bad: %v", checks)
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
func TestHealth_Service(t *testing.T) {
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
health := c.Health()
|
2017-04-29 16:34:02 +00:00
|
|
|
retry.Run("", t, func(r *retry.R) {
|
2015-01-07 00:48:54 +00:00
|
|
|
// consul service should always exist...
|
|
|
|
checks, meta, err := health.Service("consul", "", true, nil)
|
|
|
|
if err != nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatal(err)
|
2015-01-07 00:48:54 +00:00
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %v", meta)
|
2015-01-07 00:48:54 +00:00
|
|
|
}
|
|
|
|
if len(checks) == 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("Bad: %v", checks)
|
2015-01-07 00:48:54 +00:00
|
|
|
}
|
2016-08-16 17:30:30 +00:00
|
|
|
if _, ok := checks[0].Node.TaggedAddresses["wan"]; !ok {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("Bad: %v", checks[0].Node)
|
2016-08-16 17:30:30 +00:00
|
|
|
}
|
2017-04-18 12:02:24 +00:00
|
|
|
if checks[0].Node.Datacenter != "dc1" {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("Bad datacenter: %v", checks[0].Node)
|
2017-04-18 12:02:24 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
2017-01-14 01:08:43 +00:00
|
|
|
func TestHealth_Service_NodeMetaFilter(t *testing.T) {
|
|
|
|
meta := map[string]string{"somekey": "somevalue"}
|
|
|
|
c, s := makeClientWithConfig(t, nil, func(conf *testutil.TestServerConfig) {
|
|
|
|
conf.NodeMeta = meta
|
|
|
|
})
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
health := c.Health()
|
2017-04-29 16:34:02 +00:00
|
|
|
retry.Run("", t, func(r *retry.R) {
|
2017-01-14 01:08:43 +00:00
|
|
|
// consul service should always exist...
|
|
|
|
checks, meta, err := health.Service("consul", "", true, &QueryOptions{NodeMeta: meta})
|
|
|
|
if err != nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatal(err)
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %v", meta)
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
|
|
|
if len(checks) == 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("Bad: %v", checks)
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
|
|
|
if _, ok := checks[0].Node.TaggedAddresses["wan"]; !ok {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("Bad: %v", checks[0].Node)
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
2017-04-18 12:02:24 +00:00
|
|
|
if checks[0].Node.Datacenter != "dc1" {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("Bad datacenter: %v", checks[0].Node)
|
2017-04-18 12:02:24 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
func TestHealth_State(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
health := c.Health()
|
2017-04-29 16:34:02 +00:00
|
|
|
retry.Run("", t, func(r *retry.R) {
|
2015-01-07 00:48:54 +00:00
|
|
|
checks, meta, err := health.State("any", nil)
|
|
|
|
if err != nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatal(err)
|
2015-01-07 00:48:54 +00:00
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %v", meta)
|
2015-01-07 00:48:54 +00:00
|
|
|
}
|
|
|
|
if len(checks) == 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("Bad: %v", checks)
|
2015-01-07 00:48:54 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
2017-01-14 01:08:43 +00:00
|
|
|
|
|
|
|
func TestHealth_State_NodeMetaFilter(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
meta := map[string]string{"somekey": "somevalue"}
|
|
|
|
c, s := makeClientWithConfig(t, nil, func(conf *testutil.TestServerConfig) {
|
|
|
|
conf.NodeMeta = meta
|
|
|
|
})
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
health := c.Health()
|
2017-04-29 16:34:02 +00:00
|
|
|
retry.Run("", t, func(r *retry.R) {
|
2017-01-14 01:08:43 +00:00
|
|
|
checks, meta, err := health.State("any", &QueryOptions{NodeMeta: meta})
|
|
|
|
if err != nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatal(err)
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %v", meta)
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
|
|
|
if len(checks) == 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("Bad: %v", checks)
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|