net/dnsfallback: more explicitly pass through logf function

Redoes the approach from #5550 and #7539 to explicitly pass in the logf
function, instead of having global state that can be overridden.

Signed-off-by: Mihai Parparita <mihai@tailscale.com>
This commit is contained in:
Mihai Parparita
2023-04-17 10:58:40 -07:00
committed by Mihai Parparita
parent 28cb1221ba
commit 9a655a1d58
8 changed files with 20 additions and 66 deletions

View File

@ -24,11 +24,6 @@ func TestGetDERPMap(t *testing.T) {
}
func TestCache(t *testing.T) {
oldlog := logfunc.Load()
SetLogger(t.Logf)
t.Cleanup(func() {
SetLogger(oldlog)
})
cacheFile := filepath.Join(t.TempDir(), "cache.json")
// Write initial cache value
@ -73,7 +68,7 @@ func TestCache(t *testing.T) {
cachedDERPMap.Store(nil)
// Load the cache
SetCachePath(cacheFile)
SetCachePath(cacheFile, t.Logf)
if cm := cachedDERPMap.Load(); !reflect.DeepEqual(initialCache, cm) {
t.Fatalf("cached map was %+v; want %+v", cm, initialCache)
}
@ -105,11 +100,6 @@ func TestCache(t *testing.T) {
}
func TestCacheUnchanged(t *testing.T) {
oldlog := logfunc.Load()
SetLogger(t.Logf)
t.Cleanup(func() {
SetLogger(oldlog)
})
cacheFile := filepath.Join(t.TempDir(), "cache.json")
// Write initial cache value
@ -140,7 +130,7 @@ func TestCacheUnchanged(t *testing.T) {
cachedDERPMap.Store(nil)
// Load the cache
SetCachePath(cacheFile)
SetCachePath(cacheFile, t.Logf)
if cm := cachedDERPMap.Load(); !reflect.DeepEqual(initialCache, cm) {
t.Fatalf("cached map was %+v; want %+v", cm, initialCache)
}
@ -152,7 +142,7 @@ func TestCacheUnchanged(t *testing.T) {
t.Fatal(err)
}
UpdateCache(initialCache)
UpdateCache(initialCache, t.Logf)
if _, err := os.Stat(cacheFile); !os.IsNotExist(err) {
t.Fatalf("got err=%v; expected to not find cache file", err)
}
@ -173,7 +163,7 @@ func TestCacheUnchanged(t *testing.T) {
clonedNode.IPv4 = "1.2.3.5"
updatedCache.Regions[99].Nodes = append(updatedCache.Regions[99].Nodes, &clonedNode)
UpdateCache(updatedCache)
UpdateCache(updatedCache, t.Logf)
if st, err := os.Stat(cacheFile); err != nil {
t.Fatalf("could not stat cache file; err=%v", err)
} else if !st.Mode().IsRegular() || st.Size() == 0 {