@@ -68,7 +68,10 @@ func TestAgent_Stats_SSH(t *testing.T) {
68
68
session , err := sshClient .NewSession ()
69
69
require .NoError (t , err )
70
70
defer session .Close ()
71
- require .NoError (t , session .Run ("echo test" ))
71
+ stdin , err := session .StdinPipe ()
72
+ require .NoError (t , err )
73
+ err = session .Shell ()
74
+ require .NoError (t , err )
72
75
73
76
var s * agentsdk.Stats
74
77
require .Eventuallyf (t , func () bool {
@@ -78,6 +81,9 @@ func TestAgent_Stats_SSH(t *testing.T) {
78
81
}, testutil .WaitLong , testutil .IntervalFast ,
79
82
"never saw stats: %+v" , s ,
80
83
)
84
+ _ = stdin .Close ()
85
+ err = session .Wait ()
86
+ require .NoError (t , err )
81
87
}
82
88
83
89
func TestAgent_Stats_ReconnectingPTY (t * testing.T ) {
@@ -112,43 +118,69 @@ func TestAgent_Stats_ReconnectingPTY(t *testing.T) {
112
118
113
119
func TestAgent_Stats_Magic (t * testing.T ) {
114
120
t .Parallel ()
121
+ t .Run ("StripsEnvironmentVariable" , func (t * testing.T ) {
122
+ t .Parallel ()
123
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
124
+ defer cancel ()
125
+ //nolint:dogsled
126
+ conn , _ , _ , _ , _ := setupAgent (t , agentsdk.Metadata {}, 0 )
127
+ sshClient , err := conn .SSHClient (ctx )
128
+ require .NoError (t , err )
129
+ defer sshClient .Close ()
130
+ session , err := sshClient .NewSession ()
131
+ require .NoError (t , err )
132
+ session .Setenv (agent .MagicSSHSessionTypeEnvironmentVariable , agent .MagicSSHSessionTypeVSCode )
133
+ defer session .Close ()
115
134
116
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
117
- defer cancel ()
118
-
119
- //nolint:dogsled
120
- conn , _ , stats , _ , _ := setupAgent (t , agentsdk.Metadata {}, 0 )
121
- sshClient , err := conn .SSHClient (ctx )
122
- require .NoError (t , err )
123
- defer sshClient .Close ()
124
- session , err := sshClient .NewSession ()
125
- require .NoError (t , err )
126
- session .Setenv (agent .MagicSSHSessionTypeEnvironmentVariable , agent .MagicSSHSessionTypeVSCode )
127
- defer session .Close ()
128
-
129
- command := "sh -c 'echo $" + agent .MagicSSHSessionTypeEnvironmentVariable + "'"
130
- expected := ""
131
- if runtime .GOOS == "windows" {
132
- expected = "%" + agent .MagicSSHSessionTypeEnvironmentVariable + "%"
133
- command = "cmd.exe /c echo " + expected
134
- }
135
- output , err := session .Output (command )
136
- require .NoError (t , err )
137
- require .Equal (t , expected , strings .TrimSpace (string (output )))
138
- var s * agentsdk.Stats
139
- require .Eventuallyf (t , func () bool {
140
- var ok bool
141
- s , ok = <- stats
142
- return ok && s .ConnectionCount > 0 && s .RxBytes > 0 && s .TxBytes > 0 &&
143
- // Ensure that the connection didn't count as a "normal" SSH session.
144
- // This was a special one, so it should be labeled specially in the stats!
145
- s .SessionCountVSCode == 1 &&
146
- // Ensure that connection latency is being counted!
147
- // If it isn't, it's set to -1.
148
- s .ConnectionMedianLatencyMS >= 0
149
- }, testutil .WaitLong , testutil .IntervalFast ,
150
- "never saw stats: %+v" , s ,
151
- )
135
+ command := "sh -c 'echo $" + agent .MagicSSHSessionTypeEnvironmentVariable + "'"
136
+ expected := ""
137
+ if runtime .GOOS == "windows" {
138
+ expected = "%" + agent .MagicSSHSessionTypeEnvironmentVariable + "%"
139
+ command = "cmd.exe /c echo " + expected
140
+ }
141
+ output , err := session .Output (command )
142
+ require .NoError (t , err )
143
+ require .Equal (t , expected , strings .TrimSpace (string (output )))
144
+ })
145
+ t .Run ("Tracks" , func (t * testing.T ) {
146
+ t .Parallel ()
147
+ if runtime .GOOS == "window" {
148
+ t .Skip ("Sleeping for infinity doesn't work on Windows" )
149
+ }
150
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
151
+ defer cancel ()
152
+ //nolint:dogsled
153
+ conn , _ , stats , _ , _ := setupAgent (t , agentsdk.Metadata {}, 0 )
154
+ sshClient , err := conn .SSHClient (ctx )
155
+ require .NoError (t , err )
156
+ defer sshClient .Close ()
157
+ session , err := sshClient .NewSession ()
158
+ require .NoError (t , err )
159
+ session .Setenv (agent .MagicSSHSessionTypeEnvironmentVariable , agent .MagicSSHSessionTypeVSCode )
160
+ defer session .Close ()
161
+ stdin , err := session .StdinPipe ()
162
+ require .NoError (t , err )
163
+ err = session .Shell ()
164
+ require .NoError (t , err )
165
+ var s * agentsdk.Stats
166
+ require .Eventuallyf (t , func () bool {
167
+ var ok bool
168
+ s , ok = <- stats
169
+ return ok && s .ConnectionCount > 0 && s .RxBytes > 0 && s .TxBytes > 0 &&
170
+ // Ensure that the connection didn't count as a "normal" SSH session.
171
+ // This was a special one, so it should be labeled specially in the stats!
172
+ s .SessionCountVSCode == 1 &&
173
+ // Ensure that connection latency is being counted!
174
+ // If it isn't, it's set to -1.
175
+ s .ConnectionMedianLatencyMS >= 0
176
+ }, testutil .WaitLong , testutil .IntervalFast ,
177
+ "never saw stats: %+v" , s ,
178
+ )
179
+ // The shell will automatically exit if there is no stdin!
180
+ _ = stdin .Close ()
181
+ err = session .Wait ()
182
+ require .NoError (t , err )
183
+ })
152
184
}
153
185
154
186
func TestAgent_SessionExec (t * testing.T ) {
0 commit comments