2019-03-11 16:56:48 +00:00
|
|
|
package memcached
|
2016-10-24 09:03:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/http01"
|
2016-10-24 09:03:18 +00:00
|
|
|
"github.com/rainycape/memcache"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-24 19:07:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-10-24 09:03:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
domain = "lego.test"
|
|
|
|
token = "foo"
|
|
|
|
keyAuth = "bar"
|
|
|
|
)
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
var memcachedHosts = loadMemcachedHosts()
|
|
|
|
|
|
|
|
func loadMemcachedHosts() []string {
|
2016-10-24 09:03:18 +00:00
|
|
|
memcachedHostsStr := os.Getenv("MEMCACHED_HOSTS")
|
|
|
|
if len(memcachedHostsStr) > 0 {
|
2018-12-06 21:50:17 +00:00
|
|
|
return strings.Split(memcachedHostsStr, ",")
|
2016-10-24 09:03:18 +00:00
|
|
|
}
|
2018-12-06 21:50:17 +00:00
|
|
|
return nil
|
2016-10-24 09:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewMemcachedProviderEmpty(t *testing.T) {
|
|
|
|
emptyHosts := make([]string, 0)
|
|
|
|
_, err := NewMemcachedProvider(emptyHosts)
|
2023-10-31 13:08:50 +00:00
|
|
|
require.EqualError(t, err, "no memcached hosts provided")
|
2016-10-24 09:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewMemcachedProviderValid(t *testing.T) {
|
|
|
|
if len(memcachedHosts) == 0 {
|
|
|
|
t.Skip("Skipping memcached tests")
|
|
|
|
}
|
|
|
|
_, err := NewMemcachedProvider(memcachedHosts)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-24 09:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemcachedPresentSingleHost(t *testing.T) {
|
|
|
|
if len(memcachedHosts) == 0 {
|
|
|
|
t.Skip("Skipping memcached tests")
|
|
|
|
}
|
|
|
|
p, err := NewMemcachedProvider(memcachedHosts[0:1])
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-24 09:03:18 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
challengePath := path.Join("/", http01.ChallengePath(token))
|
2016-10-24 09:03:18 +00:00
|
|
|
|
|
|
|
err = p.Present(domain, token, keyAuth)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-24 09:03:18 +00:00
|
|
|
mc, err := memcache.New(memcachedHosts[0])
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-24 09:03:18 +00:00
|
|
|
i, err := mc.Get(challengePath)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-24 09:03:18 +00:00
|
|
|
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)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-24 09:03:18 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
challengePath := path.Join("/", http01.ChallengePath(token))
|
2016-10-24 09:03:18 +00:00
|
|
|
|
|
|
|
err = p.Present(domain, token, keyAuth)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-24 09:03:18 +00:00
|
|
|
for _, host := range memcachedHosts {
|
|
|
|
mc, err := memcache.New(host)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-24 09:03:18 +00:00
|
|
|
i, err := mc.Get(challengePath)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-24 09:03:18 +00:00
|
|
|
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)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-24 09:03:18 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
challengePath := path.Join("/", http01.ChallengePath(token))
|
2016-10-24 09:03:18 +00:00
|
|
|
|
|
|
|
err = p.Present(domain, token, keyAuth)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-24 09:03:18 +00:00
|
|
|
for _, host := range memcachedHosts {
|
|
|
|
mc, err := memcache.New(host)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-24 09:03:18 +00:00
|
|
|
i, err := mc.Get(challengePath)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-24 09:03:18 +00:00
|
|
|
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)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, p.CleanUp(domain, token, keyAuth))
|
2016-10-24 09:03:18 +00:00
|
|
|
}
|