Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbruinsslot committed Dec 2, 2024
1 parent 2fae57e commit dc6e9e1
Showing 1 changed file with 142 additions and 6 deletions.
148 changes: 142 additions & 6 deletions mula/tests/integration/test_boefje_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from types import SimpleNamespace
from unittest import mock

from scheduler import clients, config, models, schedulers, storage
from scheduler.storage import stores
from structlog.testing import capture_logs

from scheduler import clients, config, models, schedulers, storage
from scheduler.storage import stores
from tests.factories import (
BoefjeFactory,
BoefjeMetaFactory,
Expand Down Expand Up @@ -1273,7 +1273,7 @@ def test_push_tasks_for_scan_profile_mutations_delete_on_queue(self):
task_db = self.mock_ctx.datastores.task_store.get_task(item.id)
self.assertEqual(task_db.status, models.TaskStatus.CANCELLED)

def test_push_tasks_for_scan_profile_mutations_runon_create(self):
def test_push_tasks_for_scan_profile_mutations_op_create_runon_create(self):
"""When a boefje has the runon contains the setting create,
and we receive a create mutation, it should:
Expand Down Expand Up @@ -1310,7 +1310,7 @@ def test_push_tasks_for_scan_profile_mutations_runon_create(self):
schedule_db = self.mock_ctx.datastores.schedule_store.get_schedule_by_hash(task_db.hash)
self.assertIsNone(schedule_db)

def test_push_tasks_for_scan_profile_mutations_runon_create_update(self):
def test_push_tasks_for_scan_profile_mutations_op_create_runon_create_update(self):
"""When a boefje has the runon contains the setting create,update,
and we receive a create mutation, it should:
Expand Down Expand Up @@ -1347,7 +1347,7 @@ def test_push_tasks_for_scan_profile_mutations_runon_create_update(self):
schedule_db = self.mock_ctx.datastores.schedule_store.get_schedule_by_hash(task_db.hash)
self.assertIsNone(schedule_db)

def test_push_tasks_for_scan_profile_mutations_runon_update(self):
def test_push_tasks_for_scan_profile_mutations_op_create_runon_update(self):
"""When a boefje has the runon contains the setting update,
and we receive a create mutation, it should:
Expand All @@ -1371,7 +1371,7 @@ def test_push_tasks_for_scan_profile_mutations_runon_update(self):
# Assert: task should NOT be on priority queue
self.assertEqual(0, self.scheduler.queue.qsize())

def test_push_tasks_for_scan_profile_mutations_runon_none(self):
def test_push_tasks_for_scan_profile_mutations_op_create_runon_none(self):
"""When a boefje has the runon is empty, and we receive a create
mutation, it should:
Expand Down Expand Up @@ -1409,6 +1409,142 @@ def test_push_tasks_for_scan_profile_mutations_runon_none(self):
schedule_db = self.mock_ctx.datastores.schedule_store.get_schedule(task_db.schedule_id)
self.assertIsNotNone(schedule_db)

def test_push_tasks_for_scan_profile_mutations_op_update_runon_create(self):
"""When a boefje has the runon contains the setting create,
and we receive an update mutation, it should:
- NOT create a `Schedule`
- NOT run a `Task`
"""
# Arrange
scan_profile = ScanProfileFactory(level=0)
ooi = OOIFactory(scan_profile=scan_profile)
boefje = PluginFactory(scan_level=0, consumes=[ooi.object_type], runon=["create"])
mutation = models.ScanProfileMutation(
operation=models.MutationOperationType.UPDATE, primary_key=ooi.primary_key, value=ooi
).model_dump_json()

# Mocks
self.mock_get_boefjes_for_ooi.return_value = [boefje]

# Act
self.scheduler.push_tasks_for_scan_profile_mutations(mutation)

# Assert: task should NOT be on priority queue
self.assertEqual(0, self.scheduler.queue.qsize())

def test_push_tasks_scan_profile_mutations_op_update_runon_create_update(self):
"""When a boefje has the runon contains the setting create,update,
and we receive an update mutation, it should:
- NOT create a `Schedule`
- SHOULD run a `Task`
"""
# Arrange
scan_profile = ScanProfileFactory(level=0)
ooi = OOIFactory(scan_profile=scan_profile)
boefje = PluginFactory(scan_level=0, consumes=[ooi.object_type], runon=["create", "update"])
mutation = models.ScanProfileMutation(
operation=models.MutationOperationType.UPDATE, primary_key=ooi.primary_key, value=ooi
).model_dump_json()

# Mocks
self.mock_get_boefjes_for_ooi.return_value = [boefje]

# Act
self.scheduler.push_tasks_for_scan_profile_mutations(mutation)

# Assert: task should be on priority queue
item = self.scheduler.queue.peek(0)
task_pq = models.BoefjeTask(**item.data)
self.assertEqual(1, self.scheduler.queue.qsize())
self.assertEqual(ooi.primary_key, task_pq.input_ooi)
self.assertEqual(boefje.id, task_pq.boefje.id)

# Assert: task should be in datastore, and queued
task_db = self.mock_ctx.datastores.task_store.get_task(item.id)
self.assertEqual(task_db.status, models.TaskStatus.QUEUED)

# Assert: schedule should NOT be created
self.assertIsNone(task_db.schedule_id)
schedule_db = self.mock_ctx.datastores.schedule_store.get_schedule_by_hash(task_db.hash)
self.assertIsNone(schedule_db)

def test_push_tasks_scan_profile_mutations_op_update_runon_update(self):
"""When a boefje has the runon contains the setting update,
and we receive an update mutation, it should:
- NOT create a `Schedule`
- SHOULD run a `Task`
"""
# Arrange
scan_profile = ScanProfileFactory(level=0)
ooi = OOIFactory(scan_profile=scan_profile)
boefje = PluginFactory(scan_level=0, consumes=[ooi.object_type], runon=["update"])
mutation = models.ScanProfileMutation(
operation=models.MutationOperationType.UPDATE, primary_key=ooi.primary_key, value=ooi
).model_dump_json()

# Mocks
self.mock_get_boefjes_for_ooi.return_value = [boefje]

# Act
self.scheduler.push_tasks_for_scan_profile_mutations(mutation)

# Assert: task should be on priority queue
item = self.scheduler.queue.peek(0)
task_pq = models.BoefjeTask(**item.data)
self.assertEqual(1, self.scheduler.queue.qsize())
self.assertEqual(ooi.primary_key, task_pq.input_ooi)
self.assertEqual(boefje.id, task_pq.boefje.id)

# Assert: task should be in datastore, and queued
task_db = self.mock_ctx.datastores.task_store.get_task(item.id)
self.assertEqual(task_db.status, models.TaskStatus.QUEUED)

# Assert: schedule should NOT be created
self.assertIsNone(task_db.schedule_id)
schedule_db = self.mock_ctx.datastores.schedule_store.get_schedule_by_hash(task_db.hash)
self.assertIsNone(schedule_db)

def test_push_tasks_scan_profile_mutations_op_update_runon_none(self):
"""When a boefje has the runon is empty, and we receive an update
mutation, it should:
- SHOULD create a `Schedule`
- SHOULD run a `Task`
"""
# Arrange
scan_profile = ScanProfileFactory(level=0)
ooi = OOIFactory(scan_profile=scan_profile)
boefje = PluginFactory(scan_level=0, consumes=[ooi.object_type], runon=[])
mutation = models.ScanProfileMutation(
operation=models.MutationOperationType.UPDATE, primary_key=ooi.primary_key, value=ooi
).model_dump_json()

# Mocks
self.mock_get_boefjes_for_ooi.return_value = [boefje]
self.mock_set_cron.return_value = "0 0 * * *"

# Act
self.scheduler.push_tasks_for_scan_profile_mutations(mutation)

# Assert: task should be on priority queue
item = self.scheduler.queue.peek(0)
task_pq = models.BoefjeTask(**item.data)
self.assertEqual(1, self.scheduler.queue.qsize())
self.assertEqual(ooi.primary_key, task_pq.input_ooi)
self.assertEqual(boefje.id, task_pq.boefje.id)

# Assert: task should be in datastore, and queued
task_db = self.mock_ctx.datastores.task_store.get_task(item.id)
self.assertEqual(task_db.status, models.TaskStatus.QUEUED)

# Assert: schedule should be created
self.assertIsNotNone(task_db.schedule_id)
schedule_db = self.mock_ctx.datastores.schedule_store.get_schedule(task_db.schedule_id)
self.assertIsNotNone(schedule_db)


class NewBoefjesTestCase(BoefjeSchedulerBaseTestCase):
def setUp(self):
Expand Down

0 comments on commit dc6e9e1

Please sign in to comment.