-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_s3utils.py
135 lines (110 loc) · 5.17 KB
/
test_s3utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# This file is part of daf_butler.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (http://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import unittest
from unittest import mock
try:
import boto3
from botocore.exceptions import ParamValidationError
try:
from moto import mock_aws # v5
except ImportError:
from moto import mock_s3 as mock_aws
except ImportError:
boto3 = None
from urllib3.exceptions import LocationParseError
from lsst.resources import ResourcePath
from lsst.resources.location import Location
from lsst.resources.s3utils import (
_parse_endpoint_config,
bucketExists,
clean_test_environment_for_s3,
getS3Client,
s3CheckFileExists,
)
@unittest.skipIf(not boto3, "Warning: boto3 AWS SDK not found!")
class S3UtilsTestCase(unittest.TestCase):
"""Test for the S3 related utilities."""
bucketName = "test_bucket_name"
fileName = "testFileName"
def setUp(self):
self.enterContext(clean_test_environment_for_s3())
self.enterContext(mock_aws())
self.client = getS3Client()
try:
self.client.create_bucket(Bucket=self.bucketName)
self.client.put_object(Bucket=self.bucketName, Key=self.fileName, Body=b"test content")
except self.client.exceptions.BucketAlreadyExists:
pass
def tearDown(self):
objects = self.client.list_objects(Bucket=self.bucketName)
if "Contents" in objects:
for item in objects["Contents"]:
self.client.delete_object(Bucket=self.bucketName, Key=item["Key"])
self.client.delete_bucket(Bucket=self.bucketName)
def testBucketExists(self):
self.assertTrue(bucketExists(f"{self.bucketName}"))
self.assertFalse(bucketExists(f"{self.bucketName}_no_exist"))
def testCephBucket(self):
with mock.patch.dict(os.environ, {"LSST_DISABLE_BUCKET_VALIDATION": "N"}):
self.assertEqual(os.environ["LSST_DISABLE_BUCKET_VALIDATION"], "N")
local_client = getS3Client()
with self.assertRaises(ParamValidationError):
bucketExists("foo:bar", local_client)
with mock.patch.dict(os.environ, {"LSST_DISABLE_BUCKET_VALIDATION": "1"}):
self.assertEqual(os.environ["LSST_DISABLE_BUCKET_VALIDATION"], "1")
local_client = getS3Client()
self.assertFalse(bucketExists("foo:bar", local_client))
def testFileExists(self):
self.assertTrue(s3CheckFileExists(client=self.client, bucket=self.bucketName, path=self.fileName)[0])
self.assertFalse(
s3CheckFileExists(client=self.client, bucket=self.bucketName, path=self.fileName + "_NO_EXIST")[0]
)
datastoreRootUri = f"s3://{self.bucketName}/"
uri = f"s3://{self.bucketName}/{self.fileName}"
buri = ResourcePath(uri)
location = Location(datastoreRootUri, self.fileName)
self.assertTrue(s3CheckFileExists(client=self.client, path=buri)[0])
# just to make sure the overloaded keyword works correctly
self.assertTrue(s3CheckFileExists(buri, client=self.client)[0])
self.assertTrue(s3CheckFileExists(client=self.client, path=location)[0])
# make sure supplying strings resolves correctly too
self.assertTrue(s3CheckFileExists(uri, client=self.client))
self.assertTrue(s3CheckFileExists(uri))
def test_parsing_profile_config(self):
with self.assertRaises(LocationParseError):
_parse_endpoint_config(
"https://AKIAIOSFODNN7EXAMPLE:wJalrXUtnFEMI/FK7MDENG/[email protected]"
)
parsed = _parse_endpoint_config(
"https://AKIAIOSFODNN7EXAMPLE:wJalrXUtnFEMI%2FK7MDENG%[email protected]"
)
self.assertEqual(parsed.endpoint_url, "https://endpoint.com")
self.assertEqual(parsed.access_key_id, "AKIAIOSFODNN7EXAMPLE")
self.assertEqual(parsed.secret_access_key, "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY")
simple = _parse_endpoint_config("https://other.endpoint.com")
self.assertEqual(simple.endpoint_url, "https://other.endpoint.com")
self.assertIsNone(simple.access_key_id)
self.assertIsNone(simple.secret_access_key)
with self.assertRaisesRegex(ValueError, "S3 access key and secret not in expected format."):
_parse_endpoint_config("https://[email protected]")
if __name__ == "__main__":
unittest.main()