lego/providers/http/memcached/memcached_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

111 lines
2.7 KiB
Go

package memcached
import (
"os"
"path"
"strings"
"testing"
"github.com/rainycape/memcache"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/challenge/http01"
)
const (
domain = "lego.test"
token = "foo"
keyAuth = "bar"
)
var memcachedHosts = loadMemcachedHosts()
func loadMemcachedHosts() []string {
memcachedHostsStr := os.Getenv("MEMCACHED_HOSTS")
if len(memcachedHostsStr) > 0 {
return strings.Split(memcachedHostsStr, ",")
}
return nil
}
func TestNewMemcachedProviderEmpty(t *testing.T) {
emptyHosts := make([]string, 0)
_, err := NewMemcachedProvider(emptyHosts)
assert.EqualError(t, err, "no memcached hosts provided")
}
func TestNewMemcachedProviderValid(t *testing.T) {
if len(memcachedHosts) == 0 {
t.Skip("Skipping memcached tests")
}
_, err := NewMemcachedProvider(memcachedHosts)
require.NoError(t, err)
}
func TestMemcachedPresentSingleHost(t *testing.T) {
if len(memcachedHosts) == 0 {
t.Skip("Skipping memcached tests")
}
p, err := NewMemcachedProvider(memcachedHosts[0:1])
require.NoError(t, err)
challengePath := path.Join("/", http01.ChallengePath(token))
err = p.Present(domain, token, keyAuth)
require.NoError(t, err)
mc, err := memcache.New(memcachedHosts[0])
require.NoError(t, err)
i, err := mc.Get(challengePath)
require.NoError(t, err)
assert.Equal(t, i.Value, []byte(keyAuth))
}
func TestMemcachedPresentMultiHost(t *testing.T) {
if len(memcachedHosts) <= 1 {
t.Skip("Skipping memcached multi-host tests")
}
p, err := NewMemcachedProvider(memcachedHosts)
require.NoError(t, err)
challengePath := path.Join("/", http01.ChallengePath(token))
err = p.Present(domain, token, keyAuth)
require.NoError(t, err)
for _, host := range memcachedHosts {
mc, err := memcache.New(host)
require.NoError(t, err)
i, err := mc.Get(challengePath)
require.NoError(t, err)
assert.Equal(t, i.Value, []byte(keyAuth))
}
}
func TestMemcachedPresentPartialFailureMultiHost(t *testing.T) {
if len(memcachedHosts) == 0 {
t.Skip("Skipping memcached tests")
}
hosts := append(memcachedHosts, "5.5.5.5:11211")
p, err := NewMemcachedProvider(hosts)
require.NoError(t, err)
challengePath := path.Join("/", http01.ChallengePath(token))
err = p.Present(domain, token, keyAuth)
require.NoError(t, err)
for _, host := range memcachedHosts {
mc, err := memcache.New(host)
require.NoError(t, err)
i, err := mc.Get(challengePath)
require.NoError(t, err)
assert.Equal(t, i.Value, []byte(keyAuth))
}
}
func TestMemcachedCleanup(t *testing.T) {
if len(memcachedHosts) == 0 {
t.Skip("Skipping memcached tests")
}
p, err := NewMemcachedProvider(memcachedHosts)
require.NoError(t, err)
require.NoError(t, p.CleanUp(domain, token, keyAuth))
}