|
| 1 | +package launcher |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io/fs" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "runtime" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +// resolvableTarget picks a binary we know is on PATH for the host running |
| 15 | +// the test, so the exec.LookPath inside ResolveTarget actually succeeds. |
| 16 | +// macOS CI doesn't have powershell.exe; Windows local devs don't have |
| 17 | +// /bin/sh. Pick per-OS so the cross-platform tests stay honest about |
| 18 | +// covering the resolve path end-to-end rather than just the argv parsing. |
| 19 | +func resolvableTarget(t *testing.T) (basename, wantSuffix string) { |
| 20 | + t.Helper() |
| 21 | + if runtime.GOOS == "windows" { |
| 22 | + // cmd.exe ships on every Windows host the test would ever run on. |
| 23 | + return "cmd.exe", "cmd.exe" |
| 24 | + } |
| 25 | + return "sh", "/sh" |
| 26 | +} |
| 27 | + |
| 28 | +func TestResolveTarget_ExecMode_ForwardsArgs(t *testing.T) { |
| 29 | + basename, wantSuffix := resolvableTarget(t) |
| 30 | + target, args, err := ResolveTarget([]string{"--exec", basename, "-c", "echo hi"}) |
| 31 | + if err != nil { |
| 32 | + t.Fatalf("ResolveTarget returned error: %v", err) |
| 33 | + } |
| 34 | + if !strings.HasSuffix(target, wantSuffix) { |
| 35 | + t.Errorf("target = %q, want suffix %q", target, wantSuffix) |
| 36 | + } |
| 37 | + if len(args) != 2 || args[0] != "-c" || args[1] != "echo hi" { |
| 38 | + t.Errorf("args = %v, want [-c, \"echo hi\"]", args) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// --exec with no further args is the misuse case: a customer-supplied |
| 43 | +// task definition that includes the flag but no target. Must error |
| 44 | +// rather than fall through to the default agent-sibling path — that |
| 45 | +// would silently swallow a malformed task action. |
| 46 | +func TestResolveTarget_ExecMode_MissingTarget(t *testing.T) { |
| 47 | + _, _, err := ResolveTarget([]string{"--exec"}) |
| 48 | + if err == nil { |
| 49 | + t.Fatal("expected error for --exec with no target, got nil") |
| 50 | + } |
| 51 | + if !strings.Contains(err.Error(), ExecFlag) { |
| 52 | + t.Errorf("error %q does not mention %q", err, ExecFlag) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// An --exec target that doesn't resolve through LookPath must produce a |
| 57 | +// distinct error rather than silently exiting (which Task Scheduler |
| 58 | +// would record as a generic non-zero exit, indistinguishable from a |
| 59 | +// transient failure). exec.LookPath is wrapped, so the inner error is |
| 60 | +// preserved via errors.Is for diagnostic chains. |
| 61 | +func TestResolveTarget_ExecMode_TargetNotFound(t *testing.T) { |
| 62 | + bogus := "this-binary-definitely-does-not-exist-on-PATH-xyzzy.exe" |
| 63 | + _, _, err := ResolveTarget([]string{"--exec", bogus}) |
| 64 | + if err == nil { |
| 65 | + t.Fatalf("expected error resolving %q, got nil", bogus) |
| 66 | + } |
| 67 | + if !errors.Is(err, exec.ErrNotFound) { |
| 68 | + t.Errorf("expected error chain to include exec.ErrNotFound, got %v", err) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// --exec with no further child args means "spawn target with empty argv" |
| 73 | +// — useful for the trivial case (launcher.exe --exec foo.exe) where the |
| 74 | +// child doesn't need flags. Forwards an empty slice, not a nil slice |
| 75 | +// (callers passing this directly into exec.Command must get a usable |
| 76 | +// value). |
| 77 | +func TestResolveTarget_ExecMode_NoChildArgs(t *testing.T) { |
| 78 | + basename, _ := resolvableTarget(t) |
| 79 | + _, args, err := ResolveTarget([]string{"--exec", basename}) |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("ResolveTarget returned error: %v", err) |
| 82 | + } |
| 83 | + if args == nil { |
| 84 | + t.Error("args is nil; expected empty (but non-nil) slice") |
| 85 | + } |
| 86 | + if len(args) != 0 { |
| 87 | + t.Errorf("args = %v, want []", args) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Default mode (no --exec) computes the sibling agent path relative to |
| 92 | +// os.Executable(). During `go test` os.Executable() returns the compiled |
| 93 | +// test binary, so we can stage a fake agent next to it and assert |
| 94 | +// ResolveTarget picks it up. This is the lever the MSI install relies |
| 95 | +// on — the launcher's directory dictates where the agent comes from. |
| 96 | +func TestResolveTarget_DefaultMode_FindsSibling(t *testing.T) { |
| 97 | + exePath, err := os.Executable() |
| 98 | + if err != nil { |
| 99 | + t.Skipf("os.Executable not available on this platform: %v", err) |
| 100 | + } |
| 101 | + siblingDir := filepath.Dir(exePath) |
| 102 | + siblingPath := filepath.Join(siblingDir, AgentBinary) |
| 103 | + |
| 104 | + // Don't clobber a real agent that might be sitting next to the test |
| 105 | + // binary in a developer's checkout. Three cases to handle correctly: |
| 106 | + // |
| 107 | + // - File doesn't exist (ErrNotExist) — seed the fake, delete on |
| 108 | + // cleanup. Normal CI path. |
| 109 | + // - File exists and is readable — capture bytes + mode, seed the |
| 110 | + // fake, restore both on cleanup. |
| 111 | + // - File exists but can't be read (permission, locked) — we'd |
| 112 | + // destroy the developer's checkout if we overwrote it. Skip |
| 113 | + // the test instead. |
| 114 | + prev, readErr := os.ReadFile(siblingPath) |
| 115 | + var prevMode os.FileMode = 0o644 |
| 116 | + if readErr == nil { |
| 117 | + // Capture the original mode so the file is restored byte-for- |
| 118 | + // byte AND mode-for-mode. Without this, a real Authenticode- |
| 119 | + // signed .exe with the executable bit set on POSIX (rare on |
| 120 | + // CI but possible in a dual-platform checkout) would come back |
| 121 | + // non-executable. |
| 122 | + if info, statErr := os.Stat(siblingPath); statErr == nil { |
| 123 | + prevMode = info.Mode().Perm() |
| 124 | + } |
| 125 | + } else if !errors.Is(readErr, fs.ErrNotExist) { |
| 126 | + t.Skipf("cannot read existing sibling %q without risking developer state: %v", siblingPath, readErr) |
| 127 | + } |
| 128 | + |
| 129 | + if err := os.WriteFile(siblingPath, []byte("fake-agent"), 0o644); err != nil { |
| 130 | + t.Fatalf("seeding fake agent at %q: %v", siblingPath, err) |
| 131 | + } |
| 132 | + t.Cleanup(func() { |
| 133 | + if readErr == nil { |
| 134 | + // Restore both bytes and mode. |
| 135 | + _ = os.WriteFile(siblingPath, prev, prevMode) |
| 136 | + } else { |
| 137 | + // Original was absent; remove our seed. |
| 138 | + _ = os.Remove(siblingPath) |
| 139 | + } |
| 140 | + }) |
| 141 | + |
| 142 | + target, args, err := ResolveTarget([]string{"send-telemetry", "--install-dir=C:\\fake"}) |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("ResolveTarget returned error: %v", err) |
| 145 | + } |
| 146 | + if target != siblingPath { |
| 147 | + t.Errorf("target = %q, want %q", target, siblingPath) |
| 148 | + } |
| 149 | + if len(args) != 2 || args[0] != "send-telemetry" || args[1] != "--install-dir=C:\\fake" { |
| 150 | + t.Errorf("args = %v, did not pass through unchanged", args) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +// When the sibling agent isn't on disk, ResolveTarget must return an |
| 155 | +// error rather than a non-existent path. The launcher exit-1's on this; |
| 156 | +// surfacing it as an explicit error here means callers (and tests) can |
| 157 | +// distinguish "no agent installed" from other failure modes. |
| 158 | +func TestResolveTarget_DefaultMode_NoSibling(t *testing.T) { |
| 159 | + exePath, err := os.Executable() |
| 160 | + if err != nil { |
| 161 | + t.Skipf("os.Executable not available on this platform: %v", err) |
| 162 | + } |
| 163 | + siblingPath := filepath.Join(filepath.Dir(exePath), AgentBinary) |
| 164 | + if _, statErr := os.Stat(siblingPath); statErr == nil { |
| 165 | + // A real or stale sibling agent is in the way of this assertion. |
| 166 | + // Move it aside for the test and restore on cleanup so we don't |
| 167 | + // leave a half-broken dev checkout behind. |
| 168 | + shadow := siblingPath + ".test-shadow" |
| 169 | + if renameErr := os.Rename(siblingPath, shadow); renameErr != nil { |
| 170 | + t.Skipf("could not move existing sibling agent %q out of the way: %v", siblingPath, renameErr) |
| 171 | + } |
| 172 | + t.Cleanup(func() { _ = os.Rename(shadow, siblingPath) }) |
| 173 | + } |
| 174 | + |
| 175 | + _, _, err = ResolveTarget(nil) |
| 176 | + if err == nil { |
| 177 | + t.Fatal("expected error when sibling agent is absent, got nil") |
| 178 | + } |
| 179 | +} |
0 commit comments