Skip to content

Commit 8fcbc9f

Browse files
anyj0527jaeyun-jung
authored andcommitted
[test] Add more negative test cases for plugin-parser
- Add tests for invalid `rpk_config.json` files. - Let those invalid `rpk_config.json` files created during tests. Signed-off-by: Yongjoo Ahn <yongjoo1.ahn@samsung.com>
1 parent 594d294 commit 8fcbc9f

3 files changed

Lines changed: 98 additions & 11 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is a dummy file.

tests/plugin-parser/test_rpk_samples/invalid_rpk/res/global/sample-res-type/rpk_config.json

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/plugin-parser/unittest_mlops_plugin_parser.cc

Lines changed: 97 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,24 @@ class PkgMgrInfoMockTestFixture : public ::testing::Test
170170

171171
return func_ptr (pkgid.c_str (), appid.c_str (), metadata);
172172
}
173+
174+
/**
175+
* @brief Make a new file with given path and set its content with given value.
176+
*/
177+
bool create_and_set_file (const gchar *path, const gchar *value) {
178+
bool ret = true;
179+
GFile *new_file = g_file_new_for_path (path);
180+
GError *error = NULL;
181+
182+
if (!g_file_set_contents (g_file_get_path (new_file), value, -1, &error)) {
183+
g_warning ("Failed to create/write file '%s': %s\n", path, error->message);
184+
g_clear_error (&error);
185+
ret = false;
186+
}
187+
g_object_unref (new_file);
188+
189+
return ret;
190+
}
173191
};
174192

175193
GTestDBus *PkgMgrInfoMockTestFixture::dbus = nullptr;
@@ -331,7 +349,7 @@ TEST_F (PkgMgrInfoMockTestFixture, rpk_install6_n)
331349
}
332350

333351
/**
334-
* @brief Negative test case of invalid config file.
352+
* @brief Negative test case of various invalid config files.
335353
*/
336354
TEST_F (PkgMgrInfoMockTestFixture, rpk_install7_n)
337355
{
@@ -342,29 +360,99 @@ TEST_F (PkgMgrInfoMockTestFixture, rpk_install7_n)
342360
.WillRepeatedly (Return (PMINFO_R_OK));
343361

344362
EXPECT_CALL(*pkgMgrInfoMockInstance, pkgmgrinfo_pkginfo_get_pkginfo (_, _))
345-
.WillOnce (Return (PMINFO_R_OK));
363+
.WillRepeatedly (Return (PMINFO_R_OK));
346364

347365
char pkg_type_rpk[] = "rpk";
348366
EXPECT_CALL(*pkgMgrInfoMockInstance, pkgmgrinfo_pkginfo_get_type (_, _))
349-
.WillOnce (DoAll (SetArgPointee<1> (pkg_type_rpk),
367+
.WillRepeatedly (DoAll (SetArgPointee<1> (pkg_type_rpk),
350368
Return (PMINFO_R_OK)));
351369

352370
char root_path[] = "../tests/plugin-parser/test_rpk_samples/invalid_rpk";
353371
EXPECT_CALL(*pkgMgrInfoMockInstance, pkgmgrinfo_pkginfo_get_root_path (_, _))
354-
.WillOnce (DoAll (SetArgPointee<1> (root_path),
372+
.WillRepeatedly (DoAll (SetArgPointee<1> (root_path),
355373
Return (PMINFO_R_OK)));
356374

357375
char res_type[] = "sample-res-type";
358376
EXPECT_CALL(*pkgMgrInfoMockInstance, pkgmgrinfo_pkginfo_get_res_type (_, _))
359-
.WillOnce (DoAll (SetArgPointee<1> (res_type),
377+
.WillRepeatedly (DoAll (SetArgPointee<1> (res_type),
360378
Return (PMINFO_R_OK)));
361379

362380
char res_version[] = "1.5.0";
363381
EXPECT_CALL(*pkgMgrInfoMockInstance, pkgmgrinfo_pkginfo_get_res_version (_, _))
364-
.WillOnce (DoAll (SetArgPointee<1> (res_version),
382+
.WillRepeatedly (DoAll (SetArgPointee<1> (res_version),
365383
Return (PMINFO_R_OK)));
366384

385+
/* create and set json file with invalid value */
386+
g_autofree gchar *rpk_config_dir_path = g_strdup_printf ("%s/res/global/%s/", root_path, res_type);
387+
g_autofree gchar *config_file_path = g_strdup_printf ("%s/rpk_config.json", rpk_config_dir_path);
388+
389+
/* test 1 : invalid json format */
390+
const gchar *json_with_invalid_format = R""""(
391+
{
392+
some invalid json
393+
}
394+
)"""";
395+
ASSERT_TRUE (create_and_set_file (config_file_path, json_with_invalid_format));
367396
EXPECT_NE (exec_plugin_parser_func ("PKGMGR_MDPARSER_PLUGIN_INSTALL", pkgid, appid, NULL), 0);
397+
398+
if (remove (config_file_path) != 0) {
399+
g_printerr ("Error removing file: %s\n", g_strerror (errno));
400+
ASSERT_TRUE (false);
401+
}
402+
403+
/* test 2 : model has no 'name' field */
404+
const gchar *json_with_no_name = R""""(
405+
{
406+
"models" : {
407+
"model" : "dummy-global.tflite",
408+
"description" : "No name field, invalid rpk",
409+
"activate" : "true"
410+
}
411+
}
412+
)"""";
413+
ASSERT_TRUE (create_and_set_file (config_file_path, json_with_no_name));
414+
EXPECT_NE (exec_plugin_parser_func ("PKGMGR_MDPARSER_PLUGIN_INSTALL", pkgid, appid, NULL), 0);
415+
416+
if (remove (config_file_path) != 0) {
417+
g_printerr ("Error removing file: %s\n", g_strerror (errno));
418+
ASSERT_TRUE (false);
419+
}
420+
421+
/* test 3 : pipeline has no 'name' field */
422+
const gchar *json_with_no_name_pipeline = R""""(
423+
{
424+
"pipelines" : {
425+
"pipeline" : "pipe ! line",
426+
"description" : "No name field, invalid rpk"
427+
}
428+
}
429+
)"""";
430+
ASSERT_TRUE (create_and_set_file (config_file_path, json_with_no_name_pipeline));
431+
EXPECT_NE (exec_plugin_parser_func ("PKGMGR_MDPARSER_PLUGIN_INSTALL", pkgid, appid, NULL), 0);
432+
433+
if (remove (config_file_path) != 0) {
434+
g_printerr ("Error removing file: %s\n", g_strerror (errno));
435+
ASSERT_TRUE (false);
436+
}
437+
438+
/* test 4 : resource has no 'name' field */
439+
const gchar *json_with_no_name_resource = R""""(
440+
{
441+
"resources" : {
442+
"description" : "No name filed, invalid rpk",
443+
"path" : [
444+
"resource_00.dat"
445+
]
446+
}
447+
}
448+
)"""";
449+
ASSERT_TRUE (create_and_set_file (config_file_path, json_with_no_name_resource));
450+
EXPECT_NE (exec_plugin_parser_func ("PKGMGR_MDPARSER_PLUGIN_INSTALL", pkgid, appid, NULL), 0);
451+
452+
if (remove (config_file_path) != 0) {
453+
g_printerr ("Error removing file: %s\n", g_strerror (errno));
454+
ASSERT_TRUE (false);
455+
}
368456
}
369457

370458
/**
@@ -423,13 +511,13 @@ TEST_F (PkgMgrInfoMockTestFixture, rpk_scenario_p1)
423511
.WillRepeatedly (DoAll (SetArgPointee<1> (res_version),
424512
Return (PMINFO_R_OK)));
425513

426-
// install rpk
514+
/* install rpk */
427515
EXPECT_EQ (exec_plugin_parser_func ("PKGMGR_MDPARSER_PLUGIN_INSTALL", pkgid, appid, NULL), 0);
428516

429-
// upgrade rpk
517+
/* upgrade rpk */
430518
EXPECT_EQ (exec_plugin_parser_func ("PKGMGR_MDPARSER_PLUGIN_UPGRADE", pkgid, appid, NULL), 0);
431519

432-
// uninstall the rpk
520+
/* uninstall the rpk */
433521
EXPECT_EQ (exec_plugin_parser_func ("PKGMGR_MDPARSER_PLUGIN_UNINSTALL", pkgid, appid, NULL), 0);
434522
}
435523

0 commit comments

Comments
 (0)