Beispiel #1
0
 def test_get_return_value(self):
     cache_storage = MemoryStorage()
     back_storage = MemoryStorage()
     ec = EnfoldCache(back_storage, cache=cache_storage)
     p1 = Policy(1)
     ec.add(p1)
     backend_return = back_storage.get(1)
     ec_return = ec.get(1)
     assert backend_return == ec_return
Beispiel #2
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()
Beispiel #3
0
 def test_update_return_value(self):
     cache_storage = MemoryStorage()
     back_storage = MemoryStorage()
     ec = EnfoldCache(back_storage, cache=cache_storage)
     p1 = Policy(1, description='foo')
     ec.add(p1)
     p1.description = 'foo upd'
     backend_return = back_storage.update(p1)
     ec_return = ec.update(p1)
     assert backend_return == ec_return
Beispiel #4
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)
Beispiel #5
0
 def test_delete_ok(self):
     cache_storage = MemoryStorage()
     back_storage = MemoryStorage()
     ec = EnfoldCache(back_storage, cache=cache_storage)
     p1 = Policy(1, description='foo')
     p2 = Policy(2, description='bar')
     ec.add(p1)
     ec.add(p2)
     ec.delete(1)
     ec.delete(2)
     assert [] == cache_storage.get_all(100, 0)
     assert [] == back_storage.get_all(100, 0)
Beispiel #6
0
 def test_retrieve_all_return_value(self, storage):
     cache_storage = storage
     back_storage = MemoryStorage()
     ec = EnfoldCache(back_storage, cache=cache_storage)
     p1 = Policy(1)
     p2 = Policy(2)
     p3 = Policy(3)
     ec.add(p1)
     ec.add(p2)
     ec.add(p3)
     backend_return = back_storage.retrieve_all()
     ec_return = back_storage.retrieve_all()
     assert list(backend_return) == list(ec_return)
Beispiel #7
0
 def test_get_all_return_value(self, storage):
     cache_storage = storage
     back_storage = MemoryStorage()
     ec = EnfoldCache(back_storage, cache=cache_storage)
     p1 = Policy(1)
     p2 = Policy(2)
     p3 = Policy(3)
     ec.add(p1)
     ec.add(p2)
     ec.add(p3)
     backend_return = back_storage.get_all(100, 0)
     ec_return = back_storage.get_all(100, 0)
     assert backend_return == ec_return
Beispiel #8
0
 def test_add_ok(self):
     cache_storage = MemoryStorage()
     back_storage = MemoryStorage()
     ec = EnfoldCache(back_storage, cache=cache_storage)
     p1 = Policy(1)
     p2 = Policy(2)
     p3 = Policy(3)
     ec.add(p1)
     assert [p1] == cache_storage.get_all(100, 0)
     assert [p1] == back_storage.get_all(100, 0)
     ec.add(p2)
     ec.add(p3)
     assert [p1, p2, p3] == cache_storage.get_all(100, 0)
     assert [p1, p2, p3] == back_storage.get_all(100, 0)
Beispiel #9
0
 def test_delete_fail(self):
     cache_storage = MemoryStorage()
     back_storage = MemoryStorage()
     ec = EnfoldCache(back_storage, cache=cache_storage)
     p1 = Policy(1, description='foo')
     p2 = Policy(2, description='bar')
     ec.add(p1)
     ec.add(p2)
     back_storage.delete = Mock(side_effect=Exception('error!'))
     with pytest.raises(Exception) as excinfo:
         ec.delete(2)
     assert 'error!' == str(excinfo.value)
     assert [p1, p2] == back_storage.get_all(1000, 0)
     assert [p1, p2] == cache_storage.get_all(1000, 0)
Beispiel #10
0
 def test_add_return_value(self):
     cache_storage = MemoryStorage()
     back_storage = MemoryStorage()
     ec = EnfoldCache(back_storage, cache=cache_storage)
     backend_return = back_storage.add(Policy(1))
     ec_return = ec.add(Policy(2))
     assert backend_return == ec_return
Beispiel #11
0
 def test_update_ok(self):
     cache_storage = MemoryStorage()
     back_storage = MemoryStorage()
     ec = EnfoldCache(back_storage, cache=cache_storage)
     p1 = Policy(1, description='foo')
     p2 = Policy(2, description='bar')
     p3 = Policy(3, description='baz')
     ec.add(p1)
     ec.add(p2)
     ec.add(p3)
     p1.description = 'foo2'
     ec.update(p1)
     p2.description = 'bar2'
     ec.update(p2)
     assert [p1, p2, p3] == cache_storage.get_all(100, 0)
     assert [p1, p2, p3] == back_storage.get_all(100, 0)
Beispiel #12
0
 def test_add_fail(self):
     cache_storage = MemoryStorage()
     back_storage = MemoryStorage()
     ec = EnfoldCache(back_storage, cache=cache_storage)
     p1 = Policy(1)
     p2 = Policy(2)
     p3 = Policy(3)
     ec.add(p1)
     back_storage.add = Mock(side_effect=PolicyExistsError('2'))
     with pytest.raises(PolicyExistsError) as excinfo:
         ec.add(p2)
     assert 'Conflicting UID = 2' == str(excinfo.value)
     assert [p1] == back_storage.get_all(1000, 0)
     # test general exception
     back_storage.add = Mock(side_effect=Exception('foo'))
     with pytest.raises(Exception) as excinfo:
         ec.add(p3)
     assert 'foo' == str(excinfo.value)
     assert [p1] == back_storage.get_all(1000, 0)
     assert [p1] == cache_storage.get_all(1000, 0)