Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 61613a1

Browse files
committed
Add motd and hushlogin test
1 parent 909bec7 commit 61613a1

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

agent/agent_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package agent_test
22

33
import (
44
"bufio"
5+
"bytes"
56
"context"
67
"encoding/json"
78
"fmt"
@@ -193,6 +194,86 @@ func TestAgent(t *testing.T) {
193194
}
194195
})
195196

197+
t.Run("Session TTY MOTD", func(t *testing.T) {
198+
t.Parallel()
199+
200+
if runtime.GOOS == "windows" {
201+
// This might be our implementation, or ConPTY itself.
202+
// It's difficult to find extensive tests for it, so
203+
// it seems like it could be either.
204+
t.Skip("ConPTY appears to be inconsistent on Windows.")
205+
}
206+
207+
wantMOTD := "Welcome to your Coder workspace!"
208+
209+
name := filepath.Join(t.TempDir(), "motd")
210+
err := os.WriteFile(name, []byte(wantMOTD), 0o600)
211+
require.NoError(t, err, "write motd file")
212+
213+
session := setupSSHSession(t, codersdk.WorkspaceAgentMetadata{
214+
MOTDFile: name,
215+
})
216+
err = session.RequestPty("xterm", 128, 128, ssh.TerminalModes{})
217+
require.NoError(t, err)
218+
219+
ptty := ptytest.New(t)
220+
var stdout bytes.Buffer
221+
session.Stdout = &stdout
222+
session.Stderr = ptty.Output()
223+
session.Stdin = ptty.Input()
224+
err = session.Start("")
225+
require.NoError(t, err)
226+
227+
ptty.WriteLine("exit")
228+
err = session.Wait()
229+
require.NoError(t, err)
230+
231+
require.Contains(t, stdout.String(), wantMOTD, "should show motd")
232+
})
233+
234+
//nolint:paralleltest // This test sets an environment variable.
235+
t.Run("Session TTY Hushlogin", func(t *testing.T) {
236+
if runtime.GOOS == "windows" {
237+
// This might be our implementation, or ConPTY itself.
238+
// It's difficult to find extensive tests for it, so
239+
// it seems like it could be either.
240+
t.Skip("ConPTY appears to be inconsistent on Windows.")
241+
}
242+
243+
wantNotMOTD := "Welcome to your Coder workspace!"
244+
245+
tmpdir := t.TempDir()
246+
name := filepath.Join(tmpdir, "motd")
247+
err := os.WriteFile(name, []byte(wantNotMOTD), 0o600)
248+
require.NoError(t, err, "write motd file")
249+
f, err := os.Create(filepath.Join(tmpdir, ".hushlogin"))
250+
require.NoError(t, err, "create .hushlogin file")
251+
err = f.Close()
252+
require.NoError(t, err, "close .hushlogin file")
253+
254+
t.Setenv("HOME", tmpdir)
255+
256+
session := setupSSHSession(t, codersdk.WorkspaceAgentMetadata{
257+
MOTDFile: name,
258+
})
259+
err = session.RequestPty("xterm", 128, 128, ssh.TerminalModes{})
260+
require.NoError(t, err)
261+
262+
ptty := ptytest.New(t)
263+
var stdout bytes.Buffer
264+
session.Stdout = &stdout
265+
session.Stderr = ptty.Output()
266+
session.Stdin = ptty.Input()
267+
err = session.Start("")
268+
require.NoError(t, err)
269+
270+
ptty.WriteLine("exit")
271+
err = session.Wait()
272+
require.NoError(t, err)
273+
274+
require.NotContains(t, stdout.String(), wantNotMOTD, "should not show motd")
275+
})
276+
196277
t.Run("LocalForwarding", func(t *testing.T) {
197278
t.Parallel()
198279
random, err := net.Listen("tcp", "127.0.0.1:0")

0 commit comments

Comments
 (0)