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
Fix #73. This adds tests for consuming content from a repository that
has been configured with the `on_demand` download policy. This also adds
a method to reset Squid and its cache, and also fixes the `reset_pulp`
method.

Test results prior to this commit:

Test results after this commit:
  • Loading branch information
jeremycline committed Feb 22, 2016
1 parent a774634 commit c31c35c
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pulp_smash/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@
``urllib.parse.urlparse``.)
"""

RPM_SHA256_CHECKSUM = '7a831f9f90bf4d21027572cb503d20b702de8e8785b02c03' \
'97445c2e481d81b3'
"""The sha256 checksum of the RPM file.
See :data:`pulp_smash.constants.RPM`.
"""

USER_PATH = '/pulp/api/v2/users/'
"""See: `User APIs`_.
Expand Down
98 changes: 98 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 @@ -46,6 +47,7 @@
REPOSITORY_PATH,
RPM,
RPM_FEED_URL,
RPM_SHA256_CHECKSUM,
)


Expand Down Expand Up @@ -420,3 +422,99 @@ 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(utils.BaseAPITestCase):
"""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)

client = api.Client(cls.cfg, api.json_handler)

# Create the repository
body = _gen_repo()
body['importer_config'] = {
'feed': RPM_FEED_URL,
'download_policy': 'on_demand',
}
distributor = _gen_distributor()
distributor['auto_publish'] = True
distributor['distributor_config']['relative_url'] = body['id']
body['distributors'] = [distributor]

repo = client.post(REPOSITORY_PATH, body)

# Sync the repository
sync_path = urljoin(repo['_href'], 'actions/sync/')
client.post(sync_path, {'override_config': {}})

cls.repo = client.get(repo['_href'], params={'details': True})

client.response_handler = api.safe_handler
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_unit_count = sum([
count for name, count in self.repo['content_unit_counts'].items()
if name not in ('rpm', 'drpm', 'srpm')
])
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(
{'s-maxage=86400', 'public', 'max-age=86400'},
set(self.rpm.headers['Cache-Control'].split(', '))
)

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'])
26 changes: 24 additions & 2 deletions pulp_smash/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ 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()
Expand Down Expand Up @@ -106,3 +106,25 @@ def tearDownClass(cls):
client = api.Client(cls.cfg)
for resource in cls.resources:
client.delete(resource)


def reset_squid(server_config):
"""Stop Squid, reset its cache directory, and restart it.
:param pulp_smash.config.ServerConfig server_config: Information about the
Pulp server being targeted.
:returns: Nothing.
"""
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 c31c35c

Please sign in to comment.