| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/base64" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "runtime" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 16 | ) |
| 17 | |
| 18 | |
| 19 | |
| 20 | func TestWindowsGuestProjectAndResourceE2E(t *testing.T) { |
| 21 | profile := os.Getenv("CAGE_TEST_WINDOWS_WORKBENCH_PROFILE") |
| 22 | if profile == "" { |
| 23 | t.Skip("set CAGE_TEST_WINDOWS_WORKBENCH_PROFILE to an isolated paired QA profile") |
| 24 | } |
| 25 | if runtime.GOOS != "windows" { |
| 26 | t.Skip("requires native Windows Hyper-V host") |
| 27 | } |
| 28 | binding, err := guestos.LoadWindowsProfile(profile) |
| 29 | if err != nil || binding == nil { |
| 30 | t.Fatalf("paired QA profile unavailable: %v", err) |
| 31 | } |
| 32 | s := &Server{profile: profile, configPath: filepath.Join(profile, "env.json"), windowsGuest: binding, ready: true} |
| 33 | s.lifetimeContext() |
| 34 | defer s.lifeCancel() |
| 35 | defer s.closeProjectShares() |
| 36 | project := t.TempDir() |
| 37 | outside := t.TempDir() |
| 38 | if err = os.WriteFile(filepath.Join(project, "hello.txt"), []byte("first live value"), 0600); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | if err = os.Mkdir(filepath.Join(project, ".git"), 0700); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | if err = os.WriteFile(filepath.Join(project, ".git", "config"), []byte("protected"), 0600); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | if err = os.WriteFile(filepath.Join(outside, "private.txt"), []byte("outside selected project"), 0600); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | ctx, cancel := context.WithTimeout(s.lifetimeContext(), 60*time.Second) |
| 51 | defer cancel() |
| 52 | if _, err = s.windowsStatus(ctx); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | owners, err := controllerVMOwners(profile) |
| 56 | if err != nil || len(owners) != 1 || owners[0] != "windows/"+binding.VMID.String()+"/Running" { |
| 57 | t.Fatalf("real desktop controller missed the bound Hyper-V workload: %v %v", owners, err) |
| 58 | } |
| 59 | if err = s.ensureProjectShare(project); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | mapped, err := s.windowsProjectForHost(project) |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | command, err := guestos.WindowsRelayCommand(ctx, profile, "terminal") |
| 67 | if err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | terminal, err := startWindowsGuestTerminalAt(command, 100, 30, mapped) |
| 71 | if err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | defer terminal.Close() |
| 75 | verify := func(script string) { |
| 76 | t.Helper() |
| 77 | marker := "CAGE-REAL-" + randomToken() |
| 78 | encoded := base64.StdEncoding.EncodeToString([]byte(marker)) |
| 79 | input := `& { $ErrorActionPreference='Stop'; ` + script + `; Write-Output ([Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('` + encoded + `'))) }` + "\r" |
| 80 | if _, err := io.WriteString(terminal, input); err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | finished := make(chan error, 1) |
| 84 | go func() { |
| 85 | var output strings.Builder |
| 86 | buffer := make([]byte, 8192) |
| 87 | for output.Len() < 1<<20 { |
| 88 | n, err := terminal.Read(buffer) |
| 89 | if n > 0 { |
| 90 | output.Write(buffer[:n]) |
| 91 | if strings.Contains(output.String(), marker) { |
| 92 | finished <- nil |
| 93 | return |
| 94 | } |
| 95 | } |
| 96 | if err != nil { |
| 97 | finished <- err |
| 98 | return |
| 99 | } |
| 100 | } |
| 101 | finished <- fmt.Errorf("terminal output limit") |
| 102 | }() |
| 103 | select { |
| 104 | case err := <-finished: |
| 105 | if err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | case <-ctx.Done(): |
| 109 | terminal.Close() |
| 110 | t.Fatal("real guest terminal probe timed out") |
| 111 | } |
| 112 | } |
| 113 | verify(`if([IO.File]::ReadAllText('hello.txt') -ne 'first live value'){throw 'initial shared read failed'}`) |
| 114 | if err = os.WriteFile(filepath.Join(project, "hello.txt"), []byte("host changed value"), 0600); err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | unicode := base64.StdEncoding.EncodeToString([]byte("原子保存🙂")) |
| 118 | verify(`if([IO.File]::ReadAllText('hello.txt') -ne 'host changed value'){throw 'host live change failed'}; [IO.File]::WriteAllText('atomic.tmp',[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('` + unicode + `')),[Text.UTF8Encoding]::new($false)); Move-Item -Force atomic.tmp saved.txt; $denied=$false;try{[IO.File]::WriteAllText('.GiT/config','altered')}catch{$denied=$true};if(!$denied){throw 'protected metadata write accepted'}`) |
| 119 | if data, err := os.ReadFile(filepath.Join(project, "saved.txt")); err != nil || string(data) != "原子保存🙂" { |
| 120 | t.Fatalf("real atomic save failed: %q %v", data, err) |
| 121 | } |
| 122 | if data, err := os.ReadFile(filepath.Join(project, ".git", "config")); err != nil || string(data) != "protected" { |
| 123 | t.Fatal("real guest changed protected metadata") |
| 124 | } |
| 125 | terminal.Close() |
| 126 | _ = terminal.Wait() |
| 127 | s.closeProjectShares() |
| 128 | command, err = guestos.WindowsRelayCommand(ctx, profile, "terminal") |
| 129 | if err != nil { |
| 130 | t.Fatal(err) |
| 131 | } |
| 132 | if rejected, err := startWindowsGuestTerminalAt(command, 100, 30, mapped); err == nil { |
| 133 | rejected.Close() |
| 134 | t.Fatal("revoked real project grant still starts a terminal") |
| 135 | } |
| 136 | state, err := guestos.WindowsVMResources(ctx, profile, 0, 0) |
| 137 | if err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | sample, err := s.windowsResourceSample(ctx, state) |
| 141 | if err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | t.Logf("real Windows: live shared file, atomic save, .git protection, revoked grant; %d vCPUs and guest memory sample present=%v", state.CPUs, sample["guest"] != nil) |
| 145 | } |