Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto close fetcher session #64

Merged
merged 1 commit into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')