130 lines
4.4 KiB
Go
130 lines
4.4 KiB
Go
// Tes rediscover best-effort (Bug 1 v0.6.1): setelah tulis zona berhasil,
|
|
// Hostora harus memanggil `pdns_control rediscover` agar pdns 4.8.3
|
|
// langsung menjawab zona baru; kegagalan rediscover tidak boleh
|
|
// menggagalkan sync/hapus. Memakai pdns_control PALSU (skrip shell di
|
|
// PATH) — BUKAN pdns asli; pdns asli tidak terpasang di VM ini.
|
|
package dns
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/maspend/hostora/internal/store"
|
|
)
|
|
|
|
const fakePdnsControlScript = `#!/bin/bash
|
|
echo "$*" >> "$FAKE_PDNS_CONTROL_LOG"
|
|
[ "$FAKE_PDNS_CONTROL_FAIL" = "1" ] && exit 1
|
|
exit 0
|
|
`
|
|
|
|
// setupFakePdnsControl memasang pdns_control palsu di direktori bin yang
|
|
// sama dengan pdnsutil palsu dari setupFakePdnsutil, mengembalikan path
|
|
// file log panggilannya. Juga memasang pdns_server palsu (no-op) agar
|
|
// Backend menganggap PowerDNS terpasang (Status memeriksa binari itu).
|
|
func setupFakePdnsControl(t *testing.T, pdnsutilLogPath string) string {
|
|
t.Helper()
|
|
binDir := filepath.Join(filepath.Dir(pdnsutilLogPath), "bin")
|
|
script := filepath.Join(binDir, "pdns_control")
|
|
if err := os.WriteFile(script, []byte(fakePdnsControlScript), 0o755); err != nil {
|
|
t.Fatalf("tulis pdns_control palsu: %v", err)
|
|
}
|
|
server := filepath.Join(binDir, "pdns_server")
|
|
if err := os.WriteFile(server, []byte("#!/bin/bash\nexit 0\n"), 0o755); err != nil {
|
|
t.Fatalf("tulis pdns_server palsu: %v", err)
|
|
}
|
|
controlLog := filepath.Join(filepath.Dir(pdnsutilLogPath), "control.log")
|
|
t.Setenv("FAKE_PDNS_CONTROL_LOG", controlLog)
|
|
return controlLog
|
|
}
|
|
|
|
func readControlCalls(t *testing.T, logPath string) []string {
|
|
t.Helper()
|
|
raw, err := os.ReadFile(logPath)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return nil
|
|
}
|
|
t.Fatalf("tidak bisa membaca log pdns_control palsu: %v", err)
|
|
}
|
|
lines := strings.Split(strings.TrimSpace(string(raw)), "\n")
|
|
if len(lines) == 1 && lines[0] == "" {
|
|
return nil
|
|
}
|
|
return lines
|
|
}
|
|
|
|
func rediscoverCalled(t *testing.T, logPath string) bool {
|
|
t.Helper()
|
|
for _, c := range readControlCalls(t, logPath) {
|
|
if c == "rediscover" {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func testSyncZone() store.DNSZone {
|
|
return store.DNSZone{
|
|
Domain: "contoh.test",
|
|
Records: []store.DNSRecord{
|
|
{ID: "ns-ns1.hostora.xyz", Type: "NS", Name: "@", Value: "ns1.hostora.xyz", TTL: 3600},
|
|
{ID: "ns-ns2.hostora.xyz", Type: "NS", Name: "@", Value: "ns2.hostora.xyz", TTL: 3600},
|
|
{ID: "a-1", Type: "A", Name: "@", Value: "203.0.113.10", TTL: 3600},
|
|
{ID: "a-2", Type: "A", Name: "www", Value: "203.0.113.10", TTL: 3600},
|
|
},
|
|
}
|
|
}
|
|
|
|
// SyncZoneFromStore harus memanggil `pdns_control rediscover` setelah
|
|
// tulis zona berhasil.
|
|
func TestSyncZoneCallsRediscover(t *testing.T) {
|
|
b, pdnsutilLog := setupFakePdnsutil(t)
|
|
controlLog := setupFakePdnsControl(t, pdnsutilLog)
|
|
if err := b.SyncZoneFromStore(testSyncZone()); err != nil {
|
|
t.Fatalf("SyncZoneFromStore: %v", err)
|
|
}
|
|
if !rediscoverCalled(t, controlLog) {
|
|
t.Errorf("pdns_control tidak dipanggil rediscover (panggilan: %v)", readControlCalls(t, controlLog))
|
|
}
|
|
}
|
|
|
|
// DeleteZone harus memanggil `pdns_control rediscover` setelah hapus
|
|
// berhasil, agar pdns berhenti menjawab zona yang sudah dihapus.
|
|
func TestDeleteZoneCallsRediscover(t *testing.T) {
|
|
b, pdnsutilLog := setupFakePdnsutil(t)
|
|
controlLog := setupFakePdnsControl(t, pdnsutilLog)
|
|
if err := b.CreateZoneWithNS("contoh.test", "ns1.hostora.xyz"); err != nil {
|
|
t.Fatalf("CreateZoneWithNS: %v", err)
|
|
}
|
|
if err := b.DeleteZone("contoh.test"); err != nil {
|
|
t.Fatalf("DeleteZone: %v", err)
|
|
}
|
|
if !rediscoverCalled(t, controlLog) {
|
|
t.Errorf("pdns_control tidak dipanggil rediscover setelah hapus (panggilan: %v)", readControlCalls(t, controlLog))
|
|
}
|
|
}
|
|
|
|
// Sync tetap sukses bila pdns_control gagal (mis. pdns tidak jalan /
|
|
// socket control tidak ada) — rediscover tidak boleh menggagalkan sync.
|
|
func TestSyncZoneSucceedsWhenRediscoverFails(t *testing.T) {
|
|
b, pdnsutilLog := setupFakePdnsutil(t)
|
|
_ = setupFakePdnsControl(t, pdnsutilLog)
|
|
t.Setenv("FAKE_PDNS_CONTROL_FAIL", "1")
|
|
if err := b.SyncZoneFromStore(testSyncZone()); err != nil {
|
|
t.Fatalf("SyncZoneFromStore harus tetap sukses saat rediscover gagal: %v", err)
|
|
}
|
|
}
|
|
|
|
// rediscoverFunc mengembalikan error (bukan panic) bila binari
|
|
// pdns_control tidak ada; rediscover() sendiri menelan error itu.
|
|
func TestRediscoverFuncMissingBinary(t *testing.T) {
|
|
t.Setenv("PATH", t.TempDir())
|
|
if err := rediscoverFunc(); err == nil {
|
|
t.Errorf("rediscoverFunc tanpa pdns_control: mau error, dapat nil")
|
|
}
|
|
rediscover() // tidak boleh panic
|
|
}
|