From ebd1829ae10e2ecfc1a7873f41a1edb63ba3374f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Fri, 21 Nov 2014 14:41:03 +0100
Subject: [PATCH 4/7] tests: remove code duplication in single domain cleanup

---
 src/tests/cmocka/test_dp_opts.c          |  5 +--
 src/tests/cmocka/test_dyndns.c           |  5 +--
 src/tests/cmocka/test_negcache.c         |  5 +--
 src/tests/cmocka/test_nested_groups.c    |  5 +--
 src/tests/cmocka/test_nss_srv.c          |  5 +--
 src/tests/cmocka/test_responder_common.c |  5 +--
 src/tests/cmocka/test_utils.c            |  6 +--
 src/tests/common.h                       |  4 +-
 src/tests/common_dom.c                   | 70 ++++++--------------------------
 9 files changed, 29 insertions(+), 81 deletions(-)

diff --git a/src/tests/cmocka/test_dp_opts.c b/src/tests/cmocka/test_dp_opts.c
index 0f3052ab3c2add8286db6d5f065a8ca9391aae13..8794906e754c1f556b04637827854ebc55e23490 100644
--- a/src/tests/cmocka/test_dp_opts.c
+++ b/src/tests/cmocka/test_dp_opts.c
@@ -33,7 +33,6 @@
 #define TESTS_PATH "tests_opts"
 #define TEST_CONF_DB "test_opt_conf.ldb"
 #define TEST_DOM_NAME "opt_test"
-#define TEST_SYSDB_FILE "cache_"TEST_DOM_NAME".ldb"
 #define TEST_ID_PROVIDER "ldap"
 
 enum test_opts {
@@ -405,12 +404,12 @@ int main(int argc, const char *argv[])
     /* Even though normally the tests should clean up after themselves
      * they might not after a failed run. Remove the old db to be sure */
     tests_set_cwd();
-    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
+    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
     test_dom_suite_setup(TESTS_PATH);
 
     ret = run_tests(tests);
     if (ret == 0 && !no_cleanup) {
-        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
+        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
     }
     return ret;
 }
diff --git a/src/tests/cmocka/test_dyndns.c b/src/tests/cmocka/test_dyndns.c
index 345c170f1e595c0a9c116eb7e8fa84a6bc95ef40..58b8aad305bb8ff51fc5de7790eee7535a37af40 100644
--- a/src/tests/cmocka/test_dyndns.c
+++ b/src/tests/cmocka/test_dyndns.c
@@ -38,7 +38,6 @@
 #define TESTS_PATH "tests_dyndns"
 #define TEST_CONF_DB "test_dyndns_conf.ldb"
 #define TEST_DOM_NAME "dyndns_test"
-#define TEST_SYSDB_FILE "cache_"TEST_DOM_NAME".ldb"
 #define TEST_ID_PROVIDER "ldap"
 
 enum mock_nsupdate_states {
@@ -492,12 +491,12 @@ int main(int argc, const char *argv[])
     /* Even though normally the tests should clean up after themselves
      * they might not after a failed run. Remove the old db to be sure */
     tests_set_cwd();
-    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
+    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
     test_dom_suite_setup(TESTS_PATH);
 
     rv = run_tests(tests);
     if (rv == 0 && !no_cleanup) {
-        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
+        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
     }
     return rv;
 }
diff --git a/src/tests/cmocka/test_negcache.c b/src/tests/cmocka/test_negcache.c
index b6bcb6b087c5c8b4d5f7699feaca3911e2fdc5d8..d8e28d853e3708a407a23921887daa3e672c35a6 100644
--- a/src/tests/cmocka/test_negcache.c
+++ b/src/tests/cmocka/test_negcache.c
@@ -52,7 +52,6 @@
 #define TESTS_PATH "tests_ncache"
 #define TEST_CONF_DB "test_nss_conf.ldb"
 #define TEST_DOM_NAME "nss_test"
-#define TEST_SYSDB_FILE "cache_"TEST_DOM_NAME".ldb"
 #define TEST_ID_PROVIDER "ldap"
 
 /* register_cli_protocol_version is required in test since it links with
@@ -639,12 +638,12 @@ int main(void)
     };
 
     tests_set_cwd();
-    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
+    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
     test_dom_suite_setup(TESTS_PATH);
 
     rv = run_tests(tests);
     if (rv == 0) {
-        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
+        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
     }
     return rv;
 }
diff --git a/src/tests/cmocka/test_nested_groups.c b/src/tests/cmocka/test_nested_groups.c
index b64d67d3cc802d9bb1e585a63da04b68ecc460c4..69033130f899e6f2bf16a04813654fc5f3e529ef 100644
--- a/src/tests/cmocka/test_nested_groups.c
+++ b/src/tests/cmocka/test_nested_groups.c
@@ -34,7 +34,6 @@
 #define TESTS_PATH "tests_ldap_nested_groups"
 #define TEST_CONF_DB "test_ldap_nested_groups_conf.ldb"
 #define TEST_DOM_NAME "ldap_nested_groups_test"
-#define TEST_SYSDB_FILE "cache_"TEST_DOM_NAME".ldb"
 #define TEST_ID_PROVIDER "ldap"
 
 #define new_test(test) \
@@ -488,12 +487,12 @@ int main(int argc, const char *argv[])
     /* Even though normally the tests should clean up after themselves
      * they might not after a failed run. Remove the old db to be sure */
     tests_set_cwd();
-    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
+    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
     test_dom_suite_setup(TESTS_PATH);
 
     rv = run_tests(tests);
     if (rv == 0 && !no_cleanup) {
-        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
+        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
     }
     return rv;
 }
diff --git a/src/tests/cmocka/test_nss_srv.c b/src/tests/cmocka/test_nss_srv.c
index c318d94be867bbb9991de288cdae45d2ddc29b24..8f297a7842e4c9a21821795dbcbe1a5025259ce1 100644
--- a/src/tests/cmocka/test_nss_srv.c
+++ b/src/tests/cmocka/test_nss_srv.c
@@ -36,7 +36,6 @@
 #define TESTS_PATH "tests_nss"
 #define TEST_CONF_DB "test_nss_conf.ldb"
 #define TEST_DOM_NAME "nss_test"
-#define TEST_SYSDB_FILE "cache_"TEST_DOM_NAME".ldb"
 #define TEST_SUBDOM_NAME "test.subdomain"
 #define TEST_ID_PROVIDER "ldap"
 
@@ -2118,12 +2117,12 @@ int main(int argc, const char *argv[])
     /* Even though normally the tests should clean up after themselves
      * they might not after a failed run. Remove the old db to be sure */
     tests_set_cwd();
-    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
+    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
     test_dom_suite_setup(TESTS_PATH);
 
     rv = run_tests(tests);
     if (rv == 0 && !no_cleanup) {
-        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
+        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
     }
     return rv;
 }
diff --git a/src/tests/cmocka/test_responder_common.c b/src/tests/cmocka/test_responder_common.c
index a6f642ab23a973a423a8393b44807e37f79e5c0e..48beb15fc8cee16816b21a7f2ce257b139d5104b 100644
--- a/src/tests/cmocka/test_responder_common.c
+++ b/src/tests/cmocka/test_responder_common.c
@@ -31,7 +31,6 @@
 #define TESTS_PATH "tests_responder"
 #define TEST_CONF_DB "test_responder_conf.ldb"
 #define TEST_DOM_NAME "responder_test"
-#define TEST_SYSDB_FILE "cache_"TEST_DOM_NAME".ldb"
 #define TEST_ID_PROVIDER "ldap"
 
 #define NAME "username"
@@ -314,12 +313,12 @@ int main(int argc, const char *argv[])
     /* Even though normally the tests should clean up after themselves
      * they might not after a failed run. Remove the old db to be sure */
     tests_set_cwd();
-    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
+    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
     test_dom_suite_setup(TESTS_PATH);
 
     rv = run_tests(tests);
     if (rv == 0 && !no_cleanup) {
-        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
+        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
     }
     return rv;
 }
diff --git a/src/tests/cmocka/test_utils.c b/src/tests/cmocka/test_utils.c
index d9781377be70a0d58b0fd1fff2145483dbeb199c..84ed8ee468e697e98b4aae0039eaa9289eca04dd 100644
--- a/src/tests/cmocka/test_utils.c
+++ b/src/tests/cmocka/test_utils.c
@@ -28,7 +28,7 @@
 
 #define TESTS_PATH "tests_utils"
 #define TEST_CONF_DB "test_utils_conf.ldb"
-#define TEST_SYSDB_FILE "cache_utils_test.ldb"
+#define TEST_DOM_NAME "utils_test.ldb"
 
 #define DOM_COUNT 10
 #define DOMNAME_TMPL "name_%zu.dom"
@@ -1052,12 +1052,12 @@ int main(int argc, const char *argv[])
     /* Even though normally the tests should clean up after themselves
      * they might not after a failed run. Remove the old db to be sure */
     tests_set_cwd();
-    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
+    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
     test_dom_suite_setup(TESTS_PATH);
 
     rv = run_tests(tests);
     if (rv == 0) {
-        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_SYSDB_FILE);
+        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
     }
     return rv;
 }
diff --git a/src/tests/common.h b/src/tests/common.h
index ea3a6c9e97c56a5df66bc8755339e0d5db5329f2..714be6988aa032d935c09bc7db524545a4c9154e 100644
--- a/src/tests/common.h
+++ b/src/tests/common.h
@@ -100,8 +100,8 @@ void test_multidom_suite_cleanup(const char *tests_path,
                                  const char **domains);
 
 void test_dom_suite_cleanup(const char *tests_path,
-                            const char *confdb_path,
-                            const char *sysdb_path);
+                            const char *cdb_file,
+                            const char *domain);
 
 struct tevent_req *
 test_request_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, errno_t err);
diff --git a/src/tests/common_dom.c b/src/tests/common_dom.c
index 63ca3b3327fe1444437205f4e9b213af04a4c366..f78b8b293129b82154a36346fab69d65f7f5db1d 100644
--- a/src/tests/common_dom.c
+++ b/src/tests/common_dom.c
@@ -376,8 +376,14 @@ void test_multidom_suite_cleanup(const char *tests_path,
 
     if (domains != NULL) {
         for (i = 0; domains[i] != NULL; i++) {
-            sysdb_path = talloc_asprintf(tmp_ctx, "%s/cache_%s.ldb",
-                                         tests_path, domains[i]);
+            if (strcmp(domains[i], LOCAL_SYSDB_FILE) == 0) {
+                /* local domain */
+                sysdb_path = talloc_asprintf(tmp_ctx, "%s/%s",
+                                             tests_path, domains[i]);
+            } else {
+                sysdb_path = talloc_asprintf(tmp_ctx, "%s/cache_%s.ldb",
+                                             tests_path, domains[i]);
+            }
             if (sysdb_path == NULL) {
                 DEBUG(SSSDBG_CRIT_FAILURE, "Could not construct sysdb path\n");
                 goto done;
@@ -408,62 +414,10 @@ done:
 }
 
 void test_dom_suite_cleanup(const char *tests_path,
-                            const char *confdb_path,
-                            const char *sysdb_path)
+                            const char *cdb_file,
+                            const char *domain)
 {
-    errno_t ret;
-    char *conf_db;
-    char *sys_db;
-    TALLOC_CTX *tmp_ctx;
+    const char *domains[] = {domain, NULL};
 
-    tmp_ctx = talloc_new(NULL);
-    if (!tmp_ctx) {
-        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new failed\n");
-        return;
-    }
-
-    if (confdb_path != NULL) {
-        conf_db = talloc_asprintf(tmp_ctx, "%s/%s", tests_path, confdb_path);
-        if (!conf_db) {
-            DEBUG(SSSDBG_CRIT_FAILURE,
-                "Could not construct conf_db path\n");
-            goto done;
-        }
-
-        errno = 0;
-        ret = unlink(conf_db);
-        if (ret != 0 && errno != ENOENT) {
-            DEBUG(SSSDBG_CRIT_FAILURE,
-                "Could not delete the test config ldb file (%d) (%s)\n",
-                errno, strerror(errno));
-        }
-    }
-
-    if (sysdb_path != NULL) {
-        sys_db = talloc_asprintf(tmp_ctx, "%s/%s", tests_path, sysdb_path);
-        if (!sys_db) {
-            DEBUG(SSSDBG_CRIT_FAILURE,
-                "Could not construct sys_db path\n");
-            goto done;
-        }
-
-        errno = 0;
-        ret = unlink(sys_db);
-        if (ret != 0 && errno != ENOENT) {
-            DEBUG(SSSDBG_CRIT_FAILURE,
-                "Could not delete the test ldb file (%d) (%s)\n",
-                errno, strerror(errno));
-        }
-    }
-
-    errno = 0;
-    ret = rmdir(tests_path);
-    if (ret != 0 && errno != ENOENT) {
-        DEBUG(SSSDBG_CRIT_FAILURE,
-              "Could not delete the test dir (%d) (%s)\n",
-               errno, strerror(errno));
-    }
-
-done:
-    talloc_free(tmp_ctx);
+    test_multidom_suite_cleanup(tests_path, cdb_file, domains);
 }
-- 
1.7.11.7

