@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"strings"
"testing"
@ -117,6 +118,61 @@ func TestBuildDomainXMLRejectsSecureBootAndUnsafePaths(t *testing.T) {
}
}
func TestBuildDomainXMLBindsManagedFilterToAllocatedIPv4 ( t * testing . T ) {
definition := validDomainDefinition ( )
definition . FilterName = "virt-123e4567-e89b-42d3-a456-426614174000"
document , err := BuildDomainXML ( definition )
if err != nil {
t . Fatal ( err )
}
var domain libvirtxml . Domain
if err := domain . Unmarshal ( document ) ; err != nil {
t . Fatal ( err )
}
if len ( domain . Devices . Interfaces ) != 1 || ! managedInterfaceFilterIPMatches (
& domain . Devices . Interfaces [ 0 ] , definition . FilterName , definition . PublicIPCIDR ,
) {
t . Fatalf ( "managed interface IP binding is missing: %s" , document )
}
parameter := domain . Devices . Interfaces [ 0 ] . FilterRef . Parameters [ 0 ]
if parameter . Value != "8.8.8.10" || strings . Contains ( parameter . Value , "/" ) {
t . Fatalf ( "filter IP parameter = %q, want bare IPv4 address" , parameter . Value )
}
}
func TestManagedInterfaceFilterIPUpdateRequiresExistingExactBinding ( t * testing . T ) {
const filterName = "virt-123e4567-e89b-42d3-a456-426614174000"
bound , err := newManagedInterfaceFilterRef ( filterName , "8.8.8.10/24" )
if err != nil {
t . Fatal ( err )
}
iface := libvirtxml . DomainInterface { FilterRef : bound }
if err := updateManagedInterfaceFilterIP ( & iface , filterName , "8.8.8.10/24" , "8.8.8.11/24" ) ; err != nil {
t . Fatal ( err )
}
if ! managedInterfaceFilterIPMatches ( & iface , filterName , "8.8.8.11/24" ) {
t . Fatalf ( "updated filter binding is wrong: %#v" , iface . FilterRef )
}
for name , legacy := range map [ string ] libvirtxml . DomainInterface {
"missing parameter" : { FilterRef : & libvirtxml . DomainInterfaceFilterRef { Filter : filterName } } ,
"stale address" : {
FilterRef : & libvirtxml . DomainInterfaceFilterRef { Filter : filterName , Parameters : [ ] libvirtxml . DomainInterfaceFilterParam { { Name : "IP" , Value : "8.8.8.9" } } } ,
} ,
"duplicate parameter" : {
FilterRef : & libvirtxml . DomainInterfaceFilterRef { Filter : filterName , Parameters : [ ] libvirtxml . DomainInterfaceFilterParam { { Name : "IP" , Value : "8.8.8.10" } , { Name : "IP" , Value : "8.8.8.9" } } } ,
} ,
} {
t . Run ( name , func ( t * testing . T ) {
err := updateManagedInterfaceFilterIP ( & legacy , filterName , "8.8.8.10/24" , "8.8.8.11/24" )
var public * Error
if ! errors . As ( err , & public ) || public . Code != "network_filter_binding_changed" {
t . Fatalf ( "legacy binding error = %v, want network_filter_binding_changed" , err )
}
} )
}
}
func TestBuildPlatformFilterXMLUsesValidatedDropRules ( t * testing . T ) {
request := validIntentCreateRequest ( t )
document , err := BuildPlatformFilterXML ( "virt-123e4567-e89b-42d3-a456-426614174000" , request . PlatformNetworkPolicy )