summaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorMarcin Siodelski <marcin@isc.org>2015-01-12 12:38:47 +0100
committerMarcin Siodelski <marcin@isc.org>2015-01-12 12:38:47 +0100
commitfbce7dd40fdc965ea439746797dcebb9cb83836e (patch)
tree84cdc2e2c9a99c8188e5f18d8625c7b87449a999 /src/lib
parent[3671] Memfile loads leases from multiple lease files. (diff)
downloadkea-fbce7dd40fdc965ea439746797dcebb9cb83836e.tar.xz
kea-fbce7dd40fdc965ea439746797dcebb9cb83836e.zip
[3671] Added unit test for loading v6 leases from multiple files.
Also, prevented opening the optional lease files if they don't exist.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/dhcpsrv/memfile_lease_mgr.cc16
-rw-r--r--src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc79
-rw-r--r--src/lib/util/csv_file.cc13
-rw-r--r--src/lib/util/csv_file.h10
-rw-r--r--src/lib/util/tests/csv_file_unittest.cc26
5 files changed, 134 insertions, 10 deletions
diff --git a/src/lib/dhcpsrv/memfile_lease_mgr.cc b/src/lib/dhcpsrv/memfile_lease_mgr.cc
index 31e4dfa982..31e03c08c5 100644
--- a/src/lib/dhcpsrv/memfile_lease_mgr.cc
+++ b/src/lib/dhcpsrv/memfile_lease_mgr.cc
@@ -491,10 +491,16 @@ loadLeasesFromFiles(const std::string& filename,
s << "." << i;
}
lease_file.reset(new LeaseFileType(s.str()));
- // If the file doesn't exist it will be created as an empty
- // file (with no leases).
- lease_file->open();
- LeaseFileLoader::load<LeaseObjectType>(*lease_file, storage,
- MAX_LEASE_ERRORS);
+ // Don't open the file if it doesn't exist and it is not the
+ // primary lease file - not ending with .1 or .2. Those files
+ // are optional and we don't want to create them if they don't
+ // exist.
+ if (i == 0 || lease_file->exists()) {
+ // If the file doesn't exist it will be created as an empty
+ // file (with no leases).
+ lease_file->open();
+ LeaseFileLoader::load<LeaseObjectType>(*lease_file, storage,
+ MAX_LEASE_ERRORS);
+ }
}
}
diff --git a/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc
index ba55093572..ce6a97439e 100644
--- a/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc
+++ b/src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc
@@ -481,8 +481,9 @@ TEST_F(MemfileLeaseMgrTest, versionCheck) {
LeaseMgrFactory::destroy();
}
-// This test checks that the backend reads lease data from multiple files.
-TEST_F(MemfileLeaseMgrTest, loadMultipleLeaseFiles) {
+// This test checks that the backend reads DHCPv4 lease data from multiple
+// files.
+TEST_F(MemfileLeaseMgrTest, load4MultipleLeaseFiles) {
LeaseFileIO io2("leasefile4_0.csv.2");
io2.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
"fqdn_fwd,fqdn_rev,hostname\n"
@@ -504,25 +505,99 @@ TEST_F(MemfileLeaseMgrTest, loadMultipleLeaseFiles) {
startBackend(V4);
+ // This lease only exists in the second file and the cltt should
+ // be 0.
Lease4Ptr lease = lmptr_->getLease4(IOAddress("192.0.2.1"));
ASSERT_TRUE(lease);
EXPECT_EQ(0, lease->cltt_);
+ // This lease only exists in the first file and the cltt should
+ // be 0.
lease = lmptr_->getLease4(IOAddress("192.0.2.2"));
ASSERT_TRUE(lease);
EXPECT_EQ(0, lease->cltt_);
+ // This lease only exists in the third file and the cltt should
+ // be 0.
lease = lmptr_->getLease4(IOAddress("192.0.2.10"));
ASSERT_TRUE(lease);
EXPECT_EQ(0, lease->cltt_);
+ // This lease exists in the first and second file and the cltt
+ // should be calculated using the expiration time and the
+ // valid lifetime from the second file.
lease = lmptr_->getLease4(IOAddress("192.0.2.11"));
ASSERT_TRUE(lease);
EXPECT_EQ(200, lease->cltt_);
+ // Thsi lease exists in the second and third file and the cltt
+ // should be calculated using the expiration time and the
+ // valid lifetime from the third file.
lease = lmptr_->getLease4(IOAddress("192.0.2.12"));
ASSERT_TRUE(lease);
EXPECT_EQ(200, lease->cltt_);
}
+// This test checks that the backend reads DHCPv6 lease data from multiple
+// files.
+TEST_F(MemfileLeaseMgrTest, load6MultipleLeaseFiles) {
+ LeaseFileIO io2("leasefile6_0.csv.2");
+ io2.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
+ "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr\n"
+ "2001:db8:1::1,01:01:01:01:01:01:01:01:01:01:01:01:01,"
+ "200,200,8,100,0,7,0,1,1,,\n"
+ "2001:db8:1::2,02:02:02:02:02:02:02:02:02:02:02:02:02,"
+ "200,200,8,100,0,7,0,1,1,,\n");
+
+ LeaseFileIO io1("leasefile6_0.csv.1");
+ io1.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
+ "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr\n"
+ "2001:db8:1::3,03:03:03:03:03:03:03:03:03:03:03:03:03,"
+ "200,200,8,100,0,7,0,1,1,,\n"
+ "2001:db8:1::2,02:02:02:02:02:02:02:02:02:02:02:02:02,"
+ "300,800,8,100,0,7,0,1,1,,\n"
+ "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
+ "200,200,8,100,0,7,0,1,1,,\n");
+
+ LeaseFileIO io("leasefile6_0.csv");
+ io.writeFile("address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,"
+ "lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr\n"
+ "2001:db8:1::4,04:04:04:04:04:04:04:04:04:04:04:04:04,"
+ "400,1000,8,100,0,7,0,1,1,,\n"
+ "2001:db8:1::5,05:05:05:05:05:05:05:05:05:05:05:05:05,"
+ "200,200,8,100,0,7,0,1,1,,\n");
+
+ startBackend(V6);
+
+ // This lease only exists in the first file and the cltt should be 0.
+ Lease6Ptr lease = lmptr_->getLease6(Lease::TYPE_NA,
+ IOAddress("2001:db8:1::1"));
+ ASSERT_TRUE(lease);
+ EXPECT_EQ(0, lease->cltt_);
+
+ // This lease exists in the first and second file and the cltt should
+ // be calculated using the expiration time and the valid lifetime
+ // from the second file.
+ lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::2"));
+ ASSERT_TRUE(lease);
+ EXPECT_EQ(500, lease->cltt_);
+
+ // This lease only exists in the second file and the cltt should be 0.
+ lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::3"));
+ ASSERT_TRUE(lease);
+ EXPECT_EQ(0, lease->cltt_);
+
+ // This lease exists in the second and third file and the cltt should
+ // be calculated using the expiration time and the valid lifetime
+ // from the third file.
+ lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::4"));
+ ASSERT_TRUE(lease);
+ EXPECT_EQ(600, lease->cltt_);
+
+ // This lease only exists in the third file and the cltt should be 0.
+ lease = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::5"));
+ ASSERT_TRUE(lease);
+ EXPECT_EQ(0, lease->cltt_);
+}
+
}; // end of anonymous namespace
diff --git a/src/lib/util/csv_file.cc b/src/lib/util/csv_file.cc
index 7b29a38bf1..648d6ac3ba 100644
--- a/src/lib/util/csv_file.cc
+++ b/src/lib/util/csv_file.cc
@@ -1,4 +1,4 @@
-// Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
@@ -97,6 +97,17 @@ CSVFile::close() {
}
}
+bool
+CSVFile::exists() const {
+ std::ifstream fs(filename_);
+ if (fs.good()) {
+ fs.close();
+ return (true);
+ }
+ fs.close();
+ return (false);
+}
+
void
CSVFile::flush() const {
checkStreamStatusAndReset("flush");
diff --git a/src/lib/util/csv_file.h b/src/lib/util/csv_file.h
index 123948f8bd..2ac2bef05f 100644
--- a/src/lib/util/csv_file.h
+++ b/src/lib/util/csv_file.h
@@ -1,4 +1,4 @@
-// Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
@@ -320,6 +320,14 @@ public:
/// @brief Closes the CSV file.
void close();
+ /// @brief Checks if the CSV file exists.
+ ///
+ /// This method doesn't check if the existing file has a correct file
+ /// format.
+ ///
+ /// @return true if file exists, false otherwise.
+ bool exists() const;
+
/// @brief Flushes a file.
void flush() const;
diff --git a/src/lib/util/tests/csv_file_unittest.cc b/src/lib/util/tests/csv_file_unittest.cc
index 0a0b12d297..234a826e20 100644
--- a/src/lib/util/tests/csv_file_unittest.cc
+++ b/src/lib/util/tests/csv_file_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
@@ -454,5 +454,29 @@ TEST_F(CSVFileTest, validateHeader) {
EXPECT_THROW(csv->open(), CSVFileError);
}
+// This test checks that the exists method of the CSVFile class properly
+// checks that the file exists.
+TEST_F(CSVFileTest, exists) {
+ // Create a new CSV file that contains a header and two data rows.
+ writeFile("animal,age,color\n"
+ "cat,10,white\n"
+ "lion,15,yellow\n");
+
+ boost::scoped_ptr<CSVFile> csv(new CSVFile(testfile_));
+ // The CSVFile class should return true even if the file hasn't been
+ // opened.
+ EXPECT_TRUE(csv->exists());
+ // Now open the file and make sure it still returns true.
+ ASSERT_NO_THROW(csv->open());
+ EXPECT_TRUE(csv->exists());
+
+ // Close the file and remove it.
+ csv->close();
+ removeFile();
+
+ // The file should not exist.
+ EXPECT_FALSE(csv->exists());
+}
+
} // end of anonymous namespace