add a placeholder when arrive a snapshot

This commit is contained in:
Cayo Puigdefabregas 2022-07-13 11:23:18 +02:00
parent 98ebb0d902
commit 2c6f831068
2 changed files with 51 additions and 3 deletions

View File

@ -88,6 +88,7 @@ class Sync:
# We only want to perform Add/Remove to not new components
actions = self.add_remove(db_device, not_new_components)
db_device.components = db_components
self.create_placeholder(db_device)
return db_device, actions
def execute_register_component(
@ -235,7 +236,6 @@ class Sync:
else: # Device is new and tags are not linked to a device
device.tags.clear() # We don't want to add the transient dummy tags
db.session.add(device)
self.create_placeholder(device)
db_device = device
db_device.tags |= (
tags # Union of tags the device had plus the (potentially) new ones
@ -283,15 +283,26 @@ class Sync:
@staticmethod
def create_placeholder(device: Device):
"""If the device is new, we need create automaticaly a new placeholder"""
# import pdb; pdb.set_trace()
if device.binding:
return
dict_device = copy.copy(device.__dict__)
dict_device.pop('_sa_instance_state')
dict_device.pop('id', None)
dict_device.pop('devicehub_id', None)
dict_device.pop('actions_multiple', None)
dict_device.pop('actions_one', None)
dict_device.pop('components', None)
dev_placeholder = device.__class__(**dict_device)
for c in device.components:
c_dict = copy.copy(c.__dict__)
c_dict.pop('_sa_instance_state')
c_dict.pop('id', None)
c_dict.pop('devicehub_id', None)
c_dict.pop('actions_multiple', None)
c_dict.pop('actions_one', None)
c_placeholder = c.__class__(**c_dict)
c_placeholder.parent = dev_placeholder
c.parent = device
component_placeholder = Placeholder(device=c_placeholder, binding=c)
db.session.add(c_placeholder)
db.session.add(component_placeholder)

View File

@ -1311,5 +1311,42 @@ def test_snapshot_check_tests_lite(user: UserClient):
bodyLite, res = user.post(snapshot_lite, uri="/api/inventory/")
assert res.status_code == 201
SnapshotsLog.query.all()
assert SnapshotsLog.query.count() == 1
m.Device.query.filter_by(devicehub_id=bodyLite['dhid']).one()
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_placeholder(user: UserClient):
"""This check the structure of one placeholder generated automatically by a snapshot"""
snapshot_lite = file_json(
'test_lite/2022-4-13-19-5_user@dhub.com_b27dbf43-b88a-4505-ae27-10de5a95919e.json'
)
bodyLite, res = user.post(snapshot_lite, uri="/api/inventory/")
assert res.status_code == 201
dev = m.Device.query.filter_by(devicehub_id=bodyLite['dhid']).one()
assert dev.placeholder is None
assert dev.binding.phid == '12'
assert len(dev.binding.device.components) == 11
assert len(dev.components) == 11
assert dev.binding.device.placeholder == dev.binding
assert dev.components != dev.binding.device.components
assert dev.binding.device.actions == []
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_placeholder_actions(user: UserClient):
"""This test the actions of a placeholder of one snapshot"""
s = yaml2json('erase-sectors.snapshot')
snap1, _ = user.post(s, res=Snapshot)
dev = m.Device.query.filter_by(id=snap1['device']['id']).one()
assert dev.placeholder is None
assert dev.binding.phid == '4'
assert len(dev.binding.device.components) == 3
assert len(dev.components) == 3
assert dev.binding.device.placeholder == dev.binding
assert dev.components != dev.binding.device.components
assert dev.binding.device.actions == []
assert len(dev.components[0].actions) == 3
assert len(dev.binding.device.components[0].actions) == 0