File: test_dax.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 (456 lines) | stat: -rw-r--r-- 16,136 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
"""Unit tests for dax-supported APIs."""

import boto3
import pytest
from botocore.exceptions import ClientError

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

# See our Development Tips on writing tests for hints on how to write good tests:
# http://docs.getmoto.org/en/latest/docs/contributing/development_tips/tests.html


@mock_aws
def test_create_cluster_minimal():
    client = boto3.client("dax", region_name="us-east-2")
    iam_role_arn = f"arn:aws:iam::{ACCOUNT_ID}:role/aws-service-role/dax.amazonaws.com/AWSServiceRoleForDAX"
    created_cluster = client.create_cluster(
        ClusterName="daxcluster",
        NodeType="dax.t3.small",
        ReplicationFactor=3,
        IamRoleArn=iam_role_arn,
    )["Cluster"]
    described_cluster = client.describe_clusters(ClusterNames=["daxcluster"])[
        "Clusters"
    ][0]

    for cluster in [created_cluster, described_cluster]:
        assert cluster["ClusterName"] == "daxcluster"
        assert (
            cluster["ClusterArn"]
            == f"arn:aws:dax:us-east-2:{ACCOUNT_ID}:cache/daxcluster"
        )
        assert cluster["TotalNodes"] == 3
        assert cluster["ActiveNodes"] == 0
        assert cluster["NodeType"] == "dax.t3.small"
        assert cluster["Status"] == "creating"
        assert cluster["ClusterDiscoveryEndpoint"] == {"Port": 8111}
        assert cluster["PreferredMaintenanceWindow"] == "thu:23:30-fri:00:30"
        assert cluster["SubnetGroup"] == "default"
        assert len(cluster["SecurityGroups"]) == 1
        assert cluster["IamRoleArn"] == iam_role_arn
        assert cluster["ParameterGroup"]["ParameterGroupName"] == "default.dax1.0"
        assert cluster["SSEDescription"] == {"Status": "DISABLED"}
        assert cluster["ClusterEndpointEncryptionType"] == "NONE"


@mock_aws
def test_create_cluster_description():
    client = boto3.client("dax", region_name="us-east-2")
    iam_role_arn = f"arn:aws:iam::{ACCOUNT_ID}:role/aws-service-role/dax.amazonaws.com/AWSServiceRoleForDAX"
    created_cluster = client.create_cluster(
        ClusterName="daxcluster",
        Description="my cluster",
        NodeType="dax.t3.small",
        ReplicationFactor=3,
        IamRoleArn=iam_role_arn,
    )["Cluster"]
    described_cluster = client.describe_clusters(ClusterNames=["daxcluster"])[
        "Clusters"
    ][0]

    for cluster in [created_cluster, described_cluster]:
        assert cluster["ClusterName"] == "daxcluster"
        assert cluster["Description"] == "my cluster"


@mock_aws
def test_create_cluster_with_sse_enabled():
    client = boto3.client("dax", region_name="us-east-2")
    iam_role_arn = f"arn:aws:iam::{ACCOUNT_ID}:role/aws-service-role/dax.amazonaws.com/AWSServiceRoleForDAX"
    created_cluster = client.create_cluster(
        ClusterName="daxcluster",
        NodeType="dax.t3.small",
        ReplicationFactor=3,
        IamRoleArn=iam_role_arn,
        SSESpecification={"Enabled": True},
        ClusterEndpointEncryptionType="TLS",
    )["Cluster"]
    described_cluster = client.describe_clusters(ClusterNames=["daxcluster"])[
        "Clusters"
    ][0]

    for cluster in [created_cluster, described_cluster]:
        assert cluster["ClusterName"] == "daxcluster"
        assert cluster["SSEDescription"] == {"Status": "ENABLED"}
        assert cluster["ClusterEndpointEncryptionType"] == "TLS"


@mock_aws
@pytest.mark.parametrize(
    "iam_role,expected",
    (
        ("n/a", "ARNs must start with 'arn:': n/a"),
        ("arn:sth", "Second colon partition not found: arn:sth"),
        ("arn:sth:aws", "Third colon vendor not found: arn:sth:aws"),
        (
            "arn:sth:aws:else",
            "Fourth colon (region/namespace delimiter) not found: arn:sth:aws:else",
        ),
        (
            "arn:sth:aws:else:eu-west-1",
            "Fifth colon (namespace/relative-id delimiter) not found: arn:sth:aws:else:eu-west-1",
        ),
    ),
)
def test_create_cluster_invalid_arn(iam_role: str, expected: str):
    client = boto3.client("dax", region_name="eu-west-1")
    with pytest.raises(ClientError) as exc:
        client.create_cluster(
            ClusterName="1invalid",
            NodeType="dax.t3.small",
            ReplicationFactor=3,
            IamRoleArn=iam_role,
        )
    err = exc.value.response["Error"]

    assert err["Code"] == "InvalidParameterValueException"
    assert err["Message"] == expected


@mock_aws
@pytest.mark.parametrize(
    "name", ["1invalid", "iИvalid", "in_valid", "invalid-", "in--valid"]
)
def test_create_cluster_invalid_name(name):
    client = boto3.client("dax", region_name="eu-west-1")
    with pytest.raises(ClientError) as exc:
        client.create_cluster(
            ClusterName=name,
            NodeType="dax.t3.small",
            ReplicationFactor=3,
            IamRoleArn="arn:aws:iam::486285699788:role/apigatewayrole",
        )
    err = exc.value.response["Error"]

    assert err["Code"] == "InvalidParameterValueException"
    assert err["Message"] == (
        "Cluster ID specified is not a valid identifier. Identifiers must begin with a letter; must contain only ASCII letters, digits, and hyphens; and must not end with a hyphen or contain two consecutive hyphens."
    )


@mock_aws
@pytest.mark.parametrize(
    "name", ["1invalid", "iИvalid", "in_valid", "invalid-", "in--valid"]
)
def test_describe_clusters_invalid_name(name):
    client = boto3.client("dax", region_name="eu-west-1")
    with pytest.raises(ClientError) as exc:
        client.describe_clusters(ClusterNames=[name])
    err = exc.value.response["Error"]
    assert err["Code"] == "InvalidParameterValueException"
    assert (
        err["Message"]
        == "Cluster ID specified is not a valid identifier. Identifiers must begin with a letter; must contain only ASCII letters, digits, and hyphens; and must not end with a hyphen or contain two consecutive hyphens."
    )


@mock_aws
def test_delete_cluster_unknown():
    client = boto3.client("dax", region_name="eu-west-1")
    with pytest.raises(ClientError) as exc:
        client.delete_cluster(ClusterName="unknown")
    err = exc.value.response["Error"]

    assert err["Code"] == "ClusterNotFoundFault"
    assert err["Message"] == "Cluster not found."


@mock_aws
def test_delete_cluster():
    client = boto3.client("dax", region_name="eu-west-1")
    iam_role_arn = f"arn:aws:iam::{ACCOUNT_ID}:role/aws-service-role/dax.amazonaws.com/AWSServiceRoleForDAX"
    client.create_cluster(
        ClusterName="daxcluster",
        NodeType="dax.t3.small",
        ReplicationFactor=2,
        IamRoleArn=iam_role_arn,
    )
    client.delete_cluster(ClusterName="daxcluster")

    for _ in range(0, 3):
        # Cluster takes a while to delete...
        cluster = client.describe_clusters(ClusterNames=["daxcluster"])["Clusters"][0]

        assert cluster["Status"] == "deleting"
        assert cluster["TotalNodes"] == 2
        assert cluster["ActiveNodes"] == 0
        assert "Nodes" not in cluster

    with pytest.raises(ClientError) as exc:
        client.describe_clusters(ClusterNames=["daxcluster"])
    err = exc.value.response["Error"]

    assert err["Code"] == "ClusterNotFoundFault"


@mock_aws
def test_describe_cluster_unknown():
    client = boto3.client("dax", region_name="eu-west-1")
    with pytest.raises(ClientError) as exc:
        client.describe_clusters(ClusterNames=["unknown"])
    err = exc.value.response["Error"]

    assert err["Code"] == "ClusterNotFoundFault"
    assert err["Message"] == "Cluster unknown not found."


@mock_aws
def test_describe_clusters_returns_all():
    client = boto3.client("dax", region_name="us-east-1")
    iam_role_arn = f"arn:aws:iam::{ACCOUNT_ID}:role/aws-service-role/dax.amazonaws.com/AWSServiceRoleForDAX"
    for i in range(0, 50):
        client.create_cluster(
            ClusterName=f"daxcluster{i}",
            NodeType="dax.t3.small",
            ReplicationFactor=1,
            IamRoleArn=iam_role_arn,
        )

    assert len(client.describe_clusters()["Clusters"]) == 50


@mock_aws
def test_describe_clusters_paginates():
    client = boto3.client("dax", region_name="us-east-1")
    iam_role_arn = f"arn:aws:iam::{ACCOUNT_ID}:role/aws-service-role/dax.amazonaws.com/AWSServiceRoleForDAX"
    for i in range(0, 50):
        client.create_cluster(
            ClusterName=f"daxcluster{i}",
            NodeType="dax.t3.small",
            ReplicationFactor=1,
            IamRoleArn=iam_role_arn,
        )

    resp = client.describe_clusters(MaxResults=10)
    assert len(resp["Clusters"]) == 10
    assert "NextToken" in resp

    resp = client.describe_clusters(MaxResults=10, NextToken=resp["NextToken"])
    assert len(resp["Clusters"]) == 10
    assert "NextToken" in resp

    resp = client.describe_clusters(NextToken=resp["NextToken"])
    assert len(resp["Clusters"]) == 30
    assert "NextToken" not in resp


@mock_aws
def test_describe_clusters_returns_nodes_after_some_time():
    client = boto3.client("dax", region_name="us-east-2")
    iam_role_arn = f"arn:aws:iam::{ACCOUNT_ID}:role/aws-service-role/dax.amazonaws.com/AWSServiceRoleForDAX"
    client.create_cluster(
        ClusterName="daxcluster",
        NodeType="dax.t3.small",
        ReplicationFactor=3,
        IamRoleArn=iam_role_arn,
    )

    for _ in range(0, 3):
        # Cluster takes a while to load...
        cluster = client.describe_clusters(ClusterNames=["daxcluster"])["Clusters"][0]
        assert cluster["Status"] == "creating"
        assert "Nodes" not in cluster

    # Finished loading by now
    cluster = client.describe_clusters(ClusterNames=["daxcluster"])["Clusters"][0]

    assert cluster["TotalNodes"] == 3
    assert cluster["ActiveNodes"] == 0
    assert cluster["Status"] == "available"

    # Address Info is only available when the cluster is ready
    endpoint = cluster["ClusterDiscoveryEndpoint"]
    address = endpoint["Address"]
    cluster_id = address.split(".")[1]

    assert address == f"daxcluster.{cluster_id}.dax-clusters.us-east-2.amazonaws.com"
    assert endpoint["Port"] == 8111
    assert endpoint["URL"] == f"dax://{address}"

    # Nodes are only shown when the cluster is ready
    assert len(cluster["Nodes"]) == 3

    for idx, a in enumerate(["a", "b", "c"]):
        node = cluster["Nodes"][idx]
        expected_node_address = (
            f"daxcluster-{a}.{cluster_id}.nodes.dax-clusters.us-east-2.amazonaws.com"
        )

        assert node["NodeId"] == f"daxcluster-{a}"
        assert node["Endpoint"]["Address"] == expected_node_address
        assert node["Endpoint"]["Port"] == 8111
        assert "AvailabilityZone" in node
        assert node["NodeStatus"] == "available"
        assert node["ParameterGroupStatus"] == "in-sync"


@mock_aws
def test_list_tags_unknown():
    client = boto3.client("dax", region_name="ap-southeast-1")
    with pytest.raises(ClientError) as exc:
        client.list_tags(ResourceName="unknown")
    err = exc.value.response["Error"]

    assert err["Code"] == "ClusterNotFoundFault"


@mock_aws
def test_list_tags():
    client = boto3.client("dax", region_name="ap-southeast-1")
    cluster = client.create_cluster(
        ClusterName="daxcluster",
        NodeType="dax.t3.small",
        ReplicationFactor=3,
        IamRoleArn=f"arn:aws:iam::{ACCOUNT_ID}:role/aws-service-role/dax.amazonaws.com/AWSServiceRoleForDAX",
        Tags=[
            {"Key": "tag1", "Value": "value1"},
            {"Key": "tag2", "Value": "value2"},
            {"Key": "tag3", "Value": "value3"},
        ],
    )["Cluster"]

    for name in ["daxcluster", cluster["ClusterArn"]]:
        resp = client.list_tags(ResourceName=name)

        assert "NextToken" not in resp
        assert resp["Tags"] == (
            [
                {"Key": "tag1", "Value": "value1"},
                {"Key": "tag2", "Value": "value2"},
                {"Key": "tag3", "Value": "value3"},
            ]
        )


@mock_aws
def test_increase_replication_factor_unknown():
    client = boto3.client("dax", region_name="ap-southeast-1")
    with pytest.raises(ClientError) as exc:
        client.increase_replication_factor(
            ClusterName="unknown", NewReplicationFactor=2
        )
    err = exc.value.response["Error"]

    assert err["Code"] == "ClusterNotFoundFault"


@mock_aws
def test_increase_replication_factor():
    client = boto3.client("dax", region_name="ap-southeast-1")
    name = "daxcluster"
    cluster = client.create_cluster(
        ClusterName=name,
        NodeType="dax.t3.small",
        ReplicationFactor=2,
        IamRoleArn=f"arn:aws:iam::{ACCOUNT_ID}:role/aws-service-role/dax.amazonaws.com/AWSServiceRoleForDAX",
        Tags=[
            {"Key": "tag1", "Value": "value1"},
            {"Key": "tag2", "Value": "value2"},
            {"Key": "tag3", "Value": "value3"},
        ],
    )["Cluster"]

    assert cluster["TotalNodes"] == 2

    adjusted_cluster = client.increase_replication_factor(
        ClusterName=name, NewReplicationFactor=5
    )["Cluster"]
    described_cluster = client.describe_clusters(ClusterNames=[name])["Clusters"][0]

    assert adjusted_cluster["TotalNodes"] == 5
    assert described_cluster["TotalNodes"] == 5

    # Progress cluster until it's available
    for _ in range(0, 3):
        client.describe_clusters(ClusterNames=[name])

    described_cluster = client.describe_clusters(ClusterNames=[name])["Clusters"][0]
    node_ids = {node["NodeId"] for node in described_cluster["Nodes"]}

    assert node_ids == (
        {f"{name}-a", f"{name}-b", f"{name}-c", f"{name}-d", f"{name}-e"}
    )


@mock_aws
def test_decrease_replication_factor_unknown():
    client = boto3.client("dax", region_name="ap-southeast-1")
    with pytest.raises(ClientError) as exc:
        client.decrease_replication_factor(
            ClusterName="unknown", NewReplicationFactor=2
        )
    err = exc.value.response["Error"]

    assert err["Code"] == "ClusterNotFoundFault"


@mock_aws
def test_decrease_replication_factor():
    client = boto3.client("dax", region_name="eu-west-1")
    name = "daxcluster"
    client.create_cluster(
        ClusterName=name,
        NodeType="dax.t3.small",
        ReplicationFactor=5,
        IamRoleArn=f"arn:aws:iam::{ACCOUNT_ID}:role/aws-service-role/dax.amazonaws.com/AWSServiceRoleForDAX",
    )

    adjusted_cluster = client.decrease_replication_factor(
        ClusterName=name, NewReplicationFactor=3
    )["Cluster"]
    described_cluster = client.describe_clusters(ClusterNames=[name])["Clusters"][0]

    assert adjusted_cluster["TotalNodes"] == 3
    assert described_cluster["TotalNodes"] == 3

    # Progress cluster until it's available
    for _ in range(0, 3):
        client.describe_clusters(ClusterNames=[name])

    described_cluster = client.describe_clusters(ClusterNames=[name])["Clusters"][0]
    node_ids = {node["NodeId"] for node in described_cluster["Nodes"]}

    assert node_ids == ({f"{name}-a", f"{name}-b", f"{name}-c"})


@mock_aws
def test_decrease_replication_factor_specific_nodeids():
    client = boto3.client("dax", region_name="ap-southeast-1")
    name = "daxcluster"
    client.create_cluster(
        ClusterName=name,
        NodeType="dax.t3.small",
        ReplicationFactor=5,
        IamRoleArn=f"arn:aws:iam::{ACCOUNT_ID}:role/aws-service-role/dax.amazonaws.com/AWSServiceRoleForDAX",
    )

    adjusted_cluster = client.decrease_replication_factor(
        ClusterName=name,
        NewReplicationFactor=3,
        NodeIdsToRemove=["daxcluster-b", "daxcluster-c"],
    )["Cluster"]
    described_cluster = client.describe_clusters(ClusterNames=[name])["Clusters"][0]

    assert adjusted_cluster["TotalNodes"] == 3
    assert described_cluster["TotalNodes"] == 3

    # Progress cluster until it's available
    for _ in range(0, 3):
        client.describe_clusters(ClusterNames=[name])

    described_cluster = client.describe_clusters(ClusterNames=[name])["Clusters"][0]
    node_ids = {node["NodeId"] for node in described_cluster["Nodes"]}

    assert node_ids == ({f"{name}-a", f"{name}-d", f"{name}-e"})