|
|
package hypervisor
|
|
|
|
|
|
import (
|
|
|
"reflect"
|
|
|
"slices"
|
|
|
"strings"
|
|
|
"testing"
|
|
|
)
|
|
|
|
|
|
func TestCreateVMAdmissionUsesCanonicalTypedResources(t *testing.T) {
|
|
|
request := CreateVMRequest{
|
|
|
UUID: "123E4567-E89B-42D3-A456-426614174000",
|
|
|
TemplateID: "debian-12",
|
|
|
PublicNetwork: dualStackPublicNetwork(),
|
|
|
}
|
|
|
admission := CreateVMAdmission(request)
|
|
|
want := []string{
|
|
|
"domain:123e4567-e89b-42d3-a456-426614174000",
|
|
|
"volume:virt-cloud-managed/vm-123e4567-e89b-42d3-a456-426614174000",
|
|
|
"volume:virt-cloud-managed/123e4567-e89b-42d3-a456-426614174000-root.qcow2",
|
|
|
"volume:virt-cloud-seeds/123e4567-e89b-42d3-a456-426614174000-seed.iso",
|
|
|
"nwfilter:virt-123e4567-e89b-42d3-a456-426614174000",
|
|
|
"catalog:template/debian-12",
|
|
|
"volume:virt-cloud-templates/template-debian-12.qcow2",
|
|
|
networkIdentityLockKey(request.PublicNetwork.IPv4.AddressCIDR),
|
|
|
networkIdentityLockKey(request.PublicNetwork.IPv6.AddressCIDR),
|
|
|
networkIdentityLockKey(vmMACAddress("123e4567-e89b-42d3-a456-426614174000")),
|
|
|
}
|
|
|
if !admission.HeavyIO || !reflect.DeepEqual(admission.LockKeys, want) {
|
|
|
t.Fatalf("create admission = %#v, want %#v", admission, want)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func TestVPCNetworkAdmissionSerializesHostProjectionAndOneVPC(t *testing.T) {
|
|
|
const key = "0123456789abcdef0123456789abcdef"
|
|
|
admission := VPCNetworkAdmission(key)
|
|
|
want := []string{"catalog:vpc-host/state", "catalog:vpc/" + key}
|
|
|
if admission.HeavyIO || !reflect.DeepEqual(admission.LockKeys, want) {
|
|
|
t.Fatalf("VPC admission = %#v, want %#v", admission, want)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func TestVPCPortAdmissionAllowsIndependentPortsToRunConcurrently(t *testing.T) {
|
|
|
const uuid = "123e4567-e89b-42d3-a456-426614174000"
|
|
|
request := VPCPortRequest{
|
|
|
PortKey: "1123456789abcdef0123456789abcdef",
|
|
|
VPCKey: "0123456789abcdef0123456789abcdef",
|
|
|
PrivateIP: "10.60.0.9", MAC: "02:00:00:00:00:09",
|
|
|
}
|
|
|
admission := VPCPortAdmission(uuid, request)
|
|
|
for _, required := range []string{
|
|
|
"catalog:vpc-port/" + request.PortKey, domainLockKey(uuid),
|
|
|
volumeLockKey(seedStoragePoolName, seedVolumeName(uuid)),
|
|
|
networkIdentityLockKey(request.PrivateIP), networkIdentityLockKey(request.MAC),
|
|
|
} {
|
|
|
if !slices.Contains(admission.LockKeys, required) {
|
|
|
t.Fatalf("VPC port admission missing %q: %#v", required, admission)
|
|
|
}
|
|
|
}
|
|
|
for _, global := range []string{
|
|
|
"catalog:vpc-host/state", "catalog:vpc/" + request.VPCKey,
|
|
|
} {
|
|
|
if slices.Contains(admission.LockKeys, global) {
|
|
|
t.Fatalf("VPC port admission still has global lock %q: %#v", global, admission)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func TestPeerMigrationReceiveAdmissionIncludesVPCIdentity(t *testing.T) {
|
|
|
const uuid = "123e4567-e89b-42d3-a456-426614174000"
|
|
|
port := validVPCPortMetadata()
|
|
|
admission := ReceivePeerMigrationAdmission(ReceivePeerMigrationRequest{
|
|
|
CreateVMRequest: CreateVMRequest{
|
|
|
UUID: uuid, PublicNetwork: dualStackPublicNetwork(),
|
|
|
},
|
|
|
MigrationID: strings.Repeat("1", 32),
|
|
|
VPCPort: &port,
|
|
|
})
|
|
|
for _, required := range []string{
|
|
|
"catalog:vpc-host/state",
|
|
|
catalogLockKey("vpc", port.VPCKey),
|
|
|
catalogLockKey("vpc-port", port.PortKey),
|
|
|
networkIdentityLockKey(port.PrivateIP),
|
|
|
networkIdentityLockKey(port.MAC),
|
|
|
} {
|
|
|
if !slices.Contains(admission.LockKeys, required) {
|
|
|
t.Fatalf("peer migration receive admission missing %q: %#v", required, admission)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func TestSnapshotAndReinstallAdmissionsUseDeterministicVolumes(t *testing.T) {
|
|
|
const uuid = "123e4567-e89b-42d3-a456-426614174000"
|
|
|
reinstall := ReinstallVMAdmission(uuid, ReinstallRequest{
|
|
|
OperationID: "operation-reinstall", TemplateID: "debian-12",
|
|
|
})
|
|
|
if !reinstall.HeavyIO || !containsString(reinstall.LockKeys,
|
|
|
volumeLockKey(managedStoragePoolName, reinstallVolumeName(uuid, "operation-reinstall"))) {
|
|
|
t.Fatalf("reinstall admission = %#v", reinstall)
|
|
|
}
|
|
|
restore := RestoreSnapshotAdmission(uuid, "0123456789abcdef0123456789abcdef", "operation-restore")
|
|
|
for _, want := range []string{
|
|
|
volumeLockKey(managedStoragePoolName, snapshotVolumeName(uuid, "0123456789abcdef0123456789abcdef")),
|
|
|
volumeLockKey(managedStoragePoolName, snapshotRestoreVolumeName(uuid, "operation-restore")),
|
|
|
} {
|
|
|
if !restore.HeavyIO || !containsString(restore.LockKeys, want) {
|
|
|
t.Fatalf("restore admission %q missing from %#v", want, restore)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func TestChangeNetworkAdmissionSharesTargetNetworkIdentityWithCreate(t *testing.T) {
|
|
|
const uuid = "123e4567-e89b-42d3-a456-426614174000"
|
|
|
public := dualStackPublicNetwork()
|
|
|
create := CreateVMAdmission(CreateVMRequest{
|
|
|
UUID: uuid, TemplateID: "debian-12", PublicNetwork: public,
|
|
|
})
|
|
|
change := ChangeNetworkAdmission(uuid, public)
|
|
|
for _, cidr := range public.IdentityCIDRs() {
|
|
|
want := networkIdentityLockKey(cidr)
|
|
|
if !containsString(create.LockKeys, want) || !containsString(change.LockKeys, want) {
|
|
|
t.Fatalf("network identity %q missing from create=%#v change=%#v", want, create, change)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func TestResizeResourcesAdmissionLocksDomainAndEveryManagedVMVolume(t *testing.T) {
|
|
|
const uuid = "123E4567-E89B-42D3-A456-426614174000"
|
|
|
request := ResizeResourcesRequest{FromDiskGiB: 20, TargetDiskGiB: 40}
|
|
|
admission := ResizeResourcesAdmission(uuid, request)
|
|
|
want := []string{
|
|
|
domainLockKey(strings.ToLower(uuid)),
|
|
|
vmManagedVolumesLockKey(strings.ToLower(uuid)),
|
|
|
}
|
|
|
if !admission.HeavyIO || !reflect.DeepEqual(admission.LockKeys, want) {
|
|
|
t.Fatalf("disk resize admission = %#v, want %#v", admission, want)
|
|
|
}
|
|
|
request.TargetDiskGiB = request.FromDiskGiB
|
|
|
if admission := ResizeResourcesAdmission(uuid, request); admission.HeavyIO {
|
|
|
t.Fatalf("CPU/memory-only resize unexpectedly consumed a heavy-I/O slot: %#v", admission)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func TestNetworkIdentityAdmissionCanonicalizesAddressAndLocksDeterministicMAC(t *testing.T) {
|
|
|
if networkIdentityLockKey("192.0.2.10/24") != networkIdentityLockKey("192.0.2.10/25") {
|
|
|
t.Fatal("the same public address with different prefix lengths uses different locks")
|
|
|
}
|
|
|
first := vmMACAddress("123e4567-e89b-42d3-a456-426614174000")
|
|
|
second := vmMACAddress("123e4567-e89b-42d3-a456-426614174000")
|
|
|
if first != second || !strings.HasPrefix(first, "02:") {
|
|
|
t.Fatalf("deterministic local MAC = %q then %q", first, second)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func TestCatalogNameAdmissionIsCaseInsensitiveAndOwnerScoped(t *testing.T) {
|
|
|
first := ImportTemplateAdmission(ImportTemplateRequest{ID: "template-a", Name: "Debian 12"})
|
|
|
second := ImportTemplateAdmission(ImportTemplateRequest{ID: "template-b", Name: " debian 12 "})
|
|
|
if first.LockKeys[1] != second.LockKeys[1] {
|
|
|
t.Fatalf("equivalent template names use different locks: %q != %q", first.LockKeys[1], second.LockKeys[1])
|
|
|
}
|
|
|
for _, pair := range [][2]string{{"Σ", "ς"}, {"S", "ſ"}} {
|
|
|
left := ImportTemplateAdmission(ImportTemplateRequest{ID: "template-left", Name: pair[0]})
|
|
|
right := ImportTemplateAdmission(ImportTemplateRequest{ID: "template-right", Name: pair[1]})
|
|
|
if left.LockKeys[1] != right.LockKeys[1] {
|
|
|
t.Fatalf("EqualFold names %q and %q use different locks: %q != %q",
|
|
|
pair[0], pair[1], left.LockKeys[1], right.LockKeys[1])
|
|
|
}
|
|
|
}
|
|
|
ownerOne := ImportUserISOAdmission(ImportUserISORequest{OwnerID: 1, ID: "0123456789abcdef0123456789abcdef", Name: "tools"})
|
|
|
ownerTwo := ImportUserISOAdmission(ImportUserISORequest{OwnerID: 2, ID: "0123456789abcdef0123456789abcdef", Name: "tools"})
|
|
|
if ownerOne.LockKeys[1] == ownerTwo.LockKeys[1] {
|
|
|
t.Fatalf("different owners unexpectedly share a user ISO name lock: %#v %#v", ownerOne, ownerTwo)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func containsString(values []string, want string) bool {
|
|
|
for _, value := range values {
|
|
|
if value == want {
|
|
|
return true
|
|
|
}
|
|
|
}
|
|
|
return false
|
|
|
}
|