示例#1
0
 def test_general_flow(self, log_mock):
     cache_storage = MemoryStorage()
     back_storage = MemoryStorage()
     inq = Inquiry(action='get')
     chk1 = RulesChecker()
     p1 = Policy(1, description='initial')
     p2 = Policy(2, description='initial')
     p3 = Policy(3, description='added later')
     # initialize backend storage
     back_storage.add(p1)
     back_storage.add(p2)
     # create enfold-cache but do not populate it
     ec = EnfoldCache(back_storage, cache=cache_storage, populate=False)
     # make sure policies are returned from the backend
     assert [p1, p2] == list(ec.find_for_inquiry(inquiry=inq, checker=chk1))
     # make sure we logged warning about cache miss
     log_mock.warning.assert_called_with(
         '%s cache miss for find_for_inquiry. Trying it from backend storage',
         'EnfoldCache')
     log_mock.reset_mock()
     # populate cache with backend policies so that we do not make cache hit misses
     ec.populate()
     # make sure policies are returned
     assert [p1, p2] == list(ec.find_for_inquiry(inquiry=inq, checker=chk1))
     # make sure we do not have cache misses
     assert 0 == log_mock.warning.call_count
     log_mock.reset_mock()
     # let's add a new policy via enfold-cache
     ec.add(p3)
     # make sure policies are returned
     assert [p1, p2,
             p3] == list(ec.find_for_inquiry(inquiry=inq, checker=chk1))
     # make sure we do not have cache misses
     assert 0 == log_mock.warning.call_count
     log_mock.reset_mock()
     # make sure we have those policies in the backend-storage
     assert [p1, p2, p3] == list(
         back_storage.find_for_inquiry(inquiry=inq, checker=chk1))
     # make sure we have those policies in the cache-storage
     assert [p1, p2, p3] == list(
         cache_storage.find_for_inquiry(inquiry=inq, checker=chk1))
     # -----------------------
     # -----------------------
     # -----------------------
     # let's re-create enfold cache. This time with initial population
     cache_storage2 = MemoryStorage()
     ec2 = EnfoldCache(back_storage, cache=cache_storage2, populate=True)
     # make sure we have all policies in the cache-storage
     assert [p1, p2, p3] == list(
         cache_storage2.find_for_inquiry(inquiry=inq, checker=chk1))
     # make sure policies are returned by find_for_inquiry
     assert [p1, p2,
             p3] == list(ec2.find_for_inquiry(inquiry=inq, checker=chk1))
     # make sure we do not have cache misses
     assert 0 == log_mock.warning.call_count
     log_mock.reset_mock()
示例#2
0
 def test_find_for_inquiry_return_value(self, storage):
     cache_storage = storage
     back_storage = MemoryStorage()
     ec = EnfoldCache(back_storage, cache=cache_storage)
     inq = Inquiry(action='get')
     p1 = Policy(1)
     p2 = Policy(2)
     ec.add(p1)
     ec.add(p2)
     backend_return = back_storage.find_for_inquiry(inq)
     ec_return = ec.find_for_inquiry(inq)
     assert list(backend_return) == list(ec_return)