1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-25 02:55:11 +02:00
lego/providers/dns/sakuracloud/sakuracloud_test.go
Ludovic Fernandez ad20bf90ff Migrate to golangci-lint (#644)
* refactor: linting.

- errcheck
- govet
- golint
- goconst
- spellcheck
- ...

* refactor: migrate from gometalinter to golangci-lint.
2018-09-24 19:07:20 +00:00

126 lines
2.8 KiB
Go

package sakuracloud
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/acme"
)
var (
sakuracloudLiveTest bool
sakuracloudAccessToken string
sakuracloudAccessSecret string
sakuracloudDomain string
)
func init() {
sakuracloudAccessToken = os.Getenv("SAKURACLOUD_ACCESS_TOKEN")
sakuracloudAccessSecret = os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET")
sakuracloudDomain = os.Getenv("SAKURACLOUD_DOMAIN")
if len(sakuracloudAccessToken) > 0 && len(sakuracloudAccessSecret) > 0 && len(sakuracloudDomain) > 0 {
sakuracloudLiveTest = true
}
}
func restoreEnv() {
os.Setenv("SAKURACLOUD_ACCESS_TOKEN", sakuracloudAccessToken)
os.Setenv("SAKURACLOUD_ACCESS_TOKEN_SECRET", sakuracloudAccessSecret)
}
//
// NewDNSProvider
//
func TestNewDNSProviderValid(t *testing.T) {
defer restoreEnv()
os.Setenv("SAKURACLOUD_ACCESS_TOKEN", "123")
os.Setenv("SAKURACLOUD_ACCESS_TOKEN_SECRET", "456")
provider, err := NewDNSProvider()
assert.NotNil(t, provider)
assert.Equal(t, acme.UserAgent, provider.client.UserAgent)
require.NoError(t, err)
}
func TestNewDNSProviderInvalidWithMissingAccessToken(t *testing.T) {
defer restoreEnv()
os.Setenv("SAKURACLOUD_ACCESS_TOKEN", "")
provider, err := NewDNSProvider()
assert.Nil(t, provider)
assert.EqualError(t, err, "sakuracloud: some credentials information are missing: SAKURACLOUD_ACCESS_TOKEN,SAKURACLOUD_ACCESS_TOKEN_SECRET")
}
//
// NewDNSProviderCredentials
//
func TestNewDNSProviderCredentialsValid(t *testing.T) {
config := NewDefaultConfig()
config.Token = "123"
config.Secret = "456"
provider, err := NewDNSProviderConfig(config)
require.NoError(t, err)
assert.NotNil(t, provider)
assert.Equal(t, acme.UserAgent, provider.client.UserAgent)
}
func TestNewDNSProviderCredentialsInvalidWithMissingAccessToken(t *testing.T) {
config := NewDefaultConfig()
provider, err := NewDNSProviderConfig(config)
assert.Nil(t, provider)
assert.EqualError(t, err, "sakuracloud: AccessToken is missing")
}
//
// Present
//
func TestLiveSakuraCloudPresent(t *testing.T) {
if !sakuracloudLiveTest {
t.Skip("skipping live test")
}
config := NewDefaultConfig()
config.Token = sakuracloudAccessToken
config.Secret = sakuracloudAccessSecret
provider, err := NewDNSProviderConfig(config)
require.NoError(t, err)
err = provider.Present(sakuracloudDomain, "", "123d==")
require.NoError(t, err)
}
//
// Cleanup
//
func TestLiveSakuraCloudCleanUp(t *testing.T) {
if !sakuracloudLiveTest {
t.Skip("skipping live test")
}
time.Sleep(time.Second * 1)
config := NewDefaultConfig()
config.Token = sakuracloudAccessToken
config.Secret = sakuracloudAccessSecret
provider, err := NewDNSProviderConfig(config)
require.NoError(t, err)
err = provider.CleanUp(sakuracloudDomain, "", "123d==")
require.NoError(t, err)
}