[Test/Service] add new test to get model from ml-agent
authorJaeyun Jung <jy1210.jung@samsung.com>
Mon, 1 Apr 2024 03:19:14 +0000 (12:19 +0900)
committerjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Wed, 3 Apr 2024 03:01:01 +0000 (12:01 +0900)
We can register new model (or pipeline) using ml-service API.
Add test for registered key from ml-agent.

Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
tests/capi/unittest_capi_service_extension.cc
tests/test_models/config/config_pipeline_imgclf_key.conf [new file with mode: 0644]
tests/test_models/config/config_single_imgclf_key.conf [new file with mode: 0644]

index 8e5c006..9b60a3d 100755 (executable)
@@ -8,6 +8,7 @@
  */
 
 #include <gtest/gtest.h>
+#include <gio/gio.h>
 #include <glib.h>
 
 #include <ml-api-service-private.h>
@@ -37,6 +38,45 @@ typedef struct {
 } extension_test_data_s;
 
 /**
+ * @brief Test base class for database of ML Service API.
+ */
+class MLServiceExtensionTest : public ::testing::Test
+{
+  protected:
+  static GTestDBus *dbus;
+
+  /**
+   * @brief Setup method for each test case.
+   */
+  static void SetUpTestSuite ()
+  {
+    g_autofree gchar *services_dir = g_build_filename ("/usr/bin/ml-test/services", NULL);
+
+    dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
+    ASSERT_NE (nullptr, dbus);
+
+    g_test_dbus_add_service_dir (dbus, services_dir);
+    g_test_dbus_up (dbus);
+  }
+
+  /**
+   * @brief Teardown method for each test case.
+   */
+  static void TearDownTestSuite ()
+  {
+    if (dbus) {
+      g_test_dbus_down (dbus);
+      g_object_unref (dbus);
+    }
+  }
+};
+
+/**
+ * @brief Test dbus to run ml-agent.
+ */
+GTestDBus *MLServiceExtensionTest::dbus = nullptr;
+
+/**
  * @brief Internal function to create test-data.
  */
 static extension_test_data_s *
@@ -99,6 +139,24 @@ _get_data_path (const gchar *data_name)
 }
 
 /**
+ * @brief Internal function to get the model file path.
+ */
+static gchar *
+_get_model_path (const gchar *model_name)
+{
+  const gchar *root_path = g_getenv ("MLAPI_SOURCE_ROOT_PATH");
+
+  /* Supposed to run test in build directory. */
+  if (root_path == NULL)
+    root_path = "..";
+
+  gchar *model_file = g_build_filename (
+      root_path, "tests", "test_models", "models", model_name, NULL);
+
+  return model_file;
+}
+
+/**
  * @brief Callback function for scenario test.
  */
 static void
@@ -423,6 +481,68 @@ TEST_REQUIRE_TFLITE (MLServiceExtension, scenarioConfig3ImgClf)
 }
 
 /**
+ * @brief Usage of ml-service extension API.
+ */
+TEST_F_REQUIRE_TFLITE (MLServiceExtensionTest, scenarioConfig4ImgClf)
+{
+  ml_service_h handle;
+  int status;
+
+  const char test_name[] = "test-single-imgclf";
+  unsigned int version = 0U;
+  g_autofree gchar *config = _get_config_path ("config_single_imgclf_key.conf");
+  g_autofree gchar *model = _get_model_path ("mobilenet_v1_1.0_224_quant.tflite");
+
+  /* Register test model. */
+  ml_service_model_delete (test_name, 0U);
+  ml_service_model_register (test_name, model, true, NULL, &version);
+
+  status = ml_service_new (config, &handle);
+  ASSERT_EQ (status, ML_ERROR_NONE);
+
+  _extension_test_imgclf (handle, FALSE);
+
+  status = ml_service_destroy (handle);
+  EXPECT_EQ (status, ML_ERROR_NONE);
+
+  /* Clear test model. */
+  ml_service_model_delete (test_name, 0U);
+}
+
+/**
+ * @brief Usage of ml-service extension API.
+ */
+TEST_F_REQUIRE_TFLITE (MLServiceExtensionTest, scenarioConfig5ImgClf)
+{
+  ml_service_h handle;
+  int status;
+
+  const char test_name[] = "test-pipeline-imgclf";
+  g_autofree gchar *config = _get_config_path ("config_pipeline_imgclf_key.conf");
+  g_autofree gchar *model = _get_model_path ("mobilenet_v1_1.0_224_quant.tflite");
+  g_autofree gchar *pipeline = g_strdup_printf (
+      "appsrc name=input_img "
+      "caps=other/tensors,num_tensors=1,format=static,types=uint8,dimensions=3:224:224:1,framerate=0/1 ! "
+      "tensor_filter framework=tensorflow-lite model=%s ! tensor_sink name=result_clf",
+      model);
+
+  /* Register test pipeline. */
+  ml_service_pipeline_delete (test_name);
+  ml_service_pipeline_set (test_name, pipeline);
+
+  status = ml_service_new (config, &handle);
+  ASSERT_EQ (status, ML_ERROR_NONE);
+
+  _extension_test_imgclf (handle, TRUE);
+
+  status = ml_service_destroy (handle);
+  EXPECT_EQ (status, ML_ERROR_NONE);
+
+  /* Clear test pipeline. */
+  ml_service_pipeline_delete (test_name);
+}
+
+/**
  * @brief Testcase with invalid param.
  */
 TEST (MLServiceExtension, createConfigInvalidParam01_n)
diff --git a/tests/test_models/config/config_pipeline_imgclf_key.conf b/tests/test_models/config/config_pipeline_imgclf_key.conf
new file mode 100644 (file)
index 0000000..5a5bb53
--- /dev/null
@@ -0,0 +1,28 @@
+{
+    "pipeline" :
+    {
+        "key" : "test-pipeline-imgclf",
+        "input_node" : [
+          {
+            "name" : "input_img",
+            "info" : [
+              {
+                "type" : "uint8",
+                "dimension" : "3:224:224:1"
+              }
+            ]
+          }
+        ],
+        "output_node" : [
+          {
+            "name" : "result_clf",
+            "info" : [
+              {
+                "type" : "uint8",
+                "dimension" : "1001:1"
+              }
+            ]
+          }
+        ]
+    }
+}
diff --git a/tests/test_models/config/config_single_imgclf_key.conf b/tests/test_models/config/config_single_imgclf_key.conf
new file mode 100644 (file)
index 0000000..f245db7
--- /dev/null
@@ -0,0 +1,11 @@
+{
+    "single" :
+    {
+        "key" : "test-single-imgclf"
+    },
+    "information" :
+    {
+        "threshold" : "0.5",
+        "description" : "Config file to run unittest for ml-extension."
+    }
+}