2016-11-02 14:33:57 +00:00
package azure
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
azureLiveTest bool
azureClientID string
azureClientSecret string
azureSubscriptionID string
azureTenantID string
azureResourceGroup string
azureDomain string
)
func init ( ) {
azureClientID = os . Getenv ( "AZURE_CLIENT_ID" )
azureClientSecret = os . Getenv ( "AZURE_CLIENT_SECRET" )
azureSubscriptionID = os . Getenv ( "AZURE_SUBSCRIPTION_ID" )
azureTenantID = os . Getenv ( "AZURE_TENANT_ID" )
azureResourceGroup = os . Getenv ( "AZURE_RESOURCE_GROUP" )
azureDomain = os . Getenv ( "AZURE_DOMAIN" )
if len ( azureClientID ) > 0 && len ( azureClientSecret ) > 0 {
azureLiveTest = true
}
}
2018-06-11 15:32:50 +00:00
func restoreEnv ( ) {
2016-11-02 14:33:57 +00:00
os . Setenv ( "AZURE_CLIENT_ID" , azureClientID )
os . Setenv ( "AZURE_SUBSCRIPTION_ID" , azureSubscriptionID )
}
func TestNewDNSProviderValid ( t * testing . T ) {
if ! azureLiveTest {
t . Skip ( "skipping live test (requires credentials)" )
}
2018-06-11 15:32:50 +00:00
defer restoreEnv ( )
2016-11-02 14:33:57 +00:00
os . Setenv ( "AZURE_CLIENT_ID" , "" )
2018-06-11 15:32:50 +00:00
2016-11-02 14:33:57 +00:00
_ , err := NewDNSProviderCredentials ( azureClientID , azureClientSecret , azureSubscriptionID , azureTenantID , azureResourceGroup )
assert . NoError ( t , err )
}
func TestNewDNSProviderValidEnv ( t * testing . T ) {
if ! azureLiveTest {
t . Skip ( "skipping live test (requires credentials)" )
}
2018-06-11 15:32:50 +00:00
defer restoreEnv ( )
2016-11-02 14:33:57 +00:00
os . Setenv ( "AZURE_CLIENT_ID" , "other" )
2018-06-11 15:32:50 +00:00
2016-11-02 14:33:57 +00:00
_ , err := NewDNSProvider ( )
assert . NoError ( t , err )
}
func TestNewDNSProviderMissingCredErr ( t * testing . T ) {
2018-06-11 15:32:50 +00:00
defer restoreEnv ( )
2016-11-02 14:33:57 +00:00
os . Setenv ( "AZURE_SUBSCRIPTION_ID" , "" )
2018-06-11 15:32:50 +00:00
2016-11-02 14:33:57 +00:00
_ , err := NewDNSProvider ( )
2018-06-11 15:32:50 +00:00
assert . EqualError ( t , err , "Azure: some credentials information are missing: AZURE_CLIENT_ID,AZURE_CLIENT_SECRET,AZURE_SUBSCRIPTION_ID,AZURE_TENANT_ID,AZURE_RESOURCE_GROUP" )
2016-11-02 14:33:57 +00:00
}
func TestLiveAzurePresent ( t * testing . T ) {
if ! azureLiveTest {
t . Skip ( "skipping live test" )
}
provider , err := NewDNSProviderCredentials ( azureClientID , azureClientSecret , azureSubscriptionID , azureTenantID , azureResourceGroup )
assert . NoError ( t , err )
err = provider . Present ( azureDomain , "" , "123d==" )
assert . NoError ( t , err )
}
func TestLiveAzureCleanUp ( t * testing . T ) {
if ! azureLiveTest {
t . Skip ( "skipping live test" )
}
provider , err := NewDNSProviderCredentials ( azureClientID , azureClientSecret , azureSubscriptionID , azureTenantID , azureResourceGroup )
time . Sleep ( time . Second * 1 )
assert . NoError ( t , err )
err = provider . CleanUp ( azureDomain , "" , "123d==" )
assert . NoError ( t , err )
}