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

Function TestCacheUnchanged

net/dnsfallback/dnsfallback_test.go:107–177  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

105}
106
107func TestCacheUnchanged(t *testing.T) {
108 cacheFile := filepath.Join(t.TempDir(), "cache.json")
109
110 // Write initial cache value
111 initialCache := &tailcfg.DERPMap{
112 Regions: map[int]*tailcfg.DERPRegion{
113 99: {
114 RegionID: 99,
115 RegionCode: "test",
116 RegionName: "Testville",
117 Nodes: []*tailcfg.DERPNode{{
118 Name: "99a",
119 RegionID: 99,
120 HostName: "derp99a.tailscale.com",
121 IPv4: "1.2.3.4",
122 }},
123 },
124 },
125 }
126 d, err := json.Marshal(initialCache)
127 if err != nil {
128 t.Fatal(err)
129 }
130 if err := os.WriteFile(cacheFile, d, 0666); err != nil {
131 t.Fatal(err)
132 }
133
134 // Clear any existing cached DERP map(s)
135 cachedDERPMap.Store(nil)
136
137 // Load the cache
138 SetCachePath(cacheFile, t.Logf)
139 if cm := cachedDERPMap.Load(); !reflect.DeepEqual(initialCache, cm) {
140 t.Fatalf("cached map was %+v; want %+v", cm, initialCache)
141 }
142
143 // Remove the cache file on-disk, then re-set to the current value. If
144 // our equality comparison is working, we won't rewrite the file
145 // on-disk since the cached value won't have changed.
146 if err := os.Remove(cacheFile); err != nil {
147 t.Fatal(err)
148 }
149
150 UpdateCache(initialCache, t.Logf)
151 if _, err := os.Stat(cacheFile); !os.IsNotExist(err) {
152 t.Fatalf("got err=%v; expected to not find cache file", err)
153 }
154
155 // Now, update the cache with something slightly different and verify
156 // that we did re-write the file on-disk.
157 updatedCache := &tailcfg.DERPMap{
158 Regions: map[int]*tailcfg.DERPRegion{
159 99: {
160 RegionID: 99,
161 RegionCode: "test",
162 RegionName: "Testville",
163 Nodes: []*tailcfg.DERPNode{ /* set below */ },
164 },

Callers

nothing calls this directly

Calls 13

SetCachePathFunction · 0.85
UpdateCacheFunction · 0.85
ModeMethod · 0.80
TempDirMethod · 0.65
MarshalMethod · 0.65
FatalMethod · 0.65
WriteFileMethod · 0.65
StoreMethod · 0.65
LoadMethod · 0.65
FatalfMethod · 0.65
RemoveMethod · 0.65
StatMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…