Commit bda89012 authored by Sam Thursfield's avatar Sam Thursfield

functional-tests: Rewrite DConf code to use GObject introspection

The tests will now detect if the DConf backend is not in use and raise
an error, where previously tests would fail for confusing reasons. Also,
we no longer have to shell out to 'gsettings' to set up the
configuration.

Configuration values now have to be specified as GLib.Variant instances
instead of Python types. That's a bit of a pain. PyGI should have a way
of creating a GVariant from an arbitrary Python value, but I didn't find
one.
parent e9b3bb58
......@@ -27,6 +27,8 @@ from common.utils.dconf import DConfClient
from common.utils.helpers import MinerFsHelper, StoreHelper, ExtractorHelper, log
from common.utils.system import TrackerSystemAbstraction
from gi.repository import GLib
import dbus
from gi.repository import GLib
import os
......@@ -42,14 +44,16 @@ def get_test_uri (filename):
return "file://" + os.path.join (MINER_TMP_DIR, filename)
CONF_OPTIONS = [
(cfg.DCONF_MINER_SCHEMA, "enable-writeback", "false"),
(cfg.DCONF_MINER_SCHEMA, "index-recursive-directories", [MINER_TMP_DIR]),
(cfg.DCONF_MINER_SCHEMA, "index-single-directories", "[]"),
(cfg.DCONF_MINER_SCHEMA, "index-optical-discs", "true"),
(cfg.DCONF_MINER_SCHEMA, "index-removable-devices", "false"),
(cfg.DCONF_MINER_SCHEMA, "throttle", 5)
]
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
......
......@@ -22,18 +22,23 @@ from common.utils.system import TrackerSystemAbstraction
from common.utils.helpers import log
import unittest2 as ut
from gi.repository import GLib
import shutil
import os
import time
APPLICATIONS_TMP_DIR = os.path.join (cfg.TEST_MONITORED_TMP_DIR, "test-applications-monitored")
CONF_OPTIONS = [
(cfg.DCONF_MINER_SCHEMA, "index-recursive-directories", [APPLICATIONS_TMP_DIR]),
(cfg.DCONF_MINER_SCHEMA, "index-single-directories", "[]"),
(cfg.DCONF_MINER_SCHEMA, "index-optical-discs", "false"),
(cfg.DCONF_MINER_SCHEMA, "index-removable-devices", "false")
]
index_dirs = [APPLICATIONS_TMP_DIR]
CONF_OPTIONS = {
cfg.DCONF_MINER_SCHEMA: {
'index-recursive-directories': GLib.Variant.new_strv(index_dirs),
'index-single-directories': GLib.Variant.new_strv([]),
'index-optical-discs': GLib.Variant.new_boolean(False),
'index-removable-devices': GLib.Variant.new_boolean(False),
}
}
# Copy rate, 10KBps (1024b/100ms)
SLOWCOPY_RATE = 1024
......
import subprocess
from gi.repository import GLib
from gi.repository import Gio
import os
from helpers import log
class DConfClient:
class DConfClient(object):
"""
Shamefull implementation until we get GobjectIntrospection on libdconf
Allow changing Tracker configuration in DConf.
Tests should be run with a separate DConf profile so that these changes do
not affect the user's configuration. The 'trackertest' profile exists for
this reason, and the constructor will fail if this isn't the profile in
use, to avoid any risk of modifying or removing your real configuration.
The constructor will fail if DConf is not the default backend, because this
probably indicates that the memory backend is in use. Without DConf the
required configuration changes will not take effect, causing many tests to
break.
"""
def write (self, schema, key, value):
command = ["gsettings", "set", schema, key, str(value)]
FNULL = open('/dev/null', 'w')
cmd = subprocess.Popen (command, stdout=subprocess.PIPE) #, stdout=FNULL, stderr=FNULL)
cmd.wait ()
def read (self, schema, key):
command = ["gsettings", "get", schema, key]
FNULL = open('/dev/null', 'w')
cmd = subprocess.Popen (command, stdout=subprocess.PIPE) #, stdout=FNULL, stderr=FNULL)
return cmd.stdout.readline ()
def reset (self):
profile = os.environ ["DCONF_PROFILE"]
def __init__ (self, schema):
self._settings = Gio.Settings.new(schema)
backend = self._settings.get_property('backend')
self._check_settings_backend_is_dconf(backend)
self._check_using_correct_dconf_profile()
def _check_settings_backend_is_dconf(self, backend):
typename = type(backend).__name__.split('.')[-1]
if typename != 'DConfSettingsBackend':
raise Exception(
"The functional tests require DConf to be the default "
"GSettings backend. Got %s instead." % typename)
def _check_using_correct_dconf_profile(self):
profile = os.environ["DCONF_PROFILE"]
if not os.path.exists(profile):
raise Exception(
"Unable to find DConf profile '%s'. Check that Tracker and "
......@@ -29,6 +44,28 @@ class DConfClient:
assert os.path.basename(profile) == "trackertest"
def write(self, key, value):
"""
Write a settings value.
"""
self._settings.set_value(key, value)
def read(self, schema, key):
"""
Read a settings value.
"""
return self._settings.get_value(key)
def reset(self):
"""
Remove all stored values, resetting configuration to the default.
This can be done by removing the entire 'trackertest' configuration
database.
"""
self._check_using_correct_dconf_profile()
# XDG_CONFIG_HOME is useless, so we use HOME. This code should not be
# needed unless for some reason the test is not being run via the
# 'test-runner.sh' script.
......@@ -39,29 +76,3 @@ class DConfClient:
if os.path.exists (dconf_db):
log ("[Conf] Removing dconf database: " + dconf_db)
os.remove (dconf_db)
if __name__ == "__main__":
SCHEMA_MINER = "org.freedesktop.Tracker.Miner.Files"
os.environ ["DCONF_PROFILE"] = os.path.join (cfg.DATADIR, "tracker-tests",
"trackertest")
dconf = DConfClient ()
value = dconf.read (DConfClient.SCHEMA_MINER, "throttle")
print "Original value:", int (value)
print "Setting 5"
dconf.write (DConfClient.SCHEMA_MINER, "throttle", "5")
value = dconf.read (DConfClient.SCHEMA_MINER, "throttle")
assert int(value) == 5
print "Setting 3"
dconf.write (DConfClient.SCHEMA_MINER, "throttle", "3")
value = dconf.read (DConfClient.SCHEMA_MINER, "throttle")
assert int (value) == 3
print "Now with lists"
dconf.write (DConfClient.SCHEMA_MINER, "index-recursive-directories", ['$HOME/set-with-python'])
value = dconf.read (DConfClient.SCHEMA_MINER, "index-recursive-directories")
print "result", value
......@@ -22,6 +22,8 @@ from common.utils.system import TrackerSystemAbstraction
from common.utils.helpers import StoreHelper
import unittest2 as ut
from gi.repository import GLib
import shutil
import os
......@@ -36,13 +38,16 @@ def uri (filename):
DEFAULT_TEXT = "Some stupid content, to have a test file"
CONF_OPTIONS = [
(cfg.DCONF_MINER_SCHEMA, "index-recursive-directories", [os.path.join (MINER_TMP_DIR, "test-monitored")]),
(cfg.DCONF_MINER_SCHEMA, "index-single-directories", "[]"),
(cfg.DCONF_MINER_SCHEMA, "index-optical-discs", "false"),
(cfg.DCONF_MINER_SCHEMA, "index-removable-devices", "false"),
(cfg.DCONF_MINER_SCHEMA, "throttle", 5)
]
index_dirs = [os.path.join (MINER_TMP_DIR, "test-monitored")]
CONF_OPTIONS = {
cfg.DCONF_MINER_SCHEMA: {
'index-recursive-directories': GLib.Variant.new_strv(index_dirs),
'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),
}
}
class CommonTrackerMinerTest (ut.TestCase):
......
......@@ -37,15 +37,16 @@ class UnableToBootException (Exception):
class TrackerSystemAbstraction:
def set_up_environment (self, gsettings, ontodir):
def set_up_environment (self, settings, ontodir):
"""
Sets up the XDG_*_HOME variables and make sure the directories exist
gsettings is a list of triplets (schema, key, value) that will be set/unset in gsetting
Settings should be a dict mapping schema names to dicts that hold the
settings that should be changed in those schemas. The contents dicts
should map key->value, where key is a key name and value is a suitable
GLib.Variant instance.
"""
assert not gsettings or type(gsettings) is list
helpers.log ("[Conf] Setting test environment...")
for var, directory in TEST_ENV_DIRS.iteritems ():
......@@ -65,14 +66,18 @@ class TrackerSystemAbstraction:
os.environ [var] = value
# Previous loop should have set DCONF_PROFILE to the test location
if gsettings:
self.dconf = DConfClient ()
self.dconf.reset ()
for (schema, key, value) in gsettings:
self.dconf.write (schema, key, value)
if settings is not None:
self._apply_settings(settings)
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 unset_up_environment (self):
"""
Unset the XDG_*_HOME variables from the environment
......
......@@ -17,6 +17,9 @@
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
from gi.repository import GLib
from common.utils.system import TrackerSystemAbstraction
import shutil
import unittest2 as ut
......@@ -31,12 +34,16 @@ TEST_FILE_PNG = "writeback-test-4.png"
WRITEBACK_TMP_DIR = os.path.join (cfg.TEST_MONITORED_TMP_DIR, "writeback")
CONF_OPTIONS = [
(cfg.DCONF_MINER_SCHEMA, "index-recursive-directories", [WRITEBACK_TMP_DIR]),
(cfg.DCONF_MINER_SCHEMA, "index-single-directories", "[]"),
(cfg.DCONF_MINER_SCHEMA, "index-optical-discs", "false"),
(cfg.DCONF_MINER_SCHEMA, "index-removable-devices", "false")
]
index_dirs = [WRITEBACK_TMP_DIR]
CONF_OPTIONS = {
cfg.DCONF_MINER_SCHEMA: {
'index-recursive-directories': GLib.Variant.new_strv(index_dirs),
'index-single-directories': GLib.Variant.new_strv([]),
'index-optical-discs': GLib.Variant.new_boolean(False),
'index-removable-devices': GLib.Variant.new_boolean(False),
}
}
def uri (filename):
return "file://" + os.path.join (WRITEBACK_TMP_DIR, filename)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment