From 30289f9f65bad841636fa4aed30df6911dcf229a Mon Sep 17 00:00:00 2001 From: Sangwan Kwon Date: Mon, 20 Jul 2020 15:45:12 +0900 Subject: [PATCH] Cleanup deprecated policy-loader Policy-loader is replaced by dynamic-loader (dynamic-table) Change-Id: Ib58e15880b20a20432a3f70f7664c7bd03b702cc Signed-off-by: Sangwan Kwon --- src/vist/client/tests/virtual-table.cpp | 8 ++-- src/vist/policy/CMakeLists.txt | 1 - src/vist/policy/policy-loader.cpp | 50 -------------------- src/vist/policy/policy-loader.hpp | 56 ----------------------- src/vist/policy/policy-manager.cpp | 61 ------------------------- src/vist/policy/policy-manager.hpp | 4 -- src/vist/policy/tests/core.cpp | 33 ++++++++----- src/vist/policy/tests/storage.cpp | 4 +- 8 files changed, 28 insertions(+), 189 deletions(-) delete mode 100644 src/vist/policy/policy-loader.cpp delete mode 100644 src/vist/policy/policy-loader.hpp diff --git a/src/vist/client/tests/virtual-table.cpp b/src/vist/client/tests/virtual-table.cpp index 25178e2..530d11c 100644 --- a/src/vist/client/tests/virtual-table.cpp +++ b/src/vist/client/tests/virtual-table.cpp @@ -44,10 +44,10 @@ TEST(VirtualTableTests, policy_int_filter) VirtualTable> table; EXPECT_TRUE(table.size() > 0); - auto row = table.filter(&Policy::name, "sample-int-policy"); + auto row = table.filter(&Policy::name, "sample_int_policy"); auto value = row[&Policy::value]; EXPECT_TRUE(value > 0); - EXPECT_EQ(row[&Policy::name], "sample-int-policy"); + EXPECT_EQ(row[&Policy::name], "sample_int_policy"); } TEST(VirtualTableTests, policy_str_table) @@ -72,8 +72,8 @@ TEST(VirtualTableTests, policy_str_filter) VirtualTable> table; EXPECT_TRUE(table.size() > 0); - auto row = table.filter(&Policy::name, "sample-str-policy"); + auto row = table.filter(&Policy::name, "sample_str_policy"); auto value = row[&Policy::value]; EXPECT_TRUE(!value.empty()); - EXPECT_EQ(row[&Policy::name], "sample-str-policy"); + EXPECT_EQ(row[&Policy::name], "sample_str_policy"); } diff --git a/src/vist/policy/CMakeLists.txt b/src/vist/policy/CMakeLists.txt index 56912fc..0b9883e 100644 --- a/src/vist/policy/CMakeLists.txt +++ b/src/vist/policy/CMakeLists.txt @@ -17,7 +17,6 @@ SET(${TARGET_VIST_POLICY_LIB}_SRCS "") 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") diff --git a/src/vist/policy/policy-loader.cpp b/src/vist/policy/policy-loader.cpp deleted file mode 100644 index d6c621b..0000000 --- a/src/vist/policy/policy-loader.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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 diff --git a/src/vist/policy/policy-loader.hpp b/src/vist/policy/policy-loader.hpp deleted file mode 100644 index fe83036..0000000 --- a/src/vist/policy/policy-loader.hpp +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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 - -#include - -#include -#include - -#include - -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 - void load(const std::string& name, T& symbol); - -private: - using Handle = std::unique_ptr; - Handle handle; -}; - -template -void PluginLoader::load(const std::string& name, T& symbol) -{ - symbol = reinterpret_cast(::dlsym(handle.get(), name.c_str())); - if (symbol == nullptr) - THROW(ErrCode::RuntimeError) << "Failed to load: " << name; -} - -} // namespace policy -} // namespace vist diff --git a/src/vist/policy/policy-manager.cpp b/src/vist/policy/policy-manager.cpp index 8a67a35..52c49eb 100644 --- a/src/vist/policy/policy-manager.cpp +++ b/src/vist/policy/policy-manager.cpp @@ -15,7 +15,6 @@ */ #include "policy-manager.hpp" -#include "policy-loader.hpp" #include #include @@ -29,48 +28,6 @@ namespace policy { PolicyManager::PolicyManager() : storage(DB_PATH) { - loadProviders(PLUGIN_INSTALL_DIR); - int cnt = loadPolicies(); - INFO(VIST) << std::to_string(cnt) << "-policies loaded"; -} - -std::pair 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&& provider) @@ -94,24 +51,6 @@ void PolicyManager::addProvider(std::shared_ptr&& 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); diff --git a/src/vist/policy/policy-manager.hpp b/src/vist/policy/policy-manager.hpp index ea46d94..11cbe37 100644 --- a/src/vist/policy/policy-manager.hpp +++ b/src/vist/policy/policy-manager.hpp @@ -65,9 +65,6 @@ private: explicit PolicyManager(); ~PolicyManager() = default; - std::pair loadProviders(const std::string& path); - int loadPolicies(); - PolicyStorage storage; std::vector> providers; @@ -76,7 +73,6 @@ private: /// Policy-Provider std::unordered_map policies; - FRIEND_TEST(PolicyCoreTests, policy_loader); FRIEND_TEST(PolicyCoreTests, policy_get_policy); }; diff --git a/src/vist/policy/tests/core.cpp b/src/vist/policy/tests/core.cpp index 98b5d5b..9fa3856 100644 --- a/src/vist/policy/tests/core.cpp +++ b/src/vist/policy/tests/core.cpp @@ -1,5 +1,5 @@ /* - * 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. @@ -17,20 +17,25 @@ #include #include +#include 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(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"); @@ -52,18 +57,20 @@ TEST(PolicyCoreTests, policy_set_get_int) 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(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(policy), "AAA"); manager.disenroll("testAdmin"); @@ -72,6 +79,8 @@ TEST(PolicyCoreTests, policy_set_get_str) TEST(PolicyCoreTests, policy_get_all) { + test::init(); + auto& manager = PolicyManager::Instance(); auto policies = manager.getAll(); EXPECT_TRUE(policies.size() > 0); @@ -79,6 +88,8 @@ TEST(PolicyCoreTests, policy_get_all) 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"); diff --git a/src/vist/policy/tests/storage.cpp b/src/vist/policy/tests/storage.cpp index 87c51d0..2ee762e 100644 --- a/src/vist/policy/tests/storage.cpp +++ b/src/vist/policy/tests/storage.cpp @@ -59,14 +59,14 @@ TEST_F(PolicyStorageTests, update) 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; } -- 2.34.1