Skip to content

Commit

Permalink
Merge pull request #14 from treydock/tests
Browse files Browse the repository at this point in the history
Expanded tests
  • Loading branch information
treydock authored Mar 13, 2020
2 parents 13aeaba + f55a3db commit 825b6b2
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 31 deletions.
23 changes: 21 additions & 2 deletions cmd/gpfs_exporter/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package main

import (
"context"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -23,15 +24,27 @@ import (
"time"

"github.com/go-kit/kit/log"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"github.com/treydock/gpfs_exporter/collectors"
"gopkg.in/alecthomas/kingpin.v2"
)

const (
address = "localhost:19303"
)

var (
mmpmonStdout = `
_fs_io_s_ _n_ 10.22.0.106 _nn_ ib-pitzer-rw02.ten _rc_ 0 _t_ 1579358234 _tu_ 53212 _cl_ gpfs.osc.edu _fs_ scratch _d_ 48 _br_ 205607400434 _bw_ 74839282351 _oc_ 2377656 _cc_ 2201576 _rdc_ 59420404 _wc_ 18874626 _dir_ 40971 _iu_ 544768
_fs_io_s_ _n_ 10.22.0.106 _nn_ ib-pitzer-rw02.ten _rc_ 0 _t_ 1579358234 _tu_ 53212 _cl_ gpfs.osc.edu _fs_ project _d_ 96 _br_ 0 _bw_ 0 _oc_ 513 _cc_ 513 _rdc_ 0 _wc_ 0 _dir_ 0 _iu_ 169
`
mmgetstateStdout = `
mmgetstate::HEADER:version:reserved:reserved:nodeName:nodeNumber:state:quorum:nodesUp:totalNodes:remarks:cnfsState:
mmgetstate::0:1:::ib-proj-nsd05.domain:11:active:4:7:1122::(undefined):
`
)

func TestMain(m *testing.M) {
if _, err := kingpin.CommandLine.Parse([]string{"--no-collector.mmgetstate", "--no-collector.mmpmon"}); err != nil {
if _, err := kingpin.CommandLine.Parse([]string{}); err != nil {
os.Exit(1)
}
varTrue := true
Expand All @@ -51,6 +64,12 @@ func TestMain(m *testing.M) {
}

func TestMetricsHandler(t *testing.T) {
collectors.MmgetstateExec = func(ctx context.Context) (string, error) {
return mmgetstateStdout, nil
}
collectors.MmpmonExec = func(ctx context.Context) (string, error) {
return mmpmonStdout, nil
}
body, err := queryExporter()
if err != nil {
t.Fatalf("Unexpected error GET /metrics: %s", err.Error())
Expand Down
79 changes: 77 additions & 2 deletions cmd/gpfs_mmdf_exporter/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,93 @@
package main

import (
kingpin "gopkg.in/alecthomas/kingpin.v2"
"context"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"

"github.com/go-kit/kit/log"
"github.com/treydock/gpfs_exporter/collectors"
"gopkg.in/alecthomas/kingpin.v2"
)

var (
outputPath string
)

func TestMain(m *testing.M) {
if _, err := kingpin.CommandLine.Parse([]string{"--output=/dne"}); err != nil {
tmpDir, err := ioutil.TempDir(os.TempDir(), "output")
if err != nil {
os.Exit(1)
}
outputPath = tmpDir + "/output"
defer os.RemoveAll(tmpDir)
if _, err := kingpin.CommandLine.Parse([]string{fmt.Sprintf("--output=%s", outputPath), "--collector.mmdf.filesystems=project"}); err != nil {
os.Exit(1)
}
exitVal := m.Run()
os.Exit(exitVal)
}

func TestCollect(t *testing.T) {
mmdfStdout := `
mmdf:nsd:HEADER:version:reserved:reserved:nsdName:storagePool:diskSize:failureGroup:metadata:data:freeBlocks:freeBlocksPct:freeFragments:freeFragmentsPct:diskAvailableForAlloc:
mmdf:poolTotal:HEADER:version:reserved:reserved:poolName:poolSize:freeBlocks:freeBlocksPct:freeFragments:freeFragmentsPct:maxDiskSize:
mmdf:data:HEADER:version:reserved:reserved:totalData:freeBlocks:freeBlocksPct:freeFragments:freeFragmentsPct:
mmdf:metadata:HEADER:version:reserved:reserved:totalMetadata:freeBlocks:freeBlocksPct:freeFragments:freeFragmentsPct:
mmdf:fsTotal:HEADER:version:reserved:reserved:fsSize:freeBlocks:freeBlocksPct:freeFragments:freeFragmentsPct:
mmdf:inode:HEADER:version:reserved:reserved:usedInodes:freeInodes:allocatedInodes:maxInodes:
mmdf:nsd:0:1:::P_META_VD102:system:771751936:300:Yes:No:320274944:41:5005384:1::
mmdf:nsd:0:1:::P_DATA_VD02:data:46766489600:200:No:Yes:6092915712:13:154966272:0::
mmdf:poolTotal:0:1:::data:3647786188800:475190722560:13:12059515296:0:3860104580096:
mmdf:data:0:1:::3647786188800:475190722560:13:12059515296:0:
mmdf:metadata:0:1:::13891534848:6011299328:43:58139768:0:
mmdf:fsTotal:0:1:::3661677723648:481202021888:14:12117655064:0:
mmdf:inode:0:1:::430741822:484301506:915043328:1332164000:
`
expected := `
# HELP gpfs_fs_free_bytes GPFS filesystem free size in bytes
# TYPE gpfs_fs_free_bytes gauge
gpfs_fs_free_bytes{fs="project"} 4.92750870413312e+14
# HELP gpfs_fs_free_percent GPFS filesystem free percent
# TYPE gpfs_fs_free_percent gauge
gpfs_fs_free_percent{fs="project"} 14
# HELP gpfs_fs_inodes_allocated GPFS filesystem inodes allocated
# TYPE gpfs_fs_inodes_allocated gauge
gpfs_fs_inodes_allocated{fs="project"} 9.15043328e+08
# HELP gpfs_fs_inodes_free GPFS filesystem inodes free
# TYPE gpfs_fs_inodes_free gauge
gpfs_fs_inodes_free{fs="project"} 4.84301506e+08
# HELP gpfs_fs_inodes_total GPFS filesystem inodes total
# TYPE gpfs_fs_inodes_total gauge
gpfs_fs_inodes_total{fs="project"} 1.332164e+09
# HELP gpfs_fs_inodes_used GPFS filesystem inodes used
# TYPE gpfs_fs_inodes_used gauge
gpfs_fs_inodes_used{fs="project"} 4.30741822e+08
# HELP gpfs_fs_metadata_free_bytes GPFS metadata free size in bytes
# TYPE gpfs_fs_metadata_free_bytes gauge
gpfs_fs_metadata_free_bytes{fs="project"} 6.155570511872e+12
# HELP gpfs_fs_metadata_free_percent GPFS metadata free percent
# TYPE gpfs_fs_metadata_free_percent gauge
gpfs_fs_metadata_free_percent{fs="project"} 43
# HELP gpfs_fs_metadata_total_bytes GPFS total metadata size in bytes
# TYPE gpfs_fs_metadata_total_bytes gauge
gpfs_fs_metadata_total_bytes{fs="project"} 1.4224931684352e+13
# HELP gpfs_fs_total_bytes GPFS filesystem total size in bytes
# TYPE gpfs_fs_total_bytes gauge
gpfs_fs_total_bytes{fs="project"} 3.749557989015552e+15`
collectors.MmdfExec = func(fs string, ctx context.Context) (string, error) {
return mmdfStdout, nil
}
collect(log.NewNopLogger())
content, err := ioutil.ReadFile(outputPath)
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
return
}
if !strings.Contains(string(content), expected) {
t.Errorf("Unexpected content:\n%s\nExpected:\n%s", string(content), expected)
}
}
4 changes: 2 additions & 2 deletions collectors/mmdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var (
}
mmdfCacheMutex = sync.RWMutex{}
mmdfCache = make(map[string]DFMetric)
mmdfExec = mmdf
MmdfExec = mmdf
)

type DFMetric struct {
Expand Down Expand Up @@ -192,7 +192,7 @@ func (c *MmdfCollector) mmdfCollect(fs string) (DFMetric, error) {
var err error
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(*mmdfTimeout)*time.Second)
defer cancel()
out, err = mmdfExec(fs, ctx)
out, err = MmdfExec(fs, ctx)
if ctx.Err() == context.DeadlineExceeded {
dfMetric = c.mmdfReadCache(fs)
return dfMetric, ctx.Err()
Expand Down
14 changes: 7 additions & 7 deletions collectors/mmdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestMmdfCollector(t *testing.T) {
}
filesystems := "project"
configFilesystems = &filesystems
mmdfExec = func(fs string, ctx context.Context) (string, error) {
MmdfExec = func(fs string, ctx context.Context) (string, error) {
return mmdfStdout, nil
}
expected := `
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestMmdfCollectorError(t *testing.T) {
}
filesystems := "project"
configFilesystems = &filesystems
mmdfExec = func(fs string, ctx context.Context) (string, error) {
MmdfExec = func(fs string, ctx context.Context) (string, error) {
return "", fmt.Errorf("Error")
}
expected := `
Expand All @@ -166,7 +166,7 @@ func TestMmdfCollectorTimeout(t *testing.T) {
}
filesystems := "project"
configFilesystems = &filesystems
mmdfExec = func(fs string, ctx context.Context) (string, error) {
MmdfExec = func(fs string, ctx context.Context) (string, error) {
return "", context.DeadlineExceeded
}
expected := `
Expand All @@ -191,7 +191,7 @@ func TestMmdfCollectorCache(t *testing.T) {
filesystems := "project"
configFilesystems = &filesystems
// build cache
mmdfExec = func(fs string, ctx context.Context) (string, error) {
MmdfExec = func(fs string, ctx context.Context) (string, error) {
return mmdfStdout, nil
}
collector := NewMmdfCollector(log.NewNopLogger(), true)
Expand All @@ -200,7 +200,7 @@ func TestMmdfCollectorCache(t *testing.T) {
t.Errorf("Unexpected collection count %d, expected 14", val)
}

mmdfExec = func(fs string, ctx context.Context) (string, error) {
MmdfExec = func(fs string, ctx context.Context) (string, error) {
return "", fmt.Errorf("Error")
}
expected := `
Expand Down Expand Up @@ -255,7 +255,7 @@ func TestMmdfCollectorCache(t *testing.T) {
# TYPE gpfs_exporter_collect_timeout gauge
gpfs_exporter_collect_timeout{collector="mmdf-project"} 1
`
mmdfExec = func(fs string, ctx context.Context) (string, error) {
MmdfExec = func(fs string, ctx context.Context) (string, error) {
return "", context.DeadlineExceeded
}
if val := testutil.CollectAndCount(collector); val != 14 {
Expand All @@ -269,7 +269,7 @@ func TestMmdfCollectorCache(t *testing.T) {
}

mmdfCache = make(map[string]DFMetric)
mmdfExec = func(fs string, ctx context.Context) (string, error) {
MmdfExec = func(fs string, ctx context.Context) (string, error) {
return mmdfStdout, nil
}
if val := testutil.CollectAndCount(collector); val != 14 {
Expand Down
4 changes: 2 additions & 2 deletions collectors/mmgetstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (
mmgetstateTimeout = kingpin.Flag("collector.mmgetstate.timeout", "Timeout for executing mmgetstate").Default("5").Int()
mmgetstateStates = []string{"active", "arbitrating", "down"}
mmgetstateCache = MmgetstateMetrics{}
mmgetstateExec = mmgetstate
MmgetstateExec = mmgetstate
)

type MmgetstateMetrics struct {
Expand Down Expand Up @@ -95,7 +95,7 @@ func (c *MmgetstateCollector) collect() (MmgetstateMetrics, error) {
var err error
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(*mmgetstateTimeout)*time.Second)
defer cancel()
out, err = mmgetstateExec(ctx)
out, err = MmgetstateExec(ctx)
if ctx.Err() == context.DeadlineExceeded {
if c.useCache {
metric = mmgetstateCache
Expand Down
14 changes: 7 additions & 7 deletions collectors/mmgetstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestMmgetstateCollector(t *testing.T) {
if _, err := kingpin.CommandLine.Parse([]string{}); err != nil {
t.Fatal(err)
}
mmgetstateExec = func(ctx context.Context) (string, error) {
MmgetstateExec = func(ctx context.Context) (string, error) {
return mmgetstateStdout, nil
}
expected := `
Expand All @@ -88,7 +88,7 @@ func TestMMgetstateCollectorError(t *testing.T) {
if _, err := kingpin.CommandLine.Parse([]string{}); err != nil {
t.Fatal(err)
}
mmgetstateExec = func(ctx context.Context) (string, error) {
MmgetstateExec = func(ctx context.Context) (string, error) {
return "", fmt.Errorf("Error")
}
expected := `
Expand All @@ -110,7 +110,7 @@ func TestMMgetstateCollectorTimeout(t *testing.T) {
if _, err := kingpin.CommandLine.Parse([]string{}); err != nil {
t.Fatal(err)
}
mmgetstateExec = func(ctx context.Context) (string, error) {
MmgetstateExec = func(ctx context.Context) (string, error) {
return "", context.DeadlineExceeded
}
expected := `
Expand All @@ -133,7 +133,7 @@ func TestMMgetstateCollectorCache(t *testing.T) {
t.Fatal(err)
}
// build cache
mmgetstateExec = func(ctx context.Context) (string, error) {
MmgetstateExec = func(ctx context.Context) (string, error) {
return mmgetstateStdout, nil
}
collector := NewMmgetstateCollector(log.NewNopLogger(), true)
Expand All @@ -142,7 +142,7 @@ func TestMMgetstateCollectorCache(t *testing.T) {
t.Errorf("Unexpected collection count %d, expected 7", val)
}

mmgetstateExec = func(ctx context.Context) (string, error) {
MmgetstateExec = func(ctx context.Context) (string, error) {
return "", fmt.Errorf("Error")
}
expected := `
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestMMgetstateCollectorCache(t *testing.T) {
# TYPE gpfs_exporter_collect_timeout gauge
gpfs_exporter_collect_timeout{collector="mmgetstate"} 1
`
mmgetstateExec = func(ctx context.Context) (string, error) {
MmgetstateExec = func(ctx context.Context) (string, error) {
return "", context.DeadlineExceeded
}
if val := testutil.CollectAndCount(collector); val != 7 {
Expand All @@ -181,7 +181,7 @@ func TestMMgetstateCollectorCache(t *testing.T) {
}

mmgetstateCache = MmgetstateMetrics{}
mmgetstateExec = func(ctx context.Context) (string, error) {
MmgetstateExec = func(ctx context.Context) (string, error) {
return mmgetstateStdout, nil
}
if val := testutil.CollectAndCount(collector); val != 7 {
Expand Down
35 changes: 35 additions & 0 deletions collectors/mmhealth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,39 @@ func TestMMhealthCollectorCache(t *testing.T) {
if val := len(mmhealthCache); val != 9 {
t.Errorf("Unexpected cache size %d, expected 9", val)
}
mmhealthStdout = `
mmhealth:Event:HEADER:version:reserved:reserved:node:component:entityname:entitytype:event:arguments:activesince:identifier:ishidden:
mmhealth:State:HEADER:version:reserved:reserved:node:component:entityname:entitytype:status:laststatuschange:
mmhealth:State:0:1:::ib-haswell1.example.com:NODE:ib-haswell1.example.com:NODE:HEALTHY:2020-01-27 09%3A35%3A21.859186 EST:
mmhealth:State:0:1:::ib-haswell1.example.com:GPFS:ib-haswell1.example.com:NODE:HEALTHY:2020-01-27 09%3A35%3A21.791895 EST:
mmhealth:State:0:1:::ib-haswell1.example.com:NETWORK:ib-haswell1.example.com:NODE:HEALTHY:2020-01-07 17%3A02%3A40.131272 EST:
mmhealth:State:0:1:::ib-haswell1.example.com:NETWORK:ib0:NIC:HEALTHY:2020-01-07 16%3A47%3A39.397852 EST:
mmhealth:State:0:1:::ib-haswell1.example.com:NETWORK:mlx5_0/1:IB_RDMA:HEALTHY:2020-01-07 17%3A02%3A40.205075 EST:
mmhealth:State:0:1:::ib-haswell1.example.com:FILESYSTEM:ib-haswell1.example.com:NODE:HEALTHY:2020-01-27 09%3A35%3A21.499264 EST:
mmhealth:State:0:1:::ib-haswell1.example.com:FILESYSTEM:project:FILESYSTEM:HEALTHY:2020-01-27 09%3A35%3A21.573978 EST:
mmhealth:State:0:1:::ib-haswell1.example.com:FILESYSTEM:scratch:FILESYSTEM:HEALTHY:2020-01-27 09%3A35%3A21.657798 EST:
mmhealth:State:0:1:::ib-haswell1.example.com:FILESYSTEM:ess:FILESYSTEM:HEALTHY:2020-01-27 09%3A35%3A21.716417 EST:
`
expected = `
# HELP gpfs_health_status GPFS health status, 1=healthy 0=not healthy
# TYPE gpfs_health_status gauge
gpfs_health_status{component="FILESYSTEM",entityname="ib-haswell1.example.com",entitytype="NODE",status="HEALTHY"} 1
gpfs_health_status{component="FILESYSTEM",entityname="project",entitytype="FILESYSTEM",status="HEALTHY"} 1
gpfs_health_status{component="FILESYSTEM",entityname="scratch",entitytype="FILESYSTEM",status="HEALTHY"} 1
gpfs_health_status{component="FILESYSTEM",entityname="ess",entitytype="FILESYSTEM",status="HEALTHY"} 1
gpfs_health_status{component="GPFS",entityname="ib-haswell1.example.com",entitytype="NODE",status="HEALTHY"} 1
gpfs_health_status{component="NETWORK",entityname="ib-haswell1.example.com",entitytype="NODE",status="HEALTHY"} 1
gpfs_health_status{component="NETWORK",entityname="ib0",entitytype="NIC",status="HEALTHY"} 1
gpfs_health_status{component="NETWORK",entityname="mlx5_0/1",entitytype="IB_RDMA",status="HEALTHY"} 1
gpfs_health_status{component="NODE",entityname="ib-haswell1.example.com",entitytype="NODE",status="HEALTHY"} 1
`
mmhealthExec = func(ctx context.Context) (string, error) {
return mmhealthStdout, nil
}
if val := testutil.CollectAndCount(collector); val != 12 {
t.Errorf("Unexpected collection count %d, expected 12", val)
}
if err := testutil.GatherAndCompare(gatherers, strings.NewReader(expected), "gpfs_health_status"); err != nil {
t.Errorf("unexpected collecting result:\n%s", err)
}
}
4 changes: 2 additions & 2 deletions collectors/mmpmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (
"_iu_": "InodeUpdates",
}
mmpmonCache = []PerfMetrics{}
mmpmonExec = mmpmon
MmpmonExec = mmpmon
)

type PerfMetrics struct {
Expand Down Expand Up @@ -124,7 +124,7 @@ func (c *MmpmonCollector) collect() ([]PerfMetrics, error) {
var err error
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(*mmpmonTimeout)*time.Second)
defer cancel()
mmpmon_out, err = mmpmonExec(ctx)
mmpmon_out, err = MmpmonExec(ctx)
if ctx.Err() == context.DeadlineExceeded {
if c.useCache {
perfs = mmpmonCache
Expand Down
Loading

0 comments on commit 825b6b2

Please sign in to comment.