Skip to content

Commit

Permalink
test: add unit testing for policy factory
Browse files Browse the repository at this point in the history
Problem: the policy factory in the resource module has no
unit testing.

Solution: add a series of unit tests to validate the parsing
and selection of policy options in the factory.
  • Loading branch information
wihobbs committed Jan 22, 2025
1 parent 11294e3 commit 2efc97e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
6 changes: 6 additions & 0 deletions resource/policies/base/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
add_executable(matcher_policy_factory_test
${CMAKE_CURRENT_SOURCE_DIR}/matcher_policy_factory_test02.cpp
)
target_link_libraries(matcher_policy_factory_test PRIVATE libtap resource)
add_sanitizers(matcher_policy_factory_test)
flux_add_test(NAME matcher_policy_factory_test COMMAND matcher_policy_factory_test)
add_executable(matcher_util_api_test
${CMAKE_CURRENT_SOURCE_DIR}/matcher_util_api_test01.cpp
)
Expand Down
70 changes: 70 additions & 0 deletions resource/policies/base/test/matcher_policy_factory_test02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*****************************************************************************\
* Copyright 2024 Lawrence Livermore National Security, LLC
* (c.f. AUTHORS, NOTICE.LLNS, LICENSE)
*
* This file is part of the Flux resource manager framework.
* For details, see https://github.com/flux-framework.
*
* SPDX-License-Identifier: LGPL-3.0
\*****************************************************************************/

/*
* Test for the dfu_match_policy class(es). Compares shared pointers
* expected values to string inputs to these classes. Strings can be
* specified in config for custom policies, or some are provided.
*/

extern "C" {
#if HAVE_CONFIG_H
#include <config.h>
#endif
}

#include <string>
#include <boost/lexical_cast.hpp>
#include "resource/policies/dfu_match_policy_factory.hpp"
#include "src/common/libtap/tap.h"

using namespace Flux;
using namespace Flux::resource_model;

int test_parsers ()
{
std::map<std::string, bool> container;
bool first = Flux::resource_model::parse_custom_match_policy("high=true node_centric=true node_exclusive=true stop_on_1_matches=false blah=4", container);
ok (first == false, "blah is an unrecognized policy option");

std::map<std::string, bool> container2;
bool second = Flux::resource_model::parse_custom_match_policy("high=1 node_centric=true node_exclusive=true stop_on_1_matches=false", container2);
ok (second == false, "1 is an invalid option, must be true or false");

std::map<std::string, bool> container3;
bool third = Flux::resource_model::parse_custom_match_policy("high=true node_centric=true stop_on_1_matches=true", container3);
ok (third == true, "first is a valid policy");

bool fourth = Flux::resource_model::parse_bool_match_options ("high", container3);
ok (fourth == true, "policy first uses option high");

bool fifth = Flux::resource_model::parse_bool_match_options ("node_centric", container3);
ok (fifth == true, "policy first uses option node_centric");

bool sixth = Flux::resource_model::parse_bool_match_options ("stop_on_1_matches", container3);
ok (sixth == true, "policy first uses option stop_on_1_matches");

bool seventh = Flux::resource_model::parse_bool_match_options ("low", container3);
ok (seventh == false, "policy first does not use option low");

return 0;
}

int main (int argc, char *argv[])
{
plan (7);
test_parsers ();
done_testing ();
return 0;
}

/*
* vi: ts=4 sw=4 expandtab
*/

0 comments on commit 2efc97e

Please sign in to comment.