storage: use temp path to handle test file
parent
2d01eb4e11
commit
0d38c13990
|
@ -1,7 +1,10 @@
|
||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -56,14 +59,24 @@ var (
|
||||||
}
|
}
|
||||||
return n, rev
|
return n, rev
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tmpPath string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
tmpDir, err := ioutil.TempDir(os.TempDir(), "etcd_test_storage")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
tmpPath = path.Join(tmpDir, "database")
|
||||||
|
}
|
||||||
|
|
||||||
func TestKVRange(t *testing.T) { testKVRange(t, normalRangeFunc) }
|
func TestKVRange(t *testing.T) { testKVRange(t, normalRangeFunc) }
|
||||||
func TestKVTxnRange(t *testing.T) { testKVRange(t, txnRangeFunc) }
|
func TestKVTxnRange(t *testing.T) { testKVRange(t, txnRangeFunc) }
|
||||||
|
|
||||||
func testKVRange(t *testing.T, f rangeFunc) {
|
func testKVRange(t *testing.T, f rangeFunc) {
|
||||||
s := New("test")
|
s := New(tmpPath)
|
||||||
defer cleanup(s, "test")
|
defer cleanup(s, tmpPath)
|
||||||
|
|
||||||
s.Put([]byte("foo"), []byte("bar"))
|
s.Put([]byte("foo"), []byte("bar"))
|
||||||
s.Put([]byte("foo1"), []byte("bar1"))
|
s.Put([]byte("foo1"), []byte("bar1"))
|
||||||
|
@ -129,8 +142,8 @@ func TestKVRangeRev(t *testing.T) { testKVRangeRev(t, normalRangeFunc) }
|
||||||
func TestKVTxnRangeRev(t *testing.T) { testKVRangeRev(t, normalRangeFunc) }
|
func TestKVTxnRangeRev(t *testing.T) { testKVRangeRev(t, normalRangeFunc) }
|
||||||
|
|
||||||
func testKVRangeRev(t *testing.T, f rangeFunc) {
|
func testKVRangeRev(t *testing.T, f rangeFunc) {
|
||||||
s := New("test")
|
s := New(tmpPath)
|
||||||
defer cleanup(s, "test")
|
defer cleanup(s, tmpPath)
|
||||||
|
|
||||||
s.Put([]byte("foo"), []byte("bar"))
|
s.Put([]byte("foo"), []byte("bar"))
|
||||||
s.Put([]byte("foo1"), []byte("bar1"))
|
s.Put([]byte("foo1"), []byte("bar1"))
|
||||||
|
@ -171,8 +184,8 @@ func TestKVRangeBadRev(t *testing.T) { testKVRangeBadRev(t, normalRangeFunc)
|
||||||
func TestKVTxnRangeBadRev(t *testing.T) { testKVRangeBadRev(t, normalRangeFunc) }
|
func TestKVTxnRangeBadRev(t *testing.T) { testKVRangeBadRev(t, normalRangeFunc) }
|
||||||
|
|
||||||
func testKVRangeBadRev(t *testing.T, f rangeFunc) {
|
func testKVRangeBadRev(t *testing.T, f rangeFunc) {
|
||||||
s := New("test")
|
s := New(tmpPath)
|
||||||
defer cleanup(s, "test")
|
defer cleanup(s, tmpPath)
|
||||||
|
|
||||||
s.Put([]byte("foo"), []byte("bar"))
|
s.Put([]byte("foo"), []byte("bar"))
|
||||||
s.Put([]byte("foo1"), []byte("bar1"))
|
s.Put([]byte("foo1"), []byte("bar1"))
|
||||||
|
@ -203,8 +216,8 @@ func TestKVRangeLimit(t *testing.T) { testKVRangeLimit(t, normalRangeFunc) }
|
||||||
func TestKVTxnRangeLimit(t *testing.T) { testKVRangeLimit(t, txnRangeFunc) }
|
func TestKVTxnRangeLimit(t *testing.T) { testKVRangeLimit(t, txnRangeFunc) }
|
||||||
|
|
||||||
func testKVRangeLimit(t *testing.T, f rangeFunc) {
|
func testKVRangeLimit(t *testing.T, f rangeFunc) {
|
||||||
s := New("test")
|
s := New(tmpPath)
|
||||||
defer cleanup(s, "test")
|
defer cleanup(s, tmpPath)
|
||||||
|
|
||||||
s.Put([]byte("foo"), []byte("bar"))
|
s.Put([]byte("foo"), []byte("bar"))
|
||||||
s.Put([]byte("foo1"), []byte("bar1"))
|
s.Put([]byte("foo1"), []byte("bar1"))
|
||||||
|
@ -247,8 +260,8 @@ func TestKVPutMultipleTimes(t *testing.T) { testKVPutMultipleTimes(t, normalP
|
||||||
func TestKVTxnPutMultipleTimes(t *testing.T) { testKVPutMultipleTimes(t, txnPutFunc) }
|
func TestKVTxnPutMultipleTimes(t *testing.T) { testKVPutMultipleTimes(t, txnPutFunc) }
|
||||||
|
|
||||||
func testKVPutMultipleTimes(t *testing.T, f putFunc) {
|
func testKVPutMultipleTimes(t *testing.T, f putFunc) {
|
||||||
s := New("test")
|
s := New(tmpPath)
|
||||||
defer cleanup(s, "test")
|
defer cleanup(s, tmpPath)
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
base := int64(i + 1)
|
base := int64(i + 1)
|
||||||
|
@ -308,7 +321,7 @@ func testKVDeleteRange(t *testing.T, f deleteRangeFunc) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
s := New("test")
|
s := New(tmpPath)
|
||||||
|
|
||||||
s.Put([]byte("foo"), []byte("bar"))
|
s.Put([]byte("foo"), []byte("bar"))
|
||||||
s.Put([]byte("foo1"), []byte("bar1"))
|
s.Put([]byte("foo1"), []byte("bar1"))
|
||||||
|
@ -319,7 +332,7 @@ func testKVDeleteRange(t *testing.T, f deleteRangeFunc) {
|
||||||
t.Errorf("#%d: n = %d, rev = %d, want (%d, %d)", i, n, rev, tt.wN, tt.wrev)
|
t.Errorf("#%d: n = %d, rev = %d, want (%d, %d)", i, n, rev, tt.wN, tt.wrev)
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup(s, "test")
|
cleanup(s, tmpPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -327,8 +340,8 @@ func TestKVDeleteMultipleTimes(t *testing.T) { testKVDeleteMultipleTimes(t, n
|
||||||
func TestKVTxnDeleteMultipleTimes(t *testing.T) { testKVDeleteMultipleTimes(t, txnDeleteRangeFunc) }
|
func TestKVTxnDeleteMultipleTimes(t *testing.T) { testKVDeleteMultipleTimes(t, txnDeleteRangeFunc) }
|
||||||
|
|
||||||
func testKVDeleteMultipleTimes(t *testing.T, f deleteRangeFunc) {
|
func testKVDeleteMultipleTimes(t *testing.T, f deleteRangeFunc) {
|
||||||
s := New("test")
|
s := New(tmpPath)
|
||||||
defer cleanup(s, "test")
|
defer cleanup(s, tmpPath)
|
||||||
|
|
||||||
s.Put([]byte("foo"), []byte("bar"))
|
s.Put([]byte("foo"), []byte("bar"))
|
||||||
|
|
||||||
|
@ -347,8 +360,8 @@ func testKVDeleteMultipleTimes(t *testing.T, f deleteRangeFunc) {
|
||||||
|
|
||||||
// test that range, put, delete on single key in sequence repeatedly works correctly.
|
// test that range, put, delete on single key in sequence repeatedly works correctly.
|
||||||
func TestKVOperationInSequence(t *testing.T) {
|
func TestKVOperationInSequence(t *testing.T) {
|
||||||
s := New("test")
|
s := New(tmpPath)
|
||||||
defer cleanup(s, "test")
|
defer cleanup(s, tmpPath)
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
base := int64(i * 2)
|
base := int64(i * 2)
|
||||||
|
@ -393,8 +406,8 @@ func TestKVOperationInSequence(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestKVTxnBlockNonTnxOperations(t *testing.T) {
|
func TestKVTxnBlockNonTnxOperations(t *testing.T) {
|
||||||
s := New("test")
|
s := New(tmpPath)
|
||||||
defer cleanup(s, "test")
|
defer cleanup(s, tmpPath)
|
||||||
|
|
||||||
tests := []func(){
|
tests := []func(){
|
||||||
func() { s.Range([]byte("foo"), nil, 0, 0) },
|
func() { s.Range([]byte("foo"), nil, 0, 0) },
|
||||||
|
@ -424,8 +437,8 @@ func TestKVTxnBlockNonTnxOperations(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestKVTxnWrongID(t *testing.T) {
|
func TestKVTxnWrongID(t *testing.T) {
|
||||||
s := New("test")
|
s := New(tmpPath)
|
||||||
defer cleanup(s, "test")
|
defer cleanup(s, tmpPath)
|
||||||
|
|
||||||
id := s.TxnBegin()
|
id := s.TxnBegin()
|
||||||
wrongid := id + 1
|
wrongid := id + 1
|
||||||
|
@ -460,8 +473,8 @@ func TestKVTxnWrongID(t *testing.T) {
|
||||||
|
|
||||||
// test that txn range, put, delete on single key in sequence repeatedly works correctly.
|
// test that txn range, put, delete on single key in sequence repeatedly works correctly.
|
||||||
func TestKVTnxOperationInSequence(t *testing.T) {
|
func TestKVTnxOperationInSequence(t *testing.T) {
|
||||||
s := New("test")
|
s := New(tmpPath)
|
||||||
defer cleanup(s, "test")
|
defer cleanup(s, tmpPath)
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
id := s.TxnBegin()
|
id := s.TxnBegin()
|
||||||
|
@ -515,8 +528,8 @@ func TestKVTnxOperationInSequence(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestKVCompactReserveLastValue(t *testing.T) {
|
func TestKVCompactReserveLastValue(t *testing.T) {
|
||||||
s := New("test")
|
s := New(tmpPath)
|
||||||
defer cleanup(s, "test")
|
defer cleanup(s, tmpPath)
|
||||||
|
|
||||||
s.Put([]byte("foo"), []byte("bar0"))
|
s.Put([]byte("foo"), []byte("bar0"))
|
||||||
s.Put([]byte("foo"), []byte("bar1"))
|
s.Put([]byte("foo"), []byte("bar1"))
|
||||||
|
@ -568,8 +581,8 @@ func TestKVCompactReserveLastValue(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestKVCompactBad(t *testing.T) {
|
func TestKVCompactBad(t *testing.T) {
|
||||||
s := New("test")
|
s := New(tmpPath)
|
||||||
defer cleanup(s, "test")
|
defer cleanup(s, tmpPath)
|
||||||
|
|
||||||
s.Put([]byte("foo"), []byte("bar0"))
|
s.Put([]byte("foo"), []byte("bar0"))
|
||||||
s.Put([]byte("foo"), []byte("bar1"))
|
s.Put([]byte("foo"), []byte("bar1"))
|
||||||
|
@ -614,7 +627,7 @@ func TestKVRestore(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
s := New("test")
|
s := New(tmpPath)
|
||||||
tt(s)
|
tt(s)
|
||||||
var kvss [][]storagepb.KeyValue
|
var kvss [][]storagepb.KeyValue
|
||||||
for k := int64(0); k < 10; k++ {
|
for k := int64(0); k < 10; k++ {
|
||||||
|
@ -623,7 +636,7 @@ func TestKVRestore(t *testing.T) {
|
||||||
}
|
}
|
||||||
s.Close()
|
s.Close()
|
||||||
|
|
||||||
ns := New("test")
|
ns := New(tmpPath)
|
||||||
ns.Restore()
|
ns.Restore()
|
||||||
// wait for possible compaction to finish
|
// wait for possible compaction to finish
|
||||||
testutil.WaitSchedule()
|
testutil.WaitSchedule()
|
||||||
|
@ -632,7 +645,7 @@ func TestKVRestore(t *testing.T) {
|
||||||
nkvs, _, _ := ns.Range([]byte("a"), []byte("z"), 0, k)
|
nkvs, _, _ := ns.Range([]byte("a"), []byte("z"), 0, k)
|
||||||
nkvss = append(nkvss, nkvs)
|
nkvss = append(nkvss, nkvs)
|
||||||
}
|
}
|
||||||
cleanup(ns, "test")
|
cleanup(ns, tmpPath)
|
||||||
|
|
||||||
if !reflect.DeepEqual(nkvss, kvss) {
|
if !reflect.DeepEqual(nkvss, kvss) {
|
||||||
t.Errorf("#%d: kvs history = %+v, want %+v", i, nkvss, kvss)
|
t.Errorf("#%d: kvs history = %+v, want %+v", i, nkvss, kvss)
|
||||||
|
@ -641,8 +654,8 @@ func TestKVRestore(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestKVSnapshot(t *testing.T) {
|
func TestKVSnapshot(t *testing.T) {
|
||||||
s := New("test")
|
s := New(tmpPath)
|
||||||
defer cleanup(s, "test")
|
defer cleanup(s, tmpPath)
|
||||||
|
|
||||||
s.Put([]byte("foo"), []byte("bar"))
|
s.Put([]byte("foo"), []byte("bar"))
|
||||||
s.Put([]byte("foo1"), []byte("bar1"))
|
s.Put([]byte("foo1"), []byte("bar1"))
|
||||||
|
|
|
@ -44,7 +44,7 @@ func TestScheduleCompaction(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
s := newStore("test")
|
s := newStore(tmpPath)
|
||||||
tx := s.b.BatchTx()
|
tx := s.b.BatchTx()
|
||||||
|
|
||||||
tx.Lock()
|
tx.Lock()
|
||||||
|
@ -74,6 +74,6 @@ func TestScheduleCompaction(t *testing.T) {
|
||||||
}
|
}
|
||||||
tx.Unlock()
|
tx.Unlock()
|
||||||
|
|
||||||
cleanup(s, "test")
|
cleanup(s, tmpPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,8 +12,8 @@ import (
|
||||||
|
|
||||||
// TODO: improve to a unit test
|
// TODO: improve to a unit test
|
||||||
func TestRangeLimitWhenKeyDeleted(t *testing.T) {
|
func TestRangeLimitWhenKeyDeleted(t *testing.T) {
|
||||||
s := newStore("test")
|
s := newStore(tmpPath)
|
||||||
defer os.Remove("test")
|
defer os.Remove(tmpPath)
|
||||||
|
|
||||||
s.Put([]byte("foo"), []byte("bar"))
|
s.Put([]byte("foo"), []byte("bar"))
|
||||||
s.Put([]byte("foo1"), []byte("bar1"))
|
s.Put([]byte("foo1"), []byte("bar1"))
|
||||||
|
@ -46,8 +46,8 @@ func TestRangeLimitWhenKeyDeleted(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRestoreContinueUnfinishedCompaction(t *testing.T) {
|
func TestRestoreContinueUnfinishedCompaction(t *testing.T) {
|
||||||
s0 := newStore("test")
|
s0 := newStore(tmpPath)
|
||||||
defer os.Remove("test")
|
defer os.Remove(tmpPath)
|
||||||
|
|
||||||
s0.Put([]byte("foo"), []byte("bar"))
|
s0.Put([]byte("foo"), []byte("bar"))
|
||||||
s0.Put([]byte("foo"), []byte("bar1"))
|
s0.Put([]byte("foo"), []byte("bar1"))
|
||||||
|
@ -63,7 +63,7 @@ func TestRestoreContinueUnfinishedCompaction(t *testing.T) {
|
||||||
|
|
||||||
s0.Close()
|
s0.Close()
|
||||||
|
|
||||||
s1 := newStore("test")
|
s1 := newStore(tmpPath)
|
||||||
s1.Restore()
|
s1.Restore()
|
||||||
|
|
||||||
// wait for scheduled compaction to be finished
|
// wait for scheduled compaction to be finished
|
||||||
|
@ -86,8 +86,8 @@ func TestRestoreContinueUnfinishedCompaction(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkStorePut(b *testing.B) {
|
func BenchmarkStorePut(b *testing.B) {
|
||||||
s := newStore("test")
|
s := newStore(tmpPath)
|
||||||
defer os.Remove("test")
|
defer os.Remove(tmpPath)
|
||||||
|
|
||||||
// prepare keys
|
// prepare keys
|
||||||
keys := make([][]byte, b.N)
|
keys := make([][]byte, b.N)
|
||||||
|
|
Loading…
Reference in New Issue