2018-04-03 14:22:13 +00:00
|
|
|
package fastdns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-24 19:07:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-10-16 15:52:57 +00:00
|
|
|
"github.com/xenolf/lego/platform/tester"
|
2018-04-03 14:22:13 +00:00
|
|
|
)
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
var envTest = tester.NewEnvTest(
|
|
|
|
"AKAMAI_HOST",
|
|
|
|
"AKAMAI_CLIENT_TOKEN",
|
|
|
|
"AKAMAI_CLIENT_SECRET",
|
|
|
|
"AKAMAI_ACCESS_TOKEN").
|
|
|
|
WithDomain("AKAMAI_TEST_DOMAIN")
|
2018-04-03 14:22:13 +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{
|
|
|
|
"AKAMAI_HOST": "A",
|
|
|
|
"AKAMAI_CLIENT_TOKEN": "B",
|
|
|
|
"AKAMAI_CLIENT_SECRET": "C",
|
|
|
|
"AKAMAI_ACCESS_TOKEN": "D",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"AKAMAI_HOST": "",
|
|
|
|
"AKAMAI_CLIENT_TOKEN": "",
|
|
|
|
"AKAMAI_CLIENT_SECRET": "",
|
|
|
|
"AKAMAI_ACCESS_TOKEN": "",
|
|
|
|
},
|
|
|
|
expected: "fastdns: some credentials information are missing: AKAMAI_HOST,AKAMAI_CLIENT_TOKEN,AKAMAI_CLIENT_SECRET,AKAMAI_ACCESS_TOKEN",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing host",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"AKAMAI_HOST": "",
|
|
|
|
"AKAMAI_CLIENT_TOKEN": "B",
|
|
|
|
"AKAMAI_CLIENT_SECRET": "C",
|
|
|
|
"AKAMAI_ACCESS_TOKEN": "D",
|
|
|
|
},
|
|
|
|
expected: "fastdns: some credentials information are missing: AKAMAI_HOST",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing client token",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"AKAMAI_HOST": "A",
|
|
|
|
"AKAMAI_CLIENT_TOKEN": "",
|
|
|
|
"AKAMAI_CLIENT_SECRET": "C",
|
|
|
|
"AKAMAI_ACCESS_TOKEN": "D",
|
|
|
|
},
|
|
|
|
expected: "fastdns: some credentials information are missing: AKAMAI_CLIENT_TOKEN",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing client secret",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"AKAMAI_HOST": "A",
|
|
|
|
"AKAMAI_CLIENT_TOKEN": "B",
|
|
|
|
"AKAMAI_CLIENT_SECRET": "",
|
|
|
|
"AKAMAI_ACCESS_TOKEN": "D",
|
|
|
|
},
|
|
|
|
expected: "fastdns: some credentials information are missing: AKAMAI_CLIENT_SECRET",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing access token",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"AKAMAI_HOST": "A",
|
|
|
|
"AKAMAI_CLIENT_TOKEN": "B",
|
|
|
|
"AKAMAI_CLIENT_SECRET": "C",
|
|
|
|
"AKAMAI_ACCESS_TOKEN": "",
|
|
|
|
},
|
|
|
|
expected: "fastdns: some credentials information are missing: AKAMAI_ACCESS_TOKEN",
|
|
|
|
},
|
|
|
|
}
|
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-04-03 14:22:13 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
p, err := NewDNSProvider()
|
2018-04-03 14:22:13 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
if len(test.expected) == 0 {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-04-03 14:22:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
host string
|
|
|
|
clientToken string
|
|
|
|
clientSecret string
|
|
|
|
accessToken string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
host: "A",
|
|
|
|
clientToken: "B",
|
|
|
|
clientSecret: "C",
|
|
|
|
accessToken: "D",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
expected: "fastdns: credentials are missing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing host",
|
|
|
|
host: "",
|
|
|
|
clientToken: "B",
|
|
|
|
clientSecret: "C",
|
|
|
|
accessToken: "D",
|
|
|
|
expected: "fastdns: credentials are missing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing client token",
|
|
|
|
host: "A",
|
|
|
|
clientToken: "",
|
|
|
|
clientSecret: "C",
|
|
|
|
accessToken: "D",
|
|
|
|
expected: "fastdns: credentials are missing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing client secret",
|
|
|
|
host: "A",
|
|
|
|
clientToken: "B",
|
|
|
|
clientSecret: "",
|
|
|
|
accessToken: "B",
|
|
|
|
expected: "fastdns: credentials are missing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing access token",
|
|
|
|
host: "A",
|
|
|
|
clientToken: "B",
|
|
|
|
clientSecret: "C",
|
|
|
|
accessToken: "",
|
|
|
|
expected: "fastdns: credentials are missing",
|
|
|
|
},
|
2018-04-03 14:22:13 +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.ClientToken = test.clientToken
|
|
|
|
config.ClientSecret = test.clientSecret
|
|
|
|
config.Host = test.host
|
|
|
|
config.AccessToken = test.accessToken
|
2018-04-03 14:22:13 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
p, err := NewDNSProviderConfig(config)
|
2018-04-03 14:22:13 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
if len(test.expected) == 0 {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-04-03 14:22:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
func TestDNSProvider_findZoneAndRecordName(t *testing.T) {
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Host = "somehost"
|
|
|
|
config.ClientToken = "someclienttoken"
|
|
|
|
config.ClientSecret = "someclientsecret"
|
|
|
|
config.AccessToken = "someaccesstoken"
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-03 14:22:13 +00:00
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
type expected struct {
|
|
|
|
zone string
|
|
|
|
recordName string
|
|
|
|
}
|
2018-04-03 14:22:13 +00:00
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
fqdn string
|
|
|
|
domain string
|
|
|
|
expected expected
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "Extract root record name",
|
|
|
|
fqdn: "_acme-challenge.bar.com.",
|
|
|
|
domain: "bar.com",
|
|
|
|
expected: expected{
|
|
|
|
zone: "bar.com",
|
|
|
|
recordName: "_acme-challenge",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Extract sub record name",
|
|
|
|
fqdn: "_acme-challenge.foo.bar.com.",
|
|
|
|
domain: "foo.bar.com",
|
|
|
|
expected: expected{
|
|
|
|
zone: "bar.com",
|
|
|
|
recordName: "_acme-challenge.foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-04-03 14:22:13 +00:00
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
zone, recordName, err := provider.findZoneAndRecordName(test.fqdn, test.domain)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, test.expected.zone, zone)
|
|
|
|
assert.Equal(t, test.expected.recordName, recordName)
|
|
|
|
})
|
|
|
|
}
|
2018-04-03 14:22:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestLivePresent(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2018-10-12 17:29:18 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
envTest.RestoreEnv()
|
2018-10-12 17:29:18 +00:00
|
|
|
provider, err := NewDNSProvider()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Present Twice to handle create / update
|
2018-10-16 15:52:57 +00:00
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveCleanUp(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2018-04-03 14:22:13 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
envTest.RestoreEnv()
|
|
|
|
provider, err := NewDNSProvider()
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-03 14:22:13 +00:00
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-03 14:22:13 +00:00
|
|
|
}
|