Browse Source

[2342] Corrections based on first part for third section of review.

Stephen Morris 12 years ago
parent
commit
df141d203e

+ 46 - 1
src/lib/dhcp/lease_mgr.cc

@@ -34,6 +34,9 @@ LeaseMgr::LeaseMgr(const LeaseMgr::ParameterMap& parameters)
     : parameters_(parameters) {
     : parameters_(parameters) {
 }
 }
 
 
+LeaseMgr::~LeaseMgr() {
+}
+
 std::string LeaseMgr::getParameter(const std::string& name) const {
 std::string LeaseMgr::getParameter(const std::string& name) const {
     ParameterMap::const_iterator param = parameters_.find(name);
     ParameterMap::const_iterator param = parameters_.find(name);
     if (param == parameters_.end()) {
     if (param == parameters_.end()) {
@@ -42,5 +45,47 @@ std::string LeaseMgr::getParameter(const std::string& name) const {
     return (param->second);
     return (param->second);
 }
 }
 
 
-LeaseMgr::~LeaseMgr() {
+std::string
+Lease6::toText() {
+    ostringstream stream;
+
+    stream << "Type:          " << static_cast<int>(type_) << " (";
+    switch (type_) {
+        case Lease6::LEASE_IA_NA:
+            stream << "IA_NA)\n";
+            break;
+        case Lease6::LEASE_IA_TA:
+            stream << "IA_TA)\n";
+            break;
+        case Lease6::LEASE_IA_PD:
+            stream << "IA_PD)\n";
+            break;
+        default:
+            stream << "unknown)\n";
+    }
+    stream << "Address:       " << addr_.toText() << "\n"
+           << "Prefix length: " << static_cast<int>(prefixlen_) << "\n"
+           << "IAID:          " << iaid_ << "\n"
+           << "Pref life:     " << preferred_lft_ << "\n"
+           << "Valid life:    " << valid_lft_ << "\n"
+           << "Cltt:          " << cltt_ << "\n"
+           << "Subnet ID:     " << subnet_id_ << "\n";
+
+    return (stream.str());
+}
+
+bool
+Lease6::operator==(const Lease6& other) const {
+    return (
+        type_ == other.type_ &&
+        addr_ == other.addr_ &&
+        prefixlen_ == other.prefixlen_ &&
+        iaid_ == other.iaid_ &&
+        hwaddr_ == other.hwaddr_ &&
+        *duid_ == *other.duid_ &&
+        preferred_lft_ == other.preferred_lft_ &&
+        valid_lft_ == other.valid_lft_ &&
+        cltt_ == other.cltt_ &&
+        subnet_id_ == other.subnet_id_
+        );
 }
 }

+ 20 - 0
src/lib/dhcp/lease_mgr.h

@@ -58,6 +58,8 @@
 /// Nevertheless, we hope to have failover protocol eventually implemented in
 /// Nevertheless, we hope to have failover protocol eventually implemented in
 /// the Kea.
 /// the Kea.
 
 
+#include <iostream>
+
 namespace isc {
 namespace isc {
 namespace dhcp {
 namespace dhcp {
 
 
@@ -289,6 +291,24 @@ struct Lease6 {
     ///
     ///
     /// Initialize fields that don't have a default constructor.
     /// Initialize fields that don't have a default constructor.
     Lease6() : addr_("::") {}
     Lease6() : addr_("::") {}
+
+    /// @brief Convert Lease6 to Printable Form
+    ///
+    /// @return String form of the lease
+    std::string toText();
+
+    /// @brief Compare two leases for equality
+    ///
+    /// @param other lease6 object with which to compare
+    bool operator==(const Lease6& other) const;
+
+    /// @brief Compare two leases for inequality
+    ///
+    /// @param other lease6 object with which to compare
+    bool operator!=(const Lease6& other) const {
+        return (!operator==(other));
+    }
+
 };
 };
 
 
 /// @brief Pointer to a Lease6 structure.
 /// @brief Pointer to a Lease6 structure.

+ 9 - 0
src/lib/dhcp/mysql_lease_mgr.cc

@@ -475,6 +475,15 @@ MySqlLeaseMgr::openDatabase() {
         isc_throw(NoDatabaseName, "must specified a name for the database");
         isc_throw(NoDatabaseName, "must specified a name for the database");
     }
     }
 
 
+    // Set options for the connection:
+    // - automatic reconnection
+    my_bool auto_reconnect = 1;
+    int result = mysql_options(mysql_, MYSQL_OPT_RECONNECT, &auto_reconnect);
+    if (result != 0) {
+        isc_throw(DbOpenError, "unable to set auto-reconnect option: " <<
+                  mysql_error(mysql_));
+    }
+
     // Open the database.  Use defaults for non-specified options.
     // Open the database.  Use defaults for non-specified options.
     MYSQL* status = mysql_real_connect(mysql_, host, user, password, name,
     MYSQL* status = mysql_real_connect(mysql_, host, user, password, name,
                                        0, NULL, 0);
                                        0, NULL, 0);

+ 5 - 0
src/lib/dhcp/mysql_lease_mgr.h

@@ -22,6 +22,11 @@
 namespace isc {
 namespace isc {
 namespace dhcp {
 namespace dhcp {
 
 
+// Define the current database schema values
+
+const uint32_t CURRENT_VERSION_VERSION = 0;
+const uint32_t CURRENT_VERSION_MINOR = 1;
+
 /// @brief MySQL Lease Manager
 /// @brief MySQL Lease Manager
 ///
 ///
 /// This is a concrete API for the backend for the MySQL database.
 /// This is a concrete API for the backend for the MySQL database.

+ 37 - 67
src/lib/dhcp/tests/mysql_lease_mgr_unittest.cc

@@ -127,6 +127,19 @@ public:
 /// opened: the fixtures assume that and check basic operations.
 /// opened: the fixtures assume that and check basic operations.
 
 
 TEST(MySqlOpenTest, OpenDatabase) {
 TEST(MySqlOpenTest, OpenDatabase) {
+    // Check that database opens correctly and tidy up.  If it fails, print
+    // the error message.
+    try {
+        LeaseMgrFactory::create(validConnectionString());
+        EXPECT_NO_THROW((void) LeaseMgrFactory::instance());
+        LeaseMgrFactory::destroy();
+    } catch (const isc::Exception& ex) {
+        FAIL() << "*** ERROR: unable to open database, reason:\n"
+               << "    " << ex.what()
+               << "*** The test environment is broken and must be fixed\n"
+               << "*** before the MySQL tests will run correctly.\n";
+    }
+
     // Check that attempting to get an instance of the lease manager when
     // Check that attempting to get an instance of the lease manager when
     // none is set throws an exception.
     // none is set throws an exception.
     EXPECT_THROW(LeaseMgrFactory::instance(), NoLeaseManager);
     EXPECT_THROW(LeaseMgrFactory::instance(), NoLeaseManager);
@@ -159,29 +172,36 @@ TEST(MySqlOpenTest, OpenDatabase) {
     EXPECT_THROW(LeaseMgrFactory::create(connectionString(
     EXPECT_THROW(LeaseMgrFactory::create(connectionString(
         VALID_TYPE, NULL, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
         VALID_TYPE, NULL, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
         NoDatabaseName);
         NoDatabaseName);
-
-    // Check that database opens correctly.
-    ASSERT_NO_THROW(LeaseMgrFactory::create(validConnectionString()));
-    EXPECT_NO_THROW((void) LeaseMgrFactory::instance());
-
-    // ... and tidy up.
-    LeaseMgrFactory::destroy();
 }
 }
 
 
 /// @brief Check conversion functions
 /// @brief Check conversion functions
 TEST_F(MySqlLeaseMgrTest, CheckTimeConversion) {
 TEST_F(MySqlLeaseMgrTest, CheckTimeConversion) {
     const time_t cltt = time(NULL);
     const time_t cltt = time(NULL);
     const uint32_t valid_lft = 86400;       // 1 day
     const uint32_t valid_lft = 86400;       // 1 day
-    MYSQL_TIME expire;
-
-    MySqlLeaseMgr::convertToDatabaseTime(cltt, valid_lft, expire);
-    EXPECT_LE(2012, expire.year);       // Code was written in 2012
-    EXPECT_EQ(0, expire.second_part);
-    EXPECT_EQ(0, expire.neg);
+    struct tm tm_expire;
+    MYSQL_TIME mysql_expire;
+
+    // Work out what the broken-down time will be for one day
+    // after the current time.
+    time_t expire_time = cltt + valid_lft;
+    (void) localtime_r(&expire_time, &tm_expire);
+
+    // Convert to the database time
+    MySqlLeaseMgr::convertToDatabaseTime(cltt, valid_lft, mysql_expire);
+
+    // Are the times the same?
+    EXPECT_EQ(tm_expire.tm_year + 1900, mysql_expire.year);
+    EXPECT_EQ(tm_expire.tm_mon + 1,  mysql_expire.month);
+    EXPECT_EQ(tm_expire.tm_mday, mysql_expire.day);
+    EXPECT_EQ(tm_expire.tm_hour, mysql_expire.hour);
+    EXPECT_EQ(tm_expire.tm_min, mysql_expire.minute);
+    EXPECT_EQ(tm_expire.tm_sec, mysql_expire.second);
+    EXPECT_EQ(0, mysql_expire.second_part);
+    EXPECT_EQ(0, mysql_expire.neg);
 
 
     // Convert back
     // Convert back
     time_t converted_cltt = 0;
     time_t converted_cltt = 0;
-    MySqlLeaseMgr::convertFromDatabaseTime(expire, valid_lft, converted_cltt);
+    MySqlLeaseMgr::convertFromDatabaseTime(mysql_expire, valid_lft, converted_cltt);
     EXPECT_EQ(cltt, converted_cltt);
     EXPECT_EQ(cltt, converted_cltt);
 }
 }
 
 
@@ -190,58 +210,8 @@ TEST_F(MySqlLeaseMgrTest, CheckVersion) {
     // Check version
     // Check version
     pair<uint32_t, uint32_t> version;
     pair<uint32_t, uint32_t> version;
     ASSERT_NO_THROW(version = lmptr_->getVersion());
     ASSERT_NO_THROW(version = lmptr_->getVersion());
-    EXPECT_EQ(0, version.first);
-    EXPECT_EQ(1, version.second);
-}
-
-/// @brief Print Elements of Lease6 Structure
-///
-/// @param lease Pointer to lease to print
-/// @param title Title to print before the lease information
-void
-printLease6(const Lease6Ptr& lease, const char* title = NULL) {
-    if (title != NULL) {
-        cout << title << "\n";
-    }
-
-    cout << "   Type:          ";
-    switch (lease->type_) {
-        case Lease6::LEASE_IA_NA:
-            cout << "IA_NA\n";
-            break;
-        case Lease6::LEASE_IA_TA:
-            cout << "IA_TA\n";
-            break;
-        case Lease6::LEASE_IA_PD:
-            cout << "IA_PD\n";
-            break;
-        default:
-            cout << "unknown (" << static_cast<int>(lease->type_) << ")\n";
-    }
-    cout << "   Address:       " << lease->addr_.toText() << "\n";
-    cout << "   Prefix length: " << static_cast<int>(lease->prefixlen_) << "\n";
-    cout << "   IAID:          " << lease->iaid_ << "\n";
-    cout << "   Pref life:     " << lease->preferred_lft_ << "\n";
-    cout << "   Valid life:    " << lease->valid_lft_ << "\n";
-    cout << "   Cltt:          " << lease->cltt_ << "\n";
-    cout << "   Subnet ID:     " << lease->subnet_id_ << "\n";
-}
-
-/// @brief Compare Lease4 Structure
-bool
-compareLease6(const Lease6Ptr& first, const Lease6Ptr& second) {
-    return (
-        first->type_ == second->type_ &&
-        first->addr_ == second->addr_ &&
-        first->prefixlen_ == second->prefixlen_ &&
-        first->iaid_ == second->iaid_ &&
-        first->hwaddr_ == second->hwaddr_ &&
-        *first->duid_ == *second->duid_ &&
-        first->preferred_lft_ == second->preferred_lft_ &&
-        first->valid_lft_ == second->valid_lft_ &&
-        first->cltt_ == second->cltt_ &&
-        first->subnet_id_ == second->subnet_id_
-        );
+    EXPECT_EQ(CURRENT_VERSION_VERSION, version.first);
+    EXPECT_EQ(CURRENT_VERSION_MINOR, version.second);
 }
 }
 
 
 void
 void
@@ -318,7 +288,7 @@ TEST_F(MySqlLeaseMgrTest, BasicLease6) {
     l2->subnet_id_ = l1->subnet_id_;    // Same as l1
     l2->subnet_id_ = l1->subnet_id_;    // Same as l1
 
 
     // Sanity check that the leases are different
     // Sanity check that the leases are different
-    ASSERT_FALSE(compareLease6(l1, l2));
+    ASSERT_TRUE(*l1 != *l2);
 
 
     // Start the tests.  Add two leases to the database, read them back and
     // Start the tests.  Add two leases to the database, read them back and
     // check they are what we think they are.
     // check they are what we think they are.