baseVolumeName was base-<key>.<format>, byte-identical to what the kvm provider builds in the same pool -- for URL images too, since kvm takes hex(sum[:8]) and kvm2 takes hex(sum[:])[:16], the same eight bytes -- with a completely separate lock table. The shipped config has a live kvm-home entry and a commented-out kvm2-home entry on the same host, the same key debian-13 and the same default pool, and the comment invites the operator to enable it. Both integration tests use key test-image. On beelink both live overlays back onto kvm's base-debian-13.qcow2. Since cachedImage accepts any 64-hex marker when no sha256 is configured and never re-hashes the base, that meant two entries pointing one key at different URLs silently shared a file; a concurrent cold fetch let one entry delete the other's half-uploaded base and boot a guest from the remains; and a sha256 edit deleted the base in place under live overlays, which libvirt permits without complaint. The name now carries the provider entry: base-<entry>-<key>.<format>. imageSource gained an Entry field and resolveImage takes the provider name, so every production call site follows from imageSource.volumeName(). That closes the cross-entry sharing outright, kvm<->kvm2 and kvm2<->kvm2 alike, and turns the per-Provider imageLocks from a latent bug into a correct one: with names unique per entry, two entries cannot contend for a file, so no process-wide lock table is needed. The entry name now reaches a file name, so New checks it against the existing kvmIdentifier rule, exported as provider.IsKVMIdentifier rather than duplicated. Provider names are otherwise validated for all types at once and only for being non-empty and unique, so a name with a slash would have produced a volume nothing could address. One companion fix, because the naming alone does not stop it: cachedImage's digest-mismatch branch deleted the marker and the base and refetched. It now returns ErrPermanent naming both digests and telling the operator what to do. That branch was the remaining path that unlinks a good file under running domains, and the contract already said "add a new key instead" -- the code was contradicting it. The no-marker and non-hex-marker branches keep deleting, since there the base is debris. Not fixed, and said so in the docs rather than left to look closed: editing images.<key>.url is still a silent no-op, since nothing records the URL; and cross-process fencing still needs the deferred atomic publish (upload to a temp name, verify, publish), which lands where H2 put the discard. Migration is a non-event. The base-debian-13.qcow2 on beelink is kvm's, kvm keeps using it untouched, and kvm2 downloads its own copy once per host. Nothing is replaced under a running guest; the pool needs room for both, which the prerequisites now say. The eviction recipe in the guide named base-debian-13.qcow2, which after this change is kvm's file -- following it would have deleted the wrong provider's base. It now greps for the prefix and deletes the marker first. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
157 lines
5.4 KiB
Go
157 lines
5.4 KiB
Go
package kvm2
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"errors"
|
|
"regexp"
|
|
"testing"
|
|
|
|
"gitea.home.hrajfrisbee.cz/kacerr/egress-proxies-operator/internal/provider"
|
|
)
|
|
|
|
type checkedVolume struct {
|
|
Type string `xml:"type,attr"`
|
|
Name string `xml:"name"`
|
|
Capacity struct {
|
|
Unit string `xml:"unit,attr"`
|
|
Value int64 `xml:",chardata"`
|
|
} `xml:"capacity"`
|
|
Target struct {
|
|
Format struct {
|
|
Type string `xml:"type,attr"`
|
|
} `xml:"format"`
|
|
} `xml:"target"`
|
|
BackingStores []struct {
|
|
Path string `xml:"path"`
|
|
Format struct {
|
|
Type string `xml:"type,attr"`
|
|
} `xml:"format"`
|
|
} `xml:"backingStore"`
|
|
}
|
|
|
|
func mustBuildVolume(t *testing.T, v volumeSpec) checkedVolume {
|
|
t.Helper()
|
|
out, err := buildVolumeXML(v)
|
|
if err != nil {
|
|
t.Fatalf("buildVolumeXML: %v", err)
|
|
}
|
|
var c checkedVolume
|
|
if err := xml.Unmarshal([]byte(out), &c); err != nil {
|
|
t.Fatalf("unmarshalling produced XML: %v\n%s", err, out)
|
|
}
|
|
return c
|
|
}
|
|
|
|
func TestVolumeXML_overlayBackingStore(t *testing.T) {
|
|
t.Parallel()
|
|
c := mustBuildVolume(t, volumeSpec{
|
|
Name: "proxy-abc.qcow2",
|
|
CapacityBytes: 10 << 30,
|
|
Format: provider.KVMFormatQcow2,
|
|
BackingPath: "/var/lib/libvirt/images/base-debian-13.qcow2",
|
|
BackingFormat: provider.KVMFormatQcow2,
|
|
})
|
|
if c.Type != "file" || c.Name != "proxy-abc.qcow2" {
|
|
t.Errorf("type=%q name=%q, want file/proxy-abc.qcow2", c.Type, c.Name)
|
|
}
|
|
if c.Capacity.Unit != "bytes" || c.Capacity.Value != 10<<30 {
|
|
t.Errorf("capacity = %d %s, want %d bytes", c.Capacity.Value, c.Capacity.Unit, 10<<30)
|
|
}
|
|
if c.Target.Format.Type != "qcow2" {
|
|
t.Errorf("target format = %q, want qcow2", c.Target.Format.Type)
|
|
}
|
|
if len(c.BackingStores) != 1 {
|
|
t.Fatalf("got %d backingStore elements, want 1", len(c.BackingStores))
|
|
}
|
|
bs := c.BackingStores[0]
|
|
if bs.Path != "/var/lib/libvirt/images/base-debian-13.qcow2" || bs.Format.Type != "qcow2" {
|
|
t.Errorf("backingStore = %+v, want the base path with qcow2 format", bs)
|
|
}
|
|
}
|
|
|
|
func TestVolumeXML_rawWithoutBacking(t *testing.T) {
|
|
t.Parallel()
|
|
c := mustBuildVolume(t, volumeSpec{Name: "proxy-abc-seed.iso", CapacityBytes: 385024, Format: provider.KVMFormatRaw})
|
|
if c.Target.Format.Type != "raw" {
|
|
t.Errorf("target format = %q, want raw", c.Target.Format.Type)
|
|
}
|
|
if c.Capacity.Value != 385024 || c.Capacity.Unit != "bytes" {
|
|
t.Errorf("capacity = %d %s, want 385024 bytes", c.Capacity.Value, c.Capacity.Unit)
|
|
}
|
|
if len(c.BackingStores) != 0 {
|
|
t.Errorf("got backingStore %+v, want none", c.BackingStores)
|
|
}
|
|
}
|
|
|
|
func TestVolumeXML_rejectsBadFormat(t *testing.T) {
|
|
t.Parallel()
|
|
tests := []struct {
|
|
name string
|
|
spec volumeSpec
|
|
}{
|
|
{name: "vmdk format", spec: volumeSpec{Name: "x", CapacityBytes: 1, Format: "vmdk"}},
|
|
{name: "empty format", spec: volumeSpec{Name: "x", CapacityBytes: 1}},
|
|
{name: "bad backing format", spec: volumeSpec{Name: "x", CapacityBytes: 1, Format: "qcow2", BackingPath: "/b", BackingFormat: "vdi"}},
|
|
{name: "backing path without format", spec: volumeSpec{Name: "x", CapacityBytes: 1, Format: "qcow2", BackingPath: "/b"}},
|
|
{name: "empty name", spec: volumeSpec{CapacityBytes: 1, Format: "qcow2"}},
|
|
{name: "zero capacity", spec: volumeSpec{Name: "x", Format: "qcow2"}},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
out, err := buildVolumeXML(tc.spec)
|
|
if !errors.Is(err, provider.ErrPermanent) {
|
|
t.Errorf("err = %v, want ErrPermanent", err)
|
|
}
|
|
if out != "" {
|
|
t.Errorf("got XML %q alongside the error", out)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestVolumeNames(t *testing.T) {
|
|
t.Parallel()
|
|
tests := []struct {
|
|
name string
|
|
got string
|
|
want string
|
|
}{
|
|
{name: "overlay", got: overlayVolumeName("proxy-abc"), want: "proxy-abc.qcow2"},
|
|
{name: "seed", got: seedVolumeName("proxy-abc"), want: "proxy-abc-seed.iso"},
|
|
{name: "base qcow2", got: baseVolumeName("kvm2-home", "debian-13", provider.KVMFormatQcow2), want: "base-kvm2-home-debian-13.qcow2"},
|
|
{name: "base raw", got: baseVolumeName("kvm2-home", "alpine", provider.KVMFormatRaw), want: "base-kvm2-home-alpine.raw"},
|
|
{name: "marker", got: markerVolumeName("base-debian-13.qcow2"), want: "base-debian-13.qcow2.sha256"},
|
|
{name: "marker of url base", got: markerVolumeName(baseVolumeName("kvm2-home", urlImageKey("https://x/y.img"), "qcow2")), want: "base-kvm2-home-" + urlImageKey("https://x/y.img") + ".qcow2.sha256"},
|
|
// Two entries pointing one key at different images must not agree
|
|
// on a file name, and neither may collide with the kvm provider's
|
|
// base-<key>.<format> in the same pool.
|
|
{name: "another entry, same key", got: baseVolumeName("kvm2-lab", "debian-13", provider.KVMFormatQcow2), want: "base-kvm2-lab-debian-13.qcow2"},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
if tc.got != tc.want {
|
|
t.Errorf("got %q, want %q", tc.got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
|
|
t.Run("url key", func(t *testing.T) {
|
|
t.Parallel()
|
|
hex16 := regexp.MustCompile(`^[0-9a-f]{16}$`)
|
|
a := urlImageKey("https://cloud.debian.org/images/cloud/trixie/latest/debian-13-generic-amd64.qcow2")
|
|
b := urlImageKey("https://cloud.debian.org/images/cloud/trixie/latest/debian-13-generic-amd64.qcow2")
|
|
c := urlImageKey("https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2")
|
|
if !hex16.MatchString(a) {
|
|
t.Errorf("urlImageKey = %q, want 16 lowercase hex chars", a)
|
|
}
|
|
if a != b {
|
|
t.Errorf("urlImageKey not deterministic: %q vs %q", a, b)
|
|
}
|
|
if a == c {
|
|
t.Errorf("urlImageKey gives %q for two different URLs", a)
|
|
}
|
|
})
|
|
}
|