@@ -12,12 +12,15 @@ import (
12
12
"strconv"
13
13
"strings"
14
14
"testing"
15
+ "time"
15
16
16
17
"github.com/pion/webrtc/v3"
17
18
"github.com/pkg/sftp"
18
19
"github.com/stretchr/testify/require"
19
20
"go.uber.org/goleak"
20
21
"golang.org/x/crypto/ssh"
22
+ "golang.org/x/text/encoding/unicode"
23
+ "golang.org/x/text/transform"
21
24
22
25
"cdr.dev/slog"
23
26
"cdr.dev/slog/sloggers/slogtest"
@@ -37,7 +40,7 @@ func TestAgent(t *testing.T) {
37
40
t .Parallel ()
38
41
t .Run ("SessionExec" , func (t * testing.T ) {
39
42
t .Parallel ()
40
- session := setupSSHSession (t )
43
+ session := setupSSHSession (t , nil )
41
44
42
45
command := "echo test"
43
46
if runtime .GOOS == "windows" {
@@ -50,7 +53,7 @@ func TestAgent(t *testing.T) {
50
53
51
54
t .Run ("GitSSH" , func (t * testing.T ) {
52
55
t .Parallel ()
53
- session := setupSSHSession (t )
56
+ session := setupSSHSession (t , nil )
54
57
command := "sh -c 'echo $GIT_SSH_COMMAND'"
55
58
if runtime .GOOS == "windows" {
56
59
command = "cmd.exe /c echo %GIT_SSH_COMMAND%"
@@ -68,7 +71,7 @@ func TestAgent(t *testing.T) {
68
71
// it seems like it could be either.
69
72
t .Skip ("ConPTY appears to be inconsistent on Windows." )
70
73
}
71
- session := setupSSHSession (t )
74
+ session := setupSSHSession (t , nil )
72
75
command := "bash"
73
76
if runtime .GOOS == "windows" {
74
77
command = "cmd.exe"
@@ -128,7 +131,7 @@ func TestAgent(t *testing.T) {
128
131
129
132
t .Run ("SFTP" , func (t * testing.T ) {
130
133
t .Parallel ()
131
- sshClient , err := setupAgent (t ).SSHClient ()
134
+ sshClient , err := setupAgent (t , nil ).SSHClient ()
132
135
require .NoError (t , err )
133
136
client , err := sftp .NewClient (sshClient )
134
137
require .NoError (t , err )
@@ -140,10 +143,52 @@ func TestAgent(t *testing.T) {
140
143
_ , err = os .Stat (tempFile )
141
144
require .NoError (t , err )
142
145
})
146
+
147
+ t .Run ("EnvironmentVariables" , func (t * testing.T ) {
148
+ t .Parallel ()
149
+ key := "EXAMPLE"
150
+ value := "value"
151
+ session := setupSSHSession (t , & agent.Options {
152
+ EnvironmentVariables : map [string ]string {
153
+ key : value ,
154
+ },
155
+ })
156
+ command := "sh -c 'echo $" + key + "'"
157
+ if runtime .GOOS == "windows" {
158
+ command = "cmd.exe /c echo %" + key + "%"
159
+ }
160
+ output , err := session .Output (command )
161
+ require .NoError (t , err )
162
+ require .Equal (t , value , strings .TrimSpace (string (output )))
163
+ })
164
+
165
+ t .Run ("StartupScript" , func (t * testing.T ) {
166
+ t .Parallel ()
167
+ tempPath := filepath .Join (os .TempDir (), "content.txt" )
168
+ content := "somethingnice"
169
+ setupAgent (t , & agent.Options {
170
+ StartupScript : "echo " + content + " > " + tempPath ,
171
+ })
172
+ var gotContent string
173
+ require .Eventually (t , func () bool {
174
+ content , err := os .ReadFile (tempPath )
175
+ if err != nil {
176
+ return false
177
+ }
178
+ if runtime .GOOS == "windows" {
179
+ // Windows uses UTF16! 🪟🪟🪟
180
+ content , _ , err = transform .Bytes (unicode .UTF16 (unicode .LittleEndian , unicode .UseBOM ).NewDecoder (), content )
181
+ require .NoError (t , err )
182
+ }
183
+ gotContent = string (content )
184
+ return true
185
+ }, 15 * time .Second , 100 * time .Millisecond )
186
+ require .Equal (t , content , strings .TrimSpace (gotContent ))
187
+ })
143
188
}
144
189
145
190
func setupSSHCommand (t * testing.T , beforeArgs []string , afterArgs []string ) * exec.Cmd {
146
- agentConn := setupAgent (t )
191
+ agentConn := setupAgent (t , nil )
147
192
listener , err := net .Listen ("tcp" , "127.0.0.1:0" )
148
193
require .NoError (t , err )
149
194
go func () {
@@ -171,18 +216,22 @@ func setupSSHCommand(t *testing.T, beforeArgs []string, afterArgs []string) *exe
171
216
return exec .Command ("ssh" , args ... )
172
217
}
173
218
174
- func setupSSHSession (t * testing.T ) * ssh.Session {
175
- sshClient , err := setupAgent (t ).SSHClient ()
219
+ func setupSSHSession (t * testing.T , options * agent. Options ) * ssh.Session {
220
+ sshClient , err := setupAgent (t , options ).SSHClient ()
176
221
require .NoError (t , err )
177
222
session , err := sshClient .NewSession ()
178
223
require .NoError (t , err )
179
224
return session
180
225
}
181
226
182
- func setupAgent (t * testing.T ) * agent.Conn {
227
+ func setupAgent (t * testing.T , options * agent.Options ) * agent.Conn {
228
+ if options == nil {
229
+ options = & agent.Options {}
230
+ }
183
231
client , server := provisionersdk .TransportPipe ()
184
- closer := agent .New (func (ctx context.Context , logger slog.Logger ) (* peerbroker.Listener , error ) {
185
- return peerbroker .Listen (server , nil )
232
+ closer := agent .New (func (ctx context.Context , logger slog.Logger ) (* agent.Options , * peerbroker.Listener , error ) {
233
+ listener , err := peerbroker .Listen (server , nil )
234
+ return options , listener , err
186
235
}, slogtest .Make (t , nil ).Leveled (slog .LevelDebug ))
187
236
t .Cleanup (func () {
188
237
_ = client .Close ()
0 commit comments