2016-11-02 16:33:57 +02:00
package azure
import (
"os"
"testing"
"time"
2018-10-12 19:29:18 +02:00
"github.com/stretchr/testify/require"
2016-11-02 16:33:57 +02:00
)
var (
2018-10-12 19:29:18 +02:00
liveTest bool
envTestClientID string
envTestClientSecret string
envTestSubscriptionID string
envTestTenantID string
envTestResourceGroup string
envTestDomain string
2016-11-02 16:33:57 +02:00
)
func init ( ) {
2018-10-12 19:29:18 +02:00
envTestClientID = os . Getenv ( "AZURE_CLIENT_ID" )
envTestClientSecret = os . Getenv ( "AZURE_CLIENT_SECRET" )
envTestSubscriptionID = os . Getenv ( "AZURE_SUBSCRIPTION_ID" )
envTestTenantID = os . Getenv ( "AZURE_TENANT_ID" )
envTestResourceGroup = os . Getenv ( "AZURE_RESOURCE_GROUP" )
envTestDomain = os . Getenv ( "AZURE_DOMAIN" )
if len ( envTestClientID ) > 0 && len ( envTestClientSecret ) > 0 {
liveTest = true
2016-11-02 16:33:57 +02:00
}
}
2018-06-11 17:32:50 +02:00
func restoreEnv ( ) {
2018-10-12 19:29:18 +02:00
os . Setenv ( "AZURE_CLIENT_ID" , envTestClientID )
os . Setenv ( "AZURE_CLIENT_SECRET" , envTestClientSecret )
os . Setenv ( "AZURE_SUBSCRIPTION_ID" , envTestSubscriptionID )
os . Setenv ( "AZURE_TENANT_ID" , envTestTenantID )
os . Setenv ( "AZURE_RESOURCE_GROUP" , envTestResourceGroup )
2016-11-02 16:33:57 +02:00
}
2018-10-12 19:29:18 +02:00
func TestNewDNSProvider ( t * testing . T ) {
testCases := [ ] struct {
desc string
envVars map [ string ] string
expected string
} {
{
desc : "success" ,
envVars : map [ string ] string {
"AZURE_CLIENT_ID" : "A" ,
"AZURE_CLIENT_SECRET" : "B" ,
"AZURE_SUBSCRIPTION_ID" : "C" ,
"AZURE_TENANT_ID" : "D" ,
"AZURE_RESOURCE_GROUP" : "E" ,
} ,
} ,
{
desc : "missing credentials" ,
envVars : map [ string ] string {
"AZURE_CLIENT_ID" : "" ,
"AZURE_CLIENT_SECRET" : "" ,
"AZURE_SUBSCRIPTION_ID" : "" ,
"AZURE_TENANT_ID" : "" ,
"AZURE_RESOURCE_GROUP" : "" ,
} ,
expected : "azure: some credentials information are missing: AZURE_CLIENT_ID,AZURE_CLIENT_SECRET,AZURE_SUBSCRIPTION_ID,AZURE_TENANT_ID,AZURE_RESOURCE_GROUP" ,
} ,
{
desc : "missing client id" ,
envVars : map [ string ] string {
"AZURE_CLIENT_ID" : "" ,
"AZURE_CLIENT_SECRET" : "B" ,
"AZURE_SUBSCRIPTION_ID" : "C" ,
"AZURE_TENANT_ID" : "D" ,
"AZURE_RESOURCE_GROUP" : "E" ,
} ,
expected : "azure: some credentials information are missing: AZURE_CLIENT_ID" ,
} ,
{
desc : "missing client secret" ,
envVars : map [ string ] string {
"AZURE_CLIENT_ID" : "A" ,
"AZURE_CLIENT_SECRET" : "" ,
"AZURE_SUBSCRIPTION_ID" : "C" ,
"AZURE_TENANT_ID" : "D" ,
"AZURE_RESOURCE_GROUP" : "E" ,
} ,
expected : "azure: some credentials information are missing: AZURE_CLIENT_SECRET" ,
} ,
{
desc : "missing subscription id" ,
envVars : map [ string ] string {
"AZURE_CLIENT_ID" : "A" ,
"AZURE_CLIENT_SECRET" : "B" ,
"AZURE_SUBSCRIPTION_ID" : "" ,
"AZURE_TENANT_ID" : "D" ,
"AZURE_RESOURCE_GROUP" : "E" ,
} ,
expected : "azure: some credentials information are missing: AZURE_SUBSCRIPTION_ID" ,
} ,
{
desc : "missing tenant id" ,
envVars : map [ string ] string {
"AZURE_CLIENT_ID" : "A" ,
"AZURE_CLIENT_SECRET" : "B" ,
"AZURE_SUBSCRIPTION_ID" : "C" ,
"AZURE_TENANT_ID" : "" ,
"AZURE_RESOURCE_GROUP" : "E" ,
} ,
expected : "azure: some credentials information are missing: AZURE_TENANT_ID" ,
} ,
{
desc : "missing resource group" ,
envVars : map [ string ] string {
"AZURE_CLIENT_ID" : "A" ,
"AZURE_CLIENT_SECRET" : "B" ,
"AZURE_SUBSCRIPTION_ID" : "C" ,
"AZURE_TENANT_ID" : "D" ,
"AZURE_RESOURCE_GROUP" : "" ,
} ,
expected : "azure: some credentials information are missing: AZURE_RESOURCE_GROUP" ,
} ,
2016-11-02 16:33:57 +02:00
}
2018-06-11 17:32:50 +02:00
2018-10-12 19:29:18 +02:00
for _ , test := range testCases {
t . Run ( test . desc , func ( t * testing . T ) {
defer restoreEnv ( )
for key , value := range test . envVars {
if len ( value ) == 0 {
os . Unsetenv ( key )
} else {
os . Setenv ( key , value )
}
}
2018-06-11 17:32:50 +02:00
2018-10-12 19:29:18 +02:00
p , err := NewDNSProvider ( )
2018-09-15 19:07:24 +02:00
2018-10-12 19:29:18 +02: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 )
}
} )
}
2016-11-02 16:33:57 +02:00
}
2018-10-12 19:29:18 +02:00
func TestNewDNSProviderConfig ( t * testing . T ) {
testCases := [ ] struct {
desc string
clientID string
clientSecret string
subscriptionID string
tenantID string
resourceGroup string
expected string
} {
{
desc : "success" ,
clientID : "A" ,
clientSecret : "B" ,
subscriptionID : "C" ,
tenantID : "D" ,
resourceGroup : "E" ,
} ,
{
desc : "missing credentials" ,
expected : "azure: some credentials information are missing" ,
} ,
{
desc : "missing client id" ,
clientID : "" ,
clientSecret : "B" ,
subscriptionID : "C" ,
tenantID : "D" ,
resourceGroup : "E" ,
expected : "azure: some credentials information are missing" ,
} ,
{
desc : "missing client secret" ,
clientID : "A" ,
clientSecret : "" ,
subscriptionID : "C" ,
tenantID : "D" ,
resourceGroup : "E" ,
expected : "azure: some credentials information are missing" ,
} ,
{
desc : "missing subscription id" ,
clientID : "A" ,
clientSecret : "B" ,
subscriptionID : "" ,
tenantID : "D" ,
resourceGroup : "E" ,
expected : "azure: some credentials information are missing" ,
} ,
{
desc : "missing tenant id" ,
clientID : "A" ,
clientSecret : "B" ,
subscriptionID : "C" ,
tenantID : "" ,
resourceGroup : "E" ,
expected : "azure: some credentials information are missing" ,
} ,
{
desc : "missing resource group" ,
clientID : "A" ,
clientSecret : "B" ,
subscriptionID : "C" ,
tenantID : "D" ,
resourceGroup : "" ,
expected : "azure: some credentials information are missing" ,
} ,
2016-11-02 16:33:57 +02:00
}
2018-06-11 17:32:50 +02:00
2018-10-12 19:29:18 +02:00
for _ , test := range testCases {
t . Run ( test . desc , func ( t * testing . T ) {
defer restoreEnv ( )
os . Unsetenv ( "AZURE_CLIENT_ID" )
os . Unsetenv ( "AZURE_CLIENT_SECRET" )
os . Unsetenv ( "AZURE_SUBSCRIPTION_ID" )
os . Unsetenv ( "AZURE_TENANT_ID" )
os . Unsetenv ( "AZURE_RESOURCE_GROUP" )
2018-06-11 17:32:50 +02:00
2018-10-12 19:29:18 +02:00
config := NewDefaultConfig ( )
config . ClientID = test . clientID
config . ClientSecret = test . clientSecret
config . SubscriptionID = test . subscriptionID
config . TenantID = test . tenantID
config . ResourceGroup = test . resourceGroup
2016-11-02 16:33:57 +02:00
2018-10-12 19:29:18 +02:00
p , err := NewDNSProviderConfig ( config )
2018-06-11 17:32:50 +02:00
2018-10-12 19:29:18 +02: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 )
}
} )
}
2016-11-02 16:33:57 +02:00
}
2018-10-12 19:29:18 +02:00
func TestLivePresent ( t * testing . T ) {
if ! liveTest {
2016-11-02 16:33:57 +02:00
t . Skip ( "skipping live test" )
}
2018-10-12 19:29:18 +02:00
restoreEnv ( )
provider , err := NewDNSProvider ( )
require . NoError ( t , err )
2018-09-15 19:07:24 +02:00
2018-10-12 19:29:18 +02:00
err = provider . Present ( envTestDomain , "" , "123d==" )
require . NoError ( t , err )
2016-11-02 16:33:57 +02:00
}
2018-10-12 19:29:18 +02:00
func TestLiveCleanUp ( t * testing . T ) {
if ! liveTest {
2016-11-02 16:33:57 +02:00
t . Skip ( "skipping live test" )
}
2018-10-12 19:29:18 +02:00
restoreEnv ( )
provider , err := NewDNSProvider ( )
require . NoError ( t , err )
2016-11-02 16:33:57 +02:00
2018-10-12 19:29:18 +02:00
time . Sleep ( 1 * time . Second )
2016-11-02 16:33:57 +02:00
2018-10-12 19:29:18 +02:00
err = provider . CleanUp ( envTestDomain , "" , "123d==" )
require . NoError ( t , err )
2016-11-02 16:33:57 +02:00
}