diff --git a/command/agent/local.go b/command/agent/local.go index d0f6c0a47..ea1217f82 100644 --- a/command/agent/local.go +++ b/command/agent/local.go @@ -436,13 +436,11 @@ func (l *localState) setSyncState() error { } else { // Copy the existing check before potentially modifying // it before the compare operation. - eCopy := new(structs.HealthCheck) - *eCopy = *existing + eCopy := existing.Clone() // Copy the server's check before modifying, otherwise // in-memory RPC-based unit tests will have side effects. - cCopy := new(structs.HealthCheck) - *cCopy = *check + cCopy := check.Clone() // If there's a defer timer active then we've got a // potentially spammy check so we don't sync the output diff --git a/consul/structs/structs.go b/consul/structs/structs.go index d582560cd..1979b9b9e 100644 --- a/consul/structs/structs.go +++ b/consul/structs/structs.go @@ -396,6 +396,13 @@ func (c *HealthCheck) IsSame(other *HealthCheck) bool { return true } +// Clone returns a distinct clone of the HealthCheck. +func (c *HealthCheck) Clone() *HealthCheck { + clone := new(HealthCheck) + *clone = *c + return clone +} + type HealthChecks []*HealthCheck // CheckServiceNode is used to provide the node, its service diff --git a/consul/structs/structs_test.go b/consul/structs/structs_test.go index 6a7111688..d7672cad5 100644 --- a/consul/structs/structs_test.go +++ b/consul/structs/structs_test.go @@ -211,6 +211,28 @@ func TestStructs_HealthCheck_IsSame(t *testing.T) { check(&other.ServiceName) } +func TestStructs_HealthCheck_Clone(t *testing.T) { + hc := &HealthCheck{ + Node: "node1", + CheckID: "check1", + Name: "thecheck", + Status: HealthPassing, + Notes: "it's all good", + Output: "lgtm", + ServiceID: "service1", + ServiceName: "theservice", + } + clone := hc.Clone() + if !hc.IsSame(clone) { + t.Fatalf("should be equal to its clone") + } + + clone.Output = "different" + if hc.IsSame(clone) { + t.Fatalf("should not longer be equal to its clone") + } +} + func TestStructs_CheckServiceNodes_Shuffle(t *testing.T) { // Make a huge list of nodes. var nodes CheckServiceNodes