Skip to content

Commit

Permalink
Merge pull request #4984 from hashicorp/b-client-update-driver
Browse files Browse the repository at this point in the history
client: update driver info on new driver fingerprint
  • Loading branch information
notnoop committed Dec 11, 2018
2 parents 5f11000 + cae36e4 commit 926428f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
5 changes: 3 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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",
Expand All @@ -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 {
Expand All @@ -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()
}
Expand Down
77 changes: 77 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 926428f

Please sign in to comment.