mirror of
https://github.com/falcosecurity/falco.git
synced 2026-03-18 18:58:41 +00:00
Squash w Initial framework for unit tests of action manager.
This commit is contained in:
@@ -16,18 +16,23 @@ limitations under the License.
|
||||
|
||||
#include "app_action_manager.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
// Test actions just record the order they were run (or skipped)
|
||||
class test_action : public falco::app::runnable_action {
|
||||
public:
|
||||
|
||||
static std::list<std::string> s_actions_run;
|
||||
static std::vector<std::string> s_actions_run;
|
||||
|
||||
test_action(const std::string &name,
|
||||
const std::string &group,
|
||||
const std::list<std::string> &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<std::string> &prerequsites()
|
||||
{
|
||||
return m_prerequsites;
|
||||
@@ -55,16 +65,19 @@ public:
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
std::string m_group;
|
||||
std::list<std::string> m_prerequsites;
|
||||
run_result m_res;
|
||||
};
|
||||
|
||||
std::list<std::string> test_action::s_actions_run;
|
||||
std::vector<std::string> test_action::s_actions_run;
|
||||
|
||||
static std::list<std::string> empty;
|
||||
static std::list<std::string> prereq_a = {"a"};
|
||||
static std::list<std::string> prereq_aa = {"aa"};
|
||||
static std::list<std::string> prereq_ab = {"ab"};
|
||||
static std::list<std::string> prereq_m = {"m"};
|
||||
static std::list<std::string> 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<test_action> a = std::make_shared<test_action>(std::string("a"),
|
||||
std::string("init"),
|
||||
empty,
|
||||
success_proceed);
|
||||
|
||||
static std::shared_ptr<test_action> a_noproceed = std::make_shared<test_action>(std::string("a"),
|
||||
std::string("init"),
|
||||
empty,
|
||||
success_noproceed);
|
||||
|
||||
static std::shared_ptr<test_action> b = std::make_shared<test_action>(std::string("b"),
|
||||
std::string("init"),
|
||||
empty,
|
||||
success_proceed);
|
||||
|
||||
static std::shared_ptr<test_action> c = std::make_shared<test_action>(std::string("c"),
|
||||
std::string("init"),
|
||||
empty,
|
||||
success_proceed);
|
||||
|
||||
static std::shared_ptr<test_action> d = std::make_shared<test_action>(std::string("d"),
|
||||
std::string("init"),
|
||||
empty,
|
||||
success_proceed);
|
||||
|
||||
std::shared_ptr<test_action> aa = std::make_shared<test_action>(std::string("aa"),
|
||||
std::string("init"),
|
||||
prereq_a,
|
||||
success_proceed);
|
||||
|
||||
std::shared_ptr<test_action> ab = std::make_shared<test_action>(std::string("ab"),
|
||||
std::string("init"),
|
||||
prereq_a,
|
||||
success_proceed);
|
||||
|
||||
std::shared_ptr<test_action> aa_noproceed = std::make_shared<test_action>(std::string("aa"),
|
||||
std::string("init"),
|
||||
prereq_a,
|
||||
success_noproceed);
|
||||
|
||||
std::shared_ptr<test_action> aaa = std::make_shared<test_action>(std::string("aaa"),
|
||||
std::string("init"),
|
||||
prereq_aa,
|
||||
success_proceed);
|
||||
|
||||
std::shared_ptr<test_action> aab = std::make_shared<test_action>(std::string("aab"),
|
||||
std::string("init"),
|
||||
prereq_aa,
|
||||
success_proceed);
|
||||
|
||||
std::shared_ptr<test_action> aba = std::make_shared<test_action>(std::string("aba"),
|
||||
std::string("init"),
|
||||
prereq_ab,
|
||||
success_proceed);
|
||||
|
||||
static std::list<std::string>::iterator find_action(const std::string &name,
|
||||
std::list<std::string>::iterator begin = test_action::s_actions_run.begin())
|
||||
static std::shared_ptr<test_action> m = std::make_shared<test_action>(std::string("m"),
|
||||
std::string("run"),
|
||||
empty,
|
||||
success_proceed);
|
||||
|
||||
static std::shared_ptr<test_action> ma = std::make_shared<test_action>(std::string("ma"),
|
||||
std::string("run"),
|
||||
prereq_m,
|
||||
success_proceed);
|
||||
|
||||
static std::shared_ptr<test_action> n = std::make_shared<test_action>(std::string("n"),
|
||||
std::string("run"),
|
||||
empty,
|
||||
success_proceed);
|
||||
|
||||
static std::shared_ptr<test_action> na = std::make_shared<test_action>(std::string("na"),
|
||||
std::string("run"),
|
||||
prereq_n,
|
||||
success_proceed);
|
||||
|
||||
static std::vector<std::string>::iterator find_action(const std::string &name,
|
||||
std::vector<std::string>::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<std::string>::iterator find_action(const std::string &name,
|
||||
}
|
||||
|
||||
static bool action_is_found(const std::string &name,
|
||||
std::list<std::string>::iterator begin = test_action::s_actions_run.begin())
|
||||
std::vector<std::string>::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<std::string> 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<std::string> exp_actions_run = {"a", "aa"};
|
||||
std::vector<std::string> 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<std::string> exp_actions_run = {"a"};
|
||||
std::vector<std::string> 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<std::string> exp_actions_run = {"a", "aa"};
|
||||
std::vector<std::string> 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<std::string> exp_actions_run = {"a"};
|
||||
std::vector<std::string> 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<std::string> exp_actions_run = {"a", "aa"};
|
||||
std::vector<std::string> 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<std::string> 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<std::string>::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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user