2016-11-07 07:37:57 +00:00
|
|
|
package exoscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
exoscaleLiveTest bool
|
|
|
|
exoscaleAPIKey string
|
|
|
|
exoscaleAPISecret string
|
|
|
|
exoscaleDomain string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
exoscaleAPISecret = os.Getenv("EXOSCALE_API_SECRET")
|
|
|
|
exoscaleAPIKey = os.Getenv("EXOSCALE_API_KEY")
|
|
|
|
exoscaleDomain = os.Getenv("EXOSCALE_DOMAIN")
|
|
|
|
if len(exoscaleAPIKey) > 0 && len(exoscaleAPISecret) > 0 && len(exoscaleDomain) > 0 {
|
|
|
|
exoscaleLiveTest = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
func restoreEnv() {
|
2016-11-07 07:37:57 +00:00
|
|
|
os.Setenv("EXOSCALE_API_KEY", exoscaleAPIKey)
|
|
|
|
os.Setenv("EXOSCALE_API_SECRET", exoscaleAPISecret)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderValid(t *testing.T) {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
2016-11-07 07:37:57 +00:00
|
|
|
os.Setenv("EXOSCALE_API_KEY", "")
|
|
|
|
os.Setenv("EXOSCALE_API_SECRET", "")
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = "example@example.com"
|
|
|
|
config.APISecret = "123"
|
|
|
|
|
|
|
|
_, err := NewDNSProviderConfig(config)
|
2016-11-07 07:37:57 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-11-07 07:37:57 +00:00
|
|
|
func TestNewDNSProviderValidEnv(t *testing.T) {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
2016-11-07 07:37:57 +00:00
|
|
|
os.Setenv("EXOSCALE_API_KEY", "example@example.com")
|
|
|
|
os.Setenv("EXOSCALE_API_SECRET", "123")
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-11-07 07:37:57 +00:00
|
|
|
_, err := NewDNSProvider()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderMissingCredErr(t *testing.T) {
|
|
|
|
os.Setenv("EXOSCALE_API_KEY", "")
|
|
|
|
os.Setenv("EXOSCALE_API_SECRET", "")
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
|
|
|
|
2016-11-07 07:37:57 +00:00
|
|
|
_, err := NewDNSProvider()
|
2018-09-15 17:07:24 +00:00
|
|
|
assert.EqualError(t, err, "exoscale: some credentials information are missing: EXOSCALE_API_KEY,EXOSCALE_API_SECRET")
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtractRootRecordName(t *testing.T) {
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = "example@example.com"
|
|
|
|
config.APISecret = "123"
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2016-11-07 07:37:57 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
zone, recordName, err := provider.FindZoneAndRecordName("_acme-challenge.bar.com.", "bar.com")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "bar.com", zone)
|
|
|
|
assert.Equal(t, "_acme-challenge", recordName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtractSubRecordName(t *testing.T) {
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = "example@example.com"
|
|
|
|
config.APISecret = "123"
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2016-11-07 07:37:57 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
zone, recordName, err := provider.FindZoneAndRecordName("_acme-challenge.foo.bar.com.", "foo.bar.com")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "bar.com", zone)
|
|
|
|
assert.Equal(t, "_acme-challenge.foo", recordName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveExoscalePresent(t *testing.T) {
|
|
|
|
if !exoscaleLiveTest {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = exoscaleAPIKey
|
|
|
|
config.APISecret = exoscaleAPISecret
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2016-11-07 07:37:57 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = provider.Present(exoscaleDomain, "", "123d==")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Present Twice to handle create / update
|
|
|
|
err = provider.Present(exoscaleDomain, "", "123d==")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveExoscaleCleanUp(t *testing.T) {
|
|
|
|
if !exoscaleLiveTest {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = exoscaleAPIKey
|
|
|
|
config.APISecret = exoscaleAPISecret
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2016-11-07 07:37:57 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = provider.CleanUp(exoscaleDomain, "", "123d==")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|