EXPECT_EQ(0, lease_mgr->getLFCCount());
}
- /// @todo Replace the following with the checks that the LFC has
- /// completed successfully, i.e. the leasefile4_0.csv.2 exists
- /// and it holds the cleaned up lease information.
-
- // Until the kea-lfc is implemented and performs the cleanup, we can
- // only check that the backend has moved the lease file to a lease
- // file with suffix ".1".
- LeaseFileIO input_file(getLeaseFilePath("leasefile4_0.csv.1"), false);
+// This test that the callback function executing the cleanup of the
+// DHCPv4 lease file works as expected.
+TEST_F(MemfileLeaseMgrTest, leaseFileCleanup4) {
+ // This string contains the lease file header, which matches
+ // the contents of the new file in which no leases have been
+ // stored.
+ std::string new_file_contents =
+ "address,hwaddr,client_id,valid_lifetime,expire,"
+ "subnet_id,fqdn_fwd,fqdn_rev,hostname\n";
+
+ // This string contains the contents of the lease file with exactly
+ // one lease, but two entries. One of the entries should be removed
+ // as a result of lease file cleanup.
+ std::string current_file_contents = new_file_contents +
+ "192.0.2.2,02:02:02:02:02:02,,200,200,8,1,1,,\n"
+ "192.0.2.2,02:02:02:02:02:02,,200,800,8,1,1,,\n";
+ LeaseFileIO current_file(getLeaseFilePath("leasefile4_0.csv"));
+ current_file.writeFile(current_file_contents);
+
+ std::string previous_file_contents = new_file_contents +
+ "192.0.2.3,03:03:03:03:03:03,,200,200,8,1,1,,\n"
+ "192.0.2.3,03:03:03:03:03:03,,200,800,8,1,1,,\n";
+ LeaseFileIO previous_file(getLeaseFilePath("leasefile4_0.csv.2"));
+ previous_file.writeFile(previous_file_contents);
+
+ // Create the backend.
+ LeaseMgr::ParameterMap pmap;
+ pmap["type"] = "memfile";
+ pmap["universe"] = "4";
+ pmap["name"] = getLeaseFilePath("leasefile4_0.csv");
+ pmap["lfc-interval"] = "1";
+ boost::scoped_ptr<NakedMemfileLeaseMgr> lease_mgr(new NakedMemfileLeaseMgr(pmap));
+
+ // Try to run the lease file cleanup.
+ ASSERT_NO_THROW(lease_mgr->lfcCallback());
+
+ // The new lease file should have been created and it should contain
+ // no leases.
+ ASSERT_TRUE(current_file.exists());
+ EXPECT_EQ(new_file_contents, current_file.readFile());
+
+ // Wait for the LFC process to complete.
+ ASSERT_TRUE(waitForProcess(*lease_mgr, 2));
+
+ // And make sure it has returned an exit status of 0.
+ EXPECT_EQ(0, lease_mgr->getLFCExitStatus())
+ << "Executing the LFC process failed: make sure that"
+ " the kea-lfc program has been compiled.";
+
+ // Check if we can still write to the lease file.
+ std::vector<uint8_t> hwaddr_vec(6);
+ HWAddrPtr hwaddr(new HWAddr(hwaddr_vec, HTYPE_ETHER));
+ Lease4Ptr new_lease(new Lease4(IOAddress("192.0.2.45"), hwaddr, 0, 0,
+ 100, 50, 60, 0, 1));
+ ASSERT_NO_THROW(lease_mgr->addLease(new_lease));
+
+ std::string updated_file_contents = new_file_contents +
+ "192.0.2.45,00:00:00:00:00:00,,100,100,1,0,0,\n";
+ EXPECT_EQ(updated_file_contents, current_file.readFile());
+
- // And this file should contain the contents of the original file.
- EXPECT_EQ(current_file_contents, input_file.readFile());
++ // This string contains the contents of the lease file we
++ // expect after the LFC run. It has two leases with one
++ // entry each.
++ std::string result_file_contents = new_file_contents +
++ "192.0.2.2,02:02:02:02:02:02,,200,800,8,1,1,\n"
++ "192.0.2.3,03:03:03:03:03:03,,200,800,8,1,1,\n";
++
++ // The LFC should have created a file with the two leases and moved it
++ // to leasefile4_0.csv.2
++ LeaseFileIO input_file(getLeaseFilePath("leasefile4_0.csv.2"), false);
+ ASSERT_TRUE(input_file.exists());
- /// @todo Replace the following with the checks that the LFC has
- /// completed successfully, i.e. the leasefile6_0.csv.2 exists
- /// and it holds the cleaned up lease information.
++ // And this file should contain the contents of the result file.
++ EXPECT_EQ(result_file_contents, input_file.readFile());
+}
+
+// This test that the callback function executing the cleanup of the
+// DHCPv6 lease file works as expected.
+TEST_F(MemfileLeaseMgrTest, leaseFileCleanup6) {
+ // This string contains the lease file header, which matches
+ // the contents of the new file in which no leases have been
+ // stored.
+ std::string new_file_contents =
+ "address,duid,valid_lifetime,expire,subnet_id,"
+ "pref_lifetime,lease_type,iaid,prefix_len,fqdn_fwd,"
+ "fqdn_rev,hostname,hwaddr\n";
+
+ // This string contains the contents of the lease file with exactly
+ // one lease, but two entries. One of the entries should be removed
+ // as a result of lease file cleanup.
+ std::string current_file_contents = new_file_contents +
+ "2001:db8:1::1,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,200,200,"
+ "8,100,0,7,0,1,1,,\n"
+ "2001:db8:1::1,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,200,800,"
+ "8,100,0,7,0,1,1,,\n";
+ LeaseFileIO current_file(getLeaseFilePath("leasefile6_0.csv"));
+ current_file.writeFile(current_file_contents);
+
+ std::string previous_file_contents = new_file_contents +
+ "2001:db8:1::2,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,01:01:01:01:01:01:01:01:01:01:01:01:01,200,800,"
+ "8,100,0,7,0,1,1,,\n";
+ LeaseFileIO previous_file(getLeaseFilePath("leasefile6_0.csv.2"));
+ previous_file.writeFile(previous_file_contents);
+
+ // Create the backend.
+ LeaseMgr::ParameterMap pmap;
+ pmap["type"] = "memfile";
+ pmap["universe"] = "6";
+ pmap["name"] = getLeaseFilePath("leasefile6_0.csv");
+ pmap["lfc-interval"] = "1";
+ boost::scoped_ptr<NakedMemfileLeaseMgr> lease_mgr(new NakedMemfileLeaseMgr(pmap));
+
+ // Try to run the lease file cleanup.
+ ASSERT_NO_THROW(lease_mgr->lfcCallback());
+
+ // The new lease file should have been created and it should contain
+ // no leases.
+ ASSERT_TRUE(current_file.exists());
+ EXPECT_EQ(new_file_contents, current_file.readFile());
+
+ // Wait for the LFC process to complete.
+ ASSERT_TRUE(waitForProcess(*lease_mgr, 2));
+
+ // And make sure it has returned an exit status of 0.
+ EXPECT_EQ(0, lease_mgr->getLFCExitStatus())
+ << "Executing the LFC process failed: make sure that"
+ " the kea-lfc program has been compiled.";
+
+
+ // Check if we can still write to the lease file.
+ std::vector<uint8_t> duid_vec(13);
+ DuidPtr duid(new DUID(duid_vec));
+ Lease6Ptr new_lease(new Lease6(Lease::TYPE_NA, IOAddress("3000::1"),duid,
+ 123, 300, 400, 100, 300, 2));
+ new_lease->cltt_ = 0;
+ ASSERT_NO_THROW(lease_mgr->addLease(new_lease));
+
+ std::string update_file_contents = new_file_contents +
+ "3000::1,00:00:00:00:00:00:00:00:00:00:00:00:00,400,"
+ "400,2,300,0,123,128,0,0,,\n";
+ EXPECT_EQ(update_file_contents, current_file.readFile());
+
- // Until the kea-lfc is implemented and performs the cleanup, we can
- // only check that the backend has moved the lease file to a lease
- // file with suffix ".1".
- LeaseFileIO input_file(getLeaseFilePath("leasefile6_0.csv.1"), false);
++ // This string contains the contents of the lease file we
++ // expect after the LFC run. It has two leases with one
++ // entry each.
++ std::string result_file_contents = new_file_contents +
++ "2001:db8:1::1,00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f,200,800,"
++ "8,100,0,7,0,1,1,,\n"
++ "2001:db8:1::2,01:01:01:01:01:01:01:01:01:01:01:01:01,200,800,"
++ "8,100,0,7,0,1,1,,\n";
+
- // And this file should contain the contents of the original file.
- EXPECT_EQ(current_file_contents, input_file.readFile());
++ // The LFC should have created a file with the two leases and moved it
++ // to leasefile6_0.csv.2
++ LeaseFileIO input_file(getLeaseFilePath("leasefile6_0.csv.2"), false);
+ ASSERT_TRUE(input_file.exists());
++ // And this file should contain the contents of the result file.
++ EXPECT_EQ(result_file_contents, input_file.readFile());
+}
+
+// This test verifies that EXIT_FAILURE status code is returned when
+// the LFC process fails to start.
+TEST_F(MemfileLeaseMgrTest, leaseFileCleanupStartFail) {
+ // This string contains the lease file header, which matches
+ // the contents of the new file in which no leases have been
+ // stored.
+ std::string new_file_contents =
+ "address,hwaddr,client_id,valid_lifetime,expire,"
+ "subnet_id,fqdn_fwd,fqdn_rev,hostname\n";
+
+ // Create the lease file to be used by the backend.
+ std::string current_file_contents = new_file_contents +
+ "192.0.2.2,02:02:02:02:02:02,,200,200,8,1,1,,\n";
+ LeaseFileIO current_file(getLeaseFilePath("leasefile4_0.csv"));
+ current_file.writeFile(current_file_contents);
+
+ // Specify invalid path to the kea-lfc executable. This should result
+ // in failure status code returned by the child process.
+ setenv("KEA_LFC_EXECUTABLE", "foobar", 1);
+
+ // Create the backend.
+ LeaseMgr::ParameterMap pmap;
+ pmap["type"] = "memfile";
+ pmap["universe"] = "4";
+ pmap["name"] = getLeaseFilePath("leasefile4_0.csv");
+ pmap["lfc-interval"] = "1";
+ boost::scoped_ptr<NakedMemfileLeaseMgr> lease_mgr(new NakedMemfileLeaseMgr(pmap));
+
+ // Try to run the lease file cleanup.
+ ASSERT_NO_THROW(lease_mgr->lfcCallback());
+
+ // Wait for the LFC process to complete.
+ ASSERT_TRUE(waitForProcess(*lease_mgr, 2));
+
+ // And make sure it has returned an error.
+ EXPECT_EQ(EXIT_FAILURE, lease_mgr->getLFCExitStatus())
+ << "Executing the LFC process failed: make sure that"
+ " the kea-lfc program has been compiled.";
+}
+
// Test that the backend returns a correct value of the interval
// at which the IOService must be executed to run the handlers
// for the installed timers.