2016-08-29 19:37:19 +00:00
|
|
|
package linode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/timewasted/linode"
|
|
|
|
"github.com/timewasted/linode/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2018-10-12 17:29:18 +00:00
|
|
|
apiResponse struct {
|
2016-08-29 19:37:19 +00:00
|
|
|
Action string `json:"ACTION"`
|
|
|
|
Data interface{} `json:"DATA"`
|
|
|
|
Errors []linode.ResponseError `json:"ERRORARRAY"`
|
|
|
|
}
|
|
|
|
MockResponse struct {
|
|
|
|
Response interface{}
|
|
|
|
Errors []linode.ResponseError
|
|
|
|
}
|
|
|
|
MockResponseMap map[string]MockResponse
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-10-12 17:29:18 +00:00
|
|
|
apiKey string
|
|
|
|
liveTest bool
|
2016-08-29 19:37:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
apiKey = os.Getenv("LINODE_API_KEY")
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
liveTest = len(apiKey) != 0
|
2016-08-29 19:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func restoreEnv() {
|
|
|
|
os.Setenv("LINODE_API_KEY", apiKey)
|
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func newMockServer(responses MockResponseMap) *httptest.Server {
|
2016-08-29 19:37:19 +00:00
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Ensure that we support the requested action.
|
|
|
|
action := r.URL.Query().Get("api_action")
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Build the response that the server will return.
|
2018-10-12 17:29:18 +00:00
|
|
|
response := apiResponse{
|
2016-08-29 19:37:19 +00:00
|
|
|
Action: action,
|
|
|
|
Data: resp.Response,
|
|
|
|
Errors: resp.Errors,
|
|
|
|
}
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
rawResponse, err := json.Marshal(response)
|
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")
|
|
|
|
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)
|
|
|
|
return
|
|
|
|
}
|
2016-08-29 19:37:19 +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{
|
|
|
|
"LINODE_API_KEY": "123",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing api key",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"LINODE_API_KEY": "",
|
|
|
|
},
|
|
|
|
expected: "linode: some credentials information are missing: LINODE_API_KEY",
|
|
|
|
},
|
|
|
|
}
|
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) {
|
|
|
|
defer restoreEnv()
|
|
|
|
for key, value := range test.envVars {
|
|
|
|
if len(value) == 0 {
|
|
|
|
os.Unsetenv(key)
|
|
|
|
} else {
|
|
|
|
os.Setenv(key, value)
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
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",
|
|
|
|
expected: "linode: credentials missing",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
defer restoreEnv()
|
|
|
|
os.Unsetenv("LINODE_API_KEY")
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = 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
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-08-29 19:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_Present(t *testing.T) {
|
|
|
|
defer restoreEnv()
|
2018-06-11 15:32:50 +00:00
|
|
|
os.Setenv("LINODE_API_KEY", "testing")
|
|
|
|
|
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
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
mockResponses: MockResponseMap{
|
|
|
|
"domain.list": MockResponse{
|
|
|
|
Response: []dns.Domain{
|
|
|
|
{
|
|
|
|
Domain: domain,
|
|
|
|
DomainID: 1234,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"domain.resource.create": MockResponse{
|
|
|
|
Response: dns.ResourceResponse{
|
|
|
|
ResourceID: 1234,
|
|
|
|
},
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
{
|
|
|
|
desc: "NoDomain",
|
|
|
|
mockResponses: MockResponseMap{
|
|
|
|
"domain.list": MockResponse{
|
|
|
|
Response: []dns.Domain{{
|
|
|
|
Domain: "foobar.com",
|
|
|
|
DomainID: 1234,
|
|
|
|
}},
|
|
|
|
},
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
expectedError: "dns: requested domain not found",
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
{
|
|
|
|
desc: "CreateFailed",
|
|
|
|
mockResponses: MockResponseMap{
|
|
|
|
"domain.list": MockResponse{
|
|
|
|
Response: []dns.Domain{
|
|
|
|
{
|
|
|
|
Domain: domain,
|
|
|
|
DomainID: 1234,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"domain.resource.create": MockResponse{
|
|
|
|
Response: nil,
|
|
|
|
Errors: []linode.ResponseError{
|
|
|
|
{
|
|
|
|
Code: 1234,
|
|
|
|
Message: "Failed to create domain resource",
|
|
|
|
},
|
|
|
|
},
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
expectedError: "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) {
|
2018-10-12 17:29:18 +00:00
|
|
|
server := newMockServer(test.mockResponses)
|
|
|
|
defer server.Close()
|
2016-08-29 19:37:19 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
p.client.ToLinode().SetEndpoint(server.URL)
|
2018-06-11 15:32:50 +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)
|
|
|
|
}
|
|
|
|
})
|
2016-08-29 19:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_CleanUp(t *testing.T) {
|
|
|
|
defer restoreEnv()
|
2018-06-11 15:32:50 +00:00
|
|
|
os.Setenv("LINODE_API_KEY", "testing")
|
|
|
|
|
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
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
mockResponses: MockResponseMap{
|
|
|
|
"domain.list": MockResponse{
|
|
|
|
Response: []dns.Domain{
|
|
|
|
{
|
|
|
|
Domain: domain,
|
|
|
|
DomainID: 1234,
|
|
|
|
},
|
|
|
|
},
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
"domain.resource.list": MockResponse{
|
|
|
|
Response: []dns.Resource{
|
|
|
|
{
|
|
|
|
DomainID: 1234,
|
|
|
|
Name: "_acme-challenge",
|
|
|
|
ResourceID: 1234,
|
|
|
|
Target: "ElbOJKOkFWiZLQeoxf-wb3IpOsQCdvoM0y_wn0TEkxM",
|
|
|
|
Type: "TXT",
|
|
|
|
},
|
|
|
|
},
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
"domain.resource.delete": MockResponse{
|
|
|
|
Response: dns.ResourceResponse{
|
|
|
|
ResourceID: 1234,
|
|
|
|
},
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
{
|
|
|
|
desc: "NoDomain",
|
|
|
|
mockResponses: MockResponseMap{
|
|
|
|
"domain.list": MockResponse{
|
|
|
|
Response: []dns.Domain{
|
|
|
|
{
|
|
|
|
Domain: "foobar.com",
|
|
|
|
DomainID: 1234,
|
|
|
|
},
|
|
|
|
},
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
expectedError: "dns: requested domain not found",
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
{
|
|
|
|
desc: "DeleteFailed",
|
|
|
|
mockResponses: MockResponseMap{
|
|
|
|
"domain.list": MockResponse{
|
|
|
|
Response: []dns.Domain{
|
|
|
|
{
|
|
|
|
Domain: domain,
|
|
|
|
DomainID: 1234,
|
|
|
|
},
|
|
|
|
},
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
"domain.resource.list": MockResponse{
|
|
|
|
Response: []dns.Resource{
|
|
|
|
{
|
|
|
|
DomainID: 1234,
|
|
|
|
Name: "_acme-challenge",
|
|
|
|
ResourceID: 1234,
|
|
|
|
Target: "ElbOJKOkFWiZLQeoxf-wb3IpOsQCdvoM0y_wn0TEkxM",
|
|
|
|
Type: "TXT",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"domain.resource.delete": MockResponse{
|
|
|
|
Response: nil,
|
|
|
|
Errors: []linode.ResponseError{
|
|
|
|
{
|
|
|
|
Code: 1234,
|
|
|
|
Message: "Failed to delete domain resource",
|
|
|
|
},
|
|
|
|
},
|
2016-08-29 19:37:19 +00:00
|
|
|
},
|
|
|
|
},
|
2018-09-24 19:07:20 +00:00
|
|
|
expectedError: "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) {
|
2018-10-12 17:29:18 +00:00
|
|
|
server := newMockServer(test.mockResponses)
|
|
|
|
defer server.Close()
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
p.client.ToLinode().SetEndpoint(server.URL)
|
2016-08-29 19:37:19 +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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-08-29 19:37:19 +00:00
|
|
|
}
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
func TestLivePresent(t *testing.T) {
|
|
|
|
if !liveTest {
|
|
|
|
t.Skip("Skipping live test")
|
|
|
|
}
|
|
|
|
// TODO implement this test
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveCleanUp(t *testing.T) {
|
|
|
|
if !liveTest {
|
|
|
|
t.Skip("Skipping live test")
|
|
|
|
}
|
|
|
|
// TODO implement this test
|
|
|
|
}
|