Skip to content

Commit

Permalink
Fix test and add test case for metrics describe when StatVFS is off
Browse files Browse the repository at this point in the history
  • Loading branch information
arunvelsriram committed Nov 30, 2024
1 parent 2c20b19 commit 21e7489
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions pkg/collector/sftp_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (s *SFTPCollectorSuite) SetupTest() {
s.ctrl = gomock.NewController(s.T())
s.sftpClient = mocks.NewMockSFTPClient(s.ctrl)
s.collector = NewSFTPCollector(s.sftpClient)
viper.Set(viperkeys.SFTPStatVfs, true)
}

func (s *SFTPCollectorSuite) TearDownTest() {
Expand Down Expand Up @@ -100,6 +101,18 @@ func (s *SFTPCollectorSuite) TestSFTPCollectorDescribe() {
)
}

func (s *SFTPCollectorSuite) TestSFTPCollectorDescribeShouldSkipFileSystemMetricsWhenStatVfsIsDisabled() {
viper.Set(viperkeys.SFTPStatVfs, false)
ch := make(chan *prometheus.Desc)
go s.collector.Describe(ch)

for range 3 {
m := <-ch
s.NotContains(m.String(), "filesystem_total_space_bytes")
s.NotContains(m.String(), "filesystem_free_space_bytes")
}
}

func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldWriteUpMetric() {
viper.Set(viperkeys.SFTPPaths, []string{})
s.sftpClient.EXPECT().Connect().Return(nil)
Expand Down Expand Up @@ -349,16 +362,13 @@ func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldNotWriteObjectMetrics
}

func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldNotCallStatVFS() {
viper.Set(viperkeys.SFTPPaths, []string{"/path0", "/path1"})
viper.Set(viperkeys.SFTPPaths, []string{"/path0"})
viper.Set(viperkeys.SFTPStatVfs, false)
memFs := afero.NewMemMapFs()
_ = memFs.MkdirAll("/path0", 0755)
_ = memFs.MkdirAll("/path1", 0755)
path0Walker := fs.WalkFS("/path0", memKrFs{memFs: memFs})
path1Walker := fs.WalkFS("/path1", memKrFs{memFs: memFs})
s.sftpClient.EXPECT().Connect().Return(nil)
s.sftpClient.EXPECT().Walk("/path0").Return(path0Walker)
s.sftpClient.EXPECT().Walk("/path1").Return(path1Walker)
s.sftpClient.EXPECT().Close()
ch := make(chan prometheus.Metric)
done := make(chan bool)
Expand All @@ -368,17 +378,11 @@ func (s *SFTPCollectorSuite) TestSFTPCollectorCollectShouldNotCallStatVFS() {
done <- true
}()

m1 := <-ch
s.NotContains(m1.Desc().String(), "filesystem_total_space_bytes")
s.NotContains(m1.Desc().String(), "filesystem_free_space_bytes")
m2 := <-ch
s.NotContains(m2.Desc().String(), "filesystem_total_space_bytes")
s.NotContains(m2.Desc().String(), "filesystem_free_space_bytes")
m3 := <-ch
s.NotContains(m3.Desc().String(), "filesystem_total_space_bytes")
s.NotContains(m3.Desc().String(), "filesystem_free_space_bytes")
<-ch
<-ch
for range 3 {
m := <-ch
s.NotContains(m.Desc().String(), "filesystem_total_space_bytes")
s.NotContains(m.Desc().String(), "filesystem_free_space_bytes")
}
close(ch)
<-done
}

0 comments on commit 21e7489

Please sign in to comment.