From 1d1b08ac1557d7c28f860d2cc9dea544ee85ed04 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Thu, 18 Oct 2018 22:32:00 +0200 Subject: [PATCH] enhance the `WithLiveTestRequirements` algorithm (#681) * refactor: migrate test to envTest. * refactor: enforce WithLiveTestRequirements logic. * chore: remove gometalinter. * fix: godoc. * doc: add Docker badge. --- .gometalinter.json | 25 --------------- README.md | 6 ++-- platform/tester/env.go | 19 +++++++---- providers/dns/auroradns/auroradns.go | 1 + providers/dns/dns_providers_test.go | 41 +++++++++--------------- providers/dns/dnsmadeeasy/dnsmadeeasy.go | 1 + providers/dns/dreamhost/dreamhost.go | 2 +- providers/dns/duckdns/duckdns.go | 2 +- 8 files changed, 34 insertions(+), 63 deletions(-) delete mode 100644 .gometalinter.json diff --git a/.gometalinter.json b/.gometalinter.json deleted file mode 100644 index 3817a8b7..00000000 --- a/.gometalinter.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "Vendor": true, - "Test": true, - "Sort": [ - "path", - "line", - "column", - "linter", - "severity" - ], - "Cyclo": 12, - "Enable": [ - "gotypex", - "varcheck", - "gotype", - "interfacer", - "misspell", - "ineffassign", - "golint", - "vet", - "gosimple" - ], - "Exclude": [], - "Deadline": "2m" -} diff --git a/README.md b/README.md index 6e2b926f..528b1741 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,9 @@ Let's Encrypt client and ACME library written in Go -[![GoDoc](https://godoc.org/github.com/xenolf/lego/acme?status.svg)](https://godoc.org/github.com/xenolf/lego/acme) +[![GoDoc](https://godoc.org/github.com/xenolf/lego?status.svg)](https://godoc.org/github.com/xenolf/lego/acme) [![Build Status](https://travis-ci.org/xenolf/lego.svg?branch=master)](https://travis-ci.org/xenolf/lego) +[![Docker Pulls](https://img.shields.io/docker/pulls/xenolf/lego.svg)](https://hub.docker.com/r/xenolf/lego/) [![Dev Chat](https://img.shields.io/badge/dev%20chat-gitter-blue.svg?label=dev+chat)](https://gitter.im/xenolf/lego) [![Beerpay](https://beerpay.io/xenolf/lego/badge.svg)](https://beerpay.io/xenolf/lego) @@ -20,8 +21,7 @@ To install from source, just run: go get -u github.com/xenolf/lego ``` -To build lego inside a Docker container, just run - +To build lego inside a Docker container, just run: ```bash docker build -t lego . ``` diff --git a/platform/tester/env.go b/platform/tester/env.go index b42da278..943842db 100644 --- a/platform/tester/env.go +++ b/platform/tester/env.go @@ -45,7 +45,12 @@ func (e *EnvTest) WithDomain(key string) *EnvTest { // Replaces the default behavior (all keys are required). func (e *EnvTest) WithLiveTestRequirements(keys ...string) *EnvTest { var countValuedVars int + for _, key := range keys { + if e.domainKey != key && !e.isManagedKey(key) { + panic(fmt.Sprintf("Unauthorized action, the env var %s is not managed or it's not the key of the domain.", key)) + } + if _, ok := e.values[key]; ok { countValuedVars++ } @@ -120,15 +125,15 @@ func (e *EnvTest) liveTestExtra() bool { // Not related to the main environment variables. func (e *EnvTest) Apply(envVars map[string]string) { for key, value := range envVars { - if e.isManagedKey(key) { - if len(value) == 0 { - os.Unsetenv(key) - } else { - os.Setenv(key, value) - } - } else { + if !e.isManagedKey(key) { panic(fmt.Sprintf("Unauthorized action, the env var %s is not managed.", key)) } + + if len(value) == 0 { + os.Unsetenv(key) + } else { + os.Setenv(key, value) + } } } diff --git a/providers/dns/auroradns/auroradns.go b/providers/dns/auroradns/auroradns.go index d3cb76da..0553d85e 100644 --- a/providers/dns/auroradns/auroradns.go +++ b/providers/dns/auroradns/auroradns.go @@ -1,3 +1,4 @@ +// Package auroradns implements a DNS provider for solving the DNS-01 challenge using Aurora DNS. package auroradns import ( diff --git a/providers/dns/dns_providers_test.go b/providers/dns/dns_providers_test.go index c953a2b2..230aab0e 100644 --- a/providers/dns/dns_providers_test.go +++ b/providers/dns/dns_providers_test.go @@ -1,51 +1,40 @@ package dns import ( - "os" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/xenolf/lego/providers/dns/exoscale" + "github.com/xenolf/lego/platform/tester" + "github.com/xenolf/lego/providers/dns/exec" ) -var ( - apiKey string - apiSecret string -) - -func init() { - apiSecret = os.Getenv("EXOSCALE_API_SECRET") - apiKey = os.Getenv("EXOSCALE_API_KEY") -} - -func restoreExoscaleEnv() { - os.Setenv("EXOSCALE_API_KEY", apiKey) - os.Setenv("EXOSCALE_API_SECRET", apiSecret) -} +var envTest = tester.NewEnvTest("EXEC_PATH") func TestKnownDNSProviderSuccess(t *testing.T) { - defer restoreExoscaleEnv() - os.Setenv("EXOSCALE_API_KEY", "abc") - os.Setenv("EXOSCALE_API_SECRET", "123") + defer envTest.RestoreEnv() + envTest.Apply(map[string]string{ + "EXEC_PATH": "abc", + }) - provider, err := NewDNSChallengeProviderByName("exoscale") + provider, err := NewDNSChallengeProviderByName("exec") require.NoError(t, err) assert.NotNil(t, provider) - assert.IsType(t, &exoscale.DNSProvider{}, provider, "Not loaded correct DNS provider") + assert.IsType(t, &exec.DNSProvider{}, provider, "The loaded DNS provider doesn't have the expected type.") } func TestKnownDNSProviderError(t *testing.T) { - defer restoreExoscaleEnv() - os.Setenv("EXOSCALE_API_KEY", "") - os.Setenv("EXOSCALE_API_SECRET", "") + defer envTest.RestoreEnv() + envTest.ClearEnv() - _, err := NewDNSChallengeProviderByName("exoscale") + provider, err := NewDNSChallengeProviderByName("exec") assert.Error(t, err) + assert.Nil(t, provider) } func TestUnknownDNSProvider(t *testing.T) { - _, err := NewDNSChallengeProviderByName("foobar") + provider, err := NewDNSChallengeProviderByName("foobar") assert.Error(t, err) + assert.Nil(t, provider) } diff --git a/providers/dns/dnsmadeeasy/dnsmadeeasy.go b/providers/dns/dnsmadeeasy/dnsmadeeasy.go index 519a4451..d73d0afc 100644 --- a/providers/dns/dnsmadeeasy/dnsmadeeasy.go +++ b/providers/dns/dnsmadeeasy/dnsmadeeasy.go @@ -1,3 +1,4 @@ +// Package dnsmadeeasy implements a DNS provider for solving the DNS-01 challenge using DNS Made Easy. package dnsmadeeasy import ( diff --git a/providers/dns/dreamhost/dreamhost.go b/providers/dns/dreamhost/dreamhost.go index cc0e3633..9edd27b7 100644 --- a/providers/dns/dreamhost/dreamhost.go +++ b/providers/dns/dreamhost/dreamhost.go @@ -1,4 +1,4 @@ -// Package dreamhost Adds lego support for http://dreamhost.com DNS updates +// Package dreamhost implements a DNS provider for solving the DNS-01 challenge using DreamHost. // See https://help.dreamhost.com/hc/en-us/articles/217560167-API_overview // and https://help.dreamhost.com/hc/en-us/articles/217555707-DNS-API-commands for the API spec. package dreamhost diff --git a/providers/dns/duckdns/duckdns.go b/providers/dns/duckdns/duckdns.go index 2ac53f03..7581af17 100644 --- a/providers/dns/duckdns/duckdns.go +++ b/providers/dns/duckdns/duckdns.go @@ -1,4 +1,4 @@ -// Package duckdns Adds lego support for http://duckdns.org. +// Package duckdns implements a DNS provider for solving the DNS-01 challenge using DuckDNS. // See http://www.duckdns.org/spec.jsp for more info on updating TXT records. package duckdns