mirror of
https://github.com/coredns/coredns.git
synced 2025-10-31 18:23:13 -04:00
middleware/file: add test for reload (#361)
This add a highlevel integration test for zone reloading. It also fixes a data race in the actual reloading process.
This commit is contained in:
@@ -26,13 +26,31 @@ const (
|
||||
// Three sets of records are returned, one for the answer, one for authority and one for the additional section.
|
||||
func (z *Zone) Lookup(qname string, qtype uint16, do bool) ([]dns.RR, []dns.RR, []dns.RR, Result) {
|
||||
if qtype == dns.TypeSOA {
|
||||
if !z.NoReload {
|
||||
z.reloadMu.RLock()
|
||||
}
|
||||
return z.lookupSOA(do)
|
||||
if !z.NoReload {
|
||||
z.reloadMu.RUnlock()
|
||||
}
|
||||
}
|
||||
if qtype == dns.TypeNS && qname == z.origin {
|
||||
if !z.NoReload {
|
||||
z.reloadMu.RLock()
|
||||
}
|
||||
return z.lookupNS(do)
|
||||
if !z.NoReload {
|
||||
z.reloadMu.RUnlock()
|
||||
}
|
||||
}
|
||||
|
||||
if !z.NoReload {
|
||||
z.reloadMu.RLock()
|
||||
}
|
||||
elem, res := z.Tree.Search(qname, qtype)
|
||||
if !z.NoReload {
|
||||
z.reloadMu.RUnlock()
|
||||
}
|
||||
if elem == nil {
|
||||
if res == tree.EmptyNonTerminal {
|
||||
return z.emptyNonTerminal(qname, do)
|
||||
|
||||
@@ -163,22 +163,24 @@ func (z *Zone) Reload() error {
|
||||
select {
|
||||
case event := <-watcher.Events:
|
||||
if event.Op == fsnotify.Write && path.Clean(event.Name) == z.file {
|
||||
|
||||
reader, err := os.Open(z.file)
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] Failed to open `%s' for `%s': %v", z.file, z.origin, err)
|
||||
continue
|
||||
}
|
||||
z.reloadMu.Lock()
|
||||
zone, err := Parse(reader, z.origin, z.file)
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] Failed to parse `%s': %v", z.origin, err)
|
||||
z.reloadMu.Unlock()
|
||||
continue
|
||||
}
|
||||
|
||||
// copy elements we need
|
||||
z.reloadMu.Lock()
|
||||
z.Apex = zone.Apex
|
||||
z.Tree = zone.Tree
|
||||
z.reloadMu.Unlock()
|
||||
|
||||
log.Printf("[INFO] Successfully reloaded zone `%s'", z.origin)
|
||||
z.Notify()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user