| 1 | package vm |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/sylvinus/agent-vm/internal/cage" |
| 12 | "github.com/sylvinus/agent-vm/internal/hostimage" |
| 13 | ) |
| 14 | |
| 15 | func TestHostLinuxImageHasNoFallbackOrHostMounts(t *testing.T) { |
| 16 | l := newTestLima(t) |
| 17 | directory := t.TempDir() |
| 18 | config := filepath.Join(directory, "config.json") |
| 19 | data, _ := json.Marshal(cage.DefaultConfig()) |
| 20 | if err := os.WriteFile(config, data, 0600); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | t.Setenv(cage.ConfigEnv, config) |
| 24 | plan := hostimage.Plan{Host: hostimage.Identity{System: "ubuntu-22.04", Version: "22.04", Arch: "amd64"}, Version: "22.04", Format: "qcow2", Path: filepath.Join(directory, "ubuntu.img"), Digest: "sha256:" + strings.Repeat("a", 64)} |
| 25 | if err := hostimage.WriteLinuxTemplate(directory, plan); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | template, err := hostimage.LinuxTemplate(directory) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | none := []Mount{} |
| 33 | if err := l.Create(t.Context(), "x", template, Settings{Mounts: &none, NoContainerd: true}, io.Discard); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | instance, err := l.inspect(t.Context(), "x") |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | if len(instance.Config.Images) != 1 || instance.Config.Images[0].Location != plan.Path || string(instance.Config.Images[0].Digest) != plan.Digest { |
| 41 | t.Fatalf("unexpected image fallback: %+v", instance.Config.Images) |
| 42 | } |
| 43 | if len(instance.Config.Mounts) != 0 { |
| 44 | t.Fatal("host directories were mounted") |
| 45 | } |
| 46 | if err := hostimage.ClearLinuxTemplate(directory, plan); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | if path, err := hostimage.LinuxTemplate(directory); err != nil || path != "template:debian-13" { |
| 50 | t.Fatalf("selection was retained: %q %v", path, err) |
| 51 | } |
| 52 | } |