Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
rkoster committed Sep 29, 2021
2 parents 2c8c714 + e2d7668 commit b427644
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 68 deletions.
2 changes: 1 addition & 1 deletion agent/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,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)

Expand Down
2 changes: 1 addition & 1 deletion platform/dummy_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion platform/linux_platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -122,6 +121,8 @@ var _ = Describe("LinuxPlatform", func() {

diskUtil = fakedisk.NewFakeDiskUtil()
diskManager.GetUtilReturns(diskUtil)

vitalsService = boshvitals.NewService(collector, dirProvider, mounter)
})

JustBeforeEach(func() {
Expand Down
2 changes: 1 addition & 1 deletion platform/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
21 changes: 20 additions & 1 deletion platform/vitals/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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 == "/" {
Expand Down
157 changes: 95 additions & 62 deletions platform/vitals/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{}{
Expand Down Expand Up @@ -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")
})
})
})
2 changes: 1 addition & 1 deletion platform/windows_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b427644

Please sign in to comment.