The kvm2 block reuses KVMConfig; validateKVM gains a block-name variant so messages say kvm2. The package skeleton has a non-dialing New, strict providerID parsing, the state table and a 34-row classify test. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
55 lines
1.6 KiB
Go
55 lines
1.6 KiB
Go
package kvm2
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.home.hrajfrisbee.cz/kacerr/egress-proxies-operator/internal/provider"
|
|
)
|
|
|
|
func TestFormatProviderID_roundTrip(t *testing.T) {
|
|
t.Parallel()
|
|
id := formatProviderID("beelink", "proxy-abc123def456ghij")
|
|
if id != "hosts/beelink/domains/proxy-abc123def456ghij" {
|
|
t.Fatalf("formatProviderID() = %q", id)
|
|
}
|
|
host, name, err := parseProviderID(id)
|
|
if err != nil || host != "beelink" || name != "proxy-abc123def456ghij" {
|
|
t.Errorf("parseProviderID(%q) = %q, %q, %v; want beelink, proxy-abc123def456ghij, nil", id, host, name, err)
|
|
}
|
|
}
|
|
|
|
func TestParseProviderID_malformed(t *testing.T) {
|
|
t.Parallel()
|
|
tests := []struct {
|
|
name string
|
|
id string
|
|
}{
|
|
{name: "empty", id: ""},
|
|
{name: "two segments", id: "hosts/h"},
|
|
{name: "three segments", id: "hosts/h/domains"},
|
|
{name: "empty host", id: "hosts//domains/n"},
|
|
{name: "empty name", id: "hosts/h/domains/"},
|
|
{name: "wrong first literal", id: "nodes/h/domains/n"},
|
|
{name: "wrong third literal", id: "hosts/h/vms/n"},
|
|
{name: "extra segment", id: "hosts/h/domains/n/extra"},
|
|
{name: "slash in name", id: "hosts/h/domains/a/b"},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
host, name, err := parseProviderID(tc.id)
|
|
if err == nil {
|
|
t.Fatalf("parseProviderID(%q) = %q, %q, nil; want error", tc.id, host, name)
|
|
}
|
|
if !errors.Is(err, provider.ErrPermanent) {
|
|
t.Errorf("parseProviderID(%q) error = %v, want ErrPermanent", tc.id, err)
|
|
}
|
|
if !strings.Contains(err.Error(), tc.id) {
|
|
t.Errorf("parseProviderID(%q) error = %q, want it to name the ID", tc.id, err)
|
|
}
|
|
})
|
|
}
|
|
}
|