| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | package auto | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2018-10-21 15:59:37 +02:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	"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}`, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | 	a := Auto{ | 
					
						
							|  |  |  | 		loader: ldr, | 
					
						
							|  |  |  | 		Zones:  &Zones{}, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | 	a.Walk() | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-30 13:47:27 +01:00
										 |  |  | 	// 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) | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-30 13:47:27 +01:00
										 |  |  | 	if x := len(a.Zones.Z["example.com."].All()); x != 1 { | 
					
						
							|  |  |  | 		t.Fatalf("Expected 1 RRs, got %d", x) | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Now remove one file, rescan and see if it's gone. | 
					
						
							| 
									
										
										
										
											2018-10-21 15:59:37 +02:00
										 |  |  | 	if err := os.Remove(filepath.Join(tempdir, "db.example.com")); err != nil { | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | 	a.Walk() | 
					
						
							| 
									
										
										
										
											2016-11-07 08:00:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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.") | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-03 06:20:39 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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 | 
					
						
							| 
									
										
										
										
											2018-10-21 15:59:37 +02:00
										 |  |  | 	if err := os.Remove(filepath.Join(tempdir, "db.example.com")); err != nil { | 
					
						
							| 
									
										
										
										
											2018-07-03 06:20:39 -04:00
										 |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-21 15:59:37 +02:00
										 |  |  | 	dataDir := filepath.Join(tempdir, "..data") | 
					
						
							| 
									
										
										
										
											2018-07-03 06:20:39 -04:00
										 |  |  | 	if err = os.Mkdir(dataDir, 0755); err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-21 15:59:37 +02:00
										 |  |  | 	newFile := filepath.Join(dataDir, "db.example.com") | 
					
						
							|  |  |  | 	if err = os.Symlink(filepath.Join(tempdir, "db.example.org"), newFile); err != nil { | 
					
						
							| 
									
										
										
										
											2018-07-03 06:20:39 -04:00
										 |  |  | 		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) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-17 18:37:56 +01:00
										 |  |  | } |