Commit ca432aa5 authored by Shawn Routhier's avatar Shawn Routhier
Browse files

[trac3665] More tests and cleanup

Add more tests to verity the cleanup of leases files

Cleanup the code for comments, typos, spaces and the like.
parent 3e0491cb
......@@ -22,8 +22,6 @@
#include <dhcpsrv/lease_file_loader.h>
#include <config.h>
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <sstream>
#include <unistd.h>
......@@ -44,7 +42,7 @@ const char* LFCController::lfc_app_name_ = "DhcpLFC";
/// @brief Defines the executable name.
const char* LFCController::lfc_bin_name_ = "kea-lfc";
/// @brief Maximum number of errors to read the leases from the lease file.
/// @brief Maximum number of errors to allow when reading leases from the file.
const uint32_t MAX_LEASE_ERRORS = 100;
LFCController::LFCController()
......@@ -57,6 +55,8 @@ LFCController::~LFCController() {
void
LFCController::launch(int argc, char* argv[]) {
bool do_clean = true;
try {
parseArgs(argc, argv);
} catch (const InvalidUsage& ex) {
......@@ -64,6 +64,7 @@ LFCController::launch(int argc, char* argv[]) {
throw; // rethrow it
}
if (verbose_ == true)
std::cerr << "Starting lease file cleanup" << std::endl;
// verify we are the only instance
......@@ -88,36 +89,46 @@ LFCController::launch(int argc, char* argv[]) {
return;
}
// do other work (TBD)
// If we don't have a finish file do the processing
if (access(finish_file_.c_str(), F_OK) == -1) {
if (verbose_ == true)
std::cerr << "LFC Processing files" << std::endl;
try {
if (protocol_version_ == 4) {
processLeases<Lease4, CSVLeaseFile4, Lease4Storage>();
} else {
processLeases<Lease6, CSVLeaseFile6, Lease6Storage>();
}
} catch (const isc::Exception& proc_ex) {
// We don't want to do the cleanup but do want to get rid of the pid
do_clean = false;
std::cerr << "Processing failed: " << proc_ex.what() << std::endl;
}
}
// We either already had a finish file or just created one, do the
// file cleanup, we don't want to return after the catch as we
// If do_clean is true We either already had a finish file or
// were able to create one. We now want to do the file cleanup,
// we don't want to return after the catch as we
// still need to cleanup the pid file
try {
if (do_clean == true) {
if (verbose_ == true)
std::cerr << "LFC cleaning files" << std::endl;
try {
fileCleanup();
} catch (const RunTimeFail& run_ex) {
std::cerr << run_ex.what() << std::endl;
}
}
// delete the pid file for this instance
try {
pid_file.deleteFile();
} catch (const PIDFileError& pid_ex) {
std::cerr << pid_ex.what() << std::endl;
return;
}
if (verbose_ == true)
std::cerr << "LFC complete" << std::endl;
}
......@@ -262,7 +273,8 @@ LFCController::parseArgs(int argc, char* argv[]) {
<< "Output lease file: " << output_file_ << std::endl
<< "Finish file: " << finish_file_ << std::endl
<< "Config file: " << config_file_ << std::endl
<< "PID file: " << pid_file_ << std::endl;
<< "PID file: " << pid_file_ << std::endl
<< std::endl;
}
}
......@@ -303,26 +315,26 @@ LFCController::getVersion(const bool extended) const{
template<typename LeaseObjectType, typename LeaseFileType, typename StorageType>
void
LFCController::processLeases() const {
LeaseFileType lfPrev(previous_file_.c_str());
LeaseFileType lfCopy(copy_file_.c_str());
LeaseFileType lfOutput(output_file_.c_str());
LeaseFileType lf_prev(previous_file_.c_str());
LeaseFileType lf_copy(copy_file_.c_str());
LeaseFileType lf_output(output_file_.c_str());
StorageType storage;
storage.clear();
// If a previous file exists read the entries into storage
if (lfPrev.exists()) {
LeaseFileLoader::load<LeaseObjectType>(lfPrev, storage,
if (lf_prev.exists()) {
LeaseFileLoader::load<LeaseObjectType>(lf_prev, storage,
MAX_LEASE_ERRORS);
}
// Follow that with the copy of the current lease file
if (lfCopy.exists()) {
LeaseFileLoader::load<LeaseObjectType>(lfCopy, storage,
if (lf_copy.exists()) {
LeaseFileLoader::load<LeaseObjectType>(lf_copy, storage,
MAX_LEASE_ERRORS);
}
// Write the result out to the output file
LeaseFileLoader::write<LeaseObjectType>(lfOutput, storage);
LeaseFileLoader::write<LeaseObjectType>(lf_output, storage);
// Once we've finished the output file move it to the complete file
if (rename(output_file_.c_str(), finish_file_.c_str()) != 0)
......
......@@ -41,10 +41,6 @@ public:
/// manage the command line, check for already running instances,
/// invoke the code to process the lease files and finally to rename
/// the lease files as necessary.
///
/// @todo The current code simply processes the command line we still need to
/// -# invoke the code to read, process and write the lease files
/// -# rename and delete the shell files as required
class LFCController {
public:
/// @brief Defines the application name, it may be used to locate
......@@ -67,10 +63,10 @@ public:
/// -# parse command line arguments
/// -# verify that it is the only instance
/// -# create pid file
/// -# read leases files TBD
/// -# write lease file TBD
/// -# move leases files TBD
/// -# cleanup artifacts TBD
/// -# read leases files
/// -# write lease file
/// -# move leases files
/// -# cleanup artifacts
/// -# remove pid file
/// -# exit to the caller
///
......@@ -158,6 +154,8 @@ public:
/// @brief Process files. Read in the leases from any previous & copy
/// files we have and write the results out to the output file. Upon
/// completion of the write move the file to the finish file.
///
/// @throw RunTimeFail if we can't move the file.
template<typename LeaseObjectType, typename LeaseFileType, typename StorageType>
void processLeases() const;
......@@ -166,7 +164,7 @@ public:
/// delete the work files (previous & copy) and move the finish file
/// to be the new previous file.
///
/// @throw RunTimeFail if the command line parameters are invalid.
/// @throw RunTimeFail if we can't manipulate the files.
void fileCleanup() const;
//@}
......
......@@ -161,20 +161,20 @@ public:
}
}
/// @brief Write leaes from the storage into a lease file
/// @brief Write leases from the storage into a lease file
///
/// This method iterates over the @c Lease4 or @c Lease6 object in the
/// storage specified in the arguments and writes them to the file
/// specified in the arguments.
///
/// This method writes all entries in the storege to the file, it does
/// not perform any checks for expriation or duplication.
/// This method writes all entries in the storage to the file, it does
/// not perform any checks for expiration or duplication.
///
/// @param lease_file A reference to the @c CSVLeaseFile4 or
/// @c CSVLeaseFile6 object representing the lease file. The file
/// doesn't need to be open because the method re-opens the file.
/// @param storage A reference to the container from which leases
/// should be written..
/// should be written.
/// @tparam LeasePtrType A @c Lease4 or @c Lease6.
/// @tparam LeaseFileType A @c CSVLeaseFile4 or @c CSVLeaseFile6.
/// @tparam StorageType A @c Lease4Storage or @c Lease6Storage.
......
......@@ -82,12 +82,12 @@ public:
/// @brief Tests the write function.
///
/// This method writes the leases from the storage container to the lease file
/// then compares the output to the string provided in the aguments to verify
/// the write was correct. The order of the leases in the output will dpend
/// then compares the output to the string provided in the arguments to verify
/// the write was correct. The order of the leases in the output will depend
/// on the order in which the container provides the leases.
///
/// @param storage A reference to the container to be written to the file
/// @param compStr The string to compare to what was read from the file
/// @param compare The string to compare to what was read from the file
///
/// @tparam LeaseStorage Type of the container: @c Lease4Container
/// @c Lease6Container.
......@@ -131,6 +131,9 @@ LeaseFileLoaderTest::absolutePath(const std::string& filename) {
// This test verifies that the DHCPv4 leases can be loaded from the lease
// file and that only the most recent entry for each lease is loaded and
// the previous entries are discarded.
//
// It also tests the write function by writing the storage to a file
// and comparing that with the expected value.
TEST_F(LeaseFileLoaderTest, load4) {
// Create lease file with leases for 192.0.2.1, 192.0.3.15. The lease
// entry for the 192.0.2.3 is invalid (lacks HW address) and should
......@@ -188,6 +191,9 @@ TEST_F(LeaseFileLoaderTest, load4) {
// This test verifies that the lease with a valid lifetime of 0
// is removed from the storage. The valid lifetime of 0 is set
// for the released leases.
//
// It also tests the write function by writing the storage to a file
// and comparing that with the expected value.
TEST_F(LeaseFileLoaderTest, load4LeaseRemove) {
// Create lease file in which one of the entries for 192.0.2.1
// has a valid_lifetime of 0 and results in the deletion of the
......@@ -228,6 +234,9 @@ TEST_F(LeaseFileLoaderTest, load4LeaseRemove) {
// This test verifies that the DHCPv6 leases can be loaded from the lease
// file and that only the most recent entry for each lease is loaded and
// the previous entries are discarded.
//
// It also tests the write function by writing the storage to a file
// and comparing that with the expected value.
TEST_F(LeaseFileLoaderTest, load6) {
// Create a lease file with three valid leases: 2001:db8:1::1,
// 3000:1:: and 2001:db8:2::10.
......@@ -291,6 +300,9 @@ TEST_F(LeaseFileLoaderTest, load6) {
// This test verifies that the lease with a valid lifetime of 0
// is removed from the storage. The valid lifetime of 0 set set
// for the released leases.
//
// It also tests the write function by writing the storage to a file
// and comparing that with the expected value.
TEST_F(LeaseFileLoaderTest, load6LeaseRemove) {
// Create lease file in which one of the entries for the 2001:db8:1::1
// has valid lifetime set to 0, in which case the lease should be
......@@ -380,6 +392,9 @@ TEST_F(LeaseFileLoaderTest, loadMaxErrors) {
// This test verifies that the lease with a valid lifetime set to 0 is
// not loaded if there are no previous entries for this lease in the
// lease file.
//
// It also tests the write function by writing the storage to a file
// and comparing that with the expected value.
TEST_F(LeaseFileLoaderTest, loadLeaseWithZeroLifetime) {
// Create lease file. The second lease has a valid lifetime of 0.
io_.writeFile("address,hwaddr,client_id,valid_lifetime,expire,subnet_id,"
......
......@@ -97,4 +97,3 @@ PIDFile::deleteFile() const {
} // namespace isc::util
} // namespace isc
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment