[daemon/Test] Add unit test for ML Agent itself
authorSangjung Woo <sangjung.woo@samsung.com>
Mon, 18 Jul 2022 06:10:19 +0000 (15:10 +0900)
committergichan-jang <56856496+gichan-jang@users.noreply.github.com>
Wed, 27 Jul 2022 02:46:18 +0000 (11:46 +0900)
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 <sangjung.woo@samsung.com>
packaging/machine-learning-api.spec
tests/daemon/meson.build [new file with mode: 0644]
tests/daemon/unittest_ml_agent.cc [new file with mode: 0644]
tests/meson.build

index 5618534..78edf4d 100644 (file)
@@ -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 (file)
index 0000000..f7a2e18
--- /dev/null
@@ -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 (file)
index 0000000..8c0d7ac
--- /dev/null
@@ -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 <sangjung.woo@samsung.com>
+ * @bug         No known bugs
+ */
+
+#include <gio/gio.h>
+#include <gtest/gtest.h>
+
+#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::<unnamed>::ClassUniqueToAlwaysTrue'");
+  }
+
+  try {
+    result = RUN_ALL_TESTS ();
+  } catch (...) {
+    g_warning ("catch `testing::internal::GoogleTestFailureException`");
+  }
+
+  return result;
+}
index 2932131..7b722aa 100644 (file)
@@ -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.')