Coverage for tests/test_remote_butler.py: 59%
55 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-07 11:04 +0000
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-07 11:04 +0000
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28import os
29import unittest
30from unittest.mock import patch
32from lsst.daf.butler import Butler, Registry
33from lsst.daf.butler.datastores.file_datastore.retrieve_artifacts import (
34 determine_destination_for_retrieved_artifact,
35)
36from lsst.daf.butler.registry.tests import RegistryTests
37from lsst.resources import ResourcePath
38from pydantic import ValidationError
40try:
41 import httpx
42 from lsst.daf.butler.remote_butler import ButlerServerError, RemoteButler, RemoteButlerFactory
43except ImportError:
44 # httpx is not available in rubin-env yet, so skip these tests if it's not
45 # available
46 RemoteButler = None
48try:
49 from lsst.daf.butler.tests.server import create_test_server
50except ImportError:
51 create_test_server = None
53TESTDIR = os.path.abspath(os.path.dirname(__file__))
56@unittest.skipIf(RemoteButler is None, "httpx is not installed")
57class RemoteButlerConfigTests(unittest.TestCase):
58 """Test construction of RemoteButler via Butler()"""
60 def test_bad_config(self):
61 with self.assertRaises(ValidationError):
62 Butler({"cls": "lsst.daf.butler.remote_butler.RemoteButler", "remote_butler": {"url": "!"}})
65@unittest.skipIf(RemoteButler is None, "httpx is not installed")
66class RemoteButlerErrorHandlingTests(unittest.TestCase):
67 """Test RemoteButler error handling."""
69 def test_internal_server_error(self):
70 butler = RemoteButlerFactory.create_factory_for_url(
71 "https://doesntmatter"
72 ).create_butler_for_access_token("dontcare")
73 with patch.object(butler._client, "request") as mock:
74 mock.side_effect = httpx.HTTPError("unhandled error")
75 with self.assertRaises(ButlerServerError):
76 butler.get_dataset_type("int")
79class RemoteButlerMiscTests(unittest.TestCase):
80 """Test miscellaneous RemoteButler functionality."""
82 def test_retrieve_artifacts_security(self):
83 # Make sure that the function used to determine output file paths for
84 # retrieveArtifacts throws if a malicious server tries to escape its
85 # destination directory.
86 with self.assertRaisesRegex(ValueError, "^File path attempts to escape destination directory"):
87 determine_destination_for_retrieved_artifact(
88 ResourcePath("output_directory/"),
89 ResourcePath("../something.txt", forceAbsolute=False),
90 preserve_path=True,
91 )
93 # Make sure all paths are forced to relative paths, even if the server
94 # sends an absolute path.
95 self.assertEqual(
96 determine_destination_for_retrieved_artifact(
97 ResourcePath("/tmp/output_directory/"),
98 ResourcePath("file:///not/relative.txt"),
99 preserve_path=True,
100 ),
101 ResourcePath("/tmp/output_directory/not/relative.txt"),
102 )
105@unittest.skipIf(create_test_server is None, "Server dependencies not installed.")
106class RemoteButlerRegistryTests(RegistryTests, unittest.TestCase):
107 """Tests for RemoteButler's `Registry` shim."""
109 def setUp(self):
110 self.server_instance = self.enterContext(create_test_server(TESTDIR))
112 @classmethod
113 def getDataDir(cls) -> str:
114 return os.path.join(TESTDIR, "data", "registry")
116 def makeRegistry(self, share_repo_with: Registry | None = None) -> Registry:
117 if share_repo_with is None:
118 return self.server_instance.hybrid_butler.registry
119 else:
120 return self.server_instance.hybrid_butler._clone().registry
122 def testBasicTransaction(self):
123 # RemoteButler will never support transactions.
124 pass
126 def testNestedTransaction(self):
127 # RemoteButler will never support transactions.
128 pass
130 def testOpaque(self):
131 # This tests an internal implementation detail that isn't exposed to
132 # the client side.
133 pass
135 def testAttributeManager(self):
136 # Tests a non-public API that isn't relevant on the client side.
137 pass
140if __name__ == "__main__":
141 unittest.main()