From 7dd56a95857b4de22a25f2a052d8008c268eb22b Mon Sep 17 00:00:00 2001 From: Benjamin Gandon Date: Wed, 8 Sep 2021 08:59:59 +0200 Subject: [PATCH 1/2] Have vitals check for mount points (fixes #245) --- agent/bootstrap_test.go | 2 +- platform/dummy_platform.go | 2 +- platform/linux_platform_test.go | 3 +- platform/provider.go | 2 +- platform/vitals/service.go | 21 ++++- platform/vitals/service_test.go | 157 +++++++++++++++++++------------- platform/windows_platform.go | 2 +- 7 files changed, 121 insertions(+), 68 deletions(-) diff --git a/agent/bootstrap_test.go b/agent/bootstrap_test.go index 7e6c53e4e..393af3f25 100644 --- a/agent/bootstrap_test.go +++ b/agent/bootstrap_test.go @@ -1116,7 +1116,7 @@ var _ = Describe("bootstrap", func() { sigarCollector := boshsigar.NewSigarStatsCollector(&sigar.ConcreteSigar{}) - vitalsService := boshvitals.NewService(sigarCollector, dirProvider) + vitalsService := boshvitals.NewService(sigarCollector, dirProvider, mounter) ipResolver := boship.NewResolver(boship.NetworkInterfaceToAddrsFunc) diff --git a/platform/dummy_platform.go b/platform/dummy_platform.go index 024e70da1..0c1bfa809 100644 --- a/platform/dummy_platform.go +++ b/platform/dummy_platform.go @@ -69,7 +69,7 @@ func NewDummyPlatform( copier: boshcmd.NewGenericCpCopier(fs, logger), dirProvider: dirProvider, devicePathResolver: devicePathResolver, - vitalsService: boshvitals.NewService(collector, dirProvider), + vitalsService: boshvitals.NewService(collector, dirProvider, nil), certManager: boshcert.NewDummyCertManager(fs, cmdRunner, 0, logger), logger: logger, auditLogger: auditLogger, diff --git a/platform/linux_platform_test.go b/platform/linux_platform_test.go index 63aa92403..1ed8f5cf0 100644 --- a/platform/linux_platform_test.go +++ b/platform/linux_platform_test.go @@ -80,7 +80,6 @@ var _ = Describe("LinuxPlatform", func() { cdutil = fakecdrom.NewFakeCDUtil() compressor = boshcmd.NewTarballCompressor(cmdRunner, fs) copier = boshcmd.NewGenericCpCopier(fs, logger) - vitalsService = boshvitals.NewService(collector, dirProvider) netManager = &fakenet.FakeManager{} certManager = new(certfakes.FakeManager) monitRetryStrategy = fakeretry.NewFakeRetryStrategy() @@ -122,6 +121,8 @@ var _ = Describe("LinuxPlatform", func() { diskUtil = fakedisk.NewFakeDiskUtil() diskManager.GetUtilReturns(diskUtil) + + vitalsService = boshvitals.NewService(collector, dirProvider, mounter) }) JustBeforeEach(func() { diff --git a/platform/provider.go b/platform/provider.go index 3fba61f13..9889d8f80 100644 --- a/platform/provider.go +++ b/platform/provider.go @@ -72,7 +72,7 @@ func NewProvider(logger boshlog.Logger, dirProvider boshdirs.Provider, statsColl // Kick of stats collection as soon as possible statsCollector.StartCollecting(SigarStatsCollectionInterval, nil) - vitalsService := boshvitals.NewService(statsCollector, dirProvider) + vitalsService := boshvitals.NewService(statsCollector, dirProvider, linuxDiskManager.GetMounter()) ipResolver := boship.NewResolver(boship.NetworkInterfaceToAddrsFunc) diff --git a/platform/vitals/service.go b/platform/vitals/service.go index 9634e9014..a0e08be8c 100644 --- a/platform/vitals/service.go +++ b/platform/vitals/service.go @@ -5,6 +5,7 @@ import ( "github.com/cloudfoundry/gosigar" + boshdisk "github.com/cloudfoundry/bosh-agent/platform/disk" boshstats "github.com/cloudfoundry/bosh-agent/platform/stats" boshdirs "github.com/cloudfoundry/bosh-agent/settings/directories" bosherr "github.com/cloudfoundry/bosh-utils/errors" @@ -19,12 +20,18 @@ type Service interface { type concreteService struct { statsCollector boshstats.Collector dirProvider boshdirs.Provider + diskMounter boshdisk.Mounter } -func NewService(statsCollector boshstats.Collector, dirProvider boshdirs.Provider) Service { +func NewService( + statsCollector boshstats.Collector, + dirProvider boshdirs.Provider, + diskMounter boshdisk.Mounter, +) Service { return concreteService{ statsCollector: statsCollector, dirProvider: dirProvider, + diskMounter: diskMounter, } } @@ -110,6 +117,18 @@ func (s concreteService) getDiskStats() (diskStats DiskVitals, err error) { func (s concreteService) addDiskStats(diskStats DiskVitals, path, name string) (updated DiskVitals, err error) { updated = diskStats + if s.diskMounter != nil { + var isMountPoint bool + _, isMountPoint, err = s.diskMounter.IsMountPoint(path) + if err != nil { + err = bosherr.WrapError(err, fmt.Sprintf("Verifying if '%s' is a mount point", path)) + return + } + if !isMountPoint { + return + } + } + stat, diskErr := s.statsCollector.GetDiskStats(path) if diskErr != nil { if path == "/" { diff --git a/platform/vitals/service_test.go b/platform/vitals/service_test.go index 3578541b9..ec968412b 100644 --- a/platform/vitals/service_test.go +++ b/platform/vitals/service_test.go @@ -7,6 +7,7 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "github.com/cloudfoundry/bosh-agent/platform/disk/diskfakes" boshstats "github.com/cloudfoundry/bosh-agent/platform/stats" fakestats "github.com/cloudfoundry/bosh-agent/platform/stats/fakes" . "github.com/cloudfoundry/bosh-agent/platform/vitals" @@ -16,55 +17,63 @@ import ( const Windows = runtime.GOOS == "windows" -func buildVitalsService() (statsCollector *fakestats.FakeCollector, service Service) { - dirProvider := boshdirs.NewProvider("/fake/base/dir") - statsCollector = &fakestats.FakeCollector{ - CPULoad: boshstats.CPULoad{ - One: 0.2, - Five: 4.55, - Fifteen: 1.123, - }, - StartCollectingCPUStats: boshstats.CPUStats{ - User: 56, - Sys: 10, - Wait: 1, - Total: 100, - }, - MemStats: boshstats.Usage{ - Used: 700 * 1024, - Total: 1000 * 1024, - }, - SwapStats: boshstats.Usage{ - Used: 600 * 1024, - Total: 1000 * 1024, - }, - UptimeStats: boshstats.UptimeStats{ - Secs: 5, - }, - DiskStats: map[string]boshstats.DiskStats{ - "/": boshstats.DiskStats{ - DiskUsage: boshstats.Usage{Used: 100, Total: 200}, - InodeUsage: boshstats.Usage{Used: 50, Total: 500}, +var _ = Describe("Vitals service", func() { + var ( + dirProvider boshdirs.Provider + statsCollector *fakestats.FakeCollector + mounter *diskfakes.FakeMounter + service Service + ) + + BeforeEach(func() { + dirProvider = boshdirs.NewProvider("/fake/base/dir") + statsCollector = &fakestats.FakeCollector{ + CPULoad: boshstats.CPULoad{ + One: 0.2, + Five: 4.55, + Fifteen: 1.123, + }, + StartCollectingCPUStats: boshstats.CPUStats{ + User: 56, + Sys: 10, + Wait: 1, + Total: 100, + }, + MemStats: boshstats.Usage{ + Used: 700 * 1024, + Total: 1000 * 1024, }, - dirProvider.DataDir(): boshstats.DiskStats{ - DiskUsage: boshstats.Usage{Used: 15, Total: 20}, - InodeUsage: boshstats.Usage{Used: 10, Total: 50}, + SwapStats: boshstats.Usage{ + Used: 600 * 1024, + Total: 1000 * 1024, }, - dirProvider.StoreDir(): boshstats.DiskStats{ - DiskUsage: boshstats.Usage{Used: 2, Total: 2}, - InodeUsage: boshstats.Usage{Used: 3, Total: 4}, + UptimeStats: boshstats.UptimeStats{ + Secs: 5, + }, + DiskStats: map[string]boshstats.DiskStats{ + "/": { + DiskUsage: boshstats.Usage{Used: 100, Total: 200}, + InodeUsage: boshstats.Usage{Used: 50, Total: 500}, + }, + dirProvider.DataDir(): { + DiskUsage: boshstats.Usage{Used: 15, Total: 20}, + InodeUsage: boshstats.Usage{Used: 10, Total: 50}, + }, + dirProvider.StoreDir(): { + DiskUsage: boshstats.Usage{Used: 2, Total: 2}, + InodeUsage: boshstats.Usage{Used: 3, Total: 4}, + }, }, - }, - } + } - service = NewService(statsCollector, dirProvider) - statsCollector.StartCollecting(1*time.Millisecond, nil) - return -} + mounter = &diskfakes.FakeMounter{} + mounter.IsMountPointReturns("/dev/fake-partition-device", true, nil) -var _ = Describe("Vitals service", func() { - It("vitals construction", func() { - _, service := buildVitalsService() + service = NewService(statsCollector, dirProvider, mounter) + statsCollector.StartCollecting(1*time.Millisecond, nil) + }) + + It("constructs vitals properly", func() { vitals, err := service.Get() expectedVitals := map[string]interface{}{ @@ -107,31 +116,55 @@ var _ = Describe("Vitals service", func() { Expect(err).ToNot(HaveOccurred()) + Expect(mounter.IsMountPointCallCount()).To(Equal(3)) + boshassert.MatchesJSONMap(GinkgoT(), vitals, expectedVitals) }) - It("getting vitals when missing disks", func() { - - statsCollector, service := buildVitalsService() - statsCollector.DiskStats = map[string]boshstats.DiskStats{ - "/": boshstats.DiskStats{ - DiskUsage: boshstats.Usage{Used: 100, Total: 200}, - InodeUsage: boshstats.Usage{Used: 50, Total: 500}, - }, - } + Context("when missing stats for ephemeral and peristent disk", func() { + BeforeEach(func() { + statsCollector.DiskStats = map[string]boshstats.DiskStats{ + "/": { + DiskUsage: boshstats.Usage{Used: 100, Total: 200}, + InodeUsage: boshstats.Usage{Used: 50, Total: 500}, + }, + } + }) - vitals, err := service.Get() - Expect(err).ToNot(HaveOccurred()) + It("returns vitals for root disk only", func() { + vitals, err := service.Get() + Expect(err).ToNot(HaveOccurred()) - boshassert.LacksJSONKey(GinkgoT(), vitals.Disk, "ephemeral") - boshassert.LacksJSONKey(GinkgoT(), vitals.Disk, "persistent") + boshassert.LacksJSONKey(GinkgoT(), vitals.Disk, "ephemeral") + boshassert.LacksJSONKey(GinkgoT(), vitals.Disk, "persistent") + }) }) - It("get getting vitals on system disk error", func() { - statsCollector, service := buildVitalsService() - statsCollector.DiskStats = map[string]boshstats.DiskStats{} + Context("when missing stats for system disk", func() { + BeforeEach(func() { + statsCollector.DiskStats = map[string]boshstats.DiskStats{} + }) + + It("returns an error", func() { + _, err := service.Get() + Expect(err).To(HaveOccurred()) + }) + }) - _, err := service.Get() - Expect(err).To(HaveOccurred()) + Context("when no persistent disk is mounted", func() { + BeforeEach(func() { + mounter.IsMountPointStub = func(path string) (partitionPath string, isMountPoint bool, err error) { + partitionPath = "/dev/fake-partition-device" + isMountPoint = (path != "/fake/base/dir/store") + return + } + }) + + It("does not return vitals for persistent disk", func() { + vitals, err := service.Get() + + Expect(err).NotTo(HaveOccurred()) + boshassert.LacksJSONKey(GinkgoT(), vitals.Disk, "persistent") + }) }) }) diff --git a/platform/windows_platform.go b/platform/windows_platform.go index 62c76c5d1..4cd140934 100644 --- a/platform/windows_platform.go +++ b/platform/windows_platform.go @@ -89,7 +89,7 @@ func NewWindowsPlatform( dirProvider: dirProvider, netManager: netManager, devicePathResolver: devicePathResolver, - vitalsService: boshvitals.NewService(collector, dirProvider), + vitalsService: boshvitals.NewService(collector, dirProvider, nil), certManager: certManager, options: options, defaultNetworkResolver: defaultNetworkResolver, From e2d7668ce46a001d59eecf8e3fd98cc7033f5bdf Mon Sep 17 00:00:00 2001 From: rkoster Date: Tue, 14 Sep 2021 21:24:08 +0200 Subject: [PATCH 2/2] add lorri support --- .envrc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.envrc b/.envrc index 4767af4fb..5ba7fb39f 100644 --- a/.envrc +++ b/.envrc @@ -1,2 +1,6 @@ export GOPATH=$(realpath $PWD/../../../..) export PATH=$PATH:$GOPATH/bin + +if command -v lorri &> /dev/null; then + eval "$(lorri direnv)" +fi