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 (
|
|
|
|
LinodeResponse struct {
|
|
|
|
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 (
|
|
|
|
apiKey string
|
|
|
|
isTestLive bool
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
apiKey = os.Getenv("LINODE_API_KEY")
|
|
|
|
isTestLive = len(apiKey) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func restoreEnv() {
|
|
|
|
os.Setenv("LINODE_API_KEY", apiKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockServer(t *testing.T, responses MockResponseMap) *httptest.Server {
|
|
|
|
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-06-11 15:32:50 +00:00
|
|
|
require.FailNowf(t, "Unsupported mock action: %s", action)
|
2016-08-29 19:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the response that the server will return.
|
|
|
|
linodeResponse := LinodeResponse{
|
|
|
|
Action: action,
|
|
|
|
Data: resp.Response,
|
|
|
|
Errors: resp.Errors,
|
|
|
|
}
|
|
|
|
rawResponse, err := json.Marshal(linodeResponse)
|
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("Failed to JSON encode response: %v", err)
|
|
|
|
require.FailNow(t, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the response.
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write(rawResponse)
|
|
|
|
}))
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
return srv
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderWithEnv(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
|
|
|
_, err := NewDNSProvider()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderWithoutEnv(t *testing.T) {
|
|
|
|
defer restoreEnv()
|
2018-06-11 15:32:50 +00:00
|
|
|
os.Setenv("LINODE_API_KEY", "")
|
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
_, err := NewDNSProvider()
|
2018-06-11 15:32:50 +00:00
|
|
|
assert.EqualError(t, err, "Linode: some credentials information are missing: LINODE_API_KEY")
|
2016-08-29 19:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderCredentialsWithKey(t *testing.T) {
|
|
|
|
_, err := NewDNSProviderCredentials("testing")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderCredentialsWithoutKey(t *testing.T) {
|
|
|
|
_, err := NewDNSProviderCredentials("")
|
|
|
|
assert.EqualError(t, err, "Linode credentials missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
domain := "example.com"
|
|
|
|
keyAuth := "dGVzdGluZw=="
|
|
|
|
mockResponses := MockResponseMap{
|
|
|
|
"domain.list": MockResponse{
|
|
|
|
Response: []dns.Domain{
|
2018-06-11 15:32:50 +00:00
|
|
|
{
|
2016-08-29 19:37:19 +00:00
|
|
|
Domain: domain,
|
|
|
|
DomainID: 1234,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"domain.resource.create": MockResponse{
|
|
|
|
Response: dns.ResourceResponse{
|
|
|
|
ResourceID: 1234,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
mockSrv := newMockServer(t, mockResponses)
|
|
|
|
defer mockSrv.Close()
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
p.linode.ToLinode().SetEndpoint(mockSrv.URL)
|
|
|
|
|
|
|
|
err = p.Present(domain, "", keyAuth)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_PresentNoDomain(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()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
domain := "example.com"
|
|
|
|
keyAuth := "dGVzdGluZw=="
|
|
|
|
mockResponses := MockResponseMap{
|
|
|
|
"domain.list": MockResponse{
|
|
|
|
Response: []dns.Domain{
|
2018-06-11 15:32:50 +00:00
|
|
|
{
|
2016-08-29 19:37:19 +00:00
|
|
|
Domain: "foobar.com",
|
|
|
|
DomainID: 1234,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
mockSrv := newMockServer(t, mockResponses)
|
|
|
|
defer mockSrv.Close()
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
p.linode.ToLinode().SetEndpoint(mockSrv.URL)
|
|
|
|
|
|
|
|
err = p.Present(domain, "", keyAuth)
|
|
|
|
assert.EqualError(t, err, "dns: requested domain not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_PresentCreateFailed(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()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
domain := "example.com"
|
|
|
|
keyAuth := "dGVzdGluZw=="
|
|
|
|
mockResponses := MockResponseMap{
|
|
|
|
"domain.list": MockResponse{
|
|
|
|
Response: []dns.Domain{
|
2018-06-11 15:32:50 +00:00
|
|
|
{
|
2016-08-29 19:37:19 +00:00
|
|
|
Domain: domain,
|
|
|
|
DomainID: 1234,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"domain.resource.create": MockResponse{
|
|
|
|
Response: nil,
|
|
|
|
Errors: []linode.ResponseError{
|
2018-06-11 15:32:50 +00:00
|
|
|
{
|
2016-08-29 19:37:19 +00:00
|
|
|
Code: 1234,
|
|
|
|
Message: "Failed to create domain resource",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
mockSrv := newMockServer(t, mockResponses)
|
|
|
|
defer mockSrv.Close()
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
p.linode.ToLinode().SetEndpoint(mockSrv.URL)
|
|
|
|
|
|
|
|
err = p.Present(domain, "", keyAuth)
|
|
|
|
assert.EqualError(t, err, "Failed to create domain resource")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_PresentLive(t *testing.T) {
|
|
|
|
if !isTestLive {
|
|
|
|
t.Skip("Skipping live test")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
domain := "example.com"
|
|
|
|
keyAuth := "dGVzdGluZw=="
|
|
|
|
mockResponses := MockResponseMap{
|
|
|
|
"domain.list": MockResponse{
|
|
|
|
Response: []dns.Domain{
|
2018-06-11 15:32:50 +00:00
|
|
|
{
|
2016-08-29 19:37:19 +00:00
|
|
|
Domain: domain,
|
|
|
|
DomainID: 1234,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"domain.resource.list": MockResponse{
|
|
|
|
Response: []dns.Resource{
|
2018-06-11 15:32:50 +00:00
|
|
|
{
|
2016-08-29 19:37:19 +00:00
|
|
|
DomainID: 1234,
|
|
|
|
Name: "_acme-challenge",
|
|
|
|
ResourceID: 1234,
|
|
|
|
Target: "ElbOJKOkFWiZLQeoxf-wb3IpOsQCdvoM0y_wn0TEkxM",
|
|
|
|
Type: "TXT",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"domain.resource.delete": MockResponse{
|
|
|
|
Response: dns.ResourceResponse{
|
|
|
|
ResourceID: 1234,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
mockSrv := newMockServer(t, mockResponses)
|
|
|
|
defer mockSrv.Close()
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
p.linode.ToLinode().SetEndpoint(mockSrv.URL)
|
|
|
|
|
|
|
|
err = p.CleanUp(domain, "", keyAuth)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_CleanUpNoDomain(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()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
domain := "example.com"
|
|
|
|
keyAuth := "dGVzdGluZw=="
|
|
|
|
mockResponses := MockResponseMap{
|
|
|
|
"domain.list": MockResponse{
|
|
|
|
Response: []dns.Domain{
|
2018-06-11 15:32:50 +00:00
|
|
|
{
|
2016-08-29 19:37:19 +00:00
|
|
|
Domain: "foobar.com",
|
|
|
|
DomainID: 1234,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
mockSrv := newMockServer(t, mockResponses)
|
|
|
|
defer mockSrv.Close()
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
p.linode.ToLinode().SetEndpoint(mockSrv.URL)
|
|
|
|
|
|
|
|
err = p.CleanUp(domain, "", keyAuth)
|
|
|
|
assert.EqualError(t, err, "dns: requested domain not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_CleanUpDeleteFailed(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()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
domain := "example.com"
|
|
|
|
keyAuth := "dGVzdGluZw=="
|
|
|
|
mockResponses := MockResponseMap{
|
|
|
|
"domain.list": MockResponse{
|
|
|
|
Response: []dns.Domain{
|
2018-06-11 15:32:50 +00:00
|
|
|
{
|
2016-08-29 19:37:19 +00:00
|
|
|
Domain: domain,
|
|
|
|
DomainID: 1234,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"domain.resource.list": MockResponse{
|
|
|
|
Response: []dns.Resource{
|
2018-06-11 15:32:50 +00:00
|
|
|
{
|
2016-08-29 19:37:19 +00:00
|
|
|
DomainID: 1234,
|
|
|
|
Name: "_acme-challenge",
|
|
|
|
ResourceID: 1234,
|
|
|
|
Target: "ElbOJKOkFWiZLQeoxf-wb3IpOsQCdvoM0y_wn0TEkxM",
|
|
|
|
Type: "TXT",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"domain.resource.delete": MockResponse{
|
|
|
|
Response: nil,
|
|
|
|
Errors: []linode.ResponseError{
|
2018-06-11 15:32:50 +00:00
|
|
|
{
|
2016-08-29 19:37:19 +00:00
|
|
|
Code: 1234,
|
|
|
|
Message: "Failed to delete domain resource",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
mockSrv := newMockServer(t, mockResponses)
|
|
|
|
defer mockSrv.Close()
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
p.linode.ToLinode().SetEndpoint(mockSrv.URL)
|
|
|
|
|
|
|
|
err = p.CleanUp(domain, "", keyAuth)
|
|
|
|
assert.EqualError(t, err, "Failed to delete domain resource")
|
|
|
|
}
|