Commit 80ad0475 authored by Roque's avatar Roque

slapos_jio: fix monitor url connection parameters

- make url script return no url if there are not connection parameters
- update tests accordingly
parent f9533931
import json
from ZTUtils import make_query from ZTUtils import make_query
def ascii_encode_dict(data):
ascii_encode = lambda x: x.encode('ascii')
return dict(map(ascii_encode, pair) for pair in data.items())
# TODO how to avoid hardcode here? from InstanceTree_getConnectionParameterList? # TODO how to avoid hardcode here? from InstanceTree_getConnectionParameterList?
base_url = 'https://monitor.app.officejs.com/#/?' base_url = 'https://monitor.app.officejs.com/#/?'
url_parameter_kw = { 'page': 'ojsm_dispatch' } url_parameter_kw = { 'page': 'ojsm_dispatch' }
...@@ -6,7 +12,7 @@ url_parameter_kw = { 'page': 'ojsm_dispatch' } ...@@ -6,7 +12,7 @@ url_parameter_kw = { 'page': 'ojsm_dispatch' }
instance_tree = context instance_tree = context
if context.getPortalType() in ["Software Instance", "Slave Instance"]: if context.getPortalType() in ["Software Instance", "Slave Instance"]:
instance_tree = context.getSpecialiseValue(portal_type="Instance Tree") instance_tree = context.getSpecialiseValue(portal_type="Instance Tree")
connection_parameter_dict = instance_tree.InstanceTree_getMonitorParameterDict() connection_parameter_dict = json.loads(instance_tree.InstanceTree_getMonitorParameterDict(), object_hook=ascii_encode_dict)
if all(key in connection_parameter_dict for key in ('username', 'password', 'url')): if all(key in connection_parameter_dict for key in ('username', 'password', 'url')):
url_parameter_kw['username'] = connection_parameter_dict['username'] url_parameter_kw['username'] = connection_parameter_dict['username']
url_parameter_kw['password'] = connection_parameter_dict['password'] url_parameter_kw['password'] = connection_parameter_dict['password']
...@@ -18,6 +24,9 @@ if context.getPortalType() == "Instance Tree": ...@@ -18,6 +24,9 @@ if context.getPortalType() == "Instance Tree":
return connection_parameter['connection_value'] return connection_parameter['connection_value']
url_parameter_kw['query'] = 'portal_type:"Instance Tree" AND title:"%s"' % context.getTitle() url_parameter_kw['query'] = 'portal_type:"Instance Tree" AND title:"%s"' % context.getTitle()
if not(connection_parameter_dict):
return ''
if context.getPortalType() in ["Software Instance", "Slave Instance"]: if context.getPortalType() in ["Software Instance", "Slave Instance"]:
url_parameter_kw['query'] = 'portal_type:"Software Instance" AND title:"%s" AND ' % context.getTitle() + 'specialise_title:"%s"' % context.getSpecialiseTitle() url_parameter_kw['query'] = 'portal_type:"Software Instance" AND title:"%s" AND ' % context.getTitle() + 'specialise_title:"%s"' % context.getSpecialiseTitle()
......
...@@ -42,10 +42,25 @@ def fakeDestroyRequestedSlapState(): ...@@ -42,10 +42,25 @@ def fakeDestroyRequestedSlapState():
class TestSlapOSHalJsonStyleMixin(SlapOSTestCaseMixinWithAbort): class TestSlapOSHalJsonStyleMixin(SlapOSTestCaseMixinWithAbort):
def getMonitorUrl(self, context): def getMonitorUrl(self, context):
if context.getSlapState() == fakeDestroyRequestedSlapState():
return ''
if context.getPortalType() in ["Software Instance", "Slave Instance"]: if context.getPortalType() in ["Software Instance", "Slave Instance"]:
return 'https://monitor.app.officejs.com/#/?query=portal_type%3A%22Software%20Instance%22%20AND%20title%3A%22Template%20Software%20Instance%22%20AND%20specialise_title%3A%22Template%20Instance%20Tree%22&page=ojsm_dispatch' connection = context.getConnectionXmlAsDict()
if connection and connection.has_key('monitor-user') and \
connection.has_key('monitor-password') and \
connection.has_key('monitor-base-url'):
return 'https://monitor.app.officejs.com/#/?username=testuser&url=softinst-monitored/public/feeds&password=testpass&page=ojsm_dispatch&query=portal_type%3A%22Software%20Instance%22%20AND%20title%3A%22Template%20Software%20Instance%22%20AND%20specialise_title%3A%22Template%20Instance%20Tree%22'
else: else:
return 'https://monitor.app.officejs.com/#/?query=portal_type%3A%22Instance%20Tree%22%20AND%20title%3A%22Template%20Instance%20Tree%22&page=ojsm_dispatch' return ''
else:
soft_inst = context.getSuccessorValue()
if soft_inst:
connection = soft_inst.getConnectionXmlAsDict()
if connection and connection.has_key('monitor-user') and \
connection.has_key('monitor-password') and \
connection.has_key('monitor-base-url'):
return 'https://monitor.app.officejs.com/#/?username=testuser&url=softinst-monitored/public/feeds&password=testpass&page=ojsm_dispatch&query=portal_type%3A%22Instance%20Tree%22%20AND%20title%3A%22Template%20Instance%20Tree%22'
return ''
maxDiff = None maxDiff = None
def afterSetUp(self): def afterSetUp(self):
...@@ -223,6 +238,7 @@ class TestInstanceTree_getNewsDict(TestSlapOSHalJsonStyleMixin): ...@@ -223,6 +238,7 @@ class TestInstanceTree_getNewsDict(TestSlapOSHalJsonStyleMixin):
instance_tree = self._makeInstanceTree() instance_tree = self._makeInstanceTree()
instance = self._makeInstance() instance = self._makeInstance()
instance.edit(specialise_value=instance_tree) instance.edit(specialise_value=instance_tree)
instance_tree.edit(successor_value=instance)
self.tic() self.tic()
self.changeSkin('Hal') self.changeSkin('Hal')
news_dict = instance_tree.InstanceTree_getNewsDict() news_dict = instance_tree.InstanceTree_getNewsDict()
...@@ -248,6 +264,7 @@ class TestInstanceTree_getNewsDict(TestSlapOSHalJsonStyleMixin): ...@@ -248,6 +264,7 @@ class TestInstanceTree_getNewsDict(TestSlapOSHalJsonStyleMixin):
instance_tree = self._makeInstanceTree() instance_tree = self._makeInstanceTree()
instance = self._makeSlaveInstance() instance = self._makeSlaveInstance()
instance.edit(specialise_value=instance_tree) instance.edit(specialise_value=instance_tree)
instance_tree.edit(successor_value=instance)
self.tic() self.tic()
self.changeSkin('Hal') self.changeSkin('Hal')
news_dict = instance_tree.InstanceTree_getNewsDict() news_dict = instance_tree.InstanceTree_getNewsDict()
...@@ -265,9 +282,9 @@ class TestInstanceTree_getNewsDict(TestSlapOSHalJsonStyleMixin): ...@@ -265,9 +282,9 @@ class TestInstanceTree_getNewsDict(TestSlapOSHalJsonStyleMixin):
instance_tree = self._makeInstanceTree() instance_tree = self._makeInstanceTree()
instance = self._makeInstance() instance = self._makeInstance()
instance.edit(specialise_value=instance_tree) instance.edit(specialise_value=instance_tree)
instance_tree.edit(successor_value=instance)
instance0 = self._makeInstance() instance0 = self._makeInstance()
instance0.edit(specialise_value=instance_tree) instance0.edit(specialise_value=instance_tree)
self.tic() self.tic()
self.changeSkin('Hal') self.changeSkin('Hal')
news_dict = instance_tree.InstanceTree_getNewsDict() news_dict = instance_tree.InstanceTree_getNewsDict()
...@@ -305,6 +322,7 @@ class TestSoftwareInstance_getNewsDict(TestSlapOSHalJsonStyleMixin): ...@@ -305,6 +322,7 @@ class TestSoftwareInstance_getNewsDict(TestSlapOSHalJsonStyleMixin):
instance_tree = self._makeInstanceTree() instance_tree = self._makeInstanceTree()
instance = self._makeInstance() instance = self._makeInstance()
instance.edit(specialise_value=instance_tree) instance.edit(specialise_value=instance_tree)
instance_tree.edit(successor_value=instance)
self._logFakeAccess(instance) self._logFakeAccess(instance)
news_dict = instance.SoftwareInstance_getNewsDict() news_dict = instance.SoftwareInstance_getNewsDict()
expected_news_dict = {'created_at': self.created_at, expected_news_dict = {'created_at': self.created_at,
...@@ -325,6 +343,7 @@ class TestSoftwareInstance_getNewsDict(TestSlapOSHalJsonStyleMixin): ...@@ -325,6 +343,7 @@ class TestSoftwareInstance_getNewsDict(TestSlapOSHalJsonStyleMixin):
instance_tree = self._makeInstanceTree() instance_tree = self._makeInstanceTree()
instance = self._makeInstance() instance = self._makeInstance()
instance.edit(specialise_value=instance_tree) instance.edit(specialise_value=instance_tree)
instance_tree.edit(successor_value=instance)
self.changeSkin('Hal') self.changeSkin('Hal')
news_dict = instance.SoftwareInstance_getNewsDict() news_dict = instance.SoftwareInstance_getNewsDict()
...@@ -345,13 +364,14 @@ class TestSoftwareInstance_getNewsDict(TestSlapOSHalJsonStyleMixin): ...@@ -345,13 +364,14 @@ class TestSoftwareInstance_getNewsDict(TestSlapOSHalJsonStyleMixin):
instance_tree = self._makeInstanceTree() instance_tree = self._makeInstanceTree()
instance = self._makeSlaveInstance() instance = self._makeSlaveInstance()
instance.edit(specialise_value=instance_tree) instance.edit(specialise_value=instance_tree)
instance_tree.edit(successor_value=instance)
news_dict = instance.SoftwareInstance_getNewsDict() news_dict = instance.SoftwareInstance_getNewsDict()
expected_news_dict = { expected_news_dict = {
'portal_type': instance.getPortalType(), 'portal_type': instance.getPortalType(),
'reference': instance.getReference(), 'reference': instance.getReference(),
'is_slave': 1, 'is_slave': 1,
'text': '#nodata is a slave %s' % instance.getReference(), 'text': '#nodata is a slave %s' % instance.getReference(),
'monitor_url': 'https://monitor.app.officejs.com/#/?query=portal_type%3A%22Software%20Instance%22%20AND%20title%3A%22Template%20Slave%20Instance%22%20AND%20specialise_title%3A%22Template%20Instance%20Tree%22&page=ojsm_dispatch', 'monitor_url': 'https://monitor.app.officejs.com/#/?username=testuser&url=softinst-monitored/public/feeds&password=testpass&page=ojsm_dispatch&query=portal_type%3A%22Software%20Instance%22%20AND%20title%3A%22Template%20Slave%20Instance%22%20AND%20specialise_title%3A%22Template%20Instance%20Tree%22',
'user': 'SlapOS Master'} 'user': 'SlapOS Master'}
self.assertEqual(_decode_with_json(news_dict), self.assertEqual(_decode_with_json(news_dict),
_decode_with_json(expected_news_dict)) _decode_with_json(expected_news_dict))
...@@ -360,6 +380,7 @@ class TestSoftwareInstance_getNewsDict(TestSlapOSHalJsonStyleMixin): ...@@ -360,6 +380,7 @@ class TestSoftwareInstance_getNewsDict(TestSlapOSHalJsonStyleMixin):
instance_tree = self._makeInstanceTree() instance_tree = self._makeInstanceTree()
instance = self._makeInstance() instance = self._makeInstance()
instance.edit(specialise_value=instance_tree) instance.edit(specialise_value=instance_tree)
instance_tree.edit(successor_value=instance)
instance.getSlapState = fakeStopRequestedSlapState instance.getSlapState = fakeStopRequestedSlapState
news_dict = instance.SoftwareInstance_getNewsDict() news_dict = instance.SoftwareInstance_getNewsDict()
expected_news_dict = { expected_news_dict = {
...@@ -418,7 +439,6 @@ class TestComputerNetwork_getNewsDict(TestSlapOSHalJsonStyleMixin): ...@@ -418,7 +439,6 @@ class TestComputerNetwork_getNewsDict(TestSlapOSHalJsonStyleMixin):
'reference': network.getReference() 'reference': network.getReference()
} }
self.assertEqual(_decode_with_json(news_dict), self.assertEqual(_decode_with_json(news_dict),
_decode_with_json(expected_news_dict)) _decode_with_json(expected_news_dict))
......
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