Repository navigation
Expand file tree
/
Copy pathe2e_command_test.go
More file actions
83 lines (69 loc) · 2.75 KB
/
Copy pathe2e_command_test.go
File metadata and controls
83 lines (69 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package socket
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strings"
"testing"
"time"
)
func TestCommandExecutionP2P(t *testing.T) {
cmdBuild := exec.Command("go", "build", "-o", "test-command-binary", "./cmd/syncthing-socket")
out, err := cmdBuild.CombinedOutput()
if err != nil {
t.Fatalf("Failed to build binary: %v\nOutput: %s", err, string(out))
t.Fatalf("Failed to build binary: %v", err)
}
passphrase := fmt.Sprintf("test-command-passphrase-%d", time.Now().UnixNano())
// Start server in command mode
// We'll execute a command that reads stdin and echoes it back, plus writes to stderr
cmdServer := exec.Command("./test-command-binary", "server", "--seed", passphrase, "--command", "echo 'starting command'; cat -; echo 'error log' >&2", "--direct-port", "22003", "--discovery", "", "--relay", "", "--log-level", "debug", "--log-format", "text")
cmdServer.Stdout = os.Stdout
cmdServer.Stderr = os.Stderr
if err := cmdServer.Start(); err != nil {
t.Fatalf("Failed to start server: %v", err)
}
defer cmdServer.Process.Kill()
// Wait briefly for local socket to bind
time.Sleep(1 * time.Second)
// Start client, explicitly passing the direct TCP URI
cmdClient := exec.Command("./test-command-binary", "client", "--seed", passphrase, "--relay", "tcp://127.0.0.1:22003", "--discovery", "", "--log-level", "debug", "--log-format", "text")
// Inject mock data to client's stdin, leaving it open to prevent premature client exit
stdinRead, stdinWrite := io.Pipe()
cmdClient.Stdin = stdinRead
go func() {
// Write immediately; it will be buffered and sent once the P2P connection establishes
stdinWrite.Write([]byte("hello from client\n"))
}()
// Capture client output
stdout, _ := cmdClient.StdoutPipe()
var outputBuffer bytes.Buffer
go io.Copy(&outputBuffer, stdout)
cmdClient.Stderr = os.Stderr
if err := cmdClient.Start(); err != nil {
t.Fatalf("Failed to start client: %v", err)
}
defer cmdClient.Process.Kill()
// Deterministic polling: wait until we see the expected output, up to 30 seconds
deadline := time.Now().Add(30 * time.Second)
success := false
for time.Now().Before(deadline) {
output := outputBuffer.String()
if strings.Contains(output, "starting command") && strings.Contains(output, "hello from client") {
success = true
break
}
time.Sleep(100 * time.Millisecond)
}
if !success {
t.Fatalf("Test timed out waiting for expected output. Got: %s", outputBuffer.String())
}
output := outputBuffer.String()
// Note: stderr is not piped to the client by default unless 2>&1 is used, so it shouldn't be in the output.
if strings.Contains(output, "error log") {
t.Fatalf("Did not expect stderr 'error log' to be piped to client stdout")
}
t.Logf("Successfully executed remote command over P2P tunnel!")
}