@@ -15,6 +15,7 @@ import (
1515 "cdr.dev/slog/sloggers/slogtest/assert"
1616 "cdr.dev/wsep/internal/proto"
1717 "github.com/google/go-cmp/cmp"
18+ "golang.org/x/xerrors"
1819 "nhooyr.io/websocket"
1920)
2021
@@ -84,6 +85,46 @@ func TestRemoteExec(t *testing.T) {
8485 testExecer (ctx , t , execer )
8586}
8687
88+ func TestRemoveClose (t * testing.T ) {
89+ t .Parallel ()
90+
91+ ctx , cancel := context .WithTimeout (context .Background (), 15 * time .Second )
92+ defer cancel ()
93+
94+ ws , server := mockConn (ctx , t , nil )
95+ defer server .Close ()
96+
97+ execer := RemoteExecer (ws )
98+ cmd := Command {
99+ Command : "/bin/bash" ,
100+ TTY : true ,
101+ Stdin : true ,
102+ Env : []string {"TERM=linux" },
103+ }
104+
105+ proc , err := execer .Start (ctx , cmd )
106+ assert .Success (t , "execer Start" , err )
107+
108+ i := proc .Stdin ()
109+ o := proc .Stdout ()
110+ buf := make ([]byte , 2048 )
111+ _ , err = i .Write ([]byte ("echo foo" ))
112+ assert .Success (t , "echo" , err )
113+ bldr := strings.Builder {}
114+ for ! strings .Contains (bldr .String (), "foo" ) {
115+ n , err := o .Read (buf )
116+ if xerrors .Is (err , io .EOF ) {
117+ break
118+ }
119+ assert .Success (t , "read" , err )
120+ _ , err = bldr .Write (buf [:n ])
121+ assert .Success (t , "write to builder" , err )
122+ }
123+ err = proc .Close ()
124+ assert .Success (t , "close proc" , err )
125+ // note that proc.Close() also closes the websocket.
126+ }
127+
87128func TestRemoteExecFail (t * testing.T ) {
88129 t .Parallel ()
89130 ctx , cancel := context .WithTimeout (context .Background (), 15 * time .Second )
0 commit comments