Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212package ui
import ( "context" "strconv" "strings" "testing" "time"
"100gex/internal/hosts" "100gex/internal/rex" "100gex/internal/rex/rextest" "100gex/internal/rexsession" "100gex/internal/sshtunnel" "100gex/internal/sshtunnel/sshtest" "100gex/internal/tailnet")
// TestSwitchServer switches the window to another server, which shows its// own session. The first stays connected, and the sidebar lists both// servers' sessions; choosing the first's switches back to it.func TestSwitchServer(t *testing.T) { alpha := rextest.Start(t) bravo := rextest.Start(t) sidA, bidA := rextest.NewSession(t, alpha.Dial(t, "setup"), rextest.RawCat...) sidB, bidB := rextest.NewSession(t, bravo.Dial(t, "setup"), rextest.RawCat...)
h := startUI(t, alpha, alpha.Dial(t, "100gex-test"), sidA) h.until("the first server", func(f rexsession.Frame) bool { return f.Ready() && len(f.Visible) == 1 }) first := h.u.conn if got := sidebarText(h, sidA, sidB); got != "host,A" { t.Errorf("with one server, the sidebar has %s, want host,A", got) }
h.u.Connect(bravo.Endpoint.String()) h.until("the second server's session", func(f rexsession.Frame) bool { return f.View.SessionID == sidB && f.Ready() && len(f.Visible) == 1 && f.Visible[0] == bidB }) if state, _ := h.u.connectState(); state != "" { t.Errorf("still %s after switching", state) } if first.Err() != nil { t.Fatalf("the first connection ended: %v", first.Err()) }
// The sidebar lists each server, with its sessions under it. if got := sidebarText(h, sidA, sidB); got != "host,A,host,B" { t.Fatalf("sidebar = %s, want host,A,host,B", got) } back := h.u.find(alpha.Endpoint.String())
// A session on the first server switches back to it, on the same // connection. h.u.switchTo(back, sidA) h.until("the first server again", func(f rexsession.Frame) bool { return f.View.SessionID == sidA && f.Ready() && len(f.Visible) == 1 && f.Visible[0] == bidA }) if h.u.conn != first { t.Error("switching back made a new connection") } if len(h.u.servers) != 2 { t.Errorf("connected to %d servers, want 2", len(h.u.servers)) }
// So does connecting to it by its address again; the second server's // session is kept for coming back to. h.u.Connect(bravo.Endpoint.String()) h.until("the second server again", func(f rexsession.Frame) bool { return f.View.SessionID == sidB && f.Ready() }) if len(h.u.servers) != 2 { t.Errorf("connected to %d servers, want 2", len(h.u.servers)) }
// A server that isn't there leaves the window where it was, saying why. h.u.Connect("unix:///nonexistent/rex.sock") h.until("the failed switch", func(rexsession.Frame) bool { _, err := h.u.connectState() return err != nil }) if f := h.frame(); f.View.SessionID != sidB { t.Errorf("after a failed switch, showing %s, want %s", f.View.SessionID, sidB) }}
// TestReconnect drops the connection, as a network failure would, and// checks the window reaches the server again and shows the same session.func TestReconnect(t *testing.T) { srv := rextest.Start(t) other := srv.Dial(t, "setup") rextest.NewSession(t, other, rextest.RawCat...) // a session the server might pick instead sid, bid := rextest.NewSession(t, other, rextest.RawCat...) h := startUI(t, srv, srv.Dial(t, "100gex-test"), sid) h.until("the session", func(f rexsession.Frame) bool { return f.Ready() && f.View.SessionID == sid }) first := h.u.conn
first.Drop() h.until("a new connection, showing the same session", func(f rexsession.Frame) bool { return h.u.conn != first && f.View.SessionID == sid && f.Ready() && f.Visible[0] == bid }) if !h.u.connected() { t.Error("not connected after reconnecting") }}
// TestAddHost adds a machine as a host, as the host picker does once a// person chooses one from the tailnet, and switches back with the hosts// list. The machine is a server's network listener, standing in for a// tailnet address.func TestAddHost(t *testing.T) { local := rextest.Start(t) t.Setenv(rex.ServerEnv, local.Endpoint.String()) // where hosts are kept port := rextest.FreePort(t) studio := rextest.StartArgs(t, "", "--extra-listen", "http://127.0.0.1:"+strconv.Itoa(port), "--allow-anonymous") sidLocal, _ := rextest.NewSession(t, local.Dial(t, "setup"), rextest.RawCat...) sidStudio, _ := rextest.NewSession(t, studio.Dial(t, "setup"), rextest.RawCat...)
h := startUI(t, local, local.Dial(t, "100gex-test"), sidLocal) h.u.SetTarget("", "") // the local server h.until("the local session", func(f rexsession.Frame) bool { return f.View.SessionID == sidLocal && f.Ready() })
// A machine with no Rex server says so, and the window stays. h.u.palette.toggle(paletteMachines) h.u.addMachine(tailnet.Machine{Name: "attic", Address: "127.0.0.1:" + strconv.Itoa(rextest.FreePort(t)), Online: true}) h.until("the palette to say there's no Rex", func(rexsession.Frame) bool { return h.u.palette.statusErr && strings.Contains(h.u.palette.status, "No Rex server answered on attic") })
// A machine with one is saved as a host, and the window switches to it. h.u.addMachine(tailnet.Machine{Name: "studio", Address: "127.0.0.1:" + strconv.Itoa(port), Online: true}) h.until("studio's session", func(f rexsession.Frame) bool { return f.View.SessionID == sidStudio && f.Ready() }) if name, where := h.u.serverName(); name != "studio" || where != "remote" { t.Errorf("server chip says %s, %s; want studio, remote", name, where) } if h.u.palette.open { t.Error("the palette is still open after switching") } h.until("studio to be saved", func(rexsession.Frame) bool { saved, err := hosts.Saved(context.Background()) return err == nil && len(saved) == 1 && saved[0].Label == "studio" })
// The hosts list marks studio, and switches back to this machine. h.u.openPalette(paletteHosts) h.until("the hosts", func(rexsession.Frame) bool { return !h.u.palette.loading && len(h.u.palette.shown) == 3 }) var titles []string for _, i := range h.u.palette.shown { it := h.u.palette.items[i] titles = append(titles, it.title) if it.current != (it.title == "studio") { t.Errorf("host %s: current = %v", it.title, it.current) } } if strings.Join(titles, ",") != "This machine,studio,Add host…" { t.Errorf("hosts = %v", titles) } h.u.palette.selected = 0 h.u.choose() h.until("this machine again", func(f rexsession.Frame) bool { return f.View.SessionID == sidLocal && f.Ready() })}
// TestAddMachineOverSSH adds a tailnet machine whose Rex server isn't on// the tailnet: it's reached over ssh instead, started there, and saved with// its ssh address.func TestAddMachineOverSSH(t *testing.T) { local := rextest.Start(t) t.Setenv(rex.ServerEnv, local.Endpoint.String()) box := sshtest.StartRex(t) sshtunnel.Options = []string{"-F", box.Config} t.Cleanup(func() { sshtunnel.Options = nil }) sidLocal, _ := rextest.NewSession(t, local.Dial(t, "setup"), rextest.RawCat...)
h := startUI(t, local, local.Dial(t, "100gex-test"), sidLocal) h.u.SetTarget("", "") h.until("the local session", func(f rexsession.Frame) bool { return f.View.SessionID == sidLocal && f.Ready() })
// Nothing serves Rex on the machine's tailnet address, here sshd's. address := "127.0.0.1:" + strconv.Itoa(box.Port) h.u.palette.toggle(paletteMachines) h.u.addMachine(tailnet.Machine{Name: "box", Address: address, Online: true}) h.until("box's server", func(f rexsession.Frame) bool { return h.u.ws.Endpoint().Via != "" && f.View.SessionID != "" && f.View.SessionID != sidLocal }) if name, where := h.u.serverName(); name != "box" || where != "remote" { t.Errorf("server chip says %s, %s; want box, remote", name, where) } h.until("box to be saved", func(rexsession.Frame) bool { saved, err := hosts.Saved(context.Background()) return err == nil && len(saved) == 1 && saved[0].Label == "box" && len(saved[0].Endpoints) == 1 && saved[0].Endpoints[0] == "ssh://"+address })}
// sidebarText spells the sidebar's rows: host for a heading, note for a// note, and A or B for the sessions a and b.func sidebarText(h *uiHarness, a, b string) string { var out []string for _, row := range h.u.sidebarRows(h.u.hosts(), time.Now()) { switch { case row.kind == rowHost: out = append(out, "host") case row.kind == rowNote: out = append(out, "note") case row.session.SessionID == a: out = append(out, "A") case row.session.SessionID == b: out = append(out, "B") default: out = append(out, "?") } } return strings.Join(out, ",")}