diff --git a/manager/manager.go b/manager/manager.go index 4855cd330d..00aa00b6f7 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -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) { diff --git a/manager/manager_test.go b/manager/manager_test.go index cdf00e1735..d79ffb600f 100644 --- a/manager/manager_test.go +++ b/manager/manager_test.go @@ -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", diff --git a/pages/static/assets.go b/pages/static/assets.go index 5c6f207081..fd94677441 100644 --- a/pages/static/assets.go +++ b/pages/static/assets.go @@ -1,4 +1,4 @@ -// Copyright 2019 Google Inc. All Rights Reserved. +// Copyright 2020 Google Inc. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/pages/templates.go b/pages/templates.go index 317dff2be5..a3dfcaf986 100644 --- a/pages/templates.go +++ b/pages/templates.go @@ -1,4 +1,4 @@ -// Copyright 2019 Google Inc. All Rights Reserved. +// Copyright 2020 Google Inc. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License.