From 122919d73ca65ac0d1751e91a685a046ee793fda Mon Sep 17 00:00:00 2001 From: Shay Nehmad Date: Wed, 2 Oct 2019 11:01:08 +0300 Subject: [PATCH] Added cache test to test_monkey.py --- .../models/{monkey_test.py => test_monkey.py} | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) rename monkey/monkey_island/cc/models/{monkey_test.py => test_monkey.py} (81%) diff --git a/monkey/monkey_island/cc/models/monkey_test.py b/monkey/monkey_island/cc/models/test_monkey.py similarity index 81% rename from monkey/monkey_island/cc/models/monkey_test.py rename to monkey/monkey_island/cc/models/test_monkey.py index 6115386ea..d0b5efd71 100644 --- a/monkey/monkey_island/cc/models/monkey_test.py +++ b/monkey/monkey_island/cc/models/test_monkey.py @@ -112,3 +112,30 @@ class TestMonkey(IslandTestCase): and linux_monkey not in tunneled_monkeys and len(tunneled_monkeys) == 2) self.assertTrue(test, "Tunneling test") + + 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() + + cache_info_before_query = Monkey.get_label_by_id.storage.backend.cache_info() + self.assertEquals(cache_info_before_query.hits, 0) + + # not cached + label = Monkey.get_label_by_id(linux_monkey.id) + + self.assertIsNotNone(label) + self.assertIn(hostname_example, label) + self.assertIn(ip_example, label) + + # should be cached + _ = Monkey.get_label_by_id(linux_monkey.id) + cache_info_after_query = Monkey.get_label_by_id.storage.backend.cache_info() + self.assertEquals(cache_info_after_query.hits, 1)