(t *testing.T)
| 105 | } |
| 106 | |
| 107 | func 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 | }, |
nothing calls this directly
no test coverage detected
searching dependent graphs…