|
3 | 3 | from sevenseconds.helper.aws import get_account_id, get_az_names |
4 | 4 | from sevenseconds.config.cloudtrail import configure_cloudtrail |
5 | 5 | from sevenseconds.config.s3 import configure_s3_buckets |
6 | | -from datetime import datetime |
7 | | -import botocore.exceptions |
8 | 6 |
|
9 | 7 |
|
10 | 8 | def test_get_account_id(monkeypatch): |
11 | | - sts = MagicMock(get_caller_identity=lambda: {'Account': '01234567', |
12 | | - 'Arn': 'arn:aws:iam::01234567:assumed-role/Administrator/sevenseconds', |
13 | | - 'UserId': 'ABCDEFGHIJKLMNOPQ:sevenseconds'}) |
| 9 | + sts = MagicMock( |
| 10 | + get_caller_identity=lambda: { |
| 11 | + "Account": "01234567", |
| 12 | + "Arn": "arn:aws:iam::01234567:assumed-role/Administrator/sevenseconds", |
| 13 | + "UserId": "ABCDEFGHIJKLMNOPQ:sevenseconds", |
| 14 | + } |
| 15 | + ) |
14 | 16 | session = MagicMock(client=MagicMock(return_value=sts)) |
15 | 17 | id = get_account_id(session) |
16 | | - assert id == '01234567', 'ID from current User' |
| 18 | + assert id == "01234567", "ID from current User" |
17 | 19 |
|
18 | 20 |
|
19 | 21 | def test_get_az_names(monkeypatch): |
20 | | - conn = MagicMock(describe_availability_zones=lambda **kargs: { |
21 | | - 'AvailabilityZones': [ |
22 | | - { |
23 | | - 'ZoneName': 'eu-west-1a', |
24 | | - 'RegionName': 'eu-west-1', |
25 | | - 'State': 'available', |
26 | | - 'Messages': [] |
27 | | - }, { |
28 | | - 'ZoneName': 'eu-west-1b', |
29 | | - 'RegionName': 'eu-west-1', |
30 | | - 'State': 'available', |
31 | | - 'Messages': [] |
32 | | - }, { |
33 | | - 'ZoneName': 'eu-west-1c', |
34 | | - 'RegionName': 'eu-west-1', |
35 | | - 'State': 'available', |
36 | | - 'Messages': [] |
37 | | - }]}) |
| 22 | + conn = MagicMock( |
| 23 | + describe_availability_zones=lambda **kargs: { |
| 24 | + "AvailabilityZones": [ |
| 25 | + {"ZoneName": "eu-west-1a", "RegionName": "eu-west-1", "State": "available", "Messages": []}, |
| 26 | + {"ZoneName": "eu-west-1b", "RegionName": "eu-west-1", "State": "available", "Messages": []}, |
| 27 | + {"ZoneName": "eu-west-1c", "RegionName": "eu-west-1", "State": "available", "Messages": []}, |
| 28 | + ] |
| 29 | + } |
| 30 | + ) |
38 | 31 | session = MagicMock(client=MagicMock(return_value=conn)) |
39 | | - names = get_az_names(session, 'eu-west-1') |
40 | | - assert 'eu-west-1b' in names, 'AZ found' |
| 32 | + names = get_az_names(session, "eu-west-1") |
| 33 | + assert "eu-west-1b" in names, "AZ found" |
41 | 34 |
|
42 | | - conn = MagicMock(describe_availability_zones=lambda **kargs: { |
43 | | - 'AvailabilityZones': []}) |
| 35 | + conn = MagicMock(describe_availability_zones=lambda **kargs: {"AvailabilityZones": []}) |
44 | 36 | session = MagicMock(client=MagicMock(return_value=conn)) |
45 | | - names = get_az_names(session, 'eu-west-1') |
46 | | - assert 'eu-west-1b' in names, 'AZ found from Cache' |
| 37 | + names = get_az_names(session, "eu-west-1") |
| 38 | + assert "eu-west-1b" in names, "AZ found from Cache" |
47 | 39 |
|
48 | 40 |
|
49 | 41 | def test_configure_cloudtrail(monkeypatch): |
50 | 42 | def myinfo(text): |
51 | | - assert 'Found no Cloudtrail Section in Configfile.' in text |
| 43 | + assert "Found no Cloudtrail Section in Configfile." in text |
52 | 44 |
|
53 | | - monkeypatch.setattr('clickclick.info', myinfo) |
54 | | - account = MagicMock(name='name', |
55 | | - config={}) |
| 45 | + monkeypatch.setattr("clickclick.info", myinfo) |
| 46 | + account = MagicMock(name="name", config={}) |
56 | 47 | configure_cloudtrail(account) |
57 | 48 |
|
58 | 49 | class _test: |
59 | 50 | def _only_kwargs(f): |
60 | 51 | def _filter(*args, **kwargs): |
61 | 52 | if args or len(kwargs) == 0: |
62 | | - raise TypeError('{} only accepts keyword arguments.'.format(f.__name__)) |
| 53 | + raise TypeError("{} only accepts keyword arguments.".format(f.__name__)) |
63 | 54 | return f(**kwargs) |
| 55 | + |
64 | 56 | return _filter |
65 | 57 |
|
66 | 58 | def describe_trails(): |
67 | 59 | return { |
68 | | - 'trailList': [ |
| 60 | + "trailList": [ |
69 | 61 | { |
70 | | - 'IncludeGlobalServiceEvents': True, |
71 | | - 'Name': 'Default', |
72 | | - 'S3BucketName': 'bucketname', |
73 | | - 'S3KeyPrefix': '' |
74 | | - }]} |
| 62 | + "IncludeGlobalServiceEvents": True, |
| 63 | + "Name": "Default", |
| 64 | + "S3BucketName": "bucketname", |
| 65 | + "S3KeyPrefix": "", |
| 66 | + } |
| 67 | + ] |
| 68 | + } |
75 | 69 |
|
76 | 70 | @_only_kwargs |
77 | 71 | def update_trail(Name, S3KeyPrefix, S3BucketName, IncludeGlobalServiceEvents, **kwargs): |
78 | | - assert Name == 'Default', 'update Default' |
79 | | - assert S3BucketName == 'bucketname', 'set bucketname' |
80 | | - assert S3KeyPrefix == '', 'set directory prefix' |
81 | | - assert IncludeGlobalServiceEvents is True, 'Include global' |
| 72 | + assert Name == "Default", "update Default" |
| 73 | + assert S3BucketName == "bucketname", "set bucketname" |
| 74 | + assert S3KeyPrefix == "", "set directory prefix" |
| 75 | + assert IncludeGlobalServiceEvents is True, "Include global" |
82 | 76 |
|
83 | 77 | @_only_kwargs |
84 | 78 | def create_trail(Name, S3KeyPrefix, S3BucketName, IncludeGlobalServiceEvents, **kwargs): |
85 | | - assert Name == 'Default', 'update Default' |
86 | | - assert S3BucketName == 'bucketname', 'set bucketname' |
87 | | - assert S3KeyPrefix == '', 'set directory prefix' |
88 | | - assert IncludeGlobalServiceEvents is True, 'Include global' |
| 79 | + assert Name == "Default", "update Default" |
| 80 | + assert S3BucketName == "bucketname", "set bucketname" |
| 81 | + assert S3KeyPrefix == "", "set directory prefix" |
| 82 | + assert IncludeGlobalServiceEvents is True, "Include global" |
89 | 83 |
|
90 | 84 | @_only_kwargs |
91 | 85 | def start_logging(Name): |
92 | | - assert Name == 'Default', 'start logging for Default' |
| 86 | + assert Name == "Default", "start logging for Default" |
93 | 87 |
|
94 | 88 | @_only_kwargs |
95 | 89 | def stop_logging(Name): |
96 | | - assert Name == 'wrongconfig', 'stop wrong configuration' |
| 90 | + assert Name == "wrongconfig", "stop wrong configuration" |
97 | 91 |
|
98 | 92 | @_only_kwargs |
99 | 93 | def delete_trail(Name): |
100 | | - assert Name == 'wrongconfig', 'remove wrong configuration' |
| 94 | + assert Name == "wrongconfig", "remove wrong configuration" |
101 | 95 |
|
102 | 96 | @_only_kwargs |
103 | 97 | def get_trail_status(Name): |
104 | | - return {'IsLogging': True} |
| 98 | + return {"IsLogging": True} |
105 | 99 |
|
106 | | - account = MagicMock(name='name', |
107 | | - config={'cloudtrail': {'s3_bucket_name': 'bucketname', 's3_key_prefix': ''}}, |
108 | | - client=MagicMock(return_value=_test)) |
| 100 | + account = MagicMock( |
| 101 | + name="name", |
| 102 | + config={"cloudtrail": {"s3_bucket_name": "bucketname", "s3_key_prefix": ""}}, |
| 103 | + client=MagicMock(return_value=_test), |
| 104 | + ) |
109 | 105 | configure_cloudtrail(account) |
110 | | - _test.get_trail_status = lambda Name: {'IsLogging': False} |
| 106 | + _test.get_trail_status = lambda Name: {"IsLogging": False} |
111 | 107 | configure_cloudtrail(account) |
112 | | - _test.get_trail_status = lambda Name: {'IsLogging': True} |
| 108 | + _test.get_trail_status = lambda Name: {"IsLogging": True} |
113 | 109 | _test.describe_trails = lambda: { |
114 | | - 'trailList': [ |
| 110 | + "trailList": [ |
115 | 111 | { |
116 | | - 'IncludeGlobalServiceEvents': False, |
117 | | - 'Name': 'Default', |
118 | | - 'S3BucketName': 'oldbucketname', |
119 | | - 'S3KeyPrefix': 'dummy' |
120 | | - }]} |
| 112 | + "IncludeGlobalServiceEvents": False, |
| 113 | + "Name": "Default", |
| 114 | + "S3BucketName": "oldbucketname", |
| 115 | + "S3KeyPrefix": "dummy", |
| 116 | + } |
| 117 | + ] |
| 118 | + } |
121 | 119 | configure_cloudtrail(account) |
122 | 120 | _test.describe_trails = lambda: { |
123 | | - 'trailList': [ |
| 121 | + "trailList": [ |
124 | 122 | { |
125 | | - 'IncludeGlobalServiceEvents': False, |
126 | | - 'Name': 'wrongconfig', |
127 | | - 'S3BucketName': 'oldbucketname', |
128 | | - 'S3KeyPrefix': 'dummy' |
129 | | - }]} |
| 123 | + "IncludeGlobalServiceEvents": False, |
| 124 | + "Name": "wrongconfig", |
| 125 | + "S3BucketName": "oldbucketname", |
| 126 | + "S3KeyPrefix": "dummy", |
| 127 | + } |
| 128 | + ] |
| 129 | + } |
130 | 130 | configure_cloudtrail(account) |
131 | 131 |
|
132 | 132 |
|
133 | 133 | def test_configure_s3_buckets(): |
134 | 134 | config = { |
135 | | - 's3_buckets': { |
136 | | - 'bucket-1': { |
137 | | - 'name': 'bucket-1', |
138 | | - 'regions': ['eu-central-1'], |
139 | | - 'lifecycle_configuration': {'Rules': [{'x': 'y'}]}, |
140 | | - 'encryption_config': {'Rules': [{'a': 'b'}]}, |
141 | | - 'tags': {'foo': 'bar', 'bee': 'baz'} |
| 135 | + "s3_buckets": { |
| 136 | + "bucket-1": { |
| 137 | + "name": "bucket-1", |
| 138 | + "regions": ["eu-central-1"], |
| 139 | + "lifecycle_configuration": {"Rules": [{"x": "y"}]}, |
| 140 | + "encryption_config": {"Rules": [{"a": "b"}]}, |
| 141 | + "tags": {"foo": "bar", "bee": "baz"}, |
142 | 142 | } |
143 | 143 | } |
144 | 144 | } |
145 | 145 | account = MagicMock(config=config) |
146 | | - s3 = account.session.resource('s3', 'eu-central-1') |
147 | | - bucket = s3.Bucket('bucket-1') |
| 146 | + s3 = account.session.resource("s3", "eu-central-1") |
| 147 | + bucket = s3.Bucket("bucket-1") |
148 | 148 | bucket.creation_date = None |
149 | 149 |
|
150 | 150 | configure_s3_buckets(account) |
151 | 151 |
|
152 | 152 | bucket.create.assert_called_once() |
153 | | - s3.BucketLifecycle('bucket-1').put.assert_called_once_with( |
154 | | - LifecycleConfiguration={'Rules': [{'x': 'y'}]}) |
| 153 | + s3.BucketLifecycle("bucket-1").put.assert_called_once_with(LifecycleConfiguration={"Rules": [{"x": "y"}]}) |
155 | 154 | s3.meta.client.put_bucket_encryption.assert_called_once_with( |
156 | | - Bucket='bucket-1', |
157 | | - ServerSideEncryptionConfiguration={'Rules': [{'a': 'b'}]}) |
| 155 | + Bucket="bucket-1", ServerSideEncryptionConfiguration={"Rules": [{"a": "b"}]} |
| 156 | + ) |
158 | 157 | bucket.Tagging().put.assert_called_once_with( |
159 | | - Tagging={'TagSet': [ |
160 | | - {'Key': 'foo', 'Value': 'bar'}, |
161 | | - {'Key': 'bee', 'Value': 'baz'} |
162 | | - ]}) |
| 158 | + Tagging={"TagSet": [{"Key": "foo", "Value": "bar"}, {"Key": "bee", "Value": "baz"}]} |
| 159 | + ) |
163 | 160 |
|
164 | 161 |
|
165 | | -if __name__ == '__main__': |
| 162 | +if __name__ == "__main__": |
166 | 163 | pytest.main() |
0 commit comments