-: fix autohosts tests on Windows

This commit is contained in:
Andrey Meshkov 2020-04-15 14:36:47 +03:00
parent e96fd6e42d
commit 4bdf22eadc
2 changed files with 88 additions and 43 deletions

View File

@ -23,7 +23,7 @@ type AutoHosts struct {
hostsFn string // path to the main hosts-file hostsFn string // path to the main hosts-file
hostsDirs []string // paths to OS-specific directories with hosts-files hostsDirs []string // paths to OS-specific directories with hosts-files
watcher *fsnotify.Watcher // file and directory watcher object watcher *fsnotify.Watcher // file and directory watcher object
updateChan chan bool // signal for 'update' goroutine updateChan chan bool // signal for 'updateLoop' goroutine
onChanged onChangedT // notification to other modules onChanged onChangedT // notification to other modules
} }
@ -68,20 +68,22 @@ func (a *AutoHosts) Init(hostsFn string) {
// Start - start module // Start - start module
func (a *AutoHosts) Start() { func (a *AutoHosts) Start() {
go a.update() log.Debug("Start AutoHosts module")
go a.updateLoop()
a.updateChan <- true a.updateChan <- true
go a.watcherLoop() go a.watcherLoop()
err := a.watcher.Add(a.hostsFn) err := a.watcher.Add(a.hostsFn)
if err != nil { if err != nil {
log.Error("AutoHosts: %s", err) log.Error("Error while initializing watcher for a file %s: %s", a.hostsFn, err)
} }
for _, dir := range a.hostsDirs { for _, dir := range a.hostsDirs {
err = a.watcher.Add(dir) err = a.watcher.Add(dir)
if err != nil { if err != nil {
log.Error("AutoHosts: %s", err) log.Error("Error while initializing watcher for a directory %s: %s", dir, err)
} }
} }
} }
@ -89,7 +91,8 @@ func (a *AutoHosts) Start() {
// Close - close module // Close - close module
func (a *AutoHosts) Close() { func (a *AutoHosts) Close() {
a.updateChan <- false a.updateChan <- false
a.watcher.Close() close(a.updateChan)
_ = a.watcher.Close()
} }
// Read IP-hostname pairs from file // Read IP-hostname pairs from file
@ -174,7 +177,7 @@ func (a *AutoHosts) watcherLoop() {
log.Debug("AutoHosts: modified: %s", event.Name) log.Debug("AutoHosts: modified: %s", event.Name)
select { select {
case a.updateChan <- true: case a.updateChan <- true:
// sent a signal to 'update' goroutine // sent a signal to 'updateLoop' goroutine
default: default:
// queue is full // queue is full
} }
@ -189,15 +192,23 @@ func (a *AutoHosts) watcherLoop() {
} }
} }
// Read static hosts from system files // updateLoop - read static hosts from system files
func (a *AutoHosts) update() { func (a *AutoHosts) updateLoop() {
for { for {
select { select {
case ok := <-a.updateChan: case ok := <-a.updateChan:
if !ok { if !ok {
log.Debug("Finished AutoHosts update loop")
return return
} }
a.updateHosts()
}
}
}
// updateHosts - loads system hosts
func (a *AutoHosts) updateHosts() {
table := make(map[string][]net.IP) table := make(map[string][]net.IP)
a.load(table, a.hostsFn) a.load(table, a.hostsFn)
@ -219,10 +230,9 @@ func (a *AutoHosts) update() {
a.lock.Lock() a.lock.Lock()
a.table = table a.table = table
a.lock.Unlock() a.lock.Unlock()
a.notify() a.notify()
} }
}
}
// Process - get the list of IP addresses for the hostname // Process - get the list of IP addresses for the hostname
// Return nil if not found // Return nil if not found

View File

@ -17,38 +17,73 @@ func prepareTestDir() string {
return dir return dir
} }
func TestAutoHosts(t *testing.T) { func TestAutoHostsResolution(t *testing.T) {
ah := AutoHosts{} ah := AutoHosts{}
dir := prepareTestDir() dir := prepareTestDir()
defer func() { _ = os.RemoveAll(dir) }() defer func() { _ = os.RemoveAll(dir) }()
f, _ := ioutil.TempFile(dir, "") f, _ := ioutil.TempFile(dir, "")
defer os.Remove(f.Name()) defer func() { _ = os.Remove(f.Name()) }()
defer f.Close() defer f.Close()
_, _ = f.WriteString(" 127.0.0.1 host localhost \n") _, _ = f.WriteString(" 127.0.0.1 host localhost \n")
ah.Init(f.Name()) ah.Init(f.Name())
ah.Start()
// wait until we parse the file
time.Sleep(50 * time.Millisecond)
// Update from the hosts file
ah.updateHosts()
// Existing host
ips := ah.Process("localhost") ips := ah.Process("localhost")
assert.True(t, ips[0].Equal(net.ParseIP("127.0.0.1"))) assert.NotNil(t, ips)
ips = ah.Process("newhost") assert.Equal(t, 1, len(ips))
assert.True(t, ips == nil) assert.Equal(t, net.ParseIP("127.0.0.1"), ips[0])
// Unknown host
ips = ah.Process("newhost")
assert.Nil(t, ips)
// Test hosts file
table := ah.List() table := ah.List()
ips, _ = table["host"] ips, _ = table["host"]
assert.True(t, ips[0].String() == "127.0.0.1") assert.NotNil(t, ips)
assert.Equal(t, 1, len(ips))
assert.Equal(t, "127.0.0.1", ips[0].String())
}
func TestAutoHostsFSNotify(t *testing.T) {
ah := AutoHosts{}
dir := prepareTestDir()
defer func() { _ = os.RemoveAll(dir) }()
f, _ := ioutil.TempFile(dir, "")
defer func() { _ = os.Remove(f.Name()) }()
defer f.Close()
// Init
_, _ = f.WriteString(" 127.0.0.1 host localhost \n")
ah.Init(f.Name())
ah.updateHosts()
// Unknown host
ips := ah.Process("newhost")
assert.Nil(t, ips)
// Stat monitoring for changes
ah.Start()
defer ah.Close()
// Update file
_, _ = f.WriteString("127.0.0.2 newhost\n") _, _ = f.WriteString("127.0.0.2 newhost\n")
_ = f.Sync()
// wait until fsnotify has triggerred and processed the file-modification event // wait until fsnotify has triggerred and processed the file-modification event
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
// Check if we are notified about changes
ips = ah.Process("newhost") ips = ah.Process("newhost")
assert.True(t, ips[0].Equal(net.ParseIP("127.0.0.2"))) assert.NotNil(t, ips)
assert.Equal(t, 1, len(ips))
ah.Close() assert.Equal(t, "127.0.0.2", ips[0].String())
} }