From 8ac14989061078084165fce775cc977227bf9e39 Mon Sep 17 00:00:00 2001 From: Ted Yu Date: Fri, 10 Apr 2020 09:21:18 -0700 Subject: [PATCH] Make a copy of MachineInfo in GetMachineInfo() Signed-off-by: Ted Yu --- machine/topology_test.go | 20 ++++++++++++++------ manager/manager.go | 22 +++++++++++++++++++++- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/machine/topology_test.go b/machine/topology_test.go index b38562af728..c660c165607 100644 --- a/machine/topology_test.go +++ b/machine/topology_test.go @@ -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, @@ -277,7 +279,8 @@ func TestTopologyWithoutNodes(t *testing.T) { } ], "caches":null - }, + }` + expectedTopology2 := ` { "node_id":1, "memory":0, @@ -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) { diff --git a/manager/manager.go b/manager/manager.go index ee3444d3bfa..abb7367e736 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -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 ©, nil } func (m *manager) GetVersionInfo() (*info.VersionInfo, error) {