package loader
import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/mercemay/portr/internal/config/target"
)
const good = `
targets:
- host: dns.internal
port: 53
name: unbound
tags: [dns, core]
- host: git.internal
port: 22
name: gitea
concurrency: 8
timeout: 1s
output:
table: true
`
func TestLoad_good(t *testing.T) {
c, err := Load(strings.NewReader(good))
if err != nil {
t.Fatalf("Load: %v", err)
}
want := []target.Target{
{Host: "dns.internal", Port: 53, Name: "unbound", Tags: []string{"dns", "core"}},
{Host: "git.internal", Port: 22, Name: "gitea"},
}
if diff := cmp.Diff(want, c.Targets); diff != "" {
t.Errorf("targets mismatch (-want +got):\n%s", diff)
}
if c.Concurrency != 8 {
t.Errorf("concurrency = %d, want 8", c.Concurrency)
}
}
func TestLoad_errors(t *testing.T) {
cases := map[string]string{
"empty": ``,
"no targets": `concurrency: 4`,
"bad port": "targets:\n - host: a\n port: 99999\n",
"dup": "targets:\n - host: a\n port: 1\n - host: a\n port: 1\n",
"unknown key": "targets:\n - host: a\n port: 1\nwat: true\n",
}
for name, yaml := range cases {
t.Run(name, func(t *testing.T) {
if _, err := Load(strings.NewReader(yaml)); err == nil {
t.Errorf("expected error for %q", name)
}
})
}
}