def setUpClass(cls): cls.test_site_name = "MyGreatTestSite" cls.other_test_site_name = "MyOtherTestSite" cls.test_machine_type = "MyGreatTestMachineType" cls.tables_in_db = { "MachineTypes", "Resources", "ResourceStates", "Sites" } cls.test_resource_attributes = { "remote_resource_uuid": None, "drone_uuid": f"{cls.test_site_name}-07af52405e", "site_name": cls.test_site_name, "machine_type": cls.test_machine_type, "created": datetime.datetime(2018, 11, 16, 15, 49, 58), "updated": datetime.datetime(2018, 11, 16, 15, 49, 58), } cls.test_updated_resource_attributes = { "remote_resource_uuid": "bf85022b-fdd6-42b1-932d-086c288d4755", "drone_uuid": f"{cls.test_site_name}-07af52405e", "site_name": cls.test_site_name, "machine_type": cls.test_machine_type, "created": datetime.datetime(2018, 11, 16, 15, 49, 58), "updated": datetime.datetime(2018, 11, 16, 15, 50, 58), } cls.test_get_resources_result = { "remote_resource_uuid": cls.test_resource_attributes["remote_resource_uuid"], "drone_uuid": cls.test_resource_attributes["drone_uuid"], "state": str(RequestState()), "created": cls.test_resource_attributes["created"], "updated": cls.test_resource_attributes["updated"], } cls.test_notify_result = ( cls.test_resource_attributes["remote_resource_uuid"], cls.test_resource_attributes["drone_uuid"], str(RequestState()), cls.test_resource_attributes["site_name"], cls.test_resource_attributes["machine_type"], str(cls.test_resource_attributes["created"]), str(cls.test_resource_attributes["updated"]), ) cls.test_updated_notify_result = ( cls.test_updated_resource_attributes["remote_resource_uuid"], cls.test_updated_resource_attributes["drone_uuid"], str(BootingState()), cls.test_updated_resource_attributes["site_name"], cls.test_updated_resource_attributes["machine_type"], str(cls.test_updated_resource_attributes["created"]), str(cls.test_updated_resource_attributes["updated"]), ) cls.mock_config_patcher = patch( "tardis.plugins.sqliteregistry.Configuration") cls.mock_config = cls.mock_config_patcher.start()
def test_request_state(self): self.drone.state.return_value = RequestState() run_async(self.drone.state.return_value.run, self.drone) self.assertIsInstance(self.drone.state, BootingState) self.run_side_effects( RequestState(), self.drone.site_agent.deploy_resource, (TardisAuthError, TardisTimeout, TardisQuotaExceeded, TardisResourceStatusUpdateFailed), DownState) self.run_side_effects(RequestState(), self.drone.site_agent.deploy_resource, (TardisDroneCrashed, ), CleanupState)
def test_notify(self): test_state = RequestState() run_async(self.plugin.notify, test_state, get_test_param("6", ResourceStatus.Booting)) self.assert_gauges([1, 0, 0, 0, 0]) run_async(self.plugin.notify, test_state, get_test_param("6", ResourceStatus.Running)) self.assert_gauges([0, 1, 0, 0, 0]) run_async(self.plugin.notify, test_state, get_test_param("6", ResourceStatus.Stopped)) self.assert_gauges([0, 0, 1, 0, 0]) run_async(self.plugin.notify, test_state, get_test_param("6", ResourceStatus.Deleted)) self.assert_gauges([0, 0, 0, 1, 0]) run_async(self.plugin.notify, test_state, get_test_param("7", ResourceStatus.Error)) self.assert_gauges([0, 0, 0, 1, 1]) run_async(self.plugin.notify, test_state, get_test_param("9", ResourceStatus.Booting)) self.assert_gauges([1, 0, 0, 1, 1]) run_async(self.plugin.notify, test_state, get_test_param("8", ResourceStatus.Error)) self.assert_gauges([1, 0, 0, 1, 2])
def test_get_resources(self): self.registry.add_site(self.test_site_name) self.registry.add_machine_types(self.test_site_name, self.test_machine_type) run_async(self.registry.notify, RequestState(), self.test_resource_attributes) self.assertListEqual( self.registry.get_resources(site_name=self.test_site_name, machine_type=self.test_machine_type), [self.test_get_resources_result], )
def test_notify(self): # Database has to be queried multiple times # Define inline function to re-use code def fetch_all(): return self.execute_db_query( sql_query= """SELECT R.remote_resource_uuid, R.drone_uuid, RS.state, S.site_name, MT.machine_type, R.created, R.updated FROM Resources R JOIN ResourceStates RS ON R.state_id = RS.state_id JOIN Sites S ON R.site_id = S.site_id JOIN MachineTypes MT ON R.machine_type_id = MT.machine_type_id""" ) self.registry.add_site(self.test_site_name) self.registry.add_machine_types(self.test_site_name, self.test_machine_type) run_async(self.registry.notify, RequestState(), self.test_resource_attributes) self.assertEqual([self.test_notify_result], fetch_all()) with self.assertRaises(sqlite3.IntegrityError) as ie: run_async(self.registry.notify, RequestState(), self.test_resource_attributes) self.assertTrue("unique" in str(ie.exception).lower()) run_async( self.registry.notify, BootingState(), self.test_updated_resource_attributes, ) self.assertEqual([self.test_updated_notify_result], fetch_all()) run_async(self.registry.notify, DownState(), self.test_updated_resource_attributes) self.assertListEqual([], fetch_all())
def test_notify(self): test_param = AttributeDict(site_name='test-site', machine_type='test_machine_type', created=datetime.now(), updated=datetime.now()) test_state = RequestState() test_result = dict(state=str(test_state)) test_result.update(created=datetime.timestamp(test_param.created), updated=datetime.timestamp(test_param.updated)) test_tags = dict(site_name=test_param.site_name, machine_type=test_param.machine_type) run_async(self.plugin.notify, test_state, test_param) self.mock_aiotelegraf.Client.assert_called_with(host='telegraf.test', port=1234, tags={'test_tag': 'test'}) self.mock_aiotelegraf.Client.return_value.connect.assert_called_with() self.mock_aiotelegraf.Client.return_value.metric.assert_called_with('tardis_data', test_result, tags=test_tags) self.mock_aiotelegraf.Client.return_value.close.assert_called_with()
def test_notify(self): test_param = AttributeDict( site_name="test-site", machine_type="test_machine_type", created=datetime.now(), updated=datetime.now(), ) test_state = RequestState() test_result = dict(state=str(test_state)) test_result.update( created=datetime.timestamp(test_param.created), updated=datetime.timestamp(test_param.updated), ) test_tags = dict( site_name=test_param.site_name, machine_type=test_param.machine_type ) run_async(self.plugin.notify, test_state, test_param) self.mock_aiotelegraf.Client.assert_called_with( host="telegraf.test", port=1234, tags={"tardis_machine_name": platform.node(), "test_tag": "test"}, ) self.mock_aiotelegraf.Client.return_value.connect.assert_called_with() self.mock_aiotelegraf.Client.return_value.metric.assert_called_with( "tardis_data", test_result, tags=test_tags ) self.mock_aiotelegraf.Client.return_value.close.assert_called_with() self.mock_aiotelegraf.reset() # Test to overwrite tardis_node_name in plugin configuration self.config.Plugins.TelegrafMonitoring.default_tags = { "test_tag": "test", "tardis_machine_name": "my_test_node", } self.plugin = TelegrafMonitoring() run_async(self.plugin.notify, test_state, test_param) self.mock_aiotelegraf.Client.assert_called_with( host="telegraf.test", port=1234, tags={"tardis_machine_name": "my_test_node", "test_tag": "test"}, ) self.mock_aiotelegraf.Client.return_value.connect.assert_called_with() self.mock_aiotelegraf.Client.return_value.metric.assert_called_with( "tardis_data", test_result, tags=test_tags ) self.mock_aiotelegraf.Client.return_value.close.assert_called_with()
def test_create_drone(self, mock_site_agent, mock_batch_system_agent, mock_drone): self.assertEqual( create_drone(site_agent=mock_site_agent, batch_system_agent=mock_batch_system_agent), mock_drone(), ) mock_drone.has_call([ call( site_agent=mock_site_agent, batch_system_agent=mock_batch_system_agent, plugins=None, remote_resource_uuid=None, drone_uuid=None, state=RequestState(), created=None, updated=None, ) ])