2017-04-10 18:54:12 +02:00
|
|
|
package runtime
|
2016-03-20 18:30:44 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2016-10-08 11:52:11 +02:00
|
|
|
"time"
|
2016-03-20 18:30:44 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-03-03 16:19:35 +01:00
|
|
|
|
|
|
|
"github.com/FreifunkBremen/yanic/data"
|
2018-01-07 21:00:56 +01:00
|
|
|
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
2016-03-20 18:30:44 +01:00
|
|
|
)
|
|
|
|
|
2016-10-08 11:52:11 +02:00
|
|
|
func TestExpire(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2018-01-07 21:00:56 +01:00
|
|
|
config := &NodesConfig{}
|
|
|
|
config.OfflineAfter.Duration = time.Minute * 10
|
2017-10-12 15:25:00 +02:00
|
|
|
// to get default (100%) path of testing
|
2018-01-07 21:00:56 +01:00
|
|
|
// config.PruneAfter.Duration = time.Hour * 24 * 6
|
2016-10-08 11:52:11 +02:00
|
|
|
nodes := &Nodes{
|
2017-09-27 13:55:02 +02:00
|
|
|
config: config,
|
|
|
|
List: make(map[string]*Node),
|
|
|
|
ifaceToNodeID: make(map[string]string),
|
2016-10-08 11:52:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nodes.Update("expire", &data.ResponseData{}) // should expire
|
|
|
|
nodes.Update("offline", &data.ResponseData{}) // should become offline
|
|
|
|
nodes.Update("online", &data.ResponseData{}) // should stay online
|
|
|
|
|
|
|
|
expire := nodes.List["expire"]
|
2017-10-12 15:25:00 +02:00
|
|
|
expire.Lastseen = expire.Lastseen.Add((-7 * time.Hour * 24) - time.Minute)
|
2016-10-08 11:52:11 +02:00
|
|
|
offline := nodes.List["offline"]
|
2017-10-12 15:25:00 +02:00
|
|
|
offline.Lastseen = offline.Lastseen.Add((-7 * time.Hour * 24) + time.Minute)
|
2016-10-08 11:52:11 +02:00
|
|
|
|
|
|
|
nodes.expire()
|
|
|
|
|
|
|
|
// one expired?
|
2017-03-06 09:52:39 +01:00
|
|
|
assert.Len(nodes.List, 2)
|
2016-10-08 11:52:11 +02:00
|
|
|
assert.Nil(nodes.List["expire"])
|
|
|
|
|
|
|
|
// one offline?
|
|
|
|
assert.NotNil(nodes.List["offline"])
|
2017-04-10 18:54:12 +02:00
|
|
|
assert.False(nodes.List["offline"].Online)
|
2016-10-08 11:52:11 +02:00
|
|
|
|
|
|
|
// one online?
|
|
|
|
assert.NotNil(nodes.List["online"])
|
2017-04-10 18:54:12 +02:00
|
|
|
assert.True(nodes.List["online"].Online)
|
2016-10-08 11:52:11 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 18:30:44 +01:00
|
|
|
func TestLoadAndSave(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2018-01-07 21:00:56 +01:00
|
|
|
config := &NodesConfig{}
|
2017-10-12 15:25:00 +02:00
|
|
|
// not autoload without StatePath
|
|
|
|
NewNodes(config)
|
2016-03-20 18:30:44 +01:00
|
|
|
|
2019-05-19 10:22:56 +02:00
|
|
|
// Test unmarshalable
|
|
|
|
config.StatePath = "testdata/nodes-broken.json"
|
2016-11-20 18:45:18 +01:00
|
|
|
nodes := NewNodes(config)
|
2017-10-12 15:25:00 +02:00
|
|
|
// Test unopen able
|
2018-01-07 21:00:56 +01:00
|
|
|
config.StatePath = "/root/nodes.json"
|
2017-10-12 15:25:00 +02:00
|
|
|
nodes.load()
|
|
|
|
// works ;)
|
2018-01-07 21:00:56 +01:00
|
|
|
config.StatePath = "testdata/nodes.json"
|
2016-03-20 18:30:44 +01:00
|
|
|
nodes.load()
|
|
|
|
|
|
|
|
tmpfile, _ := ioutil.TempFile("/tmp", "nodes")
|
2018-01-07 21:00:56 +01:00
|
|
|
config.StatePath = tmpfile.Name()
|
2017-10-12 15:25:00 +02:00
|
|
|
nodes.save()
|
2016-03-20 18:30:44 +01:00
|
|
|
os.Remove(tmpfile.Name())
|
|
|
|
|
2019-01-17 13:26:16 +01:00
|
|
|
assert.Panics(func() {
|
2019-05-19 10:22:56 +02:00
|
|
|
SaveJSON(nodes, "/proc/a")
|
|
|
|
// "open /proc/a.tmp: permission denied",
|
2017-10-12 15:25:00 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
tmpfile, _ = ioutil.TempFile("/tmp", "nodes")
|
2019-01-17 13:26:16 +01:00
|
|
|
assert.Panics(func() {
|
2017-10-12 15:25:00 +02:00
|
|
|
SaveJSON(tmpfile.Name, tmpfile.Name())
|
2019-01-17 13:26:16 +01:00
|
|
|
// "json: unsupported type: func() string",
|
2017-10-12 15:25:00 +02:00
|
|
|
})
|
|
|
|
os.Remove(tmpfile.Name())
|
|
|
|
|
|
|
|
//TODO how to test easy a failing renaming
|
|
|
|
|
|
|
|
assert.Len(nodes.List, 2)
|
2016-03-20 18:30:44 +01:00
|
|
|
}
|
2016-03-21 12:26:08 +01:00
|
|
|
|
|
|
|
func TestUpdateNodes(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2017-09-27 13:55:02 +02:00
|
|
|
nodes := &Nodes{
|
|
|
|
List: make(map[string]*Node),
|
|
|
|
ifaceToNodeID: make(map[string]string),
|
|
|
|
}
|
2017-03-06 09:52:39 +01:00
|
|
|
assert.Len(nodes.List, 0)
|
2016-03-21 12:26:08 +01:00
|
|
|
|
|
|
|
res := &data.ResponseData{
|
|
|
|
Neighbours: &data.Neighbours{},
|
2017-10-12 15:25:00 +02:00
|
|
|
Statistics: &data.Statistics{
|
|
|
|
Wireless: data.WirelessStatistics{
|
|
|
|
&data.WirelessAirtime{},
|
|
|
|
},
|
|
|
|
},
|
2019-01-24 02:56:13 +01:00
|
|
|
Nodeinfo: &data.Nodeinfo{},
|
2016-03-21 12:26:08 +01:00
|
|
|
}
|
|
|
|
nodes.Update("abcdef012345", res)
|
|
|
|
|
2017-10-12 15:25:00 +02:00
|
|
|
// Update wireless statistics by running SetUtilization
|
|
|
|
nodes.Update("abcdef012345", res)
|
|
|
|
|
2017-03-06 09:52:39 +01:00
|
|
|
assert.Len(nodes.List, 1)
|
2016-03-21 12:26:08 +01:00
|
|
|
}
|
2017-10-12 15:25:00 +02:00
|
|
|
|
|
|
|
func TestSelectNodes(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2018-01-07 21:00:56 +01:00
|
|
|
config := &NodesConfig{}
|
|
|
|
config.StatePath = "testdata/nodes.json"
|
2017-10-12 15:25:00 +02:00
|
|
|
|
|
|
|
nodes := NewNodes(config)
|
|
|
|
|
|
|
|
selectedNodes := nodes.Select(func(n *Node) bool {
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
assert.Len(selectedNodes, 2)
|
|
|
|
|
|
|
|
selectedNodes = nodes.Select(func(n *Node) bool {
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
assert.Len(selectedNodes, 0)
|
|
|
|
|
|
|
|
selectedNodes = nodes.Select(func(n *Node) bool {
|
|
|
|
return n.Nodeinfo.NodeID == "f4f26dd7a30a"
|
|
|
|
})
|
|
|
|
assert.Len(selectedNodes, 1)
|
|
|
|
time := jsontime.Time{}
|
2022-03-28 03:56:00 +02:00
|
|
|
assert.NoError(time.UnmarshalJSON([]byte("\"2017-03-10T12:12:01\"")))
|
2017-10-12 15:25:00 +02:00
|
|
|
assert.Equal(time, selectedNodes[0].Firstseen)
|
|
|
|
}
|
|
|
|
|
2017-05-20 14:46:29 +02:00
|
|
|
func TestAddNode(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2018-01-07 21:00:56 +01:00
|
|
|
nodes := NewNodes(&NodesConfig{})
|
2017-05-20 14:46:29 +02:00
|
|
|
|
|
|
|
nodes.AddNode(&Node{})
|
|
|
|
assert.Len(nodes.List, 0)
|
|
|
|
|
2019-01-24 02:56:13 +01:00
|
|
|
nodes.AddNode(&Node{Nodeinfo: &data.Nodeinfo{}})
|
2017-05-20 14:46:29 +02:00
|
|
|
assert.Len(nodes.List, 0)
|
|
|
|
|
2019-01-24 02:56:13 +01:00
|
|
|
nodes.AddNode(&Node{Nodeinfo: &data.Nodeinfo{NodeID: "blub"}})
|
2017-05-20 14:46:29 +02:00
|
|
|
assert.Len(nodes.List, 1)
|
|
|
|
}
|
|
|
|
|
2017-10-12 15:25:00 +02:00
|
|
|
func TestLinksNodes(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
nodes := &Nodes{
|
|
|
|
List: make(map[string]*Node),
|
|
|
|
ifaceToNodeID: make(map[string]string),
|
|
|
|
}
|
|
|
|
assert.Len(nodes.List, 0)
|
|
|
|
|
2019-05-19 10:22:56 +02:00
|
|
|
nodes.Update("f4f26dd7a300", &data.ResponseData{
|
|
|
|
Nodeinfo: &data.Nodeinfo{
|
|
|
|
NodeID: "f4f26dd7a300",
|
|
|
|
Network: data.Network{
|
|
|
|
Mac: "f4:f2:6d:d7:a3:00",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2017-10-12 15:25:00 +02:00
|
|
|
nodes.Update("f4f26dd7a30a", &data.ResponseData{
|
2019-01-24 02:56:13 +01:00
|
|
|
Nodeinfo: &data.Nodeinfo{
|
2017-10-12 15:25:00 +02:00
|
|
|
NodeID: "f4f26dd7a30a",
|
|
|
|
Network: data.Network{
|
|
|
|
Mac: "f4:f2:6d:d7:a3:0a",
|
|
|
|
},
|
|
|
|
},
|
2019-05-19 10:22:56 +02:00
|
|
|
Neighbours: &data.Neighbours{
|
|
|
|
NodeID: "f4f26dd7a30a",
|
|
|
|
Babel: map[string]data.BabelNeighbours{
|
|
|
|
"vx_mesh_lan": {
|
|
|
|
LinkLocalAddress: "fe80::2",
|
|
|
|
Neighbours: map[string]data.BabelLink{
|
|
|
|
"fe80::1337": {
|
|
|
|
Cost: 26214,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-12 15:25:00 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
nodes.Update("f4f26dd7a30b", &data.ResponseData{
|
2019-01-24 02:56:13 +01:00
|
|
|
Nodeinfo: &data.Nodeinfo{
|
2017-10-12 15:25:00 +02:00
|
|
|
NodeID: "f4f26dd7a30b",
|
2019-05-19 10:22:56 +02:00
|
|
|
Network: data.Network{
|
|
|
|
Mesh: map[string]*data.NetworkInterface{
|
|
|
|
"babel": {
|
|
|
|
Interfaces: struct {
|
|
|
|
Wireless []string `json:"wireless,omitempty"`
|
|
|
|
Other []string `json:"other,omitempty"`
|
|
|
|
Tunnel []string `json:"tunnel,omitempty"`
|
|
|
|
}{
|
|
|
|
Other: []string{"fe80::1337"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-12 15:25:00 +02:00
|
|
|
},
|
|
|
|
Neighbours: &data.Neighbours{
|
|
|
|
NodeID: "f4f26dd7a30b",
|
|
|
|
Batadv: map[string]data.BatadvNeighbours{
|
2018-01-13 16:40:23 +01:00
|
|
|
"f4:f2:6d:d7:a3:0b": {
|
2017-10-12 15:25:00 +02:00
|
|
|
Neighbours: map[string]data.BatmanLink{
|
2018-01-13 16:40:23 +01:00
|
|
|
"f4:f2:6d:d7:a3:0a": {
|
2017-12-05 23:17:49 +01:00
|
|
|
Tq: 204, Lastseen: 0.42,
|
2017-10-12 15:25:00 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2019-05-19 10:22:56 +02:00
|
|
|
// no neighbours nodeid
|
|
|
|
node := nodes.List["f4f26dd7a300"]
|
2017-10-12 15:25:00 +02:00
|
|
|
assert.NotNil(node)
|
|
|
|
links := nodes.NodeLinks(node)
|
|
|
|
assert.Len(links, 0)
|
|
|
|
|
2019-05-19 10:22:56 +02:00
|
|
|
// babel link
|
|
|
|
node = nodes.List["f4f26dd7a30a"]
|
2017-10-12 15:25:00 +02:00
|
|
|
assert.NotNil(node)
|
|
|
|
links = nodes.NodeLinks(node)
|
|
|
|
assert.Len(links, 1)
|
|
|
|
link := links[0]
|
2019-05-19 10:22:56 +02:00
|
|
|
assert.Equal("f4f26dd7a30a", link.SourceID)
|
|
|
|
assert.Equal("fe80::2", link.SourceAddress)
|
|
|
|
assert.Equal("f4f26dd7a30b", link.TargetID)
|
|
|
|
assert.Equal("fe80::1337", link.TargetAddress)
|
|
|
|
assert.Equal(float32(0.6), link.TQ)
|
|
|
|
|
|
|
|
// batman link
|
|
|
|
node = nodes.List["f4f26dd7a30b"]
|
|
|
|
assert.NotNil(node)
|
|
|
|
links = nodes.NodeLinks(node)
|
|
|
|
assert.Len(links, 1)
|
|
|
|
link = links[0]
|
|
|
|
assert.Equal("f4f26dd7a30b", link.SourceID)
|
|
|
|
assert.Equal("f4:f2:6d:d7:a3:0b", link.SourceAddress)
|
|
|
|
assert.Equal("f4f26dd7a30a", link.TargetID)
|
|
|
|
assert.Equal("f4:f2:6d:d7:a3:0a", link.TargetAddress)
|
|
|
|
assert.Equal(float32(0.8), link.TQ)
|
2017-05-20 14:46:29 +02:00
|
|
|
|
2017-12-05 23:17:49 +01:00
|
|
|
nodeid := nodes.GetNodeIDbyAddress("f4:f2:6d:d7:a3:0a")
|
2017-05-20 14:46:29 +02:00
|
|
|
assert.Equal("f4f26dd7a30a", nodeid)
|
2017-10-12 15:25:00 +02:00
|
|
|
}
|