Skip to content

Commit

Permalink
Auto close fetcher session
Browse files Browse the repository at this point in the history
  • Loading branch information
alfinkel committed Aug 31, 2020
1 parent e256d2f commit 315434a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# UNRELEASED

- [FIXED] ComplianceFetcher session object is auto-closed now in tearDownClass.

# 1.2.7

- [CHANGED] Removed PyYAML dependency to resolve downstream dependency issues.
Expand Down
6 changes: 5 additions & 1 deletion compliance/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
class ComplianceFetcher(unittest.TestCase):
"""Compliance fetcher automation TestCase class."""

_multiprocess_can_split_ = True
@classmethod
def tearDownClass(cls):
"""Perform clean up."""
if hasattr(cls, '_session'):
cls._session.close()

@classmethod
def session(cls, url=None, creds=None, **headers):
Expand Down
31 changes: 28 additions & 3 deletions test/t_compliance/t_fetch/test_base_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

from compliance.config import ComplianceConfig
from compliance.fetch import ComplianceFetcher
from compliance.utils.http import BaseSession

import requests


class ComplianceFetchTest(unittest.TestCase):
Expand All @@ -30,9 +33,31 @@ def setUp(self):
# unittest.TestCase, we must pass a method in the
# constructor (otherwise, we will get a ValueError). Since we
# don't need this method, passing ``__doc__`` is enough for
# building a ComplianceCheck object successfully.
self.check = ComplianceFetcher('__doc__')
# building a ComplianceFetcher object successfully.
ComplianceFetcher.config = ComplianceConfig()
self.fetcher = ComplianceFetcher('__doc__')
self.fetcher.config.load()

def test_config(self):
"""Check that the config property returns a ComplianceConfig object."""
self.assertIsInstance(self.check.config, ComplianceConfig)
self.assertIsInstance(self.fetcher.config, ComplianceConfig)

def test_session(self):
"""Ensure that a session is constructed correctly."""
# Create a requests.Session
self.assertIsInstance(self.fetcher.session(), requests.Session)
# Recycle session, create a BaseSession and persist it
self.assertIsInstance(
self.fetcher.session(
'https://foo.bar.com', ('foo', 'bar'), foo='FOO', bar='BAR'
),
BaseSession
)
self.assertEqual(self.fetcher.session().baseurl, 'https://foo.bar.com')
self.assertEqual(self.fetcher.session().auth, ('foo', 'bar'))
self.assertEqual(
self.fetcher.session().headers['User-Agent'],
'your_org-compliance-checks'
)
self.assertEqual(self.fetcher.session().headers['foo'], 'FOO')
self.assertEqual(self.fetcher.session().headers['bar'], 'BAR')

0 comments on commit 315434a

Please sign in to comment.