Skip to content

Commit

Permalink
Use mock.patch.dict to mock env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
bgounon committed Aug 25, 2020
1 parent 2df7560 commit ebf3ebe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
16 changes: 11 additions & 5 deletions tests/test_butler.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def mock_s3(cls):
from lsst.daf.butler.core.repoRelocation import BUTLER_ROOT_TAG
from lsst.daf.butler.core.s3utils import (setAwsEnvCredentials,
unsetAwsEnvCredentials)
from lsst.daf.butler.core.webdavutils import isWebdavEndpoint

from lsst.daf.butler.tests import MultiDetectorFormatter, MetricsExample

Expand Down Expand Up @@ -1202,7 +1203,10 @@ def tearDown(self):
unsetAwsEnvCredentials()


@unittest.skipIf(WsgiDAVApp is None, "Warning: wsgi/cheroot not found!")
@unittest.skipIf(WsgiDAVApp is None, "Warning: wsgidav/cheroot not found!")
# Mock required environment variables during tests
@unittest.mock.patch.dict(os.environ, {"WEBDAV_AUTH_METHOD": "TOKEN",
"WEBDAV_BEARER_TOKEN": "XXXXXX"})
class WebdavDatastoreButlerTestCase(FileLikeDatastoreButlerTests, unittest.TestCase):
"""WebdavDatastore specialization of a butler; a Webdav storage Datastore +
a local in-memory SqlRegistry.
Expand Down Expand Up @@ -1257,10 +1261,10 @@ def setUpClass(cls):
# Wait for it to start
time.sleep(3)

# Mock required environment variables during tests
@unittest.mock.patch.dict(os.environ, {"WEBDAV_AUTH_METHOD": "TOKEN",
"WEBDAV_BEARER_TOKEN": "XXXXXX"})
def setUp(self):
os.environ["WEBDAV_AUTH_METHOD"] = "TOKEN"
os.environ["WEBDAV_BEARER_TOKEN"] = "XXXXXX"

config = Config(self.configFile)
uri = ButlerURI(config[".datastore.datastore.root"])
self.serverName = uri.netloc
Expand All @@ -1273,13 +1277,15 @@ def setUp(self):
self.datastoreStr = f"datastore={self.root}"
self.datastoreName = [f"WebdavDatastore@{self.rooturi}"]

from lsst.daf.butler.core.webdavutils import isWebdavEndpoint
if not isWebdavEndpoint(self.rooturi):
raise OSError("Webdav server not running properly: cannot run tests.")

Butler.makeRepo(self.rooturi, config=config, forceConfigRoot=False)
self.tmpConfigFile = posixpath.join(self.rooturi, "butler.yaml")

# Mock required environment variables during tests
@unittest.mock.patch.dict(os.environ, {"WEBDAV_AUTH_METHOD": "TOKEN",
"WEBDAV_BEARER_TOKEN": "XXXXXX"})
def tearDown(self):
ButlerURI(self.rooturi).remove()

Expand Down
6 changes: 3 additions & 3 deletions tests/test_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,12 @@ def testQuoting(self):
self.assertEqual(child.unquoted_path, "/" + subpath)


# Mock required environment variables during tests
@unittest.mock.patch.dict(os.environ, {"WEBDAV_AUTH_METHOD": "TOKEN",
"WEBDAV_BEARER_TOKEN": "XXXXXX"})
class WebdavURITestCase(unittest.TestCase):

def setUp(self):
os.environ["WEBDAV_AUTH_METHOD"] = "TOKEN"
os.environ["WEBDAV_BEARER_TOKEN"] = "XXXXXX"

serverRoot = "www.not-exists.orgx"
existingFolderName = "existingFolder"
existingFileName = "existingFile"
Expand Down

0 comments on commit ebf3ebe

Please sign in to comment.