Add a test helper to manage env vars. (#675)

This commit is contained in:
Ludovic Fernandez 2018-10-16 17:52:57 +02:00 committed by GitHub
parent 4b2605dd52
commit 4d21f8eec1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
62 changed files with 5541 additions and 1526 deletions

12
Gopkg.lock generated
View file

@ -381,10 +381,19 @@
version = "v1.0.6"
[[projects]]
digest = "1:b31059dac028ff111793a8345eacf0f99d0f1150ead34ebf32fdd2b7d54c2d45"
digest = "1:60a46e2410edbf02b419f833372dd1d24d7aa1b916a990a7370e792fada1eadd"
name = "github.com/stretchr/objx"
packages = ["."]
pruneopts = "NUT"
revision = "477a77ecc69700c7cdeb1fa9e129548e1c1c393c"
version = "v0.1.1"
[[projects]]
digest = "1:f1e5a94fc8fde9a67a97106d7d7d386ad0b938b41fde2b110e70447590aea6e7"
name = "github.com/stretchr/testify"
packages = [
"assert",
"mock",
"require",
"suite",
]
@ -609,6 +618,7 @@
"github.com/sacloud/libsacloud/api",
"github.com/sacloud/libsacloud/sacloud",
"github.com/stretchr/testify/assert",
"github.com/stretchr/testify/mock",
"github.com/stretchr/testify/require",
"github.com/stretchr/testify/suite",
"github.com/timewasted/linode",

View file

@ -7,6 +7,7 @@ import (
"fmt"
"net"
"strings"
"sync"
"time"
"github.com/miekg/dns"
@ -18,8 +19,9 @@ type preCheckDNSFunc func(fqdn, value string) (bool, error)
var (
// PreCheckDNS checks DNS propagation before notifying ACME that
// the DNS challenge is ready.
PreCheckDNS preCheckDNSFunc = checkDNSPropagation
fqdnToZone = map[string]string{}
PreCheckDNS preCheckDNSFunc = checkDNSPropagation
fqdnToZone = map[string]string{}
muFqdnToZone sync.Mutex
)
const defaultResolvConf = "/etc/resolv.conf"
@ -262,6 +264,9 @@ func lookupNameservers(fqdn string) ([]string, error) {
// FindZoneByFqdn determines the zone apex for the given fqdn by recursing up the
// domain labels until the nameserver returns a SOA record in the answer section.
func FindZoneByFqdn(fqdn string, nameservers []string) (string, error) {
muFqdnToZone.Lock()
defer muFqdnToZone.Unlock()
// Do we have it cached?
if zone, ok := fqdnToZone[fqdn]; ok {
return zone, nil

142
platform/tester/env.go Normal file
View file

@ -0,0 +1,142 @@
package tester
import (
"fmt"
"os"
)
// EnvTest Environment variables manager for tests.
type EnvTest struct {
keys []string
values map[string]string
liveTestHook func() bool
liveTestExtraHook func() bool
domain string
domainKey string
}
// NewEnvTest Creates an EnvTest.
func NewEnvTest(keys ...string) *EnvTest {
values := make(map[string]string)
for _, key := range keys {
value := os.Getenv(key)
if value != "" {
values[key] = value
}
}
return &EnvTest{
keys: keys,
values: values,
}
}
// WithDomain Defines the name of the environment variable used to define the domain related to the DNS request.
// If the domain is defined, it was considered mandatory to define a test as a "live" test.
func (e *EnvTest) WithDomain(key string) *EnvTest {
e.domainKey = key
e.domain = os.Getenv(key)
return e
}
// WithLiveTestRequirements Defines the environment variables required to define a test as a "live" test.
// Replaces the default behavior (all keys are required).
func (e *EnvTest) WithLiveTestRequirements(keys ...string) *EnvTest {
var countValuedVars int
for _, key := range keys {
if _, ok := e.values[key]; ok {
countValuedVars++
}
}
live := countValuedVars != 0 && len(keys) == countValuedVars
e.liveTestHook = func() bool {
return live
}
return e
}
// WithLiveTestExtra Allows to define an additional condition to flag a test as "live" test.
// This does not replace the default behavior.
func (e *EnvTest) WithLiveTestExtra(extra func() bool) *EnvTest {
e.liveTestExtraHook = extra
return e
}
// GetDomain Gets the domain value associated with the DNS challenge (linked to WithDomain method).
func (e *EnvTest) GetDomain() string {
return e.domain
}
// IsLiveTest Checks whether environment variables allow running a "live" test.
func (e *EnvTest) IsLiveTest() bool {
liveTest := e.liveTestExtra()
if e.liveTestHook != nil {
return liveTest && e.liveTestHook()
}
liveTest = liveTest && len(e.values) == len(e.keys)
if liveTest && len(e.domainKey) > 0 && len(e.domain) == 0 {
return false
}
return liveTest
}
// RestoreEnv Restores the environment variables to the initial state.
func (e *EnvTest) RestoreEnv() {
for key, value := range e.values {
os.Setenv(key, value)
}
}
// ClearEnv Deletes all environment variables related to the test.
func (e *EnvTest) ClearEnv() {
for _, key := range e.keys {
os.Unsetenv(key)
}
}
// GetValue Gets the stored value of an environment variable.
func (e *EnvTest) GetValue(key string) string {
return e.values[key]
}
func (e *EnvTest) liveTestExtra() bool {
if e.liveTestExtraHook == nil {
return true
}
return e.liveTestExtraHook()
}
// Apply Sets/Unsets environment variables.
// Not related to the main environment variables.
func (e *EnvTest) Apply(envVars map[string]string) {
for key, value := range envVars {
if e.isManagedKey(key) {
if len(value) == 0 {
os.Unsetenv(key)
} else {
os.Setenv(key, value)
}
} else {
panic(fmt.Sprintf("Unauthorized action, the env var %s is not managed.", key))
}
}
}
func (e *EnvTest) isManagedKey(varName string) bool {
for _, key := range e.keys {
if key == varName {
return true
}
}
return false
}

347
platform/tester/env_test.go Normal file
View file

@ -0,0 +1,347 @@
package tester_test
import (
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/xenolf/lego/platform/tester"
)
var (
envNamespace = "LEGO_TEST_"
envVar01 = envNamespace + "01"
envVar02 = envNamespace + "02"
envVarDomain = envNamespace + "DOMAIN"
)
func TestMain(m *testing.M) {
exitCode := m.Run()
clearEnv()
os.Exit(exitCode)
}
func applyEnv(envVars map[string]string) {
for key, value := range envVars {
if len(value) == 0 {
os.Unsetenv(key)
} else {
os.Setenv(key, value)
}
}
}
func clearEnv() {
environ := os.Environ()
for _, key := range environ {
if strings.HasPrefix(key, envNamespace) {
os.Unsetenv(strings.Split(key, "=")[0])
}
}
os.Unsetenv("EXTRA_LEGO_TEST")
}
func TestEnvTest(t *testing.T) {
testCases := []struct {
desc string
envVars map[string]string
envTestSetup func() *tester.EnvTest
expected func(t *testing.T, envTest *tester.EnvTest)
}{
{
desc: "simple",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.True(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "missing env var",
envVars: map[string]string{
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.False(t, envTest.IsLiveTest())
assert.Equal(t, "", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithDomain",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
envVarDomain: "D",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).WithDomain(envVarDomain)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.True(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetValue(envVarDomain))
assert.Equal(t, "D", envTest.GetDomain())
},
},
{
desc: "WithDomain missing env var",
envVars: map[string]string{
envVar01: "A",
envVarDomain: "D",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).WithDomain(envVarDomain)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.False(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetValue(envVarDomain))
assert.Equal(t, "D", envTest.GetDomain())
},
},
{
desc: "WithDomain missing domain",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).WithDomain(envVarDomain)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.False(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetValue(envVarDomain))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestRequirements",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).WithLiveTestRequirements(envVar02)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.True(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestRequirements non required var missing",
envVars: map[string]string{
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).WithLiveTestRequirements(envVar02)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.True(t, envTest.IsLiveTest())
assert.Equal(t, "", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestRequirements required var missing",
envVars: map[string]string{
envVar01: "A",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).WithLiveTestRequirements(envVar02)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.False(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestRequirements WithDomain",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
envVarDomain: "D",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).
WithDomain(envVarDomain).
WithLiveTestRequirements(envVar02)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.True(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetValue(envVarDomain))
assert.Equal(t, "D", envTest.GetDomain())
},
},
{
desc: "WithLiveTestRequirements WithDomain without domain",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).
WithDomain(envVarDomain).
WithLiveTestRequirements(envVar02)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.True(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetValue(envVarDomain))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestExtra true",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).
WithLiveTestExtra(func() bool { return true })
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.True(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestExtra false",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).
WithLiveTestExtra(func() bool { return false })
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.False(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestRequirements WithLiveTestExtra true",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).
WithLiveTestRequirements(envVar02).
WithLiveTestExtra(func() bool { return true })
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.True(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestRequirements WithLiveTestExtra false",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).
WithLiveTestRequirements(envVar02).
WithLiveTestExtra(func() bool { return false })
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.False(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestRequirements require env var missing WithLiveTestExtra true",
envVars: map[string]string{
envVar01: "A",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).
WithLiveTestRequirements(envVar02).
WithLiveTestExtra(func() bool { return true })
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.False(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer clearEnv()
applyEnv(test.envVars)
envTest := test.envTestSetup()
test.expected(t, envTest)
})
}
}
func TestEnvTest_RestoreEnv(t *testing.T) {
os.Setenv(envVar01, "A")
os.Setenv(envVar02, "B")
envTest := tester.NewEnvTest(envVar01, envVar02)
clearEnv()
envTest.RestoreEnv()
assert.Equal(t, "A", os.Getenv(envVar01))
assert.Equal(t, "B", os.Getenv(envVar02))
}
func TestEnvTest_ClearEnv(t *testing.T) {
os.Setenv(envVar01, "A")
os.Setenv(envVar02, "B")
os.Setenv("EXTRA_LEGO_TEST", "X")
envTest := tester.NewEnvTest(envVar01, envVar02)
envTest.ClearEnv()
assert.Equal(t, "", os.Getenv(envVar01))
assert.Equal(t, "", os.Getenv(envVar02))
assert.Equal(t, "X", os.Getenv("EXTRA_LEGO_TEST"))
}

View file

@ -4,10 +4,9 @@ import (
"errors"
"testing"
"github.com/cpu/goacmedns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/cpu/goacmedns"
)
var (

View file

@ -1,34 +1,17 @@
package alidns
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestAPIKey string
envTestSecretKey string
envTestDomain string
)
func init() {
envTestAPIKey = os.Getenv("ALICLOUD_ACCESS_KEY")
envTestSecretKey = os.Getenv("ALICLOUD_SECRET_KEY")
envTestDomain = os.Getenv("ALIDNS_DOMAIN")
if len(envTestAPIKey) > 0 && len(envTestSecretKey) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("ALICLOUD_ACCESS_KEY", envTestAPIKey)
os.Setenv("ALICLOUD_SECRET_KEY", envTestSecretKey)
}
var envTest = tester.NewEnvTest(
"ALICLOUD_ACCESS_KEY",
"ALICLOUD_SECRET_KEY").
WithDomain("ALICLOUD_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -71,14 +54,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -124,10 +103,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("ALICLOUD_ACCESS_KEY")
os.Unsetenv("ALICLOUD_SECRET_KEY")
config := NewDefaultConfig()
config.APIKey = test.apiKey
config.SecretKey = test.secretKey
@ -147,29 +122,29 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -5,28 +5,18 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
envTestUserID string
envTestKey string
var envTest = tester.NewEnvTest(
"AURORA_USER_ID",
"AURORA_KEY",
)
func init() {
envTestUserID = os.Getenv("AURORA_USER_ID")
envTestKey = os.Getenv("AURORA_KEY")
}
func restoreEnv() {
os.Setenv("AURORA_USER_ID", envTestUserID)
os.Setenv("AURORA_KEY", envTestKey)
}
func setupTest() (*DNSProvider, *http.ServeMux, func()) {
handler := http.NewServeMux()
server := httptest.NewServer(handler)
@ -85,14 +75,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -142,10 +128,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("AURORA_USER_ID")
os.Unsetenv("AURORA_KEY")
config := NewDefaultConfig()
config.UserID = test.userID
config.Key = test.key

View file

@ -1,43 +1,20 @@
package azure
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestClientID string
envTestClientSecret string
envTestSubscriptionID string
envTestTenantID string
envTestResourceGroup string
envTestDomain string
)
func init() {
envTestClientID = os.Getenv("AZURE_CLIENT_ID")
envTestClientSecret = os.Getenv("AZURE_CLIENT_SECRET")
envTestSubscriptionID = os.Getenv("AZURE_SUBSCRIPTION_ID")
envTestTenantID = os.Getenv("AZURE_TENANT_ID")
envTestResourceGroup = os.Getenv("AZURE_RESOURCE_GROUP")
envTestDomain = os.Getenv("AZURE_DOMAIN")
if len(envTestClientID) > 0 && len(envTestClientSecret) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("AZURE_CLIENT_ID", envTestClientID)
os.Setenv("AZURE_CLIENT_SECRET", envTestClientSecret)
os.Setenv("AZURE_SUBSCRIPTION_ID", envTestSubscriptionID)
os.Setenv("AZURE_TENANT_ID", envTestTenantID)
os.Setenv("AZURE_RESOURCE_GROUP", envTestResourceGroup)
}
var envTest = tester.NewEnvTest(
"AZURE_CLIENT_ID",
"AZURE_CLIENT_SECRET",
"AZURE_SUBSCRIPTION_ID",
"AZURE_TENANT_ID",
"AZURE_RESOURCE_GROUP").
WithDomain("AZURE_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -125,14 +102,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -218,13 +191,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("AZURE_CLIENT_ID")
os.Unsetenv("AZURE_CLIENT_SECRET")
os.Unsetenv("AZURE_SUBSCRIPTION_ID")
os.Unsetenv("AZURE_TENANT_ID")
os.Unsetenv("AZURE_RESOURCE_GROUP")
config := NewDefaultConfig()
config.ClientID = test.clientID
config.ClientSecret = test.clientSecret
@ -246,29 +212,29 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -1,48 +1,20 @@
package bluecat
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestServer string
envTestUserName string
envTestPassword string
envTestConfigName string
envTestDNSView string
envTestDomain string
)
func init() {
envTestServer = os.Getenv("BLUECAT_SERVER_URL")
envTestUserName = os.Getenv("BLUECAT_USER_NAME")
envTestPassword = os.Getenv("BLUECAT_PASSWORD")
envTestDomain = os.Getenv("BLUECAT_DOMAIN")
envTestConfigName = os.Getenv("BLUECAT_CONFIG_NAME")
envTestDNSView = os.Getenv("BLUECAT_DNS_VIEW")
if len(envTestServer) > 0 &&
len(envTestDomain) > 0 &&
len(envTestUserName) > 0 &&
len(envTestPassword) > 0 &&
len(envTestConfigName) > 0 &&
len(envTestDNSView) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("BLUECAT_SERVER_URL", envTestServer)
os.Setenv("BLUECAT_USER_NAME", envTestUserName)
os.Setenv("BLUECAT_PASSWORD", envTestPassword)
os.Setenv("BLUECAT_CONFIG_NAME", envTestConfigName)
os.Setenv("BLUECAT_DNS_VIEW", envTestDNSView)
}
var envTest = tester.NewEnvTest(
"BLUECAT_SERVER_URL",
"BLUECAT_USER_NAME",
"BLUECAT_PASSWORD",
"BLUECAT_CONFIG_NAME",
"BLUECAT_DNS_VIEW").
WithDomain("BLUECAT_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -130,14 +102,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -223,13 +191,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("BLUECAT_SERVER_URL")
os.Unsetenv("BLUECAT_USER_NAME")
os.Unsetenv("BLUECAT_PASSWORD")
os.Unsetenv("BLUECAT_CONFIG_NAME")
os.Unsetenv("BLUECAT_DNS_VIEW")
config := NewDefaultConfig()
config.BaseURL = test.baseURL
config.UserName = test.userName
@ -251,29 +212,29 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
time.Sleep(time.Second * 1)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -1,34 +1,18 @@
package cloudflare
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestEmail string
envTestAPIKey string
envTestDomain string
)
func init() {
envTestEmail = os.Getenv("CLOUDFLARE_EMAIL")
envTestAPIKey = os.Getenv("CLOUDFLARE_API_KEY")
envTestDomain = os.Getenv("CLOUDFLARE_DOMAIN")
if len(envTestEmail) > 0 && len(envTestAPIKey) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("CLOUDFLARE_EMAIL", envTestEmail)
os.Setenv("CLOUDFLARE_API_KEY", envTestAPIKey)
}
var envTest = tester.NewEnvTest(
"CLOUDFLARE_EMAIL",
"CLOUDFLARE_API_KEY").
WithDomain("CLOUDFLARE_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -71,14 +55,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -124,10 +104,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("CLOUDFLARE_EMAIL")
os.Unsetenv("CLOUDFLARE_API_KEY")
config := NewDefaultConfig()
config.AuthEmail = test.authEmail
config.AuthKey = test.authKey
@ -146,36 +122,30 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
}
func TestPresent(t *testing.T) {
if !liveTest {
func TestLivePresent(t *testing.T) {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
config := NewDefaultConfig()
config.AuthEmail = envTestEmail
config.AuthKey = envTestAPIKey
provider, err := NewDNSProviderConfig(config)
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestCleanUp(t *testing.T) {
if !liveTest {
func TestLiveCleanUp(t *testing.T) {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
time.Sleep(time.Second * 2)
config := NewDefaultConfig()
config.AuthEmail = envTestEmail
config.AuthKey = envTestAPIKey
provider, err := NewDNSProviderConfig(config)
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.CleanUp(envTestDomain, "", "123d==")
time.Sleep(2 * time.Second)
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -1,34 +1,17 @@
package cloudxns
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestAPIKey string
envTestSecretKey string
envTestDomain string
)
func init() {
envTestAPIKey = os.Getenv("CLOUDXNS_API_KEY")
envTestSecretKey = os.Getenv("CLOUDXNS_SECRET_KEY")
envTestDomain = os.Getenv("CLOUDXNS_DOMAIN")
if len(envTestAPIKey) > 0 && len(envTestSecretKey) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("CLOUDXNS_API_KEY", envTestAPIKey)
os.Setenv("CLOUDXNS_SECRET_KEY", envTestSecretKey)
}
var envTest = tester.NewEnvTest(
"CLOUDXNS_API_KEY",
"CLOUDXNS_SECRET_KEY").
WithDomain("CLOUDXNS_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -71,14 +54,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -124,10 +103,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("CLOUDXNS_API_KEY")
os.Unsetenv("CLOUDXNS_SECRET_KEY")
config := NewDefaultConfig()
config.APIKey = test.apiKey
config.SecretKey = test.secretKey
@ -146,28 +121,30 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
}
func TestPresent(t *testing.T) {
if !liveTest {
func TestLivePresent(t *testing.T) {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
provider, err := NewDNSProviderCredentials(envTestAPIKey, envTestSecretKey)
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestCleanUp(t *testing.T) {
if !liveTest {
func TestLiveCleanUp(t *testing.T) {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
time.Sleep(time.Second * 2)
provider, err := NewDNSProviderCredentials(envTestAPIKey, envTestSecretKey)
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.CleanUp(envTestDomain, "", "123d==")
time.Sleep(2 * time.Second)
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -5,24 +5,14 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
envTestAuthToken string
)
func init() {
envTestAuthToken = os.Getenv("DO_AUTH_TOKEN")
}
func restoreEnv() {
os.Setenv("DO_AUTH_TOKEN", envTestAuthToken)
}
var envTest = tester.NewEnvTest("DO_AUTH_TOKEN")
func setupTest() (*DNSProvider, *http.ServeMux, func()) {
handler := http.NewServeMux()
@ -63,14 +53,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -104,10 +90,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("DO_AUTH_TOKEN")
os.Unsetenv("ALICLOUD_SECRET_KEY")
config := NewDefaultConfig()
config.AuthToken = test.authToken

View file

@ -8,35 +8,16 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/acme"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestOauthToken string
envTestDomain string
envTestBaseURL string
)
const sandboxURL = "https://api.sandbox.fake.com"
func init() {
envTestOauthToken = os.Getenv("DNSIMPLE_OAUTH_TOKEN")
envTestDomain = os.Getenv("DNSIMPLE_DOMAIN")
envTestBaseURL = "https://api.sandbox.fake.com"
if len(envTestOauthToken) > 0 && len(envTestDomain) > 0 {
baseURL := os.Getenv("DNSIMPLE_BASE_URL")
if baseURL != "" {
envTestBaseURL = baseURL
}
liveTest = true
}
}
func restoreEnv() {
os.Setenv("DNSIMPLE_OAUTH_TOKEN", envTestOauthToken)
os.Setenv("DNSIMPLE_BASE_URL", envTestBaseURL)
}
var envTest = tester.NewEnvTest(
"DNSIMPLE_OAUTH_TOKEN",
"DNSIMPLE_BASE_URL").
WithDomain("DNSIMPLE_DOMAIN").
WithLiveTestRequirements("DNSIMPLE_OAUTH_TOKEN", "DNSIMPLE_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -70,14 +51,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
if test.userAgent != "" {
acme.UserAgent = test.userAgent
@ -132,10 +109,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("DNSIMPLE_OAUTH_TOKEN")
os.Unsetenv("DNSIMPLE_BASE_URL")
config := NewDefaultConfig()
config.AccessToken = test.accessToken
config.BaseURL = test.baseURL
@ -160,29 +133,39 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
if len(os.Getenv("DNSIMPLE_BASE_URL")) == 0 {
os.Setenv("DNSIMPLE_BASE_URL", sandboxURL)
}
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
if len(os.Getenv("DNSIMPLE_BASE_URL")) == 0 {
os.Setenv("DNSIMPLE_BASE_URL", sandboxURL)
}
provider, err := NewDNSProvider()
require.NoError(t, err)
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -5,28 +5,16 @@ import (
"testing"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestAPIKey string
envTestAPISecret string
envTestDomain string
)
var envTest = tester.NewEnvTest(
"DNSMADEEASY_API_KEY",
"DNSMADEEASY_API_SECRET").
WithDomain("DNSMADEEASY_DOMAIN")
func init() {
envTestAPIKey = os.Getenv("DNSMADEEASY_API_KEY")
envTestAPISecret = os.Getenv("DNSMADEEASY_API_SECRET")
envTestDomain = os.Getenv("DNSMADEEASY_DOMAIN")
os.Setenv("DNSMADEEASY_SANDBOX", "true")
liveTest = len(envTestAPIKey) > 0 && len(envTestAPISecret) > 0
}
func restoreEnv() {
os.Setenv("DNSMADEEASY_API_KEY", envTestAPIKey)
os.Setenv("DNSMADEEASY_API_SECRET", envTestAPISecret)
}
func TestNewDNSProvider(t *testing.T) {
@ -70,14 +58,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -123,10 +107,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("DNSMADEEASY_API_KEY")
os.Unsetenv("DNSMADEEASY_API_SECRET")
config := NewDefaultConfig()
config.APIKey = test.apiKey
config.APISecret = test.apiSecret
@ -146,17 +126,17 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresentAndCleanup(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -1,31 +1,15 @@
package dnspod
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestAPIKey string
envTestDomain string
)
func init() {
envTestAPIKey = os.Getenv("DNSPOD_API_KEY")
envTestDomain = os.Getenv("DNSPOD_DOMAIN")
if len(envTestAPIKey) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("DNSPOD_API_KEY", envTestAPIKey)
}
var envTest = tester.NewEnvTest("DNSPOD_API_KEY").
WithDomain("DNSPOD_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -50,14 +34,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -91,9 +71,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("DNSPOD_API_KEY")
config := NewDefaultConfig()
config.LoginToken = test.loginToken
@ -112,29 +89,29 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -4,37 +4,23 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestDomain string
envTestAPIKey string
var envTest = tester.NewEnvTest("DREAMHOST_API_KEY").
WithDomain("DREAMHOST_TEST_DOMAIN")
var (
fakeAPIKey = "asdf1234"
fakeChallengeToken = "foobar"
fakeKeyAuth = "w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI"
)
func init() {
envTestAPIKey = os.Getenv("DREAMHOST_API_KEY")
envTestDomain = os.Getenv("DREAMHOST_TEST_DOMAIN")
if len(envTestAPIKey) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("DREAMHOST_API_KEY", envTestAPIKey)
}
func setupTest() (*DNSProvider, *http.ServeMux, func()) {
handler := http.NewServeMux()
server := httptest.NewServer(handler)
@ -74,14 +60,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -196,19 +178,19 @@ func TestDNSProvider_Cleanup(t *testing.T) {
}
func TestLivePresentAndCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -1,30 +1,15 @@
package duckdns
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestToken string
envTestDomain string
)
func init() {
envTestToken = os.Getenv("DUCKDNS_TOKEN")
envTestDomain = os.Getenv("DUCKDNS_DOMAIN")
if len(envTestToken) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("DUCKDNS_TOKEN", envTestToken)
}
var envTest = tester.NewEnvTest("DUCKDNS_TOKEN").
WithDomain("DUCKDNS_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -49,14 +34,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -89,9 +70,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("DUCKDNS_TOKEN")
config := NewDefaultConfig()
config.Token = test.token
@ -109,29 +87,29 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
time.Sleep(10 * time.Second)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -1,37 +1,18 @@
package dyn
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestCustomerName string
envTestUserName string
envTestPassword string
envTestDomain string
)
func init() {
envTestCustomerName = os.Getenv("DYN_CUSTOMER_NAME")
envTestUserName = os.Getenv("DYN_USER_NAME")
envTestPassword = os.Getenv("DYN_PASSWORD")
envTestDomain = os.Getenv("DYN_DOMAIN")
if len(envTestCustomerName) > 0 && len(envTestUserName) > 0 && len(envTestPassword) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("DYN_CUSTOMER_NAME", envTestCustomerName)
os.Setenv("DYN_USER_NAME", envTestUserName)
os.Setenv("DYN_PASSWORD", envTestPassword)
}
var envTest = tester.NewEnvTest(
"DYN_CUSTOMER_NAME",
"DYN_USER_NAME",
"DYN_PASSWORD").
WithDomain("DYN_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -87,14 +68,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -152,9 +129,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("DNSPOD_API_KEY")
config := NewDefaultConfig()
config.CustomerName = test.customerName
config.Password = test.password
@ -174,29 +148,29 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -0,0 +1,158 @@
package exec
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/log"
)
func TestDNSProvider_Present(t *testing.T) {
backupLogger := log.Logger
defer func() {
log.Logger = backupLogger
}()
logRecorder := &LogRecorder{}
log.Logger = logRecorder
type expected struct {
args string
error bool
}
testCases := []struct {
desc string
config *Config
expected expected
}{
{
desc: "Standard mode",
config: &Config{
Program: "echo",
Mode: "",
},
expected: expected{
args: "present _acme-challenge.domain. pW9ZKG0xz_PCriK-nCMOjADy9eJcgGWIzkkj2fN4uZM 120\n",
},
},
{
desc: "program error",
config: &Config{
Program: "ogellego",
Mode: "",
},
expected: expected{error: true},
},
{
desc: "Raw mode",
config: &Config{
Program: "echo",
Mode: "RAW",
},
expected: expected{
args: "present -- domain token keyAuth\n",
},
},
}
var message string
logRecorder.On("Println", mock.Anything).Run(func(args mock.Arguments) {
message = args.String(0)
fmt.Fprintln(os.Stdout, "XXX", message)
})
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
message = ""
provider, err := NewDNSProviderConfig(test.config)
require.NoError(t, err)
err = provider.Present("domain", "token", "keyAuth")
if test.expected.error {
require.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, test.expected.args, message)
}
})
}
}
func TestDNSProvider_CleanUp(t *testing.T) {
backupLogger := log.Logger
defer func() {
log.Logger = backupLogger
}()
logRecorder := &LogRecorder{}
log.Logger = logRecorder
type expected struct {
args string
error bool
}
testCases := []struct {
desc string
config *Config
expected expected
}{
{
desc: "Standard mode",
config: &Config{
Program: "echo",
Mode: "",
},
expected: expected{
args: "cleanup _acme-challenge.domain. pW9ZKG0xz_PCriK-nCMOjADy9eJcgGWIzkkj2fN4uZM 120\n",
},
},
{
desc: "program error",
config: &Config{
Program: "ogellego",
Mode: "",
},
expected: expected{error: true},
},
{
desc: "Raw mode",
config: &Config{
Program: "echo",
Mode: "RAW",
},
expected: expected{
args: "cleanup -- domain token keyAuth\n",
},
},
}
var message string
logRecorder.On("Println", mock.Anything).Run(func(args mock.Arguments) {
message = args.String(0)
fmt.Fprintln(os.Stdout, "XXX", message)
})
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
message = ""
provider, err := NewDNSProviderConfig(test.config)
require.NoError(t, err)
err = provider.CleanUp("domain", "token", "keyAuth")
if test.expected.error {
require.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, test.expected.args, message)
}
})
}
}

View file

@ -0,0 +1,31 @@
package exec
import "github.com/stretchr/testify/mock"
type LogRecorder struct {
mock.Mock
}
func (*LogRecorder) Fatal(args ...interface{}) {
panic("implement me")
}
func (*LogRecorder) Fatalln(args ...interface{}) {
panic("implement me")
}
func (*LogRecorder) Fatalf(format string, args ...interface{}) {
panic("implement me")
}
func (*LogRecorder) Print(args ...interface{}) {
panic("implement me")
}
func (l *LogRecorder) Println(args ...interface{}) {
l.Called(args...)
}
func (*LogRecorder) Printf(format string, args ...interface{}) {
panic("implement me")
}

View file

@ -1,35 +1,18 @@
package exoscale
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestAPIKey string
envTestAPISecret string
envTestDomain string
)
func init() {
envTestAPISecret = os.Getenv("EXOSCALE_API_SECRET")
envTestAPIKey = os.Getenv("EXOSCALE_API_KEY")
envTestDomain = os.Getenv("EXOSCALE_DOMAIN")
if len(envTestAPIKey) > 0 && len(envTestAPISecret) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("EXOSCALE_API_KEY", envTestAPIKey)
os.Setenv("EXOSCALE_API_SECRET", envTestAPISecret)
}
var envTest = tester.NewEnvTest(
"EXOSCALE_API_SECRET",
"EXOSCALE_API_KEY").
WithDomain("EXOSCALE_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -72,14 +55,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -125,10 +104,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("EXOSCALE_API_KEY")
os.Unsetenv("EXOSCALE_API_SECRET")
config := NewDefaultConfig()
config.APIKey = test.apiKey
config.APISecret = test.apiSecret
@ -200,33 +175,33 @@ func TestDNSProvider_FindZoneAndRecordName(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
// Present Twice to handle create / update
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -1,41 +1,20 @@
package fastdns
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestHost string
envTestClientToken string
envTestClientSecret string
envTestAccessToken string
envTestDomain string
)
func init() {
envTestHost = os.Getenv("AKAMAI_HOST")
envTestClientToken = os.Getenv("AKAMAI_CLIENT_TOKEN")
envTestClientSecret = os.Getenv("AKAMAI_CLIENT_SECRET")
envTestAccessToken = os.Getenv("AKAMAI_ACCESS_TOKEN")
envTestDomain = os.Getenv("AKAMAI_TEST_DOMAIN")
if len(envTestHost) > 0 && len(envTestClientToken) > 0 && len(envTestClientSecret) > 0 && len(envTestAccessToken) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("AKAMAI_HOST", envTestHost)
os.Setenv("AKAMAI_CLIENT_TOKEN", envTestClientToken)
os.Setenv("AKAMAI_CLIENT_SECRET", envTestClientSecret)
os.Setenv("AKAMAI_ACCESS_TOKEN", envTestAccessToken)
}
var envTest = tester.NewEnvTest(
"AKAMAI_HOST",
"AKAMAI_CLIENT_TOKEN",
"AKAMAI_CLIENT_SECRET",
"AKAMAI_ACCESS_TOKEN").
WithDomain("AKAMAI_TEST_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -106,14 +85,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -184,12 +159,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("AKAMAI_HOST")
os.Unsetenv("AKAMAI_CLIENT_TOKEN")
os.Unsetenv("AKAMAI_CLIENT_SECRET")
os.Unsetenv("AKAMAI_ACCESS_TOKEN")
config := NewDefaultConfig()
config.ClientToken = test.clientToken
config.ClientSecret = test.clientSecret
@ -264,38 +233,33 @@ func TestDNSProvider_findZoneAndRecordName(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
// Present Twice to handle create / update
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
time.Sleep(time.Second * 1)
config := NewDefaultConfig()
config.Host = envTestHost
config.ClientToken = envTestClientToken
config.ClientSecret = envTestClientSecret
config.AccessToken = envTestAccessToken
provider, err := NewDNSProviderConfig(config)
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.CleanUp(envTestDomain, "", "123d==")
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -5,25 +5,15 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
envTestAPIKey string
)
func init() {
envTestAPIKey = os.Getenv("GANDI_API_KEY")
}
func restoreEnv() {
os.Setenv("GANDI_API_KEY", envTestAPIKey)
}
var envTest = tester.NewEnvTest("GANDI_API_KEY")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -48,14 +38,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -90,9 +76,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("GANDI_API_KEY")
config := NewDefaultConfig()
config.APIKey = test.apiKey

View file

@ -5,25 +5,15 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"regexp"
"testing"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/log"
"github.com/xenolf/lego/platform/tester"
)
var (
envTestAPIKey string
)
func init() {
envTestAPIKey = os.Getenv("GANDIV5_API_KEY")
}
func restoreEnv() {
os.Setenv("GANDIV5_API_KEY", envTestAPIKey)
}
var envTest = tester.NewEnvTest("GANDIV5_API_KEY")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -48,14 +38,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -89,9 +75,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("GANDIV5_API_KEY")
config := NewDefaultConfig()
config.APIKey = test.apiKey

View file

@ -1,41 +1,25 @@
package gcloud
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/api/dns/v1"
)
var (
liveTest bool
envTestProject string
envTestServiceAccountFile string
envTestGoogleApplicationCredentials string
envTestDomain string
)
func init() {
envTestProject = os.Getenv("GCE_PROJECT")
envTestServiceAccountFile = os.Getenv("GCE_SERVICE_ACCOUNT_FILE")
envTestGoogleApplicationCredentials = os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
envTestDomain = os.Getenv("GCE_DOMAIN")
_, err := google.DefaultClient(context.Background(), dns.NdevClouddnsReadwriteScope)
if err == nil && len(envTestProject) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("GCE_PROJECT", envTestProject)
os.Setenv("GCE_SERVICE_ACCOUNT_FILE", envTestServiceAccountFile)
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", envTestGoogleApplicationCredentials)
}
var envTest = tester.NewEnvTest(
"GCE_PROJECT",
"GCE_SERVICE_ACCOUNT_FILE",
"GOOGLE_APPLICATION_CREDENTIALS").
WithDomain("GCE_DOMAIN").
WithLiveTestExtra(func() bool {
_, err := google.DefaultClient(context.Background(), dns.NdevClouddnsReadwriteScope)
return err == nil
})
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -72,15 +56,10 @@ func TestNewDNSProvider(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
defer envTest.RestoreEnv()
envTest.ClearEnv()
for key, value := range test.envVars {
if len(value) == 0 {
os.Unsetenv(key)
} else {
os.Setenv(key, value)
}
}
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -115,9 +94,8 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("GCE_PROJECT")
os.Unsetenv("GCE_SERVICE_ACCOUNT_FILE")
defer envTest.RestoreEnv()
envTest.ClearEnv()
config := NewDefaultConfig()
config.Project = test.project
@ -137,43 +115,49 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
provider, err := NewDNSProviderCredentials(envTestProject)
envTest.RestoreEnv()
provider, err := NewDNSProviderCredentials(envTest.GetValue("GCE_PROJECT"))
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLivePresentMultiple(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
provider, err := NewDNSProviderCredentials(envTestProject)
envTest.RestoreEnv()
provider, err := NewDNSProviderCredentials(envTest.GetValue("GCE_PROJECT"))
require.NoError(t, err)
// Check that we're able to create multiple entries
err = provider.Present(envTestDomain, "1", "123d==")
err = provider.Present(envTest.GetDomain(), "1", "123d==")
require.NoError(t, err)
err = provider.Present(envTestDomain, "2", "123d==")
err = provider.Present(envTest.GetDomain(), "2", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
provider, err := NewDNSProviderCredentials(envTestProject)
envTest.RestoreEnv()
provider, err := NewDNSProviderCredentials(envTest.GetValue("GCE_PROJECT"))
require.NoError(t, err)
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -1,32 +1,16 @@
package glesys
import (
"os"
"testing"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestAPIUser string
envTestAPIKey string
envTestDomain string
)
func init() {
envTestAPIUser = os.Getenv("GLESYS_API_USER")
envTestAPIKey = os.Getenv("GLESYS_API_KEY")
if len(envTestAPIUser) > 0 && len(envTestAPIKey) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("GLESYS_API_USER", envTestAPIUser)
os.Setenv("GLESYS_API_KEY", envTestAPIKey)
}
var envTest = tester.NewEnvTest(
"GLESYS_API_USER",
"GLESYS_API_KEY").
WithDomain("GLESYS_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -69,14 +53,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -124,10 +104,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("GLESYS_API_USER")
os.Unsetenv("GLESYS_API_KEY")
config := NewDefaultConfig()
config.APIKey = test.apiKey
config.APIUser = test.apiUser
@ -147,27 +123,27 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -1,33 +1,16 @@
package godaddy
import (
"os"
"testing"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestAPIKey string
envTestAPISecret string
envTestDomain string
)
func init() {
envTestAPIKey = os.Getenv("GODADDY_API_KEY")
envTestAPISecret = os.Getenv("GODADDY_API_SECRET")
envTestDomain = os.Getenv("GODADDY_DOMAIN")
if len(envTestAPIKey) > 0 && len(envTestAPISecret) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("GODADDY_API_KEY", envTestAPIKey)
os.Setenv("GODADDY_API_SECRET", envTestAPISecret)
}
var envTest = tester.NewEnvTest(
"GODADDY_API_KEY",
"GODADDY_API_SECRET").
WithDomain("GODADDY_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -70,14 +53,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -122,10 +101,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("GODADDY_API_KEY")
os.Unsetenv("GODADDY_API_SECRET")
config := NewDefaultConfig()
config.APIKey = test.apiKey
config.APISecret = test.apiSecret
@ -144,27 +119,27 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -1,33 +1,17 @@
package hostingde
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestAPIKey string
envTestZone string
envTestDomain string
)
func init() {
envTestAPIKey = os.Getenv("HOSTINGDE_API_KEY")
envTestZone = os.Getenv("HOSTINGDE_ZONE_NAME")
envTestDomain = os.Getenv("HOSTINGDE_DOMAIN")
if len(envTestZone) > 0 && len(envTestAPIKey) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("HOSTINGDE_ZONE_NAME", envTestZone)
os.Setenv("HOSTINGDE_API_KEY", envTestAPIKey)
}
var envTest = tester.NewEnvTest(
"HOSTINGDE_API_KEY",
"HOSTINGDE_ZONE_NAME").
WithDomain("HOSTINGDE_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -70,14 +54,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -123,10 +103,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("HOSTINGDE_API_KEY")
os.Unsetenv("HOSTINGDE_ZONE_NAME")
config := NewDefaultConfig()
config.APIKey = test.apiKey
config.ZoneName = test.zoneName
@ -146,29 +122,29 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
time.Sleep(2 * time.Second)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -1,38 +1,18 @@
package iij
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestAPIAccessKey string
envTestAPISecretKeyEnv string
envTestDoServiceCodeEnv string
envTestDomain string
)
func init() {
envTestAPIAccessKey = os.Getenv("IIJ_API_ACCESS_KEY")
envTestAPISecretKeyEnv = os.Getenv("IIJ_API_SECRET_KEY")
envTestDoServiceCodeEnv = os.Getenv("IIJ_DO_SERVICE_CODE")
envTestDomain = os.Getenv("IIJ_API_TESTDOMAIN")
if len(envTestAPIAccessKey) > 0 && len(envTestAPISecretKeyEnv) > 0 && len(envTestDoServiceCodeEnv) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("IIJ_API_ACCESS_KEY", envTestAPIAccessKey)
os.Setenv("IIJ_API_SECRET_KEY", envTestAPISecretKeyEnv)
os.Setenv("IIJ_DO_SERVICE_CODE", envTestDoServiceCodeEnv)
}
var envTest = tester.NewEnvTest(
"IIJ_API_ACCESS_KEY",
"IIJ_API_SECRET_KEY",
"IIJ_DO_SERVICE_CODE").
WithDomain("IIJ_API_TESTDOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -88,14 +68,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -154,11 +130,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("IIJ_API_ACCESS_KEY")
os.Unsetenv("IIJ_API_SECRET_KEY")
os.Unsetenv("IIJ_DO_SERVICE_CODE")
config := NewDefaultConfig()
config.AccessKey = test.accessKey
config.SecretKey = test.secretKey
@ -232,27 +203,27 @@ func TestSplitDomain(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -1,8 +1,6 @@
package lightsail
import (
"fmt"
"os"
"testing"
"github.com/aws/aws-sdk-go/aws"
@ -12,15 +10,16 @@ import (
)
func TestLiveTTL(t *testing.T) {
m, err := testGetAndPreCheck()
if err != nil {
t.Skip(err.Error())
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
domain := m["lightsailDomain"]
domain := envTest.GetDomain()
err = provider.Present(domain, "foo", "bar")
require.NoError(t, err)
@ -50,24 +49,10 @@ func TestLiveTTL(t *testing.T) {
entries := resp.Domain.DomainEntries
for _, entry := range entries {
if *entry.Type == "TXT" && *entry.Name == fqdn {
if aws.StringValue(entry.Type) == "TXT" && aws.StringValue(entry.Name) == fqdn {
return
}
}
t.Fatalf("Could not find a TXT record for _acme-challenge.%s", domain)
}
func testGetAndPreCheck() (map[string]string, error) {
m := map[string]string{
"lightsailKey": os.Getenv("AWS_ACCESS_KEY_ID"),
"lightsailSecret": os.Getenv("AWS_SECRET_ACCESS_KEY"),
"lightsailDomain": os.Getenv("DNS_ZONE"),
}
for _, v := range m {
if v == "" {
return nil, fmt.Errorf("AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and R53_DOMAIN are needed to run this test")
}
}
return m, nil
}

View file

@ -10,25 +10,16 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/lightsail"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
envTestSecret string
envTestKey string
envTestZone string
)
func init() {
envTestKey = os.Getenv("AWS_ACCESS_KEY_ID")
envTestSecret = os.Getenv("AWS_SECRET_ACCESS_KEY")
}
func restoreEnv() {
os.Setenv("AWS_ACCESS_KEY_ID", envTestKey)
os.Setenv("AWS_SECRET_ACCESS_KEY", envTestSecret)
os.Setenv("AWS_REGION", "us-east-1")
os.Setenv("AWS_HOSTED_ZONE_ID", envTestZone)
}
var envTest = tester.NewEnvTest(
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"AWS_REGION",
"AWS_HOSTED_ZONE_ID").
WithDomain("DNS_ZONE").
WithLiveTestRequirements("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "DNS_ZONE")
func makeProvider(ts *httptest.Server) (*DNSProvider, error) {
config := &aws.Config{
@ -50,7 +41,9 @@ func makeProvider(ts *httptest.Server) (*DNSProvider, error) {
}
func TestCredentialsFromEnv(t *testing.T) {
defer restoreEnv()
defer envTest.RestoreEnv()
envTest.ClearEnv()
os.Setenv("AWS_ACCESS_KEY_ID", "123")
os.Setenv("AWS_SECRET_ACCESS_KEY", "123")
os.Setenv("AWS_REGION", "us-east-1")

View file

@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/timewasted/linode"
"github.com/timewasted/linode/dns"
"github.com/xenolf/lego/platform/tester"
)
type (
@ -28,20 +29,7 @@ type (
MockResponseMap map[string]MockResponse
)
var (
apiKey string
liveTest bool
)
func init() {
apiKey = os.Getenv("LINODE_API_KEY")
liveTest = len(apiKey) != 0
}
func restoreEnv() {
os.Setenv("LINODE_API_KEY", apiKey)
}
var envTest = tester.NewEnvTest("LINODE_API_KEY")
func newMockServer(responses MockResponseMap) *httptest.Server {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -103,14 +91,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -144,9 +128,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("LINODE_API_KEY")
config := NewDefaultConfig()
config.APIKey = test.apiKey
@ -165,7 +146,7 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestDNSProvider_Present(t *testing.T) {
defer restoreEnv()
defer envTest.RestoreEnv()
os.Setenv("LINODE_API_KEY", "testing")
p, err := NewDNSProvider()
@ -252,7 +233,7 @@ func TestDNSProvider_Present(t *testing.T) {
}
func TestDNSProvider_CleanUp(t *testing.T) {
defer restoreEnv()
defer envTest.RestoreEnv()
os.Setenv("LINODE_API_KEY", "testing")
p, err := NewDNSProvider()
@ -363,14 +344,14 @@ func TestDNSProvider_CleanUp(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("Skipping live test")
}
// TODO implement this test
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("Skipping live test")
}
// TODO implement this test

View file

@ -12,26 +12,14 @@ import (
"github.com/linode/linodego"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
type (
MockResponseMap map[string]interface{}
)
var (
liveTest bool
envTestAPIToken string
)
func init() {
envTestAPIToken = os.Getenv("LINODE_TOKEN")
liveTest = len(envTestAPIToken) != 0
}
func restoreEnv() {
os.Setenv("LINODE_TOKEN", envTestAPIToken)
}
var envTest = tester.NewEnvTest("LINODE_TOKEN")
func newMockServer(responses MockResponseMap) *httptest.Server {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -94,14 +82,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -135,9 +119,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("LINODE_TOKEN")
config := NewDefaultConfig()
config.Token = test.apiKey
@ -156,7 +137,7 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestDNSProvider_Present(t *testing.T) {
defer restoreEnv()
defer envTest.RestoreEnv()
os.Setenv("LINODE_TOKEN", "testing")
p, err := NewDNSProvider()
@ -246,7 +227,7 @@ func TestDNSProvider_Present(t *testing.T) {
}
func TestDNSProvider_CleanUp(t *testing.T) {
defer restoreEnv()
defer envTest.RestoreEnv()
os.Setenv("LINODE_TOKEN", "testing")
p, err := NewDNSProvider()
@ -361,14 +342,14 @@ func TestDNSProvider_CleanUp(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("Skipping live test")
}
// TODO implement this test
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("Skipping live test")
}
// TODO implement this test

View file

@ -1,34 +1,17 @@
package namedotcom
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestUsername string
envTestAPIToken string
envTestDomain string
)
func init() {
envTestUsername = os.Getenv("NAMECOM_USERNAME")
envTestAPIToken = os.Getenv("NAMECOM_API_TOKEN")
envTestDomain = os.Getenv("NAMEDOTCOM_DOMAIN")
if len(envTestAPIToken) > 0 && len(envTestUsername) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("NAMECOM_USERNAME", envTestUsername)
os.Setenv("NAMECOM_API_TOKEN", envTestAPIToken)
}
var envTest = tester.NewEnvTest(
"NAMECOM_USERNAME",
"NAMECOM_API_TOKEN").
WithDomain("NAMEDOTCOM_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -71,14 +54,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -126,10 +105,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("NAMECOM_USERNAME")
os.Unsetenv("NAMECOM_API_TOKEN")
config := NewDefaultConfig()
config.Username = test.username
config.APIToken = test.apiToken
@ -149,29 +124,29 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -11,12 +11,17 @@ import (
)
func TestLiveClientAuth(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
// Setup
client := NewClient(envTestCustomerNumber, envTestAPIKey, envTestAPIPassword)
envTest.RestoreEnv()
client := NewClient(
envTest.GetValue("NETCUP_CUSTOMER_NUMBER"),
envTest.GetValue("NETCUP_API_KEY"),
envTest.GetValue("NETCUP_API_PASSWORD"))
for i := 1; i < 4; i++ {
i := i
@ -34,17 +39,22 @@ func TestLiveClientAuth(t *testing.T) {
}
func TestLiveClientGetDnsRecords(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
client := NewClient(envTestCustomerNumber, envTestAPIKey, envTestAPIPassword)
// Setup
envTest.RestoreEnv()
client := NewClient(
envTest.GetValue("NETCUP_CUSTOMER_NUMBER"),
envTest.GetValue("NETCUP_API_KEY"),
envTest.GetValue("NETCUP_API_PASSWORD"))
sessionID, err := client.Login()
require.NoError(t, err)
fqdn, _, _ := acme.DNS01Record(envTestDomain, "123d==")
fqdn, _, _ := acme.DNS01Record(envTest.GetDomain(), "123d==")
zone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
require.NoError(t, err, "error finding DNSZone")
@ -61,17 +71,22 @@ func TestLiveClientGetDnsRecords(t *testing.T) {
}
func TestLiveClientUpdateDnsRecord(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
// Setup
client := NewClient(envTestCustomerNumber, envTestAPIKey, envTestAPIPassword)
envTest.RestoreEnv()
client := NewClient(
envTest.GetValue("NETCUP_CUSTOMER_NUMBER"),
envTest.GetValue("NETCUP_API_KEY"),
envTest.GetValue("NETCUP_API_PASSWORD"))
sessionID, err := client.Login()
require.NoError(t, err)
fqdn, _, _ := acme.DNS01Record(envTestDomain, "123d==")
fqdn, _, _ := acme.DNS01Record(envTest.GetDomain(), "123d==")
zone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
require.NoError(t, err, fmt.Errorf("error finding DNSZone, %v", err))
@ -100,7 +115,7 @@ func TestLiveClientUpdateDnsRecord(t *testing.T) {
records[recordIdx].DeleteRecord = true
// Tear down
err = client.UpdateDNSRecord(sessionID, envTestDomain, records[recordIdx])
err = client.UpdateDNSRecord(sessionID, envTest.GetDomain(), records[recordIdx])
require.NoError(t, err, "Did not remove record! Please do so yourself.")
err = client.Logout(sessionID)

View file

@ -2,37 +2,18 @@ package netcup
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/acme"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestCustomerNumber string
envTestAPIKey string
envTestAPIPassword string
envTestDomain string
)
func init() {
envTestCustomerNumber = os.Getenv("NETCUP_CUSTOMER_NUMBER")
envTestAPIKey = os.Getenv("NETCUP_API_KEY")
envTestAPIPassword = os.Getenv("NETCUP_API_PASSWORD")
envTestDomain = os.Getenv("NETCUP_DOMAIN")
if len(envTestCustomerNumber) > 0 && len(envTestAPIKey) > 0 && len(envTestAPIPassword) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("NETCUP_CUSTOMER_NUMBER", envTestCustomerNumber)
os.Setenv("NETCUP_API_KEY", envTestAPIKey)
os.Setenv("NETCUP_API_PASSWORD", envTestAPIPassword)
}
var envTest = tester.NewEnvTest(
"NETCUP_CUSTOMER_NUMBER",
"NETCUP_API_KEY",
"NETCUP_API_PASSWORD").
WithDomain("NETCUP_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -88,14 +69,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -154,11 +131,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("NETCUP_CUSTOMER_NUMBER")
os.Unsetenv("NETCUP_API_KEY")
os.Unsetenv("NETCUP_API_PASSWORD")
config := NewDefaultConfig()
config.Customer = test.customer
config.Key = test.key
@ -179,15 +151,15 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresentAndCleanup(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
p, err := NewDNSProvider()
require.NoError(t, err)
fqdn, _, _ := acme.DNS01Record(envTestDomain, "123d==")
fqdn, _, _ := acme.DNS01Record(envTest.GetDomain(), "123d==")
zone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
require.NoError(t, err, "error finding DNSZone")

View file

@ -1,34 +1,17 @@
package nifcloud
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestAccessKey string
envTestSecretKey string
envTestDomain string
)
func init() {
envTestAccessKey = os.Getenv("NIFCLOUD_ACCESS_KEY_ID")
envTestSecretKey = os.Getenv("NIFCLOUD_SECRET_ACCESS_KEY")
envTestDomain = os.Getenv("NIFCLOUD_DOMAIN")
if len(envTestAccessKey) > 0 && len(envTestSecretKey) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("NIFCLOUD_ACCESS_KEY_ID", envTestAccessKey)
os.Setenv("NIFCLOUD_SECRET_ACCESS_KEY", envTestSecretKey)
}
var envTest = tester.NewEnvTest(
"NIFCLOUD_ACCESS_KEY_ID",
"NIFCLOUD_SECRET_ACCESS_KEY").
WithDomain("NIFCLOUD_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -71,14 +54,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -124,10 +103,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("NIFCLOUD_ACCESS_KEY_ID")
os.Unsetenv("NIFCLOUD_SECRET_ACCESS_KEY")
config := NewDefaultConfig()
config.AccessKey = test.accessKey
config.SecretKey = test.secretKey
@ -147,29 +122,29 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -1,31 +1,16 @@
package ns1
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestAPIKey string
envTestDomain string
)
func init() {
envTestAPIKey = os.Getenv("NS1_API_KEY")
envTestDomain = os.Getenv("NS1_DOMAIN")
if len(envTestAPIKey) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("NS1_API_KEY", envTestAPIKey)
}
var envTest = tester.NewEnvTest("NS1_API_KEY").
WithDomain("NS1_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -50,14 +35,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -91,9 +72,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("NS1_API_KEY")
config := NewDefaultConfig()
config.APIKey = test.apiKey
@ -163,29 +141,29 @@ func Test_getAuthZone(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -10,39 +10,37 @@ import (
"github.com/stretchr/testify/assert"
)
var fakeOTCUserName = "test"
var fakeOTCPassword = "test"
var fakeOTCDomainName = "test"
var fakeOTCProjectName = "test"
var fakeOTCToken = "62244bc21da68d03ebac94e6636ff01f"
// DNSMock mock
type DNSMock struct {
// DNSServerMock mock
type DNSServerMock struct {
t *testing.T
Server *httptest.Server
server *httptest.Server
Mux *http.ServeMux
}
// NewDNSMock create a new DNSMock
func NewDNSMock(t *testing.T) *DNSMock {
return &DNSMock{
t: t,
// NewDNSServerMock create a new DNSServerMock
func NewDNSServerMock(t *testing.T) *DNSServerMock {
mux := http.NewServeMux()
return &DNSServerMock{
t: t,
server: httptest.NewServer(mux),
Mux: mux,
}
}
// Setup creates the mock server
func (m *DNSMock) Setup() {
m.Mux = http.NewServeMux()
m.Server = httptest.NewServer(m.Mux)
func (m *DNSServerMock) GetServerURL() string {
return m.server.URL
}
// ShutdownServer creates the mock server
func (m *DNSMock) ShutdownServer() {
m.Server.Close()
func (m *DNSServerMock) ShutdownServer() {
m.server.Close()
}
// HandleAuthSuccessfully Handle auth successfully
func (m *DNSMock) HandleAuthSuccessfully() {
func (m *DNSServerMock) HandleAuthSuccessfully() {
m.Mux.HandleFunc("/v3/auth/token", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Subject-Token", fakeOTCToken)
@ -64,99 +62,101 @@ func (m *DNSMock) HandleAuthSuccessfully() {
]
}
]
}}`, m.Server.URL)
}}`, m.server.URL)
})
}
// HandleListZonesSuccessfully Handle list zones successfully
func (m *DNSMock) HandleListZonesSuccessfully() {
func (m *DNSServerMock) HandleListZonesSuccessfully() {
m.Mux.HandleFunc("/v2/zones", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(m.t, r.Method, http.MethodGet)
assert.Equal(m.t, r.URL.Path, "/v2/zones")
assert.Equal(m.t, r.URL.RawQuery, "name=example.com.")
assert.Equal(m.t, r.Header.Get("Content-Type"), "application/json")
fmt.Fprintf(w, `{
"zones":[{
"id":"123123"
}]}
`)
assert.Equal(m.t, r.Method, http.MethodGet)
assert.Equal(m.t, r.URL.Path, "/v2/zones")
assert.Equal(m.t, r.URL.RawQuery, "name=example.com.")
assert.Equal(m.t, r.Header.Get("Content-Type"), "application/json")
})
}
// HandleListZonesEmpty Handle list zones empty
func (m *DNSMock) HandleListZonesEmpty() {
func (m *DNSServerMock) HandleListZonesEmpty() {
m.Mux.HandleFunc("/v2/zones", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{
"zones":[
]}
`)
assert.Equal(m.t, r.Method, http.MethodGet)
assert.Equal(m.t, r.URL.Path, "/v2/zones")
assert.Equal(m.t, r.URL.RawQuery, "name=example.com.")
assert.Equal(m.t, r.Header.Get("Content-Type"), "application/json")
fmt.Fprintf(w, `{
"zones":[
]}
`)
})
}
// HandleDeleteRecordsetsSuccessfully Handle delete recordsets successfully
func (m *DNSMock) HandleDeleteRecordsetsSuccessfully() {
func (m *DNSServerMock) HandleDeleteRecordsetsSuccessfully() {
m.Mux.HandleFunc("/v2/zones/123123/recordsets/321321", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(m.t, r.Method, http.MethodDelete)
assert.Equal(m.t, r.URL.Path, "/v2/zones/123123/recordsets/321321")
assert.Equal(m.t, r.Header.Get("Content-Type"), "application/json")
fmt.Fprintf(w, `{
"zones":[{
"id":"123123"
}]}
`)
assert.Equal(m.t, r.Method, http.MethodDelete)
assert.Equal(m.t, r.URL.Path, "/v2/zones/123123/recordsets/321321")
assert.Equal(m.t, r.Header.Get("Content-Type"), "application/json")
})
}
// HandleListRecordsetsEmpty Handle list recordsets empty
func (m *DNSMock) HandleListRecordsetsEmpty() {
func (m *DNSServerMock) HandleListRecordsetsEmpty() {
m.Mux.HandleFunc("/v2/zones/123123/recordsets", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(m.t, r.URL.Path, "/v2/zones/123123/recordsets")
assert.Equal(m.t, r.URL.RawQuery, "type=TXT&name=_acme-challenge.example.com.")
fmt.Fprintf(w, `{
"recordsets":[
]}
`)
assert.Equal(m.t, r.URL.Path, "/v2/zones/123123/recordsets")
assert.Equal(m.t, r.URL.RawQuery, "type=TXT&name=_acme-challenge.example.com.")
})
}
// HandleListRecordsetsSuccessfully Handle list recordsets successfully
func (m *DNSMock) HandleListRecordsetsSuccessfully() {
func (m *DNSServerMock) HandleListRecordsetsSuccessfully() {
m.Mux.HandleFunc("/v2/zones/123123/recordsets", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
assert.Equal(m.t, r.URL.Path, "/v2/zones/123123/recordsets")
assert.Equal(m.t, r.URL.RawQuery, "type=TXT&name=_acme-challenge.example.com.")
assert.Equal(m.t, r.Header.Get("Content-Type"), "application/json")
fmt.Fprintf(w, `{
"recordsets":[{
"id":"321321"
}]}
`)
return
}
assert.Equal(m.t, r.URL.Path, "/v2/zones/123123/recordsets")
assert.Equal(m.t, r.URL.RawQuery, "type=TXT&name=_acme-challenge.example.com.")
if r.Method == http.MethodPost {
assert.Equal(m.t, r.Header.Get("Content-Type"), "application/json")
} else if r.Method == http.MethodPost {
body, err := ioutil.ReadAll(r.Body)
assert.Nil(m.t, err)
exceptedString := "{\"name\":\"_acme-challenge.example.com.\",\"description\":\"Added TXT record for ACME dns-01 challenge using lego client\",\"type\":\"TXT\",\"ttl\":300,\"records\":[\"\\\"w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI\\\"\"]}"
assert.Equal(m.t, string(body), exceptedString)
fmt.Fprintf(w, `{
"recordsets":[{
"id":"321321"
}]}
`)
} else {
m.t.Errorf("Expected method to be 'GET' or 'POST' but got '%s'", r.Method)
return
}
assert.Equal(m.t, r.Header.Get("Content-Type"), "application/json")
http.Error(w, fmt.Sprintf("Expected method to be 'GET' or 'POST' but got '%s'", r.Method), http.StatusBadRequest)
})
}

View file

@ -5,125 +5,135 @@ import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/xenolf/lego/platform/tester"
)
type TestSuite struct {
type OTCSuite struct {
suite.Suite
Mock *DNSMock
Mock *DNSServerMock
envTest *tester.EnvTest
}
func (s *TestSuite) TearDownSuite() {
func (s *OTCSuite) SetupTest() {
s.Mock = NewDNSServerMock(s.T())
s.Mock.HandleAuthSuccessfully()
s.envTest = tester.NewEnvTest(
"OTC_DOMAIN_NAME",
"OTC_USER_NAME",
"OTC_PASSWORD",
"OTC_PROJECT_NAME",
"OTC_IDENTITY_ENDPOINT",
)
}
func (s *OTCSuite) TearDownTest() {
s.envTest.RestoreEnv()
s.Mock.ShutdownServer()
}
func (s *TestSuite) SetupTest() {
s.Mock = NewDNSMock(s.T())
s.Mock.Setup()
s.Mock.HandleAuthSuccessfully()
}
func TestTestSuite(t *testing.T) {
suite.Run(t, new(TestSuite))
suite.Run(t, new(OTCSuite))
}
func (s *TestSuite) createDNSProvider() (*DNSProvider, error) {
url := fmt.Sprintf("%s/v3/auth/token", s.Mock.Server.URL)
func (s *OTCSuite) createDNSProvider() (*DNSProvider, error) {
config := NewDefaultConfig()
config.UserName = fakeOTCUserName
config.Password = fakeOTCPassword
config.DomainName = fakeOTCDomainName
config.ProjectName = fakeOTCProjectName
config.IdentityEndpoint = url
config.UserName = "UserName"
config.Password = "Password"
config.DomainName = "DomainName"
config.ProjectName = "ProjectName"
config.IdentityEndpoint = fmt.Sprintf("%s/v3/auth/token", s.Mock.GetServerURL())
return NewDNSProviderConfig(config)
}
func (s *TestSuite) TestLogin() {
func (s *OTCSuite) TestLogin() {
provider, err := s.createDNSProvider()
require.NoError(s.T(), err)
s.Require().NoError(err)
err = provider.loginRequest()
require.NoError(s.T(), err)
assert.Equal(s.T(), provider.baseURL, fmt.Sprintf("%s/v2", s.Mock.Server.URL))
assert.Equal(s.T(), fakeOTCToken, provider.token)
s.Require().NoError(err)
s.Equal(provider.baseURL, fmt.Sprintf("%s/v2", s.Mock.GetServerURL()))
s.Equal(fakeOTCToken, provider.token)
}
func (s *TestSuite) TestLoginEnv() {
defer os.Clearenv()
func (s *OTCSuite) TestLoginEnv() {
s.envTest.ClearEnv()
os.Setenv("OTC_DOMAIN_NAME", "unittest1")
os.Setenv("OTC_USER_NAME", "unittest2")
os.Setenv("OTC_PASSWORD", "unittest3")
os.Setenv("OTC_PROJECT_NAME", "unittest4")
os.Setenv("OTC_IDENTITY_ENDPOINT", "unittest5")
s.envTest.Apply(map[string]string{
"OTC_DOMAIN_NAME": "unittest1",
"OTC_USER_NAME": "unittest2",
"OTC_PASSWORD": "unittest3",
"OTC_PROJECT_NAME": "unittest4",
"OTC_IDENTITY_ENDPOINT": "unittest5",
})
provider, err := NewDNSProvider()
require.NoError(s.T(), err)
assert.Equal(s.T(), provider.config.DomainName, "unittest1")
assert.Equal(s.T(), provider.config.UserName, "unittest2")
assert.Equal(s.T(), provider.config.Password, "unittest3")
assert.Equal(s.T(), provider.config.ProjectName, "unittest4")
assert.Equal(s.T(), provider.config.IdentityEndpoint, "unittest5")
s.Require().NoError(err)
s.Equal(provider.config.DomainName, "unittest1")
s.Equal(provider.config.UserName, "unittest2")
s.Equal(provider.config.Password, "unittest3")
s.Equal(provider.config.ProjectName, "unittest4")
s.Equal(provider.config.IdentityEndpoint, "unittest5")
os.Setenv("OTC_IDENTITY_ENDPOINT", "")
provider, err = NewDNSProvider()
require.NoError(s.T(), err)
assert.Equal(s.T(), provider.config.IdentityEndpoint, "https://iam.eu-de.otc.t-systems.com:443/v3/auth/tokens")
s.Require().NoError(err)
s.Equal(provider.config.IdentityEndpoint, "https://iam.eu-de.otc.t-systems.com:443/v3/auth/tokens")
}
func (s *TestSuite) TestLoginEnvEmpty() {
defer os.Clearenv()
func (s *OTCSuite) TestLoginEnvEmpty() {
s.envTest.ClearEnv()
_, err := NewDNSProvider()
assert.EqualError(s.T(), err, "otc: some credentials information are missing: OTC_DOMAIN_NAME,OTC_USER_NAME,OTC_PASSWORD,OTC_PROJECT_NAME")
s.EqualError(err, "otc: some credentials information are missing: OTC_DOMAIN_NAME,OTC_USER_NAME,OTC_PASSWORD,OTC_PROJECT_NAME")
}
func (s *TestSuite) TestDNSProvider_Present() {
func (s *OTCSuite) TestDNSProvider_Present() {
s.Mock.HandleListZonesSuccessfully()
s.Mock.HandleListRecordsetsSuccessfully()
provider, err := s.createDNSProvider()
require.NoError(s.T(), err)
s.Require().NoError(err)
err = provider.Present("example.com", "", "foobar")
require.NoError(s.T(), err)
s.Require().NoError(err)
}
func (s *TestSuite) TestDNSProvider_Present_EmptyZone() {
func (s *OTCSuite) TestDNSProvider_Present_EmptyZone() {
s.Mock.HandleListZonesEmpty()
s.Mock.HandleListRecordsetsSuccessfully()
provider, err := s.createDNSProvider()
require.NoError(s.T(), err)
s.Require().NoError(err)
err = provider.Present("example.com", "", "foobar")
assert.NotNil(s.T(), err)
s.NotNil(err)
}
func (s *TestSuite) TestDNSProvider_CleanUp() {
func (s *OTCSuite) TestDNSProvider_CleanUp() {
s.Mock.HandleListZonesSuccessfully()
s.Mock.HandleListRecordsetsSuccessfully()
s.Mock.HandleDeleteRecordsetsSuccessfully()
provider, err := s.createDNSProvider()
require.NoError(s.T(), err)
s.Require().NoError(err)
err = provider.CleanUp("example.com", "", "foobar")
require.NoError(s.T(), err)
s.Require().NoError(err)
}
func (s *TestSuite) TestDNSProvider_CleanUp_EmptyRecordset() {
func (s *OTCSuite) TestDNSProvider_CleanUp_EmptyRecordset() {
s.Mock.HandleListZonesSuccessfully()
s.Mock.HandleListRecordsetsEmpty()
provider, err := s.createDNSProvider()
require.NoError(s.T(), err)
s.Require().NoError(err)
err = provider.CleanUp("example.com", "", "foobar")
require.Error(s.T(), err)
s.Require().Error(err)
}

View file

@ -1,38 +1,19 @@
package ovh
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestAPIEndpoint string
envTestApplicationKey string
envTestApplicationSecret string
envTestConsumerKey string
envTestDomain string
)
func init() {
envTestAPIEndpoint = os.Getenv("OVH_ENDPOINT")
envTestApplicationKey = os.Getenv("OVH_APPLICATION_KEY")
envTestApplicationSecret = os.Getenv("OVH_APPLICATION_SECRET")
envTestConsumerKey = os.Getenv("OVH_CONSUMER_KEY")
envTestDomain = os.Getenv("OVH_DOMAIN")
liveTest = len(envTestAPIEndpoint) > 0 && len(envTestApplicationKey) > 0 && len(envTestApplicationSecret) > 0 && len(envTestConsumerKey) > 0
}
func restoreEnv() {
os.Setenv("OVH_ENDPOINT", envTestAPIEndpoint)
os.Setenv("OVH_APPLICATION_KEY", envTestApplicationKey)
os.Setenv("OVH_APPLICATION_SECRET", envTestApplicationSecret)
os.Setenv("OVH_CONSUMER_KEY", envTestConsumerKey)
}
var envTest = tester.NewEnvTest(
"OVH_ENDPOINT",
"OVH_APPLICATION_KEY",
"OVH_APPLICATION_SECRET",
"OVH_CONSUMER_KEY").
WithDomain("OVH_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -113,14 +94,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -201,12 +178,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("OVH_ENDPOINT")
os.Unsetenv("OVH_APPLICATION_KEY")
os.Unsetenv("OVH_APPLICATION_SECRET")
os.Unsetenv("OVH_CONSUMER_KEY")
config := NewDefaultConfig()
config.APIEndpoint = test.apiEndpoint
config.ApplicationKey = test.applicationKey
@ -229,29 +200,29 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -2,33 +2,16 @@ package pdns
import (
"net/url"
"os"
"testing"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestAPIURL *url.URL
envTestAPIKey string
envTestDomain string
)
func init() {
envTestAPIURL, _ = url.Parse(os.Getenv("PDNS_API_URL"))
envTestAPIKey = os.Getenv("PDNS_API_KEY")
envTestDomain = os.Getenv("PDNS_DOMAIN")
if len(envTestAPIURL.String()) > 0 && len(envTestAPIKey) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("PDNS_API_URL", envTestAPIURL.String())
os.Setenv("PDNS_API_KEY", envTestAPIKey)
}
var envTest = tester.NewEnvTest(
"PDNS_API_URL",
"PDNS_API_KEY").
WithDomain("PDNS_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -71,14 +54,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -130,10 +109,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("PDNS_API_KEY")
os.Unsetenv("PDNS_API_URL")
config := NewDefaultConfig()
config.APIKey = test.apiKey
config.Host = test.host
@ -152,17 +127,17 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresentAndCleanup(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -5,31 +5,19 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestUser string
envTestAPIKey string
envTestDomain string
)
func init() {
envTestUser = os.Getenv("RACKSPACE_USER")
envTestAPIKey = os.Getenv("RACKSPACE_API_KEY")
envTestDomain = os.Getenv("RACKSPACE_DOMAIN")
if len(envTestUser) > 0 && len(envTestAPIKey) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
var envTest = tester.NewEnvTest(
"RACKSPACE_USER",
"RACKSPACE_API_KEY").
WithDomain("RACKSPACE_DOMAIN")
func TestNewDNSProviderConfig(t *testing.T) {
config, tearDown := setupTest()
@ -72,38 +60,42 @@ func TestDNSProvider_CleanUp(t *testing.T) {
}
func TestLiveNewDNSProvider_ValidEnv(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
assert.Contains(t, provider.cloudDNSEndpoint, "https://dns.api.rackspacecloud.com/v1.0/", "The endpoint URL should contain the base")
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "112233445566==")
err = provider.Present(envTest.GetDomain(), "", "112233445566==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
time.Sleep(time.Second * 15)
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.CleanUp(envTestDomain, "", "112233445566==")
time.Sleep(15 * time.Second)
err = provider.CleanUp(envTest.GetDomain(), "", "112233445566==")
require.NoError(t, err)
}

View file

@ -7,19 +7,19 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/config/env"
)
func TestLiveTTL(t *testing.T) {
config, err := env.Get("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_REGION", "R53_DOMAIN")
if err != nil {
t.Skip(err.Error())
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
domain := config["R53_DOMAIN"]
domain := envTest.GetDomain()
err = provider.Present(domain, "foo", "bar")
require.NoError(t, err)

View file

@ -12,55 +12,20 @@ import (
"github.com/aws/aws-sdk-go/service/route53"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
envTestAwsSecretAccessKey string
envTestAwsAccessKeyID string
envTestAwsRegion string
envTestAwsHostedZoneID string
envTestAwsMaxRetries string
envTestAwsTTL string
envTestAwsPropagationTimeout string
envTestAwsPollingInterval string
)
func init() {
envTestAwsAccessKeyID = os.Getenv("AWS_ACCESS_KEY_ID")
envTestAwsSecretAccessKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
envTestAwsRegion = os.Getenv("AWS_REGION")
envTestAwsHostedZoneID = os.Getenv("AWS_HOSTED_ZONE_ID")
envTestAwsMaxRetries = os.Getenv("AWS_MAX_RETRIES")
envTestAwsTTL = os.Getenv("AWS_TTL")
envTestAwsPropagationTimeout = os.Getenv("AWS_PROPAGATION_TIMEOUT")
envTestAwsPollingInterval = os.Getenv("AWS_POLLING_INTERVAL")
}
func restoreEnv() {
os.Setenv("AWS_ACCESS_KEY_ID", envTestAwsAccessKeyID)
os.Setenv("AWS_SECRET_ACCESS_KEY", envTestAwsSecretAccessKey)
os.Setenv("AWS_REGION", envTestAwsRegion)
os.Setenv("AWS_HOSTED_ZONE_ID", envTestAwsHostedZoneID)
os.Setenv("AWS_MAX_RETRIES", envTestAwsMaxRetries)
os.Setenv("AWS_TTL", envTestAwsTTL)
os.Setenv("AWS_PROPAGATION_TIMEOUT", envTestAwsPropagationTimeout)
os.Setenv("AWS_POLLING_INTERVAL", envTestAwsPollingInterval)
}
func cleanEnv() {
os.Unsetenv("AWS_ACCESS_KEY_ID")
os.Unsetenv("AWS_SECRET_ACCESS_KEY")
os.Unsetenv("AWS_REGION")
os.Unsetenv("AWS_HOSTED_ZONE_ID")
os.Unsetenv("AWS_MAX_RETRIES")
os.Unsetenv("AWS_TTL")
os.Unsetenv("AWS_PROPAGATION_TIMEOUT")
os.Unsetenv("AWS_POLLING_INTERVAL")
}
var envTest = tester.NewEnvTest(
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"AWS_REGION",
"AWS_HOSTED_ZONE_ID",
"AWS_MAX_RETRIES",
"AWS_TTL",
"AWS_PROPAGATION_TIMEOUT",
"AWS_POLLING_INTERVAL").
WithDomain("R53_DOMAIN").
WithLiveTestRequirements("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_REGION", "R53_DOMAIN")
func makeTestProvider(ts *httptest.Server) *DNSProvider {
config := &aws.Config{
@ -80,7 +45,9 @@ func makeTestProvider(ts *httptest.Server) *DNSProvider {
}
func Test_loadCredentials_FromEnv(t *testing.T) {
defer restoreEnv()
defer envTest.RestoreEnv()
envTest.ClearEnv()
os.Setenv("AWS_ACCESS_KEY_ID", "123")
os.Setenv("AWS_SECRET_ACCESS_KEY", "456")
os.Setenv("AWS_REGION", "us-east-1")
@ -105,7 +72,9 @@ func Test_loadCredentials_FromEnv(t *testing.T) {
}
func Test_loadRegion_FromEnv(t *testing.T) {
defer restoreEnv()
defer envTest.RestoreEnv()
envTest.ClearEnv()
os.Setenv("AWS_REGION", route53.CloudWatchRegionUsEast1)
sess, err := session.NewSession(aws.NewConfig())
@ -116,7 +85,8 @@ func Test_loadRegion_FromEnv(t *testing.T) {
}
func Test_getHostedZoneID_FromEnv(t *testing.T) {
defer restoreEnv()
defer envTest.RestoreEnv()
envTest.ClearEnv()
expectedZoneID := "zoneID"
@ -132,7 +102,7 @@ func Test_getHostedZoneID_FromEnv(t *testing.T) {
}
func TestNewDefaultConfig(t *testing.T) {
defer restoreEnv()
defer envTest.RestoreEnv()
testCases := []struct {
desc string
@ -169,7 +139,7 @@ func TestNewDefaultConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
cleanEnv()
envTest.ClearEnv()
for key, value := range test.envVars {
os.Setenv(key, value)
}
@ -195,6 +165,8 @@ func TestDNSProvider_Present(t *testing.T) {
ts := newMockServer(t, mockResponses)
defer ts.Close()
defer envTest.RestoreEnv()
envTest.ClearEnv()
provider := makeTestProvider(ts)
domain := "example.com"

View file

@ -1,34 +1,17 @@
package sakuracloud
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestAccessToken string
envTestAccessSecret string
envTestDomain string
)
func init() {
envTestAccessToken = os.Getenv("SAKURACLOUD_ACCESS_TOKEN")
envTestAccessSecret = os.Getenv("SAKURACLOUD_ACCESS_TOKEN_SECRET")
envTestDomain = os.Getenv("SAKURACLOUD_DOMAIN")
if len(envTestAccessToken) > 0 && len(envTestAccessSecret) > 0 && len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("SAKURACLOUD_ACCESS_TOKEN", envTestAccessToken)
os.Setenv("SAKURACLOUD_ACCESS_TOKEN_SECRET", envTestAccessSecret)
}
var envTest = tester.NewEnvTest(
"SAKURACLOUD_ACCESS_TOKEN",
"SAKURACLOUD_ACCESS_TOKEN_SECRET").
WithDomain("SAKURACLOUD_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -71,14 +54,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -124,10 +103,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("SAKURACLOUD_ACCESS_TOKEN")
os.Unsetenv("SAKURACLOUD_ACCESS_TOKEN_SECRET")
config := NewDefaultConfig()
config.Token = test.token
config.Secret = test.secret
@ -147,29 +122,29 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -4,42 +4,19 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestClientID string
envTestClientSecret string
envTestStackID string
envTestDomain string
)
func init() {
envTestClientID = os.Getenv("STACKPATH_CLIENT_ID")
envTestClientSecret = os.Getenv("STACKPATH_CLIENT_SECRET")
envTestStackID = os.Getenv("STACKPATH_STACK_ID")
envTestDomain = os.Getenv("STACKPATH_DOMAIN")
if len(envTestClientID) > 0 &&
len(envTestClientSecret) > 0 &&
len(envTestStackID) > 0 &&
len(envTestDomain) > 0 {
liveTest = true
}
}
func restoreEnv() {
os.Setenv("STACKPATH_CLIENT_ID", envTestClientID)
os.Setenv("STACKPATH_CLIENT_SECRET", envTestClientSecret)
os.Setenv("STACKPATH_STACK_ID", envTestStackID)
os.Setenv("STACKPATH_DOMAIN", envTestDomain)
}
var envTest = tester.NewEnvTest(
"STACKPATH_CLIENT_ID",
"STACKPATH_CLIENT_SECRET",
"STACKPATH_STACK_ID").
WithDomain("STACKPATH_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -95,14 +72,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -289,29 +262,29 @@ func TestDNSProvider_getZones(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

View file

@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
const testDomain = "example.com"
@ -101,20 +102,26 @@ var jsonMap = map[string]string{
"recordDeleted": `{"status": "ok"}`,
}
var envTest = tester.NewEnvTest("SECRET_VEGADNS_KEY", "SECRET_VEGADNS_SECRET", "VEGADNS_URL")
type muxCallback func() *http.ServeMux
func TestNewDNSProvider_Fail(t *testing.T) {
os.Setenv("VEGADNS_URL", "")
defer envTest.RestoreEnv()
envTest.ClearEnv()
_, err := NewDNSProvider()
assert.Error(t, err, "VEGADNS_URL env missing")
}
func TestDNSProvider_TimeoutSuccess(t *testing.T) {
defer envTest.RestoreEnv()
envTest.ClearEnv()
ts, err := startTestServer(muxSuccess)
require.NoError(t, err)
defer ts.Close()
defer os.Clearenv()
provider, err := NewDNSProvider()
require.NoError(t, err)
@ -148,11 +155,13 @@ func TestDNSProvider_Present(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer envTest.RestoreEnv()
envTest.ClearEnv()
ts, err := startTestServer(test.callback)
require.NoError(t, err)
defer ts.Close()
defer os.Clearenv()
provider, err := NewDNSProvider()
require.NoError(t, err)
@ -191,11 +200,13 @@ func TestDNSProvider_CleanUp(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer envTest.RestoreEnv()
envTest.ClearEnv()
ts, err := startTestServer(test.callback)
require.NoError(t, err)
defer ts.Close()
defer os.Clearenv()
provider, err := NewDNSProvider()
require.NoError(t, err)

View file

@ -1,29 +1,15 @@
package vultr
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var (
liveTest bool
envTestAPIKey string
envTestDomain string
)
func init() {
envTestAPIKey = os.Getenv("VULTR_API_KEY")
envTestDomain = os.Getenv("VULTR_TEST_DOMAIN")
liveTest = len(envTestAPIKey) > 0 && len(envTestDomain) > 0
}
func restoreEnv() {
os.Setenv("VULTR_API_KEY", envTestAPIKey)
}
var envTest = tester.NewEnvTest("VULTR_API_KEY").
WithDomain("VULTR_TEST_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
@ -48,14 +34,10 @@ func TestNewDNSProvider(t *testing.T) {
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)
}
}
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
@ -89,9 +71,6 @@ func TestNewDNSProviderConfig(t *testing.T) {
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer restoreEnv()
os.Unsetenv("VULTR_API_KEY")
config := NewDefaultConfig()
config.APIKey = test.apiKey
@ -110,29 +89,29 @@ func TestNewDNSProviderConfig(t *testing.T) {
}
func TestLivePresent(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTestDomain, "", "123d==")
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !liveTest {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
restoreEnv()
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTestDomain, "", "123d==")
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

22
vendor/github.com/stretchr/objx/LICENSE generated vendored Normal file
View file

@ -0,0 +1,22 @@
The MIT License
Copyright (c) 2014 Stretchr, Inc.
Copyright (c) 2017-2018 objx contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

148
vendor/github.com/stretchr/objx/accessors.go generated vendored Normal file
View file

@ -0,0 +1,148 @@
package objx
import (
"regexp"
"strconv"
"strings"
)
// arrayAccesRegexString is the regex used to extract the array number
// from the access path
const arrayAccesRegexString = `^(.+)\[([0-9]+)\]$`
// arrayAccesRegex is the compiled arrayAccesRegexString
var arrayAccesRegex = regexp.MustCompile(arrayAccesRegexString)
// Get gets the value using the specified selector and
// returns it inside a new Obj object.
//
// If it cannot find the value, Get will return a nil
// value inside an instance of Obj.
//
// Get can only operate directly on map[string]interface{} and []interface.
//
// Example
//
// To access the title of the third chapter of the second book, do:
//
// o.Get("books[1].chapters[2].title")
func (m Map) Get(selector string) *Value {
rawObj := access(m, selector, nil, false)
return &Value{data: rawObj}
}
// Set sets the value using the specified selector and
// returns the object on which Set was called.
//
// Set can only operate directly on map[string]interface{} and []interface
//
// Example
//
// To set the title of the third chapter of the second book, do:
//
// o.Set("books[1].chapters[2].title","Time to Go")
func (m Map) Set(selector string, value interface{}) Map {
access(m, selector, value, true)
return m
}
// access accesses the object using the selector and performs the
// appropriate action.
func access(current, selector, value interface{}, isSet bool) interface{} {
switch selector.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
if array, ok := current.([]interface{}); ok {
index := intFromInterface(selector)
if index >= len(array) {
return nil
}
return array[index]
}
return nil
case string:
selStr := selector.(string)
selSegs := strings.SplitN(selStr, PathSeparator, 2)
thisSel := selSegs[0]
index := -1
var err error
if strings.Contains(thisSel, "[") {
arrayMatches := arrayAccesRegex.FindStringSubmatch(thisSel)
if len(arrayMatches) > 0 {
// Get the key into the map
thisSel = arrayMatches[1]
// Get the index into the array at the key
index, err = strconv.Atoi(arrayMatches[2])
if err != nil {
// This should never happen. If it does, something has gone
// seriously wrong. Panic.
panic("objx: Array index is not an integer. Must use array[int].")
}
}
}
if curMap, ok := current.(Map); ok {
current = map[string]interface{}(curMap)
}
// get the object in question
switch current.(type) {
case map[string]interface{}:
curMSI := current.(map[string]interface{})
if len(selSegs) <= 1 && isSet {
curMSI[thisSel] = value
return nil
}
current = curMSI[thisSel]
default:
current = nil
}
// do we need to access the item of an array?
if index > -1 {
if array, ok := current.([]interface{}); ok {
if index < len(array) {
current = array[index]
} else {
current = nil
}
}
}
if len(selSegs) > 1 {
current = access(current, selSegs[1], value, isSet)
}
}
return current
}
// intFromInterface converts an interface object to the largest
// representation of an unsigned integer using a type switch and
// assertions
func intFromInterface(selector interface{}) int {
var value int
switch selector.(type) {
case int:
value = selector.(int)
case int8:
value = int(selector.(int8))
case int16:
value = int(selector.(int16))
case int32:
value = int(selector.(int32))
case int64:
value = int(selector.(int64))
case uint:
value = int(selector.(uint))
case uint8:
value = int(selector.(uint8))
case uint16:
value = int(selector.(uint16))
case uint32:
value = int(selector.(uint32))
case uint64:
value = int(selector.(uint64))
default:
return 0
}
return value
}

13
vendor/github.com/stretchr/objx/constants.go generated vendored Normal file
View file

@ -0,0 +1,13 @@
package objx
const (
// PathSeparator is the character used to separate the elements
// of the keypath.
//
// For example, `location.address.city`
PathSeparator string = "."
// SignatureSeparator is the character that is used to
// separate the Base64 string from the security signature.
SignatureSeparator = "_"
)

108
vendor/github.com/stretchr/objx/conversions.go generated vendored Normal file
View file

@ -0,0 +1,108 @@
package objx
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/url"
)
// JSON converts the contained object to a JSON string
// representation
func (m Map) JSON() (string, error) {
result, err := json.Marshal(m)
if err != nil {
err = errors.New("objx: JSON encode failed with: " + err.Error())
}
return string(result), err
}
// MustJSON converts the contained object to a JSON string
// representation and panics if there is an error
func (m Map) MustJSON() string {
result, err := m.JSON()
if err != nil {
panic(err.Error())
}
return result
}
// Base64 converts the contained object to a Base64 string
// representation of the JSON string representation
func (m Map) Base64() (string, error) {
var buf bytes.Buffer
jsonData, err := m.JSON()
if err != nil {
return "", err
}
encoder := base64.NewEncoder(base64.StdEncoding, &buf)
_, err = encoder.Write([]byte(jsonData))
if err != nil {
return "", err
}
_ = encoder.Close()
return buf.String(), nil
}
// MustBase64 converts the contained object to a Base64 string
// representation of the JSON string representation and panics
// if there is an error
func (m Map) MustBase64() string {
result, err := m.Base64()
if err != nil {
panic(err.Error())
}
return result
}
// SignedBase64 converts the contained object to a Base64 string
// representation of the JSON string representation and signs it
// using the provided key.
func (m Map) SignedBase64(key string) (string, error) {
base64, err := m.Base64()
if err != nil {
return "", err
}
sig := HashWithKey(base64, key)
return base64 + SignatureSeparator + sig, nil
}
// MustSignedBase64 converts the contained object to a Base64 string
// representation of the JSON string representation and signs it
// using the provided key and panics if there is an error
func (m Map) MustSignedBase64(key string) string {
result, err := m.SignedBase64(key)
if err != nil {
panic(err.Error())
}
return result
}
/*
URL Query
------------------------------------------------
*/
// URLValues creates a url.Values object from an Obj. This
// function requires that the wrapped object be a map[string]interface{}
func (m Map) URLValues() url.Values {
vals := make(url.Values)
for k, v := range m {
//TODO: can this be done without sprintf?
vals.Set(k, fmt.Sprintf("%v", v))
}
return vals
}
// URLQuery gets an encoded URL query representing the given
// Obj. This function requires that the wrapped object be a
// map[string]interface{}
func (m Map) URLQuery() (string, error) {
return m.URLValues().Encode(), nil
}

66
vendor/github.com/stretchr/objx/doc.go generated vendored Normal file
View file

@ -0,0 +1,66 @@
/*
Objx - Go package for dealing with maps, slices, JSON and other data.
Overview
Objx provides the `objx.Map` type, which is a `map[string]interface{}` that exposes
a powerful `Get` method (among others) that allows you to easily and quickly get
access to data within the map, without having to worry too much about type assertions,
missing data, default values etc.
Pattern
Objx uses a preditable pattern to make access data from within `map[string]interface{}` easy.
Call one of the `objx.` functions to create your `objx.Map` to get going:
m, err := objx.FromJSON(json)
NOTE: Any methods or functions with the `Must` prefix will panic if something goes wrong,
the rest will be optimistic and try to figure things out without panicking.
Use `Get` to access the value you're interested in. You can use dot and array
notation too:
m.Get("places[0].latlng")
Once you have sought the `Value` you're interested in, you can use the `Is*` methods to determine its type.
if m.Get("code").IsStr() { // Your code... }
Or you can just assume the type, and use one of the strong type methods to extract the real value:
m.Get("code").Int()
If there's no value there (or if it's the wrong type) then a default value will be returned,
or you can be explicit about the default value.
Get("code").Int(-1)
If you're dealing with a slice of data as a value, Objx provides many useful methods for iterating,
manipulating and selecting that data. You can find out more by exploring the index below.
Reading data
A simple example of how to use Objx:
// Use MustFromJSON to make an objx.Map from some JSON
m := objx.MustFromJSON(`{"name": "Mat", "age": 30}`)
// Get the details
name := m.Get("name").Str()
age := m.Get("age").Int()
// Get their nickname (or use their name if they don't have one)
nickname := m.Get("nickname").Str(name)
Ranging
Since `objx.Map` is a `map[string]interface{}` you can treat it as such.
For example, to `range` the data, do what you would expect:
m := objx.MustFromJSON(json)
for key, value := range m {
// Your code...
}
*/
package objx

190
vendor/github.com/stretchr/objx/map.go generated vendored Normal file
View file

@ -0,0 +1,190 @@
package objx
import (
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"net/url"
"strings"
)
// MSIConvertable is an interface that defines methods for converting your
// custom types to a map[string]interface{} representation.
type MSIConvertable interface {
// MSI gets a map[string]interface{} (msi) representing the
// object.
MSI() map[string]interface{}
}
// Map provides extended functionality for working with
// untyped data, in particular map[string]interface (msi).
type Map map[string]interface{}
// Value returns the internal value instance
func (m Map) Value() *Value {
return &Value{data: m}
}
// Nil represents a nil Map.
var Nil = New(nil)
// New creates a new Map containing the map[string]interface{} in the data argument.
// If the data argument is not a map[string]interface, New attempts to call the
// MSI() method on the MSIConvertable interface to create one.
func New(data interface{}) Map {
if _, ok := data.(map[string]interface{}); !ok {
if converter, ok := data.(MSIConvertable); ok {
data = converter.MSI()
} else {
return nil
}
}
return Map(data.(map[string]interface{}))
}
// MSI creates a map[string]interface{} and puts it inside a new Map.
//
// The arguments follow a key, value pattern.
//
//
// Returns nil if any key argument is non-string or if there are an odd number of arguments.
//
// Example
//
// To easily create Maps:
//
// m := objx.MSI("name", "Mat", "age", 29, "subobj", objx.MSI("active", true))
//
// // creates an Map equivalent to
// m := objx.Map{"name": "Mat", "age": 29, "subobj": objx.Map{"active": true}}
func MSI(keyAndValuePairs ...interface{}) Map {
newMap := Map{}
keyAndValuePairsLen := len(keyAndValuePairs)
if keyAndValuePairsLen%2 != 0 {
return nil
}
for i := 0; i < keyAndValuePairsLen; i = i + 2 {
key := keyAndValuePairs[i]
value := keyAndValuePairs[i+1]
// make sure the key is a string
keyString, keyStringOK := key.(string)
if !keyStringOK {
return nil
}
newMap[keyString] = value
}
return newMap
}
// ****** Conversion Constructors
// MustFromJSON creates a new Map containing the data specified in the
// jsonString.
//
// Panics if the JSON is invalid.
func MustFromJSON(jsonString string) Map {
o, err := FromJSON(jsonString)
if err != nil {
panic("objx: MustFromJSON failed with error: " + err.Error())
}
return o
}
// FromJSON creates a new Map containing the data specified in the
// jsonString.
//
// Returns an error if the JSON is invalid.
func FromJSON(jsonString string) (Map, error) {
var data interface{}
err := json.Unmarshal([]byte(jsonString), &data)
if err != nil {
return Nil, err
}
return New(data), nil
}
// FromBase64 creates a new Obj containing the data specified
// in the Base64 string.
//
// The string is an encoded JSON string returned by Base64
func FromBase64(base64String string) (Map, error) {
decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(base64String))
decoded, err := ioutil.ReadAll(decoder)
if err != nil {
return nil, err
}
return FromJSON(string(decoded))
}
// MustFromBase64 creates a new Obj containing the data specified
// in the Base64 string and panics if there is an error.
//
// The string is an encoded JSON string returned by Base64
func MustFromBase64(base64String string) Map {
result, err := FromBase64(base64String)
if err != nil {
panic("objx: MustFromBase64 failed with error: " + err.Error())
}
return result
}
// FromSignedBase64 creates a new Obj containing the data specified
// in the Base64 string.
//
// The string is an encoded JSON string returned by SignedBase64
func FromSignedBase64(base64String, key string) (Map, error) {
parts := strings.Split(base64String, SignatureSeparator)
if len(parts) != 2 {
return nil, errors.New("objx: Signed base64 string is malformed")
}
sig := HashWithKey(parts[0], key)
if parts[1] != sig {
return nil, errors.New("objx: Signature for base64 data does not match")
}
return FromBase64(parts[0])
}
// MustFromSignedBase64 creates a new Obj containing the data specified
// in the Base64 string and panics if there is an error.
//
// The string is an encoded JSON string returned by Base64
func MustFromSignedBase64(base64String, key string) Map {
result, err := FromSignedBase64(base64String, key)
if err != nil {
panic("objx: MustFromSignedBase64 failed with error: " + err.Error())
}
return result
}
// FromURLQuery generates a new Obj by parsing the specified
// query.
//
// For queries with multiple values, the first value is selected.
func FromURLQuery(query string) (Map, error) {
vals, err := url.ParseQuery(query)
if err != nil {
return nil, err
}
m := Map{}
for k, vals := range vals {
m[k] = vals[0]
}
return m, nil
}
// MustFromURLQuery generates a new Obj by parsing the specified
// query.
//
// For queries with multiple values, the first value is selected.
//
// Panics if it encounters an error
func MustFromURLQuery(query string) Map {
o, err := FromURLQuery(query)
if err != nil {
panic("objx: MustFromURLQuery failed with error: " + err.Error())
}
return o
}

77
vendor/github.com/stretchr/objx/mutations.go generated vendored Normal file
View file

@ -0,0 +1,77 @@
package objx
// Exclude returns a new Map with the keys in the specified []string
// excluded.
func (m Map) Exclude(exclude []string) Map {
excluded := make(Map)
for k, v := range m {
if !contains(exclude, k) {
excluded[k] = v
}
}
return excluded
}
// Copy creates a shallow copy of the Obj.
func (m Map) Copy() Map {
copied := Map{}
for k, v := range m {
copied[k] = v
}
return copied
}
// Merge blends the specified map with a copy of this map and returns the result.
//
// Keys that appear in both will be selected from the specified map.
// This method requires that the wrapped object be a map[string]interface{}
func (m Map) Merge(merge Map) Map {
return m.Copy().MergeHere(merge)
}
// MergeHere blends the specified map with this map and returns the current map.
//
// Keys that appear in both will be selected from the specified map. The original map
// will be modified. This method requires that
// the wrapped object be a map[string]interface{}
func (m Map) MergeHere(merge Map) Map {
for k, v := range merge {
m[k] = v
}
return m
}
// Transform builds a new Obj giving the transformer a chance
// to change the keys and values as it goes. This method requires that
// the wrapped object be a map[string]interface{}
func (m Map) Transform(transformer func(key string, value interface{}) (string, interface{})) Map {
newMap := Map{}
for k, v := range m {
modifiedKey, modifiedVal := transformer(k, v)
newMap[modifiedKey] = modifiedVal
}
return newMap
}
// TransformKeys builds a new map using the specified key mapping.
//
// Unspecified keys will be unaltered.
// This method requires that the wrapped object be a map[string]interface{}
func (m Map) TransformKeys(mapping map[string]string) Map {
return m.Transform(func(key string, value interface{}) (string, interface{}) {
if newKey, ok := mapping[key]; ok {
return newKey, value
}
return key, value
})
}
// Checks if a string slice contains a string
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}

12
vendor/github.com/stretchr/objx/security.go generated vendored Normal file
View file

@ -0,0 +1,12 @@
package objx
import (
"crypto/sha1"
"encoding/hex"
)
// HashWithKey hashes the specified string using the security key
func HashWithKey(data, key string) string {
d := sha1.Sum([]byte(data + ":" + key))
return hex.EncodeToString(d[:])
}

17
vendor/github.com/stretchr/objx/tests.go generated vendored Normal file
View file

@ -0,0 +1,17 @@
package objx
// Has gets whether there is something at the specified selector
// or not.
//
// If m is nil, Has will always return false.
func (m Map) Has(selector string) bool {
if m == nil {
return false
}
return !m.Get(selector).IsNil()
}
// IsNil gets whether the data is nil or not.
func (v *Value) IsNil() bool {
return v == nil || v.data == nil
}

2501
vendor/github.com/stretchr/objx/type_specific_codegen.go generated vendored Normal file

File diff suppressed because it is too large Load diff

53
vendor/github.com/stretchr/objx/value.go generated vendored Normal file
View file

@ -0,0 +1,53 @@
package objx
import (
"fmt"
"strconv"
)
// Value provides methods for extracting interface{} data in various
// types.
type Value struct {
// data contains the raw data being managed by this Value
data interface{}
}
// Data returns the raw data contained by this Value
func (v *Value) Data() interface{} {
return v.data
}
// String returns the value always as a string
func (v *Value) String() string {
switch {
case v.IsStr():
return v.Str()
case v.IsBool():
return strconv.FormatBool(v.Bool())
case v.IsFloat32():
return strconv.FormatFloat(float64(v.Float32()), 'f', -1, 32)
case v.IsFloat64():
return strconv.FormatFloat(v.Float64(), 'f', -1, 64)
case v.IsInt():
return strconv.FormatInt(int64(v.Int()), 10)
case v.IsInt8():
return strconv.FormatInt(int64(v.Int8()), 10)
case v.IsInt16():
return strconv.FormatInt(int64(v.Int16()), 10)
case v.IsInt32():
return strconv.FormatInt(int64(v.Int32()), 10)
case v.IsInt64():
return strconv.FormatInt(v.Int64(), 10)
case v.IsUint():
return strconv.FormatUint(uint64(v.Uint()), 10)
case v.IsUint8():
return strconv.FormatUint(uint64(v.Uint8()), 10)
case v.IsUint16():
return strconv.FormatUint(uint64(v.Uint16()), 10)
case v.IsUint32():
return strconv.FormatUint(uint64(v.Uint32()), 10)
case v.IsUint64():
return strconv.FormatUint(v.Uint64(), 10)
}
return fmt.Sprintf("%#v", v.Data())
}

44
vendor/github.com/stretchr/testify/mock/doc.go generated vendored Normal file
View file

@ -0,0 +1,44 @@
// Package mock provides a system by which it is possible to mock your objects
// and verify calls are happening as expected.
//
// Example Usage
//
// The mock package provides an object, Mock, that tracks activity on another object. It is usually
// embedded into a test object as shown below:
//
// type MyTestObject struct {
// // add a Mock object instance
// mock.Mock
//
// // other fields go here as normal
// }
//
// When implementing the methods of an interface, you wire your functions up
// to call the Mock.Called(args...) method, and return the appropriate values.
//
// For example, to mock a method that saves the name and age of a person and returns
// the year of their birth or an error, you might write this:
//
// func (o *MyTestObject) SavePersonDetails(firstname, lastname string, age int) (int, error) {
// args := o.Called(firstname, lastname, age)
// return args.Int(0), args.Error(1)
// }
//
// The Int, Error and Bool methods are examples of strongly typed getters that take the argument
// index position. Given this argument list:
//
// (12, true, "Something")
//
// You could read them out strongly typed like this:
//
// args.Int(0)
// args.Bool(1)
// args.String(2)
//
// For objects of your own type, use the generic Arguments.Get(index) method and make a type assertion:
//
// return args.Get(0).(*MyObject), args.Get(1).(*AnotherObjectOfMine)
//
// This may cause a panic if the object you are getting is nil (the type assertion will fail), in those
// cases you should check for nil first.
package mock

885
vendor/github.com/stretchr/testify/mock/mock.go generated vendored Normal file
View file

@ -0,0 +1,885 @@
package mock
import (
"errors"
"fmt"
"reflect"
"regexp"
"runtime"
"strings"
"sync"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/pmezard/go-difflib/difflib"
"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
)
// TestingT is an interface wrapper around *testing.T
type TestingT interface {
Logf(format string, args ...interface{})
Errorf(format string, args ...interface{})
FailNow()
}
/*
Call
*/
// Call represents a method call and is used for setting expectations,
// as well as recording activity.
type Call struct {
Parent *Mock
// The name of the method that was or will be called.
Method string
// Holds the arguments of the method.
Arguments Arguments
// Holds the arguments that should be returned when
// this method is called.
ReturnArguments Arguments
// Holds the caller info for the On() call
callerInfo []string
// The number of times to return the return arguments when setting
// expectations. 0 means to always return the value.
Repeatability int
// Amount of times this call has been called
totalCalls int
// Call to this method can be optional
optional bool
// Holds a channel that will be used to block the Return until it either
// receives a message or is closed. nil means it returns immediately.
WaitFor <-chan time.Time
waitTime time.Duration
// Holds a handler used to manipulate arguments content that are passed by
// reference. It's useful when mocking methods such as unmarshalers or
// decoders.
RunFn func(Arguments)
}
func newCall(parent *Mock, methodName string, callerInfo []string, methodArguments ...interface{}) *Call {
return &Call{
Parent: parent,
Method: methodName,
Arguments: methodArguments,
ReturnArguments: make([]interface{}, 0),
callerInfo: callerInfo,
Repeatability: 0,
WaitFor: nil,
RunFn: nil,
}
}
func (c *Call) lock() {
c.Parent.mutex.Lock()
}
func (c *Call) unlock() {
c.Parent.mutex.Unlock()
}
// Return specifies the return arguments for the expectation.
//
// Mock.On("DoSomething").Return(errors.New("failed"))
func (c *Call) Return(returnArguments ...interface{}) *Call {
c.lock()
defer c.unlock()
c.ReturnArguments = returnArguments
return c
}
// Once indicates that that the mock should only return the value once.
//
// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Once()
func (c *Call) Once() *Call {
return c.Times(1)
}
// Twice indicates that that the mock should only return the value twice.
//
// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Twice()
func (c *Call) Twice() *Call {
return c.Times(2)
}
// Times indicates that that the mock should only return the indicated number
// of times.
//
// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Times(5)
func (c *Call) Times(i int) *Call {
c.lock()
defer c.unlock()
c.Repeatability = i
return c
}
// WaitUntil sets the channel that will block the mock's return until its closed
// or a message is received.
//
// Mock.On("MyMethod", arg1, arg2).WaitUntil(time.After(time.Second))
func (c *Call) WaitUntil(w <-chan time.Time) *Call {
c.lock()
defer c.unlock()
c.WaitFor = w
return c
}
// After sets how long to block until the call returns
//
// Mock.On("MyMethod", arg1, arg2).After(time.Second)
func (c *Call) After(d time.Duration) *Call {
c.lock()
defer c.unlock()
c.waitTime = d
return c
}
// Run sets a handler to be called before returning. It can be used when
// mocking a method such as unmarshalers that takes a pointer to a struct and
// sets properties in such struct
//
// Mock.On("Unmarshal", AnythingOfType("*map[string]interface{}").Return().Run(func(args Arguments) {
// arg := args.Get(0).(*map[string]interface{})
// arg["foo"] = "bar"
// })
func (c *Call) Run(fn func(args Arguments)) *Call {
c.lock()
defer c.unlock()
c.RunFn = fn
return c
}
// Maybe allows the method call to be optional. Not calling an optional method
// will not cause an error while asserting expectations
func (c *Call) Maybe() *Call {
c.lock()
defer c.unlock()
c.optional = true
return c
}
// On chains a new expectation description onto the mocked interface. This
// allows syntax like.
//
// Mock.
// On("MyMethod", 1).Return(nil).
// On("MyOtherMethod", 'a', 'b', 'c').Return(errors.New("Some Error"))
func (c *Call) On(methodName string, arguments ...interface{}) *Call {
return c.Parent.On(methodName, arguments...)
}
// Mock is the workhorse used to track activity on another object.
// For an example of its usage, refer to the "Example Usage" section at the top
// of this document.
type Mock struct {
// Represents the calls that are expected of
// an object.
ExpectedCalls []*Call
// Holds the calls that were made to this mocked object.
Calls []Call
// test is An optional variable that holds the test struct, to be used when an
// invalid mock call was made.
test TestingT
// TestData holds any data that might be useful for testing. Testify ignores
// this data completely allowing you to do whatever you like with it.
testData objx.Map
mutex sync.Mutex
}
// TestData holds any data that might be useful for testing. Testify ignores
// this data completely allowing you to do whatever you like with it.
func (m *Mock) TestData() objx.Map {
if m.testData == nil {
m.testData = make(objx.Map)
}
return m.testData
}
/*
Setting expectations
*/
// Test sets the test struct variable of the mock object
func (m *Mock) Test(t TestingT) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.test = t
}
// fail fails the current test with the given formatted format and args.
// In case that a test was defined, it uses the test APIs for failing a test,
// otherwise it uses panic.
func (m *Mock) fail(format string, args ...interface{}) {
m.mutex.Lock()
defer m.mutex.Unlock()
if m.test == nil {
panic(fmt.Sprintf(format, args...))
}
m.test.Errorf(format, args...)
m.test.FailNow()
}
// On starts a description of an expectation of the specified method
// being called.
//
// Mock.On("MyMethod", arg1, arg2)
func (m *Mock) On(methodName string, arguments ...interface{}) *Call {
for _, arg := range arguments {
if v := reflect.ValueOf(arg); v.Kind() == reflect.Func {
panic(fmt.Sprintf("cannot use Func in expectations. Use mock.AnythingOfType(\"%T\")", arg))
}
}
m.mutex.Lock()
defer m.mutex.Unlock()
c := newCall(m, methodName, assert.CallerInfo(), arguments...)
m.ExpectedCalls = append(m.ExpectedCalls, c)
return c
}
// /*
// Recording and responding to activity
// */
func (m *Mock) findExpectedCall(method string, arguments ...interface{}) (int, *Call) {
for i, call := range m.ExpectedCalls {
if call.Method == method && call.Repeatability > -1 {
_, diffCount := call.Arguments.Diff(arguments)
if diffCount == 0 {
return i, call
}
}
}
return -1, nil
}
func (m *Mock) findClosestCall(method string, arguments ...interface{}) (*Call, string) {
var diffCount int
var closestCall *Call
var err string
for _, call := range m.expectedCalls() {
if call.Method == method {
errInfo, tempDiffCount := call.Arguments.Diff(arguments)
if tempDiffCount < diffCount || diffCount == 0 {
diffCount = tempDiffCount
closestCall = call
err = errInfo
}
}
}
return closestCall, err
}
func callString(method string, arguments Arguments, includeArgumentValues bool) string {
var argValsString string
if includeArgumentValues {
var argVals []string
for argIndex, arg := range arguments {
argVals = append(argVals, fmt.Sprintf("%d: %#v", argIndex, arg))
}
argValsString = fmt.Sprintf("\n\t\t%s", strings.Join(argVals, "\n\t\t"))
}
return fmt.Sprintf("%s(%s)%s", method, arguments.String(), argValsString)
}
// Called tells the mock object that a method has been called, and gets an array
// of arguments to return. Panics if the call is unexpected (i.e. not preceded by
// appropriate .On .Return() calls)
// If Call.WaitFor is set, blocks until the channel is closed or receives a message.
func (m *Mock) Called(arguments ...interface{}) Arguments {
// get the calling function's name
pc, _, _, ok := runtime.Caller(1)
if !ok {
panic("Couldn't get the caller information")
}
functionPath := runtime.FuncForPC(pc).Name()
//Next four lines are required to use GCCGO function naming conventions.
//For Ex: github_com_docker_libkv_store_mock.WatchTree.pN39_github_com_docker_libkv_store_mock.Mock
//uses interface information unlike golang github.com/docker/libkv/store/mock.(*Mock).WatchTree
//With GCCGO we need to remove interface information starting from pN<dd>.
re := regexp.MustCompile("\\.pN\\d+_")
if re.MatchString(functionPath) {
functionPath = re.Split(functionPath, -1)[0]
}
parts := strings.Split(functionPath, ".")
functionName := parts[len(parts)-1]
return m.MethodCalled(functionName, arguments...)
}
// MethodCalled tells the mock object that the given method has been called, and gets
// an array of arguments to return. Panics if the call is unexpected (i.e. not preceded
// by appropriate .On .Return() calls)
// If Call.WaitFor is set, blocks until the channel is closed or receives a message.
func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Arguments {
m.mutex.Lock()
//TODO: could combine expected and closes in single loop
found, call := m.findExpectedCall(methodName, arguments...)
if found < 0 {
// we have to fail here - because we don't know what to do
// as the return arguments. This is because:
//
// a) this is a totally unexpected call to this method,
// b) the arguments are not what was expected, or
// c) the developer has forgotten to add an accompanying On...Return pair.
closestCall, mismatch := m.findClosestCall(methodName, arguments...)
m.mutex.Unlock()
if closestCall != nil {
m.fail("\n\nmock: Unexpected Method Call\n-----------------------------\n\n%s\n\nThe closest call I have is: \n\n%s\n\n%s\nDiff: %s",
callString(methodName, arguments, true),
callString(methodName, closestCall.Arguments, true),
diffArguments(closestCall.Arguments, arguments),
strings.TrimSpace(mismatch),
)
} else {
m.fail("\nassert: mock: I don't know what to return because the method call was unexpected.\n\tEither do Mock.On(\"%s\").Return(...) first, or remove the %s() call.\n\tThis method was unexpected:\n\t\t%s\n\tat: %s", methodName, methodName, callString(methodName, arguments, true), assert.CallerInfo())
}
}
if call.Repeatability == 1 {
call.Repeatability = -1
} else if call.Repeatability > 1 {
call.Repeatability--
}
call.totalCalls++
// add the call
m.Calls = append(m.Calls, *newCall(m, methodName, assert.CallerInfo(), arguments...))
m.mutex.Unlock()
// block if specified
if call.WaitFor != nil {
<-call.WaitFor
} else {
time.Sleep(call.waitTime)
}
m.mutex.Lock()
runFn := call.RunFn
m.mutex.Unlock()
if runFn != nil {
runFn(arguments)
}
m.mutex.Lock()
returnArgs := call.ReturnArguments
m.mutex.Unlock()
return returnArgs
}
/*
Assertions
*/
type assertExpectationser interface {
AssertExpectations(TestingT) bool
}
// AssertExpectationsForObjects asserts that everything specified with On and Return
// of the specified objects was in fact called as expected.
//
// Calls may have occurred in any order.
func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
for _, obj := range testObjects {
if m, ok := obj.(Mock); ok {
t.Logf("Deprecated mock.AssertExpectationsForObjects(myMock.Mock) use mock.AssertExpectationsForObjects(myMock)")
obj = &m
}
m := obj.(assertExpectationser)
if !m.AssertExpectations(t) {
t.Logf("Expectations didn't match for Mock: %+v", reflect.TypeOf(m))
return false
}
}
return true
}
// AssertExpectations asserts that everything specified with On and Return was
// in fact called as expected. Calls may have occurred in any order.
func (m *Mock) AssertExpectations(t TestingT) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
m.mutex.Lock()
defer m.mutex.Unlock()
var somethingMissing bool
var failedExpectations int
// iterate through each expectation
expectedCalls := m.expectedCalls()
for _, expectedCall := range expectedCalls {
if !expectedCall.optional && !m.methodWasCalled(expectedCall.Method, expectedCall.Arguments) && expectedCall.totalCalls == 0 {
somethingMissing = true
failedExpectations++
t.Logf("FAIL:\t%s(%s)\n\t\tat: %s", expectedCall.Method, expectedCall.Arguments.String(), expectedCall.callerInfo)
} else {
if expectedCall.Repeatability > 0 {
somethingMissing = true
failedExpectations++
t.Logf("FAIL:\t%s(%s)\n\t\tat: %s", expectedCall.Method, expectedCall.Arguments.String(), expectedCall.callerInfo)
} else {
t.Logf("PASS:\t%s(%s)", expectedCall.Method, expectedCall.Arguments.String())
}
}
}
if somethingMissing {
t.Errorf("FAIL: %d out of %d expectation(s) were met.\n\tThe code you are testing needs to make %d more call(s).\n\tat: %s", len(expectedCalls)-failedExpectations, len(expectedCalls), failedExpectations, assert.CallerInfo())
}
return !somethingMissing
}
// AssertNumberOfCalls asserts that the method was called expectedCalls times.
func (m *Mock) AssertNumberOfCalls(t TestingT, methodName string, expectedCalls int) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
m.mutex.Lock()
defer m.mutex.Unlock()
var actualCalls int
for _, call := range m.calls() {
if call.Method == methodName {
actualCalls++
}
}
return assert.Equal(t, expectedCalls, actualCalls, fmt.Sprintf("Expected number of calls (%d) does not match the actual number of calls (%d).", expectedCalls, actualCalls))
}
// AssertCalled asserts that the method was called.
// It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method.
func (m *Mock) AssertCalled(t TestingT, methodName string, arguments ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
m.mutex.Lock()
defer m.mutex.Unlock()
if !m.methodWasCalled(methodName, arguments) {
var calledWithArgs []string
for _, call := range m.calls() {
calledWithArgs = append(calledWithArgs, fmt.Sprintf("%v", call.Arguments))
}
if len(calledWithArgs) == 0 {
return assert.Fail(t, "Should have called with given arguments",
fmt.Sprintf("Expected %q to have been called with:\n%v\nbut no actual calls happened", methodName, arguments))
}
return assert.Fail(t, "Should have called with given arguments",
fmt.Sprintf("Expected %q to have been called with:\n%v\nbut actual calls were:\n %v", methodName, arguments, strings.Join(calledWithArgs, "\n")))
}
return true
}
// AssertNotCalled asserts that the method was not called.
// It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method.
func (m *Mock) AssertNotCalled(t TestingT, methodName string, arguments ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
m.mutex.Lock()
defer m.mutex.Unlock()
if m.methodWasCalled(methodName, arguments) {
return assert.Fail(t, "Should not have called with given arguments",
fmt.Sprintf("Expected %q to not have been called with:\n%v\nbut actually it was.", methodName, arguments))
}
return true
}
func (m *Mock) methodWasCalled(methodName string, expected []interface{}) bool {
for _, call := range m.calls() {
if call.Method == methodName {
_, differences := Arguments(expected).Diff(call.Arguments)
if differences == 0 {
// found the expected call
return true
}
}
}
// we didn't find the expected call
return false
}
func (m *Mock) expectedCalls() []*Call {
return append([]*Call{}, m.ExpectedCalls...)
}
func (m *Mock) calls() []Call {
return append([]Call{}, m.Calls...)
}
/*
Arguments
*/
// Arguments holds an array of method arguments or return values.
type Arguments []interface{}
const (
// Anything is used in Diff and Assert when the argument being tested
// shouldn't be taken into consideration.
Anything = "mock.Anything"
)
// AnythingOfTypeArgument is a string that contains the type of an argument
// for use when type checking. Used in Diff and Assert.
type AnythingOfTypeArgument string
// AnythingOfType returns an AnythingOfTypeArgument object containing the
// name of the type to check for. Used in Diff and Assert.
//
// For example:
// Assert(t, AnythingOfType("string"), AnythingOfType("int"))
func AnythingOfType(t string) AnythingOfTypeArgument {
return AnythingOfTypeArgument(t)
}
// argumentMatcher performs custom argument matching, returning whether or
// not the argument is matched by the expectation fixture function.
type argumentMatcher struct {
// fn is a function which accepts one argument, and returns a bool.
fn reflect.Value
}
func (f argumentMatcher) Matches(argument interface{}) bool {
expectType := f.fn.Type().In(0)
expectTypeNilSupported := false
switch expectType.Kind() {
case reflect.Interface, reflect.Chan, reflect.Func, reflect.Map, reflect.Slice, reflect.Ptr:
expectTypeNilSupported = true
}
argType := reflect.TypeOf(argument)
var arg reflect.Value
if argType == nil {
arg = reflect.New(expectType).Elem()
} else {
arg = reflect.ValueOf(argument)
}
if argType == nil && !expectTypeNilSupported {
panic(errors.New("attempting to call matcher with nil for non-nil expected type"))
}
if argType == nil || argType.AssignableTo(expectType) {
result := f.fn.Call([]reflect.Value{arg})
return result[0].Bool()
}
return false
}
func (f argumentMatcher) String() string {
return fmt.Sprintf("func(%s) bool", f.fn.Type().In(0).Name())
}
// MatchedBy can be used to match a mock call based on only certain properties
// from a complex struct or some calculation. It takes a function that will be
// evaluated with the called argument and will return true when there's a match
// and false otherwise.
//
// Example:
// m.On("Do", MatchedBy(func(req *http.Request) bool { return req.Host == "example.com" }))
//
// |fn|, must be a function accepting a single argument (of the expected type)
// which returns a bool. If |fn| doesn't match the required signature,
// MatchedBy() panics.
func MatchedBy(fn interface{}) argumentMatcher {
fnType := reflect.TypeOf(fn)
if fnType.Kind() != reflect.Func {
panic(fmt.Sprintf("assert: arguments: %s is not a func", fn))
}
if fnType.NumIn() != 1 {
panic(fmt.Sprintf("assert: arguments: %s does not take exactly one argument", fn))
}
if fnType.NumOut() != 1 || fnType.Out(0).Kind() != reflect.Bool {
panic(fmt.Sprintf("assert: arguments: %s does not return a bool", fn))
}
return argumentMatcher{fn: reflect.ValueOf(fn)}
}
// Get Returns the argument at the specified index.
func (args Arguments) Get(index int) interface{} {
if index+1 > len(args) {
panic(fmt.Sprintf("assert: arguments: Cannot call Get(%d) because there are %d argument(s).", index, len(args)))
}
return args[index]
}
// Is gets whether the objects match the arguments specified.
func (args Arguments) Is(objects ...interface{}) bool {
for i, obj := range args {
if obj != objects[i] {
return false
}
}
return true
}
// Diff gets a string describing the differences between the arguments
// and the specified objects.
//
// Returns the diff string and number of differences found.
func (args Arguments) Diff(objects []interface{}) (string, int) {
//TODO: could return string as error and nil for No difference
var output = "\n"
var differences int
var maxArgCount = len(args)
if len(objects) > maxArgCount {
maxArgCount = len(objects)
}
for i := 0; i < maxArgCount; i++ {
var actual, expected interface{}
var actualFmt, expectedFmt string
if len(objects) <= i {
actual = "(Missing)"
actualFmt = "(Missing)"
} else {
actual = objects[i]
actualFmt = fmt.Sprintf("(%[1]T=%[1]v)", actual)
}
if len(args) <= i {
expected = "(Missing)"
expectedFmt = "(Missing)"
} else {
expected = args[i]
expectedFmt = fmt.Sprintf("(%[1]T=%[1]v)", expected)
}
if matcher, ok := expected.(argumentMatcher); ok {
if matcher.Matches(actual) {
output = fmt.Sprintf("%s\t%d: PASS: %s matched by %s\n", output, i, actualFmt, matcher)
} else {
differences++
output = fmt.Sprintf("%s\t%d: PASS: %s not matched by %s\n", output, i, actualFmt, matcher)
}
} else if reflect.TypeOf(expected) == reflect.TypeOf((*AnythingOfTypeArgument)(nil)).Elem() {
// type checking
if reflect.TypeOf(actual).Name() != string(expected.(AnythingOfTypeArgument)) && reflect.TypeOf(actual).String() != string(expected.(AnythingOfTypeArgument)) {
// not match
differences++
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt)
}
} else {
// normal checking
if assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) {
// match
output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, actualFmt, expectedFmt)
} else {
// not match
differences++
output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, actualFmt, expectedFmt)
}
}
}
if differences == 0 {
return "No differences.", differences
}
return output, differences
}
// Assert compares the arguments with the specified objects and fails if
// they do not exactly match.
func (args Arguments) Assert(t TestingT, objects ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
// get the differences
diff, diffCount := args.Diff(objects)
if diffCount == 0 {
return true
}
// there are differences... report them...
t.Logf(diff)
t.Errorf("%sArguments do not match.", assert.CallerInfo())
return false
}
// String gets the argument at the specified index. Panics if there is no argument, or
// if the argument is of the wrong type.
//
// If no index is provided, String() returns a complete string representation
// of the arguments.
func (args Arguments) String(indexOrNil ...int) string {
if len(indexOrNil) == 0 {
// normal String() method - return a string representation of the args
var argsStr []string
for _, arg := range args {
argsStr = append(argsStr, fmt.Sprintf("%s", reflect.TypeOf(arg)))
}
return strings.Join(argsStr, ",")
} else if len(indexOrNil) == 1 {
// Index has been specified - get the argument at that index
var index = indexOrNil[0]
var s string
var ok bool
if s, ok = args.Get(index).(string); !ok {
panic(fmt.Sprintf("assert: arguments: String(%d) failed because object wasn't correct type: %s", index, args.Get(index)))
}
return s
}
panic(fmt.Sprintf("assert: arguments: Wrong number of arguments passed to String. Must be 0 or 1, not %d", len(indexOrNil)))
}
// Int gets the argument at the specified index. Panics if there is no argument, or
// if the argument is of the wrong type.
func (args Arguments) Int(index int) int {
var s int
var ok bool
if s, ok = args.Get(index).(int); !ok {
panic(fmt.Sprintf("assert: arguments: Int(%d) failed because object wasn't correct type: %v", index, args.Get(index)))
}
return s
}
// Error gets the argument at the specified index. Panics if there is no argument, or
// if the argument is of the wrong type.
func (args Arguments) Error(index int) error {
obj := args.Get(index)
var s error
var ok bool
if obj == nil {
return nil
}
if s, ok = obj.(error); !ok {
panic(fmt.Sprintf("assert: arguments: Error(%d) failed because object wasn't correct type: %v", index, args.Get(index)))
}
return s
}
// Bool gets the argument at the specified index. Panics if there is no argument, or
// if the argument is of the wrong type.
func (args Arguments) Bool(index int) bool {
var s bool
var ok bool
if s, ok = args.Get(index).(bool); !ok {
panic(fmt.Sprintf("assert: arguments: Bool(%d) failed because object wasn't correct type: %v", index, args.Get(index)))
}
return s
}
func typeAndKind(v interface{}) (reflect.Type, reflect.Kind) {
t := reflect.TypeOf(v)
k := t.Kind()
if k == reflect.Ptr {
t = t.Elem()
k = t.Kind()
}
return t, k
}
func diffArguments(expected Arguments, actual Arguments) string {
if len(expected) != len(actual) {
return fmt.Sprintf("Provided %v arguments, mocked for %v arguments", len(expected), len(actual))
}
for x := range expected {
if diffString := diff(expected[x], actual[x]); diffString != "" {
return fmt.Sprintf("Difference found in argument %v:\n\n%s", x, diffString)
}
}
return ""
}
// diff returns a diff of both values as long as both are of the same type and
// are a struct, map, slice or array. Otherwise it returns an empty string.
func diff(expected interface{}, actual interface{}) string {
if expected == nil || actual == nil {
return ""
}
et, ek := typeAndKind(expected)
at, _ := typeAndKind(actual)
if et != at {
return ""
}
if ek != reflect.Struct && ek != reflect.Map && ek != reflect.Slice && ek != reflect.Array {
return ""
}
e := spewConfig.Sdump(expected)
a := spewConfig.Sdump(actual)
diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
A: difflib.SplitLines(e),
B: difflib.SplitLines(a),
FromFile: "Expected",
FromDate: "",
ToFile: "Actual",
ToDate: "",
Context: 1,
})
return diff
}
var spewConfig = spew.ConfigState{
Indent: " ",
DisablePointerAddresses: true,
DisableCapacities: true,
SortKeys: true,
}
type tHelper interface {
Helper()
}