Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita75ed6c

Browse files
committed
Fix JetBrains tracking test
The test name only shows up in the process name if you are running thattest directly so we have to spawn a separate process instead.
1 parent254a5b6 commita75ed6c

File tree

2 files changed

+81
-20
lines changed

2 files changed

+81
-20
lines changed

‎agent/agent_test.go

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package agent_test
22

33
import (
4+
"bufio"
45
"bytes"
56
"context"
67
"encoding/json"
@@ -152,7 +153,7 @@ func TestAgent_Stats_Magic(t *testing.T) {
152153
require.NoError(t,err)
153154
require.Equal(t,expected,strings.TrimSpace(string(output)))
154155
})
155-
t.Run("Tracks",func(t*testing.T) {
156+
t.Run("TracksVSCode",func(t*testing.T) {
156157
t.Parallel()
157158
ifruntime.GOOS=="window" {
158159
t.Skip("Sleeping for infinity doesn't work on Windows")
@@ -192,36 +193,45 @@ func TestAgent_Stats_Magic(t *testing.T) {
192193
require.NoError(t,err)
193194
})
194195

195-
// This test name must contain the string checked for by the agent, since it
196-
// looks for this string in the process name.
197-
//
198-
// This test sets up a port forward that emulates what Jetbrains IDE's do when
199-
// using gateway. The remote server side of the port forward is spun up using
200-
// the gotest process, which includes the test name.
201-
// So this unit test emulates a PID in the workspace with a similar
202-
// name to the jetbrains IDE. That makes the agent this this SSH port
203-
// forward is a "jetbrains" session.
204-
t.Run("TracksIdea.vendor.name=JetBrains",func(t*testing.T) {
196+
t.Run("TracksJetBrains",func(t*testing.T) {
205197
t.Parallel()
206198
ifruntime.GOOS!="linux" {
207199
t.Skip("JetBrains tracking is only supported on Linux")
208200
}
201+
209202
ctx:=testutil.Context(t,testutil.WaitLong)
210203

211-
rl,err:=net.Listen("tcp","127.0.0.1:0")
204+
// JetBrains tracking works by looking at the process name listening on the
205+
// forwarded port. If the process's command line includes the magic string
206+
// we are looking for, then we assume it is a JetBrains editor. So when we
207+
// connect to the port we must ensure the process includes that magic string
208+
// to fool the agent into thinking this is JetBrains. To do this we need to
209+
// spawn an external process (in this case a simple echo server) so we can
210+
// control the process name. The -D here is just to mimic how Java options
211+
// are set but is not necessary as the agent looks only for the magic
212+
// string itself anywhere in the command.
213+
_,b,_,ok:=runtime.Caller(0)
214+
require.True(t,ok)
215+
dir:=filepath.Join(filepath.Dir(b),"../scripts/echoserver/main.go")
216+
echoServerCmd:=exec.Command("go","run",dir,
217+
"-D",agentssh.MagicProcessCmdlineJetBrains)
218+
stdout,err:=echoServerCmd.StdoutPipe()
212219
require.NoError(t,err)
213-
deferrl.Close()
214-
tcpAddr,valid:=rl.Addr().(*net.TCPAddr)
215-
require.True(t,valid)
216-
remotePort:=tcpAddr.Port
217-
goechoOnce(t,rl)
220+
err=echoServerCmd.Start()
221+
require.NoError(t,err)
222+
deferechoServerCmd.Process.Kill()
223+
224+
// The echo server prints its port as the first line.
225+
sc:=bufio.NewScanner(stdout)
226+
sc.Scan()
227+
remotePort:=sc.Text()
218228

219229
//nolint:dogsled
220230
conn,_,stats,_,_:=setupAgent(t, agentsdk.Manifest{},0)
221231
sshClient,err:=conn.SSHClient(ctx)
222232
require.NoError(t,err)
223233

224-
tunneledConn,err:=sshClient.Dial("tcp",fmt.Sprintf("127.0.0.1:%d",remotePort))
234+
tunneledConn,err:=sshClient.Dial("tcp",fmt.Sprintf("127.0.0.1:%s",remotePort))
225235
require.NoError(t,err)
226236
t.Cleanup(func() {
227237
// always close on failure of test
@@ -239,9 +249,10 @@ func TestAgent_Stats_Magic(t *testing.T) {
239249
"never saw stats with conn open: %+v",s,
240250
)
241251

242-
//Manually closingthe connection
252+
//Killtheserver andconnection after checking for the echo.
243253
requireEcho(t,tunneledConn)
244-
_=rl.Close()
254+
_=echoServerCmd.Process.Kill()
255+
_=tunneledConn.Close()
245256

246257
require.Eventuallyf(t,func()bool {
247258
varokbool

‎scripts/echoserver/main.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
// A simple echo server. It listens on a random port, prints that port, then
4+
// echos back anything sent to it.
5+
6+
import (
7+
"errors"
8+
"fmt"
9+
"io"
10+
"log"
11+
"net"
12+
)
13+
14+
funcmain() {
15+
l,err:=net.Listen("tcp","127.0.0.1:0")
16+
iferr!=nil {
17+
log.Fatalf("listen error: err=%s",err)
18+
}
19+
20+
deferl.Close()
21+
tcpAddr,valid:=l.Addr().(*net.TCPAddr)
22+
if!valid {
23+
log.Fatal("address is not valid")
24+
}
25+
26+
remotePort:=tcpAddr.Port
27+
_,err=fmt.Println(remotePort)
28+
iferr!=nil {
29+
log.Fatalf("print error: err=%s",err)
30+
}
31+
32+
for {
33+
conn,err:=l.Accept()
34+
iferr!=nil {
35+
log.Fatalf("accept error, err=%s",err)
36+
return
37+
}
38+
39+
gofunc() {
40+
deferconn.Close()
41+
_,err:=io.Copy(conn,conn)
42+
43+
iferrors.Is(err,io.EOF) {
44+
return
45+
}elseiferr!=nil {
46+
log.Fatalf("copy error, err=%s",err)
47+
}
48+
}()
49+
}
50+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp