Skip to content

Commit

Permalink
Merge pull request #195 from google/nomox
Browse files Browse the repository at this point in the history
test: Use mock instead of mox.
  • Loading branch information
jaqx0r authored Jun 3, 2023
2 parents 0abee0c + 91604c5 commit 6f55732
Show file tree
Hide file tree
Showing 6 changed files with 417 additions and 792 deletions.
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

0 comments on commit 6f55732

Please sign in to comment.