yanic/output/all/output_test.go

122 lines
2.3 KiB
Go
Raw Normal View History

package all
import (
"errors"
"sync"
"testing"
"github.com/FreifunkBremen/yanic/output"
"github.com/FreifunkBremen/yanic/runtime"
"github.com/stretchr/testify/assert"
)
type testOutput struct {
output.Output
countSave int
sync.Mutex
}
func (c *testOutput) Save(nodes *runtime.Nodes) {
c.Lock()
c.countSave++
c.Unlock()
}
func (c *testOutput) Get() int {
c.Lock()
defer c.Unlock()
return c.countSave
}
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{}{
2022-04-14 00:38:00 +02:00
{
"enable": false,
"path": "a1",
},
2022-04-14 00:38:00 +02:00
{
"path": "a2",
},
2022-04-14 00:38:00 +02:00
{
"enable": true,
"path": "a3",
},
},
"b": nil,
"c": []map[string]interface{}{
2022-04-14 00:38:00 +02:00
{
"path": "c1",
"filter": map[string]interface{}{},
},
},
// fetch continue command in Connect
"d": []map[string]interface{}{
2022-04-14 00:38:00 +02:00
{
"path": "d0",
},
},
})
assert.NoError(err)
assert.Equal(0, globalOutput.Get())
allOutput.Save(nodes)
assert.Equal(3, globalOutput.Get())
2018-01-13 19:08:46 +01:00
// wrong format - map
_, err = Register(map[string]interface{}{
2018-01-13 19:08:46 +01:00
"e": []interface{}{
false,
},
})
assert.Error(err)
2018-01-13 19:08:46 +01:00
// wrong format - array
2018-01-03 15:41:40 +01:00
_, err = Register(map[string]interface{}{
"e": true,
})
2018-01-03 15:41:40 +01:00
assert.Error(err)
2018-01-13 19:08:46 +01:00
// output error
_, err = Register(map[string]interface{}{
"e": []interface{}{
map[string]interface{}{
"enable": true,
},
},
})
assert.Error(err)
// output error invalid config of filter
_, err = Register(map[string]interface{}{
"a": []interface{}{
map[string]interface{}{
"enable": true,
"filter": map[string]interface{}{
2020-06-16 23:30:54 +02:00
"blocklist": true,
2018-01-13 19:08:46 +01:00
},
},
},
})
assert.Error(err)
}