You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
437 lines
19 KiB
Go
437 lines
19 KiB
Go
package hypervisor
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"encoding/xml"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"libvirt.org/go/libvirtxml"
|
|
)
|
|
|
|
func validDomainDefinition() DomainDefinition {
|
|
return DomainDefinition{
|
|
UUID: "123e4567-e89b-42d3-a456-426614174000", Name: "3:vm-123e4567e89b", DisplayName: "vm-test",
|
|
Description: "escaped <description> & safe", VCPUs: 2, MemoryMiB: 2048,
|
|
DiskGiB: 40, BandwidthMbps: 100, Hostname: "vm-test.example",
|
|
PublicIPCIDR: "8.8.8.10/24", PublicPoolKey: "0123456789abcdef0123456789abcdef",
|
|
PublicPoolCIDR: "8.8.8.0/24", Gateway: "8.8.8.1", DNSServers: []string{"1.1.1.1"},
|
|
NetworkSelector: "public", TemplateID: "debian-12",
|
|
RootDiskPath: "/var/lib/libvirt/images/123e4567-e89b-42d3-a456-426614174000-root.qcow2",
|
|
RootVolumeName: "123e4567-e89b-42d3-a456-426614174000-root.qcow2", RootDiskFormat: "qcow2",
|
|
Firmware: Firmware{
|
|
Mode: "uefi", Loader: "/usr/share/OVMF/OVMF_CODE.fd",
|
|
NVRAMTemplate: "/usr/share/OVMF/OVMF_VARS.fd",
|
|
NVRAMPath: "/var/lib/libvirt/qemu/nvram/vm-test_VARS.fd",
|
|
SecureBoot: false,
|
|
},
|
|
SeedDiskPath: "/var/lib/libvirt/images/vm-test-seed.iso",
|
|
Network: []NetworkAttachment{{Bridge: "br0", MAC: "52:54:00:12:34:56"}},
|
|
FilterName: "virt-123e4567-e89b-42d3-a456-426614174000",
|
|
PlatformNetworkPolicy: testPlatformPolicy(),
|
|
}
|
|
}
|
|
|
|
func testPlatformPolicy() PlatformNetworkPolicySnapshot {
|
|
rules := []PlatformNetworkPolicyRule{{
|
|
ID: "ssh", Label: "SSH", Category: "default", Protocols: []string{"tcp"},
|
|
PortFrom: 22, PortTo: 22, Directions: []string{"ingress"}, Reason: "default SSH policy",
|
|
}}
|
|
document := struct {
|
|
SchemaVersion uint64 `json:"schema_version"`
|
|
BasePolicyVersion uint64 `json:"base_policy_version"`
|
|
CatalogRevision uint64 `json:"catalog_revision"`
|
|
Rules []PlatformNetworkPolicyRule `json:"rules"`
|
|
}{SchemaVersion: 1, BasePolicyVersion: 1, CatalogRevision: 3, Rules: rules}
|
|
encoded, _ := json.Marshal(document)
|
|
digest := sha256.Sum256(encoded)
|
|
return PlatformNetworkPolicySnapshot{SchemaVersion: 1, BasePolicyVersion: 1, CatalogRevision: 3, Rules: rules, Digest: hex.EncodeToString(digest[:])}
|
|
}
|
|
|
|
func TestBuildDomainXMLUsesQ35UEFIWithSecureBootDisabled(t *testing.T) {
|
|
definition := validDomainDefinition()
|
|
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 domain.Name != "3:vm-123e4567e89b" {
|
|
t.Fatalf("libvirt domain name = %q, want authenticated internal name", domain.Name)
|
|
}
|
|
if domain.OS == nil || domain.OS.Type == nil || domain.OS.Type.Machine != "q35" {
|
|
t.Fatalf("machine is not q35: %#v", domain.OS)
|
|
}
|
|
if domain.OS.Loader == nil || domain.OS.Loader.Path != definition.Firmware.Loader || domain.OS.Loader.Type != "pflash" || domain.OS.Loader.Readonly != "yes" || domain.OS.Loader.Secure != "no" {
|
|
t.Fatalf("loader does not pin the configured non-Secure-Boot pflash firmware: %#v", domain.OS.Loader)
|
|
}
|
|
if domain.OS.NVRam == nil || domain.OS.NVRam.Template != definition.Firmware.NVRAMTemplate || domain.OS.NVRam.NVRam != definition.Firmware.NVRAMPath {
|
|
t.Fatalf("NVRAM is incomplete: %#v", domain.OS.NVRam)
|
|
}
|
|
if domain.Features == nil || domain.Features.SMM == nil || domain.Features.SMM.State != "off" {
|
|
t.Fatalf("SMM must be explicitly off: %#v", domain.Features)
|
|
}
|
|
if domain.CPU == nil || domain.CPU.Mode != "host-passthrough" {
|
|
t.Fatalf("CPU mode is not host-passthrough: %#v", domain.CPU)
|
|
}
|
|
if len(domain.Devices.Disks) != 2 || domain.Devices.Disks[1].Device != "cdrom" || domain.Devices.Disks[1].ReadOnly == nil {
|
|
t.Fatalf("NoCloud seed disk is missing or writable: %#v", domain.Devices.Disks)
|
|
}
|
|
if len(domain.Devices.Interfaces) != 1 || domain.Devices.Interfaces[0].Driver == nil || domain.Devices.Interfaces[0].Driver.Queues != 2 {
|
|
t.Fatalf("virtio-net queue count is missing: %#v", domain.Devices.Interfaces)
|
|
}
|
|
rate := int((uint64(100)*1_000_000 + 8191) / 8192)
|
|
bandwidth := domain.Devices.Interfaces[0].Bandwidth
|
|
if bandwidth == nil || bandwidth.Inbound == nil || bandwidth.Outbound == nil || bandwidth.Inbound.Average == nil || bandwidth.Inbound.Peak == nil || bandwidth.Outbound.Average == nil || bandwidth.Outbound.Peak == nil || *bandwidth.Inbound.Average != rate || *bandwidth.Inbound.Peak != rate || *bandwidth.Outbound.Average != rate || *bandwidth.Outbound.Peak != rate {
|
|
t.Fatalf("decimal Mbps bandwidth was not converted to average+peak KiB/s: %#v", bandwidth)
|
|
}
|
|
if len(domain.Devices.Videos) != 1 || domain.Devices.Videos[0].Model.Type != "vga" || len(domain.Devices.Inputs) != 1 || domain.Devices.Inputs[0].Type != "tablet" || domain.Devices.Inputs[0].Bus != "usb" {
|
|
t.Fatalf("console input/video devices are incomplete: %#v %#v", domain.Devices.Videos, domain.Devices.Inputs)
|
|
}
|
|
if len(domain.Devices.Channels) != 1 || domain.Devices.Channels[0].Target == nil || domain.Devices.Channels[0].Target.VirtIO == nil || domain.Devices.Channels[0].Target.VirtIO.Name != "org.qemu.guest_agent.0" {
|
|
t.Fatalf("QEMU guest agent channel is missing: %#v", domain.Devices.Channels)
|
|
}
|
|
if domain.Devices.MemBalloon == nil || domain.Devices.MemBalloon.Model != "virtio" || domain.Devices.MemBalloon.FreePageReporting != "on" || domain.Devices.MemBalloon.Stats == nil || domain.Devices.MemBalloon.Stats.Period != 10 {
|
|
t.Fatalf("virtio balloon telemetry is incomplete: %#v", domain.Devices.MemBalloon)
|
|
}
|
|
if domain.OS.Firmware != "" || domain.OS.FirmwareInfo != nil || strings.Contains(document, "<firmware>") {
|
|
t.Fatalf("manual firmware must not request autoselection features: %#v", domain.OS)
|
|
}
|
|
if strings.Contains(document, "escaped <description>") || !strings.Contains(document, "<description> & safe") {
|
|
t.Fatalf("description was not XML escaped: %s", document)
|
|
}
|
|
}
|
|
|
|
func TestBuildDomainXMLCanStageMigrationOfflineWithSnapshotCatalog(t *testing.T) {
|
|
definition := validDomainDefinition()
|
|
definition.Network[0].LinkDown = true
|
|
definition.Snapshots = []domainSnapshotMetadataXML{{
|
|
Key: "fedcba9876543210fedcba9876543210", Name: "before-maintenance",
|
|
Consistency: "crash", SizeBytes: 40 << 30, CreatedAt: 1700000000,
|
|
}}
|
|
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 || domain.Devices.Interfaces[0].Link == nil ||
|
|
domain.Devices.Interfaces[0].Link.State != "down" {
|
|
t.Fatalf("staged migration interface is not persistently down: %#v", domain.Devices.Interfaces)
|
|
}
|
|
var stored domainMetadataXML
|
|
if domain.Metadata == nil || xml.Unmarshal([]byte(domain.Metadata.XML), &stored) != nil ||
|
|
len(stored.Snapshots) != 1 || stored.Snapshots[0].Key != definition.Snapshots[0].Key {
|
|
t.Fatalf("staged migration snapshot catalog missing: %#v", stored.Snapshots)
|
|
}
|
|
}
|
|
|
|
func TestBuildDomainXMLRejectsSecureBootAndUnsafePaths(t *testing.T) {
|
|
request := validDomainDefinition()
|
|
request.Firmware.SecureBoot = true
|
|
if _, err := BuildDomainXML(request); err == nil || !strings.Contains(err.Error(), "secure boot") {
|
|
t.Fatalf("secure boot request error = %v", err)
|
|
}
|
|
request = validDomainDefinition()
|
|
request.RootDiskPath = "/var/lib/libvirt/../etc/passwd"
|
|
if _, err := BuildDomainXML(request); err == nil || !strings.Contains(err.Error(), "root_disk_path") {
|
|
t.Fatalf("unsafe disk path error = %v", err)
|
|
}
|
|
}
|
|
|
|
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 TestBuildDomainXMLBindsVPCInterfaceToPrivateAntiSpoofFilter(t *testing.T) {
|
|
definition := validDomainDefinition()
|
|
port := validVPCPortMetadata()
|
|
definition.VPCPort = &port
|
|
definition.Network = append(definition.Network, NetworkAttachment{
|
|
Bridge: port.Bridge, MAC: port.MAC,
|
|
})
|
|
document, err := BuildDomainXML(definition)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var parsed libvirtxml.Domain
|
|
if err := parsed.Unmarshal(document); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var metadata domainMetadataXML
|
|
if parsed.Metadata == nil ||
|
|
xml.Unmarshal([]byte(parsed.Metadata.XML), &metadata) != nil {
|
|
t.Fatalf("managed metadata is missing: %s", document)
|
|
}
|
|
if !managedNetworkLayoutMatches(&parsed, &metadata) ||
|
|
len(parsed.Devices.Interfaces) != 2 {
|
|
t.Fatalf("managed VPC layout is invalid: %s", document)
|
|
}
|
|
publicIndex, publicFound := managedPublicInterfaceIndex(
|
|
&parsed, definition.FilterName, definition.PublicIPCIDR,
|
|
)
|
|
private := domainVPCPortFromPublic(port)
|
|
privateIndex, privateFound := managedVPCInterfaceIndex(&parsed, &private)
|
|
if !publicFound || !privateFound || publicIndex == privateIndex ||
|
|
!managedVPCInterfaceFilterIPMatches(
|
|
&parsed.Devices.Interfaces[privateIndex], port.PrivateIP,
|
|
) || parsed.Devices.Interfaces[privateIndex].FilterRef.Filter ==
|
|
definition.FilterName {
|
|
t.Fatalf("public/private interface roles are not isolated: %#v",
|
|
parsed.Devices.Interfaces)
|
|
}
|
|
privateFilter := parsed.Devices.Interfaces[privateIndex].FilterRef
|
|
if len(privateFilter.Parameters) != 1 ||
|
|
privateFilter.Parameters[0].Value != port.PrivateIP {
|
|
t.Fatalf("private anti-spoof binding is incomplete: %#v", privateFilter)
|
|
}
|
|
privateFilter.Parameters[0].Value = "10.60.0.99"
|
|
if managedNetworkLayoutMatches(&parsed, &metadata) {
|
|
t.Fatal("managed layout accepted a VPC filter bound to another private IP")
|
|
}
|
|
privateFilter.Parameters[0].Value = port.PrivateIP
|
|
parsed.Devices.Interfaces[privateIndex].FilterRef = nil
|
|
if managedNetworkLayoutMatches(&parsed, &metadata) {
|
|
t.Fatal("managed layout accepted an unfiltered VPC interface")
|
|
}
|
|
}
|
|
|
|
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)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var filter libvirtxml.NWFilter
|
|
if err := filter.Unmarshal(document); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if filter.Name != "virt-123e4567-e89b-42d3-a456-426614174000" || len(filter.Entries) != 3 {
|
|
t.Fatalf("unexpected filter: %#v", filter)
|
|
}
|
|
if filter.Entries[0].Ref == nil || filter.Entries[0].Ref.Filter != "clean-traffic" {
|
|
t.Fatalf("anti-spoofing filter is missing: %s", document)
|
|
}
|
|
drop := filter.Entries[1].Rule
|
|
if drop == nil || drop.Action != "drop" || drop.Direction != "in" || drop.TCP == nil {
|
|
t.Fatalf("platform drop rule is missing: %s", document)
|
|
}
|
|
if drop.TCP.DstPortStart.Uint == nil || *drop.TCP.DstPortStart.Uint != 22 ||
|
|
drop.TCP.DstPortEnd.Uint == nil || *drop.TCP.DstPortEnd.Uint != 22 {
|
|
t.Fatalf("drop ports are wrong: %#v", drop.TCP)
|
|
}
|
|
accept := filter.Entries[2].Rule
|
|
if accept == nil || accept.Action != "accept" || accept.Direction != "inout" || accept.All == nil {
|
|
t.Fatalf("terminal accept rule is missing: %s", document)
|
|
}
|
|
}
|
|
|
|
func TestMetadataRoundTrip(t *testing.T) {
|
|
document, err := BuildDomainXML(validDomainDefinition())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
metadata, err := metadataFromXML(document)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if metadata.UUID != "123e4567-e89b-42d3-a456-426614174000" || metadata.Name != "vm-test" || metadata.Firmware != "uefi" || metadata.SecureBoot || metadata.MemoryMiB != 2048 ||
|
|
metadata.DiskGiB != 40 || metadata.BandwidthMbps != 100 || metadata.Hostname != "vm-test.example" || metadata.TemplateID != "debian-12" ||
|
|
metadata.PublicIPCIDR != "8.8.8.10/24" || metadata.PublicPoolKey != "0123456789abcdef0123456789abcdef" ||
|
|
metadata.PublicPoolCIDR != "8.8.8.0/24" || metadata.Gateway != "8.8.8.1" || metadata.NetworkSelector != "public" ||
|
|
len(metadata.DNSServers) != 1 || metadata.DNSServers[0] != "1.1.1.1" {
|
|
t.Fatalf("unexpected metadata: %#v", metadata)
|
|
}
|
|
}
|
|
|
|
func TestMetadataFallsBackToLegacyDomainName(t *testing.T) {
|
|
document, err := BuildDomainXML(validDomainDefinition())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
document = strings.Replace(document, "<name>3:vm-123e4567e89b</name>", "<name>legacy-vm</name>", 1)
|
|
document = strings.Replace(document, "<display-name>vm-test</display-name>", "", 1)
|
|
metadata, err := metadataFromXML(document)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if metadata.Name != "legacy-vm" {
|
|
t.Fatalf("legacy display name = %q, want legacy-vm", metadata.Name)
|
|
}
|
|
}
|
|
|
|
func TestLibvirtDomainNameUsesOwnerAndCompactUUIDPrefix(t *testing.T) {
|
|
const uuid = "83472897-3274-4abc-8def-0123456789ab"
|
|
name, err := LibvirtDomainName(3, uuid)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if name != "3:vm-834728973274" {
|
|
t.Fatalf("domain name = %q, want 3:vm-834728973274", name)
|
|
}
|
|
upper, err := LibvirtDomainName(3, strings.ToUpper(uuid))
|
|
if err != nil || upper != name {
|
|
t.Fatalf("uppercase UUID name = %q err=%v, want %q", upper, err, name)
|
|
}
|
|
if _, err := LibvirtDomainName(0, uuid); err == nil {
|
|
t.Fatal("zero owner id was accepted")
|
|
}
|
|
if _, err := LibvirtDomainName(3, "not-a-uuid"); err == nil {
|
|
t.Fatal("invalid UUID was accepted")
|
|
}
|
|
other, err := LibvirtDomainName(3, "93472897-3274-4abc-8def-0123456789ab")
|
|
if err != nil || other == name {
|
|
t.Fatalf("different UUID domain name = %q err=%v, must differ from %q", other, err, name)
|
|
}
|
|
}
|
|
|
|
func TestValidateDomainDefinitionRejectsMismatchedInternalName(t *testing.T) {
|
|
definition := validDomainDefinition()
|
|
definition.Name = "3:vm-834728973274"
|
|
if err := ValidateDomainDefinition(definition); err == nil || !strings.Contains(err.Error(), "authenticated owner") {
|
|
t.Fatalf("mismatched domain name error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateCreateVMUsesPanelIntentWithoutHostPaths(t *testing.T) {
|
|
request := validIntentCreateRequest(t)
|
|
if err := ValidateCreateVM(request); err != nil {
|
|
t.Fatalf("valid intent was rejected: %v", err)
|
|
}
|
|
request.TemplateGenerationToken = ""
|
|
if err := ValidateCreateVM(request); err == nil || !strings.Contains(err.Error(), "generation") {
|
|
t.Fatalf("partial template snapshot error = %v", err)
|
|
}
|
|
}
|
|
|
|
func validIntentCreateRequest(t *testing.T) CreateVMRequest {
|
|
t.Helper()
|
|
rules := []PlatformNetworkPolicyRule{{
|
|
ID: "ssh", Label: "SSH", Category: "default", Protocols: []string{"tcp"},
|
|
PortFrom: 22, PortTo: 22, Directions: []string{"ingress"}, Reason: "default SSH policy",
|
|
}}
|
|
document := struct {
|
|
SchemaVersion uint64 `json:"schema_version"`
|
|
BasePolicyVersion uint64 `json:"base_policy_version"`
|
|
CatalogRevision uint64 `json:"catalog_revision"`
|
|
Rules []PlatformNetworkPolicyRule `json:"rules"`
|
|
}{SchemaVersion: 1, BasePolicyVersion: 1, CatalogRevision: 3, Rules: rules}
|
|
encoded, err := json.Marshal(document)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
digest := sha256.Sum256(encoded)
|
|
return CreateVMRequest{
|
|
UUID: "123e4567-e89b-42d3-a456-426614174000", DomainName: "3:vm-123e4567e89b",
|
|
Source: "cloud", Name: "vm-test", VCPUs: 2, MemoryMiB: 2048, DiskGiB: 40,
|
|
BandwidthMbps: 100, NetworkSelector: "bridge:br0", TemplateID: "debian-12",
|
|
TemplateRevision: 1, TemplateGenerationToken: "0123456789abcdef0123456789abcdef",
|
|
RootPassword: "StrongPassword!123", PublicPoolKey: "0123456789abcdef0123456789abcdef",
|
|
PublicPoolCIDR: "8.8.8.0/24", PublicIPCIDR: "8.8.8.10/24", Gateway: "8.8.8.1",
|
|
DNSServers: []string{"1.1.1.1", "9.9.9.9"}, PlatformNetworkPolicy: PlatformNetworkPolicySnapshot{
|
|
SchemaVersion: 1, BasePolicyVersion: 1, CatalogRevision: 3, Rules: rules,
|
|
Digest: hex.EncodeToString(digest[:]),
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestValidatePublicNetworkIntentRejectsPoolDrift(t *testing.T) {
|
|
request := validIntentCreateRequest(t)
|
|
request.NetworkSelector = "public"
|
|
if err := ValidateCreateVM(request); err == nil || !strings.Contains(err.Error(),
|
|
"network selector is invalid") {
|
|
t.Fatalf("legacy alias selector error = %v", err)
|
|
}
|
|
request = validIntentCreateRequest(t)
|
|
request.PublicIPCIDR = "8.8.4.10/24"
|
|
if err := ValidateCreateVM(request); err == nil || !strings.Contains(err.Error(), "inside") {
|
|
t.Fatalf("outside allocation error = %v", err)
|
|
}
|
|
request = validIntentCreateRequest(t)
|
|
request.PublicPoolCIDR = "8.8.8.1/24"
|
|
if err := ValidateCreateVM(request); err == nil || !strings.Contains(err.Error(), "canonical") {
|
|
t.Fatalf("non-canonical pool error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeStartupScriptContract(t *testing.T) {
|
|
input := " #!/bin/sh\r\nprintf 'ok'\r\n "
|
|
got, err := NormalizeStartupScript(input)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != " #!/bin/sh\nprintf 'ok'\n " {
|
|
t.Fatalf("normalization trimmed or changed content: %q", got)
|
|
}
|
|
if _, err := NormalizeStartupScript(strings.Repeat("a", 32<<10)); err != nil {
|
|
t.Fatalf("exact 32 KiB was rejected: %v", err)
|
|
}
|
|
if _, err := NormalizeStartupScript(strings.Repeat("a", (32<<10)+1)); err == nil {
|
|
t.Fatal("script over 32 KiB was accepted")
|
|
}
|
|
if _, err := NormalizeStartupScript("ok\x00bad"); err == nil {
|
|
t.Fatal("NUL was accepted")
|
|
}
|
|
if _, err := NormalizeStartupScript(string([]byte{0xff, 0xfe})); err == nil {
|
|
t.Fatal("invalid UTF-8 was accepted")
|
|
}
|
|
}
|