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,11 +42,26 @@ def fakeDestroyRequestedSlapState(): ...@@ -42,11 +42,26 @@ 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:
return ''
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' 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):
SlapOSTestCaseMixinWithAbort.afterSetUp(self) SlapOSTestCaseMixinWithAbort.afterSetUp(self)
...@@ -110,7 +125,7 @@ class TestSlapOSHalJsonStyleMixin(SlapOSTestCaseMixinWithAbort): ...@@ -110,7 +125,7 @@ class TestSlapOSHalJsonStyleMixin(SlapOSTestCaseMixinWithAbort):
_, partition0 =SlapOSTestCaseMixinWithAbort._makeComputeNode( _, partition0 =SlapOSTestCaseMixinWithAbort._makeComputeNode(
self, owner=owner, allocation_scope=allocation_scope self, owner=owner, allocation_scope=allocation_scope
) )
self.partition0 = partition0 self.partition0 = partition0
reference = 'TESTPART-%s' % self.generateNewId() reference = 'TESTPART-%s' % self.generateNewId()
self.partition1 = self.compute_node.newContent( self.partition1 = self.compute_node.newContent(
...@@ -132,7 +147,7 @@ class TestSlapOSHalJsonStyleMixin(SlapOSTestCaseMixinWithAbort): ...@@ -132,7 +147,7 @@ class TestSlapOSHalJsonStyleMixin(SlapOSTestCaseMixinWithAbort):
self.tic() self.tic()
self.changeSkin('Hal') self.changeSkin('Hal')
return network return network
def _makeProject(self): def _makeProject(self):
project = self.portal.project_module.newContent() project = self.portal.project_module.newContent()
project.edit(reference="TESTPROJ-%s" % project.getId()) project.edit(reference="TESTPROJ-%s" % project.getId())
...@@ -188,7 +203,7 @@ class TestInstanceTree_getNewsDict(TestSlapOSHalJsonStyleMixin): ...@@ -188,7 +203,7 @@ class TestInstanceTree_getNewsDict(TestSlapOSHalJsonStyleMixin):
'monitor_url': self.getMonitorUrl(instance_tree), 'monitor_url': self.getMonitorUrl(instance_tree),
'is_slave': 1 'is_slave': 1
} }
self.assertEqual(news_dict, self.assertEqual(news_dict,
_decode_with_json(expected_news_dict)) _decode_with_json(expected_news_dict))
def test_stopped(self): def test_stopped(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 = {
...@@ -417,7 +438,6 @@ class TestComputerNetwork_getNewsDict(TestSlapOSHalJsonStyleMixin): ...@@ -417,7 +438,6 @@ class TestComputerNetwork_getNewsDict(TestSlapOSHalJsonStyleMixin):
'portal_type': network.getPortalType(), 'portal_type': network.getPortalType(),
'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))
...@@ -1506,7 +1526,7 @@ return []""") ...@@ -1506,7 +1526,7 @@ return []""")
self.portal.SoftwareProduct_getSoftwareReleaseAsHateoas("fake", True)) self.portal.SoftwareProduct_getSoftwareReleaseAsHateoas("fake", True))
) )
class TestSoftwareInstance_getAllocationInformation(TestSlapOSHalJsonStyleMixin): class TestSoftwareInstance_getAllocationInformation(TestSlapOSHalJsonStyleMixin):
def test_SoftwareInstance_getAllocationInformation_not_allocated(self): def test_SoftwareInstance_getAllocationInformation_not_allocated(self):
self._makeTree() self._makeTree()
...@@ -1547,4 +1567,4 @@ class TestSoftwareInstance_getAllocationInformation(TestSlapOSHalJsonStyleMixin) ...@@ -1547,4 +1567,4 @@ class TestSoftwareInstance_getAllocationInformation(TestSlapOSHalJsonStyleMixin)
self.login(requester.getUserId()) self.login(requester.getUserId())
self.assertEqual("Restricted information", self.assertEqual("Restricted information",
started_instance.SoftwareInstance_getAllocationInformation()) started_instance.SoftwareInstance_getAllocationInformation())
\ No newline at end of file
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