forked from diogenes1oliveira/aws_lambda_dependency_layer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
130 lines (112 loc) · 3.72 KB
/
conftest.py
File metadata and controls
130 lines (112 loc) · 3.72 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
from contextlib import contextmanager, ExitStack, wraps
from itertools import chain
import json
import logging
import os
from uuid import uuid4
import boto3
import pytest
from common import log_call
LOGGER = logging.getLogger(__file__)
LOGGER.setLevel(os.getenv('LOGGING_LEVEL') or logging.INFO)
@pytest.fixture(scope='function')
def iam_role():
return temp_iam_role_context
@contextmanager
def temp_iam_role_context(
policy_arn='arn:aws:iam::aws:policy/service-role/AWSLambdaRole',
service='lambda.amazonaws.com',
prefix='TempRole'):
"""
Creates a temporary IAM role
"""
version = str(uuid4()).replace('-', '')[:10].upper()
role_name = prefix + version
connection_args = {}
if os.getenv('IAM_URL', None):
connection_args['endpoint_url'] = os.environ['IAM_URL']
if os.getenv('AWS_DEFAULT_REGION', None):
connection_args['region_name'] = os.environ['AWS_DEFAULT_REGION']
iam_client = boto3.client('iam', **connection_args)
with ExitStack() as stack:
role_cmd = iam_client.create_role(
RoleName=role_name,
AssumeRolePolicyDocument=json.dumps(
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": service,
},
"Effect": "Allow",
"Sid": "",
},
],
},
),
)
LOGGER.info('Created role = %s', role_name)
stack.callback(
log_call(LOGGER, iam_client.delete_role),
RoleName=role_name,
)
iam_client.attach_role_policy(
RoleName=role_name,
PolicyArn=policy_arn,
)
LOGGER.info('Attached policy %s to role = %s', policy_arn, role_name)
stack.callback(
log_call(LOGGER, iam_client.detach_role_policy),
RoleName=role_name,
PolicyArn=policy_arn,
)
yield role_cmd['Role']['Arn']
@pytest.fixture(scope='function')
def temp_bucket():
return temp_bucket_context
@contextmanager
def temp_bucket_context(prefix='temp-bucket-'):
"""
Creates a temporary bucket
"""
connection_args = {}
if os.getenv('S3_URL', None):
connection_args['endpoint_url'] = os.environ['S3_URL']
if os.getenv('AWS_DEFAULT_REGION', None):
connection_args['region_name'] = os.environ['AWS_DEFAULT_REGION']
version = str(uuid4()).replace('-', '')[:10].lower()
bucket_name = prefix + version
s3_client = boto3.client('s3', **connection_args)
s3_client.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={
'LocationConstraint': s3_client.meta.region_name,
},
)
LOGGER.info('Created bucket %s', bucket_name)
try:
yield bucket_name
finally:
LOGGER.info('Deleted bucket %s', bucket_name)
versions = s3_client.list_object_versions(
Bucket=bucket_name, MaxKeys=100)
objects = []
all_versions = chain(
versions.get('Versions', None) or [],
versions.get('DeleteMarkers', None) or [],
)
for version in all_versions:
objects.append({
'Key': version['Key'],
'VersionId': version['VersionId'],
})
if objects:
s3_client.delete_objects(
Bucket=bucket_name,
Delete={
'Objects': objects,
},
)
s3_client.delete_bucket(Bucket=bucket_name)