From 315434a724aa18bddf44169b3cf8707ba1106471 Mon Sep 17 00:00:00 2001 From: Al Finkelstein Date: Mon, 31 Aug 2020 14:42:49 -0500 Subject: [PATCH] Auto close fetcher session --- CHANGES.md | 4 +++ compliance/fetch.py | 6 +++- .../t_compliance/t_fetch/test_base_fetcher.py | 31 +++++++++++++++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 5b64b273..52365929 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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. diff --git a/compliance/fetch.py b/compliance/fetch.py index 8b27342f..a0867063 100644 --- a/compliance/fetch.py +++ b/compliance/fetch.py @@ -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): diff --git a/test/t_compliance/t_fetch/test_base_fetcher.py b/test/t_compliance/t_fetch/test_base_fetcher.py index f34e3099..ede5556d 100644 --- a/test/t_compliance/t_fetch/test_base_fetcher.py +++ b/test/t_compliance/t_fetch/test_base_fetcher.py @@ -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): @@ -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')