@@ -538,7 +538,9 @@ func (r *sshRemote) ReadVersionManifest(ctx context.Context, id string) ([]byte,
|
||||
path := filepath.Join(r.config.RemoteArchive, "versions", id+".json")
|
||||
var out strings.Builder
|
||||
// 只有 "文件不存在" 才算"还没有这份版本";其它失败必须让调用方停下来。
|
||||
command := fmt.Sprintf("if [ -f %s ]; then cat %s; else exit 44; fi", shellQuote(path), shellQuote(path))
|
||||
// RemoteArchive belongs to the executor filesystem, not the SSH host.
|
||||
read := fmt.Sprintf("if [ -f %s ]; then cat %s; else exit 44; fi", shellQuote(path), shellQuote(path))
|
||||
command := "docker exec " + shellQuote(r.config.ExecutorContainer) + " sh -c " + shellQuote(read)
|
||||
if err := r.run(ctx, nil, &out, command); err != nil {
|
||||
var exit *remoteExitError
|
||||
if errors.As(err, &exit) && exit.code == 44 {
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package libraryhandoff
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SSH runs its remote shell locally; Docker is an explicit process-boundary
|
||||
// fixture. The archive path deliberately does not exist on the SSH host.
|
||||
func TestSSHManifestReadUsesExecutorFilesystem(t *testing.T) {
|
||||
bin := t.TempDir()
|
||||
write := func(name, text string) {
|
||||
t.Helper()
|
||||
if err := os.WriteFile(filepath.Join(bin, name), []byte(text), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
write("ssh", "#!/bin/sh\nfor arg do last=$arg; done\nexec /bin/sh -c \"$last\"\n")
|
||||
write("docker", `#!/bin/sh
|
||||
[ "$1" = exec ] && [ "$2" = fixture-executor ] && [ "$3" = sh ] && [ "$4" = -c ] || exit 91
|
||||
case "$5" in *"/executor-only/archive/versions/"*) ;; *) exit 92;; esac
|
||||
printf '%s' "$5" > "$MANIFEST_TRACE"
|
||||
if [ "$MANIFEST_MODE" = missing ]; then exit 44; fi
|
||||
if [ "$MANIFEST_MODE" = denied ]; then exit 13; fi
|
||||
printf '%s' '{"container":true}'
|
||||
`)
|
||||
t.Setenv("PATH", bin+":"+os.Getenv("PATH"))
|
||||
trace := filepath.Join(bin, "trace")
|
||||
t.Setenv("MANIFEST_TRACE", trace)
|
||||
r := &sshRemote{config: Config{SSHTarget: "fixture", SSHConfig: "/unused", ExecutorContainer: "fixture-executor", RemoteArchive: "/executor-only/archive", OpTimeout: time.Second * 5}}
|
||||
id := strings.Repeat("a", 64)
|
||||
got, err := r.ReadVersionManifest(context.Background(), id)
|
||||
if err != nil || string(got) != `{"container":true}` {
|
||||
t.Fatalf("must read through executor, got=%s err=%v", got, err)
|
||||
}
|
||||
cmd, err := os.ReadFile(trace)
|
||||
if err != nil || !strings.Contains(string(cmd), id+".json") {
|
||||
t.Fatalf("missing exact snapshot read: %s %v", cmd, err)
|
||||
}
|
||||
t.Setenv("MANIFEST_MODE", "missing")
|
||||
if _, err := r.ReadVersionManifest(context.Background(), id); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("missing manifest: %v", err)
|
||||
}
|
||||
t.Setenv("MANIFEST_MODE", "denied")
|
||||
if _, err := r.ReadVersionManifest(context.Background(), id); err == nil || errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("permission failure must not mean absent: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user