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"
|
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)
|
|
|
|
|
2015-01-13 19:25:19 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
checks, meta, err := health.Node(name, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
|
|
|
return false, fmt.Errorf("bad: %v", meta)
|
|
|
|
}
|
|
|
|
if len(checks) == 0 {
|
|
|
|
return false, fmt.Errorf("bad: %v", checks)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
})
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHealth_Checks(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()
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
|
2015-01-07 00:18:50 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
checks, meta, err := health.Checks("foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
|
|
|
return false, fmt.Errorf("bad: %v", meta)
|
|
|
|
}
|
|
|
|
if len(checks) == 0 {
|
|
|
|
return false, fmt.Errorf("Bad: %v", checks)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
})
|
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()
|
|
|
|
|
2015-01-07 00:48:54 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
// consul service should always exist...
|
|
|
|
checks, meta, err := health.Service("consul", "", true, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
|
|
|
return false, fmt.Errorf("bad: %v", meta)
|
|
|
|
}
|
|
|
|
if len(checks) == 0 {
|
|
|
|
return false, fmt.Errorf("Bad: %v", checks)
|
|
|
|
}
|
2016-08-16 17:30:30 +00:00
|
|
|
if _, ok := checks[0].Node.TaggedAddresses["wan"]; !ok {
|
2016-10-31 16:59:20 +00:00
|
|
|
return false, fmt.Errorf("Bad: %v", checks[0].Node)
|
2016-08-16 17:30:30 +00:00
|
|
|
}
|
2015-01-07 00:48:54 +00:00
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
})
|
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()
|
|
|
|
|
2015-01-07 00:48:54 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
checks, meta, err := health.State("any", nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if meta.LastIndex == 0 {
|
|
|
|
return false, fmt.Errorf("bad: %v", meta)
|
|
|
|
}
|
|
|
|
if len(checks) == 0 {
|
|
|
|
return false, fmt.Errorf("Bad: %v", checks)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
})
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|