File: domain_records_test.go

package info (click to toggle)
golang-github-linode-linodego 1.55.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,112 kB
  • sloc: makefile: 96; sh: 52; python: 24
file content (143 lines) | stat: -rw-r--r-- 4,092 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package integration

import (
	"context"
	"encoding/json"
	"testing"

	"github.com/linode/linodego"
)

var testDomainRecordCreateOpts = linodego.DomainRecordCreateOptions{
	Target: "127.0.0.1",
	Type:   linodego.RecordTypeA,
	Name:   "a",
}

func TestDomainRecord_Create_smoke(t *testing.T) {
	_, _, record, teardown, err := setupDomainRecord(t, "fixtures/TestDomainRecord_Create")
	defer teardown()

	if err != nil {
		t.Errorf("Error creating domain record, got error %v", err)
	}

	assertDateSet(t, record.Created)
	assertDateSet(t, record.Updated)

	expected := testDomainRecordCreateOpts

	// cant compare Target, fixture IPs are sanitized
	if record.Name != expected.Name || record.Type != expected.Type {
		t.Errorf("DomainRecord did not match CreateOptions")
	}
}

func TestDomainRecord_Update(t *testing.T) {
	client, domain, record, teardown, err := setupDomainRecord(t, "fixtures/TestDomainRecord_Update")
	defer teardown()
	if err != nil {
		t.Error(err)
	}

	assertDateSet(t, record.Created)
	assertDateSet(t, record.Updated)

	updateOpts := linodego.DomainRecordUpdateOptions{
		Name: "renamed",
	}
	recordUpdated, err := client.UpdateDomainRecord(context.Background(), domain.ID, record.ID, updateOpts)
	if err != nil {
		t.Errorf("Error updating domain record, %s", err)
	}
	if recordUpdated.Name != "renamed" || record.Type != recordUpdated.Type || recordUpdated.Target != record.Target {
		t.Errorf("DomainRecord did not match UpdateOptions")
	}
}

func TestDomainRecords_List(t *testing.T) {
	client, domain, record, teardown, err := setupDomainRecord(t, "fixtures/TestDomainRecords_List")
	defer teardown()
	if err != nil {
		t.Error(err)
	}

	filter, err := json.Marshal(map[string]interface{}{
		"name": record.Name,
	})
	if err != nil {
		t.Error(err)
	}

	listOpts := linodego.NewListOptions(0, string(filter))
	records, err := client.ListDomainRecords(context.Background(), domain.ID, listOpts)
	if err != nil {
		t.Errorf("Error listing domains records, expected array, got error %v", err)
	}
	if len(records) != 1 {
		t.Errorf("Expected ListDomainRecords to match one result")
	}
}

func TestDomainRecords_ListMultiplePages(t *testing.T) {
	client, domain, record, teardown, err := setupDomainRecord(t, "fixtures/TestDomainRecords_ListMultiplePages")
	defer teardown()
	if err != nil {
		t.Error(err)
	}

	filter, err := json.Marshal(map[string]interface{}{
		"name": record.Name,
	})
	if err != nil {
		t.Error(err)
	}
	listOpts := linodego.NewListOptions(0, string(filter))
	records, err := client.ListDomainRecords(context.Background(), domain.ID, listOpts)
	if err != nil {
		t.Errorf("Error listing domains records, expected array, got error %v", err)
	}
	if len(records) != 1 {
		t.Errorf("Expected ListDomainRecords to match one result")
	}
}

func TestDomainRecord_Get(t *testing.T) {
	client, domain, record, teardown, err := setupDomainRecord(t, "fixtures/TestDomainRecord_Get")
	defer teardown()
	if err != nil {
		t.Error(err)
	}

	recordGot, err := client.GetDomainRecord(context.Background(), domain.ID, record.ID)
	if recordGot.Name != record.Name {
		t.Errorf("GetDomainRecord did not get the expected record")
	}
	if err != nil {
		t.Errorf("Error getting domain %d, got error %v", domain.ID, err)
	}
}

func setupDomainRecord(t *testing.T, fixturesYaml string) (*linodego.Client, *linodego.Domain, *linodego.DomainRecord, func(), error) {
	t.Helper()
	var fixtureTeardown func()
	client, domain, fixtureTeardown, err := setupDomain(t, fixturesYaml)
	if err != nil {
		t.Errorf("Error creating domain, got error %v", err)
	}

	createOpts := testDomainRecordCreateOpts
	record, err := client.CreateDomainRecord(context.Background(), domain.ID, createOpts)
	if err != nil {
		t.Errorf("Error creating domain record, got error %v", err)
	}

	teardown := func() {
		// delete the DomainRecord to exercise the code
		if err := client.DeleteDomainRecord(context.Background(), domain.ID, record.ID); err != nil {
			t.Errorf("Expected to delete a domain record, but got %v", err)
		}
		fixtureTeardown()
	}
	return client, domain, record, teardown, err
}