Commit 6cd1c7ad authored by Elena Grandi's avatar Elena Grandi

Fix loading of machine data when the timestamp is earlier than the current state

parent 0a722e07
......@@ -127,15 +127,19 @@ class Machine:
(old value, new value).
"""
changes = {}
if self.last_seen < evt.timestamp:
changes["last_seen"] = (self.last_seen, evt.timestamp)
self.last_seen = evt.timestamp
else:
# If the event has an older timestamp than the machine,
# ignore it
return {}
if evt.ip is not None and self.ip != evt.ip:
changes["ip"] = (self.ip, evt.ip)
self.ip = evt.ip
if evt.name is not None and self.name != evt.name:
changes["name"] = (self.name, evt.name)
self.name = evt.name
if self.last_seen < evt.timestamp:
changes["last_seen"] = (self.last_seen, evt.timestamp)
self.last_seen = evt.timestamp
if evt.registered is not None and self.registered != evt.registered:
changes["registered"] = (self.registered, evt.registered)
self.registered = evt.registered
......
......@@ -228,13 +228,21 @@ class TestMockMachineStore(AsyncTestCase):
@gen_test
async def test_add_machine_earlier_timestamp(self):
mac = self.machine['mac']
gathered = await self.post_host_seen(mac=mac, timestamp=100)
gathered = await self.post_host_seen(
mac=mac,
timestamp=100,
ip = self.machine['ip'])
self.assertIsInstance(gathered[0], events.HostSeenEvent)
self.assertIsInstance(gathered[1], events.HostNewEvent)
self.assertEqual(self.store.machines[mac].last_seen, 100)
# HostSeen with an earlier timestamp generates no changes
gathered = await self.post_host_seen(mac=mac, timestamp=80)
# HostSeen with an earlier timestamp generates no changes, even
# if the ip has changed
gathered = await self.post_host_seen(
mac=mac,
timestamp=80,
ip = '192.168.6.101')
self.assertNotEqual(self.machine['ip'], '192.168.6.101')
self.assertIsInstance(gathered[0], events.HostSeenEvent)
self.assertEqual(len(gathered), 1)
self.assertIn(mac, self.store.machines)
......@@ -248,7 +256,7 @@ class TestMockMachineStore(AsyncTestCase):
self.assertIsInstance(gathered[1], events.HostNewEvent)
self.assertEqual(self.store.machines[mac].last_seen, 100)
# HostSeen with an earlier timestamp generates no changes
# HostSeen with a later timestamp generates no changes
gathered = await self.post_host_seen(mac=mac, timestamp=200)
self.assertIsInstance(gathered[0], events.HostSeenEvent)
self.assertIsInstance(gathered[1], events.HostChangedEvent)
......
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