2018-12-04 16:26:30 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-12-06 12:32:21 +00:00
|
|
|
package irreparabledb_test
|
2018-12-04 16:26:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-12-06 12:32:21 +00:00
|
|
|
|
2018-12-04 16:26:30 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2018-12-06 12:32:21 +00:00
|
|
|
"storj.io/storj/pkg/datarepair/irreparabledb"
|
2018-12-04 16:26:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-12-06 12:32:21 +00:00
|
|
|
// postgres connstring that works with docker-compose
|
2018-12-04 16:26:30 +00:00
|
|
|
defaultPostgresConn = "postgres://storj:storj-pass@test-postgres/teststorj?sslmode=disable"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testPostgres = flag.String("postgres-test-db", os.Getenv("STORJ_POSTGRES_TEST"), "PostgreSQL test database connection string")
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPostgres(t *testing.T) {
|
|
|
|
if *testPostgres == "" {
|
|
|
|
t.Skipf("postgres flag missing, example:\n-postgres-test-db=%s", defaultPostgresConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
// creating in-memory db and opening connection
|
2018-12-06 12:32:21 +00:00
|
|
|
irrdb, err := irreparabledb.New(*testPostgres)
|
2018-12-04 16:26:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-12-06 12:32:21 +00:00
|
|
|
defer ctx.Check(irrdb.Close)
|
2018-12-04 16:26:30 +00:00
|
|
|
|
|
|
|
testDatabase(ctx, t, irrdb)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSqlite3(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
// creating in-memory db and opening connection
|
2018-12-06 12:32:21 +00:00
|
|
|
irrdb, err := irreparabledb.New("sqlite3://file::memory:?mode=memory&cache=shared")
|
2018-12-04 16:26:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-12-06 12:32:21 +00:00
|
|
|
defer ctx.Check(irrdb.Close)
|
2018-12-04 16:26:30 +00:00
|
|
|
|
|
|
|
testDatabase(ctx, t, irrdb)
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:32:21 +00:00
|
|
|
func testDatabase(ctx context.Context, t *testing.T, irrdb *irreparabledb.Database) {
|
2018-12-04 16:26:30 +00:00
|
|
|
//testing variables
|
2018-12-06 12:32:21 +00:00
|
|
|
segmentInfo := &irreparabledb.RemoteSegmentInfo{
|
2018-12-04 16:26:30 +00:00
|
|
|
EncryptedSegmentPath: []byte("IamSegmentkeyinfo"),
|
|
|
|
EncryptedSegmentDetail: []byte("IamSegmentdetailinfo"),
|
|
|
|
LostPiecesCount: int64(10),
|
|
|
|
RepairUnixSec: time.Now().Unix(),
|
|
|
|
RepairAttemptCount: int64(10),
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // New entry
|
|
|
|
err := irrdb.IncrementRepairAttempts(ctx, segmentInfo)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ //Increment the already existing entry
|
|
|
|
err := irrdb.IncrementRepairAttempts(ctx, segmentInfo)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
segmentInfo.RepairAttemptCount++
|
|
|
|
|
|
|
|
dbxInfo, err := irrdb.Get(ctx, segmentInfo.EncryptedSegmentPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, segmentInfo, dbxInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ //Delete existing entry
|
|
|
|
err := irrdb.Delete(ctx, segmentInfo.EncryptedSegmentPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = irrdb.Get(ctx, segmentInfo.EncryptedSegmentPath)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
}
|