Coverage for tests/test_s3utils.py: 34%
59 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-01 11:14 +0000
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-01 11:14 +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 program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22import os
23import unittest
24from unittest import mock
26try:
27 import boto3
28 from botocore.exceptions import ParamValidationError
30 try:
31 from moto import mock_aws # v5
32 except ImportError:
33 from moto import mock_s3 as mock_aws
34except ImportError:
35 boto3 = None
37from lsst.resources import ResourcePath
38from lsst.resources.location import Location
39from lsst.resources.s3utils import bucketExists, clean_test_environment_for_s3, getS3Client, s3CheckFileExists
42@unittest.skipIf(not boto3, "Warning: boto3 AWS SDK not found!")
43class S3UtilsTestCase(unittest.TestCase):
44 """Test for the S3 related utilities."""
46 bucketName = "test_bucket_name"
47 fileName = "testFileName"
49 def setUp(self):
50 self.enterContext(clean_test_environment_for_s3())
51 self.enterContext(mock_aws())
53 self.client = getS3Client()
54 try:
55 self.client.create_bucket(Bucket=self.bucketName)
56 self.client.put_object(Bucket=self.bucketName, Key=self.fileName, Body=b"test content")
57 except self.client.exceptions.BucketAlreadyExists:
58 pass
60 def tearDown(self):
61 objects = self.client.list_objects(Bucket=self.bucketName)
62 if "Contents" in objects:
63 for item in objects["Contents"]:
64 self.client.delete_object(Bucket=self.bucketName, Key=item["Key"])
66 self.client.delete_bucket(Bucket=self.bucketName)
68 def testBucketExists(self):
69 self.assertTrue(bucketExists(f"{self.bucketName}"))
70 self.assertFalse(bucketExists(f"{self.bucketName}_no_exist"))
72 def testCephBucket(self):
73 with mock.patch.dict(os.environ, {"LSST_DISABLE_BUCKET_VALIDATION": "N"}):
74 self.assertEqual(os.environ["LSST_DISABLE_BUCKET_VALIDATION"], "N")
75 local_client = getS3Client()
76 with self.assertRaises(ParamValidationError):
77 bucketExists("foo:bar", local_client)
78 with mock.patch.dict(os.environ, {"LSST_DISABLE_BUCKET_VALIDATION": "1"}):
79 self.assertEqual(os.environ["LSST_DISABLE_BUCKET_VALIDATION"], "1")
80 local_client = getS3Client()
81 self.assertFalse(bucketExists("foo:bar", local_client))
83 def testFileExists(self):
84 self.assertTrue(s3CheckFileExists(client=self.client, bucket=self.bucketName, path=self.fileName)[0])
85 self.assertFalse(
86 s3CheckFileExists(client=self.client, bucket=self.bucketName, path=self.fileName + "_NO_EXIST")[0]
87 )
89 datastoreRootUri = f"s3://{self.bucketName}/"
90 uri = f"s3://{self.bucketName}/{self.fileName}"
92 buri = ResourcePath(uri)
93 location = Location(datastoreRootUri, self.fileName)
95 self.assertTrue(s3CheckFileExists(client=self.client, path=buri)[0])
96 # just to make sure the overloaded keyword works correctly
97 self.assertTrue(s3CheckFileExists(buri, client=self.client)[0])
98 self.assertTrue(s3CheckFileExists(client=self.client, path=location)[0])
100 # make sure supplying strings resolves correctly too
101 self.assertTrue(s3CheckFileExists(uri, client=self.client))
102 self.assertTrue(s3CheckFileExists(uri))
105if __name__ == "__main__":
106 unittest.main()