lego/providers/dns/linode/linode_test.go

359 lines
7.7 KiB
Go
Raw Normal View History

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