cmd: test command checks

This commit is contained in:
Vincent Bernat
2023-07-30 20:27:38 +02:00
parent e88b53eada
commit 02e527915b
5 changed files with 63 additions and 1 deletions

View File

@@ -105,7 +105,7 @@ func (c ConfigRelatedOptions) Parse(out io.Writer, component string, config inte
continue
}
kk := strings.Split(kv[0], "_")
if len(kk) < 4 || kk[0] != "AKVORADO" || kk[1] != "CFG" || kk[2] != strings.ToUpper(component) {
if len(kk) < 4 || kk[0] != "AKVORADO" || kk[1] != "CFG" || kk[2] != strings.ReplaceAll(strings.ToUpper(component), "-", "") {
continue
}
// From AKVORADO_CFG_CMP_SQUID_PURPLE_QUIRK=47, we

View File

@@ -4,6 +4,7 @@
package cmd
import (
"bytes"
"testing"
"akvorado/common/reporter"
@@ -17,3 +18,14 @@ func TestConsoleStart(t *testing.T) {
t.Fatalf("consoleStart() error:\n%+v", err)
}
}
func TestConsole(t *testing.T) {
root := RootCmd
buf := new(bytes.Buffer)
root.SetOut(buf)
root.SetArgs([]string{"console", "--check", "/dev/null"})
err := root.Execute()
if err != nil {
t.Errorf("`console` error:\n%+v", err)
}
}

View File

@@ -4,8 +4,11 @@
package cmd
import (
"bytes"
"strings"
"testing"
"akvorado/common/helpers"
"akvorado/common/reporter"
)
@@ -17,3 +20,27 @@ func TestDemoExporterStart(t *testing.T) {
t.Fatalf("demoExporterStart() error:\n%+v", err)
}
}
func TestDemoExporter(t *testing.T) {
root := RootCmd
buf := new(bytes.Buffer)
root.SetOut(buf)
root.SetArgs([]string{"demo-exporter", "--check", "/dev/null"})
t.Setenv("AKVORADO_CFG_DEMOEXPORTER_SNMP_NAME", "test")
err := root.Execute()
if err == nil {
t.Fatal("`demo-exporter` should produce an error")
}
want := []string{
`invalid configuration:`,
`Key: 'DemoExporterConfiguration.SNMP.Interfaces' Error:Field validation for 'Interfaces' failed on the 'min' tag`,
`Key: 'DemoExporterConfiguration.Flows.Flows' Error:Field validation for 'Flows' failed on the 'min' tag`,
`Key: 'DemoExporterConfiguration.Flows.Target' Error:Field validation for 'Target' failed on the 'required' tag`,
}
got := strings.Split(err.Error(), "\n")
if diff := helpers.Diff(got, want); diff != "" {
t.Fatalf("`demo-exporter` (-got, +want):\n%s", diff)
}
}

View File

@@ -4,6 +4,7 @@
package cmd
import (
"bytes"
"testing"
"akvorado/common/reporter"
@@ -17,3 +18,14 @@ func TestInletStart(t *testing.T) {
t.Fatalf("inletStart() error:\n%+v", err)
}
}
func TestInlet(t *testing.T) {
root := RootCmd
buf := new(bytes.Buffer)
root.SetOut(buf)
root.SetArgs([]string{"inlet", "--check", "/dev/null"})
err := root.Execute()
if err != nil {
t.Errorf("`inlet` error:\n%+v", err)
}
}

View File

@@ -97,3 +97,14 @@ func TestOrchestratorConfig(t *testing.T) {
})
}
}
func TestOrchestrator(t *testing.T) {
root := RootCmd
buf := new(bytes.Buffer)
root.SetOut(buf)
root.SetArgs([]string{"orchestrator", "--check", "/dev/null"})
err := root.Execute()
if err != nil {
t.Errorf("`orchestrator` error:\n%+v", err)
}
}