diff --git a/lib/backend/dynamo/dynamodbbk_test.go b/lib/backend/dynamo/dynamodbbk_test.go index 0b5c05897bfcb..fb73a09ec7ccc 100644 --- a/lib/backend/dynamo/dynamodbbk_test.go +++ b/lib/backend/dynamo/dynamodbbk_test.go @@ -31,6 +31,7 @@ import ( "github.com/gravitational/trace" "github.com/jonboulle/clockwork" log "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/gravitational/teleport/lib/backend" @@ -222,3 +223,22 @@ func TestCreateTable(t *testing.T) { }) } } + + +func TestKeyPrefix(t *testing.T) { + t.Run("leading separator in key", func(t *testing.T) { + prefixed := prependPrefix(backend.NewKey("test", "llama")) + assert.Equal(t, "teleport/test/llama", prefixed) + + key := trimPrefix(prefixed) + assert.Equal(t, "/test/llama", key.String()) + }) + + t.Run("no leading separator in key", func(t *testing.T) { + prefixed := prependPrefix(backend.Key(".locks/test/llama")) + assert.Equal(t, "teleport.locks/test/llama", prefixed) + + key := trimPrefix(prefixed) + assert.Equal(t, ".locks/test/llama", key.String()) + }) +} diff --git a/lib/backend/etcdbk/etcd_test.go b/lib/backend/etcdbk/etcd_test.go index 540a0a1391fc5..9f0f9a2f42413 100644 --- a/lib/backend/etcdbk/etcd_test.go +++ b/lib/backend/etcdbk/etcd_test.go @@ -29,6 +29,7 @@ import ( "github.com/gravitational/trace" "github.com/jonboulle/clockwork" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/gravitational/teleport/lib/backend" @@ -258,3 +259,29 @@ func etcdTestEndpoint() string { } return "https://127.0.0.1:2379" } + +func TestKeyPrefix(t *testing.T) { + prefixes := []string{"teleport", "/teleport", "/teleport/"} + + for _, prefix := range prefixes { + t.Run("prefix="+prefix, func(t *testing.T) { + bk := EtcdBackend{cfg: &Config{Key: prefix}} + + t.Run("leading separator in key", func(t *testing.T) { + prefixed := bk.prependPrefix(backend.NewKey("test", "llama")) + assert.Equal(t, prefix+"/test/llama", prefixed) + + key := bk.trimPrefix([]byte(prefixed)) + assert.Equal(t, "/test/llama", key.String()) + }) + + t.Run("no leading separator in key", func(t *testing.T) { + prefixed := bk.prependPrefix(backend.KeyFromString(".locks/test/llama")) + assert.Equal(t, prefix+".locks/test/llama", prefixed) + + key := bk.trimPrefix([]byte(prefixed)) + assert.Equal(t, ".locks/test/llama", key.String()) + }) + }) + } +} diff --git a/lib/backend/helpers.go b/lib/backend/lock.go similarity index 98% rename from lib/backend/helpers.go rename to lib/backend/lock.go index 29bdbbd32f62f..449b8e5493815 100644 --- a/lib/backend/helpers.go +++ b/lib/backend/lock.go @@ -21,11 +21,11 @@ package backend import ( "bytes" "context" + "log/slog" "time" "github.com/google/uuid" "github.com/gravitational/trace" - log "github.com/sirupsen/logrus" ) const ( @@ -205,7 +205,7 @@ func RunWhileLocked(ctx context.Context, cfg RunWhileLockedConfig, fn func(conte case <-cfg.Backend.Clock().After(refreshAfter): if err := lock.resetTTL(ctx, cfg.Backend); err != nil { cancelFunction() - log.Errorf("%v", err) + slog.ErrorContext(ctx, "failed to reset lock ttl", "error", err, "lock", logutils.StringerAttr(lock.key)) return } case <-stopRefresh: diff --git a/lib/backend/helpers_test.go b/lib/backend/lock_test.go similarity index 89% rename from lib/backend/helpers_test.go rename to lib/backend/lock_test.go index b652ee157e83a..783932860a02d 100644 --- a/lib/backend/helpers_test.go +++ b/lib/backend/lock_test.go @@ -23,9 +23,24 @@ import ( "time" "github.com/google/go-cmp/cmp" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) +func TestLockKey(t *testing.T) { + t.Run("empty parts", func(t *testing.T) { + key := lockKey() + assert.Equal(t, ".locks", key.String()) + assert.Equal(t, []string{".locks"}, key.Components()) + }) + + t.Run("with parts", func(t *testing.T) { + key := lockKey("test", "llama") + assert.Equal(t, ".locks/test/llama", key.String()) + assert.Equal(t, []string{".locks", "test", "llama"}, key.Components()) + }) +} + func TestLockConfiguration_CheckAndSetDefaults(t *testing.T) { type mockBackend struct { Backend