MCPcopy Create free account
hub / github.com/tailscale/tailscale / TestDNSTrampleRecovery

Function TestDNSTrampleRecovery

net/dns/direct_linux_test.go:21–76  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

19)
20
21func TestDNSTrampleRecovery(t *testing.T) {
22 t.Cleanup(HookWatchFile.SetForTest(watchFile))
23 synctest.Test(t, func(t *testing.T) {
24 tmp := t.TempDir()
25 if err := os.MkdirAll(filepath.Join(tmp, "etc"), 0700); err != nil {
26 t.Fatal(err)
27 }
28 const resolvPath = "/etc/resolv.conf"
29 fs := directFS{prefix: tmp}
30 readFile := func(t *testing.T, path string) string {
31 t.Helper()
32 b, err := fs.ReadFile(path)
33 if err != nil {
34 t.Errorf("Reading DNS config: %v", err)
35 }
36 return string(b)
37 }
38
39 bus := eventbustest.NewBus(t)
40 eventbustest.LogAllEvents(t, bus)
41 m := newDirectManagerOnFS(t.Logf, nil, bus, fs)
42 defer m.Close()
43
44 if err := m.SetDNS(OSConfig{
45 Nameservers: []netip.Addr{netip.MustParseAddr("8.8.8.8"), netip.MustParseAddr("8.8.4.4")},
46 SearchDomains: []dnsname.FQDN{"ts.net.", "ts-dns.test."},
47 MatchDomains: []dnsname.FQDN{"ignored."},
48 }); err != nil {
49 t.Fatal(err)
50 }
51
52 const want = `# resolv.conf(5) file generated by tailscale
53# For more info, see https://tailscale.com/s/resolvconf-overwrite
54# DO NOT EDIT THIS FILE BY HAND -- CHANGES WILL BE OVERWRITTEN
55
56nameserver 8.8.8.8
57nameserver 8.8.4.4
58search ts.net ts-dns.test
59`
60 if got := readFile(t, resolvPath); got != want {
61 t.Fatalf("resolv.conf:\n%s, want:\n%s", got, want)
62 }
63
64 tw := eventbustest.NewWatcher(t, bus)
65
66 const trample = "Hvem er det som tramper på min bro?"
67 if err := fs.WriteFile(resolvPath, []byte(trample), 0644); err != nil {
68 t.Fatal(err)
69 }
70 synctest.Wait()
71
72 if err := eventbustest.Expect(tw, eventbustest.Type[TrampleDNS]()); err != nil {
73 t.Errorf("did not see trample event: %s", err)
74 }
75 })
76}
77
78// watchFile is a test implementation of the file watcher that uses a timer

Callers

nothing calls this directly

Calls 15

ReadFileMethod · 0.95
WriteFileMethod · 0.95
NewBusFunction · 0.92
LogAllEventsFunction · 0.92
NewWatcherFunction · 0.92
ExpectFunction · 0.92
newDirectManagerOnFSFunction · 0.85
readFileFunction · 0.85
CleanupMethod · 0.65
TempDirMethod · 0.65
FatalMethod · 0.65
HelperMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…