-
Notifications
You must be signed in to change notification settings - Fork 384
/
testing.go
94 lines (78 loc) · 2.44 KB
/
testing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package bertyreplication
import (
"context"
"crypto/ed25519"
"fmt"
"testing"
"time"
sqlite "github.com/flyingtime/gorm-sqlcipher"
"github.com/ipfs/go-datastore"
ds_sync "github.com/ipfs/go-datastore/sync"
"github.com/libp2p/go-libp2p-core/peer"
libp2p_mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"gorm.io/gorm"
"moul.io/zapgorm2"
"berty.tech/berty/v2/go/internal/ipfsutil"
"berty.tech/berty/v2/go/pkg/bertyprotocol"
orbitdb "berty.tech/go-orbit-db"
)
type TestingReplicationPeer struct {
Service ReplicationService
}
func DBForTests(t testing.TB, logger *zap.Logger) (*gorm.DB, func()) {
if logger == nil {
logger = zap.NewNop()
}
db, err := gorm.Open(sqlite.Open(fmt.Sprintf("file:memdb_%d?mode=memory&cache=shared", time.Now().UnixNano())), &gorm.Config{
Logger: zapgorm2.New(logger),
DisableForeignKeyConstraintWhenMigrating: true,
})
if err != nil {
require.NoError(t, err)
}
return db, func() {
sqlDB, err := db.DB()
require.NoError(t, err)
sqlDB.Close()
}
}
func TestHelperNewReplicationService(ctx context.Context, t *testing.T, logger *zap.Logger, mn libp2p_mocknet.Mocknet, rdvp peer.AddrInfo, ds datastore.Batching, db *gorm.DB) (*replicationService, context.CancelFunc) {
t.Helper()
if ds == nil {
ds = ds_sync.MutexWrap(datastore.NewMapDatastore())
}
api, cleanup := ipfsutil.TestingCoreAPIUsingMockNet(ctx, t, &ipfsutil.TestingAPIOpts{
Logger: logger,
Mocknet: mn,
RDVPeer: rdvp,
Datastore: ds,
})
odb, err := bertyprotocol.NewBertyOrbitDB(ctx, api.API(), &bertyprotocol.NewOrbitDBOptions{
NewOrbitDBOptions: orbitdb.NewOrbitDBOptions{
Logger: logger,
Cache: bertyprotocol.NewOrbitDatastoreCache(ds),
},
})
require.NoError(t, err)
repl, err := NewReplicationService(ctx, db, odb, logger)
require.NoError(t, err)
require.NotNil(t, repl)
svc, ok := repl.(*replicationService)
require.True(t, ok)
return svc, cleanup
}
func NewReplicationMockedPeer(ctx context.Context, t *testing.T, secret []byte, sk ed25519.PublicKey, opts *bertyprotocol.TestingOpts) (*TestingReplicationPeer, func()) {
// TODO: handle auth
_ = secret
_ = sk
db, cleanup := DBForTests(t, zap.NewNop())
replServ, cleanupReplMan := TestHelperNewReplicationService(ctx, t, nil, opts.Mocknet, opts.RDVPeer, nil, db)
return &TestingReplicationPeer{
Service: replServ,
}, func() {
cleanupReplMan()
cleanup()
}
}