2016-11-02 14:33:57 +00:00
package azure
import (
"testing"
"time"
2018-10-12 17:29:18 +00:00
"github.com/stretchr/testify/require"
2018-10-16 15:52:57 +00:00
"github.com/xenolf/lego/platform/tester"
2016-11-02 14:33:57 +00:00
)
2018-10-16 15:52:57 +00:00
var envTest = tester . NewEnvTest (
"AZURE_CLIENT_ID" ,
"AZURE_CLIENT_SECRET" ,
"AZURE_SUBSCRIPTION_ID" ,
"AZURE_TENANT_ID" ,
"AZURE_RESOURCE_GROUP" ) .
WithDomain ( "AZURE_DOMAIN" )
2016-11-02 14:33:57 +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 {
"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 14:33:57 +00:00
}
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-06-11 15:32:50 +00:00
2018-10-12 17:29:18 +00:00
p , err := NewDNSProvider ( )
2018-09-15 17:07:24 +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 )
}
} )
}
2016-11-02 14:33:57 +00:00
}
2018-10-12 17:29:18 +00: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 14:33:57 +00:00
}
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 ) {
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 14:33:57 +00:00
2018-10-12 17:29:18 +00:00
p , err := NewDNSProviderConfig ( config )
2018-06-11 15:32:50 +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 )
}
} )
}
2016-11-02 14:33:57 +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 ( ) {
2016-11-02 14:33:57 +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-09-15 17:07:24 +00:00
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 )
2016-11-02 14:33:57 +00:00
}
2018-10-12 17:29:18 +00:00
func TestLiveCleanUp ( t * testing . T ) {
2018-10-16 15:52:57 +00:00
if ! envTest . IsLiveTest ( ) {
2016-11-02 14:33:57 +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 )
2016-11-02 14:33:57 +00:00
2018-10-12 17:29:18 +00:00
time . Sleep ( 1 * time . Second )
2016-11-02 14:33:57 +00:00
2018-10-16 15:52:57 +00:00
err = provider . CleanUp ( envTest . GetDomain ( ) , "" , "123d==" )
2018-10-12 17:29:18 +00:00
require . NoError ( t , err )
2016-11-02 14:33:57 +00:00
}