lego/providers/dns/lightsail/lightsail_test.go

86 lines
2.1 KiB
Go
Raw Permalink Normal View History

2019-03-11 16:56:48 +00:00
package lightsail
import (
"os"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/lightsail"
2020-09-02 01:20:01 +00:00
"github.com/go-acme/lego/v4/platform/tester"
"github.com/stretchr/testify/require"
)
const (
envAwsNamespace = "AWS_"
envAwsAccessKeyID = envAwsNamespace + "ACCESS_KEY_ID"
envAwsSecretAccessKey = envAwsNamespace + "SECRET_ACCESS_KEY"
envAwsRegion = envAwsNamespace + "REGION"
envAwsHostedZoneID = envAwsNamespace + "HOSTED_ZONE_ID"
)
var envTest = tester.NewEnvTest(
envAwsAccessKeyID,
envAwsSecretAccessKey,
envAwsRegion,
envAwsHostedZoneID).
WithDomain(EnvDNSZone).
WithLiveTestRequirements(envAwsAccessKeyID, envAwsSecretAccessKey, EnvDNSZone)
2020-12-28 22:39:00 +00:00
func makeProvider(serverURL string) (*DNSProvider, error) {
config := &aws.Config{
Credentials: credentials.NewStaticCredentials("abc", "123", " "),
2020-12-28 22:39:00 +00:00
Endpoint: aws.String(serverURL),
Region: aws.String("mock-region"),
MaxRetries: aws.Int(1),
}
sess, err := session.NewSession(config)
if err != nil {
return nil, err
}
conf := NewDefaultConfig()
client := lightsail.New(sess)
return &DNSProvider{client: client, config: conf}, nil
}
func TestCredentialsFromEnv(t *testing.T) {
defer envTest.RestoreEnv()
envTest.ClearEnv()
2020-12-28 22:39:00 +00:00
_ = os.Setenv(envAwsAccessKeyID, "123")
_ = os.Setenv(envAwsSecretAccessKey, "123")
_ = os.Setenv(envAwsRegion, "us-east-1")
config := &aws.Config{
CredentialsChainVerboseErrors: aws.Bool(true),
}
sess, err := session.NewSession(config)
require.NoError(t, err)
_, err = sess.Config.Credentials.Get()
require.NoError(t, err, "Expected credentials to be set from environment")
}
func TestDNSProvider_Present(t *testing.T) {
mockResponses := map[string]MockResponse{
"/": {StatusCode: 200, Body: ""},
}
2020-12-28 22:39:00 +00:00
serverURL := newMockServer(t, mockResponses)
2020-12-28 22:39:00 +00:00
provider, err := makeProvider(serverURL)
require.NoError(t, err)
domain := "example.com"
keyAuth := "123456d=="
err = provider.Present(domain, "", keyAuth)
require.NoError(t, err, "Expected Present to return no error")
}