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

test: Use mock instead of mox. #195

Merged
merged 4 commits into from
Jun 3, 2023
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
3 changes: 3 additions & 0 deletions nss_cache/sources/consulsource.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ def _ReadEntry(self, name, entry):
return None

try:
s = entry.get("members", "").decode("utf-8")
members = s.split("\n")
except AttributeError:
members = entry.get("members", "").split("\n")
except (ValueError, TypeError):
members = [""]
Expand Down
1 change: 0 additions & 1 deletion nss_cache/sources/consulsource_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ def setUp(self):
self.good_entry.members = ["foo", "bar"]
self.parser = consulsource.ConsulGroupMapParser()

@unittest.skip("broken")
def testGetMap(self):
group_map = group.GroupMap()
cache_info = StringIO(
Expand Down
6 changes: 5 additions & 1 deletion nss_cache/sources/gcssource_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
from nss_cache.maps import group
from nss_cache.maps import passwd
from nss_cache.maps import shadow
from nss_cache.sources import gcssource
from nss_cache.util import file_formats
from nss_cache.util import timestamps

try:
from nss_cache.sources import gcssource
except Exception as e:
raise unittest.SkipTest("`gcssource` unabled to be imported: {}".format(e))


class TestGcsSource(unittest.TestCase):
def setUp(self):
Expand Down
57 changes: 15 additions & 42 deletions nss_cache/sources/httpsource_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
import time
import unittest
import pycurl
from mox3 import mox
from unittest import mock
from io import BytesIO

from nss_cache import error
from nss_cache.maps import automount
Expand Down Expand Up @@ -224,10 +222,13 @@ def testCreateMap(self):
self.assertTrue(isinstance(self.updater.CreateMap(), passwd.PasswdMap))


class TestShadowUpdateGetter(mox.MoxTestBase):
class TestShadowUpdateGetter(unittest.TestCase):
def setUp(self):
super(TestShadowUpdateGetter, self).setUp()
self.updater = httpsource.ShadowUpdateGetter()
curl_patcher = mock.patch.object(curl, "CurlFetch")
self.addCleanup(curl_patcher.stop)
self.curl_fetch_mock = curl_patcher.start()

def testGetParser(self):
parser = self.updater.GetParser()
Expand All @@ -239,57 +240,29 @@ def testCreateMap(self):
self.assertTrue(isinstance(self.updater.CreateMap(), shadow.ShadowMap))

def testShadowGetUpdatesWithContent(self):
mock_conn = self.mox.CreateMockAnything()
mock_conn.setopt(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes()
mock_conn.getinfo(pycurl.INFO_FILETIME).AndReturn(-1)

self.mox.StubOutWithMock(pycurl, "Curl")
pycurl.Curl().AndReturn(mock_conn)

self.mox.StubOutWithMock(curl, "CurlFetch")

curl.CurlFetch("https://TEST_URL", mock_conn, self.updater.log).AndReturn(
[
200,
"",
BytesIO(
b"""usera:x:::::::
self.curl_fetch_mock.return_value = (
200,
"",
b"""usera:x:::::::
userb:x:::::::
"""
).getvalue(),
]
""",
)

self.mox.ReplayAll()
config = {}
source = httpsource.HttpFilesSource(config)
result = self.updater.GetUpdates(source, "https://TEST_URL", 1)
print(result)
self.assertEqual(len(result), 2)

def testShadowGetUpdatesWithBz2Content(self):
mock_conn = self.mox.CreateMockAnything()
mock_conn.setopt(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes()
mock_conn.getinfo(pycurl.INFO_FILETIME).AndReturn(-1)

self.mox.StubOutWithMock(pycurl, "Curl")
pycurl.Curl().AndReturn(mock_conn)

self.mox.StubOutWithMock(curl, "CurlFetch")

curl.CurlFetch("https://TEST_URL", mock_conn, self.updater.log).AndReturn(
[
200,
"",
BytesIO(
base64.b64decode(
"QlpoOTFBWSZTWfm+rXYAAAvJgAgQABAyABpAIAAhKm1GMoQAwRSpHIXejGQgz4u5IpwoSHzfVrsA"
)
).getvalue(),
]
self.curl_fetch_mock.return_value = (
200,
"",
base64.b64decode(
"QlpoOTFBWSZTWfm+rXYAAAvJgAgQABAyABpAIAAhKm1GMoQAwRSpHIXejGQgz4u5IpwoSHzfVrsA"
),
)

self.mox.ReplayAll()
config = {}
source = httpsource.HttpFilesSource(config)
result = self.updater.GetUpdates(source, "https://TEST_URL", 1)
Expand Down
Loading