| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "sync" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/sylvinus/agent-vm/internal/cage" |
| 14 | "github.com/sylvinus/agent-vm/internal/cli" |
| 15 | "github.com/sylvinus/agent-vm/internal/paths" |
| 16 | "github.com/sylvinus/agent-vm/internal/vmname" |
| 17 | ) |
| 18 | |
| 19 | func TestControllerNamesMatchCLIForIndependentProfiles(t *testing.T) { |
| 20 | base := os.TempDir() |
| 21 | if filepath.Separator == '/' { |
| 22 | base = "/tmp" |
| 23 | } |
| 24 | root, err := os.MkdirTemp(base, "vn") |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | defer os.RemoveAll(root) |
| 29 | root, err = filepath.EvalSymlinks(root) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | workspace := filepath.Join(root, strings.Repeat("long-directory-", 6)) |
| 34 | if err = os.Mkdir(workspace, 0700); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | saved, err := json.Marshal(map[string]string{"directory": workspace}) |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | a := &Server{profile: filepath.Join(root, "a")} |
| 42 | b := &Server{profile: filepath.Join(root, "another-profile")} |
| 43 | cliName := func(s *Server) string { |
| 44 | t.Helper() |
| 45 | t.Setenv(cage.ConfigEnv, filepath.Join(s.profile, "config.json")) |
| 46 | t.Setenv("AGENT_VM_STATE_DIR", filepath.Join(s.profile, "state")) |
| 47 | t.Setenv("AGENT_VM_LIMA_HOME", filepath.Join(s.profile, "state", "lima")) |
| 48 | var out, stderr bytes.Buffer |
| 49 | if n := cli.Main([]string{"name", workspace}, cli.IO{Stdout: &out, Stderr: &stderr}); n != 0 { |
| 50 | t.Fatalf("CLI name: %s", stderr.String()) |
| 51 | } |
| 52 | return strings.TrimSpace(out.String()) |
| 53 | } |
| 54 | for _, s := range []*Server{a, b} { |
| 55 | if err = os.Mkdir(s.profile, 0700); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | if err = os.WriteFile(filepath.Join(s.profile, "environment.json"), saved, 0600); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | } |
| 62 | an, bn := cliName(a), cliName(b) |
| 63 | wantA := vmname.HomeLimit(filepath.Join(a.profile, "state", "lima"), false).Name(workspace) |
| 64 | wantB := vmname.HomeLimit(filepath.Join(b.profile, "state", "lima"), false).Name(workspace) |
| 65 | if an != wantA || bn != wantB { |
| 66 | t.Fatalf("CLI names differ from their profile budgets: %q != %q, %q != %q", an, wantA, bn, wantB) |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | if filepath.Separator == '/' && (an == bn || len(bn) >= len(an) || len(an) >= 76) { |
| 72 | t.Fatalf("test requires different truncations: %q, %q", an, bn) |
| 73 | } |
| 74 | var wg sync.WaitGroup |
| 75 | for s, want := range map[*Server]string{a: an, b: bn} { |
| 76 | wg.Add(1) |
| 77 | go func() { |
| 78 | defer wg.Done() |
| 79 | for i := 0; i < 20; i++ { |
| 80 | if got := s.sshTarget(); got != "lima-"+want { |
| 81 | t.Errorf("SSH target %s; CLI name %s", got, want) |
| 82 | return |
| 83 | } |
| 84 | if got, wantDir := s.instanceDir(), filepath.Join(s.profile, "state", "lima", want); !paths.Equal(paths.Host(filepath.Clean(got)), paths.Host(wantDir)) { |
| 85 | t.Errorf("instance directory %s; expected profile-owned directory %s", got, wantDir) |
| 86 | return |
| 87 | } |
| 88 | } |
| 89 | }() |
| 90 | } |
| 91 | wg.Wait() |
| 92 | loc, err := cage.GetStorageLocation(b.profile) |
| 93 | if err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | if _, err = cage.MoveStorage(context.Background(), b.profile, filepath.Join(root, strings.Repeat("data-", 10)), loc, nil); err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | if got := cliName(b); got != bn || b.sshTarget() != "lima-"+bn { |
| 100 | t.Fatalf("storage migration changed VM name: %s -> %s", bn, got) |
| 101 | } |
| 102 | |
| 103 | old := vmname.Limit(len(bn) + 4).Name(workspace) |
| 104 | dir := filepath.Join(b.profile, "state", "lima", old) |
| 105 | if err = os.MkdirAll(dir, 0700); err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | if err = os.WriteFile(filepath.Join(dir, "lima.yaml"), []byte("{}"), 0600); err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | if got := cliName(b); got != old || b.sshTarget() != "lima-"+old { |
| 112 | t.Fatalf("existing disk %s not reused; CLI picked %s", old, got) |
| 113 | } |
| 114 | } |