Skip to content

Commit

Permalink
Add tests for lazy on-demand syncing for RPM.
Browse files Browse the repository at this point in the history
This closes issue #73
  • Loading branch information
jeremycline committed Feb 16, 2016
1 parent 45cadf4 commit 1920f06
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pulp_smash/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
``urllib.parse.urlparse``.)
"""

RPM_SHA256_CHECKSUM = '7a831f9f90bf4d21027572cb503d20b702de8e8785b02c0397445c2e481d81b3'
"""The sha256 checksum of the RPM file. See :data:`pulp_smash.constants.RPM`."""

USER_PATH = '/pulp/api/v2/users/'
"""See: `User APIs`_.
Expand Down
117 changes: 117 additions & 0 deletions pulp_smash/tests/rpm/api_v2/test_sync_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"""
from __future__ import unicode_literals

import hashlib
from itertools import product
try: # try Python 3 import first
from urllib.parse import urljoin
Expand All @@ -47,6 +48,7 @@
REPOSITORY_PATH,
RPM,
RPM_FEED_URL,
RPM_SHA256_CHECKSUM,
)


Expand Down Expand Up @@ -77,6 +79,39 @@ def _gen_distributor():
}


def _sync_and_publish_repo(server_config, importer=None, distributors=None):
"""Create and sync an RPM repository.
:param server_config: A :class:`pulp_smash.config.ServerConfig` object.
:param importer: A dict-like object with importer configuration.
:returns: A tuple of
"""
# Create the repository
body = _gen_repo()

if importer is None:
importer = {'feed': RPM_FEED_URL}
body['importer_config'] = importer

if distributors is None:
distributor = _gen_distributor()
distributor['auto_publish'] = True
distributor['distributor_config']['relative_url'] = body['id']
distributors = [distributor]
body['distributors'] = distributors

client = api.Client(server_config, api.json_handler)

repo = client.post(REPOSITORY_PATH, body)

# Sync the repository
path = urljoin(repo['_href'], 'actions/sync/')
sync = client.post(path, {'override_config': {}})
tasks = tuple(utils.poll_spawned_tasks(server_config, sync))

return repo, tasks


class _BaseTestCase(unittest2.TestCase):
"""Provide a server config, and tear down created resources."""

Expand Down Expand Up @@ -438,3 +473,85 @@ def test_unit_integrity(self):
for i, module in enumerate(self.rpms[1:]):
with self.subTest(i=i):
self.assertEqual(self.rpms[0], module)


class SyncOnDemandTestCase(_BaseTestCase):
"""Assert Yum supports on-demand syncing."""

@classmethod
def setUpClass(cls):
"""Create an RPM repository with a valid feed and sync it.
Do the following:
0. Reset Pulp, including the Squid cache.
1. Sync/publish a repository using the 'on_demand' download policy.
2. Download an RPM from the published repository.
3. Download the same RPM to ensure it is served by the cache.
"""
super(SyncOnDemandTestCase, cls).setUpClass()

# Required to ensure the `locally_stored_units` is 0 before we start.
utils.reset_squid(cls.cfg)
utils.reset_pulp(cls.cfg)
importer_config = {
'feed': RPM_FEED_URL,
'download_policy': 'on_demand',
}
repo, tasks = _sync_and_publish_repo(
cls.cfg,
importer=importer_config
)
client = api.Client(cls.cfg, api.echo_handler)
cls.repo = client.get(repo['_href'], params={'details': True}).json()

url = urljoin(
'/pulp/repos/',
repo['id'] + '/',
)
url = urljoin(url, RPM)
cls.rpm = client.get(url)
cls.same_rpm = client.get(url)

def test_local_units(self):
"""Assert no content units were downloaded besides metadata."""
metadata_units = filter(
lambda unit_tuple: unit_tuple[0] not in ('rpm', 'drpm', 'srpm'),
self.repo['content_unit_counts'].items()
)
metadata_unit_count = sum([unit[1] for unit in metadata_units])
self.assertEqual(self.repo['locally_stored_units'], metadata_unit_count)

def test_repository_units(self):
"""Assert there is at least one content unit in the repository."""
total_units = sum(self.repo['content_unit_counts'].values())
self.assertEqual(self.repo['total_repository_units'], total_units)

def test_request_history(self):
"""Assert that the initial request received a 302 Redirect."""
self.assertTrue(self.rpm.history[0].is_redirect)

def test_rpm_checksum(self):
"""Assert the checksum of the downloaded RPM matches the metadata."""
checksum = hashlib.sha256(self.rpm.content).hexdigest()
self.assertEqual(RPM_SHA256_CHECKSUM, checksum)

def test_rpm_cache_lookup_header(self):
"""Assert the first request resulted in a cache miss from Squid."""
self.assertIn('MISS', self.rpm.headers['X-Cache-Lookup'])

def test_rpm_cache_control_header(self):
"""Assert that the request has the Cache-Control header set."""
self.assertEqual(
'public, s-maxage=86400, max-age=86400',
self.rpm.headers['Cache-Control']
)

def test_same_rpm_checksum(self):
"""Assert the checksum of the second RPM matches the metadata."""
checksum = hashlib.sha256(self.same_rpm.content).hexdigest()
self.assertEqual(RPM_SHA256_CHECKSUM, checksum)

def test_same_rpm_cache_header(self):
"""Assert the second request resulted in a cache hit from Squid."""
self.assertIn('HIT', self.same_rpm.headers['X-Cache-Lookup'])
25 changes: 23 additions & 2 deletions pulp_smash/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,29 @@ def reset_pulp(server_config):
prefix = '' if client.run(('id', '-u')).stdout.strip() == '0' else 'sudo '
client.run('mongo pulp_database --eval db.dropDatabase()'.split())
client.run('sudo -u apache pulp-manage-db'.split())
client.run((prefix + 'rm -rf /var/lib/pulp/content/*').split())
client.run((prefix + 'rm -rf /var/lib/pulp/published/*').split())
client.run((prefix + 'rm -rf /var/lib/pulp/content').split())
client.run((prefix + 'rm -rf /var/lib/pulp/published').split())

for service in services:
service.start()


def reset_squid(server_config):
"""
:param server_config:
:return:
"""
squid_service = cli.Service(server_config, 'squid')
squid_service.stop()

# Clean out the cache directory and reinitialize it.
client = cli.Client(server_config)
prefix = '' if client.run(('id', '-u')).stdout.strip() == '0' else 'sudo '
client.run((prefix + 'rm -rf /var/spool/squid').split())
client.run((prefix + 'mkdir --context=system_u:object_r:squid_cache_t:s0' +
' --mode=750 /var/spool/squid').split())
client.run((prefix + 'chown squid:squid /var/spool/squid').split())
client.run((prefix + 'squid -z').split())

squid_service.start()

0 comments on commit 1920f06

Please sign in to comment.