2018-05-30 18:20:21 +00:00
package gcloud
2016-03-18 15:22:33 +00:00
import (
"os"
"testing"
"time"
2018-09-24 19:07:20 +00:00
"github.com/stretchr/testify/require"
2016-03-18 15:22:33 +00:00
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/api/dns/v1"
)
var (
2018-10-12 17:29:18 +00:00
liveTest bool
envTestProject string
envTestServiceAccountFile string
envTestGoogleApplicationCredentials string
envTestDomain string
2016-03-18 15:22:33 +00:00
)
func init ( ) {
2018-10-12 17:29:18 +00:00
envTestProject = os . Getenv ( "GCE_PROJECT" )
envTestServiceAccountFile = os . Getenv ( "GCE_SERVICE_ACCOUNT_FILE" )
envTestGoogleApplicationCredentials = os . Getenv ( "GOOGLE_APPLICATION_CREDENTIALS" )
envTestDomain = os . Getenv ( "GCE_DOMAIN" )
2016-03-18 15:22:33 +00:00
_ , err := google . DefaultClient ( context . Background ( ) , dns . NdevClouddnsReadwriteScope )
2018-10-12 17:29:18 +00:00
if err == nil && len ( envTestProject ) > 0 && len ( envTestDomain ) > 0 {
liveTest = true
2016-03-18 15:22:33 +00:00
}
}
2018-06-11 15:32:50 +00:00
func restoreEnv ( ) {
2018-10-12 17:29:18 +00:00
os . Setenv ( "GCE_PROJECT" , envTestProject )
os . Setenv ( "GCE_SERVICE_ACCOUNT_FILE" , envTestServiceAccountFile )
os . Setenv ( "GOOGLE_APPLICATION_CREDENTIALS" , envTestGoogleApplicationCredentials )
2016-03-18 15:22:33 +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 : "invalid credentials" ,
envVars : map [ string ] string {
"GCE_PROJECT" : "123" ,
"GCE_SERVICE_ACCOUNT_FILE" : "" ,
// as Travis run on GCE, we have to alter env
"GOOGLE_APPLICATION_CREDENTIALS" : "not-a-secret-file" ,
} ,
expected : "googlecloud: unable to get Google Cloud client: google: error getting credentials using GOOGLE_APPLICATION_CREDENTIALS environment variable: open not-a-secret-file: no such file or directory" ,
} ,
{
desc : "missing project" ,
envVars : map [ string ] string {
"GCE_PROJECT" : "" ,
"GCE_SERVICE_ACCOUNT_FILE" : "" ,
} ,
expected : "googlecloud: project name missing" ,
} ,
{
desc : "success" ,
envVars : map [ string ] string {
"GCE_PROJECT" : "" ,
"GCE_SERVICE_ACCOUNT_FILE" : "fixtures/gce_account_service_file.json" ,
} ,
} ,
2016-03-18 15:22:33 +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 ) {
defer restoreEnv ( )
for key , value := range test . envVars {
if len ( value ) == 0 {
os . Unsetenv ( key )
} else {
os . Setenv ( key , value )
}
}
p , err := NewDNSProvider ( )
if len ( test . expected ) == 0 {
require . NoError ( t , err )
require . NotNil ( t , p )
require . NotNil ( t , p . config )
require . NotNil ( t , p . client )
} else {
require . EqualError ( t , err , test . expected )
}
} )
2016-03-18 15:22:33 +00:00
}
}
2018-10-12 17:29:18 +00:00
func TestNewDNSProviderConfig ( t * testing . T ) {
testCases := [ ] struct {
desc string
project string
expected string
} {
{
desc : "invalid project" ,
project : "123" ,
expected : "googlecloud: unable to create Google Cloud DNS service: client is nil" ,
} ,
{
desc : "missing project" ,
expected : "googlecloud: unable to create Google Cloud DNS service: client is nil" ,
} ,
}
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 ) {
defer restoreEnv ( )
os . Unsetenv ( "GCE_PROJECT" )
os . Unsetenv ( "GCE_SERVICE_ACCOUNT_FILE" )
config := NewDefaultConfig ( )
config . Project = test . project
p , err := NewDNSProviderConfig ( config )
if len ( test . expected ) == 0 {
require . NoError ( t , err )
require . NotNil ( t , p )
require . NotNil ( t , p . config )
require . NotNil ( t , p . client )
} else {
require . EqualError ( t , err , test . expected )
}
} )
}
2016-03-18 15:22:33 +00:00
}
2018-10-12 17:29:18 +00:00
func TestLivePresent ( t * testing . T ) {
if ! liveTest {
2016-03-18 15:22:33 +00:00
t . Skip ( "skipping live test" )
}
2018-10-12 17:29:18 +00:00
provider , err := NewDNSProviderCredentials ( envTestProject )
2018-09-24 19:07:20 +00:00
require . NoError ( t , err )
2016-03-18 15:22:33 +00:00
2018-10-12 17:29:18 +00:00
err = provider . Present ( envTestDomain , "" , "123d==" )
2018-09-24 19:07:20 +00:00
require . NoError ( t , err )
2016-03-18 15:22:33 +00:00
}
2018-10-12 17:29:18 +00:00
func TestLivePresentMultiple ( t * testing . T ) {
if ! liveTest {
2016-11-02 14:47:17 +00:00
t . Skip ( "skipping live test" )
}
2018-10-12 17:29:18 +00:00
provider , err := NewDNSProviderCredentials ( envTestProject )
2018-09-24 19:07:20 +00:00
require . NoError ( t , err )
2016-11-02 14:47:17 +00:00
// Check that we're able to create multiple entries
2018-10-12 17:29:18 +00:00
err = provider . Present ( envTestDomain , "1" , "123d==" )
2018-09-24 19:07:20 +00:00
require . NoError ( t , err )
2018-10-12 17:29:18 +00:00
err = provider . Present ( envTestDomain , "2" , "123d==" )
2018-09-24 19:07:20 +00:00
require . NoError ( t , err )
2016-11-02 14:47:17 +00:00
}
2018-10-12 17:29:18 +00:00
func TestLiveCleanUp ( t * testing . T ) {
if ! liveTest {
2016-03-18 15:22:33 +00:00
t . Skip ( "skipping live test" )
}
2018-10-12 17:29:18 +00:00
provider , err := NewDNSProviderCredentials ( envTestProject )
2018-09-24 19:07:20 +00:00
require . NoError ( t , err )
2016-03-18 15:22:33 +00:00
2018-10-12 17:29:18 +00:00
time . Sleep ( 1 * time . Second )
err = provider . CleanUp ( envTestDomain , "" , "123d==" )
2018-09-24 19:07:20 +00:00
require . NoError ( t , err )
2016-03-18 15:22:33 +00:00
}