cmd: add even more tests against configuration

This commit is contained in:
Vincent Bernat
2022-08-17 19:49:45 +02:00
parent dbb191b267
commit 9ee4554a7d
9 changed files with 148 additions and 7 deletions

View File

@@ -5,9 +5,17 @@ package cmd
import (
"bytes"
"io/ioutil"
"path/filepath"
"reflect"
"strconv"
"strings"
"testing"
"akvorado/common/helpers"
"akvorado/common/reporter"
"gopkg.in/yaml.v2"
)
func TestOrchestratorStart(t *testing.T) {
@@ -19,13 +27,72 @@ func TestOrchestratorStart(t *testing.T) {
}
}
func TestOrchestratorFullConfig(t *testing.T) {
func TestOrchestratorConfig(t *testing.T) {
tests, err := ioutil.ReadDir("testdata/configurations")
if err != nil {
t.Fatalf("ReadDir(%q) error:\n%+v", "testdata/configurations", err)
}
for _, test := range tests {
if !test.IsDir() {
continue
}
t.Run(test.Name(), func(t *testing.T) {
expected, err := ioutil.ReadFile(
filepath.Join("testdata/configurations", test.Name(), "expected.yaml"))
if err != nil {
t.Fatalf("ReadFile() error:\n%+v", err)
}
var expectedYAML struct {
Paths map[string]interface{} `yaml:"paths"`
}
if err := yaml.Unmarshal(expected, &expectedYAML); err != nil {
t.Fatalf("yaml.Unmarshal(expected) error:\n%+v", err)
}
root := RootCmd
buf := new(bytes.Buffer)
root.SetOut(buf)
root.SetArgs([]string{"orchestrator", "--dump", "--check", "../akvorado.yaml"})
err := root.Execute()
root.SetArgs([]string{"orchestrator", "--dump", "--check",
filepath.Join("testdata/configurations", test.Name(), "in.yaml")})
if err := root.Execute(); err != nil {
t.Fatalf("`orchestrator` command error:\n%+v", err)
}
var gotYAML map[string]interface{}
if err := yaml.Unmarshal(buf.Bytes(), &gotYAML); err != nil {
t.Fatalf("yaml.Unmarshal(output) error:\n%+v", err)
}
for path, expected := range expectedYAML.Paths {
var got interface{}
got = gotYAML
i := 0
for _, component := range strings.Split(path, ".") {
var ok bool
i++
switch gotConcrete := got.(type) {
case []interface{}:
index, err := strconv.Atoi(component)
if err != nil {
t.Errorf("`orchestrator` command error:\n%+v", err)
t.Fatalf("key %q at level %d should be an int", path, i)
}
got = gotConcrete[index]
case map[interface{}]interface{}:
got, ok = gotConcrete[component]
if !ok {
t.Fatalf("key %q does not exist in result", path)
}
case map[string]interface{}:
got, ok = gotConcrete[component]
if !ok {
t.Fatalf("key %q does not exist in result", path)
}
default:
t.Fatalf("key %q lead to unexpected type %v at level %d",
path, reflect.TypeOf(got), i)
}
}
if diff := helpers.Diff(got, expected); diff != "" {
t.Fatalf("`orchestrator` --dump, key %q (-got, +want):\n%s", path, diff)
}
}
})
}
}

View File

@@ -0,0 +1,8 @@
---
paths:
kafka.brokers:
- 127.0.0.1:9092
inlet.0.kafka.brokers:
- 127.0.0.1:9092
clickhouse.kafka.brokers:
- 127.0.0.1:9092

View File

View File

@@ -0,0 +1,6 @@
---
paths:
inlet.0.geoip:
asndatabase: /usr/share/GeoIP/GeoLite2-ASN.mmdb
geodatabase: /usr/share/GeoIP/GeoLite2-Country.mmdb
optional: false

View File

@@ -0,0 +1,5 @@
---
inlet:
geoip:
asn-database: /usr/share/GeoIP/GeoLite2-ASN.mmdb
country-database: /usr/share/GeoIP/GeoLite2-Country.mmdb

View File

@@ -0,0 +1,33 @@
---
paths:
clickhouse.networks:
192.0.2.0/24:
name: ipv4-customers
role: customers
region: ""
site: ""
tenant: ""
203.0.113.0/24:
name: ipv4-servers
role: servers
region: ""
site: ""
tenant: ""
2a01:db8:cafe:1::/64:
name: ipv6-customers
role: customers
region: ""
site: ""
tenant: ""
2a01:db8:cafe:2::/64:
name: ipv6-servers
role: servers
region: ""
site: ""
tenant: ""
kafka.brokers:
- kafka:9092
inlet.0.kafka.brokers:
- kafka:9092
clickhouse.kafka.brokers:
- kafka:9092

View File

@@ -0,0 +1 @@
../../../../akvorado.yaml

View File

@@ -0,0 +1,15 @@
---
paths:
inlet.0.snmp:
cacheduration: 30m0s
cacherefresh: 1h0m0s
cachecheckinterval: 2m0s
cachepersistfile: ""
pollerretries: 1
pollertimeout: 1s
pollercoalesce: 10
workers: 1
communities:
::/0: yopla
203.0.113.0/24: yopli
securityparameters: {}

View File

@@ -0,0 +1,6 @@
---
inlet:
snmp:
default-community: yopla
communities:
203.0.113.0/24: yopli