feat(diff): mark records deliberately kept outside the template
This commit is contained in:
@@ -242,3 +242,62 @@ func TestIndexDedupLastWriteWins(t *testing.T) {
|
||||
t.Fatalf("expected exactly 1 diff for the duplicated key, got %d", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkCustomOnlyMarksDeletes(t *testing.T) {
|
||||
template := []model.Record{
|
||||
{Type: "A", Name: "www.example.com.", TTL: 300, Values: []string{"1.1.1.1"}},
|
||||
}
|
||||
actual := []model.Record{
|
||||
{Type: "A", Name: "www.example.com.", TTL: 300, Values: []string{"2.2.2.2"}},
|
||||
{Type: "CNAME", Name: "admin.example.com.", TTL: 300, Values: []string{"example.com."}},
|
||||
}
|
||||
cs := Diff(template, actual)
|
||||
// Ключ записи, которую шаблон описывает (Update), и ключ лишней записи (Delete).
|
||||
cs.MarkCustom([]string{"A www.example.com.", "CNAME admin.example.com."})
|
||||
|
||||
customs := cs.Customs()
|
||||
if len(customs) != 1 {
|
||||
t.Fatalf("expected exactly 1 custom diff, got %d: %+v", len(customs), customs)
|
||||
}
|
||||
if customs[0].Key() != "CNAME admin.example.com." {
|
||||
t.Fatalf("expected the Delete diff to be custom, got %q", customs[0].Key())
|
||||
}
|
||||
// Шаблон побеждает: описанный шаблоном ключ остаётся обычным Update.
|
||||
updates := cs.Updates()
|
||||
if len(updates) != 1 || updates[0].Key() != "A www.example.com." {
|
||||
t.Fatalf("expected the templated record to stay in Updates, got %+v", updates)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCustomDiffsLeaveActionableAndPrunes(t *testing.T) {
|
||||
actual := []model.Record{
|
||||
{Type: "CNAME", Name: "admin.example.com.", TTL: 300, Values: []string{"example.com."}},
|
||||
}
|
||||
cs := Diff(nil, actual)
|
||||
if len(cs.Prunes()) != 1 {
|
||||
t.Fatalf("precondition: expected 1 prune before marking, got %+v", cs.Prunes())
|
||||
}
|
||||
cs.MarkCustom([]string{"CNAME admin.example.com."})
|
||||
|
||||
if got := cs.Prunes(); len(got) != 0 {
|
||||
t.Fatalf("expected custom diff to leave Prunes, got %+v", got)
|
||||
}
|
||||
if got := cs.Actionable(); len(got) != 0 {
|
||||
t.Fatalf("expected custom diff to leave Actionable, got %+v", got)
|
||||
}
|
||||
if got := cs.Customs(); len(got) != 1 {
|
||||
t.Fatalf("expected 1 custom diff, got %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkCustomNeverMarksReadOnly(t *testing.T) {
|
||||
actual := []model.Record{
|
||||
{Type: "NS", Name: "example.com.", TTL: 300, Values: []string{"ns1.example.com."}},
|
||||
}
|
||||
cs := Diff(nil, actual)
|
||||
cs.MarkCustom([]string{"NS example.com."})
|
||||
|
||||
if got := cs.Customs(); len(got) != 0 {
|
||||
t.Fatalf("read-only diffs must never become custom, got %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user