slapos.slap: add getHateoasUrl support.

parent 45cbfc0c
......@@ -262,7 +262,7 @@ class OpenOrder(SlapRequester):
def getInformation(self, partition_reference):
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)
software_instance = SoftwareInstance()
# XXX redefine SoftwareInstance to be more consistent
......@@ -490,7 +490,7 @@ class ComputerPartition(SlapRequester):
in the Instance tree of the current Computer Partition.
"""
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)
def getId(self):
......@@ -710,6 +710,11 @@ class slap:
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:
self._hateoas_navigator = HateoasNavigator(
slapgrid_rest_uri,
......@@ -795,7 +800,7 @@ class slap:
def getOpenOrderDict(self):
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()
......
......@@ -335,6 +335,61 @@ class TestSlap(SlapMixin):
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):
"""
Tests slapos.slap.slap.Computer class functionality
......
......@@ -638,7 +638,7 @@ chmod 755 etc/run/wrapper
'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertLogContent(wrapper_log, 'Signal handler called with signal 15')
self.assertEqual(computer.sequence,
['/getFullComputerInformation', '/availableComputerPartition',
['/getHateoasUrl', '/getFullComputerInformation', '/availableComputerPartition',
'/stoppedComputerPartition'])
self.assertEqual(instance.state, 'stopped')
......@@ -698,7 +698,7 @@ exit 1
'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertLogContent(wrapper_log, 'Signal handler called with signal 15')
self.assertEqual(computer.sequence,
['/getFullComputerInformation',
['/getHateoasUrl', '/getFullComputerInformation',
'/softwareInstanceError'])
self.assertEqual(instance.state, 'started')
......@@ -734,7 +734,7 @@ exit 1
wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log')
self.assertLogContent(wrapper_log, 'Working')
self.assertEqual(computer.sequence,
['/getFullComputerInformation', '/availableComputerPartition',
['/getHateoasUrl', '/getFullComputerInformation', '/availableComputerPartition',
'/startedComputerPartition'])
self.assertEqual('started', instance.state)
......@@ -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(computer.sequence,
['/getFullComputerInformation', '/availableComputerPartition',
'/stoppedComputerPartition', '/getFullComputerInformation',
['/getHateoasUrl',
'/getFullComputerInformation', '/availableComputerPartition',
'/stoppedComputerPartition',
'/getHateoasUrl', '/getFullComputerInformation',
'/availableComputerPartition', '/stoppedComputerPartition',
'/getHateoasUrl',
'/getFullComputerInformation'])
def test_partition_timestamp_no_timestamp(self):
......@@ -1218,8 +1221,10 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
instance.timestamp = None
self.launchSlapgrid()
self.assertEqual(computer.sequence,
['/getFullComputerInformation', '/availableComputerPartition',
'/stoppedComputerPartition', '/getFullComputerInformation',
['/getHateoasUrl',
'/getFullComputerInformation', '/availableComputerPartition',
'/stoppedComputerPartition',
'/getHateoasUrl', '/getFullComputerInformation',
'/availableComputerPartition', '/stoppedComputerPartition'])
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