-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathconftest.py
More file actions
226 lines (184 loc) · 5.73 KB
/
conftest.py
File metadata and controls
226 lines (184 loc) · 5.73 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
223
224
225
226
# Copyright 2015 Planet Labs, Inc.
#
# 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.
import pytest
import boto3
from botocore.exceptions import (
ClientError as BotoClientError,
NoCredentialsError
)
from moto import mock_aws
from datalake_api import app as datalake_api
from datalake.tests import * # noqa
from datalake.common import DatalakeRecord
from datalake.tests import generate_random_metadata
YEAR_2010 = 1262304000000
# If we run with proper AWS credentials they will be used
# This will cause moto to fail
# But more critically, may impact production systems
# So we test for real credentials and fail hard if they exist
# Session fixture runs in pytest setup rather than at import time
@pytest.fixture(scope='session', autouse=True)
def verify_no_aws_credentials():
sts = boto3.client('sts')
try:
sts.get_caller_identity()
pytest.exit("Real AWS credentials detected, aborting", 3)
except NoCredentialsError:
pass
def get_client():
from datalake_api import settings
datalake_api.app.config.from_object(settings)
datalake_api.app.config['TESTING'] = True
datalake_api.app.config['AWS_REGION'] = 'us-east-1'
datalake_api.app.config['AWS_ACCESS_KEY_ID'] = 'abc'
datalake_api.app.config['AWS_SECRET_ACCESS_KEY'] = '123'
for a in ('archive_fetcher', 's3_bucket', 'dynamodb'):
try:
delattr(datalake_api.app, a)
except AttributeError:
pass
return datalake_api.app.test_client()
@pytest.fixture(scope='function')
def client():
return get_client()
@pytest.fixture
def dynamodb(request):
mock = mock_aws()
mock.start()
def tear_down():
mock.stop()
request.addfinalizer(tear_down)
return boto3.resource('dynamodb',
region_name='us-east-1',
aws_secret_access_key='123',
aws_access_key_id='abc')
attribute_definitions = [
{
'AttributeName': 'time_index_key',
'AttributeType': 'S'
},
{
'AttributeName': 'work_id_index_key',
'AttributeType': 'S'
},
{
'AttributeName': 'range_key',
'AttributeType': 'S'
}
]
key_schema = [
{
'AttributeName': 'time_index_key',
'KeyType': 'HASH'
},
{
'AttributeName': 'range_key',
'KeyType': 'RANGE'
}
]
latest_attribute_definitions = [
{
'AttributeName': 'what_where_key',
'AttributeType': 'S'
}
]
latest_key_schema = [
{
'AttributeName': 'what_where_key',
'KeyType': 'HASH'
}
]
global_secondary = [{
'IndexName': 'work-id-index',
'KeySchema': [
{
'AttributeName': 'work_id_index_key',
'KeyType': 'HASH'
},
{
'AttributeName': 'range_key',
'KeyType': 'RANGE'
}
],
'Projection': {
'ProjectionType': 'ALL'
},
'ProvisionedThroughput': {
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5,
}
}]
def _delete_table(table):
try:
table.delete()
except BotoClientError as e:
stat = e.response.get('ResponseMetadata').get('HTTPStatusCode')
code = e.response.get('Error').get('Code')
if stat == 400 and code == 'ResourceNotFoundException':
return
raise e
def _create_table(dynamodb,
table_name,
attribute_definitions,
key_schema,
global_secondary=None):
table = dynamodb.Table(table_name)
_delete_table(table)
kwargs = dict(
TableName=table_name,
AttributeDefinitions=attribute_definitions,
KeySchema=key_schema,
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
if global_secondary:
kwargs['GlobalSecondaryIndexes'] = global_secondary
dynamodb.create_table(**kwargs)
return dynamodb.Table(table_name)
def _populate_table(table, records):
with table.batch_writer() as batch:
for r in records:
batch.put_item(Item=r)
@pytest.fixture
def table_maker(request, dynamodb):
def maker(records):
table_name = 'test'
latest_table_name = 'test_latest'
table = _create_table(dynamodb, table_name, attribute_definitions, key_schema, global_secondary)
latest_table = _create_table(dynamodb, latest_table_name, latest_attribute_definitions, latest_key_schema)
_populate_table(latest_table, records)
_populate_table(table, records)
def tear_down():
_delete_table(table)
_delete_table(latest_table)
request.addfinalizer(tear_down)
return (table, latest_table)
return maker
@pytest.fixture
def record_maker(s3_file_from_metadata):
def maker(**kwargs):
m = generate_random_metadata()
m.update(**kwargs)
key = '/'.join([str(v) for v in kwargs.values()])
url = 's3://datalake-test/' + key
s3_file_from_metadata(url, m)
records = DatalakeRecord.list_from_metadata(url, m)
what = kwargs.get('what')
where = kwargs.get('where')
for record in records:
record['what_where_key'] = f"{what}:{where}"
return records
return maker