|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package history |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/hyperledger/fabric/core/ledger/ledgerconfig" |
| 25 | + "github.com/hyperledger/fabric/core/ledger/testutil" |
| 26 | + "github.com/hyperledger/fabric/core/ledger/util/couchdb" |
| 27 | +) |
| 28 | + |
| 29 | +//Complex setup to test the use of couch in ledger |
| 30 | +type testEnvCouch struct { |
| 31 | + couchDBPath string |
| 32 | + couchDBAddress string |
| 33 | + couchDatabaseName string |
| 34 | + couchUsername string |
| 35 | + couchPassword string |
| 36 | +} |
| 37 | + |
| 38 | +func newTestEnvCouch(t testing.TB, dbPath string, dbName string) *testEnvCouch { |
| 39 | + |
| 40 | + couchDBDef := ledgerconfig.GetCouchDBDefinition() |
| 41 | + os.RemoveAll(dbPath) |
| 42 | + |
| 43 | + return &testEnvCouch{ |
| 44 | + couchDBPath: dbPath, |
| 45 | + couchDBAddress: couchDBDef.URL, |
| 46 | + couchDatabaseName: dbName, |
| 47 | + couchUsername: couchDBDef.Username, |
| 48 | + couchPassword: couchDBDef.Password, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func (env *testEnvCouch) cleanup() { |
| 53 | + os.RemoveAll(env.couchDBPath) |
| 54 | + //create a new connection |
| 55 | + couchDB, _ := couchdb.CreateConnectionDefinition(env.couchDBAddress, env.couchDatabaseName, env.couchUsername, env.couchPassword) |
| 56 | + //drop the test database if it already existed |
| 57 | + couchDB.DropDatabase() |
| 58 | +} |
| 59 | + |
| 60 | +// couchdb_test.go tests couchdb functions already. This test just tests that a CouchDB history database is auto-created |
| 61 | +// upon creating a new history transaction manager |
| 62 | +func TestHistoryDatabaseAutoCreate(t *testing.T) { |
| 63 | + |
| 64 | + //call a helper method to load the core.yaml |
| 65 | + testutil.SetupCoreYAMLConfig("./../../../peer") |
| 66 | + |
| 67 | + //Only run the tests if CouchDB is explitily enabled in the code, |
| 68 | + //otherwise CouchDB may not be installed and all the tests would fail |
| 69 | + //TODO replace this with external config property rather than config within the code |
| 70 | + if ledgerconfig.IsCouchDBEnabled() == true { |
| 71 | + |
| 72 | + env := newTestEnvCouch(t, "/tmp/tests/ledger/history", "history-test") |
| 73 | + env.cleanup() //cleanup at the beginning to ensure the database doesn't exist already |
| 74 | + defer env.cleanup() //and cleanup at the end |
| 75 | + |
| 76 | + histMgr := NewCouchDBHistMgr( |
| 77 | + env.couchDBAddress, //couchDB Address |
| 78 | + env.couchDatabaseName, //couchDB db name |
| 79 | + env.couchUsername, //enter couchDB id |
| 80 | + env.couchPassword) //enter couchDB pw |
| 81 | + |
| 82 | + //NewCouchDBhistMgr should have automatically created the database, let's make sure it has been created |
| 83 | + //Retrieve the info for the new database and make sure the name matches |
| 84 | + dbResp, _, errdb := histMgr.couchDB.GetDatabaseInfo() |
| 85 | + testutil.AssertNoError(t, errdb, fmt.Sprintf("Error when trying to retrieve database information")) |
| 86 | + testutil.AssertEquals(t, dbResp.DbName, env.couchDatabaseName) |
| 87 | + |
| 88 | + //Call NewCouchDBhistMgr again, this time the database will already exist from last time |
| 89 | + histMgr2 := NewCouchDBHistMgr( |
| 90 | + env.couchDBAddress, //couchDB Address |
| 91 | + env.couchDatabaseName, //couchDB db name |
| 92 | + env.couchUsername, //enter couchDB id |
| 93 | + env.couchPassword) //enter couchDB pw |
| 94 | + |
| 95 | + //Retrieve the info for the database again, and make sure the name still matches |
| 96 | + dbResp2, _, errdb2 := histMgr2.couchDB.GetDatabaseInfo() |
| 97 | + testutil.AssertNoError(t, errdb2, fmt.Sprintf("Error when trying to retrieve database information")) |
| 98 | + testutil.AssertEquals(t, dbResp2.DbName, env.couchDatabaseName) |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments