File: test_global_clusters.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 (263 lines) | stat: -rw-r--r-- 9,077 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import boto3
import pytest
from botocore.exceptions import ClientError

from moto import mock_aws
from moto.core import DEFAULT_ACCOUNT_ID


@mock_aws
def test_create_global_cluster__not_enough_parameters():
    client = boto3.client("rds", "us-east-1")

    with pytest.raises(ClientError) as exc:
        client.create_global_cluster(GlobalClusterIdentifier="gc1")
    err = exc.value.response["Error"]
    assert err["Code"] == "InvalidParameterValue"
    assert (
        err["Message"]
        == "When creating standalone global cluster, value for engineName should be specified"
    )


@mock_aws
def test_global_cluster_members():
    # WHEN create_global_cluster is called
    # AND create_db_cluster is called with GlobalClusterIdentifier set to the global cluster ARN
    # THEN describe_global_cluster shows the second cluster as part of the GlobalClusterMembers
    # AND describe_db_clusters shows the cluster as normal
    client = boto3.client("rds", "us-east-1")

    global_cluster = client.create_global_cluster(
        GlobalClusterIdentifier="gc1", Engine="aurora-mysql"
    )["GlobalCluster"]
    assert global_cluster["GlobalClusterIdentifier"] == "gc1"
    assert "GlobalClusterResourceId" in global_cluster
    assert (
        global_cluster["GlobalClusterArn"]
        == f"arn:aws:rds::{DEFAULT_ACCOUNT_ID}:global-cluster:gc1"
    )
    assert global_cluster["Status"] == "available"
    assert "Endpoint" in global_cluster
    assert global_cluster["Engine"] == "aurora-mysql"
    assert "mysql_aurora" in global_cluster["EngineVersion"]
    assert global_cluster["StorageEncrypted"] is False
    assert global_cluster["DeletionProtection"] is False
    assert global_cluster["GlobalClusterMembers"] == []

    resp = client.create_db_cluster(
        DBClusterIdentifier="dbci",
        GlobalClusterIdentifier="gc1",
        Engine="mysql",
        MasterUsername="masterusername",
        MasterUserPassword="hunter2_",
    )["DBCluster"]
    cluster_arn = resp["DBClusterArn"]

    resp = client.describe_global_clusters(GlobalClusterIdentifier="gc1")
    assert len(resp["GlobalClusters"]) == 1
    global_cluster = resp["GlobalClusters"][0]
    assert global_cluster["GlobalClusterIdentifier"] == "gc1"
    assert "Endpoint" in global_cluster
    assert len(global_cluster["GlobalClusterMembers"]) == 1
    assert global_cluster["GlobalClusterMembers"][0]["DBClusterArn"] == cluster_arn


@mock_aws
def test_create_global_cluster_from_regular_cluster():
    # WHEN create_db_cluster is called
    # AND create_global_cluster is called with SourceDBClusterIdentifier
    #     set as the earlier created db cluster
    # THEN that db cluster is elevated to a global cluster
    # AND it still shows up when calling describe_db_clusters
    client = boto3.client("rds", "us-east-1")

    resp = client.create_db_cluster(
        DBClusterIdentifier="dbci",
        Engine="mysql",
        MasterUsername="masterusername",
        MasterUserPassword="hunter2_",
    )["DBCluster"]
    cluster_arn = resp["DBClusterArn"]

    client.create_global_cluster(
        GlobalClusterIdentifier="gc1", SourceDBClusterIdentifier=cluster_arn
    )

    resp = client.describe_global_clusters(GlobalClusterIdentifier="gc1")
    assert len(resp["GlobalClusters"]) == 1
    global_cluster = resp["GlobalClusters"][0]
    assert global_cluster["GlobalClusterIdentifier"] == "gc1"

    assert len(global_cluster["GlobalClusterMembers"]) == 1
    assert global_cluster["GlobalClusterMembers"][0]["DBClusterArn"] == cluster_arn


@mock_aws
def test_create_global_cluster_from_regular_cluster_with_reader():
    east_client = boto3.client("rds", "eu-west-1")
    west_client = boto3.client("rds", "eu-west-2")

    # Create global cluster
    east_client.create_global_cluster(
        GlobalClusterIdentifier="test-global-db",
        Engine="aurora-mysql",
        DeletionProtection=False,
        DatabaseName="test-db",
        StorageEncrypted=False,
    )

    east_client.create_db_cluster(
        DBClusterIdentifier="test-primary-cluster",
        Engine="aurora-mysql",
        GlobalClusterIdentifier="test-global-db",
        MasterUsername="testUsername",
        MasterUserPassword="testPassword",
    )

    east_client.create_db_instance(
        DBInstanceIdentifier="test-primary-cluster-i1",
        DBInstanceClass="db.r5.large",
        Engine="aurora-mysql",
        PubliclyAccessible=False,
        DBClusterIdentifier="test-primary-cluster",
    )

    west_client.create_db_cluster(
        DBClusterIdentifier="test-secondary-cluster",
        Engine="aurora-mysql",
        GlobalClusterIdentifier="test-global-db",
    )

    resp = east_client.describe_global_clusters(
        GlobalClusterIdentifier="test-global-db"
    )
    members = resp["GlobalClusters"][0]["GlobalClusterMembers"]
    assert len(members) == 2
    assert (
        members[0]["DBClusterArn"]
        == "arn:aws:rds:eu-west-1:123456789012:cluster:test-primary-cluster"
    )
    assert len(members[0]["Readers"]) == 1
    assert (
        members[0]["Readers"][0]
        == "arn:aws:rds:eu-west-2:123456789012:cluster:test-secondary-cluster"
    )
    assert members[0]["IsWriter"]

    assert (
        members[1]["DBClusterArn"]
        == "arn:aws:rds:eu-west-2:123456789012:cluster:test-secondary-cluster"
    )
    assert len(members[1]["Readers"]) == 0
    assert not members[1]["IsWriter"]


@mock_aws
def test_create_global_cluster_from_regular_cluster__using_name():
    client = boto3.client("rds", "us-east-1")

    with pytest.raises(ClientError) as exc:
        client.create_global_cluster(
            GlobalClusterIdentifier="gc1", SourceDBClusterIdentifier="dbci"
        )
    err = exc.value.response["Error"]
    assert err["Code"] == "InvalidParameterValue"
    assert err["Message"] == "Malformed db cluster arn dbci"


@mock_aws
def test_create_global_cluster_from_regular_cluster__and_specify_engine():
    client = boto3.client("rds", "us-east-1")

    resp = client.create_db_cluster(
        DBClusterIdentifier="dbci",
        Engine="mysql",
        MasterUsername="masterusername",
        MasterUserPassword="hunter2_",
    )["DBCluster"]
    cluster_arn = resp["DBClusterArn"]

    with pytest.raises(ClientError) as exc:
        client.create_global_cluster(
            GlobalClusterIdentifier="gc1",
            Engine="aurora-mysql",
            SourceDBClusterIdentifier=cluster_arn,
        )
    err = exc.value.response["Error"]
    assert err["Code"] == "InvalidParameterCombination"
    assert err["Message"] == (
        "When creating global cluster from existing db cluster, value for "
        "engineName should not be specified since it will be inherited "
        "from source cluster"
    )


@mock_aws
def test_delete_non_global_cluster():
    # WHEN a global cluster contains a regular cluster
    # AND we attempt to delete the global cluster
    # THEN we get an error message
    # An error occurs (InvalidGlobalClusterStateFault) when calling the
    #    DeleteGlobalCluster operation: Global Cluster
    #    arn:aws:rds::486285699788:global-cluster:g1 is not empty
    client = boto3.client("rds", "us-east-1")

    client.create_global_cluster(GlobalClusterIdentifier="gc1", Engine="aurora-mysql")
    _ = client.create_db_cluster(
        DBClusterIdentifier="dbci",
        GlobalClusterIdentifier="gc1",
        Engine="mysql",
        MasterUsername="masterusername",
        MasterUserPassword="hunter2_",
    )["DBCluster"]

    with pytest.raises(ClientError) as exc:
        client.delete_global_cluster(GlobalClusterIdentifier="gc1")
    err = exc.value.response["Error"]
    assert err["Code"] == "InvalidGlobalClusterStateFault"
    assert (
        err["Message"]
        == f"Global Cluster arn:aws:rds::{DEFAULT_ACCOUNT_ID}:global-cluster:gc1 is not empty"
    )

    # Delete the child first
    client.delete_db_cluster(DBClusterIdentifier="dbci")

    # Then we can delete the global cluster
    client.delete_global_cluster(GlobalClusterIdentifier="gc1")

    assert client.describe_global_clusters()["GlobalClusters"] == []


@mock_aws
def test_remove_from_global_cluster():
    client = boto3.client("rds", "us-east-1")

    client.create_global_cluster(GlobalClusterIdentifier="gc1", Engine="aurora-mysql")

    # Assign to the global cluster
    client.create_db_cluster(
        DBClusterIdentifier="dbci",
        GlobalClusterIdentifier="gc1",
        Engine="mysql",
        MasterUsername="masterusername",
        MasterUserPassword="hunter2_",
    )

    # Remove it again
    client.remove_from_global_cluster(
        GlobalClusterIdentifier="gc1",
        DbClusterIdentifier="dbci",
    )

    # Verify it's been removed
    resp = client.describe_global_clusters(GlobalClusterIdentifier="gc1")

    assert len(resp["GlobalClusters"][0]["GlobalClusterMembers"]) == 0

    # Verifying a global cluster that doesn't exist, should fail silently
    client.remove_from_global_cluster(
        GlobalClusterIdentifier="gc1",
        DbClusterIdentifier="dbci",
    )