2017-05-20 12:46:29 +00:00
|
|
|
package all
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-11-14 18:33:28 +00:00
|
|
|
"sync"
|
2017-05-20 12:46:29 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/FreifunkBremen/yanic/output"
|
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testOutput struct {
|
|
|
|
output.Output
|
2017-11-14 18:33:28 +00:00
|
|
|
countSave int
|
|
|
|
sync.Mutex
|
2017-05-20 12:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testOutput) Save(nodes *runtime.Nodes) {
|
2017-11-14 18:33:28 +00:00
|
|
|
c.Lock()
|
|
|
|
c.countSave++
|
|
|
|
c.Unlock()
|
|
|
|
}
|
|
|
|
func (c *testOutput) Get() int {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
return c.countSave
|
2017-05-20 12:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStart(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
nodes := &runtime.Nodes{}
|
|
|
|
|
|
|
|
globalOutput := &testOutput{}
|
|
|
|
output.RegisterAdapter("a", func(config map[string]interface{}) (output.Output, error) {
|
|
|
|
return globalOutput, nil
|
|
|
|
})
|
|
|
|
output.RegisterAdapter("b", func(config map[string]interface{}) (output.Output, error) {
|
|
|
|
return globalOutput, nil
|
|
|
|
})
|
|
|
|
output.RegisterAdapter("c", func(config map[string]interface{}) (output.Output, error) {
|
|
|
|
return globalOutput, nil
|
|
|
|
})
|
|
|
|
output.RegisterAdapter("d", func(config map[string]interface{}) (output.Output, error) {
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
output.RegisterAdapter("e", func(config map[string]interface{}) (output.Output, error) {
|
|
|
|
return nil, errors.New("blub")
|
|
|
|
})
|
|
|
|
allOutput, err := Register(map[string]interface{}{
|
|
|
|
"a": []map[string]interface{}{
|
2018-01-13 15:40:23 +00:00
|
|
|
{
|
2017-05-20 12:46:29 +00:00
|
|
|
"enable": false,
|
|
|
|
"path": "a1",
|
|
|
|
},
|
2018-01-13 15:40:23 +00:00
|
|
|
{
|
2017-05-20 12:46:29 +00:00
|
|
|
"path": "a2",
|
|
|
|
},
|
2018-01-13 15:40:23 +00:00
|
|
|
{
|
2017-05-20 12:46:29 +00:00
|
|
|
"enable": true,
|
|
|
|
"path": "a3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"b": nil,
|
|
|
|
"c": []map[string]interface{}{
|
2018-01-13 15:40:23 +00:00
|
|
|
{
|
2017-05-20 12:46:29 +00:00
|
|
|
"path": "c1",
|
|
|
|
"filter": map[string]interface{}{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// fetch continue command in Connect
|
|
|
|
"d": []map[string]interface{}{
|
2018-01-13 15:40:23 +00:00
|
|
|
{
|
2017-05-20 12:46:29 +00:00
|
|
|
"path": "d0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(err)
|
|
|
|
|
2017-11-14 18:33:28 +00:00
|
|
|
assert.Equal(0, globalOutput.Get())
|
2017-05-20 12:46:29 +00:00
|
|
|
allOutput.Save(nodes)
|
2017-11-14 18:33:28 +00:00
|
|
|
assert.Equal(3, globalOutput.Get())
|
2017-05-20 12:46:29 +00:00
|
|
|
|
|
|
|
_, err = Register(map[string]interface{}{
|
|
|
|
"e": []map[string]interface{}{
|
2018-01-13 15:40:23 +00:00
|
|
|
{},
|
2017-05-20 12:46:29 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Error(err)
|
|
|
|
|
2018-01-03 14:41:40 +00:00
|
|
|
// wrong format
|
|
|
|
_, err = Register(map[string]interface{}{
|
|
|
|
"e": true,
|
2017-05-20 12:46:29 +00:00
|
|
|
})
|
2018-01-03 14:41:40 +00:00
|
|
|
assert.Error(err)
|
2017-05-20 12:46:29 +00:00
|
|
|
}
|