| 1 | package dnsrec |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // A record is identified by (name, type), not by value. That is what makes |
| 10 | // EnsureRecord an UPSERT rather than a create, and an upsert is what makes |
| 11 | // every walk in internal/mail re-runnable after a half-failure. |
| 12 | func TestKeyIsNameAndTypeOnly(t *testing.T) { |
| 13 | a := Record{Name: "mail-eu-west-1.example.com", Type: TypeTXT, Values: []string{"v=spf1 -all"}} |
| 14 | b := Record{Name: "mail-eu-west-1.example.com", Type: TypeTXT, Values: []string{"different"}} |
| 15 | if a.Key() != b.Key() { |
| 16 | t.Fatal("Key must not depend on Values — an upsert has to find the record it replaces") |
| 17 | } |
| 18 | c := Record{Name: "mail-eu-west-1.example.com", Type: TypeMX, Values: []string{"10 x"}} |
| 19 | if a.Key() == c.Key() { |
| 20 | t.Fatal("Key must depend on Type — MX and TXT coexist at one name") |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func TestMemWriterUpsertsAndDeletes(t *testing.T) { |
| 25 | ctx, w := context.Background(), NewMemWriter() |
| 26 | r := Record{Name: "x._domainkey.example.com", Type: TypeCNAME, |
| 27 | Values: []string{"x.dkim.amazonses.com"}, TTL: 300} |
| 28 | if err := w.EnsureRecord(ctx, r); err != nil { |
| 29 | t.Fatalf("EnsureRecord: %v", err) |
| 30 | } |
| 31 | if got := w.Records[r.Key()]; got.Values[0] != "x.dkim.amazonses.com" { |
| 32 | t.Fatalf("record not stored: %+v", got) |
| 33 | } |
| 34 | // Re-ensuring with a new value overwrites rather than duplicating. |
| 35 | r.Values = []string{"y.dkim.amazonses.com"} |
| 36 | if err := w.EnsureRecord(ctx, r); err != nil { |
| 37 | t.Fatalf("re-EnsureRecord: %v", err) |
| 38 | } |
| 39 | if len(w.Records) != 1 || w.Records[r.Key()].Values[0] != "y.dkim.amazonses.com" { |
| 40 | t.Fatalf("upsert did not replace: %+v", w.Records) |
| 41 | } |
| 42 | if err := w.DeleteRecord(ctx, r); err != nil { |
| 43 | t.Fatalf("DeleteRecord: %v", err) |
| 44 | } |
| 45 | if len(w.Records) != 0 { |
| 46 | t.Fatalf("delete left %d records", len(w.Records)) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // "Already gone" is a success, because teardown must be re-runnable. |
| 51 | func TestMemWriterDeleteAbsentIsSuccess(t *testing.T) { |
| 52 | w := NewMemWriter() |
| 53 | if err := w.DeleteRecord(context.Background(), |
| 54 | Record{Name: "nope.example.com", Type: TypeTXT}); err != nil { |
| 55 | t.Fatalf("deleting an absent record returned %v, want nil", err) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // FailNext is one-shot, so a test can drive a walk through a half-failure |
| 60 | // at an exact step and then let the retry succeed. |
| 61 | func TestMemWriterFailNextIsOneShot(t *testing.T) { |
| 62 | ctx, w := context.Background(), NewMemWriter() |
| 63 | boom := errors.New("boom") |
| 64 | w.FailNext["EnsureRecord"] = boom |
| 65 | r := Record{Name: "a.example.com", Type: TypeTXT, Values: []string{"v"}} |
| 66 | if err := w.EnsureRecord(ctx, r); !errors.Is(err, boom) { |
| 67 | t.Fatalf("first EnsureRecord = %v, want boom", err) |
| 68 | } |
| 69 | if len(w.Records) != 0 { |
| 70 | t.Fatal("a failed call must leave state untouched") |
| 71 | } |
| 72 | if err := w.EnsureRecord(ctx, r); err != nil { |
| 73 | t.Fatalf("second EnsureRecord = %v, want nil", err) |
| 74 | } |
| 75 | } |
| 76 | |