mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-11 22:14:02 +01:00
cmd: test configuration handling without using one service
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
@@ -13,143 +14,174 @@ import (
|
||||
"akvorado/common/helpers"
|
||||
)
|
||||
|
||||
func want(t *testing.T, got, expected interface{}) {
|
||||
t.Helper()
|
||||
if diff := helpers.Diff(got, expected); diff != "" {
|
||||
t.Errorf("Configuration (-got, +want):\n%s", diff)
|
||||
}
|
||||
type dummyConfiguration struct {
|
||||
Module1 dummyModule1Configuration
|
||||
Module2 dummyModule2Configuration
|
||||
}
|
||||
type dummyModule1Configuration struct {
|
||||
Listen string
|
||||
Topic string
|
||||
Workers int
|
||||
}
|
||||
type dummyModule2Configuration struct {
|
||||
Details dummyModule2DetailsConfiguration
|
||||
Elements []dummyModule2ElementsConfiguration
|
||||
}
|
||||
type dummyModule2ElementsConfiguration struct {
|
||||
Name string
|
||||
Gauge int
|
||||
}
|
||||
type dummyModule2DetailsConfiguration struct {
|
||||
Workers int
|
||||
IntervalValue time.Duration
|
||||
}
|
||||
|
||||
var dummyDefaultConfiguration = dummyConfiguration{
|
||||
Module1: dummyModule1Configuration{
|
||||
Listen: "127.0.0.1:8080",
|
||||
Topic: "nothingness",
|
||||
Workers: 100,
|
||||
},
|
||||
Module2: dummyModule2Configuration{
|
||||
Details: dummyModule2DetailsConfiguration{
|
||||
Workers: 1,
|
||||
IntervalValue: time.Minute,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func TestDump(t *testing.T) {
|
||||
// Configuration file
|
||||
config := `---
|
||||
http:
|
||||
listen: 127.0.0.1:8000
|
||||
flow:
|
||||
inputs:
|
||||
- type: udp
|
||||
decoder: netflow
|
||||
listen: 0.0.0.0:2055
|
||||
workers: 5
|
||||
snmp:
|
||||
workers: 2
|
||||
cache-duration: 20m
|
||||
default-community: private
|
||||
kafka:
|
||||
connect:
|
||||
version: 2.8.1
|
||||
topic: netflow
|
||||
compression-codec: zstd
|
||||
core:
|
||||
workers: 3
|
||||
module1:
|
||||
topic: flows
|
||||
module2:
|
||||
details:
|
||||
workers: 5
|
||||
interval-value: 20m
|
||||
elements:
|
||||
- name: first
|
||||
gauge: 67
|
||||
- name: second
|
||||
`
|
||||
configFile := filepath.Join(t.TempDir(), "akvorado.yaml")
|
||||
configFile := filepath.Join(t.TempDir(), "config.yaml")
|
||||
ioutil.WriteFile(configFile, []byte(config), 0644)
|
||||
|
||||
c := cmd.ConfigRelatedOptions{
|
||||
Path: configFile,
|
||||
Dump: true,
|
||||
}
|
||||
conf := cmd.DefaultInletConfiguration
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
if err := c.Parse(buf, "inlet", conf); err != nil {
|
||||
|
||||
parsed := dummyDefaultConfiguration
|
||||
out := bytes.NewBuffer([]byte{})
|
||||
if err := c.Parse(out, "dummy", &parsed); err != nil {
|
||||
t.Fatalf("Parse() error:\n%+v", err)
|
||||
}
|
||||
var got map[string]map[string]interface{}
|
||||
if err := yaml.Unmarshal(buf.Bytes(), &got); err != nil {
|
||||
// Expected configuration
|
||||
expected := dummyConfiguration{
|
||||
Module1: dummyModule1Configuration{
|
||||
Listen: "127.0.0.1:8080",
|
||||
Topic: "flows",
|
||||
Workers: 100,
|
||||
},
|
||||
Module2: dummyModule2Configuration{
|
||||
Details: dummyModule2DetailsConfiguration{
|
||||
Workers: 5,
|
||||
IntervalValue: 20 * time.Minute,
|
||||
},
|
||||
Elements: []dummyModule2ElementsConfiguration{
|
||||
{"first", 67},
|
||||
{"second", 0},
|
||||
},
|
||||
},
|
||||
}
|
||||
if diff := helpers.Diff(parsed, expected); diff != "" {
|
||||
t.Errorf("Parse() (-got, +want):\n%s", diff)
|
||||
}
|
||||
|
||||
var gotRaw map[string]map[string]interface{}
|
||||
if err := yaml.Unmarshal(out.Bytes(), &gotRaw); err != nil {
|
||||
t.Fatalf("Unmarshal() error:\n%+v", err)
|
||||
}
|
||||
want(t, got["flow"], map[string]interface{}{
|
||||
"inputs": []map[string]interface{}{{
|
||||
"type": "udp",
|
||||
"decoder": "netflow",
|
||||
"listen": "0.0.0.0:2055",
|
||||
"queuesize": 100000,
|
||||
"receivebuffer": 0,
|
||||
"workers": 5,
|
||||
}},
|
||||
})
|
||||
want(t, got["snmp"]["workers"], 2)
|
||||
want(t, got["snmp"]["cacheduration"], "20m0s")
|
||||
want(t, got["snmp"]["defaultcommunity"], "private")
|
||||
want(t, got["kafka"]["connect"], map[string]interface{}{
|
||||
"brokers": []string{"127.0.0.1:9092"},
|
||||
"version": "2.8.1",
|
||||
"topic": "netflow",
|
||||
})
|
||||
expectedRaw := map[string]interface{}{
|
||||
"module1": map[string]interface{}{
|
||||
"listen": "127.0.0.1:8080",
|
||||
"topic": "flows",
|
||||
"workers": 100,
|
||||
},
|
||||
"module2": map[string]interface{}{
|
||||
"details": map[string]interface{}{
|
||||
"workers": 5,
|
||||
"intervalvalue": "20m0s",
|
||||
},
|
||||
"elements": []interface{}{
|
||||
map[string]interface{}{
|
||||
"name": "first",
|
||||
"gauge": 67,
|
||||
},
|
||||
map[string]interface{}{
|
||||
"name": "second",
|
||||
"gauge": 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
if diff := helpers.Diff(gotRaw, expectedRaw); diff != "" {
|
||||
t.Errorf("Parse() (-got, +want):\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvOverride(t *testing.T) {
|
||||
// Configuration file
|
||||
config := `---
|
||||
http:
|
||||
listen: 127.0.0.1:8000
|
||||
flow:
|
||||
inputs:
|
||||
- type: udp
|
||||
decoder: netflow
|
||||
listen: 0.0.0.0:2055
|
||||
workers: 5
|
||||
snmp:
|
||||
workers: 2
|
||||
cache-duration: 10m
|
||||
kafka:
|
||||
connect:
|
||||
version: 2.8.1
|
||||
topic: netflow
|
||||
compression-codec: zstd
|
||||
core:
|
||||
workers: 3
|
||||
module1:
|
||||
topic: flows
|
||||
module2:
|
||||
details:
|
||||
workers: 5
|
||||
interval-value: 20m
|
||||
`
|
||||
configFile := filepath.Join(t.TempDir(), "akvorado.yaml")
|
||||
configFile := filepath.Join(t.TempDir(), "config.yaml")
|
||||
ioutil.WriteFile(configFile, []byte(config), 0644)
|
||||
|
||||
// Environment
|
||||
os.Setenv("AKVORADO_INLET_SNMP_CACHEDURATION", "22m")
|
||||
os.Setenv("AKVORADO_INLET_SNMP_DEFAULTCOMMUNITY", "privateer")
|
||||
os.Setenv("AKVORADO_INLET_SNMP_WORKERS", "3")
|
||||
os.Setenv("AKVORADO_INLET_KAFKA_CONNECT_BROKERS", "127.0.0.1:9092,127.0.0.2:9092")
|
||||
os.Setenv("AKVORADO_INLET_FLOW_INPUTS_0_LISTEN", "0.0.0.0:2056")
|
||||
// We may be lucky or the environment is keeping order
|
||||
os.Setenv("AKVORADO_INLET_FLOW_INPUTS_1_TYPE", "file")
|
||||
os.Setenv("AKVORADO_INLET_FLOW_INPUTS_1_DECODER", "netflow")
|
||||
os.Setenv("AKVORADO_INLET_FLOW_INPUTS_1_PATHS", "f1,f2")
|
||||
os.Setenv("AKVORADO_DUMMY_MODULE1_LISTEN", "127.0.0.1:9000")
|
||||
os.Setenv("AKVORADO_DUMMY_MODULE1_TOPIC", "something")
|
||||
os.Setenv("AKVORADO_DUMMY_MODULE2_DETAILS_INTERVALVALUE", "10m")
|
||||
os.Setenv("AKVORADO_DUMMY_MODULE2_ELEMENTS_0_NAME", "something")
|
||||
os.Setenv("AKVORADO_DUMMY_MODULE2_ELEMENTS_0_GAUGE", "18")
|
||||
os.Setenv("AKVORADO_DUMMY_MODULE2_ELEMENTS_1_NAME", "something else")
|
||||
os.Setenv("AKVORADO_DUMMY_MODULE2_ELEMENTS_1_GAUGE", "7")
|
||||
|
||||
c := cmd.ConfigRelatedOptions{
|
||||
Path: configFile,
|
||||
Dump: true,
|
||||
}
|
||||
conf := cmd.DefaultInletConfiguration
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
if err := c.Parse(buf, "inlet", conf); err != nil {
|
||||
|
||||
parsed := dummyDefaultConfiguration
|
||||
out := bytes.NewBuffer([]byte{})
|
||||
if err := c.Parse(out, "dummy", &parsed); err != nil {
|
||||
t.Fatalf("Parse() error:\n%+v", err)
|
||||
}
|
||||
var got map[string]map[string]interface{}
|
||||
if err := yaml.Unmarshal(buf.Bytes(), &got); err != nil {
|
||||
t.Fatalf("Unmarshal() error:\n%+v", err)
|
||||
}
|
||||
want(t, got["snmp"]["cacheduration"], "22m0s")
|
||||
want(t, got["snmp"]["defaultcommunity"], "privateer")
|
||||
want(t, got["snmp"]["workers"], 3)
|
||||
want(t, got["kafka"]["connect"], map[string]interface{}{
|
||||
"brokers": []string{"127.0.0.1:9092", "127.0.0.2:9092"},
|
||||
"version": "2.8.1",
|
||||
"topic": "netflow",
|
||||
})
|
||||
want(t, got["flow"], map[string]interface{}{
|
||||
"inputs": []map[string]interface{}{
|
||||
{
|
||||
"type": "udp",
|
||||
"decoder": "netflow",
|
||||
"listen": "0.0.0.0:2056",
|
||||
"queuesize": 100000,
|
||||
"receivebuffer": 0,
|
||||
"workers": 5,
|
||||
}, {
|
||||
"type": "file",
|
||||
"decoder": "netflow",
|
||||
"paths": []string{"f1", "f2"},
|
||||
// Expected configuration
|
||||
expected := dummyConfiguration{
|
||||
Module1: dummyModule1Configuration{
|
||||
Listen: "127.0.0.1:9000",
|
||||
Topic: "something",
|
||||
Workers: 100,
|
||||
},
|
||||
Module2: dummyModule2Configuration{
|
||||
Details: dummyModule2DetailsConfiguration{
|
||||
Workers: 5,
|
||||
IntervalValue: 10 * time.Minute,
|
||||
},
|
||||
Elements: []dummyModule2ElementsConfiguration{
|
||||
{"something", 18},
|
||||
{"something else", 7},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
if diff := helpers.Diff(parsed, expected); diff != "" {
|
||||
t.Errorf("Parse() (-got, +want):\n%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user