2018-02-18 17:27:58 +02: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 02:24:20 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-02-18 17:27:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-10-12 19:29:18 +02:00
|
|
|
envTestSecret string
|
|
|
|
envTestKey string
|
|
|
|
envTestZone string
|
2018-02-18 17:27:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-10-12 19:29:18 +02:00
|
|
|
envTestKey = os.Getenv("AWS_ACCESS_KEY_ID")
|
|
|
|
envTestSecret = os.Getenv("AWS_SECRET_ACCESS_KEY")
|
2018-02-18 17:27:58 +02:00
|
|
|
}
|
|
|
|
|
2018-06-11 17:32:50 +02:00
|
|
|
func restoreEnv() {
|
2018-10-12 19:29:18 +02:00
|
|
|
os.Setenv("AWS_ACCESS_KEY_ID", envTestKey)
|
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", envTestSecret)
|
2018-02-18 17:27:58 +02:00
|
|
|
os.Setenv("AWS_REGION", "us-east-1")
|
2018-10-12 19:29:18 +02:00
|
|
|
os.Setenv("AWS_HOSTED_ZONE_ID", envTestZone)
|
2018-02-18 17:27:58 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func makeProvider(ts *httptest.Server) (*DNSProvider, error) {
|
2018-02-18 17:27:58 +02: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 02:24:20 +02:00
|
|
|
sess, err := session.NewSession(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
conf := NewDefaultConfig()
|
|
|
|
|
2018-07-01 02:24:20 +02:00
|
|
|
client := lightsail.New(sess)
|
2018-09-15 19:07:24 +02:00
|
|
|
return &DNSProvider{client: client, config: conf}, nil
|
2018-02-18 17:27:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCredentialsFromEnv(t *testing.T) {
|
2018-06-11 17:32:50 +02:00
|
|
|
defer restoreEnv()
|
2018-02-18 17:27:58 +02: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 02:24:20 +02: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 17:27:58 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestDNSProvider_Present(t *testing.T) {
|
2018-07-01 02:24:20 +02:00
|
|
|
mockResponses := map[string]MockResponse{
|
|
|
|
"/": {StatusCode: 200, Body: ""},
|
2018-02-18 17:27:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ts := newMockServer(t, mockResponses)
|
|
|
|
defer ts.Close()
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
provider, err := makeProvider(ts)
|
2018-07-01 02:24:20 +02:00
|
|
|
require.NoError(t, err)
|
2018-02-18 17:27:58 +02:00
|
|
|
|
|
|
|
domain := "example.com"
|
|
|
|
keyAuth := "123456d=="
|
|
|
|
|
2018-07-01 02:24:20 +02:00
|
|
|
err = provider.Present(domain, "", keyAuth)
|
|
|
|
require.NoError(t, err, "Expected Present to return no error")
|
2018-02-18 17:27:58 +02:00
|
|
|
}
|