@@ -31,21 +31,17 @@ import (
31
31
"github.com/spf13/viper"
32
32
)
33
33
34
- //Basic setup to test couch
35
- var connectURL = "couchdb:5984"
36
- var badConnectURL = "couchdb:5990"
37
- var username = ""
38
- var password = ""
39
- var maxRetries = 3
40
- var maxRetriesOnStartup = 10
41
- var requestTimeout = time .Second * 20
42
-
34
+ const badConnectURL = "couchdb:5990"
43
35
const updateDocumentConflictError = "conflict"
44
36
const updateDocumentConflictReason = "Document update conflict."
45
37
38
+ var couchDBDef * ledgerconfig.CouchDBDef
39
+
46
40
func cleanup (database string ) error {
47
41
//create a new connection
48
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
42
+ couchInstance , err := CreateCouchInstance (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
43
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
44
+
49
45
if err != nil {
50
46
fmt .Println ("Unexpected error" , err )
51
47
return err
@@ -68,28 +64,47 @@ type Asset struct {
68
64
var assetJSON = []byte (`{"asset_name":"marble1","color":"blue","size":"35","owner":"jerry"}` )
69
65
70
66
func TestMain (m * testing.M ) {
67
+ // Read the core.yaml file for default config.
71
68
ledgertestutil .SetupCoreYAMLConfig ("./../../../../peer" )
69
+
70
+ // Switch to CouchDB
72
71
viper .Set ("ledger.state.stateDatabase" , "CouchDB" )
72
+
73
+ // both vagrant and CI have couchdb configured at host "couchdb"
74
+ viper .Set ("ledger.state.couchDBConfig.couchDBAddress" , "couchdb:5984" )
75
+ // Replace with correct username/password such as
76
+ // admin/admin if user security is enabled on couchdb.
77
+ viper .Set ("ledger.state.couchDBConfig.username" , "" )
78
+ viper .Set ("ledger.state.couchDBConfig.password" , "" )
79
+ viper .Set ("ledger.state.couchDBConfig.maxRetries" , 3 )
80
+ viper .Set ("ledger.state.couchDBConfig.maxRetriesOnStartup" , 10 )
81
+ viper .Set ("ledger.state.couchDBConfig.requestTimeout" , time .Second * 20 )
82
+
83
+ // Create CouchDB definition from config parameters
84
+ couchDBDef = ledgerconfig .GetCouchDBDefinition ()
85
+
86
+ //run the tests
73
87
result := m .Run ()
88
+
89
+ //revert to default goleveldb
74
90
viper .Set ("ledger.state.stateDatabase" , "goleveldb" )
75
91
os .Exit (result )
76
92
}
77
93
78
94
func TestDBConnectionDef (t * testing.T ) {
79
95
80
- //call a helper method to load the core.yaml
81
- ledgertestutil .SetupCoreYAMLConfig ("./../../../../peer" )
82
-
83
96
//create a new connection
84
- _ , err := CreateConnectionDefinition (connectURL , "" , "" , maxRetries , maxRetriesOnStartup , requestTimeout )
97
+ _ , err := CreateConnectionDefinition (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
98
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
85
99
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create database connection definition" ))
86
100
87
101
}
88
102
89
103
func TestDBBadConnectionDef (t * testing.T ) {
90
104
91
105
//create a new connection
92
- _ , err := CreateConnectionDefinition ("^^^localhost:5984" , "" , "" , maxRetries , maxRetriesOnStartup , requestTimeout )
106
+ _ , err := CreateConnectionDefinition ("^^^localhost:5984" , couchDBDef .Username , couchDBDef .Password ,
107
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
93
108
testutil .AssertError (t , err , fmt .Sprintf ("Did not receive error when trying to create database connection definition with a bad hostname" ))
94
109
95
110
}
@@ -105,7 +120,8 @@ func TestDBCreateSaveWithoutRevision(t *testing.T) {
105
120
106
121
if err == nil {
107
122
//create a new instance and database object
108
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
123
+ couchInstance , err := CreateCouchInstance (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
124
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
109
125
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
110
126
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
111
127
@@ -131,7 +147,8 @@ func TestDBCreateEnsureFullCommit(t *testing.T) {
131
147
132
148
if err == nil {
133
149
//create a new instance and database object
134
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
150
+ couchInstance , err := CreateCouchInstance (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
151
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
135
152
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
136
153
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
137
154
@@ -156,25 +173,29 @@ func TestDBBadDatabaseName(t *testing.T) {
156
173
if ledgerconfig .IsCouchDBEnabled () {
157
174
158
175
//create a new instance and database object using a valid database name mixed case
159
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
176
+ couchInstance , err := CreateCouchInstance (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
177
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
160
178
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
161
179
_ , dberr := CreateCouchDatabase (* couchInstance , "testDB" )
162
180
testutil .AssertNoError (t , dberr , fmt .Sprintf ("Error when testing a valid database name" ))
163
181
164
182
//create a new instance and database object using a valid database name letters and numbers
165
- couchInstance , err = CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
183
+ couchInstance , err = CreateCouchInstance (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
184
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
166
185
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
167
186
_ , dberr = CreateCouchDatabase (* couchInstance , "test132" )
168
187
testutil .AssertNoError (t , dberr , fmt .Sprintf ("Error when testing a valid database name" ))
169
188
170
189
//create a new instance and database object using a valid database name - special characters
171
- couchInstance , err = CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
190
+ couchInstance , err = CreateCouchInstance (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
191
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
172
192
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
173
193
_ , dberr = CreateCouchDatabase (* couchInstance , "test1234~!@#$%^&*()[]{}." )
174
194
testutil .AssertNoError (t , dberr , fmt .Sprintf ("Error when testing a valid database name" ))
175
195
176
196
//create a new instance and database object using a invalid database name - too long /*
177
- couchInstance , err = CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
197
+ couchInstance , err = CreateCouchInstance (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
198
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
178
199
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
179
200
_ , dberr = CreateCouchDatabase (* couchInstance , "A12345678901234567890123456789012345678901234" +
180
201
"56789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
@@ -195,7 +216,8 @@ func TestDBBadConnection(t *testing.T) {
195
216
if ledgerconfig .IsCouchDBEnabled () {
196
217
197
218
//create a new instance and database object
198
- _ , err := CreateCouchInstance (badConnectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
219
+ _ , err := CreateCouchInstance (badConnectURL , couchDBDef .Username , couchDBDef .Password ,
220
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
199
221
testutil .AssertError (t , err , fmt .Sprintf ("Error should have been thrown for a bad connection" ))
200
222
}
201
223
}
@@ -211,7 +233,8 @@ func TestDBCreateDatabaseAndPersist(t *testing.T) {
211
233
212
234
if err == nil {
213
235
//create a new instance and database object
214
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
236
+ couchInstance , err := CreateCouchInstance (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
237
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
215
238
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
216
239
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
217
240
@@ -305,7 +328,8 @@ func TestDBRequestTimeout(t *testing.T) {
305
328
306
329
//create a new instance and database object with a timeout that will fail
307
330
//Also use a maxRetriesOnStartup=3 to reduce the number of retries
308
- _ , err := CreateCouchInstance (connectURL , username , password , maxRetries , 3 , impossibleTimeout )
331
+ _ , err := CreateCouchInstance (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
332
+ couchDBDef .MaxRetries , 3 , impossibleTimeout )
309
333
testutil .AssertError (t , err , fmt .Sprintf ("Error should have been thown while trying to create a couchdb instance with a connection timeout" ))
310
334
311
335
//see if the error message contains the timeout error
@@ -327,7 +351,8 @@ func TestDBBadJSON(t *testing.T) {
327
351
if err == nil {
328
352
329
353
//create a new instance and database object
330
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
354
+ couchInstance , err := CreateCouchInstance (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
355
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
331
356
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
332
357
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
333
358
@@ -363,7 +388,8 @@ func TestPrefixScan(t *testing.T) {
363
388
364
389
if err == nil {
365
390
//create a new instance and database object
366
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
391
+ couchInstance , err := CreateCouchInstance (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
392
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
367
393
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
368
394
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
369
395
@@ -435,7 +461,8 @@ func TestDBSaveAttachment(t *testing.T) {
435
461
attachments = append (attachments , attachment )
436
462
437
463
//create a new instance and database object
438
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
464
+ couchInstance , err := CreateCouchInstance (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
465
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
439
466
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
440
467
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
441
468
@@ -468,7 +495,8 @@ func TestDBDeleteDocument(t *testing.T) {
468
495
469
496
if err == nil {
470
497
//create a new instance and database object
471
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
498
+ couchInstance , err := CreateCouchInstance (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
499
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
472
500
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
473
501
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
474
502
@@ -506,7 +534,8 @@ func TestDBDeleteNonExistingDocument(t *testing.T) {
506
534
507
535
if err == nil {
508
536
//create a new instance and database object
509
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
537
+ couchInstance , err := CreateCouchInstance (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
538
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
510
539
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
511
540
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
512
541
@@ -645,7 +674,8 @@ func TestRichQuery(t *testing.T) {
645
674
646
675
if err == nil {
647
676
//create a new instance and database object --------------------------------------------------------
648
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
677
+ couchInstance , err := CreateCouchInstance (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
678
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
649
679
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
650
680
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
651
681
@@ -859,7 +889,8 @@ func TestBatchBatchOperations(t *testing.T) {
859
889
defer cleanup (database )
860
890
861
891
//create a new instance and database object --------------------------------------------------------
862
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
892
+ couchInstance , err := CreateCouchInstance (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
893
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
863
894
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
864
895
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
865
896
0 commit comments