Coverage for tests/test_s3utils.py: 31%
56 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-23 10:46 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-23 10:46 +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
29 from moto import mock_s3
30except ImportError:
31 boto3 = None
33from lsst.resources import ResourcePath
34from lsst.resources.location import Location
35from lsst.resources.s3utils import bucketExists, clean_test_environment_for_s3, getS3Client, s3CheckFileExists
38@unittest.skipIf(not boto3, "Warning: boto3 AWS SDK not found!")
39class S3UtilsTestCase(unittest.TestCase):
40 """Test for the S3 related utilities."""
42 bucketName = "test_bucket_name"
43 fileName = "testFileName"
45 def setUp(self):
46 self.enterContext(clean_test_environment_for_s3())
47 self.enterContext(mock_s3())
49 self.client = getS3Client()
50 try:
51 self.client.create_bucket(Bucket=self.bucketName)
52 self.client.put_object(Bucket=self.bucketName, Key=self.fileName, Body=b"test content")
53 except self.client.exceptions.BucketAlreadyExists:
54 pass
56 def tearDown(self):
57 objects = self.client.list_objects(Bucket=self.bucketName)
58 if "Contents" in objects:
59 for item in objects["Contents"]:
60 self.client.delete_object(Bucket=self.bucketName, Key=item["Key"])
62 self.client.delete_bucket(Bucket=self.bucketName)
64 def testBucketExists(self):
65 self.assertTrue(bucketExists(f"{self.bucketName}"))
66 self.assertFalse(bucketExists(f"{self.bucketName}_no_exist"))
68 def testCephBucket(self):
69 with mock.patch.dict(os.environ, {"LSST_DISABLE_BUCKET_VALIDATION": "N"}):
70 self.assertEqual(os.environ["LSST_DISABLE_BUCKET_VALIDATION"], "N")
71 local_client = getS3Client()
72 with self.assertRaises(ParamValidationError):
73 bucketExists("foo:bar", local_client)
74 with mock.patch.dict(os.environ, {"LSST_DISABLE_BUCKET_VALIDATION": "1"}):
75 self.assertEqual(os.environ["LSST_DISABLE_BUCKET_VALIDATION"], "1")
76 local_client = getS3Client()
77 self.assertFalse(bucketExists("foo:bar", local_client))
79 def testFileExists(self):
80 self.assertTrue(s3CheckFileExists(client=self.client, bucket=self.bucketName, path=self.fileName)[0])
81 self.assertFalse(
82 s3CheckFileExists(client=self.client, bucket=self.bucketName, path=self.fileName + "_NO_EXIST")[0]
83 )
85 datastoreRootUri = f"s3://{self.bucketName}/"
86 uri = f"s3://{self.bucketName}/{self.fileName}"
88 buri = ResourcePath(uri)
89 location = Location(datastoreRootUri, self.fileName)
91 self.assertTrue(s3CheckFileExists(client=self.client, path=buri)[0])
92 # just to make sure the overloaded keyword works correctly
93 self.assertTrue(s3CheckFileExists(buri, client=self.client)[0])
94 self.assertTrue(s3CheckFileExists(client=self.client, path=location)[0])
96 # make sure supplying strings resolves correctly too
97 self.assertTrue(s3CheckFileExists(uri, client=self.client))
98 self.assertTrue(s3CheckFileExists(uri))
101if __name__ == "__main__":
102 unittest.main()