def test_get_label_by_id(self): self.fail_if_not_testing_env() self.clean_monkey_db() hostname_example = "a_hostname" ip_example = "1.1.1.1" linux_monkey = Monkey(guid=str(uuid.uuid4()), description="Linux shay-Virtual-Machine", hostname=hostname_example, ip_addresses=[ip_example]) linux_monkey.save() logger.debug(id(Monkey.get_label_by_id)) cache_info_before_query = Monkey.get_label_by_id.storage.backend.cache_info( ) self.assertEqual(cache_info_before_query.hits, 0) self.assertEqual(cache_info_before_query.misses, 0) # not cached label = Monkey.get_label_by_id(linux_monkey.id) cache_info_after_query_1 = Monkey.get_label_by_id.storage.backend.cache_info( ) self.assertEqual(cache_info_after_query_1.hits, 0) self.assertEqual(cache_info_after_query_1.misses, 1) logger.debug("1) ID: {} label: {}".format(linux_monkey.id, label)) self.assertIsNotNone(label) self.assertIn(hostname_example, label) self.assertIn(ip_example, label) # should be cached label = Monkey.get_label_by_id(linux_monkey.id) logger.debug("2) ID: {} label: {}".format(linux_monkey.id, label)) cache_info_after_query_2 = Monkey.get_label_by_id.storage.backend.cache_info( ) self.assertEqual(cache_info_after_query_2.hits, 1) self.assertEqual(cache_info_after_query_2.misses, 1) # set hostname deletes the id from the cache. linux_monkey.set_hostname("Another hostname") # should be a miss label = Monkey.get_label_by_id(linux_monkey.id) logger.debug("3) ID: {} label: {}".format(linux_monkey.id, label)) cache_info_after_query_3 = Monkey.get_label_by_id.storage.backend.cache_info( ) logger.debug("Cache info: {}".format(str(cache_info_after_query_3))) # still 1 hit only self.assertEqual(cache_info_after_query_3.hits, 1) self.assertEqual(cache_info_after_query_3.misses, 2)
def test_get_label_by_id(self): hostname_example = "a_hostname" ip_example = "1.1.1.1" linux_monkey = Monkey( guid=str(uuid.uuid4()), description="Linux shay-Virtual-Machine", hostname=hostname_example, ip_addresses=[ip_example], ) linux_monkey.save() logger.debug(id(Monkey.get_label_by_id)) cache_info_before_query = Monkey.get_label_by_id.storage.backend.cache_info( ) assert cache_info_before_query.hits == 0 assert cache_info_before_query.misses == 0 # not cached label = Monkey.get_label_by_id(linux_monkey.id) cache_info_after_query_1 = Monkey.get_label_by_id.storage.backend.cache_info( ) assert cache_info_after_query_1.hits == 0 assert cache_info_after_query_1.misses == 1 logger.debug("1) ID: {} label: {}".format(linux_monkey.id, label)) assert label is not None assert hostname_example in label assert ip_example in label # should be cached label = Monkey.get_label_by_id(linux_monkey.id) logger.debug("2) ID: {} label: {}".format(linux_monkey.id, label)) cache_info_after_query_2 = Monkey.get_label_by_id.storage.backend.cache_info( ) assert cache_info_after_query_2.hits == 1 assert cache_info_after_query_2.misses == 1 # set hostname deletes the id from the cache. linux_monkey.set_hostname("Another hostname") # should be a miss label = Monkey.get_label_by_id(linux_monkey.id) logger.debug("3) ID: {} label: {}".format(linux_monkey.id, label)) cache_info_after_query_3 = Monkey.get_label_by_id.storage.backend.cache_info( ) logger.debug("Cache info: {}".format(str(cache_info_after_query_3))) # still 1 hit only assert cache_info_after_query_3.hits == 1 assert cache_info_after_query_3.misses == 2