VirtualTable<Policy<int>> table;
EXPECT_TRUE(table.size() > 0);
- auto row = table.filter(&Policy<int>::name, "sample-int-policy");
+ auto row = table.filter(&Policy<int>::name, "sample_int_policy");
auto value = row[&Policy<int>::value];
EXPECT_TRUE(value > 0);
- EXPECT_EQ(row[&Policy<int>::name], "sample-int-policy");
+ EXPECT_EQ(row[&Policy<int>::name], "sample_int_policy");
}
TEST(VirtualTableTests, policy_str_table)
VirtualTable<Policy<std::string>> table;
EXPECT_TRUE(table.size() > 0);
- auto row = table.filter(&Policy<std::string>::name, "sample-str-policy");
+ auto row = table.filter(&Policy<std::string>::name, "sample_str_policy");
auto value = row[&Policy<std::string>::value];
EXPECT_TRUE(!value.empty());
- EXPECT_EQ(row[&Policy<std::string>::name], "sample-str-policy");
+ EXPECT_EQ(row[&Policy<std::string>::name], "sample_str_policy");
}
ADD_VIST_POLICY_LIBRARY(vist_policy_core api.cpp
policy-manager.cpp
- policy-loader.cpp
policy-storage.cpp)
FILE(GLOB POLICY_CORE_TESTS "tests/*.cpp")
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#include "policy-loader.hpp"
-
-namespace vist {
-namespace policy {
-
-PolicyProvider* PolicyLoader::load(const std::string& path)
-{
- PluginLoader loader(path);
- PolicyProvider::FactoryType factory = nullptr;
- loader.load(PolicyProvider::getFactoryName(), factory);
- if (factory == nullptr)
- THROW(ErrCode::RuntimeError) << "Failed to load factory: "
- << PolicyProvider::getFactoryName();
-
- auto provider = (*factory)();
- if (provider == nullptr)
- THROW(ErrCode::RuntimeError) << "Failed to make provider: "
- << PolicyProvider::getFactoryName();
-
- return provider;
-}
-
-PluginLoader::PluginLoader(const std::string& path, int flag)
- : handle(::dlopen(path.c_str(), flag), [](void*)->int{return 0;})
- // Cleaning object after dlclose() makes SEGFAULT.
- // TODO: Sync dynamic loading's life-cycle with program.(PluginManager)
- // : handle(::dlopen(path.c_str(), flag), ::dlclose)
-{
- if (handle == nullptr)
- THROW(ErrCode::LogicError) << "Failed to open: " << path;
-}
-
-} // namespace policy
-} // namespace vist
+++ /dev/null
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License
- */
-
-#pragma once
-
-#include <vist/sdk/policy-provider.hpp>
-
-#include <vist/exception.hpp>
-
-#include <memory>
-#include <string>
-
-#include <dlfcn.h>
-
-namespace vist {
-namespace policy {
-
-struct PolicyLoader final {
- static PolicyProvider* load(const std::string& path);
-};
-
-class PluginLoader final {
-public:
- explicit PluginLoader(const std::string& path, int flag = RTLD_LAZY);
-
- template<typename T>
- void load(const std::string& name, T& symbol);
-
-private:
- using Handle = std::unique_ptr<void, int(*)(void*)>;
- Handle handle;
-};
-
-template<typename T>
-void PluginLoader::load(const std::string& name, T& symbol)
-{
- symbol = reinterpret_cast<T>(::dlsym(handle.get(), name.c_str()));
- if (symbol == nullptr)
- THROW(ErrCode::RuntimeError) << "Failed to load: " << name;
-}
-
-} // namespace policy
-} // namespace vist
*/
#include "policy-manager.hpp"
-#include "policy-loader.hpp"
#include <vist/exception.hpp>
#include <vist/logger.hpp>
PolicyManager::PolicyManager() : storage(DB_PATH)
{
- loadProviders(PLUGIN_INSTALL_DIR);
- int cnt = loadPolicies();
- INFO(VIST) << std::to_string(cnt) << "-policies loaded";
-}
-
-std::pair<int, int> PolicyManager::loadProviders(const std::string& path)
-{
- INFO(VIST) << "Load policies from :" << path;
- using namespace boost::filesystem;
- if (!is_directory(path))
- THROW(ErrCode::LogicError) << "Plugin directory is wrong.: " << path;
-
- int passed = 0, failed = 0;
- for (directory_entry& entry : directory_iterator(path)) {
- if (!is_regular_file(entry.path().string()))
- continue;
-
- try {
- auto provider = PolicyLoader::load(entry.path().string());
- DEBUG(VIST) << "Loaded provider: " << provider->getName();
-
- bool exist = false;
- for (const auto& p : this->providers) {
- if (p->getName() == provider->getName()) {
- exist = true;
- break;
- }
- }
-
- if (!exist)
- this->providers.emplace_back(std::move(provider));
- } catch (const std::exception& e) {
- ++failed;
- ERROR(VIST) << "Failed to load: " << entry.path().string() << e.what();
- continue;
- }
-
- ++passed;
- }
-
- INFO(VIST) << "Loaded result >> passed: " << passed << ", failed: " << failed;
- return std::make_pair(passed, failed);
}
void PolicyManager::addProvider(std::shared_ptr<PolicyProvider>&& provider)
this->providers.emplace_back(std::move(provider));
}
-int PolicyManager::loadPolicies()
-{
- /// Make policy-provider map for performance
- for (const auto& provider : providers) {
- for (const auto& pair : provider->policies) {
- std::string policy = pair.first;
- this->policies[policy] = provider->getName();
-
- /// Check the policy is defined on policy-storage
- if (!storage.exists(pair.first)) {
- storage.define(pair.first, pair.second->getInitial());
- }
- }
- }
-
- return this->policies.size();
-}
-
void PolicyManager::enroll(const std::string& admin)
{
this->storage.enroll(admin);
explicit PolicyManager();
~PolicyManager() = default;
- std::pair<int, int> loadProviders(const std::string& path);
- int loadPolicies();
-
PolicyStorage storage;
std::vector<std::shared_ptr<PolicyProvider>> providers;
/// Policy-Provider
std::unordered_map<std::string, std::string> policies;
- FRIEND_TEST(PolicyCoreTests, policy_loader);
FRIEND_TEST(PolicyCoreTests, policy_get_policy);
};
/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <gtest/gtest.h>
#include <vist/policy/policy-manager.hpp>
+#include <vist/service/vistd.hpp>
namespace vist {
namespace policy {
-TEST(PolicyCoreTests, policy_loader)
-{
- auto& manager = PolicyManager::Instance();
+namespace test{
- EXPECT_TRUE(manager.providers.size() > 0);
- EXPECT_TRUE(manager.policies.size() > 0);
-}
+ void init()
+ {
+ // Policies are loaded via dynamic table when actual daemon starts.
+ static_cast<void>(Vistd::Instance());
+ }
+
+} // anonymous namespace
TEST(PolicyCoreTests, policy_set_get_int)
{
+ test::init();
+
auto& manager = PolicyManager::Instance();
manager.enroll("testAdmin");
manager.set("sample_int_policy", PolicyValue(5), "testAdmin");
TEST(PolicyCoreTests, policy_set_get_str)
{
+ test::init();
+
auto& manager = PolicyManager::Instance();
manager.enroll("testAdmin");
- manager.set("sample-str-policy", PolicyValue("AAA"), "testAdmin");
+ manager.set("sample_str_policy", PolicyValue("AAA"), "testAdmin");
- auto policy = manager.get("sample-str-policy");
+ auto policy = manager.get("sample_str_policy");
EXPECT_EQ(static_cast<std::string>(policy), "AAA");
manager.enroll("testAdmin1");
- manager.set("sample-str-policy", PolicyValue("BBB"), "testAdmin1");
+ manager.set("sample_str_policy", PolicyValue("BBB"), "testAdmin1");
/// Manager should return the strongest policy.
- policy = manager.get("sample-str-policy");
+ policy = manager.get("sample_str_policy");
EXPECT_EQ(static_cast<std::string>(policy), "AAA");
manager.disenroll("testAdmin");
TEST(PolicyCoreTests, policy_get_all)
{
+ test::init();
+
auto& manager = PolicyManager::Instance();
auto policies = manager.getAll();
EXPECT_TRUE(policies.size() > 0);
TEST(PolicyCoreTests, policy_get_policy)
{
+ test::init();
+
auto& manager = PolicyManager::Instance();
const auto& policy = manager.getPolicy("sample_int_policy");
EXPECT_EQ(policy->getName(), "sample_int_policy");
bool isRaised = false;
try {
- storage->update("fakeAdmin", "sample-int-policy", PolicyValue(0));
+ storage->update("fakeAdmin", "sample_int_policy", PolicyValue(0));
} catch (const std::exception&) {
isRaised = true;
}
isRaised = false;
try {
- storage->update("testAdmin", "sample-int-policy", PolicyValue(0));
+ storage->update("testAdmin", "sample_int_policy", PolicyValue(0));
} catch (const std::exception&) {
isRaised = true;
}