Skip to content

Commit

Permalink
Merge pull request #2672 from odinuge/machine-info-backport-v0.35
Browse files Browse the repository at this point in the history
Cherry-pick #2490  Make a copy of MachineInfo in GetMachineInfo() to v0.35
  • Loading branch information
bobbypage authored Nov 5, 2020
2 parents 4903316 + 8ddb4e2 commit 92d49a1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
3 changes: 2 additions & 1 deletion manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,8 @@ func (m *manager) GetMachineInfo() (*info.MachineInfo, error) {
m.machineMu.RLock()
defer m.machineMu.RUnlock()
// Copy and return the MachineInfo.
return &m.machineInfo, nil
machineInfoCopy := m.machineInfo
return &machineInfoCopy, nil
}

func (m *manager) GetVersionInfo() (*info.VersionInfo, error) {
Expand Down
29 changes: 29 additions & 0 deletions manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,35 @@ func expectManagerWithContainersV2(containers []string, query *info.ContainerInf
return m, infosMap, handlerMap
}

func TestMachineInfoReturnValue(t *testing.T) {
containers := []string{
"/c1",
"/c2",
}

query := &info.ContainerInfoRequest{
NumStats: 256,
}

m, _, _ := expectManagerWithContainers(containers, query, t)

firstMachineInfo, err := m.GetMachineInfo()
if err != nil {
t.Fatalf("Unable to get machine info: %v", err)
}

secondMachineInfo, err := m.GetMachineInfo()
if err != nil {
t.Fatalf("Unable to get machine info: %v", err)
}

// Since the manager updates machine info in the background it should always return a copy
// to avoid race conditions at a later stage.
if firstMachineInfo == secondMachineInfo {
t.Fatalf("machineInfo should return a copy on each call")
}

}
func TestGetContainerInfo(t *testing.T) {
containers := []string{
"/c1",
Expand Down
2 changes: 1 addition & 1 deletion pages/static/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pages/templates.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 92d49a1

Please sign in to comment.