tests: refactor memcached tests

This commit is contained in:
Fernandez Ludovic 2026-01-28 19:52:34 +01:00
commit 757802bd26
3 changed files with 32 additions and 5 deletions

View file

@ -1,4 +1,4 @@
# Memcached http provider
# Memcached HTTP provider
Publishes challenges into memcached where they can be retrieved by nginx. Allows
specifying multiple memcached servers and the responses will be published to all
@ -13,3 +13,17 @@ Example nginx config:
memcached_pass 127.0.0.1:11211;
}
```
## Local Development
```bash
docker run -d --rm -p 11211:11211 memcached:alpine
```
```bash
MEMCACHED_HOSTS=localhost:11211
```
```go
os.Setenv("MEMCACHED_HOSTS", "localhost:11211")
```

View file

@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"path"
"time"
"github.com/bradfitz/gomemcache/memcache"
"github.com/go-acme/lego/v5/challenge/http01"
@ -34,6 +35,9 @@ func (w *HTTPProvider) Present(_ context.Context, _, token, keyAuth string) erro
for _, host := range w.hosts {
mc := memcache.New(host)
// Only because this is slow on GitHub action.
mc.Timeout = 1 * time.Second
item := &memcache.Item{
Key: challengePath,
Value: []byte(keyAuth),
@ -48,7 +52,7 @@ func (w *HTTPProvider) Present(_ context.Context, _, token, keyAuth string) erro
}
if len(errs) == len(w.hosts) {
return fmt.Errorf("unable to store key in any of the memcache hosts: %w", errors.Join(errs...))
return fmt.Errorf("unable to store key in any of the memcached hosts: %w", errors.Join(errs...))
}
return nil

View file

@ -18,10 +18,9 @@ const (
keyAuth = "bar"
)
var memcachedHosts = loadMemcachedHosts()
func loadMemcachedHosts() []string {
func getMemcachedHosts() []string {
memcachedHostsStr := os.Getenv("MEMCACHED_HOSTS")
if memcachedHostsStr != "" {
return strings.Split(memcachedHostsStr, ",")
}
@ -36,6 +35,8 @@ func TestNewMemcachedProviderEmpty(t *testing.T) {
}
func TestNewMemcachedProviderValid(t *testing.T) {
memcachedHosts := getMemcachedHosts()
if len(memcachedHosts) == 0 {
t.Skip("Skipping memcached tests")
}
@ -45,6 +46,8 @@ func TestNewMemcachedProviderValid(t *testing.T) {
}
func TestMemcachedPresentSingleHost(t *testing.T) {
memcachedHosts := getMemcachedHosts()
if len(memcachedHosts) == 0 {
t.Skip("Skipping memcached tests")
}
@ -65,6 +68,8 @@ func TestMemcachedPresentSingleHost(t *testing.T) {
}
func TestMemcachedPresentMultiHost(t *testing.T) {
memcachedHosts := getMemcachedHosts()
if len(memcachedHosts) <= 1 {
t.Skip("Skipping memcached multi-host tests")
}
@ -87,6 +92,8 @@ func TestMemcachedPresentMultiHost(t *testing.T) {
}
func TestMemcachedPresentPartialFailureMultiHost(t *testing.T) {
memcachedHosts := getMemcachedHosts()
if len(memcachedHosts) == 0 {
t.Skip("Skipping memcached tests")
}
@ -110,6 +117,8 @@ func TestMemcachedPresentPartialFailureMultiHost(t *testing.T) {
}
func TestMemcachedCleanup(t *testing.T) {
memcachedHosts := getMemcachedHosts()
if len(memcachedHosts) == 0 {
t.Skip("Skipping memcached tests")
}