libmultipath: add uevent_is_mpath
authorMartin Wilck <mwilck@suse.com>
Wed, 17 Jan 2018 07:49:38 +0000 (08:49 +0100)
committerChristophe Varoqui <christophe.varoqui@opensvc.com>
Wed, 7 Mar 2018 08:40:18 +0000 (09:40 +0100)
This function can be used to test if an uevent belongs to valid
multipath device. Unit tests are also added.

Signed-off-by: Martin Wilck <mwilck@suse.com>
libmultipath/uevent.c
libmultipath/uevent.h
tests/uevent.c

index 3d32e31..8f4129c 100644 (file)
@@ -52,6 +52,7 @@
 #include "util.h"
 #include "config.h"
 #include "blacklist.h"
+#include "devmapper.h"
 
 #define MAX_ACCUMULATION_COUNT 2048
 #define MAX_ACCUMULATION_TIME 30*1000
@@ -929,3 +930,14 @@ const char *uevent_get_dm_action(const struct uevent *uev)
 {
        return uevent_get_dm_str(uev, "DM_ACTION");
 }
+
+bool uevent_is_mpath(const struct uevent *uev)
+{
+       const char *uuid = uevent_get_env_var(uev, "DM_UUID");
+
+       if (uuid == NULL)
+               return false;
+       if (strncmp(uuid, UUID_PREFIX, UUID_PREFIX_LEN))
+               return false;
+       return uuid[UUID_PREFIX_LEN] != '\0';
+}
index bf70170..cb5347e 100644 (file)
@@ -39,5 +39,6 @@ int uevent_get_disk_ro(const struct uevent *uev);
 const char *uevent_get_dm_name(const struct uevent *uev);
 const char *uevent_get_dm_path(const struct uevent *uev);
 const char *uevent_get_dm_action(const struct uevent *uev);
+bool uevent_is_mpath(const struct uevent *uev);
 
 #endif /* _UEVENT_H */
index a8edd71..b7d6458 100644 (file)
@@ -16,6 +16,7 @@
  *
  */
 
+#include <stdbool.h>
 #include <stdarg.h>
 #include <stddef.h>
 #include <setjmp.h>
@@ -208,6 +209,61 @@ static void test_dm_name_good_1(void **state)
        free((void*)name);
 }
 
+static void test_dm_uuid_false_0(void **state)
+{
+       struct uevent *uev = *state;
+
+       assert_false(uevent_is_mpath(uev));
+}
+
+static void test_dm_uuid_true_0(void **state)
+{
+       struct uevent *uev = *state;
+
+       uev->envp[3] = "DM_UUID=mpath-foo";
+       assert_true(uevent_is_mpath(uev));
+}
+
+static void test_dm_uuid_false_1(void **state)
+{
+       struct uevent *uev = *state;
+
+       uev->envp[3] = "DM_UUID.mpath-foo";
+       assert_false(uevent_is_mpath(uev));
+}
+
+static void test_dm_uuid_false_2(void **state)
+{
+       struct uevent *uev = *state;
+
+       uev->envp[3] = "DM_UUID=mpath-";
+       assert_false(uevent_is_mpath(uev));
+}
+
+static void test_dm_uuid_false_3(void **state)
+{
+       struct uevent *uev = *state;
+
+       uev->envp[3] = "DM_UU=mpath-foo";
+       assert_false(uevent_is_mpath(uev));
+}
+
+static void test_dm_uuid_false_4(void **state)
+{
+       struct uevent *uev = *state;
+
+       uev->envp[3] = "DM_UUID=mpathfoo";
+       assert_false(uevent_is_mpath(uev));
+}
+
+static void test_dm_uuid_false_5(void **state)
+{
+       struct uevent *uev = *state;
+
+       uev->envp[3] = "DM_UUID=";
+       assert_false(uevent_is_mpath(uev));
+}
+
 int test_uevent_get_XXX(void)
 {
        const struct CMUnitTest tests[] = {
@@ -228,6 +284,13 @@ int test_uevent_get_XXX(void)
                cmocka_unit_test(test_dm_name_bad_0),
                cmocka_unit_test(test_dm_name_bad_1),
                cmocka_unit_test(test_dm_name_good_1),
+               cmocka_unit_test(test_dm_uuid_false_0),
+               cmocka_unit_test(test_dm_uuid_true_0),
+               cmocka_unit_test(test_dm_uuid_false_1),
+               cmocka_unit_test(test_dm_uuid_false_2),
+               cmocka_unit_test(test_dm_uuid_false_3),
+               cmocka_unit_test(test_dm_uuid_false_4),
+               cmocka_unit_test(test_dm_uuid_false_5),
        };
        return cmocka_run_group_tests(tests, setup_uev, teardown);
 }