Esempio n. 1
0
    def __exit__(self, exc_type, exc_val, exc_tb):
        """
        Set the complete=True, create the publication.

        Args:
            exc_type (Type): (optional) Type of exception raised.
            exc_val (Exception): (optional) Instance of exception raised.
            exc_tb (types.TracebackType): (optional) stack trace.
        """
        if exc_val:
            # If an exception got us here, the Publication we were trying to create is
            # Bad, and we should delete the attempt. HOWEVER - some exceptions happen before we
            # even get that far. In those cases, calling delete() results in a new not-very-useful
            # exception being raised and reported to the user, rather than the actual problem.
            try:
                self.delete()
            except Exception:
                raise exc_val.with_traceback(exc_tb)
        else:
            try:
                self.finalize_new_publication()
                self.complete = True
                self.save()
            except Exception:
                self.delete()
                raise

            # invalidate cache
            if settings.CACHE_ENABLED:
                base_paths = Distribution.objects.filter(
                    repository=self.repository_version.repository).values_list(
                        "base_path", flat=True)
                if base_paths:
                    Cache().delete(base_key=base_paths)
Esempio n. 2
0
    def __exit__(self, exc_type, exc_val, exc_tb):
        """
        Set the complete=True, create the publication.

        Args:
            exc_type (Type): (optional) Type of exception raised.
            exc_val (Exception): (optional) Instance of exception raised.
            exc_tb (types.TracebackType): (optional) stack trace.
        """
        if exc_val:
            self.delete()
        else:
            try:
                self.finalize_new_publication()
                self.complete = True
                self.save()
            except Exception:
                self.delete()
                raise

            # invalidate cache
            if settings.CACHE_ENABLED:
                base_paths = Distribution.objects.filter(
                    repository=self.repository_version.repository).values_list(
                        "base_path", flat=True)
                if base_paths:
                    Cache().delete(base_key=base_paths)
Esempio n. 3
0
 def test_03_basic_delete(self):
     """Tests deleting value"""
     cache = Cache()
     self.assertTrue(cache.exists("key"))
     cache.delete("key")
     ret = cache.get("key")
     self.assertIsNone(ret)
Esempio n. 4
0
    def delete(self, **kwargs):
        """
        Deletes a RepositoryVersion

        If RepositoryVersion is complete and has a successor, squash RepositoryContent changes into
        the successor. If version is incomplete, delete and and clean up RepositoryContent,
        CreatedResource, and Repository objects.

        Deletion of a complete RepositoryVersion should be done in a RQ Job.
        """
        if self.complete:
            if settings.CACHE_ENABLED:
                base_paths = self.distribution_set.values_list("base_path", flat=True)
                if base_paths:
                    Cache().delete(base_key=base_paths)

            repo_relations = RepositoryContent.objects.filter(repository=self.repository)
            try:
                next_version = self.next()
                self._squash(repo_relations, next_version)

            except RepositoryVersion.DoesNotExist:
                # version is the latest version so simply update repo contents
                # and delete the version
                repo_relations.filter(version_added=self).delete()
                repo_relations.filter(version_removed=self).update(version_removed=None)
            super().delete(**kwargs)

        else:
            with transaction.atomic():
                RepositoryContent.objects.filter(version_added=self).delete()
                RepositoryContent.objects.filter(version_removed=self).update(version_removed=None)
                CreatedResource.objects.filter(object_id=self.pk).delete()
                super().delete(**kwargs)
Esempio n. 5
0
 def invalidate_cache(self):
     """Invalidates the cache if remote is present."""
     if settings.CACHE_ENABLED:
         base_paths = self.distribution_set.values_list("base_path",
                                                        flat=True)
         if base_paths:
             Cache().delete(base_key=base_paths)
Esempio n. 6
0
    def delete(self, **kwargs):
        """
        Delete the publication.

        Args:
            **kwargs (dict): Delete options.

        Notes:
            Deletes the Task.created_resource when complete is False.
        """
        with transaction.atomic():
            # invalidate cache
            if settings.CACHE_ENABLED:
                # Find any publications being served directly
                base_paths = self.distribution_set.values_list("base_path",
                                                               flat=True)
                # Find any publications being served indirectly by auto-distribute feature
                versions = self.repository.versions.all()
                pubs = Publication.objects.filter(
                    repository_version__in=versions, complete=True)
                publication = pubs.latest("repository_version", "pulp_created")
                if self.pk == publication.pk:
                    base_paths |= self.repository.distributions.values_list(
                        "base_path", flat=True)
                # Invalidate cache for all distributions serving this publication
                if base_paths:
                    Cache().delete(base_key=base_paths)

            CreatedResource.objects.filter(object_id=self.pk).delete()
            super().delete(**kwargs)
Esempio n. 7
0
 def invalidate_cache(self):
     """Invalidates the cache if repository is present."""
     if settings.CACHE_ENABLED:
         distributions = self.distributions.all()
         if distributions.exists():
             base_paths = distributions.values_list("base_path", flat=True)
             if base_paths:
                 Cache().delete(base_key=base_paths)
Esempio n. 8
0
 def test_01_basic_set_get(self):
     """Tests setting value, then getting it"""
     cache = Cache()
     cache.set("key", "hello")
     ret = cache.get("key")
     self.assertEqual(ret, b"hello")
     cache.set("key", "there")
     ret = cache.get("key")
     self.assertEqual(ret, b"there")
Esempio n. 9
0
 def test_04_basic_expires(self):
     """Tests setting values with expiration times"""
     self.skipTest("Timing is inconsistent in CI")
     cache = Cache()
     cache.set("key", "hi", expires=5)
     ret = cache.get("key")
     self.assertEqual(ret, b"hi")
     sleep(5)
     ret = cache.get("key")
     self.assertIsNone(ret)
Esempio n. 10
0
    def test_06_delete_base_key(self):
        """Tests deleting multiple key-values under one base-key"""
        cache = Cache()
        cache.delete(base_key="base1")
        self.assertFalse(cache.exists("key1", base_key="base1"))
        self.assertFalse(cache.exists("key2", base_key="base1"))
        self.assertFalse(cache.exists(base_key="base1"))

        cache.set("key1", "hi", base_key="base1")
        self.assertTrue(cache.exists("key1", base_key="base1"))
        # multi delete
        cache.delete(base_key=["base1", "base2"])
        self.assertEqual(0, cache.exists(base_key=["base1", "base2"]))
Esempio n. 11
0
 def test_07_clear(self):
     """Tests clearing the cache"""
     cache = Cache()
     tuples = [
         ("key", "hi", None),
         ("key1", "there", None),
         ("key", "hey", "base"),
         ("key1", "now", "base"),
     ]
     for key, value, base_key in tuples:
         cache.set(key, value, base_key=base_key)
     cache.redis.flushdb()
     for key, _, base_key in tuples:
         self.assertFalse(cache.exists(key, base_key=base_key))
Esempio n. 12
0
    def invalidate_cache(self, everything=False):
        """Invalidates the cache if repository is present."""
        if settings.CACHE_ENABLED:
            distributions = self.distributions.all()
            if everything:
                from .publication import Distribution, Publication

                versions = self.versions.all()
                pubs = Publication.objects.filter(repository_version__in=versions, complete=True)
                distributions |= Distribution.objects.filter(publication__in=pubs)
                distributions |= Distribution.objects.filter(repository_version__in=versions)
            if distributions.exists():
                base_paths = distributions.values_list("base_path", flat=True)
                if base_paths:
                    Cache().delete(base_key=base_paths)
Esempio n. 13
0
    def test_05_group_with_base_key(self):
        """Tests grouping multiple key-values under one base-key"""
        cache = Cache()
        tuples = [
            ("key1", "hi", "base1"),
            ("key2", "friends", "base1"),
            ("key1", "hola", "base2"),
            ("key2", "amigos", "base2"),
        ]
        for key, value, base_key in tuples:
            cache.set(key, value, base_key=base_key)
        for key, value, base_key in tuples:
            self.assertEqual(value.encode(), cache.get(key, base_key=base_key))

        dict1 = {a.encode(): b.encode() for a, b, _ in tuples[:2]}
        dict2 = {a.encode(): b.encode() for a, b, _ in tuples[2:]}
        self.assertDictEqual(dict1, cache.get(None, base_key="base1"))
        self.assertDictEqual(dict2, cache.get(None, base_key="base2"))
        self.assertTrue(cache.exists(base_key="base1"))
        self.assertTrue(cache.exists(base_key="base2"))
        self.assertEqual(2, cache.exists(base_key=["base1", "base2"]))
Esempio n. 14
0
 def invalidate_cache(self):
     """Invalidates the cache if enabled."""
     if settings.CACHE_ENABLED:
         Cache().delete(base_key=self.base_path)
Esempio n. 15
0
 def test_02_basic_exists(self):
     """Tests that keys already set exist"""
     cache = Cache()
     self.assertTrue(cache.exists("key"))
     self.assertFalse(cache.exists("absent"))
Esempio n. 16
0
 def invalidate_cache(self):
     if settings.CACHE_ENABLED:
         base_paths = self.distribution_set.values_list("base_path",
                                                        flat=True)
         if base_paths:
             Cache().delete(base_key=base_paths)
Esempio n. 17
0
from time import sleep
from django.test import TestCase
from unittest import skipUnless

from pulpcore.cache import Cache, ConnectionError

try:
    is_redis_connected = Cache().redis.ping()
except (ConnectionError, AttributeError):
    is_redis_connected = False


@skipUnless(is_redis_connected, "Could not connect to the Redis server")
class CacheBasicOperationsTestCase(TestCase):
    """Tests the basic APIs of the Cache object"""
    def test_01_basic_set_get(self):
        """Tests setting value, then getting it"""
        cache = Cache()
        cache.set("key", "hello")
        ret = cache.get("key")
        self.assertEqual(ret, b"hello")
        cache.set("key", "there")
        ret = cache.get("key")
        self.assertEqual(ret, b"there")

    def test_02_basic_exists(self):
        """Tests that keys already set exist"""
        cache = Cache()
        self.assertTrue(cache.exists("key"))
        self.assertFalse(cache.exists("absent"))