Skip to content

Commit

Permalink
Make a copy of MachineInfo in GetMachineInfo()
Browse files Browse the repository at this point in the history
Signed-off-by: Ted Yu <yuzhihong@gmail.com>
  • Loading branch information
tedyu committed Apr 10, 2020
1 parent 49f4075 commit 8ac1498
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
20 changes: 14 additions & 6 deletions machine/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,12 @@ func TestTopologyWithoutNodes(t *testing.T) {
assert.Equal(t, 2, len(topology))
assert.Equal(t, 4, numCores)

topologyJSON, err := json.Marshal(topology)
topologyJSON1, err := json.Marshal(topology[0])
assert.Nil(t, err)
topologyJSON2, err := json.Marshal(topology[1])
assert.Nil(t, err)

expectedTopology := `[
expectedTopology1 := `
{
"node_id":0,
"memory":0,
Expand All @@ -277,7 +279,8 @@ func TestTopologyWithoutNodes(t *testing.T) {
}
],
"caches":null
},
}`
expectedTopology2 := `
{
"node_id":1,
"memory":0,
Expand All @@ -299,9 +302,14 @@ func TestTopologyWithoutNodes(t *testing.T) {
}
],
"caches":null
}
]`
assert.JSONEq(t, expectedTopology, string(topologyJSON))
}`

if expectedTopology1 == string(topologyJSON1) {
assert.JSONEq(t, expectedTopology2, string(topologyJSON2))
} else {
assert.JSONEq(t, expectedTopology2, string(topologyJSON1))
assert.JSONEq(t, expectedTopology1, string(topologyJSON2))
}
}

func TestTopologyWithNodesWithoutCPU(t *testing.T) {
Expand Down
22 changes: 21 additions & 1 deletion manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,27 @@ func (m *manager) GetMachineInfo() (*info.MachineInfo, error) {
m.machineMu.RLock()
defer m.machineMu.RUnlock()
// Copy and return the MachineInfo.
return &m.machineInfo, nil
copy := info.MachineInfo{
NumCores: m.machineInfo.NumCores,
NumPhysicalCores: m.machineInfo.NumPhysicalCores,
NumSockets: m.machineInfo.NumSockets,
CpuFrequency: m.machineInfo.CpuFrequency,
MemoryCapacity: m.machineInfo.MemoryCapacity,
MemoryByType: m.machineInfo.MemoryByType,
NVMInfo: m.machineInfo.NVMInfo,
HugePages: m.machineInfo.HugePages,
MachineID: m.machineInfo.MachineID,
SystemUUID: m.machineInfo.SystemUUID,
BootID: m.machineInfo.BootID,
Filesystems: m.machineInfo.Filesystems,
DiskMap: m.machineInfo.DiskMap,
NetworkDevices: m.machineInfo.NetworkDevices,
Topology: m.machineInfo.Topology,
CloudProvider: m.machineInfo.CloudProvider,
InstanceType: m.machineInfo.InstanceType,
InstanceID: m.machineInfo.InstanceID,
}
return &copy, nil
}

func (m *manager) GetVersionInfo() (*info.VersionInfo, error) {
Expand Down

0 comments on commit 8ac1498

Please sign in to comment.