2019-03-11 16:56:48 +00:00
|
|
|
package linodev4
|
2018-09-23 11:01:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-07-30 19:19:32 +00:00
|
|
|
"github.com/go-acme/lego/v3/platform/tester"
|
2018-09-23 11:01:40 +00:00
|
|
|
"github.com/linode/linodego"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
type MockResponseMap map[string]interface{}
|
2018-09-23 11:01:40 +00:00
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
var envTest = tester.NewEnvTest(EnvToken)
|
2018-09-23 11:01:40 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func newMockServer(responses MockResponseMap) *httptest.Server {
|
2018-09-23 11:01:40 +00:00
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Ensure that we support the requested action.
|
|
|
|
action := r.Method + ":" + r.URL.Path
|
|
|
|
resp, ok := responses[action]
|
|
|
|
if !ok {
|
2018-10-12 17:29:18 +00:00
|
|
|
http.Error(w, fmt.Sprintf("Unsupported mock action: %q", action), http.StatusInternalServerError)
|
|
|
|
return
|
2018-09-23 11:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rawResponse, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
2018-10-12 17:29:18 +00:00
|
|
|
http.Error(w, fmt.Sprintf("Failed to JSON encode response: %v", err), http.StatusInternalServerError)
|
|
|
|
return
|
2018-09-23 11:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send the response.
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
if err, ok := resp.(linodego.APIError); ok {
|
|
|
|
if err.Errors[0].Reason == "Not found" {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
2018-09-24 19:07:20 +00:00
|
|
|
|
|
|
|
_, err = w.Write(rawResponse)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2018-09-23 11:01:40 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
return srv
|
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvToken: "123",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing api key",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvToken: "",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
expected: "linodev4: some credentials information are missing: LINODE_TOKEN",
|
|
|
|
},
|
|
|
|
}
|
2018-09-23 11:01:40 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
|
|
|
envTest.Apply(test.envVars)
|
2018-09-23 11:01:40 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
p, err := NewDNSProvider()
|
2018-09-23 11:01:40 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
if len(test.expected) == 0 {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-09-23 11:01:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
apiKey string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
apiKey: "123",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
expected: "linodev4: Linode Access Token missing",
|
|
|
|
},
|
|
|
|
}
|
2018-09-23 11:01:40 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Token = test.apiKey
|
2018-09-23 11:01:40 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
p, err := NewDNSProviderConfig(config)
|
2018-09-23 11:01:40 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
if len(test.expected) == 0 {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-09-23 11:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_Present(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
defer envTest.RestoreEnv()
|
2020-03-11 22:51:10 +00:00
|
|
|
os.Setenv(EnvToken, "testing")
|
2018-09-23 11:01:40 +00:00
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
2018-09-23 11:01:40 +00:00
|
|
|
|
|
|
|
domain := "example.com"
|
|
|
|
keyAuth := "dGVzdGluZw=="
|
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
mockResponses MockResponseMap
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "Success",
|
|
|
|
mockResponses: MockResponseMap{
|
|
|
|
"GET:/domains": linodego.DomainsPagedResponse{
|
|
|
|
PageOptions: &linodego.PageOptions{
|
|
|
|
Pages: 1,
|
|
|
|
Results: 1,
|
|
|
|
Page: 1,
|
|
|
|
},
|
|
|
|
Data: []linodego.Domain{{
|
|
|
|
Domain: domain,
|
|
|
|
ID: 1234,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
"POST:/domains/1234/records": linodego.DomainRecord{
|
|
|
|
ID: 1234,
|
|
|
|
},
|
2018-09-23 11:01:40 +00:00
|
|
|
},
|
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
{
|
|
|
|
desc: "NoDomain",
|
|
|
|
mockResponses: MockResponseMap{
|
|
|
|
"GET:/domains": linodego.APIError{
|
|
|
|
Errors: []linodego.APIErrorReason{{
|
|
|
|
Reason: "Not found",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: "[404] Not found",
|
2018-09-23 11:01:40 +00:00
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
{
|
|
|
|
desc: "CreateFailed",
|
|
|
|
mockResponses: MockResponseMap{
|
|
|
|
"GET:/domains": &linodego.DomainsPagedResponse{
|
|
|
|
PageOptions: &linodego.PageOptions{
|
|
|
|
Pages: 1,
|
|
|
|
Results: 1,
|
|
|
|
Page: 1,
|
|
|
|
},
|
|
|
|
Data: []linodego.Domain{{
|
|
|
|
Domain: "foobar.com",
|
|
|
|
ID: 1234,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
"POST:/domains/1234/records": linodego.APIError{
|
|
|
|
Errors: []linodego.APIErrorReason{{
|
|
|
|
Reason: "Failed to create domain resource",
|
|
|
|
Field: "somefield",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: "[400] [somefield] Failed to create domain resource",
|
2018-09-23 11:01:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2018-10-12 17:29:18 +00:00
|
|
|
server := newMockServer(test.mockResponses)
|
|
|
|
defer server.Close()
|
2018-09-23 11:01:40 +00:00
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
assert.NotNil(t, p.client)
|
2018-10-12 17:29:18 +00:00
|
|
|
p.client.SetBaseURL(server.URL)
|
2018-09-23 11:01:40 +00:00
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
err = p.Present(domain, "", keyAuth)
|
|
|
|
if len(test.expectedError) == 0 {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, test.expectedError)
|
|
|
|
}
|
|
|
|
})
|
2018-09-23 11:01:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_CleanUp(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
defer envTest.RestoreEnv()
|
2020-03-11 22:51:10 +00:00
|
|
|
os.Setenv(EnvToken, "testing")
|
2018-09-23 11:01:40 +00:00
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2018-09-23 11:01:40 +00:00
|
|
|
|
|
|
|
domain := "example.com"
|
|
|
|
keyAuth := "dGVzdGluZw=="
|
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
mockResponses MockResponseMap
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "Success",
|
|
|
|
mockResponses: MockResponseMap{
|
|
|
|
"GET:/domains": &linodego.DomainsPagedResponse{
|
|
|
|
PageOptions: &linodego.PageOptions{
|
|
|
|
Pages: 1,
|
|
|
|
Results: 1,
|
|
|
|
Page: 1,
|
|
|
|
},
|
|
|
|
Data: []linodego.Domain{{
|
|
|
|
Domain: "foobar.com",
|
|
|
|
ID: 1234,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
"GET:/domains/1234/records": &linodego.DomainRecordsPagedResponse{
|
|
|
|
PageOptions: &linodego.PageOptions{
|
|
|
|
Pages: 1,
|
|
|
|
Results: 1,
|
|
|
|
Page: 1,
|
|
|
|
},
|
|
|
|
Data: []linodego.DomainRecord{{
|
|
|
|
ID: 1234,
|
|
|
|
Name: "_acme-challenge",
|
|
|
|
Target: "ElbOJKOkFWiZLQeoxf-wb3IpOsQCdvoM0y_wn0TEkxM",
|
|
|
|
Type: "TXT",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
"DELETE:/domains/1234/records/1234": struct{}{},
|
2018-09-23 11:01:40 +00:00
|
|
|
},
|
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
{
|
|
|
|
desc: "NoDomain",
|
|
|
|
mockResponses: MockResponseMap{
|
|
|
|
"GET:/domains": linodego.APIError{
|
|
|
|
Errors: []linodego.APIErrorReason{{
|
|
|
|
Reason: "Not found",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
"GET:/domains/1234/records": linodego.APIError{
|
|
|
|
Errors: []linodego.APIErrorReason{{
|
|
|
|
Reason: "Not found",
|
|
|
|
}},
|
|
|
|
},
|
2018-09-23 11:01:40 +00:00
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
expectedError: "[404] Not found",
|
2018-09-23 11:01:40 +00:00
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
{
|
|
|
|
desc: "DeleteFailed",
|
|
|
|
mockResponses: MockResponseMap{
|
|
|
|
"GET:/domains": linodego.DomainsPagedResponse{
|
|
|
|
PageOptions: &linodego.PageOptions{
|
|
|
|
Pages: 1,
|
|
|
|
Results: 1,
|
|
|
|
Page: 1,
|
|
|
|
},
|
|
|
|
Data: []linodego.Domain{{
|
|
|
|
ID: 1234,
|
|
|
|
Domain: "example.com",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
"GET:/domains/1234/records": linodego.DomainRecordsPagedResponse{
|
|
|
|
PageOptions: &linodego.PageOptions{
|
|
|
|
Pages: 1,
|
|
|
|
Results: 1,
|
|
|
|
Page: 1,
|
|
|
|
},
|
|
|
|
Data: []linodego.DomainRecord{{
|
|
|
|
ID: 1234,
|
|
|
|
Name: "_acme-challenge",
|
|
|
|
Target: "ElbOJKOkFWiZLQeoxf-wb3IpOsQCdvoM0y_wn0TEkxM",
|
|
|
|
Type: "TXT",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
"DELETE:/domains/1234/records/1234": linodego.APIError{
|
|
|
|
Errors: []linodego.APIErrorReason{{
|
|
|
|
Reason: "Failed to delete domain resource",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: "[400] Failed to delete domain resource",
|
2018-09-23 11:01:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2018-10-12 17:29:18 +00:00
|
|
|
server := newMockServer(test.mockResponses)
|
|
|
|
defer server.Close()
|
2018-09-23 11:01:40 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
p.client.SetBaseURL(server.URL)
|
2018-09-23 11:01:40 +00:00
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
err = p.CleanUp(domain, "", keyAuth)
|
|
|
|
if len(test.expectedError) == 0 {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, test.expectedError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-09-23 11:01:40 +00:00
|
|
|
}
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
func TestLivePresent(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2018-10-12 17:29:18 +00:00
|
|
|
t.Skip("Skipping live test")
|
|
|
|
}
|
|
|
|
// TODO implement this test
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveCleanUp(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2018-10-12 17:29:18 +00:00
|
|
|
t.Skip("Skipping live test")
|
|
|
|
}
|
|
|
|
// TODO implement this test
|
|
|
|
}
|