2023-03-28 20:12:41 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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
|
|
|
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2018-06-07 09:17:44 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-01-06 18:40:00 +00:00
|
|
|
)
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_HealthNode(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-05-04 22:52:53 +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
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
func TestAPI_HealthNode_Filter(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
// this sets up the catalog entries with things we can filter on
|
|
|
|
testNodeServiceCheckRegistrations(t, c, "dc1")
|
|
|
|
|
|
|
|
health := c.Health()
|
|
|
|
|
|
|
|
// filter for just the redis service checks
|
|
|
|
checks, _, err := health.Node("foo", &QueryOptions{Filter: "ServiceName == redis"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, checks, 2)
|
|
|
|
|
|
|
|
// filter out service checks
|
|
|
|
checks, _, err = health.Node("foo", &QueryOptions{Filter: "ServiceID == ``"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, checks, 2)
|
|
|
|
}
|
|
|
|
|
2017-06-30 20:58:55 +00:00
|
|
|
func TestAPI_HealthChecks_AggregatedStatus(t *testing.T) {
|
2016-11-29 21:15:20 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_HealthChecks(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2023-03-20 20:18:59 +00:00
|
|
|
const nodename = "node123"
|
2017-04-27 23:03:05 +00:00
|
|
|
c, s := makeClientWithConfig(t, nil, func(conf *testutil.TestServerConfig) {
|
2023-03-20 20:18:59 +00:00
|
|
|
conf.NodeName = nodename
|
2017-04-27 23:03:05 +00:00
|
|
|
})
|
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)
|
|
|
|
}
|
|
|
|
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-04-27 23:03:05 +00:00
|
|
|
checks := HealthChecks{
|
|
|
|
&HealthCheck{
|
2023-03-20 20:18:59 +00:00
|
|
|
Node: nodename,
|
2017-04-27 23:03:05 +00:00
|
|
|
CheckID: "service:foo",
|
|
|
|
Name: "Service 'foo' check",
|
|
|
|
Status: "critical",
|
|
|
|
ServiceID: "foo",
|
|
|
|
ServiceName: "foo",
|
|
|
|
ServiceTags: []string{"bar"},
|
2019-10-17 18:33:11 +00:00
|
|
|
Type: "ttl",
|
2022-08-31 21:15:32 +00:00
|
|
|
Partition: defaultPartition,
|
|
|
|
Namespace: defaultNamespace,
|
2017-04-27 23:03:05 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2018-12-12 17:14:02 +00:00
|
|
|
checks[0].CreateIndex = out[0].CreateIndex
|
|
|
|
checks[0].ModifyIndex = out[0].ModifyIndex
|
2019-03-29 15:29:27 +00:00
|
|
|
require.Equal(r, checks, out)
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_HealthChecks_NodeMetaFilter(t *testing.T) {
|
2017-01-14 01:08:43 +00:00
|
|
|
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()
|
|
|
|
|
2019-02-22 15:38:39 +00:00
|
|
|
s.WaitForSerfCheck(t)
|
|
|
|
|
2017-01-14 01:08:43 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2017-05-04 22:52:53 +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
|
|
|
}
|
2019-10-17 18:33:11 +00:00
|
|
|
if len(checks) != 1 {
|
|
|
|
r.Fatalf("expected 1 check, got %d", len(checks))
|
|
|
|
}
|
|
|
|
if checks[0].Type != "ttl" {
|
|
|
|
r.Fatalf("expected type ttl, got %s", checks[0].Type)
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
func TestAPI_HealthChecks_Filter(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
// this sets up the catalog entries with things we can filter on
|
|
|
|
testNodeServiceCheckRegistrations(t, c, "dc1")
|
|
|
|
|
|
|
|
health := c.Health()
|
|
|
|
|
|
|
|
checks, _, err := health.Checks("redis", &QueryOptions{Filter: "Node == foo"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
// 1 service check for each instance
|
|
|
|
require.Len(t, checks, 2)
|
|
|
|
|
|
|
|
checks, _, err = health.Checks("redis", &QueryOptions{Filter: "Node == bar"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
// 1 service check for each instance
|
|
|
|
require.Len(t, checks, 1)
|
|
|
|
|
|
|
|
checks, _, err = health.Checks("redis", &QueryOptions{Filter: "Node == foo and v1 in ServiceTags"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
// 1 service check for the matching instance
|
|
|
|
require.Len(t, checks, 1)
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_HealthService(t *testing.T) {
|
2017-09-25 18:40:42 +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-05-04 22:52:53 +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
|
|
|
}
|
|
|
|
|
2018-10-11 11:50:05 +00:00
|
|
|
func TestAPI_HealthService_SingleTag(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClientWithConfig(t, nil, func(conf *testutil.TestServerConfig) {
|
|
|
|
conf.NodeName = "node123"
|
|
|
|
})
|
|
|
|
defer s.Stop()
|
|
|
|
agent := c.Agent()
|
|
|
|
health := c.Health()
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
ID: "foo1",
|
|
|
|
Tags: []string{"bar"},
|
|
|
|
Check: &AgentServiceCheck{
|
|
|
|
Status: HealthPassing,
|
|
|
|
TTL: "15s",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.NoError(t, agent.ServiceRegister(reg))
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
services, meta, err := health.Service("foo", "bar", true, nil)
|
2019-07-12 18:57:41 +00:00
|
|
|
require.NoError(r, err)
|
|
|
|
require.NotEqual(r, meta.LastIndex, 0)
|
|
|
|
require.Len(r, services, 1)
|
|
|
|
require.Equal(r, services[0].Service.ID, "foo1")
|
2019-10-17 18:33:11 +00:00
|
|
|
|
|
|
|
for _, check := range services[0].Checks {
|
|
|
|
if check.CheckID == "service:foo1" && check.Type != "ttl" {
|
|
|
|
r.Fatalf("expected type ttl, got %s", check.Type)
|
|
|
|
}
|
|
|
|
}
|
2018-10-11 11:50:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
func TestAPI_HealthService_MultipleTags(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClientWithConfig(t, nil, func(conf *testutil.TestServerConfig) {
|
|
|
|
conf.NodeName = "node123"
|
|
|
|
})
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
health := c.Health()
|
|
|
|
|
|
|
|
// Make two services with a check
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
ID: "foo1",
|
|
|
|
Tags: []string{"bar"},
|
|
|
|
Check: &AgentServiceCheck{
|
|
|
|
Status: HealthPassing,
|
|
|
|
TTL: "15s",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.NoError(t, agent.ServiceRegister(reg))
|
|
|
|
|
|
|
|
reg2 := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
ID: "foo2",
|
|
|
|
Tags: []string{"bar", "v2"},
|
|
|
|
Check: &AgentServiceCheck{
|
|
|
|
Status: HealthPassing,
|
|
|
|
TTL: "15s",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.NoError(t, agent.ServiceRegister(reg2))
|
|
|
|
|
|
|
|
// Test searching with one tag (two results)
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
services, meta, err := health.ServiceMultipleTags("foo", []string{"bar"}, true, nil)
|
|
|
|
|
2019-07-12 18:57:41 +00:00
|
|
|
require.NoError(r, err)
|
|
|
|
require.NotEqual(r, meta.LastIndex, 0)
|
|
|
|
require.Len(r, services, 2)
|
2018-10-11 11:50:05 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Test searching with two tags (one result)
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
services, meta, err := health.ServiceMultipleTags("foo", []string{"bar", "v2"}, true, nil)
|
|
|
|
|
2019-07-12 18:57:41 +00:00
|
|
|
require.NoError(r, err)
|
|
|
|
require.NotEqual(r, meta.LastIndex, 0)
|
|
|
|
require.Len(r, services, 1)
|
|
|
|
require.Equal(r, services[0].Service.ID, "foo2")
|
2018-10-11 11:50:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPI_HealthService_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()
|
|
|
|
|
2019-07-12 15:37:29 +00:00
|
|
|
s.WaitForSerfCheck(t)
|
|
|
|
|
2018-10-11 11:50:05 +00:00
|
|
|
health := c.Health()
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
// consul service should always exist...
|
|
|
|
checks, meta, err := health.Service("consul", "", true, &QueryOptions{NodeMeta: meta})
|
2019-07-12 18:57:41 +00:00
|
|
|
require.NoError(r, err)
|
|
|
|
require.NotEqual(r, meta.LastIndex, 0)
|
|
|
|
require.NotEqual(r, len(checks), 0)
|
|
|
|
require.Equal(r, checks[0].Node.Datacenter, "dc1")
|
|
|
|
require.Contains(r, checks[0].Node.TaggedAddresses, "wan")
|
2018-10-11 11:50:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
func TestAPI_HealthService_Filter(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
// this sets up the catalog entries with things we can filter on
|
|
|
|
testNodeServiceCheckRegistrations(t, c, "dc1")
|
|
|
|
|
|
|
|
health := c.Health()
|
|
|
|
|
|
|
|
services, _, err := health.Service("redis", "", false, &QueryOptions{Filter: "Service.Meta.version == 2"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, services, 1)
|
|
|
|
|
|
|
|
services, _, err = health.Service("web", "", false, &QueryOptions{Filter: "Node.Meta.os == linux"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, services, 2)
|
|
|
|
require.Equal(t, "baz", services[0].Node.Node)
|
|
|
|
require.Equal(t, "baz", services[1].Node.Node)
|
|
|
|
|
|
|
|
services, _, err = health.Service("web", "", false, &QueryOptions{Filter: "Node.Meta.os == linux and Service.Meta.version == 1"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, services, 1)
|
|
|
|
}
|
|
|
|
|
2018-03-26 15:51:43 +00:00
|
|
|
func TestAPI_HealthConnect(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
health := c.Health()
|
|
|
|
|
2019-02-22 15:38:39 +00:00
|
|
|
s.WaitForSerfCheck(t)
|
|
|
|
|
2018-03-26 15:51:43 +00:00
|
|
|
// Make a service with a proxy
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
err := agent.ServiceRegister(reg)
|
2018-06-07 09:17:44 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-26 15:51:43 +00:00
|
|
|
|
|
|
|
// Register the proxy
|
|
|
|
proxyReg := &AgentServiceRegistration{
|
2018-09-12 16:07:47 +00:00
|
|
|
Name: "foo-proxy",
|
|
|
|
Port: 8001,
|
|
|
|
Kind: ServiceKindConnectProxy,
|
|
|
|
Proxy: &AgentServiceConnectProxyConfig{
|
|
|
|
DestinationServiceName: "foo",
|
|
|
|
},
|
2018-03-26 15:51:43 +00:00
|
|
|
}
|
|
|
|
err = agent.ServiceRegister(proxyReg)
|
2018-06-07 09:17:44 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-26 15:51:43 +00:00
|
|
|
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
services, meta, err := health.Connect("foo", "", true, nil)
|
|
|
|
if err != nil {
|
|
|
|
r.Fatal(err)
|
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
|
|
|
r.Fatalf("bad: %v", meta)
|
|
|
|
}
|
|
|
|
// Should be exactly 1 service - the original shouldn't show up as a connect
|
|
|
|
// endpoint, only it's proxy.
|
|
|
|
if len(services) != 1 {
|
|
|
|
r.Fatalf("Bad: %v", services)
|
|
|
|
}
|
|
|
|
if services[0].Node.Datacenter != "dc1" {
|
|
|
|
r.Fatalf("Bad datacenter: %v", services[0].Node)
|
|
|
|
}
|
|
|
|
if services[0].Service.Port != proxyReg.Port {
|
|
|
|
r.Fatalf("Bad port: %v", services[0])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
func TestAPI_HealthConnect_Filter(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
// this sets up the catalog entries with things we can filter on
|
|
|
|
testNodeServiceCheckRegistrations(t, c, "dc1")
|
|
|
|
|
|
|
|
health := c.Health()
|
|
|
|
|
|
|
|
services, _, err := health.Connect("web", "", false, &QueryOptions{Filter: "Node.Meta.os == linux"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, services, 2)
|
|
|
|
require.Equal(t, "baz", services[0].Node.Node)
|
|
|
|
require.Equal(t, "baz", services[1].Node.Node)
|
|
|
|
|
|
|
|
services, _, err = health.Service("web", "", false, &QueryOptions{Filter: "Node.Meta.os == linux and Service.Meta.version == 1"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, services, 1)
|
|
|
|
}
|
|
|
|
|
2020-06-10 18:07:15 +00:00
|
|
|
func TestAPI_HealthIngress(t *testing.T) {
|
2020-06-09 17:09:59 +00:00
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
agent := c.Agent()
|
|
|
|
health := c.Health()
|
|
|
|
|
|
|
|
s.WaitForSerfCheck(t)
|
|
|
|
|
|
|
|
// Make a service with a proxy
|
|
|
|
reg := &AgentServiceRegistration{
|
|
|
|
Name: "foo",
|
|
|
|
Port: 8000,
|
|
|
|
}
|
|
|
|
err := agent.ServiceRegister(reg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Register the gateway
|
|
|
|
gatewayReg := &AgentServiceRegistration{
|
|
|
|
Name: "foo-gateway",
|
|
|
|
Port: 8001,
|
|
|
|
Kind: ServiceKindIngressGateway,
|
|
|
|
}
|
|
|
|
err = agent.ServiceRegister(gatewayReg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Associate service and gateway
|
|
|
|
gatewayConfig := &IngressGatewayConfigEntry{
|
|
|
|
Kind: IngressGateway,
|
|
|
|
Name: "foo-gateway",
|
|
|
|
Listeners: []IngressListener{
|
|
|
|
{
|
|
|
|
Port: 2222,
|
|
|
|
Protocol: "tcp",
|
|
|
|
Services: []IngressService{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, wm, err := c.ConfigEntries().Set(gatewayConfig, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, wm)
|
|
|
|
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
services, meta, err := health.Ingress("foo", true, nil)
|
|
|
|
require.NoError(r, err)
|
|
|
|
|
|
|
|
require.NotZero(r, meta.LastIndex)
|
|
|
|
|
|
|
|
// Should be exactly 1 service - the original shouldn't show up as a connect
|
|
|
|
// endpoint, only it's proxy.
|
|
|
|
require.Len(r, services, 1)
|
|
|
|
require.Equal(r, services[0].Node.Datacenter, "dc1")
|
|
|
|
require.Equal(r, services[0].Service.Service, gatewayReg.Name)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_HealthState(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-05-04 22:52:53 +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
|
|
|
|
2017-06-30 21:05:02 +00:00
|
|
|
func TestAPI_HealthState_NodeMetaFilter(t *testing.T) {
|
2017-01-14 01:08:43 +00:00
|
|
|
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-05-04 22:52:53 +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
|
|
|
}
|
2019-04-16 16:00:15 +00:00
|
|
|
|
|
|
|
func TestAPI_HealthState_Filter(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
// this sets up the catalog entries with things we can filter on
|
|
|
|
testNodeServiceCheckRegistrations(t, c, "dc1")
|
|
|
|
|
|
|
|
health := c.Health()
|
|
|
|
|
|
|
|
checks, _, err := health.State(HealthAny, &QueryOptions{Filter: "Node == baz"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, checks, 6)
|
|
|
|
|
|
|
|
checks, _, err = health.State(HealthAny, &QueryOptions{Filter: "Status == warning or Status == critical"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, checks, 2)
|
|
|
|
|
|
|
|
checks, _, err = health.State(HealthCritical, &QueryOptions{Filter: "Node == baz"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, checks, 1)
|
|
|
|
|
|
|
|
checks, _, err = health.State(HealthWarning, &QueryOptions{Filter: "Node == baz"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, checks, 1)
|
|
|
|
}
|