Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mvcc/backend tests: Refactor: Do not mix testing&prod code. #12883

Merged
merged 1 commit into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions bill-of-materials.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@
}
]
},
{
"project": "github.com/davecgh/go-spew/spew",
"licenses": [
{
"type": "ISC License",
"confidence": 0.9850746268656716
}
]
},
{
"project": "github.com/dustin/go-humanize",
"licenses": [
Expand Down Expand Up @@ -386,6 +395,15 @@
}
]
},
{
"project": "github.com/stretchr/testify/assert",
"licenses": [
{
"type": "MIT License",
"confidence": 1
}
]
},
{
"project": "github.com/tmc/grpc-websocket-proxy/wsproxy",
"licenses": [
Expand Down
28 changes: 11 additions & 17 deletions server/auth/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"encoding/base64"
"fmt"
"os"
"reflect"
"strings"
"sync"
Expand All @@ -29,6 +28,7 @@ import (
pb "go.etcd.io/etcd/api/v3/etcdserverpb"
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
"go.etcd.io/etcd/server/v3/mvcc/backend"
betesting "go.etcd.io/etcd/server/v3/mvcc/backend/testing"

"go.uber.org/zap"
"golang.org/x/crypto/bcrypt"
Expand All @@ -46,8 +46,7 @@ func dummyIndexWaiter(index uint64) <-chan struct{} {
// TestNewAuthStoreRevision ensures newly auth store
// keeps the old revision when there are no changes.
func TestNewAuthStoreRevision(t *testing.T) {
b, tPath := backend.NewDefaultTmpBackend(t)
defer os.Remove(tPath)
b, tPath := betesting.NewDefaultTmpBackend(t)

tp, err := NewTokenProvider(zap.NewExample(), tokenTypeSimple, dummyIndexWaiter, simpleTokenTTLDefault)
if err != nil {
Expand Down Expand Up @@ -76,9 +75,8 @@ func TestNewAuthStoreRevision(t *testing.T) {

// TestNewAuthStoreBryptCost ensures that NewAuthStore uses default when given bcrypt-cost is invalid
func TestNewAuthStoreBcryptCost(t *testing.T) {
b, tPath := backend.NewDefaultTmpBackend(t)
defer b.Close()
defer os.Remove(tPath)
b, _ := betesting.NewDefaultTmpBackend(t)
defer betesting.Close(t, b)

tp, err := NewTokenProvider(zap.NewExample(), tokenTypeSimple, dummyIndexWaiter, simpleTokenTTLDefault)
if err != nil {
Expand All @@ -101,7 +99,7 @@ func encodePassword(s string) string {
}

func setupAuthStore(t *testing.T) (store *authStore, teardownfunc func(t *testing.T)) {
b, tPath := backend.NewDefaultTmpBackend(t)
b, _ := betesting.NewDefaultTmpBackend(t)

tp, err := NewTokenProvider(zap.NewExample(), tokenTypeSimple, dummyIndexWaiter, simpleTokenTTLDefault)
if err != nil {
Expand All @@ -127,7 +125,6 @@ func setupAuthStore(t *testing.T) (store *authStore, teardownfunc func(t *testin

tearDown := func(_ *testing.T) {
b.Close()
os.Remove(tPath)
as.Close()
}
return as, tearDown
Expand Down Expand Up @@ -653,9 +650,8 @@ func TestIsAuthEnabled(t *testing.T) {

// TestAuthRevisionRace ensures that access to authStore.revision is thread-safe.
func TestAuthInfoFromCtxRace(t *testing.T) {
b, tPath := backend.NewDefaultTmpBackend(t)
defer b.Close()
defer os.Remove(tPath)
b, _ := betesting.NewDefaultTmpBackend(t)
defer betesting.Close(t, b)

tp, err := NewTokenProvider(zap.NewExample(), tokenTypeSimple, dummyIndexWaiter, simpleTokenTTLDefault)
if err != nil {
Expand Down Expand Up @@ -807,9 +803,8 @@ func TestHammerSimpleAuthenticate(t *testing.T) {

// TestRolesOrder tests authpb.User.Roles is sorted
func TestRolesOrder(t *testing.T) {
b, tPath := backend.NewDefaultTmpBackend(t)
defer b.Close()
defer os.Remove(tPath)
b, _ := betesting.NewDefaultTmpBackend(t)
defer betesting.Close(t, b)

tp, err := NewTokenProvider(zap.NewExample(), tokenTypeSimple, dummyIndexWaiter, simpleTokenTTLDefault)
defer tp.disable()
Expand Down Expand Up @@ -865,9 +860,8 @@ func TestAuthInfoFromCtxWithRootJWT(t *testing.T) {

// testAuthInfoFromCtxWithRoot ensures "WithRoot" properly embeds token in the context.
func testAuthInfoFromCtxWithRoot(t *testing.T, opts string) {
b, tPath := backend.NewDefaultTmpBackend(t)
defer b.Close()
defer os.Remove(tPath)
b, _ := betesting.NewDefaultTmpBackend(t)
defer betesting.Close(t, b)

tp, err := NewTokenProvider(zap.NewExample(), opts, dummyIndexWaiter, simpleTokenTTLDefault)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions server/etcdserver/cindex/cindex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@ package cindex

import (
"math/rand"
"os"
"testing"
"time"

"go.etcd.io/etcd/server/v3/mvcc/backend"
betesting "go.etcd.io/etcd/server/v3/mvcc/backend/testing"
)

// TestConsistentIndex ensures that LoadConsistentIndex/Save/ConsistentIndex and backend.BatchTx can work well together.
func TestConsistentIndex(t *testing.T) {

be, tmpPath := backend.NewTmpBackend(t, time.Microsecond, 10)
defer os.Remove(tmpPath)
be, tmpPath := betesting.NewTmpBackend(t, time.Microsecond, 10)
ci := NewConsistentIndex(be.BatchTx())

tx := be.BatchTx()
Expand Down
17 changes: 7 additions & 10 deletions server/etcdserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import (
"go.etcd.io/etcd/server/v3/mock/mockstore"
"go.etcd.io/etcd/server/v3/mock/mockwait"
"go.etcd.io/etcd/server/v3/mvcc"
"go.etcd.io/etcd/server/v3/mvcc/backend"
betesting "go.etcd.io/etcd/server/v3/mvcc/backend/testing"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
)
Expand Down Expand Up @@ -972,10 +972,7 @@ func TestSyncTrigger(t *testing.T) {

// snapshot should snapshot the store and cut the persistent
func TestSnapshot(t *testing.T) {
be, tmpPath := backend.NewDefaultTmpBackend(t)
defer func() {
os.RemoveAll(tmpPath)
}()
be, _ := betesting.NewDefaultTmpBackend(t)

s := raft.NewMemoryStorage()
s.Append([]raftpb.Entry{{Index: 1}})
Expand Down Expand Up @@ -1066,7 +1063,7 @@ func TestSnapshotOrdering(t *testing.T) {
storage: p,
raftStorage: rs,
})
be, tmpPath := backend.NewDefaultTmpBackend(t)
be, tmpPath := betesting.NewDefaultTmpBackend(t)
defer os.RemoveAll(tmpPath)
s := &EtcdServer{
lgMu: new(sync.RWMutex),
Expand Down Expand Up @@ -1128,7 +1125,7 @@ func TestSnapshotOrdering(t *testing.T) {

// Applied > SnapshotCount should trigger a SaveSnap event
func TestTriggerSnap(t *testing.T) {
be, tmpPath := backend.NewDefaultTmpBackend(t)
be, tmpPath := betesting.NewDefaultTmpBackend(t)
defer func() {
os.RemoveAll(tmpPath)
}()
Expand Down Expand Up @@ -1217,7 +1214,7 @@ func TestConcurrentApplyAndSnapshotV3(t *testing.T) {
storage: mockstorage.NewStorageRecorder(testdir),
raftStorage: rs,
})
be, tmpPath := backend.NewDefaultTmpBackend(t)
be, tmpPath := betesting.NewDefaultTmpBackend(t)
defer func() {
os.RemoveAll(tmpPath)
}()
Expand Down Expand Up @@ -1552,7 +1549,7 @@ func TestPublishV3(t *testing.T) {
w := wait.NewWithResponse(ch)
ctx, cancel := context.WithCancel(context.Background())
lg := zaptest.NewLogger(t)
be, _ := backend.NewDefaultTmpBackend(t)
be, _ := betesting.NewDefaultTmpBackend(t)
srv := &EtcdServer{
lgMu: new(sync.RWMutex),
lg: lg,
Expand Down Expand Up @@ -1622,7 +1619,7 @@ func TestPublishV3Retry(t *testing.T) {
n := newNodeRecorderStream()

lg := zaptest.NewLogger(t)
be, _ := backend.NewDefaultTmpBackend(t)
be, _ := betesting.NewDefaultTmpBackend(t)
srv := &EtcdServer{
lgMu: new(sync.RWMutex),
lg: lg,
Expand Down
26 changes: 11 additions & 15 deletions server/lease/leasehttp/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@ import (
"context"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

"go.etcd.io/etcd/server/v3/lease"
"go.etcd.io/etcd/server/v3/mvcc/backend"
"go.uber.org/zap"
betesting "go.etcd.io/etcd/server/v3/mvcc/backend/testing"
"go.uber.org/zap/zaptest"
)

func TestRenewHTTP(t *testing.T) {
lg := zap.NewNop()
be, tmpPath := backend.NewTmpBackend(t, time.Hour, 10000)
defer os.Remove(tmpPath)
defer be.Close()
lg := zaptest.NewLogger(t)
be, _ := betesting.NewTmpBackend(t, time.Hour, 10000)
defer betesting.Close(t, be)

le := lease.NewLessor(lg, be, lease.LessorConfig{MinLeaseTTL: int64(5)}, nil)
le.Promote(time.Second)
Expand All @@ -53,10 +51,9 @@ func TestRenewHTTP(t *testing.T) {
}

func TestTimeToLiveHTTP(t *testing.T) {
lg := zap.NewNop()
be, tmpPath := backend.NewTmpBackend(t, time.Hour, 10000)
defer os.Remove(tmpPath)
defer be.Close()
lg := zaptest.NewLogger(t)
be, _ := betesting.NewTmpBackend(t, time.Hour, 10000)
defer betesting.Close(t, be)

le := lease.NewLessor(lg, be, lease.LessorConfig{MinLeaseTTL: int64(5)}, nil)
le.Promote(time.Second)
Expand Down Expand Up @@ -95,10 +92,9 @@ func TestTimeToLiveHTTPTimeout(t *testing.T) {
}

func testApplyTimeout(t *testing.T, f func(*lease.Lease, string) error) {
lg := zap.NewNop()
be, tmpPath := backend.NewTmpBackend(t, time.Hour, 10000)
defer os.Remove(tmpPath)
defer be.Close()
lg := zaptest.NewLogger(t)
be, _ := betesting.NewTmpBackend(t, time.Hour, 10000)
defer betesting.Close(t, be)

le := lease.NewLessor(lg, be, lease.LessorConfig{MinLeaseTTL: int64(5)}, nil)
le.Promote(time.Second)
Expand Down
4 changes: 2 additions & 2 deletions server/lease/lessor_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"testing"
"time"

"go.etcd.io/etcd/server/v3/mvcc/backend"
betesting "go.etcd.io/etcd/server/v3/mvcc/backend/testing"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -65,7 +65,7 @@ func demote(le *lessor) {
// return new lessor and tearDown to release resource
func setUp(t testing.TB) (le *lessor, tearDown func()) {
lg := zap.NewNop()
be, _ := backend.NewDefaultTmpBackend(t)
be, _ := betesting.NewDefaultTmpBackend(t)
// MinLeaseTTL is negative, so we can grant expired lease in benchmark.
// ExpiredLeasesRetryInterval should small, so benchmark of findExpired will recheck expired lease.
le = newLessor(lg, be, LessorConfig{MinLeaseTTL: -1000, ExpiredLeasesRetryInterval: 10 * time.Microsecond}, nil)
Expand Down
17 changes: 0 additions & 17 deletions server/mvcc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"path/filepath"
"sync"
"sync/atomic"
"testing"
"time"

humanize "github.com/dustin/go-humanize"
Expand Down Expand Up @@ -544,22 +543,6 @@ func (b *backend) OpenReadTxN() int64 {
return atomic.LoadInt64(&b.openReadTxN)
}

// NewTmpBackend creates a backend implementation for testing.
func NewTmpBackend(t testing.TB, batchInterval time.Duration, batchLimit int) (*backend, string) {
dir, err := ioutil.TempDir(t.TempDir(), "etcd_backend_test")
if err != nil {
panic(err)
}
tmpPath := filepath.Join(dir, "database")
bcfg := DefaultBackendConfig()
bcfg.Path, bcfg.BatchInterval, bcfg.BatchLimit = tmpPath, batchInterval, batchLimit
return newBackend(bcfg), tmpPath
}

func NewDefaultTmpBackend(t testing.TB) (*backend, string) {
return NewTmpBackend(t, defaultBatchInterval, defaultBatchLimit)
}

type snapshot struct {
*bolt.Tx
stopc chan struct{}
Expand Down
17 changes: 10 additions & 7 deletions server/mvcc/backend/backend_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,31 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package backend
package backend_test

import (
"crypto/rand"
"os"
"testing"
"time"

"github.com/stretchr/testify/assert"
betesting "go.etcd.io/etcd/server/v3/mvcc/backend/testing"
)

func BenchmarkBackendPut(b *testing.B) {
backend, tmppath := NewTmpBackend(b, 100*time.Millisecond, 10000)
defer backend.Close()
defer os.Remove(tmppath)
backend, _ := betesting.NewTmpBackend(b, 100*time.Millisecond, 10000)
defer betesting.Close(b, backend)

// prepare keys
keys := make([][]byte, b.N)
for i := 0; i < b.N; i++ {
keys[i] = make([]byte, 64)
rand.Read(keys[i])
_, err := rand.Read(keys[i])
assert.NoError(b, err)
}
value := make([]byte, 128)
rand.Read(value)
_, err := rand.Read(value)
assert.NoError(b, err)

batchTx := backend.BatchTx()

Expand Down
Loading