185 lines
4.8 KiB
Go
185 lines
4.8 KiB
Go
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||
|
||
package dnsclients_test
|
||
|
||
import (
|
||
"encoding/json"
|
||
"testing"
|
||
|
||
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients"
|
||
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
|
||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||
"github.com/iwind/TeaGo/dbs"
|
||
"github.com/iwind/TeaGo/logs"
|
||
"github.com/iwind/TeaGo/maps"
|
||
)
|
||
|
||
const GnameTestDomain = "xyyp.xyz" // 请修改为您的测试域名(使用您账户下实际拥有的域名)
|
||
|
||
func TestGnameProvider_GetDomains(t *testing.T) {
|
||
provider, err := testGnameProvider()
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
domains, err := provider.GetDomains()
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
t.Log("domains count:", len(domains))
|
||
if len(domains) > 0 {
|
||
t.Log("first domain:", domains[0])
|
||
}
|
||
logs.PrintAsJSON(domains, t)
|
||
}
|
||
|
||
func TestGnameProvider_GetRecords(t *testing.T) {
|
||
provider, err := testGnameProvider()
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
records, err := provider.GetRecords(GnameTestDomain)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
t.Log("records count:", len(records))
|
||
if len(records) > 0 {
|
||
t.Log("first record:", records[0].Name, records[0].Type, records[0].Value)
|
||
}
|
||
logs.PrintAsJSON(records, t)
|
||
}
|
||
|
||
func TestGnameProvider_GetRoutes(t *testing.T) {
|
||
provider, err := testGnameProvider()
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
routes, err := provider.GetRoutes(GnameTestDomain)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
t.Log("routes count:", len(routes))
|
||
logs.PrintAsJSON(routes, t)
|
||
}
|
||
|
||
func TestGnameProvider_QueryRecord(t *testing.T) {
|
||
provider, err := testGnameProvider()
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
// 测试查询不同类型的记录
|
||
testCases := []struct {
|
||
name string
|
||
recordType dnstypes.RecordType
|
||
}{
|
||
{"www", dnstypes.RecordTypeA},
|
||
{"@", dnstypes.RecordTypeA},
|
||
{"", dnstypes.RecordTypeA},
|
||
{"www", dnstypes.RecordTypeCNAME},
|
||
{"mail", dnstypes.RecordTypeA},
|
||
}
|
||
|
||
for _, tc := range testCases {
|
||
t.Logf("=== Query: %s %s ===", tc.name, tc.recordType)
|
||
record, err := provider.QueryRecord(GnameTestDomain, tc.name, tc.recordType)
|
||
if err != nil {
|
||
t.Logf("Error querying %s %s: %v", tc.name, tc.recordType, err)
|
||
continue
|
||
}
|
||
if record == nil {
|
||
t.Logf("Record not found: %s %s", tc.name, tc.recordType)
|
||
} else {
|
||
logs.PrintAsJSON(record, t)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestGnameProvider_QueryRecords(t *testing.T) {
|
||
provider, err := testGnameProvider()
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
// 测试查询多个记录
|
||
testCases := []struct {
|
||
name string
|
||
recordType dnstypes.RecordType
|
||
}{
|
||
{"www", dnstypes.RecordTypeA},
|
||
{"@", dnstypes.RecordTypeA},
|
||
{"", dnstypes.RecordTypeA},
|
||
{"www", dnstypes.RecordTypeCNAME},
|
||
}
|
||
|
||
for _, tc := range testCases {
|
||
t.Logf("=== QueryRecords: %s %s ===", tc.name, tc.recordType)
|
||
records, err := provider.QueryRecords(GnameTestDomain, tc.name, tc.recordType)
|
||
if err != nil {
|
||
t.Logf("Error querying %s %s: %v", tc.name, tc.recordType, err)
|
||
continue
|
||
}
|
||
if len(records) == 0 {
|
||
t.Logf("No records found: %s %s", tc.name, tc.recordType)
|
||
} else {
|
||
t.Logf("Found %d records", len(records))
|
||
logs.PrintAsJSON(records, t)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestGnameProvider_DefaultRoute(t *testing.T) {
|
||
provider, err := testGnameProvider()
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
defaultRoute := provider.DefaultRoute()
|
||
t.Log("default route:", defaultRoute)
|
||
if len(defaultRoute) == 0 {
|
||
t.Error("default route should not be empty")
|
||
}
|
||
}
|
||
|
||
// testGnameProvider 创建测试用的 Gname Provider
|
||
// 方式1: 从数据库读取配置(如果数据库中有配置)
|
||
func testGnameProvider() (dnsclients.ProviderInterface, error) {
|
||
// 尝试从数据库读取配置
|
||
db, err := dbs.Default()
|
||
if err == nil {
|
||
one, err := db.FindOne("SELECT * FROM edgeDNSProviders WHERE type='gname' ORDER BY id DESC")
|
||
if err == nil && one != nil {
|
||
apiParams := maps.Map{}
|
||
err = json.Unmarshal([]byte(one.GetString("apiParams")), &apiParams)
|
||
if err == nil {
|
||
provider := &dnsclients.GnameProvider{}
|
||
err = provider.Auth(apiParams)
|
||
if err == nil {
|
||
return provider, nil
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 方式2: 使用硬编码配置(请修改为您的实际 APPID 和 Secret)
|
||
provider := &dnsclients.GnameProvider{}
|
||
|
||
// 请在此处填写您的 Gname API 凭证
|
||
// 可以使用 appid/secret 或 apiKey/apiSecret(兼容旧参数名)
|
||
authParams := maps.Map{
|
||
"appid": "1519156931639cd973f", // 请修改为您的 APPID
|
||
"secret": "5uBTJCwYFNQBjbYsc8ak", // 请修改为您的 Secret
|
||
// 或者使用旧参数名(二选一):
|
||
// "apiKey": "your-api-key-here",
|
||
// "apiSecret": "your-api-secret-here",
|
||
}
|
||
|
||
err = provider.Auth(authParams)
|
||
if err != nil {
|
||
return nil, errors.New("please set your Gname API credentials (appid/secret or apiKey/apiSecret) in testGnameProvider() function or in database")
|
||
}
|
||
return provider, nil
|
||
}
|