Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116package hosts
import ( "context" "errors" "os" "os/exec" "testing" "time"
"100gex/internal/rex" "100gex/internal/rex/rextest" "100gex/internal/sshtunnel" "100gex/internal/sshtunnel/sshtest")
// TestDialLabel names a server by a host's label, which the local server// (here, the one REX_SERVER names) keeps, as rex -S does.func TestDialLabel(t *testing.T) { local := rextest.Start(t) studio := rextest.Start(t) t.Setenv(rex.ServerEnv, local.Endpoint.String()) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel()
if err := Save(ctx, "studio", studio.Endpoint.String()); err != nil { t.Fatal(err) } conn, ep, label, err := Dial(ctx, "Studio") // labels ignore case if err != nil { t.Fatal(err) } defer conn.Close() if label != "studio" || ep != studio.Endpoint { t.Errorf("Dial(Studio) = %v, label %q; want %v, studio", ep, label, studio.Endpoint) }
// Saving the label again replaces the host, rather than adding one. if err := Save(ctx, "studio", "studio.tail1234.ts.net"); err != nil { t.Fatal(err) } saved, err := Saved(ctx) if err != nil { t.Fatal(err) } if len(saved) != 1 || saved[0].Endpoints[0] != "studio.tail1234.ts.net" { t.Errorf("hosts after saving twice = %+v, want one, at its new address", saved) }
// A name that isn't a label is an address. if _, _, _, err := Dial(ctx, "nosuchhost.invalid"); err == nil { t.Error("connected to an address that can't resolve") }}
// TestDialSSH reaches a host's server over ssh, starting it there, and// checks the tunnel goes with the connection, and that the connection ends// if ssh does.func TestDialSSH(t *testing.T) { local := rextest.Start(t) t.Setenv(rex.ServerEnv, local.Endpoint.String()) s := sshtest.StartRex(t) sshtunnel.Options = []string{"-F", s.Config} t.Cleanup(func() { sshtunnel.Options = nil }) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel()
// A host's addresses are tried in turn. if err := saveEndpoints(ctx, "box", "http://127.0.0.1:1", s.Address); err != nil { t.Fatal(err) } conn, ep, label, err := Dial(ctx, "box") if err != nil { t.Fatal(err) } if label != "box" || ep.Via != s.Address || ep.Local() { t.Errorf("Dial(box) = %v (via %q), label %q", ep, ep.Via, label) } if _, err := conn.Call(ctx, "session.list", nil, nil); err != nil { t.Errorf("session.list over ssh: %v", err) } conn.Close() if _, err := os.Stat(ep.Socket); !errors.Is(err, os.ErrNotExist) { t.Errorf("the tunnel's socket is still there after Close: %v", err) }
// ssh ending, as when the network goes, ends the connection. conn, ep, _, err = Dial(ctx, s.Address) if err != nil { t.Fatal(err) } defer conn.Close() out, err := exec.Command("pkill", "-f", "--", "-L "+ep.Socket).CombinedOutput() if err != nil { t.Fatalf("pkill: %v %s", err, out) } deadline := time.Now().Add(10 * time.Second) for conn.Err() == nil && time.Now().Before(deadline) { time.Sleep(20 * time.Millisecond) } if err := conn.Err(); err == nil || errors.Is(err, rex.ErrClosed) { t.Errorf("after ssh ended, the connection's error is %v", err) }}
// saveEndpoints saves a host with several addresses, as rex hosts add does.func saveEndpoints(ctx context.Context, label string, endpoints ...string) error { c, err := local(ctx) if err != nil { return err } defer c.Close() _, err = c.Call(ctx, "hosts.put", map[string]any{"host": map[string]any{"label": label, "endpoints": endpoints}}, nil) return err}