2016-01-20 20:11:28 +00:00
|
|
|
package snmp
|
|
|
|
|
|
|
|
import (
|
2016-07-28 14:55:03 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
2016-01-20 20:11:28 +00:00
|
|
|
"testing"
|
2016-07-28 14:55:03 +00:00
|
|
|
"time"
|
2016-01-20 20:11:28 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2016-07-28 14:55:03 +00:00
|
|
|
"github.com/influxdata/toml"
|
|
|
|
"github.com/soniah/gosnmp"
|
2016-04-18 02:08:56 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-01-20 20:11:28 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
type testSNMPConnection struct {
|
|
|
|
host string
|
|
|
|
values map[string]interface{}
|
2016-01-20 20:11:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
func (tsc *testSNMPConnection) Host() string {
|
|
|
|
return tsc.host
|
2016-01-20 20:11:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
func (tsc *testSNMPConnection) Get(oids []string) (*gosnmp.SnmpPacket, error) {
|
|
|
|
sp := &gosnmp.SnmpPacket{}
|
|
|
|
for _, oid := range oids {
|
|
|
|
v, ok := tsc.values[oid]
|
|
|
|
if !ok {
|
|
|
|
sp.Variables = append(sp.Variables, gosnmp.SnmpPDU{
|
|
|
|
Name: oid,
|
|
|
|
Type: gosnmp.NoSuchObject,
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sp.Variables = append(sp.Variables, gosnmp.SnmpPDU{
|
|
|
|
Name: oid,
|
|
|
|
Value: v,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return sp, nil
|
|
|
|
}
|
2016-01-20 20:11:28 +00:00
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
func (tsc *testSNMPConnection) Walk(oid string, wf gosnmp.WalkFunc) error {
|
|
|
|
for void, v := range tsc.values {
|
|
|
|
if void == oid || (len(void) > len(oid) && void[:len(oid)+1] == oid+".") {
|
|
|
|
if err := wf(gosnmp.SnmpPDU{
|
|
|
|
Name: void,
|
|
|
|
Value: v,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2016-01-20 20:11:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
var tsc = &testSNMPConnection{
|
|
|
|
host: "tsc",
|
|
|
|
values: map[string]interface{}{
|
|
|
|
".1.2.3.0.0.1.0": "foo",
|
|
|
|
".1.2.3.0.0.1.1": []byte("bar"),
|
|
|
|
".1.2.3.0.0.102": "bad",
|
|
|
|
".1.2.3.0.0.2.0": 1,
|
|
|
|
".1.2.3.0.0.2.1": 2,
|
|
|
|
".1.2.3.0.0.3.0": "0.123",
|
|
|
|
".1.2.3.0.0.3.1": "0.456",
|
|
|
|
".1.2.3.0.0.3.2": "9.999",
|
|
|
|
".1.2.3.0.0.4.0": 123456,
|
|
|
|
".1.2.3.0.1": "baz",
|
|
|
|
".1.2.3.0.2": 234,
|
|
|
|
".1.2.3.0.3": []byte("byte slice"),
|
|
|
|
},
|
|
|
|
}
|
2016-01-20 20:11:28 +00:00
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
func TestSampleConfig(t *testing.T) {
|
|
|
|
conf := struct {
|
|
|
|
Inputs struct {
|
|
|
|
Snmp []*Snmp
|
|
|
|
}
|
|
|
|
}{}
|
|
|
|
err := toml.Unmarshal([]byte("[[inputs.snmp]]\n"+(*Snmp)(nil).SampleConfig()), &conf)
|
|
|
|
assert.NoError(t, err)
|
2016-01-20 20:11:28 +00:00
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
s := Snmp{
|
|
|
|
Agents: []string{"127.0.0.1:161"},
|
|
|
|
Version: 2,
|
|
|
|
Community: "public",
|
|
|
|
MaxRepetitions: 50,
|
|
|
|
|
|
|
|
Name: "system",
|
|
|
|
Fields: []Field{
|
|
|
|
{Name: "hostname", Oid: ".1.2.3.0.1.1"},
|
|
|
|
{Name: "uptime", Oid: ".1.2.3.0.1.200"},
|
2016-07-29 16:27:35 +00:00
|
|
|
{Name: "", Oid: ".1.2.3.0.1.201"},
|
2016-07-28 14:55:03 +00:00
|
|
|
},
|
|
|
|
Tables: []Table{
|
|
|
|
{
|
|
|
|
Name: "remote_servers",
|
|
|
|
InheritTags: []string{"hostname"},
|
|
|
|
Fields: []Field{
|
|
|
|
{Name: "server", Oid: ".1.2.3.0.0.0", IsTag: true},
|
|
|
|
{Name: "connections", Oid: ".1.2.3.0.0.1"},
|
|
|
|
{Name: "latency", Oid: ".1.2.3.0.0.2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, s, *conf.Inputs.Snmp[0])
|
2016-01-20 20:11:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
func TestGetSNMPConnection_v2(t *testing.T) {
|
|
|
|
s := &Snmp{
|
|
|
|
Timeout: "3s",
|
|
|
|
Retries: 4,
|
2016-01-20 20:11:28 +00:00
|
|
|
Version: 2,
|
2016-07-28 14:55:03 +00:00
|
|
|
Community: "foo",
|
2016-01-20 20:11:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
gsc, err := s.getConnection("1.2.3.4:567")
|
2016-01-20 20:11:28 +00:00
|
|
|
require.NoError(t, err)
|
2016-07-28 14:55:03 +00:00
|
|
|
gs := gsc.(gosnmpWrapper)
|
|
|
|
assert.Equal(t, "1.2.3.4", gs.Target)
|
|
|
|
assert.EqualValues(t, 567, gs.Port)
|
|
|
|
assert.Equal(t, gosnmp.Version2c, gs.Version)
|
|
|
|
assert.Equal(t, "foo", gs.Community)
|
2016-01-20 20:11:28 +00:00
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
gsc, err = s.getConnection("1.2.3.4")
|
|
|
|
require.NoError(t, err)
|
|
|
|
gs = gsc.(gosnmpWrapper)
|
|
|
|
assert.Equal(t, "1.2.3.4", gs.Target)
|
|
|
|
assert.EqualValues(t, 161, gs.Port)
|
2016-01-20 20:11:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
func TestGetSNMPConnection_v3(t *testing.T) {
|
|
|
|
s := &Snmp{
|
|
|
|
Version: 3,
|
|
|
|
MaxRepetitions: 20,
|
|
|
|
ContextName: "mycontext",
|
|
|
|
SecLevel: "authPriv",
|
|
|
|
SecName: "myuser",
|
|
|
|
AuthProtocol: "md5",
|
|
|
|
AuthPassword: "password123",
|
|
|
|
PrivProtocol: "des",
|
|
|
|
PrivPassword: "321drowssap",
|
|
|
|
EngineID: "myengineid",
|
|
|
|
EngineBoots: 1,
|
|
|
|
EngineTime: 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
gsc, err := s.getConnection("1.2.3.4")
|
2016-01-20 20:11:28 +00:00
|
|
|
require.NoError(t, err)
|
2016-07-28 14:55:03 +00:00
|
|
|
gs := gsc.(gosnmpWrapper)
|
|
|
|
assert.Equal(t, gs.Version, gosnmp.Version3)
|
|
|
|
sp := gs.SecurityParameters.(*gosnmp.UsmSecurityParameters)
|
|
|
|
assert.Equal(t, "1.2.3.4", gsc.Host())
|
|
|
|
assert.Equal(t, 20, gs.MaxRepetitions)
|
|
|
|
assert.Equal(t, "mycontext", gs.ContextName)
|
|
|
|
assert.Equal(t, gosnmp.AuthPriv, gs.MsgFlags&gosnmp.AuthPriv)
|
|
|
|
assert.Equal(t, "myuser", sp.UserName)
|
|
|
|
assert.Equal(t, gosnmp.MD5, sp.AuthenticationProtocol)
|
|
|
|
assert.Equal(t, "password123", sp.AuthenticationPassphrase)
|
|
|
|
assert.Equal(t, gosnmp.DES, sp.PrivacyProtocol)
|
|
|
|
assert.Equal(t, "321drowssap", sp.PrivacyPassphrase)
|
|
|
|
assert.Equal(t, "myengineid", sp.AuthoritativeEngineID)
|
|
|
|
assert.EqualValues(t, 1, sp.AuthoritativeEngineBoots)
|
|
|
|
assert.EqualValues(t, 2, sp.AuthoritativeEngineTime)
|
2016-01-20 20:11:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
func TestGetSNMPConnection_caching(t *testing.T) {
|
|
|
|
s := &Snmp{}
|
|
|
|
gs1, err := s.getConnection("1.2.3.4")
|
2016-01-20 20:11:28 +00:00
|
|
|
require.NoError(t, err)
|
2016-07-28 14:55:03 +00:00
|
|
|
gs2, err := s.getConnection("1.2.3.4")
|
|
|
|
require.NoError(t, err)
|
|
|
|
gs3, err := s.getConnection("1.2.3.5")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, gs1 == gs2)
|
|
|
|
assert.False(t, gs2 == gs3)
|
2016-01-20 20:11:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
func TestGosnmpWrapper_walk_retry(t *testing.T) {
|
|
|
|
srvr, err := net.ListenUDP("udp4", &net.UDPAddr{})
|
|
|
|
defer srvr.Close()
|
2016-01-20 20:11:28 +00:00
|
|
|
require.NoError(t, err)
|
2016-07-28 14:55:03 +00:00
|
|
|
reqCount := 0
|
|
|
|
// Set up a WaitGroup to wait for the server goroutine to exit and protect
|
|
|
|
// reqCount.
|
|
|
|
// Even though simultaneous access is impossible because the server will be
|
|
|
|
// blocked on ReadFrom, without this the race detector gets unhappy.
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
buf := make([]byte, 256)
|
|
|
|
for {
|
|
|
|
_, addr, err := srvr.ReadFrom(buf)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reqCount++
|
|
|
|
|
|
|
|
srvr.WriteTo([]byte{'X'}, addr) // will cause decoding error
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
gs := &gosnmp.GoSNMP{
|
|
|
|
Target: srvr.LocalAddr().(*net.UDPAddr).IP.String(),
|
|
|
|
Port: uint16(srvr.LocalAddr().(*net.UDPAddr).Port),
|
|
|
|
Version: gosnmp.Version2c,
|
|
|
|
Community: "public",
|
|
|
|
Timeout: time.Millisecond * 10,
|
|
|
|
Retries: 1,
|
|
|
|
}
|
|
|
|
err = gs.Connect()
|
|
|
|
require.NoError(t, err)
|
|
|
|
conn := gs.Conn
|
|
|
|
|
|
|
|
gsw := gosnmpWrapper{gs}
|
|
|
|
err = gsw.Walk(".1.2.3", func(_ gosnmp.SnmpPDU) error { return nil })
|
|
|
|
srvr.Close()
|
|
|
|
wg.Wait()
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.False(t, gs.Conn == conn)
|
|
|
|
assert.Equal(t, (gs.Retries+1)*2, reqCount)
|
|
|
|
}
|
2016-01-20 20:11:28 +00:00
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
func TestGosnmpWrapper_get_retry(t *testing.T) {
|
|
|
|
srvr, err := net.ListenUDP("udp4", &net.UDPAddr{})
|
|
|
|
defer srvr.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
reqCount := 0
|
|
|
|
// Set up a WaitGroup to wait for the server goroutine to exit and protect
|
|
|
|
// reqCount.
|
|
|
|
// Even though simultaneous access is impossible because the server will be
|
|
|
|
// blocked on ReadFrom, without this the race detector gets unhappy.
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
buf := make([]byte, 256)
|
|
|
|
for {
|
|
|
|
_, addr, err := srvr.ReadFrom(buf)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reqCount++
|
|
|
|
|
|
|
|
srvr.WriteTo([]byte{'X'}, addr) // will cause decoding error
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
gs := &gosnmp.GoSNMP{
|
|
|
|
Target: srvr.LocalAddr().(*net.UDPAddr).IP.String(),
|
|
|
|
Port: uint16(srvr.LocalAddr().(*net.UDPAddr).Port),
|
|
|
|
Version: gosnmp.Version2c,
|
|
|
|
Community: "public",
|
|
|
|
Timeout: time.Millisecond * 10,
|
|
|
|
Retries: 1,
|
|
|
|
}
|
|
|
|
err = gs.Connect()
|
|
|
|
require.NoError(t, err)
|
|
|
|
conn := gs.Conn
|
|
|
|
|
|
|
|
gsw := gosnmpWrapper{gs}
|
|
|
|
_, err = gsw.Get([]string{".1.2.3"})
|
|
|
|
srvr.Close()
|
|
|
|
wg.Wait()
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.False(t, gs.Conn == conn)
|
|
|
|
assert.Equal(t, (gs.Retries+1)*2, reqCount)
|
2016-01-20 20:11:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
func TestTableBuild_walk(t *testing.T) {
|
|
|
|
tbl := Table{
|
|
|
|
Name: "mytable",
|
|
|
|
Fields: []Field{
|
|
|
|
{
|
|
|
|
Name: "myfield1",
|
|
|
|
Oid: ".1.2.3.0.0.1",
|
|
|
|
IsTag: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "myfield2",
|
|
|
|
Oid: ".1.2.3.0.0.2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "myfield3",
|
|
|
|
Oid: ".1.2.3.0.0.3",
|
|
|
|
Conversion: "float",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tb, err := tbl.Build(tsc, true)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tb.Name, "mytable")
|
|
|
|
rtr1 := RTableRow{
|
|
|
|
Tags: map[string]string{"myfield1": "foo"},
|
|
|
|
Fields: map[string]interface{}{"myfield2": 1, "myfield3": float64(0.123)},
|
2016-01-20 20:11:28 +00:00
|
|
|
}
|
2016-07-28 14:55:03 +00:00
|
|
|
rtr2 := RTableRow{
|
|
|
|
Tags: map[string]string{"myfield1": "bar"},
|
|
|
|
Fields: map[string]interface{}{"myfield2": 2, "myfield3": float64(0.456)},
|
2016-01-20 20:11:28 +00:00
|
|
|
}
|
2016-07-28 14:55:03 +00:00
|
|
|
assert.Len(t, tb.Rows, 2)
|
|
|
|
assert.Contains(t, tb.Rows, rtr1)
|
|
|
|
assert.Contains(t, tb.Rows, rtr2)
|
|
|
|
}
|
2016-01-20 20:11:28 +00:00
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
func TestTableBuild_noWalk(t *testing.T) {
|
|
|
|
tbl := Table{
|
|
|
|
Name: "mytable",
|
|
|
|
Fields: []Field{
|
|
|
|
{
|
|
|
|
Name: "myfield1",
|
|
|
|
Oid: ".1.2.3.0.1",
|
|
|
|
IsTag: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "myfield2",
|
|
|
|
Oid: ".1.2.3.0.2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "myfield3",
|
|
|
|
Oid: ".1.2.3.0.2",
|
|
|
|
IsTag: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tb, err := tbl.Build(tsc, false)
|
2016-01-20 20:11:28 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
rtr := RTableRow{
|
|
|
|
Tags: map[string]string{"myfield1": "baz", "myfield3": "234"},
|
|
|
|
Fields: map[string]interface{}{"myfield2": 234},
|
2016-01-20 20:11:28 +00:00
|
|
|
}
|
2016-07-28 14:55:03 +00:00
|
|
|
assert.Len(t, tb.Rows, 1)
|
|
|
|
assert.Contains(t, tb.Rows, rtr)
|
|
|
|
}
|
2016-01-20 20:11:28 +00:00
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
func TestGather(t *testing.T) {
|
|
|
|
s := &Snmp{
|
|
|
|
Agents: []string{"TestGather"},
|
|
|
|
Name: "mytable",
|
|
|
|
Fields: []Field{
|
|
|
|
{
|
|
|
|
Name: "myfield1",
|
|
|
|
Oid: ".1.2.3.0.1",
|
|
|
|
IsTag: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "myfield2",
|
|
|
|
Oid: ".1.2.3.0.2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "myfield3",
|
|
|
|
Oid: "1.2.3.0.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Tables: []Table{
|
|
|
|
{
|
|
|
|
Name: "myOtherTable",
|
|
|
|
InheritTags: []string{"myfield1"},
|
|
|
|
Fields: []Field{
|
|
|
|
{
|
|
|
|
Name: "myOtherField",
|
|
|
|
Oid: ".1.2.3.0.0.4",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
connectionCache: map[string]snmpConnection{
|
|
|
|
"TestGather": tsc,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := &testutil.Accumulator{}
|
|
|
|
|
|
|
|
tstart := time.Now()
|
|
|
|
s.Gather(acc)
|
|
|
|
tstop := time.Now()
|
|
|
|
|
|
|
|
require.Len(t, acc.Metrics, 2)
|
|
|
|
|
|
|
|
m := acc.Metrics[0]
|
|
|
|
assert.Equal(t, "mytable", m.Measurement)
|
|
|
|
assert.Equal(t, "tsc", m.Tags["agent_host"])
|
|
|
|
assert.Equal(t, "baz", m.Tags["myfield1"])
|
|
|
|
assert.Len(t, m.Fields, 2)
|
|
|
|
assert.Equal(t, 234, m.Fields["myfield2"])
|
|
|
|
assert.Equal(t, "baz", m.Fields["myfield3"])
|
|
|
|
assert.True(t, tstart.Before(m.Time))
|
|
|
|
assert.True(t, tstop.After(m.Time))
|
|
|
|
|
|
|
|
m2 := acc.Metrics[1]
|
|
|
|
assert.Equal(t, "myOtherTable", m2.Measurement)
|
|
|
|
assert.Equal(t, "tsc", m2.Tags["agent_host"])
|
|
|
|
assert.Equal(t, "baz", m2.Tags["myfield1"])
|
|
|
|
assert.Len(t, m2.Fields, 1)
|
|
|
|
assert.Equal(t, 123456, m2.Fields["myOtherField"])
|
|
|
|
}
|
2016-01-20 20:11:28 +00:00
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
func TestGather_host(t *testing.T) {
|
|
|
|
s := &Snmp{
|
|
|
|
Agents: []string{"TestGather"},
|
|
|
|
Name: "mytable",
|
|
|
|
Fields: []Field{
|
|
|
|
{
|
|
|
|
Name: "host",
|
|
|
|
Oid: ".1.2.3.0.1",
|
|
|
|
IsTag: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "myfield2",
|
|
|
|
Oid: ".1.2.3.0.2",
|
|
|
|
},
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
connectionCache: map[string]snmpConnection{
|
|
|
|
"TestGather": tsc,
|
2016-01-20 20:11:28 +00:00
|
|
|
},
|
2016-07-28 14:55:03 +00:00
|
|
|
}
|
2016-01-20 20:11:28 +00:00
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
acc := &testutil.Accumulator{}
|
2016-01-20 20:11:28 +00:00
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
s.Gather(acc)
|
|
|
|
|
|
|
|
require.Len(t, acc.Metrics, 1)
|
|
|
|
m := acc.Metrics[0]
|
|
|
|
assert.Equal(t, "baz", m.Tags["host"])
|
2016-01-20 20:11:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
func TestFieldConvert(t *testing.T) {
|
|
|
|
testTable := []struct {
|
|
|
|
input interface{}
|
|
|
|
conv string
|
|
|
|
expected interface{}
|
|
|
|
}{
|
|
|
|
{[]byte("foo"), "", string("foo")},
|
|
|
|
{"0.123", "float", float64(0.123)},
|
|
|
|
{[]byte("0.123"), "float", float64(0.123)},
|
|
|
|
{float32(0.123), "float", float64(float32(0.123))},
|
|
|
|
{float64(0.123), "float", float64(0.123)},
|
|
|
|
{123, "float", float64(123)},
|
|
|
|
{123, "float(0)", float64(123)},
|
|
|
|
{123, "float(4)", float64(0.0123)},
|
|
|
|
{int8(123), "float(3)", float64(0.123)},
|
|
|
|
{int16(123), "float(3)", float64(0.123)},
|
|
|
|
{int32(123), "float(3)", float64(0.123)},
|
|
|
|
{int64(123), "float(3)", float64(0.123)},
|
|
|
|
{uint(123), "float(3)", float64(0.123)},
|
|
|
|
{uint8(123), "float(3)", float64(0.123)},
|
|
|
|
{uint16(123), "float(3)", float64(0.123)},
|
|
|
|
{uint32(123), "float(3)", float64(0.123)},
|
|
|
|
{uint64(123), "float(3)", float64(0.123)},
|
|
|
|
{"123", "int", int64(123)},
|
|
|
|
{[]byte("123"), "int", int64(123)},
|
|
|
|
{float32(12.3), "int", int64(12)},
|
|
|
|
{float64(12.3), "int", int64(12)},
|
|
|
|
{int(123), "int", int64(123)},
|
|
|
|
{int8(123), "int", int64(123)},
|
|
|
|
{int16(123), "int", int64(123)},
|
|
|
|
{int32(123), "int", int64(123)},
|
|
|
|
{int64(123), "int", int64(123)},
|
|
|
|
{uint(123), "int", int64(123)},
|
|
|
|
{uint8(123), "int", int64(123)},
|
|
|
|
{uint16(123), "int", int64(123)},
|
|
|
|
{uint32(123), "int", int64(123)},
|
|
|
|
{uint64(123), "int", int64(123)},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testTable {
|
|
|
|
act := fieldConvert(tc.conv, tc.input)
|
|
|
|
assert.EqualValues(t, tc.expected, act, "input=%T(%v) conv=%s expected=%T(%v)", tc.input, tc.input, tc.conv, tc.expected, tc.expected)
|
2016-01-20 20:11:28 +00:00
|
|
|
}
|
2016-07-28 14:55:03 +00:00
|
|
|
}
|
2016-01-20 20:11:28 +00:00
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
func TestError(t *testing.T) {
|
|
|
|
e := fmt.Errorf("nested error")
|
|
|
|
err := Errorf(e, "top error %d", 123)
|
|
|
|
require.Error(t, err)
|
2016-01-20 20:11:28 +00:00
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
ne, ok := err.(NestedError)
|
|
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, e, ne.NestedErr)
|
2016-01-20 20:11:28 +00:00
|
|
|
|
2016-07-28 14:55:03 +00:00
|
|
|
assert.Contains(t, err.Error(), "top error 123")
|
|
|
|
assert.Contains(t, err.Error(), "nested error")
|
2016-01-20 20:11:28 +00:00
|
|
|
}
|