Merge pull request #4984 from hashicorp/b-client-update-driver

client: update driver info on new driver fingerprint
This commit is contained in:
Mahmood Ali 2018-12-11 18:01:03 -05:00 committed by GitHub
commit 3d166e6e9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 2 deletions

View File

@ -1156,7 +1156,6 @@ func (c *Client) updateNodeFromDriver(name string, info *structs.DriverInfo) *st
if !hadDriver {
// If the driver info has not yet been set, do that here
hasChanged = true
c.config.Node.Drivers[name] = info
for attrName, newVal := range info.Attributes {
c.config.Node.Attributes[attrName] = newVal
}
@ -1165,11 +1164,11 @@ func (c *Client) updateNodeFromDriver(name string, info *structs.DriverInfo) *st
// The driver info has already been set, fix it up
if oldVal.Detected != info.Detected {
hasChanged = true
c.config.Node.Drivers[name].Detected = info.Detected
}
if oldVal.Healthy != info.Healthy || oldVal.HealthDescription != info.HealthDescription {
hasChanged = true
if info.HealthDescription != "" {
event := &structs.NodeEvent{
Subsystem: "Driver",
@ -1188,6 +1187,7 @@ func (c *Client) updateNodeFromDriver(name string, info *structs.DriverInfo) *st
}
hasChanged = true
if newVal == "" {
delete(c.config.Node.Attributes, attrName)
} else {
@ -1207,6 +1207,7 @@ func (c *Client) updateNodeFromDriver(name string, info *structs.DriverInfo) *st
}
if hasChanged {
c.config.Node.Drivers[name] = info
c.config.Node.Drivers[name].UpdateTime = time.Now()
c.updateNodeLocked()
}

View File

@ -1183,3 +1183,80 @@ func TestClient_computeAllocatedDeviceStats(t *testing.T) {
assert.EqualValues(t, expected, result)
}
func TestClient_updateNodeFromDriverUpdatesAll(t *testing.T) {
t.Parallel()
client, cleanup := TestClient(t, nil)
defer cleanup()
// initial update
{
info := &structs.DriverInfo{
Detected: true,
Healthy: false,
HealthDescription: "not healthy at start",
Attributes: map[string]string{
"node.mock.testattr1": "val1",
},
}
n := client.updateNodeFromDriver("mock", info)
updatedInfo := *n.Drivers["mock"]
// compare without update time
updatedInfo.UpdateTime = info.UpdateTime
assert.EqualValues(t, updatedInfo, *info)
// check node attributes
assert.Equal(t, "val1", n.Attributes["node.mock.testattr1"])
}
// initial update
{
info := &structs.DriverInfo{
Detected: true,
Healthy: true,
HealthDescription: "healthy",
Attributes: map[string]string{
"node.mock.testattr1": "val2",
},
}
n := client.updateNodeFromDriver("mock", info)
updatedInfo := *n.Drivers["mock"]
// compare without update time
updatedInfo.UpdateTime = info.UpdateTime
assert.EqualValues(t, updatedInfo, *info)
// check node attributes are updated
assert.Equal(t, "val2", n.Attributes["node.mock.testattr1"])
// update once more with the same info, updateTime shouldn't change
un := client.updateNodeFromDriver("mock", info)
assert.EqualValues(t, n, un)
}
// update once more to unhealthy because why not
{
info := &structs.DriverInfo{
Detected: true,
Healthy: false,
HealthDescription: "lost track",
Attributes: map[string]string{
"node.mock.testattr1": "",
},
}
n := client.updateNodeFromDriver("mock", info)
updatedInfo := *n.Drivers["mock"]
// compare without update time
updatedInfo.UpdateTime = info.UpdateTime
assert.EqualValues(t, updatedInfo, *info)
// check node attributes are updated
assert.Equal(t, "", n.Attributes["node.mock.testattr1"])
// update once more with the same info, updateTime shouldn't change
un := client.updateNodeFromDriver("mock", info)
assert.EqualValues(t, n, un)
}
}