forked from TrueCloudLab/lego
vegadns: refactor tests. (#910)
This commit is contained in:
parent
346d3222d7
commit
83618fed79
1 changed files with 29 additions and 59 deletions
|
@ -1,13 +1,9 @@
|
||||||
// Package vegadns implements a DNS provider for solving the DNS-01
|
|
||||||
// challenge using VegaDNS.
|
|
||||||
package vegadns
|
package vegadns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -18,12 +14,8 @@ import (
|
||||||
|
|
||||||
const testDomain = "example.com"
|
const testDomain = "example.com"
|
||||||
|
|
||||||
const ipPort = "127.0.0.1:2112"
|
|
||||||
|
|
||||||
var envTest = tester.NewEnvTest("SECRET_VEGADNS_KEY", "SECRET_VEGADNS_SECRET", "VEGADNS_URL")
|
var envTest = tester.NewEnvTest("SECRET_VEGADNS_KEY", "SECRET_VEGADNS_SECRET", "VEGADNS_URL")
|
||||||
|
|
||||||
type muxCallback func() *http.ServeMux
|
|
||||||
|
|
||||||
func TestNewDNSProvider_Fail(t *testing.T) {
|
func TestNewDNSProvider_Fail(t *testing.T) {
|
||||||
defer envTest.RestoreEnv()
|
defer envTest.RestoreEnv()
|
||||||
envTest.ClearEnv()
|
envTest.ClearEnv()
|
||||||
|
@ -36,37 +28,35 @@ func TestDNSProvider_TimeoutSuccess(t *testing.T) {
|
||||||
defer envTest.RestoreEnv()
|
defer envTest.RestoreEnv()
|
||||||
envTest.ClearEnv()
|
envTest.ClearEnv()
|
||||||
|
|
||||||
ts, err := startTestServer(muxSuccess)
|
tearDown := startTestServer(muxSuccess())
|
||||||
require.NoError(t, err)
|
defer tearDown()
|
||||||
|
|
||||||
defer ts.Close()
|
|
||||||
|
|
||||||
provider, err := NewDNSProvider()
|
provider, err := NewDNSProvider()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
timeout, interval := provider.Timeout()
|
timeout, interval := provider.Timeout()
|
||||||
assert.Equal(t, timeout, time.Duration(720000000000))
|
assert.Equal(t, timeout, 12*time.Minute)
|
||||||
assert.Equal(t, interval, time.Duration(60000000000))
|
assert.Equal(t, interval, 1*time.Minute)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDNSProvider_Present(t *testing.T) {
|
func TestDNSProvider_Present(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
callback muxCallback
|
handler http.Handler
|
||||||
expectedError string
|
expectedError string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
desc: "Success",
|
desc: "Success",
|
||||||
callback: muxSuccess,
|
handler: muxSuccess(),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "FailToFindZone",
|
desc: "FailToFindZone",
|
||||||
callback: muxFailToFindZone,
|
handler: muxFailToFindZone(),
|
||||||
expectedError: "vegadns: can't find Authoritative Zone for _acme-challenge.example.com. in Present: Unable to find auth zone for fqdn _acme-challenge.example.com",
|
expectedError: "vegadns: can't find Authoritative Zone for _acme-challenge.example.com. in Present: Unable to find auth zone for fqdn _acme-challenge.example.com",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "FailToCreateTXT",
|
desc: "FailToCreateTXT",
|
||||||
callback: muxFailToCreateTXT,
|
handler: muxFailToCreateTXT(),
|
||||||
expectedError: "vegadns: Got bad answer from VegaDNS on CreateTXT. Code: 400. Message: ",
|
expectedError: "vegadns: Got bad answer from VegaDNS on CreateTXT. Code: 400. Message: ",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -76,10 +66,8 @@ func TestDNSProvider_Present(t *testing.T) {
|
||||||
defer envTest.RestoreEnv()
|
defer envTest.RestoreEnv()
|
||||||
envTest.ClearEnv()
|
envTest.ClearEnv()
|
||||||
|
|
||||||
ts, err := startTestServer(test.callback)
|
tearDown := startTestServer(test.handler)
|
||||||
require.NoError(t, err)
|
defer tearDown()
|
||||||
|
|
||||||
defer ts.Close()
|
|
||||||
|
|
||||||
provider, err := NewDNSProvider()
|
provider, err := NewDNSProvider()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -97,21 +85,21 @@ func TestDNSProvider_Present(t *testing.T) {
|
||||||
func TestDNSProvider_CleanUp(t *testing.T) {
|
func TestDNSProvider_CleanUp(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
callback muxCallback
|
handler http.Handler
|
||||||
expectedError string
|
expectedError string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
desc: "Success",
|
desc: "Success",
|
||||||
callback: muxSuccess,
|
handler: muxSuccess(),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "FailToFindZone",
|
desc: "FailToFindZone",
|
||||||
callback: muxFailToFindZone,
|
handler: muxFailToFindZone(),
|
||||||
expectedError: "vegadns: can't find Authoritative Zone for _acme-challenge.example.com. in CleanUp: Unable to find auth zone for fqdn _acme-challenge.example.com",
|
expectedError: "vegadns: can't find Authoritative Zone for _acme-challenge.example.com. in CleanUp: Unable to find auth zone for fqdn _acme-challenge.example.com",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "FailToGetRecordID",
|
desc: "FailToGetRecordID",
|
||||||
callback: muxFailToGetRecordID,
|
handler: muxFailToGetRecordID(),
|
||||||
expectedError: "vegadns: couldn't get Record ID in CleanUp: Got bad answer from VegaDNS on GetRecordID. Code: 404. Message: ",
|
expectedError: "vegadns: couldn't get Record ID in CleanUp: Got bad answer from VegaDNS on GetRecordID. Code: 404. Message: ",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -121,10 +109,8 @@ func TestDNSProvider_CleanUp(t *testing.T) {
|
||||||
defer envTest.RestoreEnv()
|
defer envTest.RestoreEnv()
|
||||||
envTest.ClearEnv()
|
envTest.ClearEnv()
|
||||||
|
|
||||||
ts, err := startTestServer(test.callback)
|
tearDown := startTestServer(test.handler)
|
||||||
require.NoError(t, err)
|
defer tearDown()
|
||||||
|
|
||||||
defer ts.Close()
|
|
||||||
|
|
||||||
provider, err := NewDNSProvider()
|
provider, err := NewDNSProvider()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -287,32 +273,16 @@ func muxFailToGetRecordID() *http.ServeMux {
|
||||||
return mux
|
return mux
|
||||||
}
|
}
|
||||||
|
|
||||||
// Starts and returns a test server using a custom ip/port. Defer close() afterwards.
|
func startTestServer(handler http.Handler) func() {
|
||||||
func startTestServer(callback muxCallback) (*httptest.Server, error) {
|
ts := httptest.NewServer(handler)
|
||||||
err := os.Setenv("SECRET_VEGADNS_KEY", "key")
|
|
||||||
if err != nil {
|
envTest.Apply(map[string]string{
|
||||||
return nil, err
|
"SECRET_VEGADNS_KEY": "key",
|
||||||
|
"SECRET_VEGADNS_SECRET": "secret",
|
||||||
|
"VEGADNS_URL": ts.URL,
|
||||||
|
})
|
||||||
|
|
||||||
|
return func() {
|
||||||
|
ts.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.Setenv("SECRET_VEGADNS_SECRET", "secret")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = os.Setenv("VEGADNS_URL", "http://"+ipPort)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
ts := httptest.NewUnstartedServer(callback())
|
|
||||||
|
|
||||||
l, err := net.Listen("tcp", ipPort)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
ts.Listener = l
|
|
||||||
ts.Start()
|
|
||||||
|
|
||||||
return ts, nil
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue