File: test_rds_export_tasks.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 (214 lines) | stat: -rw-r--r-- 6,984 bytes parent folder | download | duplicates (2)
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
import boto3
import pytest
from botocore.exceptions import ClientError

from moto import mock_aws
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID


def _prepare_db_snapshot(client, snapshot_name="snapshot-1"):
    client.create_db_instance(
        DBInstanceIdentifier="db-primary-1",
        AllocatedStorage=10,
        Engine="postgres",
        DBName="staging-postgres",
        DBInstanceClass="db.m1.small",
        MasterUsername="root",
        MasterUserPassword="hunter2",
        Port=1234,
        DBSecurityGroups=["my_sg"],
    )
    resp = client.create_db_snapshot(
        DBInstanceIdentifier="db-primary-1", DBSnapshotIdentifier=snapshot_name
    )
    return resp["DBSnapshot"]["DBSnapshotArn"]


def _prepare_db_cluster_snapshot(client, snapshot_name="cluster-snapshot-1"):
    db_cluster_identifier = "db-cluster-primary-1"
    client.create_db_cluster(
        AvailabilityZones=[
            "us-west-2",
        ],
        BackupRetentionPeriod=1,
        DBClusterIdentifier=db_cluster_identifier,
        DBClusterParameterGroupName="db-cluster-primary-1-group",
        DatabaseName="staging-postgres",
        Engine="postgres",
        EngineVersion="5.6.10a",
        MasterUserPassword="hunterxhunder",
        MasterUsername="root",
        Port=3306,
        StorageEncrypted=True,
    )
    resp = client.create_db_cluster_snapshot(
        DBClusterSnapshotIdentifier=snapshot_name,
        DBClusterIdentifier=db_cluster_identifier,
    )
    return resp["DBClusterSnapshot"]["DBClusterSnapshotArn"]


@mock_aws
def test_start_export_task_fails_unknown_snapshot():
    client = boto3.client("rds", region_name="us-west-2")

    with pytest.raises(ClientError) as ex:
        client.start_export_task(
            ExportTaskIdentifier="export-snapshot-1",
            SourceArn=f"arn:aws:rds:us-west-2:{ACCOUNT_ID}:snapshot:snapshot-1",
            S3BucketName="export-bucket",
            IamRoleArn="",
            KmsKeyId="",
        )

    err = ex.value.response["Error"]
    assert err["Code"] == "DBSnapshotNotFound"
    assert err["Message"] == "DBSnapshot snapshot-1 not found."


@mock_aws
def test_start_export_task_db():
    client = boto3.client("rds", region_name="us-west-2")
    source_arn = _prepare_db_snapshot(client)

    export = client.start_export_task(
        ExportTaskIdentifier="export-snapshot-1",
        SourceArn=source_arn,
        S3BucketName="export-bucket",
        S3Prefix="snaps/",
        IamRoleArn="arn:aws:iam:::role/export-role",
        KmsKeyId="arn:aws:kms:::key/0ea3fef3-80a7-4778-9d8c-1c0c6EXAMPLE",
        ExportOnly=["schema.table"],
    )

    assert export["ExportTaskIdentifier"] == "export-snapshot-1"
    assert export["SourceArn"] == source_arn
    assert export["S3Bucket"] == "export-bucket"
    assert export["S3Prefix"] == "snaps/"
    assert export["IamRoleArn"] == "arn:aws:iam:::role/export-role"
    assert export["KmsKeyId"] == (
        "arn:aws:kms:::key/0ea3fef3-80a7-4778-9d8c-1c0c6EXAMPLE"
    )
    assert export["ExportOnly"] == ["schema.table"]
    assert export["SourceType"] == "SNAPSHOT"


@mock_aws
def test_start_export_task_db_cluster():
    client = boto3.client("rds", region_name="us-west-2")
    source_arn = _prepare_db_cluster_snapshot(client)

    export = client.start_export_task(
        ExportTaskIdentifier="export-snapshot-1",
        SourceArn=source_arn,
        S3BucketName="export-bucket",
        S3Prefix="snaps/",
        IamRoleArn="arn:aws:iam:::role/export-role",
        KmsKeyId="arn:aws:kms:::key/0ea3fef3-80a7-4778-9d8c-1c0c6EXAMPLE",
        ExportOnly=["schema.table"],
    )

    assert export["ExportTaskIdentifier"] == "export-snapshot-1"
    assert export["SourceArn"] == source_arn
    assert export["S3Bucket"] == "export-bucket"
    assert export["S3Prefix"] == "snaps/"
    assert export["IamRoleArn"] == "arn:aws:iam:::role/export-role"
    assert export["KmsKeyId"] == (
        "arn:aws:kms:::key/0ea3fef3-80a7-4778-9d8c-1c0c6EXAMPLE"
    )
    assert export["ExportOnly"] == ["schema.table"]
    assert export["SourceType"] == "CLUSTER"


@mock_aws
def test_start_export_task_fail_already_exists():
    client = boto3.client("rds", region_name="us-west-2")
    source_arn = _prepare_db_snapshot(client)

    client.start_export_task(
        ExportTaskIdentifier="export-snapshot-1",
        SourceArn=source_arn,
        S3BucketName="export-bucket",
        IamRoleArn="",
        KmsKeyId="",
    )
    with pytest.raises(ClientError) as ex:
        client.start_export_task(
            ExportTaskIdentifier="export-snapshot-1",
            SourceArn=source_arn,
            S3BucketName="export-bucket",
            IamRoleArn="",
            KmsKeyId="",
        )

    err = ex.value.response["Error"]
    assert err["Code"] == "ExportTaskAlreadyExists"
    assert err["Message"] == (
        "Cannot start export task because a task with the identifier "
        "export-snapshot-1 already exists."
    )


@mock_aws
def test_cancel_export_task_fails_unknown_task():
    client = boto3.client("rds", region_name="us-west-2")
    with pytest.raises(ClientError) as ex:
        client.cancel_export_task(ExportTaskIdentifier="export-snapshot-1")

    err = ex.value.response["Error"]
    assert err["Code"] == "ExportTaskNotFound"
    assert err["Message"] == (
        "Cannot cancel export task because a task with the identifier "
        "export-snapshot-1 is not exist."
    )


@mock_aws
def test_cancel_export_task():
    client = boto3.client("rds", region_name="us-west-2")
    source_arn = _prepare_db_snapshot(client)

    client.start_export_task(
        ExportTaskIdentifier="export-snapshot-1",
        SourceArn=source_arn,
        S3BucketName="export-bucket",
        IamRoleArn="",
        KmsKeyId="",
    )

    export = client.cancel_export_task(ExportTaskIdentifier="export-snapshot-1")

    assert export["ExportTaskIdentifier"] == "export-snapshot-1"
    assert export["Status"] == "canceled"


@mock_aws
def test_describe_export_tasks():
    client = boto3.client("rds", region_name="us-west-2")
    source_arn = _prepare_db_snapshot(client)
    client.start_export_task(
        ExportTaskIdentifier="export-snapshot-1",
        SourceArn=source_arn,
        S3BucketName="export-bucket",
        IamRoleArn="",
        KmsKeyId="",
    )

    exports = client.describe_export_tasks()["ExportTasks"]

    assert len(exports) == 1
    assert exports[0]["ExportTaskIdentifier"] == "export-snapshot-1"


@mock_aws
def test_describe_export_tasks_fails_unknown_task():
    client = boto3.client("rds", region_name="us-west-2")
    with pytest.raises(ClientError) as ex:
        client.describe_export_tasks(ExportTaskIdentifier="export-snapshot-1")

    err = ex.value.response["Error"]
    assert err["Code"] == "ExportTaskNotFound"
    assert err["Message"] == (
        "Cannot cancel export task because a task with the identifier "
        "export-snapshot-1 is not exist."
    )