2018-04-03 14:22:13 +00:00
|
|
|
package fastdns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-24 19:07:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-04-03 14:22:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
fastdnsLiveTest bool
|
|
|
|
host string
|
|
|
|
clientToken string
|
|
|
|
clientSecret string
|
|
|
|
accessToken string
|
|
|
|
testDomain string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
host = os.Getenv("AKAMAI_HOST")
|
|
|
|
clientToken = os.Getenv("AKAMAI_CLIENT_TOKEN")
|
|
|
|
clientSecret = os.Getenv("AKAMAI_CLIENT_SECRET")
|
|
|
|
accessToken = os.Getenv("AKAMAI_ACCESS_TOKEN")
|
|
|
|
testDomain = os.Getenv("AKAMAI_TEST_DOMAIN")
|
|
|
|
|
|
|
|
if len(host) > 0 && len(clientToken) > 0 && len(clientSecret) > 0 && len(accessToken) > 0 {
|
|
|
|
fastdnsLiveTest = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
func restoreEnv() {
|
2018-04-03 14:22:13 +00:00
|
|
|
os.Setenv("AKAMAI_HOST", host)
|
|
|
|
os.Setenv("AKAMAI_CLIENT_TOKEN", clientToken)
|
|
|
|
os.Setenv("AKAMAI_CLIENT_SECRET", clientSecret)
|
|
|
|
os.Setenv("AKAMAI_ACCESS_TOKEN", accessToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderValid(t *testing.T) {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
2018-04-03 14:22:13 +00:00
|
|
|
os.Setenv("AKAMAI_HOST", "")
|
|
|
|
os.Setenv("AKAMAI_CLIENT_TOKEN", "")
|
|
|
|
os.Setenv("AKAMAI_CLIENT_SECRET", "")
|
|
|
|
os.Setenv("AKAMAI_ACCESS_TOKEN", "")
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Host = "somehost"
|
|
|
|
config.ClientToken = "someclienttoken"
|
|
|
|
config.ClientSecret = "someclientsecret"
|
|
|
|
config.AccessToken = "someaccesstoken"
|
|
|
|
|
|
|
|
_, err := NewDNSProviderConfig(config)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-03 14:22:13 +00:00
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-04-03 14:22:13 +00:00
|
|
|
func TestNewDNSProviderValidEnv(t *testing.T) {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
2018-04-03 14:22:13 +00:00
|
|
|
os.Setenv("AKAMAI_HOST", "somehost")
|
|
|
|
os.Setenv("AKAMAI_CLIENT_TOKEN", "someclienttoken")
|
|
|
|
os.Setenv("AKAMAI_CLIENT_SECRET", "someclientsecret")
|
|
|
|
os.Setenv("AKAMAI_ACCESS_TOKEN", "someaccesstoken")
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-04-03 14:22:13 +00:00
|
|
|
_, err := NewDNSProvider()
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-03 14:22:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderMissingCredErr(t *testing.T) {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
2018-04-03 14:22:13 +00:00
|
|
|
os.Setenv("AKAMAI_HOST", "")
|
|
|
|
os.Setenv("AKAMAI_CLIENT_TOKEN", "")
|
|
|
|
os.Setenv("AKAMAI_CLIENT_SECRET", "")
|
|
|
|
os.Setenv("AKAMAI_ACCESS_TOKEN", "")
|
|
|
|
|
|
|
|
_, err := NewDNSProvider()
|
2018-09-15 17:07:24 +00:00
|
|
|
assert.EqualError(t, err, "fastdns: some credentials information are missing: AKAMAI_HOST,AKAMAI_CLIENT_TOKEN,AKAMAI_CLIENT_SECRET,AKAMAI_ACCESS_TOKEN")
|
2018-04-03 14:22:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveFastdnsPresent(t *testing.T) {
|
|
|
|
if !fastdnsLiveTest {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Host = host
|
|
|
|
config.ClientToken = clientToken
|
|
|
|
config.ClientSecret = clientSecret
|
|
|
|
config.AccessToken = accessToken
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-03 14:22:13 +00:00
|
|
|
|
|
|
|
err = provider.Present(testDomain, "", "123d==")
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-03 14:22:13 +00:00
|
|
|
|
|
|
|
// Present Twice to handle create / update
|
|
|
|
err = provider.Present(testDomain, "", "123d==")
|
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
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveFastdnsCleanUp(t *testing.T) {
|
|
|
|
if !fastdnsLiveTest {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Host = host
|
|
|
|
config.ClientToken = clientToken
|
|
|
|
config.ClientSecret = clientSecret
|
|
|
|
config.AccessToken = accessToken
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-03 14:22:13 +00:00
|
|
|
|
|
|
|
err = provider.CleanUp(testDomain, "", "123d==")
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-03 14:22:13 +00:00
|
|
|
}
|