forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
222 lines (161 loc) · 5.99 KB
/
conftest.py
File metadata and controls
222 lines (161 loc) · 5.99 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Useful utilities for STS samples tests.
"""
import json
import os
import uuid
import boto3
from google.cloud import storage, storage_transfer
from google.cloud.storage_transfer import TransferJob
import pytest
@pytest.fixture(scope='module')
def project_id():
yield os.environ.get("GOOGLE_CLOUD_PROJECT")
@pytest.fixture(scope='module')
def aws_access_key_id():
yield os.environ.get("AWS_ACCESS_KEY_ID")
@pytest.fixture(scope='module')
def aws_secret_access_key():
yield os.environ.get("AWS_SECRET_ACCESS_KEY")
@pytest.fixture(scope='module')
def bucket_name():
yield f"sts-python-samples-test-{uuid.uuid4()}"
@pytest.fixture(scope='module')
def sts_service_account(project_id):
client = storage_transfer.StorageTransferServiceClient()
account = client.get_google_service_account({'project_id': project_id})
yield account.account_email
@pytest.fixture(scope='module')
def job_description_unique(project_id: str):
"""
Generate a unique job description. Attempts to find and delete a job with
this generated description after tests are ran.
"""
# Create description
client = storage_transfer.StorageTransferServiceClient()
description = f"Storage Transfer Service Samples Test - {uuid.uuid4().hex}"
yield description
# Remove job based on description as the job's name isn't predetermined
transfer_job_to_delete: TransferJob = None
transfer_jobs = client.list_transfer_jobs({
'filter': json.dumps({"projectId": project_id})
})
for transfer_job in transfer_jobs:
if transfer_job.description == description:
transfer_job_to_delete = transfer_job
break
if transfer_job_to_delete and \
transfer_job_to_delete.status != TransferJob.Status.DELETED:
client.update_transfer_job({
"job_name": transfer_job_to_delete.name,
"project_id": project_id,
"transfer_job": {
"status": storage_transfer.TransferJob.Status.DELETED
}
})
@pytest.fixture(scope='module')
def aws_source_bucket(bucket_name: str):
"""
Creates an S3 bucket for testing. Empties and auto-deletes after
tests are ran.
"""
s3_client = boto3.client('s3')
s3_resource = boto3.resource('s3')
s3_client.create_bucket(Bucket=bucket_name)
yield bucket_name
s3_resource.Bucket(bucket_name).objects.all().delete()
s3_client.delete_bucket(Bucket=bucket_name)
@pytest.fixture(scope='module')
def gcs_bucket(project_id: str, bucket_name: str):
"""
Yields and auto-cleans up a CGS bucket for use in STS jobs
"""
storage_client = storage.Client(project=project_id)
bucket = storage_client.create_bucket(bucket_name)
yield bucket
bucket.delete()
@pytest.fixture(scope='module')
def source_bucket(gcs_bucket: storage.Bucket, sts_service_account: str):
"""
Yields and auto-cleans up a CGS bucket preconfigured with necessary
STS service account read perms
"""
# Setup policy for STS
member: str = f"serviceAccount:{sts_service_account}"
objectViewer = "roles/storage.objectViewer"
bucketReader = "roles/storage.legacyBucketReader"
# Prepare policy
policy = gcs_bucket.get_iam_policy(requested_policy_version=3)
policy.bindings.append({"role": objectViewer, "members": {member}})
policy.bindings.append({"role": bucketReader, "members": {member}})
# Set policy
gcs_bucket.set_iam_policy(policy)
yield gcs_bucket
@pytest.fixture(scope='module')
def destination_bucket(gcs_bucket: storage.Bucket, sts_service_account: str):
"""
Yields and auto-cleans up a CGS bucket preconfigured with necessary
STS service account write perms
"""
# Setup policy for STS
member: str = f"serviceAccount:{sts_service_account}"
bucketWriter = "roles/storage.legacyBucketWriter"
# Prepare policy
policy = gcs_bucket.get_iam_policy(requested_policy_version=3)
policy.bindings.append({"role": bucketWriter, "members": {member}})
# Set policy
gcs_bucket.set_iam_policy(policy)
yield gcs_bucket
@pytest.fixture(scope='module')
def intermediate_bucket(gcs_bucket: storage.Bucket, sts_service_account: str):
"""
Yields and auto-cleans up a GCS bucket preconfigured with necessary
STS service account write perms
"""
# Setup policy for STS
member: str = f"serviceAccount:{sts_service_account}"
objectViewer = "roles/storage.objectViewer"
bucketReader = "roles/storage.legacyBucketReader"
bucketWriter = "roles/storage.legacyBucketWriter"
# Prepare policy
policy = gcs_bucket.get_iam_policy(requested_policy_version=3)
policy.bindings.append({"role": objectViewer, "members": {member}})
policy.bindings.append({"role": bucketReader, "members": {member}})
policy.bindings.append({"role": bucketWriter, "members": {member}})
# Set policy
gcs_bucket.set_iam_policy(policy)
yield gcs_bucket
@pytest.fixture(scope='module')
def agent_pool_name():
"""
Yields a source agent pool name
"""
# use default agent
yield ''
@pytest.fixture(scope='module')
def posix_root_directory():
"""
Yields a POSIX root directory
"""
# use arbitrary path
yield '/my-posix-root/'
@pytest.fixture(scope='module')
def manifest_file(source_bucket: storage.Bucket):
"""
Yields a transfer manifest file name
"""
# use arbitrary path and name
yield f'gs://{source_bucket.name}/test-manifest.csv'