2019-03-11 16:56:48 +00:00
|
|
|
package linode
|
2016-08-29 19:37:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2020-09-02 00:51:04 +00:00
|
|
|
"github.com/linode/linodego"
|
2016-08-29 19:37:19 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-09-02 00:51:04 +00:00
|
|
|
type MockResponseMap map[string]interface{}
|
2016-08-29 19:37:19 +00:00
|
|
|
|
2020-09-02 00:51:04 +00:00
|
|
|
var envTest = tester.NewEnvTest(EnvToken)
|
2016-08-29 19:37:19 +00:00
|
|
|
|
2021-11-01 23:52:38 +00:00
|
|
|
func setupTest(t *testing.T, responses MockResponseMap) string {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-08-29 19:37:19 +00:00
|
|
|
// Ensure that we support the requested action.
|
2020-09-02 00:51:04 +00:00
|
|
|
action := r.Method + ":" + r.URL.Path
|
2016-08-29 19:37:19 +00:00
|
|
|
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
|
2016-08-29 19:37:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 00:51:04 +00:00
|
|
|
rawResponse, err := json.Marshal(resp)
|
2016-08-29 19:37:19 +00:00
|
|
|
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
|
2016-08-29 19:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send the response.
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2020-09-02 00:51:04 +00:00
|
|
|
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)
|
2021-11-03 19:39:12 +00:00
|
|
|
return
|
2018-09-24 19:07:20 +00:00
|
|
|
}
|
2021-11-01 23:52:38 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
server := httptest.NewServer(handler)
|
|
|
|
t.Cleanup(server.Close)
|
2016-08-29 19:37:19 +00:00
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2021-11-01 23:52:38 +00:00
|
|
|
|
|
|
|
return server.URL
|
2016-08-29 19:37:19 +00:00
|
|
|
}
|
|
|
|
|
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-09-02 00:51:04 +00:00
|
|
|
EnvToken: "123",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing api key",
|
|
|
|
envVars: map[string]string{
|
2020-09-02 00:51:04 +00:00
|
|
|
EnvToken: "",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
2020-09-02 00:51:04 +00:00
|
|
|
expected: "linode: some credentials information are missing: LINODE_TOKEN",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
}
|
2018-06-11 15:32:50 +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)
|
2016-08-29 19:37:19 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
p, err := NewDNSProvider()
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expected == "" {
|
2018-10-12 17:29:18 +00:00
|
|
|
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-08-29 19:37:19 +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",
|
2020-09-02 00:51:04 +00:00
|
|
|
expected: "linode: Linode Access Token missing",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
2020-09-02 00:51:04 +00:00
|
|
|
config.Token = test.apiKey
|
2016-08-29 19:37:19 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
p, err := NewDNSProviderConfig(config)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expected == "" {
|
2018-10-12 17:29:18 +00:00
|
|
|
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-08-29 19:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_Present(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
defer envTest.RestoreEnv()
|
2020-09-02 00:51:04 +00:00
|
|
|
os.Setenv(EnvToken, "testing")
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
p, err := NewDNSProvider()
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-02 00:51:04 +00:00
|
|
|
require.NotNil(t, p)
|
2016-08-29 19:37:19 +00:00
|
|
|
|
|
|
|
domain := "example.com"
|
|
|
|
keyAuth := "dGVzdGluZw=="
|
2018-09-24 19:07:20 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
mockResponses MockResponseMap
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
2020-09-02 00:51:04 +00:00
|
|
|
desc: "Success",
|
2018-09-24 19:07:20 +00:00
|
|
|
mockResponses: MockResponseMap{
|
2020-09-02 00:51:04 +00:00
|
|
|
"GET:/domains": linodego.DomainsPagedResponse{
|
|
|
|
PageOptions: &linodego.PageOptions{
|
|
|
|
Pages: 1,
|
|
|
|
Results: 1,
|
|
|
|
Page: 1,
|
2018-09-24 19:07:20 +00:00
|
|
|
},
|
2020-09-02 00:51:04 +00:00
|
|
|
Data: []linodego.Domain{{
|
|
|
|
Domain: domain,
|
|
|
|
ID: 1234,
|
|
|
|
}},
|
2018-09-24 19:07:20 +00:00
|
|
|
},
|
2020-09-02 00:51:04 +00:00
|
|
|
"POST:/domains/1234/records": linodego.DomainRecord{
|
|
|
|
ID: 1234,
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
{
|
|
|
|
desc: "NoDomain",
|
|
|
|
mockResponses: MockResponseMap{
|
2020-09-02 00:51:04 +00:00
|
|
|
"GET:/domains": linodego.APIError{
|
|
|
|
Errors: []linodego.APIErrorReason{{
|
|
|
|
Reason: "Not found",
|
2018-09-24 19:07:20 +00:00
|
|
|
}},
|
|
|
|
},
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
2020-09-02 00:51:04 +00:00
|
|
|
expectedError: "[404] Not found",
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
{
|
|
|
|
desc: "CreateFailed",
|
|
|
|
mockResponses: MockResponseMap{
|
2020-09-02 00:51:04 +00:00
|
|
|
"GET:/domains": &linodego.DomainsPagedResponse{
|
|
|
|
PageOptions: &linodego.PageOptions{
|
|
|
|
Pages: 1,
|
|
|
|
Results: 1,
|
|
|
|
Page: 1,
|
2018-09-24 19:07:20 +00:00
|
|
|
},
|
2020-09-02 00:51:04 +00:00
|
|
|
Data: []linodego.Domain{{
|
|
|
|
Domain: "foobar.com",
|
|
|
|
ID: 1234,
|
|
|
|
}},
|
2018-09-24 19:07:20 +00:00
|
|
|
},
|
2020-09-02 00:51:04 +00:00
|
|
|
"POST:/domains/1234/records": linodego.APIError{
|
|
|
|
Errors: []linodego.APIErrorReason{{
|
|
|
|
Reason: "Failed to create domain resource",
|
|
|
|
Field: "somefield",
|
|
|
|
}},
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
|
|
|
},
|
2020-09-02 00:51:04 +00:00
|
|
|
expectedError: "[400] [somefield] Failed to create domain resource",
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2021-11-01 23:52:38 +00:00
|
|
|
serverURL := setupTest(t, test.mockResponses)
|
2016-08-29 19:37:19 +00:00
|
|
|
|
2020-09-02 00:51:04 +00:00
|
|
|
assert.NotNil(t, p.client)
|
2021-11-01 23:52:38 +00:00
|
|
|
p.client.SetBaseURL(serverURL)
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
err = p.Present(domain, "", keyAuth)
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expectedError == "" {
|
2018-09-24 19:07:20 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, test.expectedError)
|
|
|
|
}
|
|
|
|
})
|
2016-08-29 19:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_CleanUp(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
defer envTest.RestoreEnv()
|
2020-09-02 00:51:04 +00:00
|
|
|
os.Setenv(EnvToken, "testing")
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
p, err := NewDNSProvider()
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-08-29 19:37:19 +00:00
|
|
|
|
|
|
|
domain := "example.com"
|
|
|
|
keyAuth := "dGVzdGluZw=="
|
2018-09-24 19:07:20 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
mockResponses MockResponseMap
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
2020-09-02 00:51:04 +00:00
|
|
|
desc: "Success",
|
2018-09-24 19:07:20 +00:00
|
|
|
mockResponses: MockResponseMap{
|
2020-09-02 00:51:04 +00:00
|
|
|
"GET:/domains": &linodego.DomainsPagedResponse{
|
|
|
|
PageOptions: &linodego.PageOptions{
|
|
|
|
Pages: 1,
|
|
|
|
Results: 1,
|
|
|
|
Page: 1,
|
2018-09-24 19:07:20 +00:00
|
|
|
},
|
2020-09-02 00:51:04 +00:00
|
|
|
Data: []linodego.Domain{{
|
|
|
|
Domain: "foobar.com",
|
|
|
|
ID: 1234,
|
|
|
|
}},
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
2020-09-02 00:51:04 +00:00
|
|
|
"GET:/domains/1234/records": &linodego.DomainRecordsPagedResponse{
|
|
|
|
PageOptions: &linodego.PageOptions{
|
|
|
|
Pages: 1,
|
|
|
|
Results: 1,
|
|
|
|
Page: 1,
|
2018-09-24 19:07:20 +00:00
|
|
|
},
|
2020-09-02 00:51:04 +00:00
|
|
|
Data: []linodego.DomainRecord{{
|
|
|
|
ID: 1234,
|
|
|
|
Name: "_acme-challenge",
|
|
|
|
Target: "ElbOJKOkFWiZLQeoxf-wb3IpOsQCdvoM0y_wn0TEkxM",
|
|
|
|
Type: "TXT",
|
|
|
|
}},
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
2020-09-02 00:51:04 +00:00
|
|
|
"DELETE:/domains/1234/records/1234": struct{}{},
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
{
|
|
|
|
desc: "NoDomain",
|
|
|
|
mockResponses: MockResponseMap{
|
2020-09-02 00:51:04 +00:00
|
|
|
"GET:/domains": linodego.APIError{
|
|
|
|
Errors: []linodego.APIErrorReason{{
|
|
|
|
Reason: "Not found",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
"GET:/domains/1234/records": linodego.APIError{
|
|
|
|
Errors: []linodego.APIErrorReason{{
|
|
|
|
Reason: "Not found",
|
|
|
|
}},
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
|
|
|
},
|
2020-09-02 00:51:04 +00:00
|
|
|
expectedError: "[404] Not found",
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
{
|
|
|
|
desc: "DeleteFailed",
|
|
|
|
mockResponses: MockResponseMap{
|
2020-09-02 00:51:04 +00:00
|
|
|
"GET:/domains": linodego.DomainsPagedResponse{
|
|
|
|
PageOptions: &linodego.PageOptions{
|
|
|
|
Pages: 1,
|
|
|
|
Results: 1,
|
|
|
|
Page: 1,
|
2018-09-24 19:07:20 +00:00
|
|
|
},
|
2020-09-02 00:51:04 +00:00
|
|
|
Data: []linodego.Domain{{
|
|
|
|
ID: 1234,
|
|
|
|
Domain: "example.com",
|
|
|
|
}},
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
2020-09-02 00:51:04 +00:00
|
|
|
"GET:/domains/1234/records": linodego.DomainRecordsPagedResponse{
|
|
|
|
PageOptions: &linodego.PageOptions{
|
|
|
|
Pages: 1,
|
|
|
|
Results: 1,
|
|
|
|
Page: 1,
|
2018-09-24 19:07:20 +00:00
|
|
|
},
|
2020-09-02 00:51:04 +00:00
|
|
|
Data: []linodego.DomainRecord{{
|
|
|
|
ID: 1234,
|
|
|
|
Name: "_acme-challenge",
|
|
|
|
Target: "ElbOJKOkFWiZLQeoxf-wb3IpOsQCdvoM0y_wn0TEkxM",
|
|
|
|
Type: "TXT",
|
|
|
|
}},
|
2018-09-24 19:07:20 +00:00
|
|
|
},
|
2020-09-02 00:51:04 +00:00
|
|
|
"DELETE:/domains/1234/records/1234": linodego.APIError{
|
|
|
|
Errors: []linodego.APIErrorReason{{
|
|
|
|
Reason: "Failed to delete domain resource",
|
|
|
|
}},
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
|
|
|
},
|
2020-09-02 00:51:04 +00:00
|
|
|
expectedError: "[400] Failed to delete domain resource",
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2021-11-01 23:52:38 +00:00
|
|
|
serverURL := setupTest(t, test.mockResponses)
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2021-11-01 23:52:38 +00:00
|
|
|
p.client.SetBaseURL(serverURL)
|
2016-08-29 19:37:19 +00:00
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
err = p.CleanUp(domain, "", keyAuth)
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expectedError == "" {
|
2018-09-24 19:07:20 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, test.expectedError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-08-29 19:37:19 +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
|
|
|
|
}
|