File: test_s3_replication.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (192 lines) | stat: -rw-r--r-- 6,269 bytes parent folder | download
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
from uuid import uuid4

import boto3
import pytest
from botocore.exceptions import ClientError

from moto import mock_aws

DEFAULT_REGION_NAME = "us-east-1"


@mock_aws
def test_get_bucket_replication_for_unexisting_bucket():
    bucket_name = str(uuid4())
    s3_client = boto3.client("s3", region_name=DEFAULT_REGION_NAME)
    with pytest.raises(ClientError) as exc:
        s3_client.get_bucket_replication(Bucket=bucket_name)
    err = exc.value.response["Error"]
    assert err["Code"] == "NoSuchBucket"
    assert err["Message"] == "The specified bucket does not exist"
    assert err["BucketName"] == bucket_name


@mock_aws
def test_get_bucket_replication_bucket_without_replication():
    bucket_name = str(uuid4())
    s3_client = boto3.client("s3", region_name=DEFAULT_REGION_NAME)
    s3_client.create_bucket(Bucket=bucket_name)

    with pytest.raises(ClientError) as exc:
        s3_client.get_bucket_replication(Bucket=bucket_name)
    err = exc.value.response["Error"]
    assert err["Code"] == "ReplicationConfigurationNotFoundError"
    assert err["Message"] == "The replication configuration was not found"
    assert err["BucketName"] == bucket_name


@mock_aws
def test_delete_bucket_replication_unknown_bucket():
    bucket_name = str(uuid4())
    s3_client = boto3.client("s3", region_name=DEFAULT_REGION_NAME)
    with pytest.raises(ClientError) as exc:
        s3_client.delete_bucket_replication(Bucket=bucket_name)
    err = exc.value.response["Error"]
    assert err["Code"] == "NoSuchBucket"
    assert err["Message"] == "The specified bucket does not exist"
    assert err["BucketName"] == bucket_name


@mock_aws
def test_delete_bucket_replication_bucket_without_replication():
    bucket_name = str(uuid4())
    s3_client = boto3.client("s3", region_name=DEFAULT_REGION_NAME)

    s3_client.create_bucket(Bucket=bucket_name)
    # No-op
    s3_client.delete_bucket_replication(Bucket=bucket_name)


@mock_aws
def test_create_replication_without_versioning():
    bucket_name = str(uuid4())
    s3_client = boto3.client("s3", region_name=DEFAULT_REGION_NAME)
    s3_client.create_bucket(Bucket=bucket_name)

    with pytest.raises(ClientError) as exc:
        s3_client.put_bucket_replication(
            Bucket=bucket_name,
            ReplicationConfiguration={
                "Role": "myrole",
                "Rules": [
                    {"Destination": {"Bucket": "secondbucket"}, "Status": "Enabled"}
                ],
            },
        )
    err = exc.value.response["Error"]
    assert err["Code"] == "InvalidRequest"
    assert err["Message"] == (
        "Versioning must be 'Enabled' on the bucket to apply a replication configuration"
    )
    assert err["BucketName"] == bucket_name


@mock_aws
def test_create_and_retrieve_replication_with_single_rules():
    bucket_name = str(uuid4())
    s3_client = boto3.client("s3", region_name=DEFAULT_REGION_NAME)

    s3_client.create_bucket(Bucket=bucket_name)
    s3_client.put_bucket_versioning(
        Bucket=bucket_name, VersioningConfiguration={"Status": "Enabled"}
    )
    s3_client.put_bucket_replication(
        Bucket=bucket_name,
        ReplicationConfiguration={
            "Role": "myrole",
            "Rules": [
                {
                    "ID": "firstrule",
                    "Priority": 2,
                    "Destination": {"Bucket": "secondbucket"},
                    "Status": "Enabled",
                }
            ],
        },
    )

    config = s3_client.get_bucket_replication(Bucket=bucket_name)[
        "ReplicationConfiguration"
    ]
    assert config == {
        "Role": "myrole",
        "Rules": [
            {
                "DeleteMarkerReplication": {"Status": "Disabled"},
                "Destination": {"Bucket": "secondbucket"},
                "Filter": {"Prefix": ""},
                "ID": "firstrule",
                "Priority": 2,
                "Status": "Enabled",
            }
        ],
    }

    s3_client.delete_bucket_replication(Bucket=bucket_name)

    # Can't retrieve replication that has been deleted
    with pytest.raises(ClientError) as exc:
        s3_client.get_bucket_replication(Bucket=bucket_name)
    err = exc.value.response["Error"]
    assert err["Code"] == "ReplicationConfigurationNotFoundError"
    assert err["Message"] == "The replication configuration was not found"
    assert err["BucketName"] == bucket_name


@mock_aws
def test_create_and_retrieve_replication_with_multiple_rules():
    bucket_name = str(uuid4())
    s3_client = boto3.client("s3", region_name=DEFAULT_REGION_NAME)

    s3_client.create_bucket(Bucket=bucket_name)
    s3_client.put_bucket_versioning(
        Bucket=bucket_name, VersioningConfiguration={"Status": "Enabled"}
    )
    s3_client.put_bucket_replication(
        Bucket=bucket_name,
        ReplicationConfiguration={
            "Role": "myrole",
            "Rules": [
                {"Destination": {"Bucket": "secondbucket"}, "Status": "Enabled"},
                {
                    "ID": "secondrule",
                    "Priority": 2,
                    "Destination": {"Bucket": "thirdbucket"},
                    "Status": "Disabled",
                },
                {
                    "Destination": {
                        "Bucket": "x-account-bucket",
                        "Account": "1234567890",
                    },
                    "Status": "Enabled",
                },
            ],
        },
    )

    config = s3_client.get_bucket_replication(Bucket=bucket_name)[
        "ReplicationConfiguration"
    ]
    assert config["Role"] == "myrole"
    rules = config["Rules"]
    assert len(rules) == 3

    first_rule = rules[0]
    assert "ID" in first_rule
    assert first_rule["Priority"] == 1
    assert first_rule["Status"] == "Enabled"
    assert first_rule["Destination"] == {"Bucket": "secondbucket"}

    second = rules[1]
    assert second["ID"] == "secondrule"
    assert second["Priority"] == 2
    assert second["Status"] == "Disabled"
    assert second["Destination"] == {"Bucket": "thirdbucket"}

    third = rules[2]
    assert third["Status"] == "Enabled"
    assert third["Destination"] == {
        "Bucket": "x-account-bucket",
        "Account": "1234567890",
    }