package main import ( "context" "encoding/json" "os" "path/filepath" "testing" "time" ) // Run on a real machine with MESH_LIVE=1: every tool reads this machine's buses, and the watcher's // connection answers. Nothing is changed. func TestLiveTools(t *testing.T) { if os.Getenv("MESH_LIVE") == "" { t.Skip("set MESH_LIVE=1 on a machine with a system bus") } ctx := context.Background() m := Here() b, err := DialSystemBus() if err != nil { t.Fatal(err) } defer b.Close() if err := b.Ping(ctx); err != nil { t.Fatal(err) } id, _ := b.ID(ctx) pid, _ := b.PID(ctx, busName) t.Logf("bus %s, driver pid %d in %s", id, pid, m.UnitOf(pid)) show := func(name string, v any, err error) { raw, _ := json.Marshal(v) if len(raw) > 1500 { raw = append(raw[:1500], "…"...) } t.Logf("%s: %v\n%s", name, err, raw) } n, err := m.Names(ctx, "system") show("names system", n, err) n, err = m.Names(ctx, "session") show("names session", n, err) o, err := m.Introspect(ctx, "system", "org.freedesktop.login1", "/org/freedesktop/login1") show("introspect", o, err) w, err := m.Monitor(ctx, "system", 2, "", nil) show("monitor", w, err) show("health", m.Health(ctx, nil), nil) show("check", m.Check(ctx, nil), nil) } // Run with MESH_LIVE=1: the watcher connects, pings and baselines the names, and says nothing. func TestLiveWatcher(t *testing.T) { if os.Getenv("MESH_LIVE") == "" { t.Skip("set MESH_LIVE=1 on a machine with a system bus") } m := Here() var said []string w := NewWatcher(m, func(e string, b any) error { said = append(said, e); return nil }, DialSystemBus, m.Denials) w.state = filepath.Join(t.TempDir(), "bus") ctx, cancel := context.WithTimeout(context.Background(), PingEvery+2*time.Second) defer cancel() w.Run(ctx) s := w.Snapshot() t.Logf("%+v said %v", s, said) if s.BusID == "" || s.LastPingAt == "" || s.Stalled || s.Services == 0 { t.Fatalf("%+v", s) } }