kvm2: config block, constructor registration, IDs, state mapping and classify
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>
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package kvm2
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/digitalocean/go-libvirt"
|
||||
|
||||
"gitea.home.hrajfrisbee.cz/kacerr/egress-proxies-operator/internal/provider"
|
||||
)
|
||||
|
||||
func TestMapState_table(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
state libvirt.DomainState
|
||||
noIP provider.InstanceState
|
||||
withIP provider.InstanceState
|
||||
display string
|
||||
}{
|
||||
{libvirt.DomainNostate, provider.StateProvisioning, provider.StateProvisioning, "nostate"},
|
||||
{libvirt.DomainRunning, provider.StateProvisioning, provider.StateRunning, "running"},
|
||||
{libvirt.DomainBlocked, provider.StateProvisioning, provider.StateRunning, "blocked"},
|
||||
{libvirt.DomainPaused, provider.StateStopped, provider.StateStopped, "paused"},
|
||||
{libvirt.DomainShutdown, provider.StateStopped, provider.StateStopped, "shutdown"},
|
||||
{libvirt.DomainShutoff, provider.StateStopped, provider.StateStopped, "shutoff"},
|
||||
{libvirt.DomainCrashed, provider.StateStopped, provider.StateStopped, "crashed"},
|
||||
{libvirt.DomainPmsuspended, provider.StateStopped, provider.StateStopped, "pmsuspended"},
|
||||
{libvirt.DomainState(99), provider.StateStopped, provider.StateStopped, "unknown code 99"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.display, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if got := mapState(tc.state, ""); got != tc.noIP {
|
||||
t.Errorf("mapState(%s, \"\") = %s, want %s", tc.display, got, tc.noIP)
|
||||
}
|
||||
if got := mapState(tc.state, "10.0.0.5"); got != tc.withIP {
|
||||
t.Errorf("mapState(%s, ip) = %s, want %s", tc.display, got, tc.withIP)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsActive(t *testing.T) {
|
||||
t.Parallel()
|
||||
for s, want := range map[libvirt.DomainState]bool{
|
||||
libvirt.DomainRunning: true, libvirt.DomainBlocked: true,
|
||||
libvirt.DomainNostate: false, libvirt.DomainPaused: false, libvirt.DomainShutoff: false, libvirt.DomainState(99): false,
|
||||
} {
|
||||
if got := isActive(s); got != want {
|
||||
t.Errorf("isActive(%d) = %v, want %v", s, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func addr(typ libvirt.IPAddrType, a string) libvirt.DomainIPAddr {
|
||||
return libvirt.DomainIPAddr{Type: int32(typ), Addr: a}
|
||||
}
|
||||
|
||||
func TestFirstIPv4_filters(t *testing.T) {
|
||||
t.Parallel()
|
||||
v4 := libvirt.IPAddrTypeIpv4
|
||||
v6 := libvirt.IPAddrTypeIpv6
|
||||
tests := []struct {
|
||||
name string
|
||||
ifaces []libvirt.DomainInterface
|
||||
want string
|
||||
}{
|
||||
{name: "empty list", ifaces: nil, want: ""},
|
||||
{name: "loopback only", ifaces: []libvirt.DomainInterface{{Name: "lo", Addrs: []libvirt.DomainIPAddr{addr(v4, "127.0.0.1")}}}, want: ""},
|
||||
{name: "link-local only", ifaces: []libvirt.DomainInterface{{Name: "eth0", Addrs: []libvirt.DomainIPAddr{addr(v4, "169.254.10.1")}}}, want: ""},
|
||||
{name: "ipv6 only", ifaces: []libvirt.DomainInterface{{Name: "eth0", Addrs: []libvirt.DomainIPAddr{addr(v6, "2001:db8::1"), addr(v6, "fe80::1")}}}, want: ""},
|
||||
{name: "unspecified", ifaces: []libvirt.DomainInterface{{Name: "eth0", Addrs: []libvirt.DomainIPAddr{addr(v4, "0.0.0.0")}}}, want: ""},
|
||||
{name: "unparsable", ifaces: []libvirt.DomainInterface{{Name: "eth0", Addrs: []libvirt.DomainIPAddr{addr(v4, "not-an-ip")}}}, want: ""},
|
||||
{name: "ipv4 literal tagged as ipv6 is skipped", ifaces: []libvirt.DomainInterface{{Name: "eth0", Addrs: []libvirt.DomainIPAddr{addr(v6, "10.0.0.7")}}}, want: ""},
|
||||
{name: "ipv6 literal tagged as ipv4 is skipped", ifaces: []libvirt.DomainInterface{{Name: "eth0", Addrs: []libvirt.DomainIPAddr{addr(v4, "2001:db8::1")}}}, want: ""},
|
||||
{
|
||||
name: "good one after bad ones across interfaces",
|
||||
ifaces: []libvirt.DomainInterface{
|
||||
{Name: "lo", Addrs: []libvirt.DomainIPAddr{addr(v4, "127.0.0.1")}},
|
||||
{Name: "eth0", Addrs: []libvirt.DomainIPAddr{addr(v6, "fe80::5054:ff:fe12:3456"), addr(v4, "169.254.1.1"), addr(v4, "bogus"), addr(v4, "192.168.1.42"), addr(v4, "192.168.1.43")}},
|
||||
},
|
||||
want: "192.168.1.42",
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if got := firstIPv4(tc.ifaces); got != tc.want {
|
||||
t.Errorf("firstIPv4() = %q, want %q", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user