| 1 | package vm |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestMoveOld(t *testing.T) { |
| 13 | ctx := context.Background() |
| 14 | l := newTestLima(t) |
| 15 | old := os.Getenv("LIMA_HOME") |
| 16 | off := false |
| 17 | proj := t.TempDir() |
| 18 | mounts := []Mount{{Location: proj, MountPoint: proj, Writable: true, SSHFS: &SSHFS{SFTPDriver: "builtin", Cache: &off, ReadonlyNames: []string{".git", ".hg"}}}} |
| 19 | rev := ReverseSSHFS |
| 20 | if err := l.Create(ctx, "agent-vm-p-12345678", "template:debian-13", Settings{CPUs: 1, Mounts: &mounts, MountType: &rev}, io.Discard); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | none := []Mount{} |
| 24 | if err := l.Create(ctx, "other", "template:debian-13", Settings{CPUs: 1, Mounts: &none}, io.Discard); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | dir := filepath.Join(old, "agent-vm-p-12345678") |
| 28 | for _, f := range []string{"ha.sock", "stale.pid", "x.tmp", "protected", "vz-identifier"} { |
| 29 | os.WriteFile(filepath.Join(dir, f), []byte("x"), 0o644) |
| 30 | } |
| 31 | |
| 32 | found, err := FindOld(old, "agent-vm-") |
| 33 | if err != nil || len(found) != 1 || found[0].Name != "agent-vm-p-12345678" || found[0].Running { |
| 34 | t.Fatalf("FindOld: %+v %v", found, err) |
| 35 | } |
| 36 | |
| 37 | now := filepath.Join(t.TempDir(), "lima") |
| 38 | if err := MoveOld(ctx, old, now, "agent-vm-p-12345678", "p-12345678"); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | if _, err := os.Stat(dir); !os.IsNotExist(err) { |
| 42 | t.Error("left in the old home") |
| 43 | } |
| 44 | moved := filepath.Join(now, "p-12345678") |
| 45 | for _, f := range []string{"ha.sock", "stale.pid", "x.tmp", "protected"} { |
| 46 | if _, err := os.Stat(filepath.Join(moved, f)); err == nil { |
| 47 | t.Errorf("%s was moved", f) |
| 48 | } |
| 49 | } |
| 50 | if b, err := os.ReadFile(filepath.Join(moved, "vz-identifier")); err != nil || len(b) != 0 { |
| 51 | t.Errorf("vz-identifier: %q %v", b, err) |
| 52 | } |
| 53 | if _, err := os.Stat(filepath.Join(old, "other")); err != nil { |
| 54 | t.Error("a VM that is not agent-vm's was touched") |
| 55 | } |
| 56 | |
| 57 | l2, err := NewLima(now, t.TempDir()) |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | inst, err := l2.Get(ctx, "p-12345678") |
| 62 | if err != nil || inst.Name != "p-12345678" || inst.MountType != ReverseSSHFS || len(inst.Mounts) != 1 || inst.Mounts[0].Location != proj || inst.Status != Stopped { |
| 63 | t.Fatalf("in the new home: %+v %v", inst, err) |
| 64 | } |
| 65 | |
| 66 | |
| 67 | long := filepath.Join(t.TempDir(), strings.Repeat("d", 90)) |
| 68 | if err := MoveOld(ctx, now, long, "p-12345678", "p-12345678"); err == nil || !strings.Contains(err.Error(), "too long") { |
| 69 | t.Errorf("long path: %v", err) |
| 70 | } |
| 71 | if _, err := os.Stat(moved); err != nil { |
| 72 | t.Error("moved although refused") |
| 73 | } |
| 74 | } |