Commit bfb0168f authored by Cédric de Saint Martin's avatar Cédric de Saint Martin

slapos.slap: add getHateoasUrl support.

parent 45cbfc0c
...@@ -262,7 +262,7 @@ class OpenOrder(SlapRequester): ...@@ -262,7 +262,7 @@ class OpenOrder(SlapRequester):
def getInformation(self, partition_reference): def getInformation(self, partition_reference):
if not getattr(self, '_hateoas_navigator', None): if not getattr(self, '_hateoas_navigator', None):
raise Exception('SlapOS Master REST URL (master_rest_url) has not been configured.') raise Exception('SlapOS Master Hateoas API required for this operation is not availble.')
raw_information = self._hateoas_navigator.getHostingSubscriptionRootSoftwareInstanceInformation(partition_reference) raw_information = self._hateoas_navigator.getHostingSubscriptionRootSoftwareInstanceInformation(partition_reference)
software_instance = SoftwareInstance() software_instance = SoftwareInstance()
# XXX redefine SoftwareInstance to be more consistent # XXX redefine SoftwareInstance to be more consistent
...@@ -490,7 +490,7 @@ class ComputerPartition(SlapRequester): ...@@ -490,7 +490,7 @@ class ComputerPartition(SlapRequester):
in the Instance tree of the current Computer Partition. in the Instance tree of the current Computer Partition.
""" """
if not getattr(self, '_hateoas_navigator', None): if not getattr(self, '_hateoas_navigator', None):
raise Exception('SlapOS Master REST URL (master_rest_url) has not been configured.') raise Exception('SlapOS Master Hateoas API required for this operation is not availble.')
return self._hateoas_navigator.getSoftwareReleaseInformation(partition_reference) return self._hateoas_navigator.getSoftwareReleaseInformation(partition_reference)
def getId(self): def getId(self):
...@@ -710,6 +710,11 @@ class slap: ...@@ -710,6 +710,11 @@ class slap:
self._connection_helper = ConnectionHelper(slapgrid_uri, key_file, cert_file, master_ca_file, timeout) self._connection_helper = ConnectionHelper(slapgrid_uri, key_file, cert_file, master_ca_file, timeout)
if not slapgrid_rest_uri:
try:
slapgrid_rest_uri = self._connection_helper.GET('getHateoasUrl')
except:
pass
if slapgrid_rest_uri: if slapgrid_rest_uri:
self._hateoas_navigator = HateoasNavigator( self._hateoas_navigator = HateoasNavigator(
slapgrid_rest_uri, slapgrid_rest_uri,
...@@ -795,7 +800,7 @@ class slap: ...@@ -795,7 +800,7 @@ class slap:
def getOpenOrderDict(self): def getOpenOrderDict(self):
if not getattr(self, '_hateoas_navigator', None): if not getattr(self, '_hateoas_navigator', None):
raise Exception('SlapOS Master REST URL (master_rest_url) has not been configured.') raise Exception('SlapOS Master Hateoas API required for this operation is not availble.')
return self._hateoas_navigator.getHostingSubscriptionDict() return self._hateoas_navigator.getHostingSubscriptionDict()
......
...@@ -335,6 +335,61 @@ class TestSlap(SlapMixin): ...@@ -335,6 +335,61 @@ class TestSlap(SlapMixin):
self.slap.getSoftwareReleaseListFromSoftwareProduct self.slap.getSoftwareReleaseListFromSoftwareProduct
) )
def test_initializeConnection_getHateoasUrl(self):
"""
Test that by default, slap will try to fetch Hateoas URL from XML/RPC URL.
"""
hateoas_url = 'foo'
def handler(url, req):
qs = urlparse.parse_qs(url.query)
if (url.path == '/getHateoasUrl'):
return {
'status_code': 200,
'content': hateoas_url
}
with httmock.HTTMock(handler):
self.slap.initializeConnection('http://bar')
self.assertEqual(
self.slap._hateoas_navigator.slapos_master_hateoas_uri,
hateoas_url
)
def test_initializeConnection_specifiedHateoasUrl(self):
"""
Test that if rest URL is specified, slap will NOT try to fetch
Hateoas URL from XML/RPC URL.
"""
hateoas_url = 'foo'
def handler(url, req):
qs = urlparse.parse_qs(url.query)
if (url.path == '/getHateoasUrl'):
self.fail('slap should not have contacted master to get Hateoas URL.')
with httmock.HTTMock(handler):
self.slap.initializeConnection('http://bar', slapgrid_rest_uri=hateoas_url)
self.assertEqual(
self.slap._hateoas_navigator.slapos_master_hateoas_uri,
hateoas_url
)
def test_initializeConnection_noHateoasUrl(self):
"""
Test that if no rest URL is specified and master does not know about rest,
it still work.
"""
hateoas_url = 'foo'
def handler(url, req):
qs = urlparse.parse_qs(url.query)
if (url.path == '/getHateoasUrl'):
return {
'status_code': 404,
}
with httmock.HTTMock(handler):
self.slap.initializeConnection('http://bar')
self.assertEqual(None, getattr(self.slap, '_hateoas_navigator', None))
class TestComputer(SlapMixin): class TestComputer(SlapMixin):
""" """
Tests slapos.slap.slap.Computer class functionality Tests slapos.slap.slap.Computer class functionality
......
...@@ -638,7 +638,7 @@ chmod 755 etc/run/wrapper ...@@ -638,7 +638,7 @@ chmod 755 etc/run/wrapper
'etc', 'software_release', 'worked', '.slapos-retention-lock-delay']) 'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertLogContent(wrapper_log, 'Signal handler called with signal 15') self.assertLogContent(wrapper_log, 'Signal handler called with signal 15')
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['/getFullComputerInformation', '/availableComputerPartition', ['/getHateoasUrl', '/getFullComputerInformation', '/availableComputerPartition',
'/stoppedComputerPartition']) '/stoppedComputerPartition'])
self.assertEqual(instance.state, 'stopped') self.assertEqual(instance.state, 'stopped')
...@@ -698,7 +698,7 @@ exit 1 ...@@ -698,7 +698,7 @@ exit 1
'etc', 'software_release', 'worked', '.slapos-retention-lock-delay']) 'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertLogContent(wrapper_log, 'Signal handler called with signal 15') self.assertLogContent(wrapper_log, 'Signal handler called with signal 15')
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['/getFullComputerInformation', ['/getHateoasUrl', '/getFullComputerInformation',
'/softwareInstanceError']) '/softwareInstanceError'])
self.assertEqual(instance.state, 'started') self.assertEqual(instance.state, 'started')
...@@ -734,7 +734,7 @@ exit 1 ...@@ -734,7 +734,7 @@ exit 1
wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log') wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log')
self.assertLogContent(wrapper_log, 'Working') self.assertLogContent(wrapper_log, 'Working')
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['/getFullComputerInformation', '/availableComputerPartition', ['/getHateoasUrl', '/getFullComputerInformation', '/availableComputerPartition',
'/startedComputerPartition']) '/startedComputerPartition'])
self.assertEqual('started', instance.state) self.assertEqual('started', instance.state)
...@@ -1196,9 +1196,12 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1196,9 +1196,12 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
self.assertEqual(self.launchSlapgrid(), slapgrid.SLAPGRID_SUCCESS) self.assertEqual(self.launchSlapgrid(), slapgrid.SLAPGRID_SUCCESS)
self.assertEqual(self.launchSlapgrid(), slapgrid.SLAPGRID_SUCCESS) self.assertEqual(self.launchSlapgrid(), slapgrid.SLAPGRID_SUCCESS)
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['/getFullComputerInformation', '/availableComputerPartition', ['/getHateoasUrl',
'/stoppedComputerPartition', '/getFullComputerInformation', '/getFullComputerInformation', '/availableComputerPartition',
'/stoppedComputerPartition',
'/getHateoasUrl', '/getFullComputerInformation',
'/availableComputerPartition', '/stoppedComputerPartition', '/availableComputerPartition', '/stoppedComputerPartition',
'/getHateoasUrl',
'/getFullComputerInformation']) '/getFullComputerInformation'])
def test_partition_timestamp_no_timestamp(self): def test_partition_timestamp_no_timestamp(self):
...@@ -1218,8 +1221,10 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1218,8 +1221,10 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
instance.timestamp = None instance.timestamp = None
self.launchSlapgrid() self.launchSlapgrid()
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['/getFullComputerInformation', '/availableComputerPartition', ['/getHateoasUrl',
'/stoppedComputerPartition', '/getFullComputerInformation', '/getFullComputerInformation', '/availableComputerPartition',
'/stoppedComputerPartition',
'/getHateoasUrl', '/getFullComputerInformation',
'/availableComputerPartition', '/stoppedComputerPartition']) '/availableComputerPartition', '/stoppedComputerPartition'])
def test_partition_periodicity_remove_timestamp(self): def test_partition_periodicity_remove_timestamp(self):
......
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