Skip to content

Commit

Permalink
[TASK] extract enable to all databases + increase testing in database
Browse files Browse the repository at this point in the history
(#78)
  • Loading branch information
genofire committed Dec 31, 2017
1 parent 3422a29 commit 69079b7
Show file tree
Hide file tree
Showing 16 changed files with 431 additions and 44 deletions.
26 changes: 26 additions & 0 deletions data/nodeinfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package data

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNodeinfoBatAddresses(t *testing.T) {
assert := assert.New(t)
batIface := &BatInterface{
Interfaces: struct {
Wireless []string `json:"wireless,omitempty"`
Other []string `json:"other,omitempty"`
Tunnel []string `json:"tunnel,omitempty"`
}{
Wireless: nil,
Other: []string{"aa:aa:aa:aa:aa", "aa:aa:aa:aa:ab"},
Tunnel: []string{},
},
}

addr := batIface.Addresses()
assert.NotNil(addr)
assert.Equal([]string{"aa:aa:aa:aa:aa", "aa:aa:aa:aa:ab"}, addr)
}
18 changes: 15 additions & 3 deletions database/all/internal.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package all

import (
"log"
"time"

"github.com/FreifunkBremen/yanic/database"
Expand All @@ -12,12 +13,23 @@ type Connection struct {
list []database.Connection
}

func Connect(configuration interface{}) (database.Connection, error) {
func Connect(allConnection map[string]interface{}) (database.Connection, error) {
var list []database.Connection
allConnection := configuration.(map[string][]interface{})
for dbType, conn := range database.Adapters {
dbConfigs := allConnection[dbType]
configForType := allConnection[dbType]
if configForType == nil {
log.Printf("the output type '%s' has no configuration\n", dbType)
continue
}
dbConfigs, ok := configForType.([]map[string]interface{})
if !ok {
log.Panicf("the output type '%s' has the wrong format\n", dbType)
}

for _, config := range dbConfigs {
if c, ok := config["enable"].(bool); ok && !c {
continue
}
connected, err := conn(config)
if err != nil {
return nil, err
Expand Down
155 changes: 155 additions & 0 deletions database/all/internel_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,156 @@
package all

import (
"errors"
"sync"
"testing"
"time"

"github.com/FreifunkBremen/yanic/database"
"github.com/FreifunkBremen/yanic/runtime"
"github.com/stretchr/testify/assert"
)

type testConn struct {
database.Connection
countNode int
countLink int
countGlobals int
countPrune int
countClose int
sync.Mutex
}

func (c *testConn) InsertNode(node *runtime.Node) {
c.Lock()
c.countNode++
c.Unlock()
}
func (c *testConn) GetNode() int {
c.Lock()
defer c.Unlock()
return c.countNode
}
func (c *testConn) InsertLink(link *runtime.Link, time time.Time) {
c.Lock()
c.countLink++
c.Unlock()
}
func (c *testConn) GetLink() int {
c.Lock()
defer c.Unlock()
return c.countLink
}
func (c *testConn) InsertGlobals(stats *runtime.GlobalStats, time time.Time, site string) {
c.Lock()
c.countGlobals++
c.Unlock()
}
func (c *testConn) GetGlobal() int {
c.Lock()
defer c.Unlock()
return c.countGlobals
}
func (c *testConn) PruneNodes(time.Duration) {
c.Lock()
c.countPrune++
c.Unlock()
}
func (c *testConn) GetPrune() int {
c.Lock()
defer c.Unlock()
return c.countPrune
}
func (c *testConn) Close() {
c.Lock()
c.countClose++
c.Unlock()
}
func (c *testConn) GetClose() int {
c.Lock()
defer c.Unlock()
return c.countClose
}

func TestStart(t *testing.T) {
assert := assert.New(t)

globalConn := &testConn{}
database.RegisterAdapter("a", func(config map[string]interface{}) (database.Connection, error) {
return globalConn, nil
})
database.RegisterAdapter("b", func(config map[string]interface{}) (database.Connection, error) {
return globalConn, nil
})
database.RegisterAdapter("c", func(config map[string]interface{}) (database.Connection, error) {
return globalConn, nil
})
database.RegisterAdapter("d", func(config map[string]interface{}) (database.Connection, error) {
return nil, nil
})
database.RegisterAdapter("e", func(config map[string]interface{}) (database.Connection, error) {
return nil, errors.New("blub")
})
allConn, err := Connect(map[string]interface{}{
"a": []map[string]interface{}{
map[string]interface{}{
"enable": false,
"path": "a1",
},
map[string]interface{}{
"path": "a2",
},
map[string]interface{}{
"enable": true,
"path": "a3",
},
},
"b": nil,
"c": []map[string]interface{}{
map[string]interface{}{
"path": "c1",
},
},
// fetch continue command in Connect
"d": []map[string]interface{}{
map[string]interface{}{
"path": "d0",
},
},
})
assert.NoError(err)

assert.Equal(0, globalConn.GetNode())
allConn.InsertNode(nil)
assert.Equal(3, globalConn.GetNode())

assert.Equal(0, globalConn.GetLink())
allConn.InsertLink(nil, time.Now())
assert.Equal(3, globalConn.GetLink())

assert.Equal(0, globalConn.GetGlobal())
allConn.InsertGlobals(nil, time.Now(), runtime.GLOBAL_SITE)
assert.Equal(3, globalConn.GetGlobal())

assert.Equal(0, globalConn.GetPrune())
allConn.PruneNodes(time.Second)
assert.Equal(3, globalConn.GetPrune())

assert.Equal(0, globalConn.GetClose())
allConn.Close()
assert.Equal(3, globalConn.GetClose())

_, err = Connect(map[string]interface{}{
"e": []map[string]interface{}{
map[string]interface{}{},
},
})
assert.Error(err)

// wrong format -> the only panic in Register
assert.Panics(func() {
Connect(map[string]interface{}{
"e": true,
})
})
}
2 changes: 1 addition & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Connection interface {
}

// Connect function with config to get DB connection interface
type Connect func(config interface{}) (Connection, error)
type Connect func(config map[string]interface{}) (Connection, error)

// Adapters is the list of registered database adapters
var Adapters = map[string]Connect{}
Expand Down
4 changes: 2 additions & 2 deletions database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"github.com/stretchr/testify/assert"
)

func TestNow(t *testing.T) {
func TestRegister(t *testing.T) {
assert := assert.New(t)
assert.Len(Adapters, 0)

RegisterAdapter("blub", func(config interface{}) (Connection, error) {
RegisterAdapter("blub", func(config map[string]interface{}) (Connection, error) {
return nil, nil
})

Expand Down
12 changes: 2 additions & 10 deletions database/graphite/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,10 @@ func (c Config) Prefix() string {
return c["prefix"].(string)
}

func (c Config) Enable() bool {
return c["enable"].(bool)
}

func Connect(configuration interface{}) (database.Connection, error) {
func Connect(configuration map[string]interface{}) (database.Connection, error) {
var config Config

config = configuration.(map[string]interface{})

if !config.Enable() {
return nil, nil
}
config = configuration

con := &Connection{
client: graphigo.Client{
Expand Down
11 changes: 3 additions & 8 deletions database/influxdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ type Connection struct {

type Config map[string]interface{}

func (c Config) Enable() bool {
return c["enable"].(bool)
}
func (c Config) Address() string {
return c["address"].(string)
}
Expand All @@ -57,12 +54,10 @@ func (c Config) Tags() map[string]interface{} {
func init() {
database.RegisterAdapter("influxdb", Connect)
}
func Connect(configuration interface{}) (database.Connection, error) {
func Connect(configuration map[string]interface{}) (database.Connection, error) {
var config Config
config = configuration.(map[string]interface{})
if !config.Enable() {
return nil, nil
}
config = configuration

// Make client
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: config.Address(),
Expand Down
21 changes: 21 additions & 0 deletions database/influxdb/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ import (
"github.com/stretchr/testify/assert"
)

func TestConnect(t *testing.T) {
assert := assert.New(t)

conn, err := Connect(map[string]interface{}{
"address": "",
"username": "",
"password": "",
})
assert.Nil(conn)
assert.Error(err)

conn, err = Connect(map[string]interface{}{
"address": "http://localhost",
"database": "",
"username": "",
"password": "",
})
assert.NotNil(conn)
assert.NoError(err)
}

func TestAddPoint(t *testing.T) {
assert := assert.New(t)

Expand Down
Loading

0 comments on commit 69079b7

Please sign in to comment.