Skip to content

Commit

Permalink
Add test for collector Describe method
Browse files Browse the repository at this point in the history
  • Loading branch information
arunvelsriram committed Aug 13, 2020
1 parent adc12c9 commit f8cd072
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/arunvelsriram/sftp-exporter
go 1.14

require (
github.com/golang/mock v1.4.3 // indirect
github.com/golang/mock v1.4.3
github.com/pkg/sftp v1.11.0
github.com/prometheus/client_golang v0.9.3
github.com/sirupsen/logrus v1.2.0
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "github.com/arunvelsriram/sftp-exporter/cmd"

//go:generate mkdir -p pkg/internal/mocks
//go:generate mockgen -source pkg/config/config.go -destination pkg/internal/mocks/config.go -package mocks
//go:generate mockgen -source pkg/client/sftp_client.go -destination pkg/internal/mocks/sftp_client.go -package mocks
func main() {
cmd.Execute()
}
11 changes: 7 additions & 4 deletions pkg/collector/sftp_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ var (
)
)

type CreateClientFn func(config.Config) (client.SFTPClient, error)

type SFTPCollector struct {
config config.Config
config config.Config
createClientFn CreateClientFn
}

func (s SFTPCollector) Describe(ch chan<- *prometheus.Desc) {
Expand All @@ -59,7 +62,7 @@ func (s SFTPCollector) Describe(ch chan<- *prometheus.Desc) {
}

func (s SFTPCollector) Collect(ch chan<- prometheus.Metric) {
sftpClient, err := client.NewSFTPClient(s.config)
sftpClient, err := s.createClientFn(s.config)
if err != nil {
ch <- prometheus.MustNewConstMetric(up, prometheus.GaugeValue, 0)
log.WithFields(log.Fields{"event": "creating SFTP sftpClient"}).Error(err)
Expand Down Expand Up @@ -89,6 +92,6 @@ func (s SFTPCollector) Collect(ch chan<- prometheus.Metric) {
}
}

func NewSFTPCollector(cfg config.Config) prometheus.Collector {
return SFTPCollector{config: cfg}
func NewSFTPCollector(cfg config.Config, fn CreateClientFn) prometheus.Collector {
return SFTPCollector{config: cfg, createClientFn: fn}
}
75 changes: 75 additions & 0 deletions pkg/collector/sftp_collector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package collector

import (
"testing"

"github.com/arunvelsriram/sftp-exporter/pkg/client"
"github.com/arunvelsriram/sftp-exporter/pkg/config"
"github.com/arunvelsriram/sftp-exporter/pkg/internal/mocks"
"github.com/golang/mock/gomock"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/suite"
)

type SFTPCollectorSuite struct {
suite.Suite
ctrl *gomock.Controller
config config.Config
sftpClient client.SFTPClient
createClientFn CreateClientFn
collector prometheus.Collector
}

func TestSFTPCollectorSuite(t *testing.T) {
suite.Run(t, new(SFTPCollectorSuite))
}

func (s *SFTPCollectorSuite) SetupTest() {
s.ctrl = gomock.NewController(s.T())
s.config = mocks.NewMockConfig(s.ctrl)
s.sftpClient = mocks.NewMockSFTPClient(s.ctrl)
fn := func(c config.Config) (client.SFTPClient, error) {
return s.sftpClient, nil
}
s.createClientFn = CreateClientFn(fn)
s.collector = NewSFTPCollector(s.config, s.createClientFn)
}

func (s *SFTPCollectorSuite) TearDownTest() {
s.ctrl.Finish()
}

func (s *SFTPCollectorSuite) TestSFTPCollectorDescribe() {
ch := make(chan *prometheus.Desc)
go s.collector.Describe(ch)

up := <-ch
s.Equal(
`Desc{fqName: "sftp_up", help: "Tells if exporter is able to connect to SFTP", constLabels: {}, variableLabels: []}`,
up.String(),
)

fsTotalSpace := <-ch
s.Equal(
`Desc{fqName: "sftp_filesystem_total_space_bytes", help: "Total space in the filesystem containing the path", constLabels: {}, variableLabels: [path]}`,
fsTotalSpace.String(),
)

fsFreeSpace := <-ch
s.Equal(
`Desc{fqName: "sftp_filesystem_free_space_bytes", help: "Free space in the filesystem containing the path", constLabels: {}, variableLabels: [path]}`,
fsFreeSpace.String(),
)

objectCount := <-ch
s.Equal(
`Desc{fqName: "sftp_objects_count_total", help: "Total number of objects in the path", constLabels: {}, variableLabels: [path]}`,
objectCount.String(),
)

objectSize := <-ch
s.Equal(
`Desc{fqName: "sftp_objects_size_total_bytes", help: "Total size of all objects in the path", constLabels: {}, variableLabels: [path]}`,
objectSize.String(),
)
}
76 changes: 76 additions & 0 deletions pkg/internal/mocks/sftp_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"

"github.com/arunvelsriram/sftp-exporter/pkg/client"
"github.com/arunvelsriram/sftp-exporter/pkg/collector"
"github.com/arunvelsriram/sftp-exporter/pkg/config"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -12,7 +13,8 @@ import (
)

func Start(cfg config.Config) error {
sftpCollector := collector.NewSFTPCollector(cfg)
createClientFn := collector.CreateClientFn(client.NewSFTPClient)
sftpCollector := collector.NewSFTPCollector(cfg, createClientFn)
prometheus.MustRegister(sftpCollector)

r := http.NewServeMux()
Expand Down

0 comments on commit f8cd072

Please sign in to comment.