-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider_test.go
175 lines (155 loc) · 4.02 KB
/
provider_test.go
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package selectel_test
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/joho/godotenv"
"github.com/libdns/libdns"
"github.com/libdns/selectel"
"github.com/stretchr/testify/assert"
)
var provider selectel.Provider
var zone string
var ctx context.Context
var addedRecords []libdns.Record
var sourceRecords []libdns.Record
// load init data from .env
func setup() {
err := godotenv.Load(".env")
if err != nil {
panic("Error loading .env file")
}
provider = selectel.Provider{
User: os.Getenv("SELECTEL_USER"),
Password: os.Getenv("SELECTEL_PASSWORD"),
AccountId: os.Getenv("SELECTEL_ACCOUNT_ID"),
ProjectName: os.Getenv("SELECTEL_PROJECT_NAME"),
ZonesCache: make(map[string]string),
}
zone = os.Getenv("SELECTEL_ZONE")
ctx = context.Background()
sourceRecords = []libdns.Record{
{ // 0
Type: "A",
Name: fmt.Sprintf("test1.%s.", os.Getenv("SELECTEL_ZONE")),
Value: "1.2.3.1",
TTL: 61 * time.Second,
},
{ // 1
Type: "A",
Name: fmt.Sprintf("test2.%s.", os.Getenv("SELECTEL_ZONE")),
Value: "1.2.3.2",
TTL: 61 * time.Second,
},
{ // 2
Type: "A",
Name: "test3",
Value: "1.2.3.3",
TTL: 61 * time.Second,
},
{ // 3
Type: "TXT",
Name: "test1",
Value: "test1 txt",
TTL: 61 * time.Second,
},
{ // 4
Type: "TXT",
Name: fmt.Sprintf("test2.%s.", os.Getenv("SELECTEL_ZONE")),
Value: "test2 txt",
TTL: 61 * time.Second,
},
{ // 5
Type: "TXT",
Name: "test3",
Value: "test3 txt",
TTL: 61 * time.Second,
},
}
}
// testing GetRecord
func TestProvider_GetRecords(t *testing.T) {
setup()
// delete sourceRec if exists
provider.DeleteRecords(ctx, zone, sourceRecords)
records, err := provider.GetRecords(ctx, zone)
assert.NoError(t, err)
assert.NotNil(t, records)
assert.True(t, len(records) > 0, "No records found")
t.Logf("GetRecords test passed. Records found: %d", len(records))
}
// testing append record
func TestProvider_AppendRecords(t *testing.T) {
setup()
// entries to add
newRecords := []libdns.Record{
sourceRecords[0],
sourceRecords[1],
sourceRecords[3],
sourceRecords[4],
}
records, err := provider.AppendRecords(ctx, zone, newRecords)
addedRecords = records
assert.NoError(t, err)
assert.NotNil(t, records)
assert.Equal(t, 4, len(records))
assert.Equal(t, "A", records[0].Type)
assert.Equal(t, "TXT", records[2].Type)
t.Logf("AppendRecords test passed. Append count: %d", len(records))
}
// testing set
func TestProvider_SetRecords(t *testing.T) {
setup()
second := addedRecords[1]
second.TTL = 62 * time.Second
fourth := addedRecords[3]
fourth.Value = "test 1 txt with additional line\nsecondline"
fifth := sourceRecords[4]
fifth.Value = "test 2 txt changed"
// entries to set
setRecords := []libdns.Record{
{ // record from Append without id
Type: "A",
Name: "test1.", // <---- without zone, but with .
Value: "1.2.3.1",
TTL: 62 * time.Second, // <---- changed
},
second, // record from Append, but new ttl = 62
sourceRecords[2], // new record
fourth, // changed value. 2 lines
fifth,
sourceRecords[5],
}
records, err := provider.SetRecords(ctx, zone, setRecords)
addedRecords = records
assert.NoError(t, err)
assert.NotNil(t, records)
assert.Equal(t, 6, len(records))
assert.Equal(t, "A", records[2].Type)
assert.Equal(t, "1.2.3.2", records[1].Value)
assert.Equal(t, 62, int(records[0].TTL.Seconds()))
t.Logf("SetRecords test passed. Set count: %d", len(records))
}
// testing delete
func TestProvider_DeleteRecords(t *testing.T) {
setup()
// entries to delete
delRecords := []libdns.Record{
addedRecords[0],
sourceRecords[1],
addedRecords[2],
sourceRecords[3],
addedRecords[4],
sourceRecords[5],
}
records, err := provider.DeleteRecords(ctx, zone, delRecords)
assert.NoError(t, err)
assert.NotNil(t, records)
assert.Equal(t, 6, len(records))
assert.Equal(t, "A", records[0].Type)
assert.Equal(t, "1.2.3.2", records[1].Value)
assert.Equal(t, 61, int(records[2].TTL.Seconds()))
t.Logf("DeleteRecords test passed. Delete count: %d", len(records))
}