Coverage for tests/test_s3utils.py: 33%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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 unittest
24try:
25 import boto3
26 from moto import mock_s3
27except ImportError:
28 boto3 = None
30 def mock_s3(cls):
31 """A no-op decorator in case moto mock_s3 can not be imported.
32 """
33 return cls
35from lsst.daf.butler.core._butlerUri.s3utils import (getS3Client, bucketExists, s3CheckFileExists,
36 setAwsEnvCredentials, unsetAwsEnvCredentials)
37from lsst.daf.butler import Location, ButlerURI
40@unittest.skipIf(not boto3, "Warning: boto3 AWS SDK not found!")
41@mock_s3
42class S3UtilsTestCase(unittest.TestCase):
43 """Test for the S3 related utilities.
44 """
45 bucketName = "test_bucket_name"
46 fileName = "testFileName"
48 def setUp(self):
49 # set up some fake credentials if they do not exist
50 self.usingDummyCredentials = setAwsEnvCredentials()
52 self.client = getS3Client()
53 try:
54 self.client.create_bucket(Bucket=self.bucketName)
55 self.client.put_object(Bucket=self.bucketName, Key=self.fileName,
56 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 # unset any potentially set dummy credentials
69 if self.usingDummyCredentials:
70 unsetAwsEnvCredentials()
72 def testBucketExists(self):
73 self.assertTrue(bucketExists(f"{self.bucketName}"))
74 self.assertFalse(bucketExists(f"{self.bucketName}_no_exist"))
76 def testFileExists(self):
77 self.assertTrue(s3CheckFileExists(client=self.client, bucket=self.bucketName,
78 path=self.fileName)[0])
79 self.assertFalse(s3CheckFileExists(client=self.client, bucket=self.bucketName,
80 path=self.fileName+"_NO_EXIST")[0])
82 datastoreRootUri = f"s3://{self.bucketName}/"
83 uri = f"s3://{self.bucketName}/{self.fileName}"
85 buri = ButlerURI(uri)
86 location = Location(datastoreRootUri, self.fileName)
88 self.assertTrue(s3CheckFileExists(client=self.client, path=buri)[0])
89 # just to make sure the overloaded keyword works correctly
90 self.assertTrue(s3CheckFileExists(buri, client=self.client)[0])
91 self.assertTrue(s3CheckFileExists(client=self.client, path=location)[0])
93 # make sure supplying strings resolves correctly too
94 self.assertTrue(s3CheckFileExists(uri, client=self.client))
95 self.assertTrue(s3CheckFileExists(uri))
98if __name__ == "__main__": 98 ↛ 99line 98 didn't jump to line 99, because the condition on line 98 was never true
99 unittest.main()