File: hosts_file_test.go

package info (click to toggle)
golang-github-containers-gvisor-tap-vsocks 0.8.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 800 kB
  • sloc: sh: 95; makefile: 59
file content (41 lines) | stat: -rw-r--r-- 1,058 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package dns

import (
	"os"
	"path/filepath"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestHostsFile(t *testing.T) {
	hostsFile := filepath.Join(t.TempDir(), "hosts")
	assert.NoError(t, os.WriteFile(hostsFile, []byte(`127.0.0.1 entry1`), 0600))

	hosts, err := NewHostsFile(hostsFile)
	assert.NoError(t, err)
	ip, err := hosts.LookupByHostname("entry1")
	assert.NoError(t, err)
	assert.Equal(t, "127.0.0.1", ip.String())
}

func TestReloadingHostsFile(t *testing.T) {
	hostsFile := filepath.Join(t.TempDir(), "hosts")
	assert.NoError(t, os.WriteFile(hostsFile, []byte(`127.0.0.1   entry1`), 0600))

	hosts, err := NewHostsFile(hostsFile)
	time.Sleep(time.Second)
	assert.NoError(t, err)
	ip, err := hosts.LookupByHostname("entry1")
	assert.NoError(t, err)
	assert.Equal(t, "127.0.0.1", ip.String())

	assert.NoError(t, os.WriteFile(hostsFile, []byte(`127.0.0.1   entry2 foobar`), 0600))
	time.Sleep(time.Second)

	ipBar, err := hosts.LookupByHostname("foobar")
	assert.NoError(t, err)
	assert.Equal(t, "127.0.0.1", ipBar.String())

}