| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/sylvinus/agent-vm/internal/state" |
| 12 | "github.com/sylvinus/agent-vm/internal/vm/vmtest" |
| 13 | ) |
| 14 | |
| 15 | |
| 16 | |
| 17 | func TestOwnLimaHome(t *testing.T) { |
| 18 | home := t.TempDir() |
| 19 | t.Setenv("HOME", home) |
| 20 | |
| 21 | t.Setenv("USERPROFILE", home) |
| 22 | lima := t.TempDir() |
| 23 | t.Setenv("LIMA_HOME", lima) |
| 24 | t.Setenv("AGENT_VM_LIMA_HOME", lima+"/") |
| 25 | e, err := newApp(IO{Stdin: strings.NewReader(""), Stdout: io.Discard, Stderr: io.Discard}) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if _, err := e.backend(); err == nil || !strings.Contains(err.Error(), "your own Lima's home") { |
| 30 | t.Errorf("own home: %v", err) |
| 31 | } |
| 32 | |
| 33 | |
| 34 | os.Unsetenv("LIMA_HOME") |
| 35 | t.Setenv("AGENT_VM_LIMA_HOME", "") |
| 36 | for i := range 2 { |
| 37 | e, _ := newApp(IO{Stdin: strings.NewReader(""), Stdout: io.Discard, Stderr: io.Discard}) |
| 38 | if _, err := e.backend(); err != nil { |
| 39 | t.Errorf("command %d: %v", i+1, err) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | func TestMigrateNotFromReaders(t *testing.T) { |
| 48 | home := t.TempDir() |
| 49 | t.Setenv("HOME", home) |
| 50 | |
| 51 | t.Setenv("USERPROFILE", home) |
| 52 | for _, args := range [][]string{{"doctor"}, {"info"}, {"list"}, {"stop", "x"}} { |
| 53 | te := newTestEnv(t, vmtest.New()) |
| 54 | moved := false |
| 55 | te.migrate = func() { moved = true } |
| 56 | te.run(args...) |
| 57 | if want := args[0] == "stop"; moved != want { |
| 58 | t.Errorf("%s: moved %v", args[0], moved) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestMigrateOld(t *testing.T) { |
| 64 | old := t.TempDir() |
| 65 | now := filepath.Join(t.TempDir(), "lima") |
| 66 | |
| 67 | |
| 68 | os.MkdirAll(filepath.Join(now, "p-12345678"), 0o755) |
| 69 | os.MkdirAll(filepath.Join(old, "agent-vm-p-12345678"), 0o755) |
| 70 | os.WriteFile(filepath.Join(old, "agent-vm-p-12345678", "lima.yaml"), []byte("{"), 0o644) |
| 71 | os.MkdirAll(filepath.Join(old, "theirs"), 0o755) |
| 72 | os.WriteFile(filepath.Join(old, "theirs", "lima.yaml"), []byte("{"), 0o644) |
| 73 | |
| 74 | te := newTestEnv(t, vmtest.New(), "n\n") |
| 75 | os.MkdirAll(string(te.state), 0o755) |
| 76 | os.WriteFile(te.state.Marker(state.BuiltByOf, "agent-vm-p-12345678"), []byte("0.2.1\n"), 0o644) |
| 77 | te.migrateOld(context.Background(), old, now) |
| 78 | if out := te.out(); !strings.Contains(out, " agent-vm-p-12345678, as p-12345678\n") || strings.Contains(out, "theirs") || !strings.Contains(out, "Not moved") { |
| 79 | t.Errorf("declined:\n%s", out) |
| 80 | } |
| 81 | if _, err := os.Stat(te.state.Path(migratedMarker)); err == nil { |
| 82 | t.Error("declined, and marked as done") |
| 83 | } |
| 84 | |
| 85 | st := te.state |
| 86 | te = newTestEnv(t, te.fake) |
| 87 | te.state = st |
| 88 | te.migrateOld(context.Background(), old, now) |
| 89 | if out := te.out(); !strings.Contains(out, "Error: could not move agent-vm-p-12345678") { |
| 90 | t.Errorf("no terminal, failed move:\n%s", out) |
| 91 | } |
| 92 | if _, err := os.Stat(te.state.Path(migratedMarker)); err == nil { |
| 93 | t.Error("a failed move was marked as done") |
| 94 | } |
| 95 | |
| 96 | |
| 97 | |
| 98 | os.RemoveAll(filepath.Join(now, "p-12345678")) |
| 99 | te.stderr.Reset() |
| 100 | te.migrateOld(context.Background(), old, now) |
| 101 | if out := te.out(); !strings.HasSuffix(out, "Done: they keep their shares and settings, under their new names.\n") { |
| 102 | t.Errorf("no terminal:\n%s", out) |
| 103 | } |
| 104 | if _, err := os.Stat(filepath.Join(now, "p-12345678", "lima.yaml")); err != nil { |
| 105 | t.Error("not moved") |
| 106 | } |
| 107 | if te.state.ReadMarker(state.BuiltByOf, "p-12345678") != "0.2.1" { |
| 108 | t.Error("its markers did not follow") |
| 109 | } |
| 110 | if _, err := os.Stat(filepath.Join(old, "theirs", "lima.yaml")); err != nil { |
| 111 | t.Error("another VM was moved") |
| 112 | } |
| 113 | if _, err := os.Stat(te.state.Path(migratedMarker)); err != nil { |
| 114 | t.Error("not marked as done") |
| 115 | } |
| 116 | os.MkdirAll(filepath.Join(old, "agent-vm-q-12345678"), 0o755) |
| 117 | os.WriteFile(filepath.Join(old, "agent-vm-q-12345678", "lima.yaml"), []byte("{"), 0o644) |
| 118 | te.stderr.Reset() |
| 119 | te.migrateOld(context.Background(), old, now) |
| 120 | if te.out() != "" { |
| 121 | t.Errorf("after the marker: %q", te.out()) |
| 122 | } |
| 123 | |
| 124 | te = newTestEnv(t, te.fake) |
| 125 | te.migrateOld(context.Background(), t.TempDir(), now) |
| 126 | if te.out() != "" { |
| 127 | t.Errorf("nothing to move: %q", te.out()) |
| 128 | } |
| 129 | } |