224 lines
7.1 KiB
Go
224 lines
7.1 KiB
Go
//go:build darwin || linux
|
|
|
|
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// testTmux is a tmux server of this test alone, killed afterwards.
|
|
func testTmux(t *testing.T) *terminalTmux {
|
|
t.Helper()
|
|
tmux := findTerminalTmux(fmt.Sprintf("pipelines-test-%d-%d", os.Getpid(), time.Now().UnixNano()))
|
|
if tmux == nil {
|
|
t.Skip("tmux is not installed")
|
|
}
|
|
t.Cleanup(func() {
|
|
_ = tmux.command("kill-server").Run()
|
|
dir := os.Getenv("TMUX_TMPDIR")
|
|
if dir == "" {
|
|
dir = "/tmp"
|
|
}
|
|
_ = os.Remove(filepath.Join(dir, fmt.Sprintf("tmux-%d", os.Getuid()), tmux.socket))
|
|
})
|
|
return tmux
|
|
}
|
|
|
|
// newTestTerminals is one server process's terminal manager over store.
|
|
func newTestTerminals(t *testing.T, store *pipeline.Store, tmux *terminalTmux) (*terminalManager, context.CancelFunc) {
|
|
t.Helper()
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
m := newTerminalManager(ctx, store, tmux)
|
|
m.shell = func() string { return "/bin/sh" }
|
|
m.recover(ctx)
|
|
t.Cleanup(func() { cancel(); m.Wait() })
|
|
return m, cancel
|
|
}
|
|
|
|
// waitOutput polls a terminal's retained output until it contains want.
|
|
func waitTerminalOutput(t *testing.T, m *terminalManager, id, want string) protocol.TerminalRecord {
|
|
t.Helper()
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for {
|
|
record, err := m.Output(context.Background(), id, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(record.Output, want) {
|
|
return record
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("output of %s never contained %q: %q", id, want, record.Output)
|
|
}
|
|
time.Sleep(50 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
// A tmux-backed shell outlives its server process: the next one lists it as
|
|
// running, reattaches, redraws its screen and takes input again. Closing it
|
|
// ends the tmux session.
|
|
func TestTerminalTmuxSurvivesRestart(t *testing.T) {
|
|
terminalHome(t)
|
|
tmux := testTmux(t)
|
|
store := newOperatorStore(t)
|
|
first, stop := newTestTerminals(t, store, tmux)
|
|
term, err := first.Open(context.Background(), "", "~", 90, 20)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if term.Tmux != terminalTmuxName(term.ID) {
|
|
t.Fatalf("tmux name = %q", term.Tmux)
|
|
}
|
|
if err := first.Input(term.ID, "export KEEP=still-here; echo marker-$((6*7))\r"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
waitTerminalOutput(t, first, term.ID, "marker-42")
|
|
stop()
|
|
first.Wait()
|
|
if !tmux.alive(term.Tmux) {
|
|
t.Fatal("the tmux session ended with the server")
|
|
}
|
|
second, _ := newTestTerminals(t, store, tmux)
|
|
list, err := second.List(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(list) != 1 || list[0].ID != term.ID || list[0].Status != protocol.TerminalRunning {
|
|
t.Fatalf("list after restart = %+v", list)
|
|
}
|
|
// The reattached client redraws the screen, earlier output included,
|
|
// and the shell keeps its state.
|
|
waitTerminalOutput(t, second, term.ID, "marker-42")
|
|
if err := second.Input(term.ID, "echo $KEEP\r"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
waitTerminalOutput(t, second, term.ID, "still-here")
|
|
closed, err := second.Close(context.Background(), term.ID)
|
|
if err != nil || closed.Status != protocol.TerminalClosed {
|
|
t.Fatalf("close = %+v, %v", closed, err)
|
|
}
|
|
if tmux.alive(term.Tmux) {
|
|
t.Fatal("the tmux session outlived close")
|
|
}
|
|
if list, _ := second.List(context.Background()); len(list) != 0 {
|
|
t.Fatalf("list after close = %+v", list)
|
|
}
|
|
}
|
|
|
|
// A tmux client killed while its session lives is replaced.
|
|
func TestTerminalTmuxClientReplaced(t *testing.T) {
|
|
terminalHome(t)
|
|
tmux := testTmux(t)
|
|
m, _ := newTestTerminals(t, newOperatorStore(t), tmux)
|
|
term, err := m.Open(context.Background(), "", "", 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
waitTerminalOutput(t, m, term.ID, "$")
|
|
if err := tmux.command("detach-client", "-s", "="+term.Tmux).Run(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
time.Sleep(300 * time.Millisecond)
|
|
if err := m.Input(term.ID, "echo back-$((1+1))\r"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
waitTerminalOutput(t, m, term.ID, "back-2")
|
|
if record, _ := m.Output(context.Background(), term.ID, 0); record.Status != protocol.TerminalRunning {
|
|
t.Fatalf("record = %+v", record)
|
|
}
|
|
}
|
|
|
|
// Without tmux a shell ends with its server: the stop is recorded as
|
|
// ended, and a record a crashed server left running is marked ended too.
|
|
func TestTerminalWithoutTmuxEndsWithServer(t *testing.T) {
|
|
terminalHome(t)
|
|
store := newOperatorStore(t)
|
|
first, stop := newTestTerminals(t, store, nil)
|
|
term, err := first.Open(context.Background(), "", "", 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
stop()
|
|
first.Wait()
|
|
crashed := protocol.TerminalRecord{ID: "term_crashed", Cwd: "/", CreatedAt: 1, Status: protocol.TerminalRunning, Cols: 80, Rows: 24}
|
|
gone := protocol.TerminalRecord{ID: "term_gone", Cwd: "/", CreatedAt: 2, Tmux: "pipelines-term_gone", Status: protocol.TerminalRunning}
|
|
for _, record := range []protocol.TerminalRecord{crashed, gone} {
|
|
if err := store.SaveTerminal(context.Background(), record, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
second, _ := newTestTerminals(t, store, testTmux(t))
|
|
list, err := second.List(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reasons := map[string]string{}
|
|
for _, record := range list {
|
|
if record.Status != protocol.TerminalExited || record.EndedAt == 0 {
|
|
t.Fatalf("record = %+v", record)
|
|
}
|
|
reasons[record.ID] = record.Reason
|
|
}
|
|
if reasons[term.ID] != terminalStopReason || reasons["term_crashed"] != terminalNoTmuxReason || reasons["term_gone"] != terminalGoneReason {
|
|
t.Fatalf("reasons = %+v", reasons)
|
|
}
|
|
if _, err := second.Close(context.Background(), "term_gone"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if list, _ := second.List(context.Background()); len(list) != 2 {
|
|
t.Fatalf("list after closing an ended terminal = %+v", list)
|
|
}
|
|
}
|
|
|
|
// The window title the shell sets is followed across reads and recorded.
|
|
func TestTerminalTitles(t *testing.T) {
|
|
var titles terminalTitles
|
|
if _, ok := titles.feed([]byte("plain \x1b[1mtext")); ok {
|
|
t.Fatal("title from plain text")
|
|
}
|
|
if _, ok := titles.feed([]byte("\x1b]0;~/src")); ok {
|
|
t.Fatal("title before its end")
|
|
}
|
|
if title, ok := titles.feed([]byte("/app\x07more")); !ok || title != "~/src/app" {
|
|
t.Fatalf("title = %q %v", title, ok)
|
|
}
|
|
if title, ok := titles.feed([]byte("\x1b]2;vim\x01 x\x1b\\")); !ok || title != "vim x" {
|
|
t.Fatalf("ST title = %q %v", title, ok)
|
|
}
|
|
if _, ok := titles.feed([]byte("\x1b]8;;https://x\x07")); ok {
|
|
t.Fatal("a hyperlink is not a title")
|
|
}
|
|
if title, _ := titles.feed([]byte("\x1b]0;" + strings.Repeat("é", protocol.TerminalTitleLimit) + "\x07")); len(title) > protocol.TerminalTitleLimit {
|
|
t.Fatalf("title of %d bytes", len(title))
|
|
}
|
|
terminalHome(t)
|
|
m, _ := newTestTerminals(t, newOperatorStore(t), nil)
|
|
term, err := m.Open(context.Background(), "", "", 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := m.Input(term.ID, "printf '\\033]0;build-%s\\007' ok\r"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
for {
|
|
list, _ := m.List(context.Background())
|
|
if len(list) == 1 && list[0].Title == "build-ok" {
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("list = %+v", list)
|
|
}
|
|
time.Sleep(50 * time.Millisecond)
|
|
}
|
|
}
|