2017-04-10 18:54:12 +02:00
|
|
|
package influxdb
|
2017-01-20 23:23:31 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-01-15 20:19:55 +01:00
|
|
|
"github.com/influxdata/influxdb1-client/v2"
|
2017-01-20 23:23:31 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-03-03 16:19:35 +01:00
|
|
|
|
|
|
|
"github.com/FreifunkBremen/yanic/data"
|
2017-04-10 18:54:12 +02:00
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
2017-01-20 23:23:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestToInflux(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2017-04-10 18:54:12 +02:00
|
|
|
node := &runtime.Node{
|
2017-01-20 23:23:31 +01:00
|
|
|
Statistics: &data.Statistics{
|
2017-09-27 13:55:02 +02:00
|
|
|
NodeID: "deadbeef",
|
2017-01-20 23:23:31 +01:00
|
|
|
LoadAverage: 0.5,
|
2018-05-06 00:11:01 +02:00
|
|
|
ProcStats: &data.ProcStats{
|
|
|
|
CPU: data.ProcStatsCPU{
|
|
|
|
User: 1,
|
|
|
|
},
|
|
|
|
ContextSwitches: 3,
|
|
|
|
},
|
2017-01-20 23:23:31 +01:00
|
|
|
Wireless: data.WirelessStatistics{
|
|
|
|
&data.WirelessAirtime{Frequency: 5500},
|
|
|
|
},
|
|
|
|
Traffic: struct {
|
|
|
|
Tx *data.Traffic `json:"tx"`
|
|
|
|
Rx *data.Traffic `json:"rx"`
|
|
|
|
Forward *data.Traffic `json:"forward"`
|
|
|
|
MgmtTx *data.Traffic `json:"mgmt_tx"`
|
|
|
|
MgmtRx *data.Traffic `json:"mgmt_rx"`
|
|
|
|
}{
|
|
|
|
Tx: &data.Traffic{Dropped: 1321},
|
|
|
|
Rx: &data.Traffic{Bytes: 1213},
|
|
|
|
Forward: &data.Traffic{Bytes: 1322},
|
|
|
|
MgmtTx: &data.Traffic{Packets: 2327},
|
|
|
|
MgmtRx: &data.Traffic{Bytes: 2331},
|
|
|
|
},
|
|
|
|
MeshVPN: &data.MeshVPN{
|
|
|
|
Groups: map[string]*data.MeshVPNPeerGroup{
|
2018-01-13 16:40:23 +01:00
|
|
|
"ffhb": {
|
2017-01-20 23:23:31 +01:00
|
|
|
Peers: map[string]*data.MeshVPNPeerLink{
|
2018-01-13 16:40:23 +01:00
|
|
|
"vpn01": {Established: 3},
|
|
|
|
"vpn02": {},
|
2017-01-21 04:24:04 +01:00
|
|
|
"trash": nil,
|
2018-01-13 16:40:23 +01:00
|
|
|
"vpn03": {Established: 0},
|
2017-01-20 23:23:31 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Nodeinfo: &data.NodeInfo{
|
2017-09-27 13:55:02 +02:00
|
|
|
NodeID: "deadbeef",
|
2017-01-20 23:23:31 +01:00
|
|
|
Owner: &data.Owner{
|
|
|
|
Contact: "nobody",
|
|
|
|
},
|
2017-11-20 12:22:52 +01:00
|
|
|
System: data.System{
|
2018-01-17 20:20:35 +01:00
|
|
|
SiteCode: "ffhb",
|
|
|
|
DomainCode: "city",
|
2017-11-20 12:22:52 +01:00
|
|
|
},
|
2017-01-20 23:23:31 +01:00
|
|
|
Wireless: &data.Wireless{
|
|
|
|
TxPower24: 3,
|
|
|
|
Channel24: 4,
|
|
|
|
},
|
2017-09-27 13:55:02 +02:00
|
|
|
Network: data.Network{
|
|
|
|
Mac: "DEADMAC",
|
|
|
|
},
|
2017-11-14 19:33:50 +01:00
|
|
|
Software: data.Software{
|
|
|
|
Autoupdater: struct {
|
|
|
|
Enabled bool `json:"enabled,omitempty"`
|
|
|
|
Branch string `json:"branch,omitempty"`
|
|
|
|
}{
|
|
|
|
Enabled: true,
|
|
|
|
Branch: "testing",
|
|
|
|
},
|
|
|
|
},
|
2017-01-20 23:23:31 +01:00
|
|
|
},
|
|
|
|
Neighbours: &data.Neighbours{
|
2017-09-27 13:55:02 +02:00
|
|
|
NodeID: "deadbeef",
|
2017-01-20 23:23:31 +01:00
|
|
|
Batadv: map[string]data.BatadvNeighbours{
|
2018-01-13 16:40:23 +01:00
|
|
|
"a-interface": {
|
2017-01-20 23:23:31 +01:00
|
|
|
Neighbours: map[string]data.BatmanLink{
|
2018-01-13 16:40:23 +01:00
|
|
|
"BAFF1E5": {
|
2017-09-27 13:55:02 +02:00
|
|
|
Tq: 204,
|
|
|
|
},
|
2017-01-20 23:23:31 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-12-31 05:26:17 +01:00
|
|
|
LLDP: map[string]data.LLDPNeighbours{
|
2018-01-13 16:40:23 +01:00
|
|
|
"b-interface": {},
|
2017-12-31 05:26:17 +01:00
|
|
|
},
|
2017-01-20 23:23:31 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-01-13 16:54:48 +01:00
|
|
|
neighbour := &runtime.Node{
|
2017-09-27 13:55:02 +02:00
|
|
|
Nodeinfo: &data.NodeInfo{
|
|
|
|
NodeID: "foobar",
|
|
|
|
Network: data.Network{
|
|
|
|
Mac: "BAFF1E5",
|
|
|
|
},
|
2017-11-14 19:33:50 +01:00
|
|
|
Software: data.Software{
|
|
|
|
Autoupdater: struct {
|
|
|
|
Enabled bool `json:"enabled,omitempty"`
|
|
|
|
Branch string `json:"branch,omitempty"`
|
|
|
|
}{
|
|
|
|
Enabled: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Statistics: &data.Statistics{
|
|
|
|
NodeID: "foobar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not add a empty statistics of a node
|
|
|
|
droppednode := &runtime.Node{
|
|
|
|
Nodeinfo: &data.NodeInfo{
|
|
|
|
NodeID: "notfound",
|
|
|
|
Network: data.Network{
|
|
|
|
Mac: "instats",
|
|
|
|
},
|
2017-09-27 13:55:02 +02:00
|
|
|
},
|
|
|
|
Statistics: &data.Statistics{},
|
|
|
|
}
|
|
|
|
|
2018-01-13 16:54:48 +01:00
|
|
|
points := testPoints(node, neighbour, droppednode)
|
2017-09-27 13:55:02 +02:00
|
|
|
var fields map[string]interface{}
|
|
|
|
var tags map[string]string
|
|
|
|
|
2017-11-14 19:33:50 +01:00
|
|
|
assert.Len(points, 3)
|
2017-09-27 13:55:02 +02:00
|
|
|
|
|
|
|
// first point contains the neighbour
|
|
|
|
sPoint := points[0]
|
|
|
|
tags = sPoint.Tags()
|
|
|
|
fields, _ = sPoint.Fields()
|
|
|
|
|
|
|
|
assert.EqualValues("deadbeef", tags["nodeid"])
|
|
|
|
assert.EqualValues("nobody", tags["owner"])
|
2017-11-14 19:33:50 +01:00
|
|
|
assert.EqualValues("testing", tags["autoupdater"])
|
2018-01-17 20:20:35 +01:00
|
|
|
assert.EqualValues("ffhb", tags["site"])
|
|
|
|
assert.EqualValues("city", tags["domain"])
|
2017-09-27 13:55:02 +02:00
|
|
|
assert.EqualValues(0.5, fields["load"])
|
|
|
|
assert.EqualValues(0, fields["neighbours.lldp"])
|
|
|
|
assert.EqualValues(1, fields["neighbours.batadv"])
|
|
|
|
assert.EqualValues(1, fields["neighbours.vpn"])
|
|
|
|
assert.EqualValues(1, fields["neighbours.total"])
|
|
|
|
|
|
|
|
assert.EqualValues(uint32(3), fields["wireless.txpower24"])
|
|
|
|
assert.EqualValues(uint32(5500), fields["airtime11a.frequency"])
|
|
|
|
assert.EqualValues("", tags["frequency5500"])
|
|
|
|
|
|
|
|
assert.EqualValues(int64(1213), fields["traffic.rx.bytes"])
|
|
|
|
assert.EqualValues(float64(1321), fields["traffic.tx.dropped"])
|
|
|
|
assert.EqualValues(int64(1322), fields["traffic.forward.bytes"])
|
|
|
|
assert.EqualValues(int64(2331), fields["traffic.mgmt_rx.bytes"])
|
|
|
|
assert.EqualValues(float64(2327), fields["traffic.mgmt_tx.packets"])
|
|
|
|
|
2017-11-14 19:33:50 +01:00
|
|
|
// second point contains the link
|
2017-09-27 13:55:02 +02:00
|
|
|
nPoint := points[1]
|
|
|
|
tags = nPoint.Tags()
|
|
|
|
fields, _ = nPoint.Fields()
|
|
|
|
assert.EqualValues("link", nPoint.Name())
|
|
|
|
assert.EqualValues(map[string]string{
|
2017-12-05 23:17:49 +01:00
|
|
|
"source.id": "deadbeef",
|
|
|
|
"source.addr": "a-interface",
|
|
|
|
"target.id": "foobar",
|
|
|
|
"target.addr": "BAFF1E5",
|
2017-09-27 13:55:02 +02:00
|
|
|
}, tags)
|
|
|
|
assert.EqualValues(80, fields["tq"])
|
2017-11-14 19:33:50 +01:00
|
|
|
|
|
|
|
// third point contains the neighbour
|
|
|
|
nPoint = points[2]
|
|
|
|
tags = nPoint.Tags()
|
|
|
|
assert.EqualValues("disabled", tags["autoupdater"])
|
2017-09-27 13:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Processes data and returns the InfluxDB points
|
|
|
|
func testPoints(nodes ...*runtime.Node) (points []*client.Point) {
|
|
|
|
// Create dummy client
|
|
|
|
influxClient, err := client.NewHTTPClient(client.HTTPConfig{Addr: "http://127.0.0.1"})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-01-07 21:00:56 +01:00
|
|
|
nodesList := runtime.NewNodes(&runtime.NodesConfig{})
|
2017-09-27 13:55:02 +02:00
|
|
|
|
|
|
|
// Create dummy connection
|
|
|
|
conn := &Connection{
|
|
|
|
points: make(chan *client.Point),
|
|
|
|
client: influxClient,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range nodes {
|
2017-05-20 14:46:29 +02:00
|
|
|
nodesList.AddNode(node)
|
2017-09-27 13:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process data
|
|
|
|
go func() {
|
|
|
|
for _, node := range nodes {
|
|
|
|
conn.InsertNode(node)
|
|
|
|
if node.Neighbours != nil {
|
|
|
|
for _, link := range nodesList.NodeLinks(node) {
|
|
|
|
conn.InsertLink(&link, node.Lastseen.GetTime())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Read points
|
|
|
|
for point := range conn.points {
|
|
|
|
points = append(points, point)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2017-01-20 23:23:31 +01:00
|
|
|
}
|