2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2014-08-22 19:38:33 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2020-10-23 19:21:37 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/raft"
|
|
|
|
|
2017-11-29 02:01:17 +00:00
|
|
|
consulfsm "github.com/hashicorp/consul/agent/consul/fsm"
|
2020-10-23 19:21:37 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2014-08-22 19:38:33 +00:00
|
|
|
)
|
|
|
|
|
2017-11-29 02:01:17 +00:00
|
|
|
func makeLog(buf []byte) *raft.Log {
|
|
|
|
return &raft.Log{
|
|
|
|
Index: 1,
|
|
|
|
Term: 1,
|
|
|
|
Type: raft.LogCommand,
|
|
|
|
Data: buf,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 19:38:33 +00:00
|
|
|
// Testing for GH-300 and GH-279
|
|
|
|
func TestHealthCheckRace(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
2020-10-23 19:21:37 +00:00
|
|
|
fsm := consulfsm.NewFromDeps(consulfsm.Deps{
|
|
|
|
Logger: hclog.New(nil),
|
|
|
|
NewStateStore: func() *state.Store {
|
|
|
|
return state.NewStateStore(nil)
|
|
|
|
},
|
2023-04-04 16:30:06 +00:00
|
|
|
StorageBackend: consulfsm.NullStorageBackend,
|
2020-10-23 19:21:37 +00:00
|
|
|
})
|
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",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2014-08-22 19:38:33 +00:00
|
|
|
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
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
idx, out1, err := state.CheckServiceNodes(nil, "db", nil, "")
|
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
|
2017-04-19 23:00:11 +00:00
|
|
|
req.Check.Status = api.HealthCritical
|
2014-08-22 19:38:33 +00:00
|
|
|
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
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
idx, out2, err := state.CheckServiceNodes(nil, "db", nil, "")
|
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])
|
|
|
|
}
|
|
|
|
}
|