2014-08-22 19:38:33 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Testing for GH-300 and GH-279
|
|
|
|
func TestHealthCheckRace(t *testing.T) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
2014-10-15 22:03:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-13 05:21:39 +00:00
|
|
|
state := fsm.State()
|
2014-08-22 19:38:33 +00:00
|
|
|
|
|
|
|
req := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "db",
|
|
|
|
Service: "db",
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "db connectivity",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "db",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.RegisterRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log := makeLog(buf)
|
|
|
|
log.Index = 10
|
|
|
|
resp := fsm.Apply(log)
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the index
|
2017-01-24 07:37:21 +00:00
|
|
|
idx, out1, err := state.CheckServiceNodes(nil, "db")
|
2015-10-12 22:34:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-08-22 19:38:33 +00:00
|
|
|
if idx != 10 {
|
2015-10-12 22:34:32 +00:00
|
|
|
t.Fatalf("Bad index: %d", idx)
|
2014-08-22 19:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the check state
|
|
|
|
req.Check.Status = structs.HealthCritical
|
|
|
|
buf, err = structs.Encode(structs.RegisterRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log = makeLog(buf)
|
|
|
|
log.Index = 20
|
|
|
|
resp = fsm.Apply(log)
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the index changed
|
2017-01-24 07:37:21 +00:00
|
|
|
idx, out2, err := state.CheckServiceNodes(nil, "db")
|
2015-10-12 22:34:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-08-22 19:38:33 +00:00
|
|
|
if idx != 20 {
|
2015-10-12 22:34:32 +00:00
|
|
|
t.Fatalf("Bad index: %d", idx)
|
2014-08-22 19:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if reflect.DeepEqual(out1, out2) {
|
|
|
|
t.Fatalf("match: %#v %#v", *out1[0].Checks[0], *out2[0].Checks[0])
|
|
|
|
}
|
|
|
|
}
|