2018-02-18 15:27:58 +00:00
|
|
|
package lightsail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http/httptest"
|
|
|
|
"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"
|
2018-07-01 00:24:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-02-18 15:27:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
lightsailSecret string
|
|
|
|
lightsailKey string
|
|
|
|
lightsailZone string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
lightsailKey = os.Getenv("AWS_ACCESS_KEY_ID")
|
|
|
|
lightsailSecret = os.Getenv("AWS_SECRET_ACCESS_KEY")
|
|
|
|
}
|
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
func restoreEnv() {
|
2018-02-18 15:27:58 +00:00
|
|
|
os.Setenv("AWS_ACCESS_KEY_ID", lightsailKey)
|
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", lightsailSecret)
|
|
|
|
os.Setenv("AWS_REGION", "us-east-1")
|
|
|
|
os.Setenv("AWS_HOSTED_ZONE_ID", lightsailZone)
|
|
|
|
}
|
|
|
|
|
2018-07-01 00:24:20 +00:00
|
|
|
func makeLightsailProvider(ts *httptest.Server) (*DNSProvider, error) {
|
2018-02-18 15:27:58 +00:00
|
|
|
config := &aws.Config{
|
|
|
|
Credentials: credentials.NewStaticCredentials("abc", "123", " "),
|
|
|
|
Endpoint: aws.String(ts.URL),
|
|
|
|
Region: aws.String("mock-region"),
|
|
|
|
MaxRetries: aws.Int(1),
|
|
|
|
}
|
|
|
|
|
2018-07-01 00:24:20 +00:00
|
|
|
sess, err := session.NewSession(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
conf := NewDefaultConfig()
|
|
|
|
|
2018-07-01 00:24:20 +00:00
|
|
|
client := lightsail.New(sess)
|
2018-09-15 17:07:24 +00:00
|
|
|
return &DNSProvider{client: client, config: conf}, nil
|
2018-02-18 15:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCredentialsFromEnv(t *testing.T) {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
2018-02-18 15:27:58 +00:00
|
|
|
os.Setenv("AWS_ACCESS_KEY_ID", "123")
|
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", "123")
|
|
|
|
os.Setenv("AWS_REGION", "us-east-1")
|
|
|
|
|
|
|
|
config := &aws.Config{
|
|
|
|
CredentialsChainVerboseErrors: aws.Bool(true),
|
|
|
|
}
|
|
|
|
|
2018-07-01 00:24:20 +00:00
|
|
|
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")
|
2018-02-18 15:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLightsailPresent(t *testing.T) {
|
2018-07-01 00:24:20 +00:00
|
|
|
mockResponses := map[string]MockResponse{
|
|
|
|
"/": {StatusCode: 200, Body: ""},
|
2018-02-18 15:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ts := newMockServer(t, mockResponses)
|
|
|
|
defer ts.Close()
|
|
|
|
|
2018-07-01 00:24:20 +00:00
|
|
|
provider, err := makeLightsailProvider(ts)
|
|
|
|
require.NoError(t, err)
|
2018-02-18 15:27:58 +00:00
|
|
|
|
|
|
|
domain := "example.com"
|
|
|
|
keyAuth := "123456d=="
|
|
|
|
|
2018-07-01 00:24:20 +00:00
|
|
|
err = provider.Present(domain, "", keyAuth)
|
|
|
|
require.NoError(t, err, "Expected Present to return no error")
|
2018-02-18 15:27:58 +00:00
|
|
|
}
|