From 68c4cc2d90fd172354370dc27a6b3f84ef94d66a Mon Sep 17 00:00:00 2001 From: Ahmed Messaoud Date: Wed, 8 Jun 2022 12:42:53 -0700 Subject: [PATCH 01/11] Added modulestest reports to CI --- .github/workflows/ci.yml | 8 ++++++++ src/modules/test/CMakeLists.txt | 1 - 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e0e5cd3b5..25bfacf09 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -69,6 +69,14 @@ jobs: working-directory: ${{ env.container-workspace }}/build cmd: ctest + - name: Run modulestest + if: success() || failure() + uses: ./.github/actions/container-exec + with: + container: ${{ steps.container.outputs.id }} + working-directory: ${{ env.container-workspace }}/build/modules/test + cmd: modulestest --gtest_output=xml:${{ env.container-workspace }}/build/gtest-output/TestRecipes.xml testplate.json + - uses: ./.github/actions/gtest-xml if: success() || failure() with: diff --git a/src/modules/test/CMakeLists.txt b/src/modules/test/CMakeLists.txt index 6bfbecfd1..5277b19f5 100644 --- a/src/modules/test/CMakeLists.txt +++ b/src/modules/test/CMakeLists.txt @@ -1,7 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -include(GoogleTest) project(modulestest) set(CMAKE_CXX_STANDARD 17) From ab08e73a1702271f56f49a3bccbc29ef2472220c Mon Sep 17 00:00:00 2001 From: Ahmed Messaoud Date: Wed, 8 Jun 2022 13:05:28 -0700 Subject: [PATCH 02/11] wip --- .github/workflows/ci.yml | 2 +- src/modules/test/CMakeLists.txt | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 25bfacf09..41393bf84 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -75,7 +75,7 @@ jobs: with: container: ${{ steps.container.outputs.id }} working-directory: ${{ env.container-workspace }}/build/modules/test - cmd: modulestest --gtest_output=xml:${{ env.container-workspace }}/build/gtest-output/TestRecipes.xml testplate.json + cmd: ./modulestest --gtest_output=xml:${{ env.container-workspace }}/build/gtest-output/TestRecipes.xml testplate.json - uses: ./.github/actions/gtest-xml if: success() || failure() diff --git a/src/modules/test/CMakeLists.txt b/src/modules/test/CMakeLists.txt index 5277b19f5..39a1ef582 100644 --- a/src/modules/test/CMakeLists.txt +++ b/src/modules/test/CMakeLists.txt @@ -36,11 +36,6 @@ else() ) endif() -gtest_discover_tests(modulestest - EXTRA_ARGS ${CMAKE_CURRENT_BINARY_DIR}/testplate.json - XML_OUTPUT_DIR ${GTEST_OUTPUT_DIR} -) - if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8) target_link_libraries(modulestest stdc++fs) endif() From a36a1a7092dffc5f74becd65c4668ae68eed722b Mon Sep 17 00:00:00 2001 From: Ahmed Messaoud Date: Wed, 8 Jun 2022 13:34:04 -0700 Subject: [PATCH 03/11] wip --- .github/workflows/ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 41393bf84..3a39edcdc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -75,7 +75,9 @@ jobs: with: container: ${{ steps.container.outputs.id }} working-directory: ${{ env.container-workspace }}/build/modules/test - cmd: ./modulestest --gtest_output=xml:${{ env.container-workspace }}/build/gtest-output/TestRecipes.xml testplate.json + cmd: | + mkdir -p /etc/osconfig/ + ./modulestest --gtest_output=xml:${{ env.container-workspace }}/build/gtest-output/TestRecipes.xml testplate.json - uses: ./.github/actions/gtest-xml if: success() || failure() From 030f050191d0a454b18c1e2e4cbf1ea22813a534 Mon Sep 17 00:00:00 2001 From: Ahmed Messaoud Date: Wed, 8 Jun 2022 15:19:03 -0700 Subject: [PATCH 04/11] corrected commandrunner recipe --- src/modules/test/recipes/CommandRunnerTests.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/test/recipes/CommandRunnerTests.json b/src/modules/test/recipes/CommandRunnerTests.json index 20b52c7aa..0570c9b23 100644 --- a/src/modules/test/recipes/CommandRunnerTests.json +++ b/src/modules/test/recipes/CommandRunnerTests.json @@ -22,13 +22,13 @@ "Payload": "{\"commandId\": \"test1\", \"arguments\": \"echo Hello World\", \"timeout\": 0, \"singleLineTextResult\": true, \"action\": 3}", "PayloadSizeBytes": 123, "ExpectedResult": 0, - "WaitSeconds": 10 + "WaitSeconds": 0 }, { "ComponentName": "CommandRunner", "ObjectName": "commandStatus", "Desired": false, - "Payload": "{\"commandId\":\"test1\",\"resultCode\":0,\"textResult\":\"Hello World\",\"currentState\":2}", + "Payload": "{\"commandId\":\"test1\",\"resultCode\":0,\"textResult\":\"\",\"currentState\":2}", "ExpectedResult": 0 } ] \ No newline at end of file From 86fde7a44bc21a375946235f5f0ee18c2a9799f0 Mon Sep 17 00:00:00 2001 From: Ahmed Messaoud Date: Wed, 8 Jun 2022 15:29:27 -0700 Subject: [PATCH 05/11] corrected commandrunner recipe --- src/modules/test/recipes/CommandRunnerTests.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/test/recipes/CommandRunnerTests.json b/src/modules/test/recipes/CommandRunnerTests.json index 0570c9b23..5082d5391 100644 --- a/src/modules/test/recipes/CommandRunnerTests.json +++ b/src/modules/test/recipes/CommandRunnerTests.json @@ -22,7 +22,7 @@ "Payload": "{\"commandId\": \"test1\", \"arguments\": \"echo Hello World\", \"timeout\": 0, \"singleLineTextResult\": true, \"action\": 3}", "PayloadSizeBytes": 123, "ExpectedResult": 0, - "WaitSeconds": 0 + "WaitSeconds": 5 }, { "ComponentName": "CommandRunner", From edf3b53065431aeb0a5ee51162701ffbc5f96a66 Mon Sep 17 00:00:00 2001 From: Ahmed Messaoud Date: Mon, 13 Jun 2022 11:12:22 -0700 Subject: [PATCH 06/11] Removed Tpm test recipe --- src/modules/test/recipes/TpmTests.json | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 src/modules/test/recipes/TpmTests.json diff --git a/src/modules/test/recipes/TpmTests.json b/src/modules/test/recipes/TpmTests.json deleted file mode 100644 index a79bf6b3c..000000000 --- a/src/modules/test/recipes/TpmTests.json +++ /dev/null @@ -1,9 +0,0 @@ -[ - { - "ComponentName": "Tpm", - "ObjectName": "tpmStatus", - "Desired": false, - "ExpectedResult": 0, - "WaitSeconds": 0 - } -] \ No newline at end of file From f8e2ad99ad9b0293f7dc06ee12fd5e1e3aded23f Mon Sep 17 00:00:00 2001 From: Ahmed Messaoud Date: Tue, 14 Jun 2022 14:47:39 -0700 Subject: [PATCH 07/11] Added ManagementModule.h --- src/modules/test/CMakeLists.txt | 3 +- src/modules/test/ManagementModule.h | 123 ++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 src/modules/test/ManagementModule.h diff --git a/src/modules/test/CMakeLists.txt b/src/modules/test/CMakeLists.txt index 39a1ef582..f2f9db625 100644 --- a/src/modules/test/CMakeLists.txt +++ b/src/modules/test/CMakeLists.txt @@ -14,8 +14,7 @@ target_link_libraries(testfactorylib parsonlib) target_include_directories(testfactorylib PUBLIC - ${MODULES_INC_DIR} - ${PLATFORM_INC_DIR}) + ${MODULES_INC_DIR}) add_executable(modulestest main.cpp) target_link_libraries(modulestest gtest gtest_main pthread testfactorylib) diff --git a/src/modules/test/ManagementModule.h b/src/modules/test/ManagementModule.h new file mode 100644 index 000000000..3e99eb6f0 --- /dev/null +++ b/src/modules/test/ManagementModule.h @@ -0,0 +1,123 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#ifndef MANAGEMENTMODULE_H +#define MANAGEMENTMODULE_H + +// MMI function definitions +using Mmi_GetInfo = int (*)(const char*, MMI_JSON_STRING*, int*); +using Mmi_Free = void (*)(MMI_JSON_STRING); +using Mmi_Open = MMI_HANDLE (*)(const char*, const unsigned int); +using Mmi_Set = int (*)(MMI_HANDLE, const char*, const char*, const MMI_JSON_STRING, const int); +using Mmi_Get = int (*)(MMI_HANDLE, const char*, const char*, MMI_JSON_STRING*, int*); +using Mmi_Close = void (*)(MMI_HANDLE); + +class ManagementModule +{ +public: + // Lifetime of the operation - see MmiGetInfo Schema for Lifetime property + enum Lifetime + { + Undefined = 0, + KeepAlive = 1, + Short = 2 + }; + + struct Version + { + unsigned int major = 0; + unsigned int minor = 0; + unsigned int patch = 0; + unsigned int tweak = 0; + + // Only defining "<" as we are only using that operator in comparisons + bool operator < (const Version &rhs) const + { + return (((major < rhs.major) || + ((major == rhs.major) && (minor < rhs.minor)) || + ((major == rhs.major) && (minor == rhs.minor) && (patch < rhs.patch)) || + ((major == rhs.major) && (minor == rhs.minor) && (patch == rhs.patch) && (tweak < rhs.tweak))) + ? true : false); + } + + std::string ToString() const + { + std::ostringstream ostream; + ostream << major << "." << minor << "." << patch << "." << tweak; + return ostream.str(); + } + }; + + // Structure maps to the MmiGetInfo JSON Schema - see src/modules/schema/mmi-get-info.schema.json for more info + struct Info + { + std::string name; + std::string description; + std::string manufacturer; + Version version; + std::string versionInfo; + std::vector components; + Lifetime lifetime; + std::string licenseUri; + std::string projectUri; + unsigned int userAccount; + + static int Deserialize(const rapidjson::Value& object, Info& info); + }; + + ManagementModule(); + ManagementModule(const std::string path); + virtual ~ManagementModule(); + + virtual int Load(); + virtual void Unload(); + + Info GetInfo() const; + +protected: + const std::string m_modulePath; + + // The handle retuned by dlopen() + void* m_handle; + + // Management Module Interface (MMI) imported functions + Mmi_GetInfo m_mmiGetInfo; + Mmi_Open m_mmiOpen; + Mmi_Close m_mmiClose; + Mmi_Set m_mmiSet; + Mmi_Get m_mmiGet; + Mmi_Free m_mmiFree; + + Info m_info; + + virtual int CallMmiGetInfo(const char* clientName, MMI_JSON_STRING* payload, int* payloadSizeBytes); + virtual MMI_HANDLE CallMmiOpen(const char* componentName, unsigned int maxPayloadSizeBytes); + virtual void CallMmiClose(MMI_HANDLE handle); + virtual int CallMmiSet(MMI_HANDLE handle, const char* componentName, const char* objectName, const MMI_JSON_STRING payload, const int payloadSizeBytes); + virtual int CallMmiGet(MMI_HANDLE handle, const char* componentName, const char* objectName, MMI_JSON_STRING *payload, int *payloadSizeBytes); + + friend class MmiSession; +}; + +class MmiSession +{ +public: + MmiSession(std::shared_ptr module, const std::string& clientName, unsigned int maxPayloadSizeBytes = 0); + ~MmiSession(); + + int Open(); + void Close(); + + int Set(const char* componentName, const char* objectName, const MMI_JSON_STRING payload, const int payloadSizeBytes); + int Get(const char* componentName, const char* objectName, MMI_JSON_STRING *payload, int *payloadSizeBytes); + + ManagementModule::Info GetInfo(); +private: + const std::string m_clientName; + const unsigned int m_maxPayloadSizeBytes; + std::shared_ptr m_module; + + MMI_HANDLE m_mmiHandle; +}; + +#endif // MANAGEMENTMODULE_H \ No newline at end of file From 42f8a1e951d1a7e2186175f434a90a2ba15a15e6 Mon Sep 17 00:00:00 2001 From: Ahmed Messaoud Date: Wed, 15 Jun 2022 12:44:29 -0700 Subject: [PATCH 08/11] wip --- devops/scripts/generate_moduletest_metadata.sh | 2 +- src/modules/test/Common.h | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/devops/scripts/generate_moduletest_metadata.sh b/devops/scripts/generate_moduletest_metadata.sh index b1a004411..21744dc92 100755 --- a/devops/scripts/generate_moduletest_metadata.sh +++ b/devops/scripts/generate_moduletest_metadata.sh @@ -26,7 +26,7 @@ while IFS= read -r line ; if [ ! -z "$recipepath" ] && [ ! -z "$mimpath" ]; then # Create entry if both mim+recipe are found - echo "Found '$modulename' module adding to test metadata" + echo "Found '$modulename' module adding to recipe configuration" json="[{ \"ModulePath\": \"$line\", \"MimPath\": \"$mimpath\", \"TestRecipesPath\": \"$recipepath\" }]" testJSON=$(echo $testJSON | jq --argjson testMetadata "$json" '. |= . + $testMetadata') fi diff --git a/src/modules/test/Common.h b/src/modules/test/Common.h index 7df4db76e..43b668c7e 100644 --- a/src/modules/test/Common.h +++ b/src/modules/test/Common.h @@ -55,8 +55,16 @@ inline void TestLogInfo(const char* log) inline void TestLogInfo(const char* format, const char* args...) { char buf[g_lineLength] = {0}; - std::snprintf(buf, g_lineLength, format, args); - std::cout << buf << std::endl; + int status = std::snprintf(buf, g_lineLength -1, format, args); + std::cout << "============ status: " << status << std::endl; + if ((0 < status) && (static_cast(status) < (g_lineLength-1))) + { + std::cout << buf << std::endl; + } + else + { + std::cerr << "Failed to log -- insufficient buffer, needs: " << status << " bytes." << std::endl; + } } inline void TestLogError(const char* log) From 056510f7ad7f41b76ed37dd001eeb8a7928ec816 Mon Sep 17 00:00:00 2001 From: Ahmed Messaoud Date: Wed, 15 Jun 2022 13:26:15 -0700 Subject: [PATCH 09/11] Fixed logging --- src/modules/test/Common.h | 35 ++--------------------------------- 1 file changed, 2 insertions(+), 33 deletions(-) diff --git a/src/modules/test/Common.h b/src/modules/test/Common.h index 43b668c7e..82269345d 100644 --- a/src/modules/test/Common.h +++ b/src/modules/test/Common.h @@ -45,38 +45,7 @@ #include "RecipeInvoker.h" #include "MimParser.h" -constexpr const size_t g_lineLength = 256; - -inline void TestLogInfo(const char* log) -{ - std::cout << log << std::endl; -} - -inline void TestLogInfo(const char* format, const char* args...) -{ - char buf[g_lineLength] = {0}; - int status = std::snprintf(buf, g_lineLength -1, format, args); - std::cout << "============ status: " << status << std::endl; - if ((0 < status) && (static_cast(status) < (g_lineLength-1))) - { - std::cout << buf << std::endl; - } - else - { - std::cerr << "Failed to log -- insufficient buffer, needs: " << status << " bytes." << std::endl; - } -} - -inline void TestLogError(const char* log) -{ - std::cerr << log << std::endl; -} - -inline void TestLogError(const char* format, const char* args...) -{ - char buf[g_lineLength] = {0}; - std::snprintf(buf, g_lineLength, format, args); - std::cerr << buf << std::endl; -} +#define TestLogInfo(format, ...) printf(format, ##__VA_ARGS__); std::cout << std::endl +#define TestLogError(format, ...) fprintf(stderr, format, ##__VA_ARGS__); std::cerr << std::endl #endif // MODULESTESTCOMMON_H \ No newline at end of file From 80255652dec1c802603da5113bf487f73bfa86eb Mon Sep 17 00:00:00 2001 From: Ahmed Messaoud Date: Wed, 15 Jun 2022 13:29:01 -0700 Subject: [PATCH 10/11] Re-added Tpm recipe --- src/modules/test/recipes/TpmTests.json | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/modules/test/recipes/TpmTests.json diff --git a/src/modules/test/recipes/TpmTests.json b/src/modules/test/recipes/TpmTests.json new file mode 100644 index 000000000..a79bf6b3c --- /dev/null +++ b/src/modules/test/recipes/TpmTests.json @@ -0,0 +1,9 @@ +[ + { + "ComponentName": "Tpm", + "ObjectName": "tpmStatus", + "Desired": false, + "ExpectedResult": 0, + "WaitSeconds": 0 + } +] \ No newline at end of file From aca413f2e1aca7ec6d9dfcf3f7bafb3bceeebba2 Mon Sep 17 00:00:00 2001 From: Ahmed Messaoud Date: Wed, 15 Jun 2022 14:07:35 -0700 Subject: [PATCH 11/11] addressed comments --- src/modules/test/Common.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/test/Common.h b/src/modules/test/Common.h index 82269345d..70e5c3c5d 100644 --- a/src/modules/test/Common.h +++ b/src/modules/test/Common.h @@ -45,7 +45,7 @@ #include "RecipeInvoker.h" #include "MimParser.h" -#define TestLogInfo(format, ...) printf(format, ##__VA_ARGS__); std::cout << std::endl -#define TestLogError(format, ...) fprintf(stderr, format, ##__VA_ARGS__); std::cerr << std::endl +#define TestLogInfo(format, ...) {printf(format, ##__VA_ARGS__); std::cout << std::endl;} +#define TestLogError(format, ...) {fprintf(stderr, format, ##__VA_ARGS__); std::cerr << std::endl;} #endif // MODULESTESTCOMMON_H \ No newline at end of file