Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC improve testing #469

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions pkg/data/measurement/measurement.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,47 @@ type Measurement struct {
FilterCfg *config.MeasFilterCfg
Filter filter.Filter
log *logrus.Logger
snmpClient *gosnmp.GoSNMP
snmpClient SNMPClient
DisableBulk bool `json:"-"`
GetData func() (int64, int64, int64, error) `json:"-"`
Walk func(string, gosnmp.WalkFunc) error `json:"-"`
MultiIndexMeas []*Measurement
}

type SNMPClient interface {
Version() gosnmp.SnmpVersion
Walk() func(rootOid string, walkFn gosnmp.WalkFunc) error
BulkWalk() func(rootOid string, walkFn gosnmp.WalkFunc) error
Target() string
Get(oids []string) (result *gosnmp.SnmpPacket, err error)
}

type GoSNMPConverter struct {
s *gosnmp.GoSNMP
}

func (g *GoSNMPConverter) Version() gosnmp.SnmpVersion {
return g.s.Version
}

func (g *GoSNMPConverter) Walk() func(rootOid string, walkFn gosnmp.WalkFunc) error {
return g.s.Walk
}

func (g *GoSNMPConverter) BulkWalk() func(rootOid string, walkFn gosnmp.WalkFunc) error {
return g.s.BulkWalk
}

func (g *GoSNMPConverter) Target() string {
return g.s.Target
}

func (g *GoSNMPConverter) Get(oids []string) (result *gosnmp.SnmpPacket, err error) {
return g.s.Get(oids)
}

//New creates object with config , log + goSnmp client
func New(c *config.MeasurementCfg, l *logrus.Logger, cli *gosnmp.GoSNMP, db bool) (*Measurement, error) {
func New(c *config.MeasurementCfg, l *logrus.Logger, cli SNMPClient, db bool) (*Measurement, error) {
m := &Measurement{ID: c.ID, MName: c.Name, cfg: c, log: l, snmpClient: cli, DisableBulk: db}
err := m.Init()
return m, err
Expand Down Expand Up @@ -86,10 +118,10 @@ func (m *Measurement) Init() error {

// If GetMode is multiindex, we can reuse a simple measurement to init each one and get all funcions...?
switch {
case m.snmpClient.Version == gosnmp.Version1 || m.DisableBulk:
m.Walk = m.snmpClient.Walk
case m.snmpClient.Version() == gosnmp.Version1 || m.DisableBulk:
m.Walk = m.snmpClient.Walk()
default:
m.Walk = m.snmpClient.BulkWalk
m.Walk = m.snmpClient.BulkWalk()
}

if m.cfg.GetMode == "indexed_multiple" {
Expand Down Expand Up @@ -356,15 +388,15 @@ func (m *Measurement) LoadMultiIndex() error {
}

// SetSnmpClient set a GoSNMP client to the Measurement
func (m *Measurement) SetSnmpClient(cli *gosnmp.GoSNMP) {
func (m *Measurement) SetSnmpClient(cli SNMPClient) {

m.snmpClient = cli

switch {
case m.snmpClient.Version == gosnmp.Version1 || m.DisableBulk:
m.Walk = m.snmpClient.Walk
case m.snmpClient.Version() == gosnmp.Version1 || m.DisableBulk:
m.Walk = m.snmpClient.Walk()
default:
m.Walk = m.snmpClient.BulkWalk
m.Walk = m.snmpClient.BulkWalk()
}
}

Expand Down Expand Up @@ -610,7 +642,7 @@ func (m *Measurement) SnmpWalkData() (int64, int64, int64, error) {

for _, v := range m.cfg.FieldMetric {
if err := m.Walk(v.BaseOID, setRawData); err != nil {
m.Errorf("SNMP WALK (%s) for OID (%s) get error: %s\n", m.snmpClient.Target, v.BaseOID, err)
m.Errorf("SNMP WALK (%s) for OID (%s) get error: %s\n", m.snmpClient.Target(), v.BaseOID, err)
errors += int64(m.MetricTable.Len())
}
}
Expand Down Expand Up @@ -749,7 +781,7 @@ func (m *Measurement) SnmpGetData() (int64, int64, int64, error) {
pkt, err := m.snmpClient.Get(m.snmpOids[i:end])
if err != nil {
m.Debugf("selected OIDS %+v", m.snmpOids[i:end])
m.Errorf("SNMP (%s) for OIDs (%d/%d) get error: %s\n", m.snmpClient.Target, i, end, err)
m.Errorf("SNMP (%s) for OIDs (%d/%d) get error: %s\n", m.snmpClient.Target(), i, end, err)
errs++
continue
}
Expand Down
77 changes: 52 additions & 25 deletions pkg/data/measurement/measurement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"sort"
"time"

"github.com/gosnmp/gosnmp"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -87,6 +86,54 @@ func GetOutputInfluxMetrics(m *Measurement) {
// 3.- SNMP CLIENT SETUP
// 4.- METRICMAP SETUP
// 5.- MEASUREMENT CONFIG SETUP
//
type MockSNMP struct {
Listen string
Want []gosnmp.SnmpPDU
}

func (m *MockSNMP) Version() gosnmp.SnmpVersion {
return gosnmp.Version1
}

func (m *MockSNMP) Walk() func(rootOid string, walkFn gosnmp.WalkFunc) error {
return func(rootOid string, walkFn gosnmp.WalkFunc) error {
return nil
}
}

func (m *MockSNMP) BulkWalk() func(rootOid string, walkFn gosnmp.WalkFunc) error {
return func(rootOid string, walkFn gosnmp.WalkFunc) error {
return nil
}
}

func (m *MockSNMP) Target() string {
return "target"
}

func (m *MockSNMP) Get(oids []string) (result *gosnmp.SnmpPacket, err error) {
vars := []gosnmp.SnmpPDU{}

for _, o := range oids {
for _, v := range m.Want {
if v.Name == o {
vars = append(vars, gosnmp.SnmpPDU{
Name: v.Name,
Type: v.Type,
Value: v.Value,
})
}
}

}

pkt := gosnmp.SnmpPacket{
Variables: vars,
}

return &pkt, nil
}

func Example_Measurement_GetMode_Value() {

Expand All @@ -100,7 +147,7 @@ func Example_Measurement_GetMode_Value() {

// 2.- MOCK SERVER SETUP

s := &mock.SnmpServer{
s := &MockSNMP{
Listen: "127.0.0.1:1161",
Want: []gosnmp.SnmpPDU{
{Name: ".1.1.1", Type: gosnmp.Integer, Value: int(51)},
Expand All @@ -114,30 +161,8 @@ func Example_Measurement_GetMode_Value() {
},
}

err := s.Start()
if err != nil {
l.Errorf("error on start snmp mock server: %s", err)
return
}
defer s.Stop()

// 3.- SNMP CLIENT SETUP

cli := &gosnmp.GoSNMP{
Target: "127.0.0.1",
Port: 1161,
Version: gosnmp.Version2c,
Community: "test1",
Timeout: 5 * time.Second,
Retries: 0,
Logger: l,
}
err = cli.Connect()
if err != nil {
l.Fatalf("Connect() err: %v", err)
}
defer cli.Conn.Close()

// 4.- METRICMAP SETUP

metrics := map[string]*config.SnmpMetricCfg{
Expand Down Expand Up @@ -191,7 +216,7 @@ func Example_Measurement_GetMode_Value() {

// 6.- MEASUREMENT ENGINE SETUP

m, err := New(cfg, l, cli, false)
m, err := New(cfg, l, s, false)
if err != nil {
l.Errorf("Can not create measurement %s", err)
return
Expand All @@ -215,6 +240,7 @@ func Example_Measurement_GetMode_Value() {

}

/*
func Example_Measurement_GetMode_Indexed() {

// 1.- SETUP LOGGER
Expand Down Expand Up @@ -3040,3 +3066,4 @@ func Example_Measurement_Indexed_Multi_Indirect_STRINGEVAL() {
// Measurement:interfaces_data Tags:{ portName:eth1 } Field:output ValueType:int64 Value:21

}
*/