| 1 | package hetzner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/hetznercloud/hcloud-go/v2/hcloud" |
| 8 | ) |
| 9 | |
| 10 | func TestChallengeProviderRequiresAPIToken(t *testing.T) { |
| 11 | t.Parallel() |
| 12 | |
| 13 | provider := New("") |
| 14 | challengeProvider, err := provider.ChallengeProvider(context.Background()) |
| 15 | if challengeProvider != nil { |
| 16 | t.Fatalf("ChallengeProvider() provider = %T, want nil", challengeProvider) |
| 17 | } |
| 18 | if err == nil || err.Error() != "hetzner api token is required" { |
| 19 | t.Fatalf("ChallengeProvider() error = %v, want local api token error", err) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func TestRelativeRecordName(t *testing.T) { |
| 24 | t.Parallel() |
| 25 | |
| 26 | zone := &hcloud.Zone{Name: "example.com"} |
| 27 | testCases := []struct { |
| 28 | name string |
| 29 | fqdn string |
| 30 | want string |
| 31 | }{ |
| 32 | {name: "apex", fqdn: "example.com", want: "@"}, |
| 33 | {name: "subdomain", fqdn: "portal.example.com", want: "portal"}, |
| 34 | {name: "wildcard", fqdn: "*.example.com", want: "*"}, |
| 35 | {name: "nested", fqdn: "_ens.portal.example.com", want: "_ens.portal"}, |
| 36 | } |
| 37 | |
| 38 | for _, tc := range testCases { |
| 39 | t.Run(tc.name, func(t *testing.T) { |
| 40 | t.Parallel() |
| 41 | |
| 42 | got, err := relativeRecordName(tc.fqdn, zone) |
| 43 | if err != nil { |
| 44 | t.Fatalf("relativeRecordName() error = %v", err) |
| 45 | } |
| 46 | if got != tc.want { |
| 47 | t.Fatalf("relativeRecordName() = %q, want %q", got, tc.want) |
| 48 | } |
| 49 | }) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestTXTContent(t *testing.T) { |
| 54 | t.Parallel() |
| 55 | |
| 56 | got := txtContent(`"ENS1 0x238A8F792dFA6033814B18618aD4100654aeef01" " 0xabc"`) |
| 57 | if got != "ENS1 0x238A8F792dFA6033814B18618aD4100654aeef01 0xabc" { |
| 58 | t.Fatalf("txtContent() = %q", got) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestSameRecordsIgnoresOrder(t *testing.T) { |
| 63 | t.Parallel() |
| 64 | |
| 65 | a := []hcloud.ZoneRRSetRecord{{Value: "b"}, {Value: "a", Comment: "one"}} |
| 66 | b := []hcloud.ZoneRRSetRecord{{Value: "a", Comment: "one"}, {Value: "b"}} |
| 67 | if !sameRecords(a, b) { |
| 68 | t.Fatal("sameRecords() = false, want true") |
| 69 | } |
| 70 | } |