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