//go:build libvirt package hypervisor import ( "errors" "reflect" "testing" "git.luoli.live/root/virt/agent/operation" "libvirt.org/go/libvirt" ) const testGiB = uint64(1 << 30) type fakeNewRootVolume struct { calls []string info libvirt.StorageVolInfo resizeErr error infoErr error ignoreResize bool capacity uint64 flags libvirt.StorageVolResizeFlags } func (v *fakeNewRootVolume) Resize(capacity uint64, flags libvirt.StorageVolResizeFlags) error { v.calls = append(v.calls, "resize") v.capacity = capacity v.flags = flags if v.resizeErr == nil && !v.ignoreResize { v.info.Capacity = capacity } return v.resizeErr } func (v *fakeNewRootVolume) GetXMLDesc(uint32) (string, error) { v.calls = append(v.calls, "xml") return ``, nil } func (v *fakeNewRootVolume) GetInfo() (*libvirt.StorageVolInfo, error) { v.calls = append(v.calls, "info") if v.infoErr != nil { return nil, v.infoErr } copy := v.info return ©, nil } func TestEstablishNewRootVolumeCapacityAlwaysResizesBeforeValidation(t *testing.T) { volume := &fakeNewRootVolume{info: libvirt.StorageVolInfo{Capacity: 3 * testGiB}} want := uint64(20 * testGiB) if err := establishNewRootVolumeCapacity(volume, want); err != nil { t.Fatalf("establish capacity: %v", err) } if volume.capacity != want { t.Fatalf("resize capacity = %d, want %d", volume.capacity, want) } if volume.flags != 0 { t.Fatalf("resize flags = %d, want sparse flags 0", volume.flags) } if !reflect.DeepEqual(volume.calls, []string{"resize", "xml", "info"}) { t.Fatalf("calls = %v, want mandatory resize before validation", volume.calls) } } func TestEstablishNewRootVolumeCapacityFailsClosed(t *testing.T) { t.Run("resize failure stops before validation", func(t *testing.T) { volume := &fakeNewRootVolume{resizeErr: errors.New("resize rejected")} if err := establishNewRootVolumeCapacity(volume, 20*testGiB); err == nil { t.Fatal("resize failure was accepted") } if !reflect.DeepEqual(volume.calls, []string{"resize"}) { t.Fatalf("calls = %v, want resize only", volume.calls) } }) t.Run("wrong final capacity is rejected", func(t *testing.T) { volume := &fakeNewRootVolume{ info: libvirt.StorageVolInfo{Capacity: 3 * testGiB}, ignoreResize: true, } err := establishNewRootVolumeCapacity(volume, 20*testGiB) if err == nil || errorPublicCode(err) != "invalid_qcow2_volume" { t.Fatalf("capacity mismatch = %v, want invalid_qcow2_volume", err) } if !reflect.DeepEqual(volume.calls, []string{"resize", "xml", "info"}) { t.Fatalf("calls = %v, want resize then validation", volume.calls) } }) } func TestClassifyUnpublishedVolumeRollbackRequiresAbsenceProof(t *testing.T) { cause := NewError(ErrConflict, "construction_failed", "construction failed", nil) confirmed := classifyUnpublishedVolumeRollback( cause, errors.New("delete response lost"), nil, true, "delete temporary volume", ) if operation.IsUncertain(confirmed) { t.Fatalf("confirmed absence remained uncertain: %v", confirmed) } retained := classifyUnpublishedVolumeRollback(cause, nil, nil, false, "delete temporary volume") if !operation.IsUncertain(retained) { t.Fatalf("retained volume was not uncertain: %v", retained) } unobservable := classifyUnpublishedVolumeRollback( cause, nil, errors.New("pool refresh failed"), false, "delete temporary volume", ) if !operation.IsUncertain(unobservable) { t.Fatalf("unconfirmed absence was not uncertain: %v", unobservable) } }