From b7bbefd9b1d4c1032c6afece425a99ba17e43cdb Mon Sep 17 00:00:00 2001 From: Phillip Wittrock Date: Wed, 19 Aug 2015 17:27:25 -0700 Subject: [PATCH] Always to a live lookup of version info instead of caching. --- integration/framework/framework.go | 21 ++++++++++++-- integration/tests/api/attributes_test.go | 36 ++++++++++++++++++++++++ manager/manager.go | 16 +++++++---- 3 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 integration/tests/api/attributes_test.go diff --git a/integration/framework/framework.go b/integration/framework/framework.go index f2e80155..db1c31ab 100644 --- a/integration/framework/framework.go +++ b/integration/framework/framework.go @@ -24,6 +24,7 @@ import ( "time" "github.com/google/cadvisor/client" + "github.com/google/cadvisor/client/v2" "github.com/google/cadvisor/integration/common" ) @@ -123,12 +124,14 @@ type ShellActions interface { type CadvisorActions interface { // Returns a cAdvisor client to the machine being tested. Client() *client.Client + ClientV2() *v2.Client } type realFramework struct { - hostname HostnameInfo - t *testing.T - cadvisorClient *client.Client + hostname HostnameInfo + t *testing.T + cadvisorClient *client.Client + cadvisorClientV2 *v2.Client shellActions shellActions dockerActions dockerActions @@ -195,6 +198,18 @@ func (self *realFramework) Client() *client.Client { return self.cadvisorClient } +// Gets a v2 client to the cAdvisor being tested. +func (self *realFramework) ClientV2() *v2.Client { + if self.cadvisorClientV2 == nil { + cadvisorClientV2, err := v2.NewClient(self.Hostname().FullHostname()) + if err != nil { + self.t.Fatalf("Failed to instantiate the cAdvisor client: %v", err) + } + self.cadvisorClientV2 = cadvisorClientV2 + } + return self.cadvisorClientV2 +} + func (self dockerActions) RunPause() string { return self.Run(DockerRunArgs{ Image: "kubernetes/pause", diff --git a/integration/tests/api/attributes_test.go b/integration/tests/api/attributes_test.go new file mode 100644 index 00000000..aa4ccb9e --- /dev/null +++ b/integration/tests/api/attributes_test.go @@ -0,0 +1,36 @@ +// Copyright 2015 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. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package api + +import ( + "testing" + + "github.com/google/cadvisor/integration/framework" + "github.com/stretchr/testify/assert" +) + +func TestAttributeInformationIsReturned(t *testing.T) { + fm := framework.New(t) + defer fm.Cleanup() + + attributes, err := fm.Cadvisor().ClientV2().Attributes() + if err != nil { + t.Fatal(err) + } + + vp := `\d+\.\d+\.\d+` + assert.True(t, assert.Regexp(t, vp, attributes.DockerVersion), + "Expected %s to match %s", attributes.DockerVersion, vp) +} diff --git a/manager/manager.go b/manager/manager.go index 1c1680b3..df360263 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -161,8 +161,7 @@ func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingIn if err != nil { return nil, err } - newManager.versionInfo = *versionInfo - glog.Infof("Version: %+v", newManager.versionInfo) + glog.Infof("Version: %+v", *versionInfo) newManager.eventHandler = events.NewEventManager(parseEventsStoragePolicy()) return newManager, nil @@ -183,7 +182,6 @@ type manager struct { memoryCache *memory.InMemoryCache fsInfo fs.FsInfo machineInfo info.MachineInfo - versionInfo info.VersionInfo quitChannels []chan error cadvisorContainer string inHostNamespace bool @@ -658,7 +656,11 @@ func (m *manager) GetMachineInfo() (*info.MachineInfo, error) { } func (m *manager) GetVersionInfo() (*info.VersionInfo, error) { - return &m.versionInfo, nil + // TODO: Consider caching this and periodically updating. The VersionInfo may change if + // the docker daemon is started after the cAdvisor client is created. Caching the value + // would be helpful so we would be able to return the last known docker version if + // docker was down at the time of a query. + return getVersionInfo() } func (m *manager) Exists(containerName string) bool { @@ -1149,8 +1151,12 @@ func (m *manager) DockerInfo() (DockerStatus, error) { if err != nil { return DockerStatus{}, err } + versionInfo, err := m.GetVersionInfo() + if err != nil { + return DockerStatus{}, err + } out := DockerStatus{} - out.Version = m.versionInfo.DockerVersion + out.Version = versionInfo.DockerVersion if val, ok := info["KernelVersion"]; ok { out.KernelVersion = val }