Commit a9b92d27 authored by Alain Takoudjou's avatar Alain Takoudjou

slapproxy: propagate parent partition state to children

parent 3ba5547c
......@@ -547,6 +547,19 @@ def getAllocatedSlaveInstance(slave_reference, requested_computer_id):
# XXX: check there is only one result
return execute_db(table, q, args, one=True)
def getRootPartition(reference):
p = 'SELECT * FROM %s WHERE reference=?'
parent_partition = execute_db('partition', p, [reference], one=True)
while parent_partition is not None:
parent_reference = parent_partition['requested_by']
if not parent_reference or parent_reference == reference:
break
reference = parent_reference
parent_partition = execute_db('partition', p, [reference], one=True)
return parent_partition
def requestNotSlave(software_release, software_type, partition_reference, partition_id, partition_parameter_kw, filter_kw, requested_state):
instance_xml = dict2xml(partition_parameter_kw)
requested_computer_id = filter_kw['computer_guid']
......@@ -563,10 +576,6 @@ def requestNotSlave(software_release, software_type, partition_reference, partit
a = args.append
q = 'UPDATE %s SET slap_state="busy"'
if requested_state:
q += ', requested_state=?'
a(requested_state)
if partition is None:
partition = execute_db('partition',
'SELECT * FROM %s WHERE slap_state="free" and computer_reference=?',
......@@ -589,6 +598,16 @@ def requestNotSlave(software_release, software_type, partition_reference, partit
if partition['software_release'].encode() != software_release:
q += ' ,software_release=?'
a(software_release)
if partition['requested_by']:
root_partition = getRootPartition(partition['requested_by'])
if root_partition and root_partition['requested_state'] != "started":
# propagate parent state to child
# child can be stopped or destroyed while parent is started
requested_state = root_partition['requested_state']
if requested_state:
q += ', requested_state=?'
a(requested_state)
#
# XXX change software_type when requested
......
......@@ -285,7 +285,7 @@ class MasterMixin(BasicMixin, unittest.TestCase):
Define advanced tool for test proxy simulating behavior slap library tools
"""
def _requestComputerPartition(self, software_release, software_type, partition_reference,
partition_id,
partition_id=None,
shared=False, partition_parameter_kw=None, filter_kw=None,
state=None):
"""
......@@ -406,6 +406,48 @@ class TestRequest(MasterMixin):
self.request('http://sr//', None, 'MyFirstInstance', 'slappart2').__dict__,
self.request('http://sr//', None, 'MyFirstInstance', 'slappart2').__dict__)
def test_request_propagate_partition_state(self):
"""
Request will return same partition for two equal requests
"""
self.add_free_partition(2)
partition_parent = self.request('http://sr//', None, 'MyFirstInstance')
parent_dict = partition_parent.__dict__
partition_child = self.request('http://sr//', None, 'MySubInstance', parent_dict['_partition_id'])
self.assertEqual(partition_parent.getState(), 'started')
self.assertEqual(partition_child.getState(), 'started')
partition_parent = self.request('http://sr//', None, 'MyFirstInstance', state='stopped')
partition_child = self.request('http://sr//', None, 'MySubInstance', parent_dict['_partition_id'])
self.assertEqual(partition_parent.getState(), 'stopped')
self.assertEqual(partition_child.getState(), 'stopped')
partition_parent = self.request('http://sr//', None, 'MyFirstInstance', state='started')
partition_child = self.request('http://sr//', None, 'MySubInstance', parent_dict['_partition_id'])
self.assertEqual(partition_parent.getState(), 'started')
self.assertEqual(partition_child.getState(), 'started')
def test_request_parent_started_children_stopped(self):
"""
Request will return same partition for two equal requests
"""
self.add_free_partition(2)
partition_parent = self.request('http://sr//', None, 'MyFirstInstance')
parent_dict = partition_parent.__dict__
partition_child = self.request('http://sr//', None, 'MySubInstance', parent_dict['_partition_id'])
self.assertEqual(partition_parent.getState(), 'started')
self.assertEqual(partition_child.getState(), 'started')
partition_parent = self.request('http://sr//', None, 'MyFirstInstance')
partition_child = self.request('http://sr//', None, 'MySubInstance', parent_dict['_partition_id'], state='stopped')
self.assertEqual(partition_parent.getState(), 'started')
self.assertEqual(partition_child.getState(), 'stopped')
def test_two_requests_with_different_parameters_but_same_reference(self):
"""
Request will return same partition for two different requests but will
......
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