memory: bug fix to not register twice frontends parameters + added some tests for param update
This commit is contained in:
parent
ff81f76184
commit
7ea4dca1b0
4 changed files with 66 additions and 30 deletions
|
|
@ -296,7 +296,7 @@ class Params(object):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def updateParams(self, xml, security_limit=NO_SECURITY_LIMIT, app=''):
|
def updateParams(self, xml, security_limit=NO_SECURITY_LIMIT, app=''):
|
||||||
"""import xml in parameters, do nothing if the param already exist
|
"""import xml in parameters, update if the param already exists
|
||||||
If security_limit is specified and greater than -1, the parameters
|
If security_limit is specified and greater than -1, the parameters
|
||||||
that have a security level greater than security_limit are skipped.
|
that have a security level greater than security_limit are skipped.
|
||||||
@param xml: parameters in xml form
|
@param xml: parameters in xml form
|
||||||
|
|
@ -339,7 +339,7 @@ class Params(object):
|
||||||
tgt_parent.appendChild(child)
|
tgt_parent.appendChild(child)
|
||||||
else:
|
else:
|
||||||
if child.nodeName == "param":
|
if child.nodeName == "param":
|
||||||
# The child update an existing parameter, we replace the node
|
# The child updates an existing parameter, we replace the node
|
||||||
tgt_parent.replaceChild(child, node)
|
tgt_parent.replaceChild(child, node)
|
||||||
else:
|
else:
|
||||||
# the node already exists, we recurse 1 more level
|
# the node already exists, we recurse 1 more level
|
||||||
|
|
@ -361,10 +361,11 @@ class Params(object):
|
||||||
warning(_("Trying to register frontends parameters with no specified app: aborted"))
|
warning(_("Trying to register frontends parameters with no specified app: aborted"))
|
||||||
return
|
return
|
||||||
if not hasattr(self, "frontends_cache"):
|
if not hasattr(self, "frontends_cache"):
|
||||||
self.frontends_cache = {}
|
self.frontends_cache = []
|
||||||
if app in self.frontends_cache:
|
if app in self.frontends_cache:
|
||||||
warning(_("Trying to register twice frontends parameters for %(app)s: aborted" % {"app": app}))
|
debug(_("Trying to register twice frontends parameters for %(app)s: aborted" % {"app": app}))
|
||||||
return
|
return
|
||||||
|
self.frontends_cache.append(app)
|
||||||
self.updateParams(xml, security_limit, app)
|
self.updateParams(xml, security_limit, app)
|
||||||
debug("Frontends parameters registered for %(app)s" % {'app': app})
|
debug("Frontends parameters registered for %(app)s" % {'app': app})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,5 +39,6 @@ class Const(object):
|
||||||
|
|
||||||
# To test frontend parameters
|
# To test frontend parameters
|
||||||
APP_NAME = "dummy_frontend"
|
APP_NAME = "dummy_frontend"
|
||||||
ENABLE_UNIBOX_KEY = D_("Composition")
|
COMPOSITION_KEY = D_("Composition")
|
||||||
ENABLE_UNIBOX_PARAM = D_("Enable unibox")
|
ENABLE_UNIBOX_PARAM = D_("Enable unibox")
|
||||||
|
PARAM_IN_QUOTES = D_("'Wysiwyg' edition")
|
||||||
|
|
|
||||||
|
|
@ -240,6 +240,7 @@ class FakeMemory(Memory):
|
||||||
re-initialise the memory first to not fake the result."""
|
re-initialise the memory first to not fake the result."""
|
||||||
self.params.load_default_params()
|
self.params.load_default_params()
|
||||||
self.params.params.clear()
|
self.params.params.clear()
|
||||||
|
self.params.frontends_cache = []
|
||||||
self.entities_data = {}
|
self.entities_data = {}
|
||||||
self.server_features = {}
|
self.server_features = {}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,33 +31,41 @@ class MemoryTest(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.host = helpers.FakeSAT()
|
self.host = helpers.FakeSAT()
|
||||||
|
|
||||||
def _getParamXML(self, name=None, category=None, security_level=None):
|
def _getParamXML(self, param="1", security_level=None):
|
||||||
if not name:
|
def getParam(name):
|
||||||
name = Const.ENABLE_UNIBOX_PARAM
|
return """
|
||||||
if not category:
|
<param name="%(param_name)s" label="%(param_label)s" value="true" type="bool" %(security)s/>
|
||||||
category = Const.ENABLE_UNIBOX_KEY
|
""" % {'param_name': name,
|
||||||
|
'param_label': _(name),
|
||||||
|
'security': '' if security_level is None else ('security="%d"' % security_level)
|
||||||
|
}
|
||||||
|
if param == "1":
|
||||||
|
params = getParam(Const.ENABLE_UNIBOX_PARAM)
|
||||||
|
elif param == "2":
|
||||||
|
params = getParam(Const.PARAM_IN_QUOTES)
|
||||||
|
else:
|
||||||
|
params = getParam(Const.ENABLE_UNIBOX_PARAM) + getParam(Const.PARAM_IN_QUOTES)
|
||||||
return """
|
return """
|
||||||
<params>
|
<params>
|
||||||
<individual>
|
<individual>
|
||||||
<category name="%(category_name)s" label="%(category_label)s">
|
<category name="%(category_name)s" label="%(category_label)s">
|
||||||
<param name="%(param_name)s" label="%(param_label)s" value="true" type="bool" %(security)s/>
|
%(params)s
|
||||||
</category>
|
</category>
|
||||||
</individual>
|
</individual>
|
||||||
</params>
|
</params>
|
||||||
""" % {
|
""" % {
|
||||||
'category_name': category,
|
'category_name': Const.COMPOSITION_KEY,
|
||||||
'category_label': _(category),
|
'category_label': _(Const.COMPOSITION_KEY),
|
||||||
'param_name': name,
|
'params': params
|
||||||
'param_label': _(name),
|
|
||||||
'security': '' if security_level is None else ('security="%d"' % security_level)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def _paramExists(self, name=None, category=None, src=None):
|
def _paramExists(self, param="1", src=None):
|
||||||
"""@return: True is the param (category, name) exists"""
|
"""@return: True is the param (category, name) exists"""
|
||||||
if not name:
|
if param == "1":
|
||||||
name = Const.ENABLE_UNIBOX_PARAM
|
name = Const.ENABLE_UNIBOX_PARAM
|
||||||
if not category:
|
else:
|
||||||
category = Const.ENABLE_UNIBOX_KEY
|
name = Const.PARAM_IN_QUOTES
|
||||||
|
category = Const.COMPOSITION_KEY
|
||||||
if src is None:
|
if src is None:
|
||||||
src = self.host.memory.params.dom.documentElement
|
src = self.host.memory.params.dom.documentElement
|
||||||
for type_node in src.childNodes:
|
for type_node in src.childNodes:
|
||||||
|
|
@ -74,28 +82,28 @@ class MemoryTest(unittest.TestCase):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def assertParam_generic(self, src=None, exists=True, deferred=False):
|
def assertParam_generic(self, param="1", src=None, exists=True, deferred=False):
|
||||||
msg = "Expected parameter not found!\n" if exists else "Unexpected parameter found!\n"
|
msg = "Expected parameter not found!\n" if exists else "Unexpected parameter found!\n"
|
||||||
if deferred == False:
|
if deferred == False:
|
||||||
# in this stack we can see the line where the error came from,
|
# in this stack we can see the line where the error came from,
|
||||||
# if limit=5, 6 is not enough you can increase the value
|
# if limit=5, 6 is not enough you can increase the value
|
||||||
msg += "\n".join(traceback.format_stack(limit=5 if exists else 6))
|
msg += "\n".join(traceback.format_stack(limit=5 if exists else 6))
|
||||||
assertion = self._paramExists(src=src)
|
assertion = self._paramExists(param, src)
|
||||||
getattr(self, "assert%s" % exists)(assertion, msg)
|
getattr(self, "assert%s" % exists)(assertion, msg)
|
||||||
|
|
||||||
def assertParamExists(self, src=None):
|
def assertParamExists(self, param="1", src=None):
|
||||||
self.assertParam_generic(src, True)
|
self.assertParam_generic(param, src, True)
|
||||||
|
|
||||||
def assertParamNotExists(self, src=None):
|
def assertParamNotExists(self, param="1", src=None):
|
||||||
self.assertParam_generic(src, False)
|
self.assertParam_generic(param, src, False)
|
||||||
|
|
||||||
def assertParamExists_async(self, src):
|
def assertParamExists_async(self, src, param="1"):
|
||||||
"""@param src: a deferred result from Memory.getParams"""
|
"""@param src: a deferred result from Memory.getParams"""
|
||||||
self.assertParam_generic(minidom.parseString(src.encode("utf-8")), True, True)
|
self.assertParam_generic(param, minidom.parseString(src.encode("utf-8")), True, True)
|
||||||
|
|
||||||
def assertParamNotExists_async(self, src):
|
def assertParamNotExists_async(self, src, param="1"):
|
||||||
"""@param src: a deferred result from Memory.getParams"""
|
"""@param src: a deferred result from Memory.getParams"""
|
||||||
self.assertParam_generic(minidom.parseString(src.encode("utf-8")), False, True)
|
self.assertParam_generic(param, minidom.parseString(src.encode("utf-8")), False, True)
|
||||||
|
|
||||||
def _getParams(self, security_limit, app='', profile_key='@NONE@'):
|
def _getParams(self, security_limit, app='', profile_key='@NONE@'):
|
||||||
if profile_key == '@NONE@':
|
if profile_key == '@NONE@':
|
||||||
|
|
@ -112,6 +120,29 @@ class MemoryTest(unittest.TestCase):
|
||||||
self.host.memory.updateParams(self._getParamXML())
|
self.host.memory.updateParams(self._getParamXML())
|
||||||
self.assertEqual(previous.toxml().encode("utf-8"), self.host.memory.params.dom.toxml().encode("utf-8"))
|
self.assertEqual(previous.toxml().encode("utf-8"), self.host.memory.params.dom.toxml().encode("utf-8"))
|
||||||
|
|
||||||
|
self.host.memory.init()
|
||||||
|
# check successive updates (without intersection)
|
||||||
|
self.host.memory.updateParams(self._getParamXML('1'))
|
||||||
|
self.assertParamExists("1")
|
||||||
|
self.assertParamNotExists("2")
|
||||||
|
self.host.memory.updateParams(self._getParamXML('2'))
|
||||||
|
self.assertParamExists("1")
|
||||||
|
self.assertParamExists("2")
|
||||||
|
|
||||||
|
previous = self.host.memory.params.dom.cloneNode(True) # save for later
|
||||||
|
|
||||||
|
self.host.memory.init()
|
||||||
|
# check successive updates (with intersection)
|
||||||
|
self.host.memory.updateParams(self._getParamXML('1'))
|
||||||
|
self.assertParamExists("1")
|
||||||
|
self.assertParamNotExists("2")
|
||||||
|
self.host.memory.updateParams(self._getParamXML('both'))
|
||||||
|
self.assertParamExists("1")
|
||||||
|
self.assertParamExists("2")
|
||||||
|
|
||||||
|
# successive updates with or without intersection should have the same result
|
||||||
|
self.assertEqual(previous.toxml().encode("utf-8"), self.host.memory.params.dom.toxml().encode("utf-8"))
|
||||||
|
|
||||||
def test_getParams(self):
|
def test_getParams(self):
|
||||||
# tests with no security level on the parameter (most secure)
|
# tests with no security level on the parameter (most secure)
|
||||||
params = self._getParamXML()
|
params = self._getParamXML()
|
||||||
|
|
@ -155,6 +186,7 @@ class MemoryTest(unittest.TestCase):
|
||||||
self.host.memory.init()
|
self.host.memory.init()
|
||||||
register(params, 1, Const.APP_NAME)
|
register(params, 1, Const.APP_NAME)
|
||||||
self.assertParamNotExists()
|
self.assertParamNotExists()
|
||||||
|
|
||||||
# tests with security level 0 on the parameter (not secure)
|
# tests with security level 0 on the parameter (not secure)
|
||||||
params = self._getParamXML(security_level=0)
|
params = self._getParamXML(security_level=0)
|
||||||
self.host.memory.init()
|
self.host.memory.init()
|
||||||
|
|
@ -166,6 +198,7 @@ class MemoryTest(unittest.TestCase):
|
||||||
self.host.memory.init()
|
self.host.memory.init()
|
||||||
register(params, 1, Const.APP_NAME)
|
register(params, 1, Const.APP_NAME)
|
||||||
self.assertParamExists()
|
self.assertParamExists()
|
||||||
|
|
||||||
# tests with security level 1 on the parameter (more secure)
|
# tests with security level 1 on the parameter (more secure)
|
||||||
params = self._getParamXML(security_level=1)
|
params = self._getParamXML(security_level=1)
|
||||||
self.host.memory.init()
|
self.host.memory.init()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue