Skip to content

Commit

Permalink
server: fix unexported-return using interfaces
Browse files Browse the repository at this point in the history
Signed-off-by: adeyemi <[email protected]>
  • Loading branch information
aladesawe committed Feb 5, 2025
1 parent 8f447cf commit da82a69
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion server/auth/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ func (as *authStore) IsAuthEnabled() bool {
}

// NewAuthStore creates a new AuthStore.
func NewAuthStore(lg *zap.Logger, be AuthBackend, tp TokenProvider, bcryptCost int) *authStore {
func NewAuthStore(lg *zap.Logger, be AuthBackend, tp TokenProvider, bcryptCost int) AuthStore {
if lg == nil {
lg = zap.NewNop()
}
Expand Down
9 changes: 6 additions & 3 deletions server/auth/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
"golang.org/x/crypto/bcrypt"
"google.golang.org/grpc/metadata"
Expand Down Expand Up @@ -116,12 +117,14 @@ func setupAuthStore(t *testing.T) (store *authStore, teardownfunc func(t *testin

// The UserAdd function cannot generate old etcd version user data (user's option is nil)
// add special users through the underlying interface
addUserWithNoOption(as)
asImpl, ok := as.(*authStore)
require.Truef(t, ok, "addUserWithNoOption: needs an AuthStore implementation")
addUserWithNoOption(asImpl)

tearDown := func(_ *testing.T) {
as.Close()
}
return as, tearDown
return asImpl, tearDown
}

func addUserWithNoOption(as *authStore) {
Expand All @@ -136,7 +139,7 @@ func addUserWithNoOption(as *authStore) {
as.refreshRangePermCache(tx)
}

func enableAuthAndCreateRoot(as *authStore) error {
func enableAuthAndCreateRoot(as AuthStore) error {
_, err := as.UserAdd(&pb.AuthUserAddRequest{Name: "root", HashedPassword: encodePassword("root"), Options: &authpb.UserAddOptions{NoPassword: false}})
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type serverVersionAdapter struct {
*EtcdServer
}

func NewServerVersionAdapter(s *EtcdServer) *serverVersionAdapter {
func NewServerVersionAdapter(s *EtcdServer) serverversion.Server {
return &serverVersionAdapter{
EtcdServer: s,
}
Expand Down
1 change: 1 addition & 0 deletions server/storage/mvcc/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type store struct {

// NewStore returns a new store. It is useful to create a store inside
// mvcc pkg. It should only be used for testing externally.
// revive:disable-next-line:unexported-return this is used internally in the mvcc pkg
func NewStore(lg *zap.Logger, b backend.Backend, le lease.Lessor, cfg StoreConfig) *store {
if lg == nil {
lg = zap.NewNop()
Expand Down
2 changes: 1 addition & 1 deletion server/storage/mvcc/watchable_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var _ WatchableKV = (*watchableStore)(nil)
// cancel operations.
type cancelFunc func()

func New(lg *zap.Logger, b backend.Backend, le lease.Lessor, cfg StoreConfig) *watchableStore {
func New(lg *zap.Logger, b backend.Backend, le lease.Lessor, cfg StoreConfig) WatchableKV {
s := newWatchableStore(lg, b, le, cfg)
s.wg.Add(2)
go s.syncWatchersLoop()
Expand Down
21 changes: 13 additions & 8 deletions server/storage/mvcc/watchable_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestWatch(t *testing.T) {
defer w.Close()

w.Watch(0, testKey, nil, 0)
if !s.synced.contains(string(testKey)) {
if !s.(*watchableStore).synced.contains(string(testKey)) {
// the key must have had an entry in synced
t.Errorf("existence = false, want true")
}
Expand All @@ -67,7 +67,7 @@ func TestNewWatcherCancel(t *testing.T) {
t.Error(err)
}

if s.synced.contains(string(testKey)) {
if s.(*watchableStore).synced.contains(string(testKey)) {
// the key shoud have been deleted
t.Errorf("existence = true, want false")
}
Expand Down Expand Up @@ -340,7 +340,9 @@ func TestWatchNoEventLossOnCompact(t *testing.T) {
require.NoError(t, err)
}
// fill up w.Chan() with 1 buf via 2 compacted watch response
s.syncWatchers([]mvccpb.Event{})
sImpl, ok := s.(*watchableStore)
require.Truef(t, ok, "TestWatchNoEventLossOnCompact: needs a WatchableKV implementation")
sImpl.syncWatchers([]mvccpb.Event{})

for len(watchers) > 0 {
resp := <-w.Chan()
Expand All @@ -355,7 +357,7 @@ func TestWatchNoEventLossOnCompact(t *testing.T) {
require.Equalf(t, nextRev, ev.Kv.ModRevision, "got event revision %d but want %d for watcher with watch ID %d", ev.Kv.ModRevision, nextRev, resp.WatchID)
nextRev++
}
if nextRev == s.rev()+1 {
if nextRev == sImpl.rev()+1 {
delete(watchers, resp.WatchID)
}
}
Expand Down Expand Up @@ -566,10 +568,13 @@ func TestWatchBatchUnsynced(t *testing.T) {
}
assert.Equal(t, tc.expectRevisionBatches, revisionBatches)

s.store.revMu.Lock()
defer s.store.revMu.Unlock()
assert.Equal(t, 1, s.synced.size())
assert.Equal(t, 0, s.unsynced.size())
sImpl, ok := s.(*watchableStore)
require.Truef(t, ok, "TestWatchBatchUnsynced: needs a WatchableKV implementation")

sImpl.store.revMu.Lock()
defer sImpl.store.revMu.Unlock()
assert.Equal(t, 1, sImpl.synced.size())
assert.Equal(t, 0, sImpl.unsynced.size())
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/storage/schema/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type authBackend struct {

var _ auth.AuthBackend = (*authBackend)(nil)

func NewAuthBackend(lg *zap.Logger, be backend.Backend) *authBackend {
func NewAuthBackend(lg *zap.Logger, be backend.Backend) auth.AuthBackend {
return &authBackend{
be: be,
lg: lg,
Expand Down
6 changes: 2 additions & 4 deletions server/storage/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"go.etcd.io/etcd/api/v3/version"
"go.etcd.io/etcd/server/v3/storage/backend"
"go.etcd.io/etcd/server/v3/storage/wal"
)

// Validate checks provided backend to confirm that schema used is supported.
Expand All @@ -47,10 +48,7 @@ func localBinaryVersion() semver.Version {
return semver.Version{Major: v.Major, Minor: v.Minor}
}

type WALVersion interface {
// MinimalEtcdVersion returns minimal etcd version able to interpret WAL log.
MinimalEtcdVersion() *semver.Version
}
type WALVersion = wal.Version

// Migrate updates storage schema to provided target version.
// Downgrading requires that provided WAL doesn't contain unsupported entries.
Expand Down
8 changes: 7 additions & 1 deletion server/storage/wal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ import (
"go.etcd.io/raft/v3/raftpb"
)

// Version defines the wal version interface.
type Version interface {
// MinimalEtcdVersion returns minimal etcd version able to interpret WAL log.
MinimalEtcdVersion() *semver.Version
}

// ReadWALVersion reads remaining entries from opened WAL and returns struct
// that implements schema.WAL interface.
func ReadWALVersion(w *WAL) (*walVersion, error) {
func ReadWALVersion(w *WAL) (Version, error) {
_, _, ents, err := w.ReadAll()
if err != nil {
return nil, err
Expand Down

0 comments on commit da82a69

Please sign in to comment.