-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathalidns_test.go
More file actions
107 lines (94 loc) · 2.65 KB
/
alidns_test.go
File metadata and controls
107 lines (94 loc) · 2.65 KB
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
// SPDX-License-Identifier: MIT
package main
import (
"crypto/rand"
"encoding/hex"
"io"
"os"
"testing"
alidns "github.com/alibabacloud-go/alidns-20150109/v4/client"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
"github.com/alibabacloud-go/tea/tea"
"github.com/stretchr/testify/assert"
)
func newTestAliDNS(t *testing.T) *AliDNS {
accessKeyId := os.Getenv("WEBHOOK_ACCESS_KEY_ID")
accessKeySecret := os.Getenv("WEBHOOK_ACCESS_KEY_SECRET")
if len(accessKeyId) == 0 || len(accessKeySecret) == 0 {
t.Skipf("no accessKeyId or accessKeySecret set")
}
cli, err := alidns.NewClient(&openapi.Config{
Endpoint: tea.String("dns.aliyuncs.com"),
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret),
})
if assert.NoError(t, err) {
return &AliDNS{cli: cli}
}
return nil
}
func randomDNSRecord(t *testing.T) (string, string) {
domainName := os.Getenv("WEBHOOK_DOMAIN_NAME")
if len(domainName) == 0 {
t.Skipf("no domain name set")
}
subDomain := make([]byte, 4)
if _, err := io.ReadFull(rand.Reader, subDomain); assert.NoError(t, err) {
return hex.EncodeToString(subDomain) + "." + domainName + ".", domainName + "."
}
return "", ""
}
func TestAliDNS_AddRecord(t *testing.T) {
if c := newTestAliDNS(t); c != nil {
t.Run("not exists", func(t *testing.T) {
if fqdn, zone := randomDNSRecord(t); len(fqdn) != 0 {
err := c.AddRecord(fqdn, zone, "foobar")
if assert.NoError(t, err) {
_ = c.DeleteRecord(fqdn, zone)
}
}
})
t.Run("same value", func(t *testing.T) {
if fqdn, zone := randomDNSRecord(t); len(fqdn) != 0 {
err := c.AddRecord(fqdn, zone, "foobar")
if assert.NoError(t, err) {
// add record again
err = c.AddRecord(fqdn, zone, "foobar")
if assert.NoError(t, err) {
_ = c.DeleteRecord(fqdn, zone)
}
}
}
})
t.Run("updated", func(t *testing.T) {
if fqdn, zone := randomDNSRecord(t); len(fqdn) != 0 {
err := c.AddRecord(fqdn, zone, "foobar")
if assert.NoError(t, err) {
// add record again
err = c.AddRecord(fqdn, zone, "updated")
if assert.NoError(t, err) {
_ = c.DeleteRecord(fqdn, zone)
}
}
}
})
}
}
func TestAliDNS_DeleteRecord(t *testing.T) {
if c := newTestAliDNS(t); c != nil {
t.Run("not exists", func(t *testing.T) {
if fqdn, zone := randomDNSRecord(t); len(fqdn) != 0 {
err := c.DeleteRecord(fqdn, zone)
assert.NoError(t, err)
}
})
t.Run("exists", func(t *testing.T) {
if fqdn, zone := randomDNSRecord(t); len(fqdn) != 0 {
err := c.AddRecord(fqdn, zone, "foobar")
if assert.NoError(t, err) {
_ = c.DeleteRecord(fqdn, zone)
}
}
})
}
}