Skip to content

Commit 5040355

Browse files
authored
Merge pull request #5 from dan13ram/dev
Added e2e tests
2 parents 7fe79f6 + 9719068 commit 5040355

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3515
-169
lines changed

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ go.work
2222
*.sw[a-z]
2323

2424
# config
25-
*.yml
26-
!config.sample.yml
27-
!docker-compose.yml
28-
!defaults.yml
25+
config.local.yml
2926

3027
# env
3128
.env*

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,27 @@ docker run -d --env-file .env docker.io/dan13ram/wpokt-validator:latest
102102
103103
Ensure you have set the required environment variables in the `.env` file or directly in the command above.
104104
105+
## Running End-to-End Tests
106+
107+
To test the core functionalities of the wPOKT Validator in a controlled environment, follow these steps:
108+
109+
1. **Navigate to E2E Directory:**
110+
- Open your terminal and move to the `./e2e` directory in the project.
111+
112+
2. **Start Local Network:**
113+
- Within the `./e2e` directory, execute the following commands to set up the local network:
114+
```shell
115+
make clean
116+
make network
117+
```
118+
- Please allow up to 5 minutes for the local network to be fully operational.
119+
120+
3. **Run Tests:**
121+
- Once the local network is ready, initiate the end-to-end tests using the command:
122+
```shell
123+
make test
124+
```
125+
105126
## License
106127
107128
This project is licensed under the MIT License.

app/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestReadConfigFromConfigFile(t *testing.T) {
2424

2525
assert.Equal(t, read, true)
2626
assert.Equal(t, Config.MongoDB.Database, "mongodb-database")
27-
assert.Equal(t, Config.MongoDB.TimeoutMillis, 2000)
27+
assert.Equal(t, Config.MongoDB.TimeoutMillis, int64(2000))
2828
})
2929

3030
t.Run("No Config File Provided", func(t *testing.T) {

app/database.go

Lines changed: 28 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package app
33
import (
44
"context"
55
"crypto/rand"
6-
"errors"
76
"time"
87

98
"github.com/dan13ram/wpokt-validator/models"
@@ -28,18 +27,17 @@ type Database interface {
2827
UpdateOne(collection string, filter interface{}, update interface{}) error
2928
UpsertOne(collection string, filter interface{}, update interface{}) error
3029

31-
XLock(collection string, resourceId string) (string, error)
32-
SLock(collection string, resourceId string) (string, error)
33-
Unlock(collection string, lockId string) error
30+
XLock(resourceId string) (string, error)
31+
SLock(resourceId string) (string, error)
32+
Unlock(lockId string) error
3433
}
3534

3635
// mongoDatabase is a wrapper around the mongo database
3736
type mongoDatabase struct {
3837
db *mongo.Database
39-
timeout time.Duration
4038
uri string
4139
database string
42-
lockers map[string]*lock.Client
40+
locker *lock.Client
4341
}
4442

4543
var (
@@ -49,9 +47,9 @@ var (
4947
// Connect connects to the database
5048
func (d *mongoDatabase) Connect() error {
5149
log.Debug("[DB] Connecting to database")
52-
wcMajority := writeconcern.New(writeconcern.WMajority(), writeconcern.WTimeout(d.timeout))
50+
wcMajority := writeconcern.New(writeconcern.WMajority(), writeconcern.WTimeout(time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond))
5351

54-
ctx, cancel := context.WithTimeout(context.Background(), d.timeout)
52+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond)
5553
defer cancel()
5654

5755
client, err := mongo.Connect(ctx, options.Client().ApplyURI(d.uri).SetWriteConcern(wcMajority))
@@ -67,36 +65,14 @@ func (d *mongoDatabase) Connect() error {
6765
// SetupLocker sets up the locker
6866
func (d *mongoDatabase) SetupLockers() error {
6967
log.Debug("[DB] Setting up locker")
70-
d.lockers = make(map[string]*lock.Client)
7168
var locker *lock.Client
7269

73-
ctx, cancel := context.WithTimeout(context.Background(), d.timeout)
70+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond)
7471
defer cancel()
7572

76-
locker = lock.NewClient(d.db.Collection(models.CollectionMints))
73+
locker = lock.NewClient(d.db.Collection("locks"))
7774
locker.CreateIndexes(ctx)
78-
d.lockers[models.CollectionMints] = locker
79-
80-
ctx, cancel = context.WithTimeout(context.Background(), d.timeout)
81-
defer cancel()
82-
83-
locker = lock.NewClient(d.db.Collection(models.CollectionInvalidMints))
84-
locker.CreateIndexes(ctx)
85-
d.lockers[models.CollectionInvalidMints] = locker
86-
87-
ctx, cancel = context.WithTimeout(context.Background(), d.timeout)
88-
defer cancel()
89-
90-
locker = lock.NewClient(d.db.Collection(models.CollectionBurns))
91-
locker.CreateIndexes(ctx)
92-
d.lockers[models.CollectionBurns] = locker
93-
94-
ctx, cancel = context.WithTimeout(context.Background(), d.timeout)
95-
defer cancel()
96-
97-
locker = lock.NewClient(d.db.Collection(models.CollectionHealthChecks))
98-
locker.CreateIndexes(ctx)
99-
d.lockers[models.CollectionHealthChecks] = locker
75+
d.locker = locker
10076

10177
log.Info("[DB] Locker setup")
10278
return nil
@@ -113,46 +89,31 @@ func randomString(n int) string {
11389
}
11490

11591
// XLock locks a resource for exclusive access
116-
func (d *mongoDatabase) XLock(collection string, resourceId string) (string, error) {
117-
locker := d.lockers[collection]
118-
if locker == nil {
119-
return "", errors.New("Locker not found")
120-
}
121-
122-
ctx, cancel := context.WithTimeout(context.Background(), d.timeout)
92+
func (d *mongoDatabase) XLock(resourceId string) (string, error) {
93+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond)
12394
defer cancel()
12495

12596
lockId := randomString(32)
126-
err := locker.XLock(ctx, resourceId, lockId, lock.LockDetails{})
97+
err := d.locker.XLock(ctx, resourceId, lockId, lock.LockDetails{})
12798
return lockId, err
12899
}
129100

130101
// SLock locks a resource for shared access
131-
func (d *mongoDatabase) SLock(collection string, resourceId string) (string, error) {
132-
locker := d.lockers[collection]
133-
if locker == nil {
134-
return "", errors.New("Locker not found")
135-
}
136-
137-
ctx, cancel := context.WithTimeout(context.Background(), d.timeout)
102+
func (d *mongoDatabase) SLock(resourceId string) (string, error) {
103+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond)
138104
defer cancel()
139105

140106
lockId := randomString(32)
141-
err := locker.SLock(ctx, resourceId, lockId, lock.LockDetails{}, -1)
107+
err := d.locker.SLock(ctx, resourceId, lockId, lock.LockDetails{}, -1)
142108
return lockId, err
143109
}
144110

145111
// Unlock unlocks a resource
146-
func (d *mongoDatabase) Unlock(collection string, lockId string) error {
147-
locker := d.lockers[collection]
148-
if locker == nil {
149-
return errors.New("Locker not found")
150-
}
151-
152-
ctx, cancel := context.WithTimeout(context.Background(), d.timeout)
112+
func (d *mongoDatabase) Unlock(lockId string) error {
113+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond)
153114
defer cancel()
154115

155-
_, err := locker.Unlock(ctx, lockId)
116+
_, err := d.locker.Unlock(ctx, lockId)
156117
return err
157118
}
158119

@@ -162,7 +123,7 @@ func (d *mongoDatabase) SetupIndexes() error {
162123

163124
// setup unique index for mints
164125
log.Debug("[DB] Setting up indexes for mints")
165-
ctx, cancel := context.WithTimeout(context.Background(), d.timeout)
126+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond)
166127
defer cancel()
167128
_, err := d.db.Collection(models.CollectionMints).Indexes().CreateOne(ctx, mongo.IndexModel{
168129
Keys: bson.D{{Key: "transaction_hash", Value: 1}},
@@ -174,7 +135,7 @@ func (d *mongoDatabase) SetupIndexes() error {
174135

175136
// setup unique index for invalid mints
176137
log.Debug("[DB] Setting up indexes for invalid mints")
177-
ctx, cancel = context.WithTimeout(context.Background(), d.timeout)
138+
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond)
178139
defer cancel()
179140
_, err = d.db.Collection(models.CollectionInvalidMints).Indexes().CreateOne(ctx, mongo.IndexModel{
180141
Keys: bson.D{{Key: "transaction_hash", Value: 1}},
@@ -186,7 +147,7 @@ func (d *mongoDatabase) SetupIndexes() error {
186147

187148
// setup unique index for burns
188149
log.Debug("[DB] Setting up indexes for burns")
189-
ctx, cancel = context.WithTimeout(context.Background(), d.timeout)
150+
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond)
190151
defer cancel()
191152
_, err = d.db.Collection(models.CollectionBurns).Indexes().CreateOne(ctx, mongo.IndexModel{
192153
Keys: bson.D{{Key: "transaction_hash", Value: 1}, {Key: "log_index", Value: 1}},
@@ -198,7 +159,7 @@ func (d *mongoDatabase) SetupIndexes() error {
198159

199160
// setup unique index for healthchecks
200161
log.Debug("[DB] Setting up indexes for healthchecks")
201-
ctx, cancel = context.WithTimeout(context.Background(), d.timeout)
162+
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond)
202163
defer cancel()
203164
_, err = d.db.Collection(models.CollectionHealthChecks).Indexes().CreateOne(ctx, mongo.IndexModel{
204165
Keys: bson.D{{Key: "validator_id", Value: 1}, {Key: "hostname", Value: 1}},
@@ -216,7 +177,7 @@ func (d *mongoDatabase) SetupIndexes() error {
216177
// Disconnect disconnects from the database
217178
func (d *mongoDatabase) Disconnect() error {
218179
log.Debug("[DB] Disconnecting from database")
219-
ctx, cancel := context.WithTimeout(context.Background(), d.timeout)
180+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond)
220181
defer cancel()
221182
err := d.db.Client().Disconnect(ctx)
222183
log.Info("[DB] Disconnected from database")
@@ -225,23 +186,23 @@ func (d *mongoDatabase) Disconnect() error {
225186

226187
// method for insert single value in a collection
227188
func (d *mongoDatabase) InsertOne(collection string, data interface{}) error {
228-
ctx, cancel := context.WithTimeout(context.Background(), d.timeout)
189+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond)
229190
defer cancel()
230191
_, err := d.db.Collection(collection).InsertOne(ctx, data)
231192
return err
232193
}
233194

234195
// method for find single value in a collection
235196
func (d *mongoDatabase) FindOne(collection string, filter interface{}, result interface{}) error {
236-
ctx, cancel := context.WithTimeout(context.Background(), d.timeout)
197+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond)
237198
defer cancel()
238199
err := d.db.Collection(collection).FindOne(ctx, filter).Decode(result)
239200
return err
240201
}
241202

242203
// method for find multiple values in a collection
243204
func (d *mongoDatabase) FindMany(collection string, filter interface{}, result interface{}) error {
244-
ctx, cancel := context.WithTimeout(context.Background(), d.timeout)
205+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond)
245206
defer cancel()
246207
cursor, err := d.db.Collection(collection).Find(ctx, filter)
247208
if err != nil {
@@ -253,15 +214,15 @@ func (d *mongoDatabase) FindMany(collection string, filter interface{}, result i
253214

254215
//method for update single value in a collection
255216
func (d *mongoDatabase) UpdateOne(collection string, filter interface{}, update interface{}) error {
256-
ctx, cancel := context.WithTimeout(context.Background(), d.timeout)
217+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond)
257218
defer cancel()
258219
_, err := d.db.Collection(collection).UpdateOne(ctx, filter, update)
259220
return err
260221
}
261222

262223
//method for upsert single value in a collection
263224
func (d *mongoDatabase) UpsertOne(collection string, filter interface{}, update interface{}) error {
264-
ctx, cancel := context.WithTimeout(context.Background(), d.timeout)
225+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Config.MongoDB.TimeoutMillis)*time.Millisecond)
265226
defer cancel()
266227

267228
opts := options.Update().SetUpsert(true)
@@ -272,7 +233,6 @@ func (d *mongoDatabase) UpsertOne(collection string, filter interface{}, update
272233
// InitDB creates a new database wrapper
273234
func InitDB() {
274235
DB = &mongoDatabase{
275-
timeout: time.Millisecond * time.Duration(Config.MongoDB.TimeoutMillis),
276236
uri: Config.MongoDB.URI,
277237
database: Config.MongoDB.Database,
278238
}

e2e/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data/

e2e/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
network:
2+
docker-compose up --force-recreate
3+
4+
clean:
5+
sudo rm -rf /tmp/data
6+
7+
test:
8+
cd tester && yarn install && yarn test

0 commit comments

Comments
 (0)