@@ -20,7 +20,6 @@ import (
20
20
"archive/tar"
21
21
"bytes"
22
22
"fmt"
23
- "log"
24
23
"os"
25
24
"testing"
26
25
"time"
@@ -328,34 +327,13 @@ func TestHandleChaincodeDeploy(t *testing.T) {
328
327
savePoint := version .NewHeight (2 , 22 )
329
328
db .ApplyUpdates (batch , savePoint )
330
329
331
- //Create a buffer for the tar file
332
- buffer := new (bytes.Buffer )
333
- tarWriter := tar .NewWriter (buffer )
334
-
335
- //Add 2 index definitions
336
- var files = []struct {
337
- Name , Body string
338
- }{
339
- {"META-INF/statedb/couchdb/indexes/indexColorSortName.json" , "{\" index\" :{\" fields\" :[{\" color\" :\" desc\" }]},\" ddoc\" :\" indexColorSortName\" ,\" name\" :\" indexColorSortName\" ,\" type\" :\" json\" }" },
340
- {"META-INF/statedb/couchdb/indexes/indexSizeSortName.json" , "{\" index\" :{\" fields\" :[{\" size\" :\" desc\" }]},\" ddoc\" :\" indexSizeSortName\" ,\" name\" :\" indexSizeSortName\" ,\" type\" :\" json\" }" },
341
- }
342
- for _ , file := range files {
343
- tarHeader := & tar.Header {
344
- Name : file .Name ,
345
- Mode : 0600 ,
346
- Size : int64 (len (file .Body )),
347
- }
348
- err := tarWriter .WriteHeader (tarHeader )
349
- testutil .AssertNoError (t , err , "" )
350
-
351
- _ , err = tarWriter .Write ([]byte (file .Body ))
352
- testutil .AssertNoError (t , err , "" )
353
-
354
- }
355
- // Make sure to check the error on Close.
356
- if err := tarWriter .Close (); err != nil {
357
- log .Fatalln (err )
358
- }
330
+ //Create a tar file for test with 2 index definitions
331
+ dbArtifactsTarBytes := createTarBytesForTest (t ,
332
+ []* testFile {
333
+ {"META-INF/statedb/couchdb/indexes/indexColorSortName.json" , `{"index":{"fields":[{"color":"desc"}]},"ddoc":"indexColorSortName","name":"indexColorSortName","type":"json"}` },
334
+ {"META-INF/statedb/couchdb/indexes/indexSizeSortName.json" , `{"index":{"fields":[{"size":"desc"}]},"ddoc":"indexSizeSortName","name":"indexSizeSortName","type":"json"}` },
335
+ },
336
+ )
359
337
360
338
//Create a query
361
339
queryString := `{"selector":{"owner":"fred"}}`
@@ -369,38 +347,98 @@ func TestHandleChaincodeDeploy(t *testing.T) {
369
347
_ , err = db .ExecuteQuery ("ns1" , queryString )
370
348
testutil .AssertError (t , err , "Error should have been thrown for a missing index" )
371
349
372
- if handleDefinition , ok := db .(cceventmgmt.ChaincodeLifecycleEventListener ); ok {
350
+ handleDefinition , _ := db .(cceventmgmt.ChaincodeLifecycleEventListener )
373
351
374
- chaincodeDef := & cceventmgmt.ChaincodeDefinition {Name : "ns1" , Hash : nil , Version : "" }
352
+ chaincodeDef := & cceventmgmt.ChaincodeDefinition {Name : "ns1" , Hash : nil , Version : "" }
375
353
376
- //Test HandleChaincodeDefinition with a valid tar file
377
- err : = handleDefinition .HandleChaincodeDeploy (chaincodeDef , buffer . Bytes () )
378
- testutil .AssertNoError (t , err , "" )
354
+ //Test HandleChaincodeDefinition with a valid tar file
355
+ err = handleDefinition .HandleChaincodeDeploy (chaincodeDef , dbArtifactsTarBytes )
356
+ testutil .AssertNoError (t , err , "" )
379
357
380
- //Test HandleChaincodeDefinition with a nil tar file
381
- err = handleDefinition .HandleChaincodeDeploy (chaincodeDef , nil )
382
- testutil .AssertNoError (t , err , "" )
358
+ //Sleep to allow time for index creation
359
+ time .Sleep (100 * time .Millisecond )
360
+ //Create a query with a sort
361
+ queryString = `{"selector":{"owner":"fred"}, "sort": [{"size": "desc"}]}`
362
+
363
+ //Query should complete without error
364
+ _ , err = db .ExecuteQuery ("ns1" , queryString )
365
+ testutil .AssertNoError (t , err , "" )
366
+
367
+ //Query namespace "ns2", index is only created in "ns1". This should return an error.
368
+ _ , err = db .ExecuteQuery ("ns2" , queryString )
369
+ testutil .AssertError (t , err , "Error should have been thrown for a missing index" )
383
370
384
- //Test HandleChaincodeDefinition with a bad tar file
385
- err = handleDefinition .HandleChaincodeDeploy (chaincodeDef , [] byte ( `This is a really bad tar file` ) )
386
- testutil .AssertError (t , err , "Error should have been thrown for a bad tar file " )
371
+ //Test HandleChaincodeDefinition with a nil tar file
372
+ err = handleDefinition .HandleChaincodeDeploy (chaincodeDef , nil )
373
+ testutil .AssertNoError (t , err , "" )
387
374
388
- //Test HandleChaincodeDefinition with a nil chaincodeDef
389
- err = handleDefinition .HandleChaincodeDeploy (nil , []byte (`This is a really bad tar file` ))
390
- testutil .AssertError (t , err , "Error should have been thrown for a nil chaincodeDefinition " )
375
+ //Test HandleChaincodeDefinition with a bad tar file
376
+ err = handleDefinition .HandleChaincodeDeploy (chaincodeDef , []byte (`This is a really bad tar file` ))
377
+ testutil .AssertNoError (t , err , "Error should not have been thrown for a bad tar file " )
391
378
392
- //Sleep to allow time for index creation
393
- time . Sleep ( 100 * time . Millisecond )
394
- //Create a query with a sort
395
- queryString = `{"selector":{"owner":"fred"}, "sort": [{"size": "desc"}]}`
379
+ //Test HandleChaincodeDefinition with a nil chaincodeDef
380
+ err = handleDefinition . HandleChaincodeDeploy ( nil , dbArtifactsTarBytes )
381
+ testutil . AssertError ( t , err , "Error should have been thrown for a nil chaincodeDefinition" )
382
+ }
396
383
397
- //Query should complete without error
398
- _ , err = db .ExecuteQuery ("ns1" , queryString )
399
- testutil .AssertNoError (t , err , "" )
384
+ func TestHandleChaincodeDeployErroneousIndexFile (t * testing.T ) {
385
+ channelName := "ch1"
386
+ env := NewTestVDBEnv (t )
387
+ env .Cleanup (channelName )
388
+ defer env .Cleanup (channelName )
389
+ db , err := env .DBProvider .GetDBHandle (channelName )
390
+ testutil .AssertNoError (t , err , "" )
391
+ db .Open ()
392
+ defer db .Close ()
393
+
394
+ batch := statedb .NewUpdateBatch ()
395
+ batch .Put ("ns1" , "key1" , []byte (`{"asset_name": "marble1","color": "blue","size": 1,"owner": "tom"}` ), version .NewHeight (1 , 1 ))
396
+ batch .Put ("ns1" , "key2" , []byte (`{"asset_name": "marble2","color": "blue","size": 2,"owner": "jerry"}` ), version .NewHeight (1 , 2 ))
397
+ ccEventListener , _ := db .(cceventmgmt.ChaincodeLifecycleEventListener )
398
+
399
+ // Create a tar file for test with 2 index definitions - one of them being errorneous
400
+ badSyntaxFileContent := `{"index":{"fields": This is a bad json}`
401
+ dbArtifactsTarBytes := createTarBytesForTest (t ,
402
+ []* testFile {
403
+ {"META-INF/statedb/couchdb/indexes/indexSizeSortName.json" , `{"index":{"fields":[{"size":"desc"}]},"ddoc":"indexSizeSortName","name":"indexSizeSortName","type":"json"}` },
404
+ {"META-INF/statedb/couchdb/indexes/badSyntax.json" , badSyntaxFileContent },
405
+ },
406
+ )
407
+ // Test HandleChaincodeDefinition with a bad tar file
408
+ chaincodeName := "ns1"
409
+ chaincodeVer := "1.0"
410
+ chaincodeDef := & cceventmgmt.ChaincodeDefinition {Name : chaincodeName , Hash : []byte ("Hash for test chaincode" ), Version : chaincodeVer }
411
+ err = ccEventListener .HandleChaincodeDeploy (chaincodeDef , dbArtifactsTarBytes )
412
+ testutil .AssertNoError (t , err , "A tar with a bad syntax file should not cause an error" )
413
+ //Sleep to allow time for index creation
414
+ time .Sleep (100 * time .Millisecond )
415
+ //Query should complete without error
416
+ _ , err = db .ExecuteQuery ("ns1" , `{"selector":{"owner":"fred"}, "sort": [{"size": "desc"}]}` )
417
+ testutil .AssertNoError (t , err , "" )
418
+ }
400
419
401
- //Query namespace "ns2", index is only created in "ns1". This should return an error.
402
- _ , err = db . ExecuteQuery ( "ns2" , queryString )
403
- testutil . AssertError ( t , err , "Error should have been thrown for a missing index" )
420
+ type testFile struct {
421
+ name , body string
422
+ }
404
423
424
+ func createTarBytesForTest (t * testing.T , testFiles []* testFile ) []byte {
425
+ //Create a buffer for the tar file
426
+ buffer := new (bytes.Buffer )
427
+ tarWriter := tar .NewWriter (buffer )
428
+
429
+ for _ , file := range testFiles {
430
+ tarHeader := & tar.Header {
431
+ Name : file .name ,
432
+ Mode : 0600 ,
433
+ Size : int64 (len (file .body )),
434
+ }
435
+ err := tarWriter .WriteHeader (tarHeader )
436
+ testutil .AssertNoError (t , err , "" )
437
+
438
+ _ , err = tarWriter .Write ([]byte (file .body ))
439
+ testutil .AssertNoError (t , err , "" )
405
440
}
441
+ // Make sure to check the error on Close.
442
+ testutil .AssertNoError (t , tarWriter .Close (), "" )
443
+ return buffer .Bytes ()
406
444
}
0 commit comments