-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FAB-828 Create couchdb database automatically
Create couchdb database automatically for main system ledger upon startup. If couchdb database already exists do not re-create. There will be one couchdb database for system ledger, and eventually one couchdb database for each subledger. Tests for auto-create are included, as well as some minor refactoring and debug enhancements that were required as part of the testing. Change-Id: Ia90ffd4de74346e2472d31eceaf63f24ad1bf04e Signed-off-by: denyeart <enyeart@us.ibm.com>
- Loading branch information
Showing
6 changed files
with
218 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
core/ledger/kvledger/txmgmt/couchdbtxmgmt/couchdb_txmgmt_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
Copyright IBM Corp. 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package couchdbtxmgmt | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/hyperledger/fabric/core/ledger/kvledger/kvledgerconfig" | ||
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/couchdbtxmgmt/couchdb" | ||
"github.com/hyperledger/fabric/core/ledger/testutil" | ||
) | ||
|
||
type testEnv struct { | ||
conf *Conf | ||
couchHost string | ||
couchPort int | ||
couchDatabaseName string | ||
couchUsername string | ||
couchPassword string | ||
} | ||
|
||
func newTestEnv(t testing.TB) *testEnv { | ||
conf := &Conf{"/tmp/tests/ledger/kvledger/txmgmt/couchdbtxmgmt"} | ||
os.RemoveAll(conf.DBPath) | ||
return &testEnv{ | ||
conf: conf, | ||
couchHost: "127.0.0.1", | ||
couchPort: 5984, | ||
couchDatabaseName: "system_test", | ||
couchUsername: "", | ||
couchPassword: "", | ||
} | ||
} | ||
|
||
func (env *testEnv) Cleanup() { | ||
os.RemoveAll(env.conf.DBPath) | ||
|
||
//create a new connection | ||
couchDB, _ := couchdb.CreateConnectionDefinition(env.couchHost, env.couchPort, env.couchDatabaseName, env.couchUsername, env.couchPassword) | ||
|
||
//drop the test database if it already existed | ||
couchDB.DropDatabase() | ||
} | ||
|
||
// couchdb_test.go tests couchdb functions already. This test just tests that a CouchDB state database is auto-created | ||
// upon creating a new ledger transaction manager | ||
func TestDatabaseAutoCreate(t *testing.T) { | ||
|
||
//Only run the tests if CouchDB is explitily enabled in the code, | ||
//otherwise CouchDB may not be installed and all the tests would fail | ||
//TODO replace this with external config property rather than config within the code | ||
if kvledgerconfig.IsCouchDBEnabled() == true { | ||
|
||
env := newTestEnv(t) | ||
env.Cleanup() //cleanup at the beginning to ensure the database doesn't exist already | ||
defer env.Cleanup() //and cleanup at the end | ||
|
||
txMgr := NewCouchDBTxMgr(env.conf, | ||
env.couchHost, //couchDB host | ||
env.couchPort, //couchDB port | ||
env.couchDatabaseName, //couchDB db name | ||
env.couchUsername, //enter couchDB id | ||
env.couchPassword) //enter couchDB pw | ||
|
||
//NewCouchDBTxMgr should have automatically created the database, let's make sure it has been created | ||
//Retrieve the info for the new database and make sure the name matches | ||
dbResp, _, errdb := txMgr.couchDB.GetDatabaseInfo() | ||
testutil.AssertNoError(t, errdb, fmt.Sprintf("Error when trying to retrieve database information")) | ||
testutil.AssertEquals(t, dbResp.DbName, env.couchDatabaseName) | ||
|
||
txMgr.Shutdown() | ||
|
||
//Call NewCouchDBTxMgr again, this time the database will already exist from last time | ||
txMgr2 := NewCouchDBTxMgr(env.conf, | ||
env.couchHost, //couchDB host | ||
env.couchPort, //couchDB port | ||
env.couchDatabaseName, //couchDB db name | ||
env.couchUsername, //enter couchDB id | ||
env.couchPassword) //enter couchDB pw | ||
|
||
//Retrieve the info for the database again, and make sure the name still matches | ||
dbResp2, _, errdb2 := txMgr.couchDB.GetDatabaseInfo() | ||
testutil.AssertNoError(t, errdb2, fmt.Sprintf("Error when trying to retrieve database information")) | ||
testutil.AssertEquals(t, dbResp2.DbName, env.couchDatabaseName) | ||
|
||
txMgr2.Shutdown() | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.