[tracker/sam/functional-test-runner: 15/15] WIP: functional-tests miner tests



commit ece5cdad661538f0a679b8c8771c095b0c1ebda5
Author: Sam Thursfield <sam afuera me uk>
Date:   Tue Dec 30 21:18:28 2014 +0000

    WIP: functional-tests miner tests
    
    still need to think about how to get useful logging output
    from the tests when debugging ...

 tests/functional-tests/common/{utils => }/dconf.py |    4 +-
 tests/functional-tests/common/helpers.py           |   77 +++++++++-----------
 tests/functional-tests/common/sandbox.py           |   27 +++++++-
 tests/functional-tests/common/utils/system.py      |   35 ---------
 tests/functional-tests/miner/__init__.py           |   28 +++++++-
 .../utils/minertest.py => miner/miner_testcase.py} |   37 ++++++----
 .../{300-miner-basic-ops.py => test_basic.py}      |   18 +----
 .../{310-fts-indexing.py => test_fts_indexing.py}  |   17 ++---
 ...esource-removal.py => test_resource_removal.py} |   34 +--------
 9 files changed, 126 insertions(+), 151 deletions(-)
---
diff --git a/tests/functional-tests/common/utils/dconf.py b/tests/functional-tests/common/dconf.py
similarity index 96%
rename from tests/functional-tests/common/utils/dconf.py
rename to tests/functional-tests/common/dconf.py
index 986aeee..3509bdb 100644
--- a/tests/functional-tests/common/utils/dconf.py
+++ b/tests/functional-tests/common/dconf.py
@@ -3,7 +3,7 @@ from gi.repository import Gio
 
 import os
 
-from helpers import log
+from logging import info
 
 
 class DConfClient(object):
@@ -76,5 +76,5 @@ class DConfClient(object):
                                 "dconf",
                                 "trackertest")
         if os.path.exists(dconf_db):
-            log("[Conf] Removing dconf database: " + dconf_db)
+            info("[Conf] Removing dconf database: " + dconf_db)
             os.remove(dconf_db)
diff --git a/tests/functional-tests/common/helpers.py b/tests/functional-tests/common/helpers.py
index 44403ef..a591b09 100644
--- a/tests/functional-tests/common/helpers.py
+++ b/tests/functional-tests/common/helpers.py
@@ -30,12 +30,10 @@ import sys
 import subprocess
 import time
 
-from logging import info
+from logging import debug, info, warn
 
 import utils.configuration as cfg
 
-from utils import options
-
 
 class NoMetadataException (Exception):
     pass
@@ -43,11 +41,6 @@ class NoMetadataException (Exception):
 REASONABLE_TIMEOUT = 30
 
 
-def log(message):
-    if options.is_verbose():
-        print (message)
-
-
 class Helper:
 
     """
@@ -108,17 +101,18 @@ class Helper:
                         "FLAGS",
                         [])
 
-        if options.is_manual_start():
-            print ("Start %s manually" % self.PROCESS_NAME)
-        else:
+        #if options.is_manual_start():
+        #    print ("Start %s manually" % self.PROCESS_NAME)
+        #else:
+        if True:
             kws = {}
 
-            if not options.is_verbose():
-                FNULL = open('/dev/null', 'w')
-                kws = {'stdout': FNULL, 'stderr': FNULL}
+            #if not options.is_verbose():
+            #    FNULL = open('/dev/null', 'w')
+            #    kws = {'stdout': FNULL, 'stderr': FNULL}
 
             command = [path] + flags
-            log("Starting %s" % ' '.join(command))
+            info("Starting %s" % ' '.join(command))
 
             if extra_env:
                 kws['env'] = os.environ.copy()
@@ -129,13 +123,13 @@ class Helper:
     def _name_owner_changed_cb(self, name, old_owner, new_owner):
         if name == self.BUS_NAME:
             if old_owner == '' and new_owner != '':
-                log("[%s] appeared in the bus" % self.PROCESS_NAME)
+                info("[%s] appeared in the bus" % self.PROCESS_NAME)
                 self.available = True
             elif old_owner != '' and new_owner == '':
-                log("[%s] disappeared from the bus" % self.PROCESS_NAME)
+                info("[%s] disappeared from the bus" % self.PROCESS_NAME)
                 self.available = False
             else:
-                log("[%s] name change %s -> %s" %
+                info("[%s] name change %s -> %s" %
                     (self.PROCESS_NAME, old_owner, new_owner))
 
             self.loop.quit()
@@ -153,7 +147,7 @@ class Helper:
                         (self.PROCESS_NAME, status))
 
     def _timeout_on_idle_cb(self):
-        log("[%s] Timeout waiting... asumming idle." % self.PROCESS_NAME)
+        info("[%s] Timeout waiting... asumming idle." % self.PROCESS_NAME)
         self.loop.quit()
         self.timeout_id = None
         return False
@@ -176,7 +170,7 @@ class Helper:
                                          dbus_interface="org.freedesktop.DBus")
 
         self.process = self._start_process(extra_env)
-        log('[%s] Started process %i' % (self.PROCESS_NAME, self.process.pid))
+        info('[%s] Started process %i' % (self.PROCESS_NAME, self.process.pid))
 
         self.process_watch_timeout = GLib.timeout_add(
             200, self._process_watch_cb)
@@ -208,12 +202,12 @@ class Helper:
                 time.sleep(0.1)
 
                 if time.time() > (start + REASONABLE_TIMEOUT):
-                    log("[%s] Failed to terminate, sending kill!" %
-                        self.PROCESS_NAME)
+                    warn("[%s] Failed to terminate, sending kill!",
+                         self.PROCESS_NAME)
                     self.process.kill()
                     self.process.wait()
 
-        log("[%s] stopped." % self.PROCESS_NAME)
+        info("[%s] stopped." % self.PROCESS_NAME)
         self.loop.run()
 
         # Disconnect the signals of the next start we get duplicated messages
@@ -225,7 +219,7 @@ class Helper:
         # Name owner changed callback should take us out from this loop
         self.loop.run()
 
-        log("[%s] killed." % self.PROCESS_NAME)
+        info("[%s] killed." % self.PROCESS_NAME)
         self.bus._clean_up_signal_match(self.name_owner_match)
 
 
@@ -274,9 +268,9 @@ class StoreHelper (Helper):
         self.status_iface = dbus.Interface(
             tracker_status, dbus_interface=cfg.STATUS_IFACE)
 
-        log("[%s] booting..." % self.PROCESS_NAME)
+        info("[%s] booting..." % self.PROCESS_NAME)
         self.status_iface.Wait()
-        log("[%s] ready." % self.PROCESS_NAME)
+        info("[%s] ready." % self.PROCESS_NAME)
 
         self.reset_graph_updates_tracking()
         self.graph_updated_handler_id = self.bus.add_signal_receiver(
@@ -347,14 +341,14 @@ class StoreHelper (Helper):
         self.matched_resource_urn = None
         self.matched_resource_id = None
 
-        log("Await new %s (%i existing inserts)" %
-            (rdf_class, len(self.inserts_list)))
+        debug("Await new %s (%i existing inserts)", rdf_class,
+              len(self.inserts_list))
 
         if required_property is not None:
             required_property_id = self.get_resource_id_by_uri(
                 required_property)
-            log("Required property %s id %i" %
-                (required_property, required_property_id))
+            debug("Required property %s id %i", required_property,
+                  required_property_id)
 
         known_subjects = set()
 
@@ -388,18 +382,18 @@ class StoreHelper (Helper):
                         matched_creation = True
                         self.matched_resource_urn = result_set[0][0]
                         self.matched_resource_id = insert[1]
-                        log("Matched creation of resource %s (%i)" %
-                            (self.matched_resource_urn,
-                             self.matched_resource_id))
+                        debug("Matched creation of resource %s (%i)",
+                              self.matched_resource_urn,
+                              self.matched_resource_id)
                         if required_property is not None:
-                            log("Waiting for property %s (%i) to be set" %
-                                (required_property, required_property_id))
+                            debug("Waiting for property %s (%i) to be set",
+                                  required_property, required_property_id)
 
                 if required_property is not None and matched_creation and not matched_required_property:
                     if id == self.matched_resource_id and insert[2] == required_property_id:
                         matched_required_property = True
-                        log("Matched %s %s" %
-                            (self.matched_resource_urn, required_property))
+                        debug("Matched %s %s", self.matched_resource_urn,
+                              required_property)
 
                 if not matched_creation or id != self.matched_resource_id:
                     remaining_events += [insert]
@@ -436,8 +430,8 @@ class StoreHelper (Helper):
         assert (self.deletes_match_function == None)
 
         def find_resource_deletion(deletes_list):
-            log("find_resource_deletion: looking for %i in %s" %
-                (id, deletes_list))
+            debug("find_resource_deletion: looking for %i in %s", id,
+                  deletes_list)
 
             matched = False
             remaining_events = []
@@ -455,8 +449,7 @@ class StoreHelper (Helper):
             exit_loop = matched
             return exit_loop, remaining_events
 
-        log("Await deletion of %i (%i existing)" %
-            (id, len(self.deletes_list)))
+        debug("Await deletion of %i (%i existing)", id, len(self.deletes_list))
 
         (existing_match, self.deletes_list) = find_resource_deletion(
             self.deletes_list)
@@ -490,7 +483,7 @@ class StoreHelper (Helper):
 
             for insert in inserts_list:
                 if insert[1] == subject_id and insert[2] == property_id:
-                    log("Matched property change: %s" % str(insert))
+                    debug("Matched property change: %s", str(insert))
                     matched = True
                 else:
                     remaining_events += [insert]
diff --git a/tests/functional-tests/common/sandbox.py b/tests/functional-tests/common/sandbox.py
index 3879b22..04ed313 100644
--- a/tests/functional-tests/common/sandbox.py
+++ b/tests/functional-tests/common/sandbox.py
@@ -23,6 +23,17 @@ import tempfile
 
 from logging import info
 
+import utils.configuration as cfg
+
+from dconf import DConfClient
+
+
+TEST_ENV_VARS = {
+    "DCONF_PROFILE": os.path.join(cfg.DATADIR, "tracker-tests", "trackertest"),
+    "LC_COLLATE": "en_GB.utf8",
+    "TRACKER_DISABLE_MEEGOTOUCH_LOCALE": "",
+}
+
 
 class TrackerSandbox(object):
     '''FIXME: merge with 'tracker-sandbox' tool
@@ -31,18 +42,32 @@ class TrackerSandbox(object):
     from the host system.
 
     '''
-    def __init__(self, user_dirs=True, message_bus=True):
+    def __init__(self, config=None, user_dirs=True, message_bus=True):
         self.tempdir = None
         self.dbus_process = self.dbus_address = None
 
         self._old_environ = os.environ
 
+        for var, value in TEST_ENV_VARS.iteritems():
+            info("%s=%s", var, value)
+            os.environ[var] = value
+
+        if config:
+            self._apply_settings(config)
+
         if user_dirs:
             self.tempdir = self._sandbox_user_dirs()
 
         if message_bus:
             self.dbus_process, self.dbus_address = self._sandbox_message_bus()
 
+    def _apply_settings(self, settings):
+        for schema_name, contents in settings.iteritems():
+            dconf = DConfClient(schema_name)
+            dconf.reset()
+            for key, value in contents.iteritems():
+                dconf.write(key, value)
+
     def _sandbox_user_dirs(self):
         tempdir = tempfile.mkdtemp(prefix='tracker-test')
 
diff --git a/tests/functional-tests/common/utils/system.py b/tests/functional-tests/common/utils/system.py
index 6aebb21..10787c1 100644
--- a/tests/functional-tests/common/utils/system.py
+++ b/tests/functional-tests/common/utils/system.py
@@ -1,7 +1,6 @@
 #!/usr/bin/python
 import os
 import shutil
-import configuration as cfg
 
 from dconf import DConfClient
 
@@ -60,40 +59,6 @@ class TrackerSystemAbstraction:
 
         helpers.log("[Conf] environment ready")
 
-    def _apply_settings(self, settings):
-        for schema_name, contents in settings.iteritems():
-            dconf = DConfClient(schema_name)
-            dconf.reset()
-            for key, value in contents.iteritems():
-                dconf.write(key, value)
-
-    def tracker_store_stop_brutally(self):
-        self.store.kill()
-
-    def tracker_miner_fs_testing_start(self, config, dbus_address):
-        """
-        Stops any previous instance of the store and miner, calls set_up_environment,
-        and starts a new instance of the store and miner-fs
-        """
-        self.set_up_environment(config, None)
-
-        self.store = helpers.StoreHelper()
-        self.store.start(dbus_address)
-
-        self.extractor = helpers.ExtractorHelper()
-        self.extractor.start(dbus_address)
-
-        self.miner_fs = helpers.MinerFsHelper()
-        self.miner_fs.start(dbus_address)
-
-    def tracker_miner_fs_testing_stop(self):
-        """
-        Stops the extractor, miner-fs and store running
-        """
-        self.extractor.stop()
-        self.miner_fs.stop()
-        self.store.stop()
-
     def tracker_writeback_testing_start(self, config, dbus_address):
         # Start the miner-fs (and store) and then the writeback process
         self.tracker_miner_fs_testing_start(config, dbus_address)
diff --git a/tests/functional-tests/miner/__init__.py b/tests/functional-tests/miner/__init__.py
index 61b258f..660ac3d 100644
--- a/tests/functional-tests/miner/__init__.py
+++ b/tests/functional-tests/miner/__init__.py
@@ -1,3 +1,25 @@
-"""
-This module contains functional tests for Tracker.
-"""
+# Copyright (C) 2014, Sam Thursfield <sam afuera me uk>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
+
+# Including all tests in this file ensures that they can be run with
+# `python -m unittest module` as well as `python -m unittest discover module`.
+
+
+import test_basic
+import test_fts_indexing
+import test_resource_removal
diff --git a/tests/functional-tests/common/utils/minertest.py b/tests/functional-tests/miner/miner_testcase.py
similarity index 84%
rename from tests/functional-tests/common/utils/minertest.py
rename to tests/functional-tests/miner/miner_testcase.py
index aadfa70..1c9d748 100644
--- a/tests/functional-tests/common/utils/minertest.py
+++ b/tests/functional-tests/miner/miner_testcase.py
@@ -1,5 +1,3 @@
-#!/usr/bin/env python
-#
 # Copyright (C) 2010, Nokia <ivan frade nokia com>
 #
 # This program is free software; you can redistribute it and/or
@@ -16,18 +14,21 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301, USA.
-#
-from common.utils import configuration as cfg
-from common.utils.system import TrackerSystemAbstraction
-import unittest as ut
+
 
 from gi.repository import GLib
 
 import shutil
 import os
-import warnings
+import unittest
+
 from itertools import chain
 
+import common.helpers
+
+from common.utils import configuration as cfg
+
+
 MINER_TMP_DIR = cfg.TEST_MONITORED_TMP_DIR
 
 
@@ -53,8 +54,7 @@ CONF_OPTIONS = {
 }
 
 
-class CommonTrackerMinerTest (ut.TestCase):
-
+class MinerTestCase (unittest.TestCase):
     def prepare_directories(self):
         #
         #     ~/test-monitored/
@@ -100,17 +100,26 @@ class CommonTrackerMinerTest (ut.TestCase):
                 shutil.rmtree(dirname)
             os.makedirs(dirname)
 
-        self.system = TrackerSystemAbstraction()
+        self.sandbox = common.sandbox.TrackerSandbox(CONF_OPTIONS)
+        self.store = common.helpers.StoreHelper()
+        self.store.start(self.sandbox)
+
+        self.extractor = common.helpers.ExtractorHelper()
+        self.extractor.start(self.sandbox)
+
+        self.miner_fs = common.helpers.MinerFsHelper()
+        self.miner_fs.start(self.sandbox)
 
-        self.system.tracker_miner_fs_testing_start(CONF_OPTIONS)
-        self.tracker = self.system.store
+        self.tracker = self.store
 
         try:
             self.prepare_directories()
             self.tracker.reset_graph_updates_tracking()
-        except Exception as e:
+        except Exception:
             self.tearDown()
             raise
 
     def tearDown(self):
-        self.system.tracker_miner_fs_testing_stop()
+        self.extractor.stop()
+        self.miner_fs.stop()
+        self.store.stop()
diff --git a/tests/functional-tests/miner/300-miner-basic-ops.py b/tests/functional-tests/miner/test_basic.py
old mode 100755
new mode 100644
similarity index 96%
rename from tests/functional-tests/miner/300-miner-basic-ops.py
rename to tests/functional-tests/miner/test_basic.py
index 4e38562..1c3e60b
--- a/tests/functional-tests/miner/300-miner-basic-ops.py
+++ b/tests/functional-tests/miner/test_basic.py
@@ -1,5 +1,3 @@
-#!/usr/bin/python
-
 # Copyright (C) 2010, Nokia (ivan frade nokia com)
 #
 # This library is free software; you can redistribute it and/or
@@ -21,20 +19,20 @@
 # TODO:
 #     These tests are for files... we need to write them for folders!
 #
+
 """
 Monitor a test directory and copy/move/remove/update files and folders there.
 Check the basic data of the files is updated accordingly in tracker.
 """
+
 import os
 import shutil
 import time
 
-import unittest as ut
-from common.utils.helpers import log
-from common.utils.minertest import CommonTrackerMinerTest, MINER_TMP_DIR, uri, path
+from miner_testcase import MinerTestCase, MINER_TMP_DIR, uri, path
 
 
-class MinerCrawlTest (CommonTrackerMinerTest):
+class MinerCrawlTest (MinerTestCase):
 
     """
     Test cases to check if miner is able to monitor files that are created, deleted or moved
@@ -325,11 +323,3 @@ class MinerCrawlTest (CommonTrackerMinerTest):
         # Check everything is fine
         result = self.__get_text_documents()
         self.assertEquals(len(result), 3)
-
-if __name__ == "__main__":
-    print """
-     Tests for Copy/move/delete operations of FILES between monitored/unmonitored locations.
-
-     We need to do the same for DIRECTORIES!
-    """
-    ut.main()
diff --git a/tests/functional-tests/miner/310-fts-indexing.py 
b/tests/functional-tests/miner/test_fts_indexing.py
old mode 100755
new mode 100644
similarity index 97%
rename from tests/functional-tests/miner/310-fts-indexing.py
rename to tests/functional-tests/miner/test_fts_indexing.py
index 8014ed2..1021a78
--- a/tests/functional-tests/miner/310-fts-indexing.py
+++ b/tests/functional-tests/miner/test_fts_indexing.py
@@ -1,6 +1,5 @@
-#!/usr/bin/python
 #-*- coding: utf-8 -*-
-
+#
 # Copyright (C) 2010, Nokia (ivan frade nokia com)
 #
 # This library is free software; you can redistribute it and/or
@@ -22,22 +21,24 @@
 # TODO:
 #     These tests are for files... we need to write them for folders!
 #
+
 """
 Monitor a directory, copy/move/remove/update text files and check that
 the text contents are updated accordingly in the indexes.
 """
+
+
 import os
 import shutil
 import locale
 import time
 
-import unittest as ut
-from common.utils.helpers import log
-from common.utils.minertest import CommonTrackerMinerTest, MINER_TMP_DIR, uri, path, DEFAULT_TEXT
+
+from miner_testcase import MinerTestCase, MINER_TMP_DIR, uri, path, DEFAULT_TEXT
 from common.utils import configuration as cfg
 
 
-class CommonMinerFTS (CommonTrackerMinerTest):
+class CommonMinerFTS (MinerTestCase):
 
     """
     Superclass to share methods. Shouldn't be run by itself.
@@ -338,7 +339,3 @@ class MinerFTSStopwordsTest (CommonMinerFTS):
 
     # FIXME add all the special character tests!
     # http://git.gnome.org/browse/tracker/commit/?id=81c0d3bd754a6b20ac72323481767dc5b4a6217b
-
-
-if __name__ == "__main__":
-    ut.main()
diff --git a/tests/functional-tests/miner/301-miner-resource-removal.py 
b/tests/functional-tests/miner/test_resource_removal.py
old mode 100755
new mode 100644
similarity index 82%
rename from tests/functional-tests/miner/301-miner-resource-removal.py
rename to tests/functional-tests/miner/test_resource_removal.py
index 0ed2d83..d91f02e
--- a/tests/functional-tests/miner/301-miner-resource-removal.py
+++ b/tests/functional-tests/miner/test_resource_removal.py
@@ -1,5 +1,3 @@
-#!/usr/bin/python
-
 # Copyright (C) 2010, Nokia (ivan frade nokia com)
 #
 # This library is free software; you can redistribute it and/or
@@ -22,38 +20,18 @@ Test that resource removal does not leave debris or clobber too much,
 especially in the case where nie:InformationElement != nie:DataObject
 """
 
-from common.utils import configuration as cfg
-from common.utils.dconf import DConfClient
-from common.utils.helpers import MinerFsHelper, StoreHelper, ExtractorHelper, log
-from common.utils.minertest import CommonTrackerMinerTest, path, uri
-from common.utils.system import TrackerSystemAbstraction
-
 from gi.repository import GLib
 
-import dbus
-from gi.repository import GLib
 import os
-import shutil
-import unittest as ut
-
-MINER_TMP_DIR = cfg.TEST_MONITORED_TMP_DIR
 
+from common.utils import configuration as cfg
+from miner_testcase import MinerTestCase, path, uri
 
-CONF_OPTIONS = {
-    cfg.DCONF_MINER_SCHEMA: {
-        'enable-writeback': GLib.Variant.new_boolean(False),
-        'index-recursive-directories': GLib.Variant.new_strv([MINER_TMP_DIR]),
-        'index-single-directories': GLib.Variant.new_strv([]),
-        'index-optical-discs': GLib.Variant.new_boolean(False),
-        'index-removable-devices': GLib.Variant.new_boolean(False),
-        'throttle': GLib.Variant.new_int32(5),
-    }
-}
 
-REASONABLE_TIMEOUT = 30
+MINER_TMP_DIR = cfg.TEST_MONITORED_TMP_DIR
 
 
-class MinerResourceRemovalTest (CommonTrackerMinerTest):
+class MinerResourceRemovalTest (MinerTestCase):
 
     def prepare_directories(self):
         # Override content from the base class
@@ -141,7 +119,3 @@ class MinerResourceRemovalTest (CommonTrackerMinerTest):
 
         # Check that only the data on the removable volume was deleted
         #self.await_updates (2)
-
-
-if __name__ == "__main__":
-    ut.main()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]