lego/providers/dns/exoscale/exoscale_test.go

207 lines
4.3 KiB
Go
Raw Normal View History

2019-03-11 16:56:48 +00:00
package exoscale
2016-11-07 07:37:57 +00:00
import (
"testing"
"time"
2020-09-02 01:20:01 +00:00
"github.com/go-acme/lego/v4/platform/tester"
2016-11-07 07:37:57 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2016-11-07 07:37:57 +00:00
)
const envDomain = envNamespace + "DOMAIN"
var envTest = tester.NewEnvTest(
EnvAPISecret,
EnvAPIKey).
WithDomain(envDomain)
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
desc string
envVars map[string]string
expected string
}{
{
desc: "success",
envVars: map[string]string{
EnvAPIKey: "123",
EnvAPISecret: "456",
},
},
{
desc: "missing credentials",
envVars: map[string]string{
EnvAPIKey: "",
EnvAPISecret: "",
},
expected: "exoscale: some credentials information are missing: EXOSCALE_API_KEY,EXOSCALE_API_SECRET",
},
{
desc: "missing access key",
envVars: map[string]string{
EnvAPIKey: "",
EnvAPISecret: "456",
},
expected: "exoscale: some credentials information are missing: EXOSCALE_API_KEY",
},
{
desc: "missing secret key",
envVars: map[string]string{
EnvAPIKey: "123",
EnvAPISecret: "",
},
expected: "exoscale: some credentials information are missing: EXOSCALE_API_SECRET",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
2021-03-04 19:16:59 +00:00
if test.expected == "" {
require.NoError(t, err)
require.NotNil(t, p)
require.NotNil(t, p.config)
require.NotNil(t, p.client)
} else {
require.EqualError(t, err, test.expected)
}
})
}
2016-11-07 07:37:57 +00:00
}
func TestNewDNSProviderConfig(t *testing.T) {
testCases := []struct {
desc string
apiKey string
apiSecret string
expected string
}{
{
desc: "success",
apiKey: "123",
apiSecret: "456",
},
{
desc: "missing credentials",
expected: "exoscale: credentials missing",
},
{
desc: "missing api key",
apiSecret: "456",
expected: "exoscale: credentials missing",
},
{
desc: "missing secret key",
apiKey: "123",
expected: "exoscale: credentials missing",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
config := NewDefaultConfig()
config.APIKey = test.apiKey
config.APISecret = test.apiSecret
p, err := NewDNSProviderConfig(config)
2021-03-04 19:16:59 +00:00
if test.expected == "" {
require.NoError(t, err)
require.NotNil(t, p)
require.NotNil(t, p.config)
require.NotNil(t, p.client)
} else {
require.EqualError(t, err, test.expected)
}
})
}
2016-11-07 07:37:57 +00:00
}
func TestDNSProvider_FindZoneAndRecordName(t *testing.T) {
config := NewDefaultConfig()
config.APIKey = "example@example.com"
config.APISecret = "123"
provider, err := NewDNSProviderConfig(config)
require.NoError(t, err)
2016-11-07 07:37:57 +00:00
type expected struct {
zone string
recordName string
}
2016-11-07 07:37:57 +00:00
testCases := []struct {
desc string
fqdn string
expected expected
}{
{
2022-11-25 17:12:21 +00:00
desc: "Extract root record name",
2024-02-24 19:49:40 +00:00
fqdn: "_acme-challenge.example.com.",
expected: expected{
2024-02-24 19:49:40 +00:00
zone: "example.com",
recordName: "_acme-challenge",
},
},
{
2022-11-25 17:12:21 +00:00
desc: "Extract sub record name",
2024-02-24 19:49:40 +00:00
fqdn: "_acme-challenge.foo.example.com.",
expected: expected{
2024-02-24 19:49:40 +00:00
zone: "example.com",
recordName: "_acme-challenge.foo",
},
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
2016-11-07 07:37:57 +00:00
2022-11-25 17:12:21 +00:00
zone, recordName, err := provider.findZoneAndRecordName(test.fqdn)
require.NoError(t, err)
assert.Equal(t, test.expected.zone, zone)
assert.Equal(t, test.expected.recordName, recordName)
})
}
2016-11-07 07:37:57 +00:00
}
func TestLivePresent(t *testing.T) {
if !envTest.IsLiveTest() {
2016-11-07 07:37:57 +00:00
t.Skip("skipping live test")
}
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
2016-11-07 07:37:57 +00:00
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
2016-11-07 07:37:57 +00:00
// Present Twice to handle create / update
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
2016-11-07 07:37:57 +00:00
}
func TestLiveCleanUp(t *testing.T) {
if !envTest.IsLiveTest() {
2016-11-07 07:37:57 +00:00
t.Skip("skipping live test")
}
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
2016-11-07 07:37:57 +00:00
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
2016-11-07 07:37:57 +00:00
}