2019-03-11 16:56:48 +00:00
|
|
|
package vultr
|
2016-03-20 23:23:37 +00:00
|
|
|
|
|
|
|
import (
|
2020-11-28 13:53:02 +00:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strconv"
|
2016-03-20 23:23:37 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2020-11-28 13:53:02 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-24 19:07:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-11-28 13:53:02 +00:00
|
|
|
"github.com/vultr/govultr/v2"
|
2016-03-20 23:23:37 +00:00
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
const envDomain = envNamespace + "TEST_DOMAIN"
|
|
|
|
|
|
|
|
var envTest = tester.NewEnvTest(EnvAPIKey).
|
|
|
|
WithDomain(envDomain)
|
2016-03-20 23:23:37 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIKey: "123",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing api key",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIKey: "",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
expected: "vultr: some credentials information are missing: VULTR_API_KEY",
|
|
|
|
},
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
|
|
|
envTest.Apply(test.envVars)
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expected == "" {
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
require.NotNil(t, p.client)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-03-20 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
apiKey string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
apiKey: "123",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
expected: "vultr: credentials missing",
|
|
|
|
},
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = test.apiKey
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expected == "" {
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
require.NotNil(t, p.client)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-03-20 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
2020-11-28 13:53:02 +00:00
|
|
|
func TestDNSProvider_getHostedZone(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
domain string
|
|
|
|
expected string
|
|
|
|
expectedPageCount int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "exact match, in latest page",
|
|
|
|
domain: "test.my.example.com",
|
|
|
|
expected: "test.my.example.com",
|
|
|
|
expectedPageCount: 5,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "exact match, in the middle",
|
|
|
|
domain: "my.example.com",
|
|
|
|
expected: "my.example.com",
|
|
|
|
expectedPageCount: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "exact match, first page",
|
|
|
|
domain: "example.com",
|
|
|
|
expected: "example.com",
|
|
|
|
expectedPageCount: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "match on apex",
|
|
|
|
domain: "test.example.org",
|
|
|
|
expected: "example.org",
|
|
|
|
expectedPageCount: 5,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "match on parent",
|
|
|
|
domain: "test.my.example.net",
|
|
|
|
expected: "my.example.net",
|
|
|
|
expectedPageCount: 5,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
domains := []govultr.Domain{{Domain: "example.com"}, {Domain: "example.org"}, {Domain: "example.net"}}
|
|
|
|
|
|
|
|
for i := 0; i < 50; i++ {
|
|
|
|
domains = append(domains, govultr.Domain{Domain: fmt.Sprintf("my%02d.example.com", i)})
|
|
|
|
}
|
|
|
|
|
|
|
|
domains = append(domains, govultr.Domain{Domain: "my.example.com"}, govultr.Domain{Domain: "my.example.net"})
|
|
|
|
|
|
|
|
for i := 50; i < 100; i++ {
|
|
|
|
domains = append(domains, govultr.Domain{Domain: fmt.Sprintf("my%02d.example.com", i)})
|
|
|
|
}
|
|
|
|
|
|
|
|
domains = append(domains, govultr.Domain{Domain: "test.my.example.com"})
|
|
|
|
|
|
|
|
type domainsBase struct {
|
|
|
|
Domains []govultr.Domain `json:"domains"`
|
|
|
|
Meta *govultr.Meta `json:"meta"`
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
t.Cleanup(server.Close)
|
|
|
|
|
|
|
|
client := govultr.NewClient(nil)
|
|
|
|
err := client.SetBaseURL(server.URL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
p := &DNSProvider{client: client}
|
|
|
|
|
|
|
|
var pageCount int
|
|
|
|
|
|
|
|
mux.HandleFunc("/v2/domains", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
pageCount++
|
|
|
|
|
|
|
|
query := req.URL.Query()
|
|
|
|
cursor, _ := strconv.Atoi(query.Get("cursor"))
|
|
|
|
perPage, _ := strconv.Atoi(query.Get("per_page"))
|
|
|
|
|
|
|
|
var next string
|
|
|
|
if len(domains)/perPage > cursor {
|
|
|
|
next = strconv.Itoa(cursor + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
start := cursor * perPage
|
|
|
|
if len(domains) < start {
|
|
|
|
start = cursor * len(domains)
|
|
|
|
}
|
|
|
|
|
|
|
|
end := (cursor + 1) * perPage
|
|
|
|
if len(domains) < end {
|
|
|
|
end = len(domains)
|
|
|
|
}
|
|
|
|
|
|
|
|
db := domainsBase{
|
|
|
|
Domains: domains[start:end],
|
|
|
|
Meta: &govultr.Meta{
|
|
|
|
Total: len(domains),
|
|
|
|
Links: &govultr.Links{Next: next},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(rw).Encode(db)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
zone, err := p.getHostedZone(context.Background(), test.domain)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, zone)
|
|
|
|
assert.Equal(t, test.expectedPageCount, pageCount)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 23:23:37 +00:00
|
|
|
func TestLivePresent(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2016-03-20 23:23:37 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
envTest.RestoreEnv()
|
2016-03-20 23:23:37 +00:00
|
|
|
provider, err := NewDNSProvider()
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-03-20 23:23:37 +00:00
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-03-20 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveCleanUp(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2016-03-20 23:23:37 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
envTest.RestoreEnv()
|
2016-03-20 23:23:37 +00:00
|
|
|
provider, err := NewDNSProvider()
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-03-20 23:23:37 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-03-20 23:23:37 +00:00
|
|
|
}
|