lego/platform/tester/env_test.go
Ludovic Fernandez 42941ccea6
Refactor the core of the lib (#700)
- Packages
- Isolate code used by the CLI into the package `cmd`
- (experimental) Add e2e tests for HTTP01, TLS-ALPN-01 and DNS-01, use [Pebble](https://github.com/letsencrypt/pebble) and [challtestsrv](https://github.com/letsencrypt/boulder/tree/master/test/challtestsrv) 
- Support non-ascii domain name (punnycode)
- Check all challenges in a predictable order
- No more global exported variables
- Archive revoked certificates
- Fixes revocation for subdomains and non-ascii domains
- Disable pending authorizations
- use pointer for RemoteError/ProblemDetails
- Poll authz URL instead of challenge URL
- The ability for a DNS provider to solve the challenge sequentially
- Check all nameservers in a predictable order
- Option to disable the complete propagation Requirement
- CLI, support for renew with CSR
- CLI, add SAN on renew
- Add command to list certificates.
- Logs every iteration of waiting for the propagation
- update DNSimple client
- update github.com/miekg/dns
2018-12-06 22:50:17 +01:00

347 lines
9.8 KiB
Go

package tester_test
import (
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/xenolf/lego/platform/tester"
)
const (
envNamespace = "LEGO_TEST_"
envVar01 = envNamespace + "01"
envVar02 = envNamespace + "02"
envVarDomain = envNamespace + "DOMAIN"
)
func TestMain(m *testing.M) {
exitCode := m.Run()
clearEnv()
os.Exit(exitCode)
}
func applyEnv(envVars map[string]string) {
for key, value := range envVars {
if len(value) == 0 {
os.Unsetenv(key)
} else {
os.Setenv(key, value)
}
}
}
func clearEnv() {
environ := os.Environ()
for _, key := range environ {
if strings.HasPrefix(key, envNamespace) {
os.Unsetenv(strings.Split(key, "=")[0])
}
}
os.Unsetenv("EXTRA_LEGO_TEST")
}
func TestEnvTest(t *testing.T) {
testCases := []struct {
desc string
envVars map[string]string
envTestSetup func() *tester.EnvTest
expected func(t *testing.T, envTest *tester.EnvTest)
}{
{
desc: "simple",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.True(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "missing env var",
envVars: map[string]string{
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.False(t, envTest.IsLiveTest())
assert.Equal(t, "", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithDomain",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
envVarDomain: "D",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).WithDomain(envVarDomain)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.True(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetValue(envVarDomain))
assert.Equal(t, "D", envTest.GetDomain())
},
},
{
desc: "WithDomain missing env var",
envVars: map[string]string{
envVar01: "A",
envVarDomain: "D",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).WithDomain(envVarDomain)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.False(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetValue(envVarDomain))
assert.Equal(t, "D", envTest.GetDomain())
},
},
{
desc: "WithDomain missing domain",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).WithDomain(envVarDomain)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.False(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetValue(envVarDomain))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestRequirements",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).WithLiveTestRequirements(envVar02)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.True(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestRequirements non required var missing",
envVars: map[string]string{
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).WithLiveTestRequirements(envVar02)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.True(t, envTest.IsLiveTest())
assert.Equal(t, "", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestRequirements required var missing",
envVars: map[string]string{
envVar01: "A",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).WithLiveTestRequirements(envVar02)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.False(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestRequirements WithDomain",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
envVarDomain: "D",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).
WithDomain(envVarDomain).
WithLiveTestRequirements(envVar02)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.True(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetValue(envVarDomain))
assert.Equal(t, "D", envTest.GetDomain())
},
},
{
desc: "WithLiveTestRequirements WithDomain without domain",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).
WithDomain(envVarDomain).
WithLiveTestRequirements(envVar02)
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.True(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetValue(envVarDomain))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestExtra true",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).
WithLiveTestExtra(func() bool { return true })
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.True(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestExtra false",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).
WithLiveTestExtra(func() bool { return false })
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.False(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestRequirements WithLiveTestExtra true",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).
WithLiveTestRequirements(envVar02).
WithLiveTestExtra(func() bool { return true })
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.True(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestRequirements WithLiveTestExtra false",
envVars: map[string]string{
envVar01: "A",
envVar02: "B",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).
WithLiveTestRequirements(envVar02).
WithLiveTestExtra(func() bool { return false })
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.False(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "B", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
{
desc: "WithLiveTestRequirements require env var missing WithLiveTestExtra true",
envVars: map[string]string{
envVar01: "A",
},
envTestSetup: func() *tester.EnvTest {
return tester.NewEnvTest(envVar01, envVar02).
WithLiveTestRequirements(envVar02).
WithLiveTestExtra(func() bool { return true })
},
expected: func(t *testing.T, envTest *tester.EnvTest) {
assert.False(t, envTest.IsLiveTest())
assert.Equal(t, "A", envTest.GetValue(envVar01))
assert.Equal(t, "", envTest.GetValue(envVar02))
assert.Equal(t, "", envTest.GetDomain())
},
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer clearEnv()
applyEnv(test.envVars)
envTest := test.envTestSetup()
test.expected(t, envTest)
})
}
}
func TestEnvTest_RestoreEnv(t *testing.T) {
os.Setenv(envVar01, "A")
os.Setenv(envVar02, "B")
envTest := tester.NewEnvTest(envVar01, envVar02)
clearEnv()
envTest.RestoreEnv()
assert.Equal(t, "A", os.Getenv(envVar01))
assert.Equal(t, "B", os.Getenv(envVar02))
}
func TestEnvTest_ClearEnv(t *testing.T) {
os.Setenv(envVar01, "A")
os.Setenv(envVar02, "B")
os.Setenv("EXTRA_LEGO_TEST", "X")
envTest := tester.NewEnvTest(envVar01, envVar02)
envTest.ClearEnv()
assert.Equal(t, "", os.Getenv(envVar01))
assert.Equal(t, "", os.Getenv(envVar02))
assert.Equal(t, "X", os.Getenv("EXTRA_LEGO_TEST"))
}