2018-05-30 18:20:21 +00:00
|
|
|
package gcloud
|
2016-03-18 15:22:33 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"golang.org/x/oauth2/google"
|
|
|
|
"google.golang.org/api/dns/v1"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
gcloudLiveTest bool
|
|
|
|
gcloudProject string
|
|
|
|
gcloudDomain string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
gcloudProject = os.Getenv("GCE_PROJECT")
|
|
|
|
gcloudDomain = os.Getenv("GCE_DOMAIN")
|
|
|
|
_, err := google.DefaultClient(context.Background(), dns.NdevClouddnsReadwriteScope)
|
|
|
|
if err == nil && len(gcloudProject) > 0 && len(gcloudDomain) > 0 {
|
|
|
|
gcloudLiveTest = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
func restoreEnv() {
|
2016-03-18 15:22:33 +00:00
|
|
|
os.Setenv("GCE_PROJECT", gcloudProject)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderValid(t *testing.T) {
|
|
|
|
if !gcloudLiveTest {
|
|
|
|
t.Skip("skipping live test (requires credentials)")
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
|
|
|
defer restoreEnv()
|
2016-03-18 15:22:33 +00:00
|
|
|
os.Setenv("GCE_PROJECT", "")
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-03-17 20:59:15 +00:00
|
|
|
_, err := NewDNSProviderCredentials("my-project")
|
2016-03-18 15:22:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderValidEnv(t *testing.T) {
|
|
|
|
if !gcloudLiveTest {
|
|
|
|
t.Skip("skipping live test (requires credentials)")
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
|
|
|
defer restoreEnv()
|
2016-03-18 15:22:33 +00:00
|
|
|
os.Setenv("GCE_PROJECT", "my-project")
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-03-17 20:59:15 +00:00
|
|
|
_, err := NewDNSProvider()
|
2016-03-18 15:22:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderMissingCredErr(t *testing.T) {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
2016-03-18 15:22:33 +00:00
|
|
|
os.Setenv("GCE_PROJECT", "")
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-03-17 20:59:15 +00:00
|
|
|
_, err := NewDNSProvider()
|
2018-09-15 17:07:24 +00:00
|
|
|
assert.EqualError(t, err, "googlecloud: project name missing")
|
2016-03-18 15:22:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveGoogleCloudPresent(t *testing.T) {
|
|
|
|
if !gcloudLiveTest {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2016-03-17 20:59:15 +00:00
|
|
|
provider, err := NewDNSProviderCredentials(gcloudProject)
|
2016-03-18 15:22:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = provider.Present(gcloudDomain, "", "123d==")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2016-11-02 14:47:17 +00:00
|
|
|
func TestLiveGoogleCloudPresentMultiple(t *testing.T) {
|
|
|
|
if !gcloudLiveTest {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderCredentials(gcloudProject)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Check that we're able to create multiple entries
|
|
|
|
err = provider.Present(gcloudDomain, "1", "123d==")
|
2018-05-30 17:53:04 +00:00
|
|
|
assert.NoError(t, err)
|
2016-11-02 14:47:17 +00:00
|
|
|
err = provider.Present(gcloudDomain, "2", "123d==")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2016-03-18 15:22:33 +00:00
|
|
|
func TestLiveGoogleCloudCleanUp(t *testing.T) {
|
|
|
|
if !gcloudLiveTest {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
|
2016-03-17 20:59:15 +00:00
|
|
|
provider, err := NewDNSProviderCredentials(gcloudProject)
|
2016-03-18 15:22:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = provider.CleanUp(gcloudDomain, "", "123d==")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|