2016-03-19 15:07:44 +01:00
|
|
|
package respond
|
|
|
|
|
|
|
|
import (
|
2023-09-18 10:55:05 +02:00
|
|
|
"os"
|
2016-03-19 15:07:44 +01:00
|
|
|
"testing"
|
2017-10-18 18:22:14 +02:00
|
|
|
"time"
|
2016-03-19 15:07:44 +01:00
|
|
|
|
2017-10-18 18:22:14 +02:00
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
2016-03-19 15:07:44 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2018-01-17 20:20:35 +01:00
|
|
|
const (
|
|
|
|
SITE_TEST = "ffhb"
|
|
|
|
DOMAIN_TEST = "city"
|
|
|
|
)
|
2017-11-21 15:12:06 +01:00
|
|
|
|
2017-10-18 18:22:14 +02:00
|
|
|
func TestCollector(t *testing.T) {
|
2018-01-07 21:00:56 +01:00
|
|
|
nodes := runtime.NewNodes(&runtime.NodesConfig{})
|
2019-11-17 10:33:20 +01:00
|
|
|
config := &Config{
|
|
|
|
Sites: map[string]SiteConfig{
|
|
|
|
SITE_TEST: {
|
|
|
|
Domains: []string{DOMAIN_TEST},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-10-18 18:22:14 +02:00
|
|
|
|
2019-11-17 10:33:20 +01:00
|
|
|
collector := NewCollector(nil, nodes, config)
|
2017-10-18 18:22:14 +02:00
|
|
|
collector.Start(time.Millisecond)
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
collector.Close()
|
|
|
|
}
|
|
|
|
|
2016-03-19 15:07:44 +01:00
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
// read testdata
|
2023-09-18 10:55:05 +02:00
|
|
|
compressed, err := os.ReadFile("testdata/nodeinfo.flated")
|
2016-03-19 15:07:44 +01:00
|
|
|
assert.Nil(err)
|
|
|
|
|
2016-10-03 19:55:37 +02:00
|
|
|
res := &Response{
|
2016-03-19 15:07:44 +01:00
|
|
|
Raw: compressed,
|
2016-10-03 19:55:37 +02:00
|
|
|
}
|
|
|
|
|
2019-11-17 10:44:11 +01:00
|
|
|
data, err := res.parse([]CustomFieldConfig{})
|
2016-10-03 19:55:37 +02:00
|
|
|
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotNil(data)
|
2016-03-19 15:07:44 +01:00
|
|
|
|
2019-01-24 02:56:13 +01:00
|
|
|
assert.Equal("f81a67a5e9c1", data.Nodeinfo.NodeID)
|
2016-03-19 15:07:44 +01:00
|
|
|
}
|
2019-11-17 10:44:11 +01:00
|
|
|
|
|
|
|
func TestParseCustomFields(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
// read testdata
|
2023-09-18 10:55:05 +02:00
|
|
|
compressed, err := os.ReadFile("testdata/nodeinfo.flated")
|
2019-11-17 10:44:11 +01:00
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
res := &Response{
|
|
|
|
Raw: compressed,
|
|
|
|
}
|
|
|
|
|
|
|
|
customFields := []CustomFieldConfig{
|
|
|
|
{
|
|
|
|
Name: "my_custom_field",
|
|
|
|
Path: "nodeinfo.hostname",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := res.parse(customFields)
|
|
|
|
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotNil(data)
|
|
|
|
|
|
|
|
assert.Equal("Trillian", data.CustomFields["my_custom_field"])
|
|
|
|
assert.Equal("Trillian", data.Nodeinfo.Hostname)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseCustomFieldNotExistant(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
// read testdata
|
2023-09-18 10:55:05 +02:00
|
|
|
compressed, err := os.ReadFile("testdata/nodeinfo.flated")
|
2019-11-17 10:44:11 +01:00
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
res := &Response{
|
|
|
|
Raw: compressed,
|
|
|
|
}
|
|
|
|
|
|
|
|
customFields := []CustomFieldConfig{
|
|
|
|
{
|
|
|
|
Name: "some_other_field",
|
|
|
|
Path: "nodeinfo.some_field_which_doesnt_exist",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := res.parse(customFields)
|
|
|
|
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotNil(data)
|
|
|
|
|
|
|
|
_, ok := data.CustomFields["some_other_field"]
|
|
|
|
assert.Equal("Trillian", data.Nodeinfo.Hostname)
|
|
|
|
assert.False(ok)
|
|
|
|
}
|