URL: https://github.com/SSSD/sssd/pull/454
Author: mzidek-rh
 Title: #454: TESTS: Order list of entries in some lists
Action: opened

PR body:
"""
Some tests started to fail becuase we depended on specific
order users in groups or messages in ldb results to be
returned and that order changed.

This patch adds a simple helper functions into these tests
that order the entries before comparison with expected results.
more deterministic.

Resolves:
https://pagure.io/SSSD/sssd/issue/3563
"""

To pull the PR as Git branch:
git remote add ghsssd https://github.com/SSSD/sssd
git fetch ghsssd pull/454/head:pr454
git checkout pr454
From 108c0a8b5cf61622344ae1e0767086d7149f7fad Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michal=20=C5=BDidek?= <mzi...@redhat.com>
Date: Mon, 13 Nov 2017 16:15:21 +0100
Subject: [PATCH] TESTS: Order list of entries in some lists

Some tests started to fail becuase we depended on specific
order users in groups or messages in ldb results to be
returned and that order changed.

This patch adds a simple helper functions into these tests
that order the entries before comparison with expected results.
more deterministic.

Resolves:
https://pagure.io/SSSD/sssd/issue/3563
---
 src/tests/cmocka/test_nss_srv.c     | 31 +++++++++++++++++++++++
 src/tests/cmocka/test_sysdb_views.c | 49 +++++++++++++++++++++++++++++++++----
 2 files changed, 75 insertions(+), 5 deletions(-)

diff --git a/src/tests/cmocka/test_nss_srv.c b/src/tests/cmocka/test_nss_srv.c
index 6aa726153..3ccbd3907 100644
--- a/src/tests/cmocka/test_nss_srv.c
+++ b/src/tests/cmocka/test_nss_srv.c
@@ -585,6 +585,34 @@ static errno_t delete_group(struct nss_test_ctx *ctx,
     return ret;
 }
 
+static void order_string_array(char **_list, int size)
+{
+    char **pos1 = NULL;
+    char **pos2 = NULL;
+    char *tmp = NULL;
+    int rounds;
+    int rounds2;
+
+    if (_list == NULL || *_list == NULL || size < 2) {
+        /* Nothing to do */
+        return;
+    }
+
+    /* The point is to have deterministic order of the
+     * strings. strcmp is used for comparisons */
+    for (rounds = 0; rounds != size - 1; rounds++) {
+        for (pos1 = _list, pos2 = _list + 1, rounds2 = rounds;
+             rounds2 != size - 1;
+             pos1++, pos2++, rounds2++) {
+            if (strcmp(*pos1, *pos2) > 0) {
+                tmp = *pos1;
+                *pos1 = *pos2;
+                *pos2 = tmp;
+            }
+        }
+    }
+}
+
 static void assert_groups_equal(struct group *expected,
                                 struct group *gr, const int nmem)
 {
@@ -594,6 +622,9 @@ static void assert_groups_equal(struct group *expected,
     assert_string_equal(gr->gr_name, expected->gr_name);
     assert_string_equal(gr->gr_passwd, expected->gr_passwd);
 
+    order_string_array(gr->gr_mem, nmem);
+    order_string_array(expected->gr_mem, nmem);
+
     for (i = 0; i < nmem; i++) {
         assert_string_equal(gr->gr_mem[i], expected->gr_mem[i]);
     }
diff --git a/src/tests/cmocka/test_sysdb_views.c b/src/tests/cmocka/test_sysdb_views.c
index 0378254b4..2176b59fb 100644
--- a/src/tests/cmocka/test_sysdb_views.c
+++ b/src/tests/cmocka/test_sysdb_views.c
@@ -612,6 +612,39 @@ static int test_enum_users_setup(void **state)
     return 0;
 }
 
+/* Make the order of ldb results deterministic */
+static void order_ldb_res_msgs(struct ldb_result *res, const char *key)
+{
+    struct ldb_message **pos1 = NULL;
+    struct ldb_message **pos2 = NULL;
+    struct ldb_message *tmp = NULL;
+    const char *str1;
+    const char *str2;
+    int rounds;
+    int rounds2;
+
+    if (res == NULL || res->count < 2) {
+        /* Nothing to do */
+        return;
+    }
+
+    /* The point is to have deterministic order of the
+     * results. strcmp is used for comparison of key attributes */
+    for (rounds = 0; rounds != res->count - 1; rounds++) {
+        for (pos1 = &res->msgs[0], pos2 = &res->msgs[1], rounds2 = rounds;
+             rounds2 != res->count - 1;
+             pos1++, pos2++, rounds2++) {
+            str1 = ldb_msg_find_attr_as_string(*pos1, key, NULL);
+            str2 = ldb_msg_find_attr_as_string(*pos2, key, NULL);
+            if (strcmp(str1, str2) > 0) {
+                tmp = *pos1;
+                *pos1 = *pos2;
+                *pos2 = tmp;
+            }
+        }
+    }
+}
+
 static void assert_user_attrs(struct ldb_message *msg,
                               struct sss_domain_info *dom,
                               const char *shortname,
@@ -660,8 +693,9 @@ static void check_enumpwent(int ret, struct sss_domain_info *dom,
     assert_int_equal(ret, EOK);
     assert_int_equal(res->count, N_ELEMENTS(users)-1);
 
-    assert_user_attrs(res->msgs[0], dom, "barney", views);
-    assert_user_attrs(res->msgs[1], dom, "alice", views);
+    order_ldb_res_msgs(res, SYSDB_NAME);
+    assert_user_attrs(res->msgs[0], dom, "alice", views);
+    assert_user_attrs(res->msgs[1], dom, "barney", views);
     assert_user_attrs(res->msgs[2], dom, "bob", views);
 }
 
@@ -703,6 +737,7 @@ static void test_sysdb_enumpwent_filter(void **state)
     ret = sysdb_enumpwent_filter(test_ctx, test_ctx->domain, "b*", 0, &res);
     assert_int_equal(ret, EOK);
     assert_int_equal(res->count, 2);
+    order_ldb_res_msgs(res, SYSDB_NAME);
     assert_user_attrs(res->msgs[0], test_ctx->domain, "barney", false);
     assert_user_attrs(res->msgs[1], test_ctx->domain, "bob", false);
 
@@ -749,6 +784,7 @@ static void test_sysdb_enumpwent_filter_views(void **state)
                                             "b*", NULL, &res);
     assert_int_equal(ret, EOK);
     assert_int_equal(res->count, 2);
+    order_ldb_res_msgs(res, SYSDB_NAME);
     assert_user_attrs(res->msgs[0], test_ctx->domain, "barney", true);
     assert_user_attrs(res->msgs[1], test_ctx->domain, "bob", true);
 
@@ -896,10 +932,11 @@ static void check_enumgrent(int ret, struct sss_domain_info *dom,
 {
     assert_int_equal(ret, EOK);
     assert_int_equal(res->count, N_ELEMENTS(groups)-1);
-    assert_group_attrs(res->msgs[0], dom, "three",
-                       views ? TEST_GID_OVERRIDE_BASE + 2 : 0);
-    assert_group_attrs(res->msgs[1], dom, "one",
+    order_ldb_res_msgs(res, SYSDB_NAME);
+    assert_group_attrs(res->msgs[0], dom, "one",
                        views ? TEST_GID_OVERRIDE_BASE : 0);
+    assert_group_attrs(res->msgs[1], dom, "three",
+                       views ? TEST_GID_OVERRIDE_BASE + 2 : 0);
     assert_group_attrs(res->msgs[2], dom, "two",
                        views ? TEST_GID_OVERRIDE_BASE + 1 : 0);
 }
@@ -942,6 +979,7 @@ static void test_sysdb_enumgrent_filter(void **state)
     ret = sysdb_enumgrent_filter(test_ctx, test_ctx->domain, "t*", 0, &res);
     assert_int_equal(ret, EOK);
     assert_int_equal(res->count, 2);
+    order_ldb_res_msgs(res, SYSDB_NAME);
     assert_group_attrs(res->msgs[0], test_ctx->domain, "three", 0);
     assert_group_attrs(res->msgs[1], test_ctx->domain, "two", 0);
 
@@ -988,6 +1026,7 @@ static void test_sysdb_enumgrent_filter_views(void **state)
                                             "t*", NULL, &res);
     assert_int_equal(ret, EOK);
     assert_int_equal(res->count, 2);
+    order_ldb_res_msgs(res, SYSDB_NAME);
     assert_group_attrs(res->msgs[0], test_ctx->domain,
                        "three", TEST_GID_OVERRIDE_BASE + 2);
     assert_group_attrs(res->msgs[1], test_ctx->domain, "two",
_______________________________________________
sssd-devel mailing list -- sssd-devel@lists.fedorahosted.org
To unsubscribe send an email to sssd-devel-le...@lists.fedorahosted.org

Reply via email to