Re-add logic to handle the undocumented duration fields

This commit is contained in:
Kyle Havlovitz 2019-03-26 10:44:02 -07:00
parent 3f5e20452e
commit 716a20d8a6
No known key found for this signature in database
GPG Key ID: 8A5E6B173056AD6C
2 changed files with 81 additions and 19 deletions

View File

@ -221,6 +221,24 @@ func (s *HTTPServer) convertOps(resp http.ResponseWriter, req *http.Request) (st
} }
check := in.Check.Check check := in.Check.Check
// Check if the internal duration fields are set as well as the normal ones. This is
// to be backwards compatible with a bug where the internal duration fields were being
// deserialized from instead of the correct fields.
// See https://github.com/hashicorp/consul/issues/5477 for more details.
interval := check.Definition.IntervalDuration
if dur := time.Duration(check.Definition.Interval); dur != 0 {
interval = dur
}
timeout := check.Definition.TimeoutDuration
if dur := time.Duration(check.Definition.Timeout); dur != 0 {
timeout = dur
}
deregisterCriticalServiceAfter := check.Definition.DeregisterCriticalServiceAfterDuration
if dur := time.Duration(check.Definition.DeregisterCriticalServiceAfter); dur != 0 {
deregisterCriticalServiceAfter = dur
}
out := &structs.TxnOp{ out := &structs.TxnOp{
Check: &structs.TxnCheckOp{ Check: &structs.TxnCheckOp{
Verb: in.Check.Verb, Verb: in.Check.Verb,
@ -240,9 +258,9 @@ func (s *HTTPServer) convertOps(resp http.ResponseWriter, req *http.Request) (st
Header: check.Definition.Header, Header: check.Definition.Header,
Method: check.Definition.Method, Method: check.Definition.Method,
TCP: check.Definition.TCP, TCP: check.Definition.TCP,
Interval: time.Duration(check.Definition.Interval), Interval: interval,
Timeout: time.Duration(check.Definition.Timeout), Timeout: timeout,
DeregisterCriticalServiceAfter: time.Duration(check.Definition.DeregisterCriticalServiceAfter), DeregisterCriticalServiceAfter: deregisterCriticalServiceAfter,
}, },
RaftIndex: structs.RaftIndex{ RaftIndex: structs.RaftIndex{
ModifyIndex: check.ModifyIndex, ModifyIndex: check.ModifyIndex,

View File

@ -410,7 +410,8 @@ func TestTxnEndpoint_UpdateCheck(t *testing.T) {
defer a.Shutdown() defer a.Shutdown()
testrpc.WaitForTestAgent(t, a.RPC, "dc1") testrpc.WaitForTestAgent(t, a.RPC, "dc1")
// Make sure the fields of a check are handled correctly when both creating and updating. // Make sure the fields of a check are handled correctly when both creating and
// updating, and test both sets of duration fields to ensure backwards compatibility.
buf := bytes.NewBuffer([]byte(fmt.Sprintf(` buf := bytes.NewBuffer([]byte(fmt.Sprintf(`
[ [
{ {
@ -456,9 +457,31 @@ func TestTxnEndpoint_UpdateCheck(t *testing.T) {
} }
} }
} }
},
{
"Check": {
"Verb": "set",
"Check": {
"Node": "%s",
"CheckID": "nodecheck",
"Name": "Node http check",
"Status": "passing",
"Notes": "Http based health check",
"Output": "success",
"ServiceID": "",
"ServiceName": "",
"Definition": {
"IntervalDuration": "15s",
"TimeoutDuration": "15s",
"DeregisterCriticalServiceAfterDuration": "30m",
"HTTP": "http://localhost:9000",
"TLSSkipVerify": false
}
}
}
} }
] ]
`, a.config.NodeName, a.config.NodeName))) `, a.config.NodeName, a.config.NodeName, a.config.NodeName)))
req, _ := http.NewRequest("PUT", "/v1/txn", buf) req, _ := http.NewRequest("PUT", "/v1/txn", buf)
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
obj, err := a.srv.Txn(resp, req) obj, err := a.srv.Txn(resp, req)
@ -473,7 +496,7 @@ func TestTxnEndpoint_UpdateCheck(t *testing.T) {
if !ok { if !ok {
t.Fatalf("bad type: %T", obj) t.Fatalf("bad type: %T", obj)
} }
if len(txnResp.Results) != 2 { if len(txnResp.Results) != 3 {
t.Fatalf("bad: %v", txnResp) t.Fatalf("bad: %v", txnResp)
} }
index := txnResp.Results[0].Check.ModifyIndex index := txnResp.Results[0].Check.ModifyIndex
@ -520,6 +543,27 @@ func TestTxnEndpoint_UpdateCheck(t *testing.T) {
}, },
}, },
}, },
&structs.TxnResult{
Check: &structs.HealthCheck{
Node: a.config.NodeName,
CheckID: "nodecheck",
Name: "Node http check",
Status: api.HealthPassing,
Notes: "Http based health check",
Output: "success",
Definition: structs.HealthCheckDefinition{
Interval: 15 * time.Second,
Timeout: 15 * time.Second,
DeregisterCriticalServiceAfter: 30 * time.Minute,
HTTP: "http://localhost:9000",
TLSSkipVerify: false,
},
RaftIndex: structs.RaftIndex{
CreateIndex: index,
ModifyIndex: index,
},
},
},
}, },
} }
verify.Values(t, "", txnResp, expected) verify.Values(t, "", txnResp, expected)