aboutsummaryrefslogtreecommitdiff
path: root/plugin/auto/watcher_test.go
blob: 43f1ff374f0f6c07bbab22729977be312a406dd4 (plain) (blame)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package auto

import (
	"os"
	"path/filepath"
	"regexp"
	"testing"
)

func TestWatcher(t *testing.T) {
	tempdir, err := createFiles()
	if err != nil {
		if tempdir != "" {
			os.RemoveAll(tempdir)
		}
		t.Fatal(err)
	}
	defer os.RemoveAll(tempdir)

	ldr := loader{
		directory: tempdir,
		re:        regexp.MustCompile(`db\.(.*)`),
		template:  `${1}`,
	}

	a := Auto{
		loader: ldr,
		Zones:  &Zones{},
	}

	a.Walk()

	// example.org and example.com should exist, we have 3 apex rrs and 1 "real" record. All() returns the non-apex ones.
	if x := len(a.Zones.Z["example.org."].All()); x != 1 {
		t.Fatalf("Expected 1 RRs, got %d", x)
	}
	if x := len(a.Zones.Z["example.com."].All()); x != 1 {
		t.Fatalf("Expected 1 RRs, got %d", x)
	}

	// Now remove one file, rescan and see if it's gone.
	if err := os.Remove(filepath.Join(tempdir, "db.example.com")); err != nil {
		t.Fatal(err)
	}

	a.Walk()

	if _, ok := a.Zones.Z["example.com."]; ok {
		t.Errorf("Expected %q to be gone.", "example.com.")
	}
	if _, ok := a.Zones.Z["example.org."]; !ok {
		t.Errorf("Expected %q to still be there.", "example.org.")
	}
}

func TestSymlinks(t *testing.T) {
	tempdir, err := createFiles()
	if err != nil {
		if tempdir != "" {
			os.RemoveAll(tempdir)
		}
		t.Fatal(err)
	}
	defer os.RemoveAll(tempdir)

	ldr := loader{
		directory: tempdir,
		re:        regexp.MustCompile(`db\.(.*)`),
		template:  `${1}`,
	}

	a := Auto{
		loader: ldr,
		Zones:  &Zones{},
	}

	a.Walk()

	// Now create a duplicate file in a subdirectory and repoint the symlink
	if err := os.Remove(filepath.Join(tempdir, "db.example.com")); err != nil {
		t.Fatal(err)
	}
	dataDir := filepath.Join(tempdir, "..data")
	if err = os.Mkdir(dataDir, 0755); err != nil {
		t.Fatal(err)
	}
	newFile := filepath.Join(dataDir, "db.example.com")
	if err = os.Symlink(filepath.Join(tempdir, "db.example.org"), newFile); err != nil {
		t.Fatal(err)
	}

	a.Walk()

	if storedZone, ok := a.Zones.Z["example.com."]; ok {
		storedFile := storedZone.File()
		if storedFile != newFile {
			t.Errorf("Expected %q to reflect new path %q", storedFile, newFile)
		}
	}
}