227c036aa5
A new terminal runs su -l for the server's own account in its PTY, so the operator types the account password before getting a shell. Opening is refused when su is missing, the server runs as root, or su opens a shell without asking. The Pipelines-owned tmux backing, its reattach and its persisted session names are removed: terminals end with the server and are listed as ended. A starting server ends leftover pipelines-term_* sessions on tmux -L pipelines only.
55 lines
1.6 KiB
Go
55 lines
1.6 KiB
Go
//go:build darwin || linux
|
|
|
|
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Startup cleanup ends only the terminal sessions an earlier Pipelines
|
|
// server left on its socket; another session there, and every other tmux
|
|
// server, is left alone. The test uses a socket of its own.
|
|
func TestEndLegacyTerminalTmux(t *testing.T) {
|
|
program := findLegacyTmux()
|
|
if program == "" {
|
|
t.Skip("tmux is not installed")
|
|
}
|
|
tmpdir, err := os.MkdirTemp("/tmp", "pl-tmux-")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("TMUX_TMPDIR", tmpdir)
|
|
socket := fmt.Sprintf("pipelines-test-%d-%d", os.Getpid(), time.Now().UnixNano())
|
|
tmux := func(args ...string) *exec.Cmd {
|
|
return exec.Command(program, append([]string{"-L", socket, "-f", os.DevNull}, args...)...)
|
|
}
|
|
t.Cleanup(func() {
|
|
_ = tmux("kill-server").Run()
|
|
_ = os.RemoveAll(tmpdir)
|
|
})
|
|
if ended := endLegacyTerminalTmux(socket); ended != 0 {
|
|
t.Fatalf("ended %d with no tmux server", ended)
|
|
}
|
|
for _, name := range []string{"pipelines-term_a1", "pipelines-term_b2", "mine"} {
|
|
if out, err := tmux("new-session", "-d", "-s", name, "sleep", "60").CombinedOutput(); err != nil {
|
|
t.Fatalf("new-session %s: %v %s", name, err, out)
|
|
}
|
|
}
|
|
if ended := endLegacyTerminalTmux(socket); ended != 2 {
|
|
t.Fatalf("ended %d sessions", ended)
|
|
}
|
|
out, err := tmux("list-sessions", "-F", "#{session_name}").Output()
|
|
if err != nil || strings.TrimSpace(string(out)) != "mine" {
|
|
t.Fatalf("sessions left = %q, %v", out, err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(tmpdir, fmt.Sprintf("tmux-%d", os.Getuid()), socket)); err != nil {
|
|
t.Fatalf("test socket not under TMUX_TMPDIR: %v", err)
|
|
}
|
|
}
|