From 4adf4e9603fb479da74e123c9b161c11b86b2315 Mon Sep 17 00:00:00 2001 From: Mark Stemm Date: Thu, 3 Mar 2022 17:45:32 -0800 Subject: [PATCH] Squash w Initial framework for unit tests of action manager. --- tests/falco/test_actions.cpp | 127 ++++++++++++++++++++++++++++++++--- 1 file changed, 116 insertions(+), 11 deletions(-) diff --git a/tests/falco/test_actions.cpp b/tests/falco/test_actions.cpp index b00e82c3..41408ad8 100644 --- a/tests/falco/test_actions.cpp +++ b/tests/falco/test_actions.cpp @@ -16,18 +16,23 @@ limitations under the License. #include "app_action_manager.h" +#include +#include + #include // Test actions just record the order they were run (or skipped) class test_action : public falco::app::runnable_action { public: - static std::list s_actions_run; + static std::vector s_actions_run; test_action(const std::string &name, + const std::string &group, const std::list &prerequsites, run_result res) : m_name(name), + m_group(group), m_prerequsites(prerequsites), m_res(res) { @@ -42,6 +47,11 @@ public: return m_name; } + const std::string &group() + { + return m_group; + } + const std::list &prerequsites() { return m_prerequsites; @@ -55,16 +65,19 @@ public: private: std::string m_name; + std::string m_group; std::list m_prerequsites; run_result m_res; }; -std::list test_action::s_actions_run; +std::vector test_action::s_actions_run; static std::list empty; static std::list prereq_a = {"a"}; static std::list prereq_aa = {"aa"}; static std::list prereq_ab = {"ab"}; +static std::list prereq_m = {"m"}; +static std::list prereq_n = {"n"}; // The action names denote the dependency order e.g. "a", "b", "c" are // all independent, "aa" and "ab" depend on a but are independent of @@ -76,51 +89,82 @@ static falco::app::runnable_action::run_result success_noproceed{true, "", false static std::shared_ptr a = std::make_shared(std::string("a"), + std::string("init"), empty, success_proceed); static std::shared_ptr a_noproceed = std::make_shared(std::string("a"), + std::string("init"), empty, success_noproceed); static std::shared_ptr b = std::make_shared(std::string("b"), + std::string("init"), empty, success_proceed); static std::shared_ptr c = std::make_shared(std::string("c"), + std::string("init"), empty, success_proceed); static std::shared_ptr d = std::make_shared(std::string("d"), + std::string("init"), empty, success_proceed); std::shared_ptr aa = std::make_shared(std::string("aa"), + std::string("init"), prereq_a, success_proceed); std::shared_ptr ab = std::make_shared(std::string("ab"), + std::string("init"), prereq_a, success_proceed); std::shared_ptr aa_noproceed = std::make_shared(std::string("aa"), + std::string("init"), prereq_a, success_noproceed); std::shared_ptr aaa = std::make_shared(std::string("aaa"), + std::string("init"), prereq_aa, success_proceed); std::shared_ptr aab = std::make_shared(std::string("aab"), + std::string("init"), prereq_aa, success_proceed); std::shared_ptr aba = std::make_shared(std::string("aba"), + std::string("init"), prereq_ab, success_proceed); -static std::list::iterator find_action(const std::string &name, - std::list::iterator begin = test_action::s_actions_run.begin()) +static std::shared_ptr m = std::make_shared(std::string("m"), + std::string("run"), + empty, + success_proceed); + +static std::shared_ptr ma = std::make_shared(std::string("ma"), + std::string("run"), + prereq_m, + success_proceed); + +static std::shared_ptr n = std::make_shared(std::string("n"), + std::string("run"), + empty, + success_proceed); + +static std::shared_ptr na = std::make_shared(std::string("na"), + std::string("run"), + prereq_n, + success_proceed); + +static std::vector::iterator find_action(const std::string &name, + std::vector::iterator begin = test_action::s_actions_run.begin()) { return std::find(begin, test_action::s_actions_run.end(), @@ -128,7 +172,7 @@ static std::list::iterator find_action(const std::string &name, } static bool action_is_found(const std::string &name, - std::list::iterator begin = test_action::s_actions_run.begin()) + std::vector::iterator begin = test_action::s_actions_run.begin()) { auto it = find_action(name, begin); @@ -137,10 +181,13 @@ static bool action_is_found(const std::string &name, TEST_CASE("action manager can add and run actions", "[actions]") { - falco::app::action_manager amgr; + std::list groups = {"init", "run"}; SECTION("Two independent") { + falco::app::action_manager amgr; + amgr.set_groups(groups); + test_action::s_actions_run.clear(); amgr.add(a); @@ -155,6 +202,9 @@ TEST_CASE("action manager can add and run actions", "[actions]") SECTION("Two dependent") { + falco::app::action_manager amgr; + amgr.set_groups(groups); + test_action::s_actions_run.clear(); amgr.add(a); @@ -162,12 +212,15 @@ TEST_CASE("action manager can add and run actions", "[actions]") amgr.run(); - std::list exp_actions_run = {"a", "aa"}; + std::vector exp_actions_run = {"a", "aa"}; REQUIRE(test_action::s_actions_run == exp_actions_run); } SECTION("One independent, two dependent") { + falco::app::action_manager amgr; + amgr.set_groups(groups); + test_action::s_actions_run.clear(); amgr.add(a); @@ -188,6 +241,9 @@ TEST_CASE("action manager can add and run actions", "[actions]") SECTION("Two dependent, first does not proceed") { + falco::app::action_manager amgr; + amgr.set_groups(groups); + test_action::s_actions_run.clear(); amgr.add(a_noproceed); @@ -195,12 +251,15 @@ TEST_CASE("action manager can add and run actions", "[actions]") amgr.run(); - std::list exp_actions_run = {"a"}; + std::vector exp_actions_run = {"a"}; REQUIRE(test_action::s_actions_run == exp_actions_run); } SECTION("Two dependent, second does not proceed") { + falco::app::action_manager amgr; + amgr.set_groups(groups); + test_action::s_actions_run.clear(); amgr.add(a); @@ -208,12 +267,15 @@ TEST_CASE("action manager can add and run actions", "[actions]") amgr.run(); - std::list exp_actions_run = {"a", "aa"}; + std::vector exp_actions_run = {"a", "aa"}; REQUIRE(test_action::s_actions_run == exp_actions_run); } SECTION("Three dependent, first does not proceed") { + falco::app::action_manager amgr; + amgr.set_groups(groups); + test_action::s_actions_run.clear(); amgr.add(a_noproceed); @@ -222,12 +284,15 @@ TEST_CASE("action manager can add and run actions", "[actions]") amgr.run(); - std::list exp_actions_run = {"a"}; + std::vector exp_actions_run = {"a"}; REQUIRE(test_action::s_actions_run == exp_actions_run); } SECTION("Three dependent, second does not proceed") { + falco::app::action_manager amgr; + amgr.set_groups(groups); + test_action::s_actions_run.clear(); amgr.add(a); @@ -236,12 +301,33 @@ TEST_CASE("action manager can add and run actions", "[actions]") amgr.run(); - std::list exp_actions_run = {"a", "aa"}; + std::vector exp_actions_run = {"a", "aa"}; + REQUIRE(test_action::s_actions_run == exp_actions_run); + } + + SECTION("Groups") + { + falco::app::action_manager amgr; + amgr.set_groups(groups); + + test_action::s_actions_run.clear(); + + amgr.add(ma); + amgr.add(m); + amgr.add(aa); + amgr.add(a); + + amgr.run(); + + std::vector exp_actions_run = {"a", "aa", "m", "ma"}; REQUIRE(test_action::s_actions_run == exp_actions_run); } SECTION("Complex") { + falco::app::action_manager amgr; + amgr.set_groups(groups); + test_action::s_actions_run.clear(); amgr.add(a); @@ -253,6 +339,10 @@ TEST_CASE("action manager can add and run actions", "[actions]") amgr.add(aaa); amgr.add(aab); amgr.add(aba); + amgr.add(m); + amgr.add(ma); + amgr.add(n); + amgr.add(na); amgr.run(); @@ -275,6 +365,21 @@ TEST_CASE("action manager can add and run actions", "[actions]") // aba must be after ab it = find_action(ab->name()); REQUIRE(action_is_found(aba->name(), it) == true); + + // The run actions must be the last four + std::vector::iterator last_four = test_action::s_actions_run.end() - 4; + REQUIRE(action_is_found(m->name(), last_four) == true); + REQUIRE(action_is_found(ma->name(), last_four) == true); + REQUIRE(action_is_found(n->name(), last_four) == true); + REQUIRE(action_is_found(na->name(), last_four) == true); + + // ma must be after m + it = find_action(m->name()); + REQUIRE(action_is_found(ma->name(), it) == true); + + // na must be after n + it = find_action(n->name()); + REQUIRE(action_is_found(na->name(), it) == true); } }