From 7334c9f6749404023ff451c1eabca7c166f14bfa Mon Sep 17 00:00:00 2001 From: Sangjung Woo Date: Mon, 18 Jul 2022 15:10:19 +0900 Subject: [PATCH] [daemon/Test] Add unit test for ML Agent itself To test the daemon process itself, this patch adds unit tests for ML Agent in the GBS environment. This test case does not use the service APIs but directly calls the Proxy methods. Signed-off-by: Sangjung Woo --- packaging/machine-learning-api.spec | 4 ++ tests/daemon/meson.build | 7 ++ tests/daemon/unittest_ml_agent.cc | 129 ++++++++++++++++++++++++++++++++++++ tests/meson.build | 1 + 4 files changed, 141 insertions(+) create mode 100644 tests/daemon/meson.build create mode 100644 tests/daemon/unittest_ml_agent.cc diff --git a/packaging/machine-learning-api.spec b/packaging/machine-learning-api.spec index 5618534..78edf4d 100644 --- a/packaging/machine-learning-api.spec +++ b/packaging/machine-learning-api.spec @@ -351,6 +351,10 @@ bash %{test_script} ./tests/capi/unittest_datatype_consistency bash %{test_script} ./tests/capi/unittest_capi_service bash %{test_script} ./tests/capi/unittest_capi_service_db_mock +%if 0%{?enable_machine_learning_agent} +bash %{test_script} ./tests/daemon/unittest_ml_agent +%endif + %if 0%{?nnfw_support} bash %{test_script} ./tests/capi/unittest_capi_inference_nnfw_runtime %endif diff --git a/tests/daemon/meson.build b/tests/daemon/meson.build new file mode 100644 index 0000000..f7a2e18 --- /dev/null +++ b/tests/daemon/meson.build @@ -0,0 +1,7 @@ +unittest_ml_agent = executable('unittest_ml_agent', + 'unittest_ml_agent.cc', + dependencies: [unittest_common_dep, gdbus_gen_header_test_dep], + install: get_option('install-test'), + install_dir: unittest_install_dir +) +test('unittest_ml_agent', unittest_ml_agent, env: testenv, timeout: 100) diff --git a/tests/daemon/unittest_ml_agent.cc b/tests/daemon/unittest_ml_agent.cc new file mode 100644 index 0000000..8c0d7ac --- /dev/null +++ b/tests/daemon/unittest_ml_agent.cc @@ -0,0 +1,129 @@ +/** + * @file unittest_ml_agent.cc + * @date 16 Jul 2022 + * @brief Unit test for ML Agent itself + * @see https://github.com/nnstreamer/api + * @author Sangjung Woo + * @bug No known bugs + */ + +#include +#include + +#include "test-dbus.h" +#include "../../daemon/includes/dbus-interface.h" +#include "../../daemon/includes/test-dbus-interface.h" + +/** + * @brief Test base class for ML Agent Daemon + */ +class MLAgentTest : public::testing::Test +{ +protected: + GTestDBus *dbus; + GDBusProxy *proxy; + +public: + /** + * @brief Setup method for each test case. + */ + void SetUp() override + { + gchar *services_dir = g_build_filename (g_get_current_dir (), "tests/services", NULL); + dbus = g_test_dbus_new (G_TEST_DBUS_NONE); + g_test_dbus_add_service_dir (dbus, services_dir); + g_test_dbus_up (dbus); + + GError *error = NULL; + proxy = g_dbus_proxy_new_for_bus_sync ( + G_BUS_TYPE_SESSION, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + DBUS_ML_BUS_NAME, + DBUS_TEST_PATH, + DBUS_TEST_INTERFACE, + NULL, + &error); + + if (!proxy || error) { + if (error) { + g_critical ("Error Message : %s", error->message); + g_clear_error (&error); + } + } + + g_free (services_dir); + } + + /** + * @brief Teardown method for each test case. + */ + void TearDown() override + { + if (proxy) + g_object_unref (proxy); + + g_test_dbus_down (dbus); + g_object_unref (dbus); + } +}; + +/** + * @brief Call the 'get_state' DBus method and check the result. + */ +TEST_F (MLAgentTest, call_method) +{ + MachinelearningServiceTest *proxy = NULL; + GError *error = NULL; + int status = 0; + int result = 0; + + /* Test : Connect to the DBus Interface */ + proxy = machinelearning_service_test_proxy_new_for_bus_sync ( + G_BUS_TYPE_SESSION, + G_DBUS_PROXY_FLAGS_NONE, + DBUS_ML_BUS_NAME, + DBUS_TEST_PATH, + NULL, &error); + if (error != NULL) { + g_error_free (error); + FAIL(); + } + + /* Test: Call the DBus method */ + machinelearning_service_test_call_get_state_sync (proxy, &status, &result, NULL, &error); + if (error != NULL) { + g_critical ("Error : %s", error->message); + g_error_free (error); + FAIL(); + } + + /* Check the return value */ + EXPECT_EQ (result, 0); + EXPECT_EQ (status, 1); + + g_object_unref (proxy); +} + +/** + * @brief Main gtest + */ +int +main (int argc, char **argv) +{ + int result = -1; + + try { + testing::InitGoogleTest (&argc, argv); + } catch (...) { + g_warning ("catch 'testing::internal::::ClassUniqueToAlwaysTrue'"); + } + + try { + result = RUN_ALL_TESTS (); + } catch (...) { + g_warning ("catch `testing::internal::GoogleTestFailureException`"); + } + + return result; +} diff --git a/tests/meson.build b/tests/meson.build index 2932131..7b722aa 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -12,6 +12,7 @@ if gtest_dep.found() subdir('capi') if get_option('enable-machine-learning-agent') subdir('services') + subdir('daemon') endif else warning('You should install google-test on your machine.') -- 2.7.4