2019-03-11 18:56:48 +02:00
package bluecat
2018-04-03 04:50:15 +02:00
import (
"testing"
"time"
2018-05-30 19:53:04 +02:00
2020-09-02 03:20:01 +02:00
"github.com/go-acme/lego/v4/platform/tester"
2018-10-12 19:29:18 +02:00
"github.com/stretchr/testify/require"
2018-04-03 04:50:15 +02:00
)
2020-03-12 00:51:10 +02:00
const envDomain = envNamespace + "DOMAIN"
2018-10-16 17:52:57 +02:00
var envTest = tester . NewEnvTest (
2020-03-12 00:51:10 +02:00
EnvServerURL ,
EnvUserName ,
EnvPassword ,
EnvConfigName ,
EnvDNSView ) .
WithDomain ( envDomain )
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 {
2020-03-12 00:51:10 +02:00
EnvServerURL : "http://localhost" ,
EnvUserName : "A" ,
EnvPassword : "B" ,
EnvConfigName : "C" ,
EnvDNSView : "D" ,
2018-10-12 19:29:18 +02:00
} ,
} ,
{
desc : "missing credentials" ,
envVars : map [ string ] string {
2020-03-12 00:51:10 +02:00
EnvServerURL : "" ,
EnvUserName : "" ,
EnvPassword : "" ,
EnvConfigName : "" ,
EnvDNSView : "" ,
2018-10-12 19:29:18 +02:00
} ,
expected : "bluecat: some credentials information are missing: BLUECAT_SERVER_URL,BLUECAT_USER_NAME,BLUECAT_PASSWORD,BLUECAT_CONFIG_NAME,BLUECAT_DNS_VIEW" ,
} ,
{
desc : "missing server url" ,
envVars : map [ string ] string {
2020-03-12 00:51:10 +02:00
EnvServerURL : "" ,
EnvUserName : "A" ,
EnvPassword : "B" ,
EnvConfigName : "C" ,
EnvDNSView : "D" ,
2018-10-12 19:29:18 +02:00
} ,
expected : "bluecat: some credentials information are missing: BLUECAT_SERVER_URL" ,
} ,
{
desc : "missing username" ,
envVars : map [ string ] string {
2020-03-12 00:51:10 +02:00
EnvServerURL : "http://localhost" ,
EnvUserName : "" ,
EnvPassword : "B" ,
EnvConfigName : "C" ,
EnvDNSView : "D" ,
2018-10-12 19:29:18 +02:00
} ,
expected : "bluecat: some credentials information are missing: BLUECAT_USER_NAME" ,
} ,
{
desc : "missing password" ,
envVars : map [ string ] string {
2020-03-12 00:51:10 +02:00
EnvServerURL : "http://localhost" ,
EnvUserName : "A" ,
EnvPassword : "" ,
EnvConfigName : "C" ,
EnvDNSView : "D" ,
2018-10-12 19:29:18 +02:00
} ,
expected : "bluecat: some credentials information are missing: BLUECAT_PASSWORD" ,
} ,
{
desc : "missing config name" ,
envVars : map [ string ] string {
2020-03-12 00:51:10 +02:00
EnvServerURL : "http://localhost" ,
EnvUserName : "A" ,
EnvPassword : "B" ,
EnvConfigName : "" ,
EnvDNSView : "D" ,
2018-10-12 19:29:18 +02:00
} ,
expected : "bluecat: some credentials information are missing: BLUECAT_CONFIG_NAME" ,
} ,
{
desc : "missing DNS view" ,
envVars : map [ string ] string {
2020-03-12 00:51:10 +02:00
EnvServerURL : "http://localhost" ,
EnvUserName : "A" ,
EnvPassword : "B" ,
EnvConfigName : "C" ,
EnvDNSView : "" ,
2018-10-12 19:29:18 +02:00
} ,
expected : "bluecat: some credentials information are missing: BLUECAT_DNS_VIEW" ,
} ,
}
for _ , test := range testCases {
t . Run ( test . desc , func ( t * testing . T ) {
2018-10-16 17:52:57 +02:00
defer envTest . RestoreEnv ( )
envTest . ClearEnv ( )
envTest . Apply ( test . envVars )
2018-10-12 19:29:18 +02:00
p , err := NewDNSProvider ( )
2021-03-04 21:16:59 +02:00
if test . expected == "" {
2018-10-12 19:29:18 +02:00
require . NoError ( t , err )
require . NotNil ( t , p )
require . NotNil ( t , p . config )
} else {
require . EqualError ( t , err , test . expected )
}
} )
2018-04-03 04:50:15 +02:00
}
}
2018-10-12 19:29:18 +02:00
func TestNewDNSProviderConfig ( t * testing . T ) {
testCases := [ ] struct {
desc string
baseURL string
userName string
password string
configName string
dnsView string
expected string
} {
{
desc : "success" ,
baseURL : "http://localhost" ,
userName : "A" ,
password : "B" ,
configName : "C" ,
dnsView : "D" ,
} ,
{
desc : "missing credentials" ,
expected : "bluecat: credentials missing" ,
} ,
{
desc : "missing base URL" ,
baseURL : "" ,
userName : "A" ,
password : "B" ,
configName : "C" ,
dnsView : "D" ,
expected : "bluecat: credentials missing" ,
} ,
{
desc : "missing username" ,
baseURL : "http://localhost" ,
userName : "" ,
password : "B" ,
configName : "C" ,
dnsView : "D" ,
expected : "bluecat: credentials missing" ,
} ,
{
desc : "missing password" ,
baseURL : "http://localhost" ,
userName : "A" ,
password : "" ,
configName : "C" ,
dnsView : "D" ,
expected : "bluecat: credentials missing" ,
} ,
{
desc : "missing config name" ,
baseURL : "http://localhost" ,
userName : "A" ,
password : "B" ,
configName : "" ,
dnsView : "D" ,
expected : "bluecat: credentials missing" ,
} ,
{
desc : "missing DNS view" ,
baseURL : "http://localhost" ,
userName : "A" ,
password : "B" ,
configName : "C" ,
dnsView : "" ,
expected : "bluecat: credentials missing" ,
} ,
}
for _ , test := range testCases {
t . Run ( test . desc , func ( t * testing . T ) {
config := NewDefaultConfig ( )
config . BaseURL = test . baseURL
config . UserName = test . userName
config . Password = test . password
config . ConfigName = test . configName
config . DNSView = test . dnsView
p , err := NewDNSProviderConfig ( config )
2021-03-04 21:16:59 +02:00
if test . expected == "" {
2018-10-12 19:29:18 +02:00
require . NoError ( t , err )
require . NotNil ( t , p )
require . NotNil ( t , p . config )
} else {
require . EqualError ( t , err , test . expected )
}
} )
}
}
func TestLivePresent ( t * testing . T ) {
2018-10-16 17:52:57 +02:00
if ! envTest . IsLiveTest ( ) {
2018-04-03 04:50:15 +02:00
t . Skip ( "skipping live test" )
}
2018-10-16 17:52:57 +02:00
envTest . RestoreEnv ( )
2018-04-03 04:50:15 +02:00
provider , err := NewDNSProvider ( )
2018-10-12 19:29:18 +02:00
require . NoError ( t , err )
2018-04-03 04:50:15 +02:00
2018-10-16 17:52:57 +02:00
err = provider . Present ( envTest . GetDomain ( ) , "" , "123d==" )
2018-10-12 19:29:18 +02:00
require . NoError ( t , err )
2018-04-03 04:50:15 +02:00
}
2018-10-12 19:29:18 +02:00
func TestLiveCleanUp ( t * testing . T ) {
2018-10-16 17:52:57 +02:00
if ! envTest . IsLiveTest ( ) {
2018-04-03 04:50:15 +02:00
t . Skip ( "skipping live test" )
}
2018-10-16 17:52:57 +02:00
envTest . RestoreEnv ( )
2018-04-03 04:50:15 +02:00
provider , err := NewDNSProvider ( )
2018-10-12 19:29:18 +02:00
require . NoError ( t , err )
time . Sleep ( time . Second * 1 )
2018-04-03 04:50:15 +02:00
2018-10-16 17:52:57 +02:00
err = provider . CleanUp ( envTest . GetDomain ( ) , "" , "123d==" )
2018-10-12 19:29:18 +02:00
require . NoError ( t , err )
2018-04-03 04:50:15 +02:00
}