From da817dd2f1594b6b4a44caea97af0d31b3392311 Mon Sep 17 00:00:00 2001 From: Stefan Kroboth Date: Mon, 29 Aug 2022 11:00:15 +0200 Subject: [PATCH] still broken tests --- tardis/plugins/auditor.py | 16 ++--- tests/plugins_t/test_auditor.py | 113 ++++++++++++++------------------ 2 files changed, 58 insertions(+), 71 deletions(-) diff --git a/tardis/plugins/auditor.py b/tardis/plugins/auditor.py index dc5346b5..ab459bc9 100644 --- a/tardis/plugins/auditor.py +++ b/tardis/plugins/auditor.py @@ -4,7 +4,7 @@ from ..utilities.attributedict import AttributeDict from ..resources.dronestates import AvailableState, DownState -from pyauditor import AuditorClientBuilder, Record, Component, Score +import pyauditor import logging import pytz @@ -33,16 +33,16 @@ def __init__(self): self._resources[site.name][machine_type][r] = getattr( config, site.name ).MachineMetaData[machine_type][r] - self._components[site.name][machine_type][r] = config_auditor[ - "components" - ][machine_type].get(r, {}) + self._components[site.name][machine_type][r] = getattr( + config_auditor.components, machine_type + ).get(r, {}) self._user = config_auditor.user if config_auditor.user else "tardis" self._group = config_auditor.group if config_auditor.group else "tardis" auditor_timeout = config_auditor.get("timeout", 30) self._local_timezone = get_localzone() self._client = ( - AuditorClientBuilder() + pyauditor.AuditorClientBuilder() .address(config_auditor.host, config_auditor.port) .timeout(auditor_timeout) .build() @@ -76,7 +76,7 @@ async def notify(self, state: State, resource_attributes: AttributeDict) -> None await self._client.update(record) def construct_record(self, resource_attributes: AttributeDict): - record = Record( + record = pyauditor.Record( resource_attributes["drone_uuid"], resource_attributes["site_name"], self._user, @@ -89,11 +89,11 @@ def construct_record(self, resource_attributes: AttributeDict): for (resource, amount) in self._resources[resource_attributes["site_name"]][ resource_attributes["machine_type"] ].items(): - component = Component(resource, amount) + component = pyauditor.Component(resource, amount) for score_name, score_val in self._components[ resource_attributes["site_name"] ][resource_attributes["machine_type"]][resource].items(): - component = component.with_score(Score(score_name, score_val)) + component = component.with_score(pyauditor.Score(score_name, score_val)) record = record.with_component(component) diff --git a/tests/plugins_t/test_auditor.py b/tests/plugins_t/test_auditor.py index 8119692b..3f326c0f 100644 --- a/tests/plugins_t/test_auditor.py +++ b/tests/plugins_t/test_auditor.py @@ -7,6 +7,7 @@ from unittest import TestCase from unittest.mock import patch +from ..utilities.utilities import async_return from ..utilities.utilities import run_async @@ -14,100 +15,86 @@ class TestAuditor(TestCase): @classmethod def setUpClass(cls): cls.mock_config_patcher = patch("tardis.plugins.auditor.Configuration") - cls.mock_auditor_patcher = patch("tardis.plugins.auditor.Auditor") + cls.mock_auditorclientbuilder_patcher = patch( + "tardis.plugins.auditor.pyauditor.AuditorClientBuilder" + ) - cls.mock_auditor_patcher = patch("tardis.plugins.auditor.AuditorClientBuilder") cls.mock_config = cls.mock_config_patcher.start() - cls.mock_auditor = cls.mock_auditor_patcher.start() + cls.mock_auditorclientbuilder = cls.mock_auditorclientbuilder_patcher.start() @classmethod def tearDownClass(cls): cls.mock_config_patcher.stop() - cls.mock_auditor_patcher.stop() + cls.mock_auditorclientbuilder_patcher.stop() def setUp(self): self.address = "127.0.0.1" self.port = 8000 self.user = "user-1" self.group = "group-1" + self.site = "testsite" + self.cores = 12 + self.memory = 100 + self.drone_uuid = "test-drone" + self.machine_type = "test_machine_type" config = self.mock_config.return_value config.Plugins.Auditor.address = self.address config.Plugins.Auditor.port = self.port config.Plugins.Auditor.user = self.user config.Plugins.Auditor.group = self.group - config.Plugins.Auditor.components.test_machine_type.Cores = AttributeDict( - HEPSPEC=1.2, BENCHMARK=3.0 + config.Plugins.Auditor.components.test_machine_type = AttributeDict( + Cores=AttributeDict(HEPSPEC=1.2, BENCHMARK=3.0), + Memory=AttributeDict(BLUBB=1.4), ) - config.Plugins.Auditor.components.test_machine_type.Memory = AttributeDict( - BLUBB=1.4 + config.Sites = [AttributeDict(name=self.site)] + config.testsite.MachineTypes = [self.machine_type] + config.testsite.MachineMetaData = AttributeDict( + test_machine_type=AttributeDict(Cores=self.cores, Memory=self.memory) ) - config.Sites.name = ["testsite"] - config.testsite.MachineTypes = ["test_machine_type"] - config.testsite.MachineMetaData.test_machine_type.Cores = 12 - config.testsite.MachineMetaData.test_machine_type.Memory = 100 + + # auditor_client_builder = self.mock_auditorclientbuilder.return_value + # auditor_client_builder.build.add.return_value = async_return() self.plugin = Auditor() def test_notify(self): test_param = AttributeDict( - site_name="testsite", - machine_type="test_machine_type", + site_name=self.site, + machine_type=self.machine_type, created=datetime.now(), updated=datetime.now(), resource_status=ResourceStatus.Booting, - drone_uuid="test-drone", + drone_uuid=self.drone_uuid, ) - self.mock_auditor.return_value.search.return_value = { - "hits": {"total": {"value": 2}} - } - run_async( self.plugin.notify, state=AvailableState(), resource_attributes=test_param ) - # self.mock_auditor.return_value.search.assert_called_with( - # index=f"{self.plugin._index}*", - # body={"query": {"term": {"drone_uuid.keyword": test_param.drone_uuid}}}, - # ) - # self.mock_auditor.return_value.create.assert_called_with( - # body=test_param_ext, - # id=f"{test_param.drone_uuid}-2", - # index=f"{self.plugin._index}-{self.mock_datetime.now.return_value.strftime.return_value}", # noqa: B950 - # ) + def test_construct_record(self): + test_param = AttributeDict( + site_name=self.site, + machine_type=self.machine_type, + created=datetime.now(), + updated=datetime.now(), + resource_status=ResourceStatus.Booting, + drone_uuid=self.drone_uuid, + ) + + record = self.plugin.construct_record(resource_attributes=test_param) + + assert record.record_id == self.drone_uuid + assert record.site_id == self.site + assert record.user_id == self.user + assert record.group_id == self.group + assert record.components[0].name == "Cores" + assert record.components[0].amount == 12 + assert record.components[0].scores[0].name == "HEPSPEC" + assert record.components[0].scores[0].factor == 1.2 + assert record.components[0].scores[1].name == "BENCHMARK" + assert record.components[0].scores[1].factor == 3.0 + assert record.components[1].name == "Memory" + assert record.components[1].amount == 100 + assert record.components[1].scores[0].name == "BLUBB" + assert record.components[1].scores[0].factor == 1.4 - # def test_notify_resource_status_missing(self): - # test_param = AttributeDict( - # site_name="test-site", - # machine_type="test_machine_type", - # created=datetime.now(), - # updated=datetime.now(), - # drone_uuid="test-drone", - # ) - # - # test_param_ext = { - # **test_param, - # "state": str(CleanupState()), - # "meta": self.plugin._meta, - # "timestamp": int(self.mock_time.return_value * 1000), - # "resource_status": "", - # "revision": 2, - # } - # - # self.mock_auditor.return_value.search.return_value = { - # "hits": {"total": {"value": 2}} - # } - # - # run_async( - # self.plugin.notify, state=CleanupState(), resource_attributes=test_param - # ) - # - # self.mock_auditor.return_value.search.assert_called_with( - # index=f"{self.plugin._index}*", - # body={"query": {"term": {"drone_uuid.keyword": test_param.drone_uuid}}}, - # ) - # self.mock_auditor.return_value.create.assert_called_with( - # body=test_param_ext, - # id=f"{test_param.drone_uuid}-2", - # index=f"{self.plugin._index}-{self.mock_datetime.now.return_value.strftime.return_value}", # noqa: B950 - # )