From d892d1693d18c9db2327d5c7d0c3ef70b8ffefc4 Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Sun, 7 Jan 2018 21:00:56 +0100 Subject: [PATCH] [TASK] extract seperate config to packages --- cmd/config.go | 5 +- cmd/query.go | 2 +- cmd/serve.go | 2 +- database/all/internal.go | 8 ++- database/all/internel_test.go | 16 ++++-- database/config.go | 9 +++ database/influxdb/global_test.go | 2 +- database/influxdb/node_test.go | 2 +- {runtime => lib/duration}/duration.go | 2 +- {runtime => lib/duration}/duration_test.go | 2 +- {jsontime => lib/jsontime}/jsontime.go | 0 {jsontime => lib/jsontime}/jsontime_test.go | 0 output/all/filter.go | 6 +- output/meshviewer-ffrgb/meshviewer.go | 2 +- output/meshviewer-ffrgb/meshviewer_test.go | 2 +- output/meshviewer-ffrgb/struct.go | 2 +- output/meshviewer-ffrgb/struct_test.go | 4 +- output/meshviewer/graph_test.go | 2 +- output/meshviewer/node.go | 2 +- output/meshviewer/nodes_test.go | 2 +- output/meshviewer/nodes_v1.go | 2 +- output/meshviewer/nodes_v2.go | 2 +- output/nodelist/nodelist.go | 2 +- output/nodelist/nodelist_test.go | 4 +- respond/collector.go | 4 +- respond/collector_test.go | 2 +- runtime/config.go | 57 ------------------- runtime/config/config.go | 46 +++++++++++++++ runtime/{ => config}/config_test.go | 4 +- .../{ => config}/testdata/config_failed.toml | 0 runtime/node.go | 2 +- runtime/nodes.go | 18 +++--- runtime/nodes_config.go | 11 ++++ runtime/nodes_test.go | 24 ++++---- runtime/stats_test.go | 2 +- 35 files changed, 137 insertions(+), 115 deletions(-) create mode 100644 database/config.go rename {runtime => lib/duration}/duration.go (98%) rename {runtime => lib/duration}/duration_test.go (98%) rename {jsontime => lib/jsontime}/jsontime.go (100%) rename {jsontime => lib/jsontime}/jsontime_test.go (100%) delete mode 100644 runtime/config.go create mode 100644 runtime/config/config.go rename runtime/{ => config}/config_test.go (95%) rename runtime/{ => config}/testdata/config_failed.toml (100%) create mode 100644 runtime/nodes_config.go diff --git a/cmd/config.go b/cmd/config.go index 35beb4f..b0215a8 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -7,6 +7,7 @@ import ( "github.com/FreifunkBremen/yanic/database" "github.com/FreifunkBremen/yanic/respond" "github.com/FreifunkBremen/yanic/runtime" + "github.com/FreifunkBremen/yanic/runtime/config" ) var ( @@ -16,8 +17,8 @@ var ( nodes *runtime.Nodes ) -func loadConfig() *runtime.Config { - config, err := runtime.ReadConfigFile(configPath) +func loadConfig() *config.Config { + config, err := config.ReadConfigFile(configPath) if err != nil { fmt.Fprintln(os.Stderr, "unable to load config file:", err) os.Exit(2) diff --git a/cmd/query.go b/cmd/query.go index 8e7a73c..ea81708 100644 --- a/cmd/query.go +++ b/cmd/query.go @@ -24,7 +24,7 @@ var queryCmd = &cobra.Command{ log.Printf("Sending request address=%s iface=%s", dstAddress, iface) - nodes := runtime.NewNodes(&runtime.Config{}) + nodes := runtime.NewNodes(&runtime.NodesConfig{}) collector := respond.NewCollector(nil, nodes, []string{}, []string{iface}, 0) defer collector.Close() diff --git a/cmd/serve.go b/cmd/serve.go index f803696..89df9f2 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -29,7 +29,7 @@ var serveCmd = &cobra.Command{ } defer allDatabase.Close() - nodes = runtime.NewNodes(config) + nodes = runtime.NewNodes(&config.Nodes) nodes.Start() err = allOutput.Start(nodes, config.Nodes) diff --git a/database/all/internal.go b/database/all/internal.go index ec94453..6731454 100644 --- a/database/all/internal.go +++ b/database/all/internal.go @@ -1,27 +1,30 @@ package all import ( + "sync" "time" "github.com/FreifunkBremen/yanic/database" - "github.com/FreifunkBremen/yanic/runtime" ) var conn database.Connection +var wg = sync.WaitGroup{} var quit chan struct{} -func Start(config runtime.DatabaseConfig) (err error) { +func Start(config database.Config) (err error) { conn, err = Connect(config.Connection) if err != nil { return } quit = make(chan struct{}) + wg.Add(1) go deleteWorker(config.DeleteInterval.Duration, config.DeleteAfter.Duration) return } func Close() { close(quit) + wg.Wait() conn.Close() quit = nil } @@ -35,6 +38,7 @@ func deleteWorker(deleteInterval time.Duration, deleteAfter time.Duration) { conn.PruneNodes(deleteAfter) case <-quit: ticker.Stop() + wg.Done() return } } diff --git a/database/all/internel_test.go b/database/all/internel_test.go index 862284b..e3981db 100644 --- a/database/all/internel_test.go +++ b/database/all/internel_test.go @@ -6,7 +6,7 @@ import ( "time" "github.com/FreifunkBremen/yanic/database" - "github.com/FreifunkBremen/yanic/runtime" + "github.com/FreifunkBremen/yanic/lib/duration" "github.com/stretchr/testify/assert" ) @@ -21,8 +21,8 @@ func TestStart(t *testing.T) { }) // Test for PruneNodes (by start) assert.Nil(quit) - err := Start(runtime.DatabaseConfig{ - DeleteInterval: runtime.Duration{Duration: time.Millisecond}, + err := Start(database.Config{ + DeleteInterval: duration.Duration{Duration: time.Millisecond}, Connection: map[string]interface{}{ "a": []map[string]interface{}{ map[string]interface{}{ @@ -54,6 +54,7 @@ func TestStart(t *testing.T) { assert.NoError(err) assert.NotNil(quit) + // connection type not found _, err = Connect(map[string]interface{}{ "e": []map[string]interface{}{ map[string]interface{}{}, @@ -61,9 +62,14 @@ func TestStart(t *testing.T) { }) assert.Error(err) + // test close + Close() + // wrong format - _, err = Connect(map[string]interface{}{ - "e": true, + err = Start(database.Config{ + Connection: map[string]interface{}{ + "e": true, + }, }) assert.Error(err) } diff --git a/database/config.go b/database/config.go new file mode 100644 index 0000000..9844901 --- /dev/null +++ b/database/config.go @@ -0,0 +1,9 @@ +package database + +import "github.com/FreifunkBremen/yanic/lib/duration" + +type Config struct { + DeleteInterval duration.Duration `toml:"delete_interval"` // Delete stats of nodes every n minutes + DeleteAfter duration.Duration `toml:"delete_after"` // Delete stats of nodes till now-deletetill n minutes + Connection map[string]interface{} +} diff --git a/database/influxdb/global_test.go b/database/influxdb/global_test.go index 72d2dec..f990b5e 100644 --- a/database/influxdb/global_test.go +++ b/database/influxdb/global_test.go @@ -88,7 +88,7 @@ func TestGlobalStats(t *testing.T) { } func createTestNodes() *runtime.Nodes { - nodes := runtime.NewNodes(&runtime.Config{}) + nodes := runtime.NewNodes(&runtime.NodesConfig{}) nodeData := &runtime.Node{ Online: true, diff --git a/database/influxdb/node_test.go b/database/influxdb/node_test.go index 9e45d86..caf6299 100644 --- a/database/influxdb/node_test.go +++ b/database/influxdb/node_test.go @@ -177,7 +177,7 @@ func testPoints(nodes ...*runtime.Node) (points []*client.Point) { panic(err) } - nodesList := runtime.NewNodes(&runtime.Config{}) + nodesList := runtime.NewNodes(&runtime.NodesConfig{}) // Create dummy connection conn := &Connection{ diff --git a/runtime/duration.go b/lib/duration/duration.go similarity index 98% rename from runtime/duration.go rename to lib/duration/duration.go index 255d4ca..7ab819c 100644 --- a/runtime/duration.go +++ b/lib/duration/duration.go @@ -1,4 +1,4 @@ -package runtime +package duration import ( "fmt" diff --git a/runtime/duration_test.go b/lib/duration/duration_test.go similarity index 98% rename from runtime/duration_test.go rename to lib/duration/duration_test.go index d17f475..79af648 100644 --- a/runtime/duration_test.go +++ b/lib/duration/duration_test.go @@ -1,4 +1,4 @@ -package runtime +package duration import ( "testing" diff --git a/jsontime/jsontime.go b/lib/jsontime/jsontime.go similarity index 100% rename from jsontime/jsontime.go rename to lib/jsontime/jsontime.go diff --git a/jsontime/jsontime_test.go b/lib/jsontime/jsontime_test.go similarity index 100% rename from jsontime/jsontime_test.go rename to lib/jsontime/jsontime_test.go diff --git a/output/all/filter.go b/output/all/filter.go index d8da125..ce9d0c2 100644 --- a/output/all/filter.go +++ b/output/all/filter.go @@ -1,6 +1,8 @@ package all -import "github.com/FreifunkBremen/yanic/runtime" +import ( + "github.com/FreifunkBremen/yanic/runtime" +) // Config Filter type filterConfig map[string]interface{} @@ -13,7 +15,7 @@ func noFilter(node *runtime.Node) *runtime.Node { // Create Filter func (f filterConfig) filtering(nodesOrigin *runtime.Nodes) *runtime.Nodes { - nodes := runtime.NewNodes(&runtime.Config{}) + nodes := runtime.NewNodes(&runtime.NodesConfig{}) filterfuncs := []filterFunc{ f.HasLocation(), f.Blacklist(), diff --git a/output/meshviewer-ffrgb/meshviewer.go b/output/meshviewer-ffrgb/meshviewer.go index 98d3b21..5e05835 100644 --- a/output/meshviewer-ffrgb/meshviewer.go +++ b/output/meshviewer-ffrgb/meshviewer.go @@ -4,7 +4,7 @@ import ( "fmt" "strings" - "github.com/FreifunkBremen/yanic/jsontime" + "github.com/FreifunkBremen/yanic/lib/jsontime" "github.com/FreifunkBremen/yanic/runtime" ) diff --git a/output/meshviewer-ffrgb/meshviewer_test.go b/output/meshviewer-ffrgb/meshviewer_test.go index efd622a..90c2ee5 100644 --- a/output/meshviewer-ffrgb/meshviewer_test.go +++ b/output/meshviewer-ffrgb/meshviewer_test.go @@ -11,7 +11,7 @@ import ( func TestTransform(t *testing.T) { assert := assert.New(t) - nodes := runtime.NewNodes(&runtime.Config{}) + nodes := runtime.NewNodes(&runtime.NodesConfig{}) nodes.AddNode(&runtime.Node{ Online: true, Nodeinfo: &data.NodeInfo{ diff --git a/output/meshviewer-ffrgb/struct.go b/output/meshviewer-ffrgb/struct.go index 233775d..3c3fb82 100644 --- a/output/meshviewer-ffrgb/struct.go +++ b/output/meshviewer-ffrgb/struct.go @@ -3,7 +3,7 @@ package meshviewerFFRGB import ( "time" - "github.com/FreifunkBremen/yanic/jsontime" + "github.com/FreifunkBremen/yanic/lib/jsontime" "github.com/FreifunkBremen/yanic/runtime" ) diff --git a/output/meshviewer-ffrgb/struct_test.go b/output/meshviewer-ffrgb/struct_test.go index 863f19f..162c2d9 100644 --- a/output/meshviewer-ffrgb/struct_test.go +++ b/output/meshviewer-ffrgb/struct_test.go @@ -10,7 +10,7 @@ import ( func TestRegister(t *testing.T) { assert := assert.New(t) - nodes := runtime.NewNodes(&runtime.Config{}) + nodes := runtime.NewNodes(&runtime.NodesConfig{}) node := NewNode(nodes, &runtime.Node{ Nodeinfo: &data.NodeInfo{ Owner: &data.Owner{ @@ -21,7 +21,7 @@ func TestRegister(t *testing.T) { }, Location: &data.Location{ Longitude: 13.3, - Latitude: 8.7, + Latitude: 8.7, }, }, Statistics: &data.Statistics{ diff --git a/output/meshviewer/graph_test.go b/output/meshviewer/graph_test.go index 6c1313f..5d113eb 100644 --- a/output/meshviewer/graph_test.go +++ b/output/meshviewer/graph_test.go @@ -33,7 +33,7 @@ func TestGenerateGraph(t *testing.T) { func testGetNodesByFile(files ...string) *runtime.Nodes { - nodes := runtime.NewNodes(&runtime.Config{}) + nodes := runtime.NewNodes(&runtime.NodesConfig{}) for _, file := range files { node := testGetNodeByFile(file) diff --git a/output/meshviewer/node.go b/output/meshviewer/node.go index 4ffeefa..eb2d0e2 100644 --- a/output/meshviewer/node.go +++ b/output/meshviewer/node.go @@ -2,7 +2,7 @@ package meshviewer import ( "github.com/FreifunkBremen/yanic/data" - "github.com/FreifunkBremen/yanic/jsontime" + "github.com/FreifunkBremen/yanic/lib/jsontime" ) // Node struct diff --git a/output/meshviewer/nodes_test.go b/output/meshviewer/nodes_test.go index 27f5385..caf59de 100644 --- a/output/meshviewer/nodes_test.go +++ b/output/meshviewer/nodes_test.go @@ -23,7 +23,7 @@ func TestNodesV2(t *testing.T) { } func createTestNodes() *runtime.Nodes { - nodes := runtime.NewNodes(&runtime.Config{}) + nodes := runtime.NewNodes(&runtime.NodesConfig{}) nodeData := &runtime.Node{ Statistics: &data.Statistics{ diff --git a/output/meshviewer/nodes_v1.go b/output/meshviewer/nodes_v1.go index 7f07415..9ebb922 100644 --- a/output/meshviewer/nodes_v1.go +++ b/output/meshviewer/nodes_v1.go @@ -1,7 +1,7 @@ package meshviewer import ( - "github.com/FreifunkBremen/yanic/jsontime" + "github.com/FreifunkBremen/yanic/lib/jsontime" "github.com/FreifunkBremen/yanic/runtime" ) diff --git a/output/meshviewer/nodes_v2.go b/output/meshviewer/nodes_v2.go index c9d5611..2f726ab 100644 --- a/output/meshviewer/nodes_v2.go +++ b/output/meshviewer/nodes_v2.go @@ -1,7 +1,7 @@ package meshviewer import ( - "github.com/FreifunkBremen/yanic/jsontime" + "github.com/FreifunkBremen/yanic/lib/jsontime" "github.com/FreifunkBremen/yanic/runtime" ) diff --git a/output/nodelist/nodelist.go b/output/nodelist/nodelist.go index c101dde..ca680a0 100644 --- a/output/nodelist/nodelist.go +++ b/output/nodelist/nodelist.go @@ -1,7 +1,7 @@ package nodelist import ( - "github.com/FreifunkBremen/yanic/jsontime" + "github.com/FreifunkBremen/yanic/lib/jsontime" "github.com/FreifunkBremen/yanic/runtime" ) diff --git a/output/nodelist/nodelist_test.go b/output/nodelist/nodelist_test.go index e7a32f1..efd1159 100644 --- a/output/nodelist/nodelist_test.go +++ b/output/nodelist/nodelist_test.go @@ -17,7 +17,7 @@ func TestTransform(t *testing.T) { } func createTestNodes() *runtime.Nodes { - nodes := runtime.NewNodes(&runtime.Config{}) + nodes := runtime.NewNodes(&runtime.NodesConfig{}) nodeData := &runtime.Node{ Statistics: &data.Statistics{ @@ -47,7 +47,7 @@ func createTestNodes() *runtime.Nodes { Model: "TP-Link 841", }, Location: &data.Location{ - Latitude: 23, + Latitude: 23, Longitude: 2, }, }, diff --git a/respond/collector.go b/respond/collector.go index 7642f95..2e1327d 100644 --- a/respond/collector.go +++ b/respond/collector.go @@ -11,7 +11,7 @@ import ( "github.com/FreifunkBremen/yanic/data" "github.com/FreifunkBremen/yanic/database" - "github.com/FreifunkBremen/yanic/jsontime" + "github.com/FreifunkBremen/yanic/lib/jsontime" "github.com/FreifunkBremen/yanic/runtime" ) @@ -30,7 +30,7 @@ type Collector struct { } // NewCollector creates a Collector struct -func NewCollector(db database.Connection, nodes *runtime.Nodes, sites []string , ifaces []string, port int) *Collector { +func NewCollector(db database.Connection, nodes *runtime.Nodes, sites []string, ifaces []string, port int) *Collector { coll := &Collector{ db: db, diff --git a/respond/collector_test.go b/respond/collector_test.go index ba1ff0a..d6e5bd9 100644 --- a/respond/collector_test.go +++ b/respond/collector_test.go @@ -12,7 +12,7 @@ import ( const SITE_TEST = "ffxx" func TestCollector(t *testing.T) { - nodes := runtime.NewNodes(&runtime.Config{}) + nodes := runtime.NewNodes(&runtime.NodesConfig{}) collector := NewCollector(nil, nodes, []string{SITE_TEST}, []string{}, 10001) collector.Start(time.Millisecond) diff --git a/runtime/config.go b/runtime/config.go deleted file mode 100644 index b704560..0000000 --- a/runtime/config.go +++ /dev/null @@ -1,57 +0,0 @@ -package runtime - -import ( - "io/ioutil" - - "github.com/BurntSushi/toml" -) - -//Config the config File of this daemon -type Config struct { - Respondd struct { - Enable bool `toml:"enable"` - Synchronize Duration `toml:"synchronize"` - Interfaces []string `toml:"interfaces"` - Sites []string `toml:"sites"` - Port int `toml:"port"` - CollectInterval Duration `toml:"collect_interval"` - } - Webserver struct { - Enable bool `toml:"enable"` - Bind string `toml:"bind"` - Webroot string `toml:"webroot"` - } - Nodes NodesConfig - Database DatabaseConfig -} - -type NodesConfig struct { - StatePath string `toml:"state_path"` - SaveInterval Duration `toml:"save_interval"` // Save nodes periodically - OfflineAfter Duration `toml:"offline_after"` // Set node to offline if not seen within this period - PruneAfter Duration `toml:"prune_after"` // Remove nodes after n days of inactivity - Output map[string]interface{} -} - -type DatabaseConfig struct { - DeleteInterval Duration `toml:"delete_interval"` // Delete stats of nodes every n minutes - DeleteAfter Duration `toml:"delete_after"` // Delete stats of nodes till now-deletetill n minutes - Connection map[string]interface{} -} - -// ReadConfigFile reads a config model from path of a yml file -func ReadConfigFile(path string) (config *Config, err error) { - config = &Config{} - - file, err := ioutil.ReadFile(path) - if err != nil { - return nil, err - } - - err = toml.Unmarshal(file, config) - if err != nil { - return nil, err - } - - return -} diff --git a/runtime/config/config.go b/runtime/config/config.go new file mode 100644 index 0000000..fde1345 --- /dev/null +++ b/runtime/config/config.go @@ -0,0 +1,46 @@ +package config + +import ( + "io/ioutil" + + "github.com/BurntSushi/toml" + "github.com/FreifunkBremen/yanic/database" + "github.com/FreifunkBremen/yanic/lib/duration" + "github.com/FreifunkBremen/yanic/runtime" +) + +//Config the config File of this daemon +type Config struct { + Respondd struct { + Enable bool `toml:"enable"` + Synchronize duration.Duration `toml:"synchronize"` + Interfaces []string `toml:"interfaces"` + Sites []string `toml:"sites"` + Port int `toml:"port"` + CollectInterval duration.Duration `toml:"collect_interval"` + } + Webserver struct { + Enable bool `toml:"enable"` + Bind string `toml:"bind"` + Webroot string `toml:"webroot"` + } + Nodes runtime.NodesConfig + Database database.Config +} + +// ReadConfigFile reads a config model from path of a yml file +func ReadConfigFile(path string) (config *Config, err error) { + config = &Config{} + + file, err := ioutil.ReadFile(path) + if err != nil { + return nil, err + } + + err = toml.Unmarshal(file, config) + if err != nil { + return nil, err + } + + return +} diff --git a/runtime/config_test.go b/runtime/config/config_test.go similarity index 95% rename from runtime/config_test.go rename to runtime/config/config_test.go index ae6269d..77cd389 100644 --- a/runtime/config_test.go +++ b/runtime/config/config_test.go @@ -1,4 +1,4 @@ -package runtime +package config import ( "testing" @@ -10,7 +10,7 @@ import ( func TestReadConfig(t *testing.T) { assert := assert.New(t) - config, err := ReadConfigFile("../config_example.toml") + config, err := ReadConfigFile("../../config_example.toml") assert.NoError(err, "no error during reading") assert.NotNil(config) diff --git a/runtime/testdata/config_failed.toml b/runtime/config/testdata/config_failed.toml similarity index 100% rename from runtime/testdata/config_failed.toml rename to runtime/config/testdata/config_failed.toml diff --git a/runtime/node.go b/runtime/node.go index 76c45d0..f49807b 100644 --- a/runtime/node.go +++ b/runtime/node.go @@ -4,7 +4,7 @@ import ( "net" "github.com/FreifunkBremen/yanic/data" - "github.com/FreifunkBremen/yanic/jsontime" + "github.com/FreifunkBremen/yanic/lib/jsontime" ) // Node struct diff --git a/runtime/nodes.go b/runtime/nodes.go index b963974..be5f9b3 100644 --- a/runtime/nodes.go +++ b/runtime/nodes.go @@ -8,26 +8,26 @@ import ( "time" "github.com/FreifunkBremen/yanic/data" - "github.com/FreifunkBremen/yanic/jsontime" + "github.com/FreifunkBremen/yanic/lib/jsontime" ) // Nodes struct: cache DB of Node's structs type Nodes struct { List map[string]*Node `json:"nodes"` // the current nodemap, indexed by node ID ifaceToNodeID map[string]string // mapping from MAC address to NodeID - config *Config + config *NodesConfig sync.RWMutex } // NewNodes create Nodes structs -func NewNodes(config *Config) *Nodes { +func NewNodes(config *NodesConfig) *Nodes { nodes := &Nodes{ List: make(map[string]*Node), ifaceToNodeID: make(map[string]string), config: config, } - if config.Nodes.StatePath != "" { + if config.StatePath != "" { nodes.load() } @@ -130,7 +130,7 @@ func (nodes *Nodes) NodeLinks(node *Node) (result []Link) { // Periodically saves the cached DB to json file func (nodes *Nodes) worker() { - c := time.Tick(nodes.config.Nodes.SaveInterval.Duration) + c := time.Tick(nodes.config.SaveInterval.Duration) for range c { nodes.expire() @@ -143,14 +143,14 @@ func (nodes *Nodes) expire() { now := jsontime.Now() // Nodes last seen before expireAfter will be removed - prunePeriod := nodes.config.Nodes.PruneAfter.Duration + prunePeriod := nodes.config.PruneAfter.Duration if prunePeriod == 0 { prunePeriod = time.Hour * 24 * 7 // our default } pruneAfter := now.Add(-prunePeriod) // Nodes last seen within OfflineAfter are changed to 'offline' - offlineAfter := now.Add(-nodes.config.Nodes.OfflineAfter.Duration) + offlineAfter := now.Add(-nodes.config.OfflineAfter.Duration) // Locking foo nodes.Lock() @@ -194,7 +194,7 @@ func (nodes *Nodes) readIfaces(nodeinfo *data.NodeInfo) { } func (nodes *Nodes) load() { - path := nodes.config.Nodes.StatePath + path := nodes.config.StatePath if f, err := os.Open(path); err == nil { // transform data to legacy meshviewer if err = json.NewDecoder(f).Decode(nodes); err == nil { @@ -222,7 +222,7 @@ func (nodes *Nodes) save() { defer nodes.RUnlock() // serialize nodes - SaveJSON(nodes, nodes.config.Nodes.StatePath) + SaveJSON(nodes, nodes.config.StatePath) } // SaveJSON to path diff --git a/runtime/nodes_config.go b/runtime/nodes_config.go new file mode 100644 index 0000000..6d9520c --- /dev/null +++ b/runtime/nodes_config.go @@ -0,0 +1,11 @@ +package runtime + +import "github.com/FreifunkBremen/yanic/lib/duration" + +type NodesConfig struct { + StatePath string `toml:"state_path"` + SaveInterval duration.Duration `toml:"save_interval"` // Save nodes periodically + OfflineAfter duration.Duration `toml:"offline_after"` // Set node to offline if not seen within this period + PruneAfter duration.Duration `toml:"prune_after"` // Remove nodes after n days of inactivity + Output map[string]interface{} +} diff --git a/runtime/nodes_test.go b/runtime/nodes_test.go index a42ddcd..9b88503 100644 --- a/runtime/nodes_test.go +++ b/runtime/nodes_test.go @@ -9,15 +9,15 @@ import ( "github.com/stretchr/testify/assert" "github.com/FreifunkBremen/yanic/data" - "github.com/FreifunkBremen/yanic/jsontime" + "github.com/FreifunkBremen/yanic/lib/jsontime" ) func TestExpire(t *testing.T) { assert := assert.New(t) - config := &Config{} - config.Nodes.OfflineAfter.Duration = time.Minute * 10 + config := &NodesConfig{} + config.OfflineAfter.Duration = time.Minute * 10 // to get default (100%) path of testing - // config.Nodes.PruneAfter.Duration = time.Hour * 24 * 6 + // config.PruneAfter.Duration = time.Hour * 24 * 6 nodes := &Nodes{ config: config, List: make(map[string]*Node), @@ -51,22 +51,22 @@ func TestExpire(t *testing.T) { func TestLoadAndSave(t *testing.T) { assert := assert.New(t) - config := &Config{} + config := &NodesConfig{} // not autoload without StatePath NewNodes(config) // Test unmarshalable /dev/null - autolead with StatePath - config.Nodes.StatePath = "/dev/null" + config.StatePath = "/dev/null" nodes := NewNodes(config) // Test unopen able - config.Nodes.StatePath = "/root/nodes.json" + config.StatePath = "/root/nodes.json" nodes.load() // works ;) - config.Nodes.StatePath = "testdata/nodes.json" + config.StatePath = "testdata/nodes.json" nodes.load() tmpfile, _ := ioutil.TempFile("/tmp", "nodes") - config.Nodes.StatePath = tmpfile.Name() + config.StatePath = tmpfile.Name() nodes.save() os.Remove(tmpfile.Name()) @@ -113,8 +113,8 @@ func TestUpdateNodes(t *testing.T) { func TestSelectNodes(t *testing.T) { assert := assert.New(t) - config := &Config{} - config.Nodes.StatePath = "testdata/nodes.json" + config := &NodesConfig{} + config.StatePath = "testdata/nodes.json" nodes := NewNodes(config) @@ -139,7 +139,7 @@ func TestSelectNodes(t *testing.T) { func TestAddNode(t *testing.T) { assert := assert.New(t) - nodes := NewNodes(&Config{}) + nodes := NewNodes(&NodesConfig{}) nodes.AddNode(&Node{}) assert.Len(nodes.List, 0) diff --git a/runtime/stats_test.go b/runtime/stats_test.go index 59e8378..e25ac62 100644 --- a/runtime/stats_test.go +++ b/runtime/stats_test.go @@ -54,7 +54,7 @@ func TestGlobalStats(t *testing.T) { } func createTestNodes() *Nodes { - nodes := NewNodes(&Config{}) + nodes := NewNodes(&NodesConfig{}) nodeData := &Node{ Online: true,