From 5b101df9698c5ce9b10f15bdbb0916ea2623eeb6 Mon Sep 17 00:00:00 2001 From: nandan Date: Fri, 13 Jan 2023 15:31:15 +0530 Subject: [PATCH 1/4] FOGL-7272 : Filter unit tests written Signed-off-by: nandan --- Jenkinsfile | 61 ++++++++++++++++++++++++++ plugin.cpp | 10 ++++- tests/CMakeLists.txt | 90 ++++++++++++++++++++++++++++++++++++++ tests/main.cpp | 17 ++++++++ tests/test_info.cpp | 30 +++++++++++++ tests/test_metadata.cpp | 95 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 302 insertions(+), 1 deletion(-) create mode 100644 Jenkinsfile create mode 100644 tests/CMakeLists.txt create mode 100644 tests/main.cpp create mode 100644 tests/test_info.cpp create mode 100644 tests/test_metadata.cpp diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..843007c --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,61 @@ +timestamps { + node("ubuntu18-agent") { + catchError { + checkout scm + dir_exists = sh ( + script: "test -d 'tests' && echo 'Y' || echo 'N' ", + returnStdout: true + ).trim() + + if (dir_exists == 'N'){ + currentBuild.result= 'FAILURE' + echo "No tests directory found! Exiting." + return + } + try { + stage("Prerequisites"){ + // Change to corresponding CORE_BRANCH as required + // e.g. FOGL-xxxx, main etc. + sh ''' + CORE_BRANCH='develop' + ${HOME}/buildFledge ${CORE_BRANCH} ${WORKSPACE} + ''' + } + } catch (e) { + currentBuild.result = 'FAILURE' + echo "Failed to build Fledge; required to run the tests!" + return + } + + try { + stage("Run Tests"){ + echo "Executing tests..." + sh ''' + export FLEDGE_ROOT=$HOME/fledge + cd tests && cmake . && make && ./RunTests --gtest_output=xml:test_output.xml + ''' + echo "Done." + } + } catch (e) { + result = "TESTS FAILED" + currentBuild.result = 'FAILURE' + echo "Tests failed!" + } + + try { + stage("Publish Test Report"){ + junit "tests/test_output.xml" + } + } catch (e) { + result = "TEST REPORT GENERATION FAILED" + currentBuild.result = 'FAILURE' + echo "Failed to generate test reports!" + } + } + + stage ("Cleanup"){ + // Add here if any cleanup is required + echo "Done." + } + } +} \ No newline at end of file diff --git a/plugin.cpp b/plugin.cpp index 4e96f46..2e39bb4 100644 --- a/plugin.cpp +++ b/plugin.cpp @@ -166,7 +166,15 @@ void plugin_ingest(PLUGIN_HANDLE *handle, { elem->addDatapoint(new Datapoint(*it)); } - AssetTracker::getAssetTracker()->addAssetTrackingTuple(info->configCatName, elem->getAssetName(), string("Filter")); + //AssetTracker::getAssetTracker()->addAssetTrackingTuple(info->configCatName, elem->getAssetName(), string("Filter")); + AssetTracker *instance = nullptr; + instance = AssetTracker::getAssetTracker(); + if (instance != nullptr) + { + instance->addAssetTrackingTuple(info->configCatName, elem->getAssetName(), string("Filter")); + } + + } filter->m_func(filter->m_data, origReadingSet); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..5738450 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,90 @@ +cmake_minimum_required(VERSION 2.6.0) + +project(RunTests) + +# Supported options: +# -DFLEDGE_INCLUDE +# -DFLEDGE_LIB +# -DFLEDGE_SRC +# -DFLEDGE_INSTALL +# +# If no -D options are given and FLEDGE_ROOT environment variable is set +# then Fledge libraries and header files are pulled from FLEDGE_ROOT path. + +set(CMAKE_CXX_FLAGS "-std=c++11 -O3") + +# Generation version header file +set_source_files_properties(version.h PROPERTIES GENERATED TRUE) +add_custom_command( + OUTPUT version.h + DEPENDS ${CMAKE_SOURCE_DIR}/../VERSION + COMMAND ${CMAKE_SOURCE_DIR}/../mkversion ${CMAKE_SOURCE_DIR}/.. + COMMENT "Generating version header" + VERBATIM +) +include_directories(${CMAKE_BINARY_DIR}) + +# Add here all needed Fledge libraries as list +set(NEEDED_FLEDGE_LIBS common-lib services-common-lib filters-common-lib) + +set(BOOST_COMPONENTS system thread) + +find_package(Boost 1.53.0 COMPONENTS ${BOOST_COMPONENTS} REQUIRED) +include_directories(SYSTEM ${Boost_INCLUDE_DIR}) + +# Find source files +file(GLOB SOURCES ../*.cpp) +file(GLOB unittests "*.cpp") + +# Find Fledge includes and libs, by including FindFledge.cmak file +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/..) +find_package(Fledge) +# If errors: make clean and remove Makefile +if (NOT FLEDGE_FOUND) + if (EXISTS "${CMAKE_BINARY_DIR}/Makefile") + execute_process(COMMAND make clean WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) + file(REMOVE "${CMAKE_BINARY_DIR}/Makefile") + endif() + # Stop the build process + message(FATAL_ERROR "Fledge plugin '${PROJECT_NAME}' build error.") +endif() +# On success, FLEDGE_INCLUDE_DIRS and FLEDGE_LIB_DIRS variables are set + +# Locate GTest +find_package(GTest REQUIRED) +include_directories(${GTEST_INCLUDE_DIRS}) + +# Add ../include +include_directories(../include) +# Add Fledge include dir(s) +include_directories(${FLEDGE_INCLUDE_DIRS}) + +# Add other include paths +if (FLEDGE_SRC) + message(STATUS "Using third-party includes " ${FLEDGE_SRC}/C/thirdparty) + include_directories(${FLEDGE_SRC}/C/thirdparty/rapidjson/include) + include_directories(${FLEDGE_SRC}/C/thirdparty/Simple-Web-Server) +endif() + +# Add Fledge lib path +link_directories(${FLEDGE_LIB_DIRS}) + +# Link runTests with what we want to test and the GTest and pthread library +add_executable(RunTests ${unittests} ${SOURCES} version.h) + +# Add additional libraries + +# Add additional link directories + +set(FLEDGE_INSTALL "" CACHE INTERNAL "") +# Install library +if (FLEDGE_INSTALL) + message(STATUS "Installing ${PROJECT_NAME} in ${FLEDGE_INSTALL}/plugins/${PLUGIN_TYPE}/${PROJECT_NAME}") + install(TARGETS ${PROJECT_NAME} DESTINATION ${FLEDGE_INSTALL}/plugins/${PLUGIN_TYPE}/${PROJECT_NAME}) +endif() + + +target_link_libraries(RunTests ${GTEST_LIBRARIES} pthread) +target_link_libraries(RunTests ${NEEDED_FLEDGE_LIBS}) +target_link_libraries(RunTests ${Boost_LIBRARIES}) +target_link_libraries(RunTests -lpthread -ldl) diff --git a/tests/main.cpp b/tests/main.cpp new file mode 100644 index 0000000..8642435 --- /dev/null +++ b/tests/main.cpp @@ -0,0 +1,17 @@ +#include +#include +#include +#include + +using namespace std; + +int main(int argc, char **argv) +{ + testing::InitGoogleTest(&argc, argv); + + testing::GTEST_FLAG(repeat) = 200; + testing::GTEST_FLAG(shuffle) = true; + testing::GTEST_FLAG(death_test_style) = "threadsafe"; + + return RUN_ALL_TESTS(); +} diff --git a/tests/test_info.cpp b/tests/test_info.cpp new file mode 100644 index 0000000..c4c991d --- /dev/null +++ b/tests/test_info.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include + +using namespace std; +using namespace rapidjson; + +extern "C" +{ + PLUGIN_INFORMATION *plugin_info(); +}; + +TEST(METADATA_INFO, PluginInfo) +{ + PLUGIN_INFORMATION *info = plugin_info(); + ASSERT_STREQ(info->name, "metadata"); + ASSERT_EQ(info->type, PLUGIN_TYPE_FILTER); +} + +TEST(METADATA_INFO, PluginInfoConfigParse) +{ + PLUGIN_INFORMATION *info = plugin_info(); + Document doc; + doc.Parse(info->config); + ASSERT_EQ(doc.HasParseError(), false); + ASSERT_EQ(doc.IsObject(), true); + ASSERT_EQ(doc.HasMember("plugin"), true); +} diff --git a/tests/test_metadata.cpp b/tests/test_metadata.cpp new file mode 100644 index 0000000..dbccb99 --- /dev/null +++ b/tests/test_metadata.cpp @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace rapidjson; + +extern "C" +{ + PLUGIN_INFORMATION *plugin_info(); + void plugin_ingest(void *handle, + READINGSET *readingSet); + PLUGIN_HANDLE plugin_init(ConfigCategory *config, + OUTPUT_HANDLE *outHandle, + OUTPUT_STREAM output); + int called = 0; + + void Handler(void *handle, READINGSET *readings) + { + called++; + *(READINGSET **)handle = readings; + } +}; + +TEST(METADATA, MetadataDisabled) +{ + PLUGIN_INFORMATION *info = plugin_info(); + ConfigCategory *config = new ConfigCategory("replace", info->config); + ASSERT_NE(config, (ConfigCategory *)NULL); + config->setItemsValueFromDefault(); + ASSERT_EQ(config->itemExists("config"), true); + ASSERT_EQ(config->itemExists("enable"), true); + config->setValue("config", "{ \"Source\" : \"Camera\"}"); + config->setValue("enable", "false"); + ReadingSet *outReadings; + void *handle = plugin_init(config, &outReadings, Handler); + vector *readings = new vector; + + long testValue = 2; + DatapointValue dpv(testValue); + Datapoint *value = new Datapoint("test", dpv); + Reading *in = new Reading("test", value); + readings->push_back(in); + + ReadingSet *readingSet = new ReadingSet(readings); + plugin_ingest(handle, (READINGSET *)readingSet); + + vector results = outReadings->getAllReadings(); + ASSERT_EQ(results.size(), 1); + Reading *out = results[0]; + ASSERT_EQ(out->getDatapointCount(), 1); + + vector points = out->getReadingData(); + ASSERT_EQ(points.size(), 1); +} + +TEST(METADATA, MetadataAddDataPoints) +{ + PLUGIN_INFORMATION *info = plugin_info(); + ConfigCategory *config = new ConfigCategory("replace", info->config); + ASSERT_NE(config, (ConfigCategory *)NULL); + config->setItemsValueFromDefault(); + ASSERT_EQ(config->itemExists("config"), true); + ASSERT_EQ(config->itemExists("enable"), true); + config->setValue("config", "{ \"Source\":\"Camera\"}"); + config->setValue("enable", "true"); + ReadingSet *outReadings; + void *handle = plugin_init(config, &outReadings, Handler); + vector *readings = new vector; + + long testValue = 2; + DatapointValue dpv(testValue); + Datapoint *value = new Datapoint("test", dpv); + Reading *in = new Reading("test", value); + readings->push_back(in); + + ReadingSet *readingSet = new ReadingSet(readings); + plugin_ingest(handle, (READINGSET *)readingSet); + + vector results = outReadings->getAllReadings(); + ASSERT_EQ(results.size(), 1); + Reading *out = results[0]; + ASSERT_EQ(out->getDatapointCount(), 2); + + vector points = out->getReadingData(); + ASSERT_EQ(points.size(), 2); +} \ No newline at end of file From 5aefe584393598d3975a249a37492627f34f7da5 Mon Sep 17 00:00:00 2001 From: nandan Date: Fri, 13 Jan 2023 16:00:04 +0530 Subject: [PATCH 2/4] FOGL-7272 : Fixed typo Signed-off-by: nandan --- tests/test_metadata.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_metadata.cpp b/tests/test_metadata.cpp index dbccb99..720e052 100644 --- a/tests/test_metadata.cpp +++ b/tests/test_metadata.cpp @@ -33,7 +33,7 @@ extern "C" TEST(METADATA, MetadataDisabled) { PLUGIN_INFORMATION *info = plugin_info(); - ConfigCategory *config = new ConfigCategory("replace", info->config); + ConfigCategory *config = new ConfigCategory("metadata", info->config); ASSERT_NE(config, (ConfigCategory *)NULL); config->setItemsValueFromDefault(); ASSERT_EQ(config->itemExists("config"), true); @@ -92,4 +92,5 @@ TEST(METADATA, MetadataAddDataPoints) vector points = out->getReadingData(); ASSERT_EQ(points.size(), 2); -} \ No newline at end of file +} + From 56e03172aae40a89072583d72e1e828babb88fe2 Mon Sep 17 00:00:00 2001 From: nandan Date: Fri, 13 Jan 2023 18:48:56 +0530 Subject: [PATCH 3/4] FOGL-7272 : Modified git ignore and add README file for unit test cases Signed-off-by: nandan --- .gitignore | 8 +++++++- tests/README.rst | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/README.rst diff --git a/.gitignore b/.gitignore index 1c9d6f7..285d5f4 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,12 @@ # build and packages build +tests/build +tests/cmake_install.cmake +tests/CMakeCache.txt +tests/CMakeFiles +tests/*.xml # IDE -*.idea \ No newline at end of file +*.idea + diff --git a/tests/README.rst b/tests/README.rst new file mode 100644 index 0000000..2e75df8 --- /dev/null +++ b/tests/README.rst @@ -0,0 +1,14 @@ +===================================================== +Build and Run test cases +===================================================== + +To build Fledge "metadata" C++ filter plugin unit test cases: + +.. code-block:: console + + $ mkdir build + $ cd build + $ cmake .. + $ make + $ ./RunTests + From c2af10eee721e3cf607fd0d1516ae7e3d5febd78 Mon Sep 17 00:00:00 2001 From: dianomicbot Date: Tue, 17 Oct 2023 11:54:38 +0000 Subject: [PATCH 4/4] VERSION changed Signed-off-by: dianomicbot --- Jenkinsfile | 2 +- VERSION | 2 +- fledge.version | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 843007c..67dbbdf 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -17,7 +17,7 @@ timestamps { // Change to corresponding CORE_BRANCH as required // e.g. FOGL-xxxx, main etc. sh ''' - CORE_BRANCH='develop' + CORE_BRANCH='2.2.0RC' ${HOME}/buildFledge ${CORE_BRANCH} ${WORKSPACE} ''' } diff --git a/VERSION b/VERSION index 7ec1d6d..ccbccc3 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.1.0 +2.2.0 diff --git a/fledge.version b/fledge.version index 5c5c46f..78e4465 100644 --- a/fledge.version +++ b/fledge.version @@ -1 +1 @@ -fledge_version>=2.1 +fledge_version>=2.2