server: Use zaptest in bucket tests and move backendMock to separate file

This commit is contained in:
Marek Siarkowicz 2021-07-14 13:05:43 +02:00
parent a0554a6bd3
commit bc16461995
5 changed files with 142 additions and 125 deletions

View File

@ -0,0 +1,127 @@
// Copyright 2021 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package auth
import "go.etcd.io/etcd/api/v3/authpb"
type backendMock struct {
users map[string]*authpb.User
roles map[string]*authpb.Role
enabled bool
revision uint64
}
func newBackendMock() *backendMock {
return &backendMock{
users: make(map[string]*authpb.User),
roles: make(map[string]*authpb.Role),
}
}
func (b *backendMock) CreateAuthBuckets() {
}
func (b *backendMock) ForceCommit() {
}
func (b *backendMock) BatchTx() AuthBatchTx {
return &txMock{be: b}
}
func (b *backendMock) GetUser(s string) *authpb.User {
return b.users[s]
}
func (b *backendMock) GetAllUsers() []*authpb.User {
return b.BatchTx().UnsafeGetAllUsers()
}
func (b *backendMock) GetRole(s string) *authpb.Role {
return b.roles[s]
}
func (b *backendMock) GetAllRoles() []*authpb.Role {
return b.BatchTx().UnsafeGetAllRoles()
}
var _ AuthBackend = (*backendMock)(nil)
type txMock struct {
be *backendMock
}
var _ AuthBatchTx = (*txMock)(nil)
func (t txMock) UnsafeReadAuthEnabled() bool {
return t.be.enabled
}
func (t txMock) UnsafeReadAuthRevision() uint64 {
return t.be.revision
}
func (t txMock) UnsafeGetUser(s string) *authpb.User {
return t.be.users[s]
}
func (t txMock) UnsafeGetRole(s string) *authpb.Role {
return t.be.roles[s]
}
func (t txMock) UnsafeGetAllUsers() []*authpb.User {
users := []*authpb.User{}
for _, u := range t.be.users {
users = append(users, u)
}
return users
}
func (t txMock) UnsafeGetAllRoles() []*authpb.Role {
roles := []*authpb.Role{}
for _, r := range t.be.roles {
roles = append(roles, r)
}
return roles
}
func (t txMock) Lock() {
}
func (t txMock) Unlock() {
}
func (t txMock) UnsafeSaveAuthEnabled(enabled bool) {
t.be.enabled = enabled
}
func (t txMock) UnsafeSaveAuthRevision(rev uint64) {
t.be.revision = rev
}
func (t txMock) UnsafePutUser(user *authpb.User) {
t.be.users[string(user.Name)] = user
}
func (t txMock) UnsafeDeleteUser(s string) {
delete(t.be.users, s)
}
func (t txMock) UnsafePutRole(role *authpb.Role) {
t.be.roles[string(role.Name)] = role
}
func (t txMock) UnsafeDeleteRole(s string) {
delete(t.be.roles, s)
}

View File

@ -969,113 +969,3 @@ func TestUserChangePasswordWithOldLog(t *testing.T) {
t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
}
}
type backendMock struct {
users map[string]*authpb.User
roles map[string]*authpb.Role
enabled bool
revision uint64
}
func newBackendMock() *backendMock {
return &backendMock{
users: make(map[string]*authpb.User),
roles: make(map[string]*authpb.Role),
}
}
func (b *backendMock) CreateAuthBuckets() {
}
func (b *backendMock) ForceCommit() {
}
func (b *backendMock) BatchTx() AuthBatchTx {
return &txMock{be: b}
}
func (b *backendMock) GetUser(s string) *authpb.User {
return b.users[s]
}
func (b *backendMock) GetAllUsers() []*authpb.User {
return b.BatchTx().UnsafeGetAllUsers()
}
func (b *backendMock) GetRole(s string) *authpb.Role {
return b.roles[s]
}
func (b *backendMock) GetAllRoles() []*authpb.Role {
return b.BatchTx().UnsafeGetAllRoles()
}
var _ AuthBackend = (*backendMock)(nil)
type txMock struct {
be *backendMock
}
var _ AuthBatchTx = (*txMock)(nil)
func (t txMock) UnsafeReadAuthEnabled() bool {
return t.be.enabled
}
func (t txMock) UnsafeReadAuthRevision() uint64 {
return t.be.revision
}
func (t txMock) UnsafeGetUser(s string) *authpb.User {
return t.be.users[s]
}
func (t txMock) UnsafeGetRole(s string) *authpb.Role {
return t.be.roles[s]
}
func (t txMock) UnsafeGetAllUsers() []*authpb.User {
users := []*authpb.User{}
for _, u := range t.be.users {
users = append(users, u)
}
return users
}
func (t txMock) UnsafeGetAllRoles() []*authpb.Role {
roles := []*authpb.Role{}
for _, r := range t.be.roles {
roles = append(roles, r)
}
return roles
}
func (t txMock) Lock() {
}
func (t txMock) Unlock() {
}
func (t txMock) UnsafeSaveAuthEnabled(enabled bool) {
t.be.enabled = enabled
}
func (t txMock) UnsafeSaveAuthRevision(rev uint64) {
t.be.revision = rev
}
func (t txMock) UnsafePutUser(user *authpb.User) {
t.be.users[string(user.Name)] = user
}
func (t txMock) UnsafeDeleteUser(s string) {
delete(t.be.users, s)
}
func (t txMock) UnsafePutRole(role *authpb.Role) {
t.be.roles[string(role.Name)] = role
}
func (t txMock) UnsafeDeleteRole(s string) {
delete(t.be.roles, s)
}

View File

@ -19,7 +19,7 @@ import (
"time"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
"go.etcd.io/etcd/api/v3/authpb"
"go.etcd.io/etcd/server/v3/auth"
@ -110,7 +110,7 @@ func TestGetAllRoles(t *testing.T) {
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
be, tmpPath := betesting.NewTmpBackend(t, time.Microsecond, 10)
abe := NewAuthBackend(zap.NewNop(), be)
abe := NewAuthBackend(zaptest.NewLogger(t), be)
abe.CreateAuthBuckets()
tx := abe.BatchTx()
@ -123,7 +123,7 @@ func TestGetAllRoles(t *testing.T) {
be2 := backend.NewDefaultBackend(tmpPath)
defer be2.Close()
abe2 := NewAuthBackend(zap.NewNop(), be2)
abe2 := NewAuthBackend(zaptest.NewLogger(t), be2)
users := abe2.GetAllRoles()
assert.Equal(t, tc.want, users)
@ -206,7 +206,7 @@ func TestGetRole(t *testing.T) {
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
be, tmpPath := betesting.NewTmpBackend(t, time.Microsecond, 10)
abe := NewAuthBackend(zap.NewNop(), be)
abe := NewAuthBackend(zaptest.NewLogger(t), be)
abe.CreateAuthBuckets()
tx := abe.BatchTx()
@ -219,7 +219,7 @@ func TestGetRole(t *testing.T) {
be2 := backend.NewDefaultBackend(tmpPath)
defer be2.Close()
abe2 := NewAuthBackend(zap.NewNop(), be2)
abe2 := NewAuthBackend(zaptest.NewLogger(t), be2)
users := abe2.GetRole("role1")
assert.Equal(t, tc.want, users)

View File

@ -20,7 +20,7 @@ import (
"time"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
"go.etcd.io/etcd/server/v3/storage/backend"
betesting "go.etcd.io/etcd/server/v3/storage/backend/testing"
@ -53,7 +53,7 @@ func TestAuthEnabled(t *testing.T) {
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
be, tmpPath := betesting.NewTmpBackend(t, time.Microsecond, 10)
abe := NewAuthBackend(zap.NewNop(), be)
abe := NewAuthBackend(zaptest.NewLogger(t), be)
tx := abe.BatchTx()
abe.CreateAuthBuckets()
@ -67,7 +67,7 @@ func TestAuthEnabled(t *testing.T) {
be2 := backend.NewDefaultBackend(tmpPath)
defer be2.Close()
abe2 := NewAuthBackend(zap.NewNop(), be2)
abe2 := NewAuthBackend(zaptest.NewLogger(t), be2)
tx = abe2.BatchTx()
tx.Lock()
defer tx.Unlock()
@ -103,7 +103,7 @@ func TestAuthRevision(t *testing.T) {
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
be, tmpPath := betesting.NewTmpBackend(t, time.Microsecond, 10)
abe := NewAuthBackend(zap.NewNop(), be)
abe := NewAuthBackend(zaptest.NewLogger(t), be)
abe.CreateAuthBuckets()
if tc.setRevision != 0 {
@ -117,7 +117,7 @@ func TestAuthRevision(t *testing.T) {
be2 := backend.NewDefaultBackend(tmpPath)
defer be2.Close()
abe2 := NewAuthBackend(zap.NewNop(), be2)
abe2 := NewAuthBackend(zaptest.NewLogger(t), be2)
tx := abe2.BatchTx()
tx.Lock()
defer tx.Unlock()

View File

@ -19,7 +19,7 @@ import (
"time"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
"go.etcd.io/etcd/api/v3/authpb"
"go.etcd.io/etcd/server/v3/auth"
@ -98,7 +98,7 @@ func TestGetAllUsers(t *testing.T) {
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
be, tmpPath := betesting.NewTmpBackend(t, time.Microsecond, 10)
abe := NewAuthBackend(zap.NewNop(), be)
abe := NewAuthBackend(zaptest.NewLogger(t), be)
abe.CreateAuthBuckets()
tx := abe.BatchTx()
@ -111,7 +111,7 @@ func TestGetAllUsers(t *testing.T) {
be2 := backend.NewDefaultBackend(tmpPath)
defer be2.Close()
abe2 := NewAuthBackend(zap.NewNop(), be2)
abe2 := NewAuthBackend(zaptest.NewLogger(t), be2)
users := abe2.GetAllUsers()
assert.Equal(t, tc.want, users)
@ -182,7 +182,7 @@ func TestGetUser(t *testing.T) {
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
be, tmpPath := betesting.NewTmpBackend(t, time.Microsecond, 10)
abe := NewAuthBackend(zap.NewNop(), be)
abe := NewAuthBackend(zaptest.NewLogger(t), be)
abe.CreateAuthBuckets()
tx := abe.BatchTx()
@ -195,7 +195,7 @@ func TestGetUser(t *testing.T) {
be2 := backend.NewDefaultBackend(tmpPath)
defer be2.Close()
abe2 := NewAuthBackend(zap.NewNop(), be2)
abe2 := NewAuthBackend(zaptest.NewLogger(t), be2)
users := abe2.GetUser("alice")
assert.Equal(t, tc.want, users)