File: test_es.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 (303 lines) | stat: -rw-r--r-- 10,892 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
"""Unit tests for es-supported APIs."""

import boto3
import pytest
from botocore.exceptions import ClientError

from moto import mock_aws
from tests import DEFAULT_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


@pytest.mark.parametrize(
    "name", ["getmoto.org", "search-is-$$$", "dev_or_test", "dev/test", "1love", "DEV"]
)
@mock_aws
def test_create_domain_invalid_name(name):
    client = boto3.client("es", region_name="us-east-2")
    with pytest.raises(ClientError) as exc:
        client.create_elasticsearch_domain(DomainName=name)
    err = exc.value.response["Error"]
    assert (
        err["Message"]
        == f"1 validation error detected: Value '{name}' at 'domainName' failed to satisfy constraint: Member must satisfy regular expression pattern: [a-z][a-z0-9\\-]+"
    )
    assert err["Code"] == "ValidationException"


@mock_aws
def test_create_elasticsearch_domain_minimal():
    client = boto3.client("es", region_name="us-east-2")
    resp = client.create_elasticsearch_domain(DomainName="motosearch")

    domain = resp["DomainStatus"]
    assert domain["DomainName"] == "motosearch"
    assert (
        domain["ARN"]
        == f"arn:aws:es:us-east-2:{DEFAULT_ACCOUNT_ID}:domain/{domain['DomainName']}"
    )
    assert domain["Created"] is True
    assert domain["Deleted"] is False
    assert domain["Processing"] is False
    assert domain["UpgradeProcessing"] is False
    assert "ElasticsearchVersion" not in domain


@mock_aws
def test_create_elasticsearch_domain():
    client = boto3.client("es", region_name="us-east-2")
    resp = client.create_elasticsearch_domain(
        DomainName="motosearch",
        ElasticsearchVersion="7.10",
        ElasticsearchClusterConfig={
            "InstanceType": "m3.large.elasticsearch",
            "InstanceCount": 1,
            "DedicatedMasterEnabled": True,
            "DedicatedMasterType": "m3.large.elasticsearch",
            "DedicatedMasterCount": 1,
            "ZoneAwarenessEnabled": False,
            "WarmEnabled": False,
            "ColdStorageOptions": {"Enabled": False},
        },
        EBSOptions={
            "EBSEnabled": True,
            "VolumeType": "io2",
            "VolumeSize": 10,
            "Iops": 1,
        },
        AccessPolicies="some unvalidated accesspolicy",
        SnapshotOptions={"AutomatedSnapshotStartHour": 1},
        VPCOptions={"SubnetIds": ["s1"], "SecurityGroupIds": ["sg1"]},
        CognitoOptions={"Enabled": False},
        EncryptionAtRestOptions={"Enabled": False},
        NodeToNodeEncryptionOptions={"Enabled": False},
        AdvancedOptions={"option": "value"},
        LogPublishingOptions={"log1": {"Enabled": False}},
        DomainEndpointOptions={"EnforceHTTPS": True, "CustomEndpointEnabled": False},
        AdvancedSecurityOptions={"Enabled": False},
        AutoTuneOptions={"DesiredState": "ENABLED"},
    )

    domain = resp["DomainStatus"]
    assert "DomainId" in domain
    assert domain["Created"] is True
    assert domain["ElasticsearchVersion"] == "7.10"

    cluster_config = domain["ElasticsearchClusterConfig"]
    assert cluster_config["ColdStorageOptions"] == {"Enabled": False}
    assert cluster_config["DedicatedMasterCount"] == 1
    assert cluster_config["DedicatedMasterType"] == "m3.large.elasticsearch"
    assert cluster_config["WarmEnabled"] is False

    ebs = domain["EBSOptions"]
    assert ebs["EBSEnabled"] is True
    assert ebs["Iops"] == 1
    assert ebs["VolumeSize"] == 10
    assert ebs["VolumeType"] == "io2"

    assert domain["AccessPolicies"] == "some unvalidated accesspolicy"

    snapshots = domain["SnapshotOptions"]
    assert snapshots["AutomatedSnapshotStartHour"] == 1

    vpcs = domain["VPCOptions"]
    assert vpcs["SubnetIds"] == ["s1"]
    assert vpcs["SecurityGroupIds"] == ["sg1"]

    cognito = domain["CognitoOptions"]
    assert cognito["Enabled"] is False

    encryption_at_rest = domain["EncryptionAtRestOptions"]
    assert encryption_at_rest["Enabled"] is False

    encryption = domain["NodeToNodeEncryptionOptions"]
    assert encryption["Enabled"] is False

    advanced = domain["AdvancedOptions"]
    assert advanced["option"] == "value"

    advanced = domain["LogPublishingOptions"]
    assert advanced["log1"] == {"Enabled": False}

    endpoint = domain["DomainEndpointOptions"]
    assert endpoint["EnforceHTTPS"] is True
    assert endpoint["CustomEndpointEnabled"] is False

    advanced_security = domain["AdvancedSecurityOptions"]
    assert advanced_security["Enabled"] is False

    auto_tune = domain["AutoTuneOptions"]
    assert auto_tune["State"] == "ENABLED"


@mock_aws
def test_delete_elasticsearch_domain():
    client = boto3.client("es", region_name="ap-southeast-1")
    client.create_elasticsearch_domain(DomainName="motosearch")
    client.delete_elasticsearch_domain(DomainName="motosearch")

    assert client.list_domain_names()["DomainNames"] == []


@mock_aws
def test_missing_delete_elasticsearch_domain():
    client = boto3.client("es", region_name="ap-southeast-1")
    with pytest.raises(ClientError) as exc:
        client.delete_elasticsearch_domain(DomainName="unknown")

    meta = exc.value.response["ResponseMetadata"]
    assert meta["HTTPStatusCode"] == 409

    err = exc.value.response["Error"]
    assert err["Code"] == "ResourceNotFoundException"
    assert err["Message"] == "Domain not found: unknown"


@mock_aws
def test_describe_invalid_domain():
    client = boto3.client("es", region_name="us-east-2")
    with pytest.raises(ClientError) as exc:
        client.describe_elasticsearch_domain(DomainName="moto.org")
    meta = exc.value.response["ResponseMetadata"]
    assert meta["HTTPStatusCode"] == 400
    err = exc.value.response["Error"]
    assert (
        err["Message"]
        == "1 validation error detected: Value 'moto.org' at 'domainName' failed to satisfy constraint: Member must satisfy regular expression pattern: [a-z][a-z0-9\\-]+"
    )
    assert err["Code"] == "ValidationException"


@mock_aws
def test_describe_unknown_domain():
    client = boto3.client("es", region_name="ap-southeast-1")
    with pytest.raises(ClientError) as exc:
        client.describe_elasticsearch_domain(DomainName="unknown")

    meta = exc.value.response["ResponseMetadata"]
    assert meta["HTTPStatusCode"] == 409

    err = exc.value.response["Error"]
    assert err["Code"] == "ResourceNotFoundException"
    assert err["Message"] == "Domain not found: unknown"


@mock_aws
def test_describe_elasticsearch_domain():
    client = boto3.client("es", region_name="ap-southeast-1")
    client.create_elasticsearch_domain(DomainName="motosearch")
    resp = client.describe_elasticsearch_domain(DomainName="motosearch")

    domain = resp["DomainStatus"]
    assert domain["DomainName"] == "motosearch"
    assert (
        domain["ARN"]
        == f"arn:aws:es:ap-southeast-1:{DEFAULT_ACCOUNT_ID}:domain/{domain['DomainName']}"
    )
    assert domain["Created"] is True
    assert domain["Deleted"] is False
    assert domain["Processing"] is False
    assert domain["UpgradeProcessing"] is False
    assert "ElasticsearchVersion" not in domain


@mock_aws
def test_describe_elasticsearch_domain_config():
    client = boto3.client("es", region_name="ap-southeast-1")
    client.create_elasticsearch_domain(DomainName="motosearch")

    resp = client.describe_elasticsearch_domain_config(DomainName="motosearch")
    domain_config = resp["DomainConfig"]
    assert domain_config["ElasticsearchClusterConfig"]["Options"]["InstanceCount"] == 1


@mock_aws
def test_list_domain_names_initial():
    client = boto3.client("es", region_name="eu-west-1")
    resp = client.list_domain_names()

    assert resp["DomainNames"] == []


@mock_aws
def test_list_domain_names_enginetype():
    client = boto3.client("es", region_name="us-east-1")
    client.create_elasticsearch_domain(DomainName="elasticsearch-domain")

    resp = client.list_domain_names(EngineType="Elasticsearch")
    assert len(resp["DomainNames"]) == 1

    resp = client.list_domain_names(EngineType="OpenSearch")
    assert len(resp["DomainNames"]) == 0


@mock_aws
def test_list_domain_names_with_multiple_domains():
    client = boto3.client("es", region_name="eu-west-1")
    domain_names = [f"env{i}" for i in range(1, 5)]
    for name in domain_names:
        client.create_elasticsearch_domain(DomainName=name)
    resp = client.list_domain_names()

    assert len(resp["DomainNames"]) == 4
    for name in domain_names:
        assert {"DomainName": name, "EngineType": "Elasticsearch"} in resp[
            "DomainNames"
        ]


@mock_aws
def test_describe_elasticsearch_domains():
    client = boto3.client("es", region_name="us-east-1")
    domain_names = [f"env{i}" for i in range(1, 5)]
    for name in domain_names:
        client.create_elasticsearch_domain(
            DomainName=name,
            ElasticsearchVersion="7.10",
            AdvancedOptions={"option": "value"},
            AdvancedSecurityOptions={"Enabled": False},
        )
    resp = client.describe_elasticsearch_domains(DomainNames=domain_names)

    assert len(resp["DomainStatusList"]) == 4
    for domain in resp["DomainStatusList"]:
        assert domain["DomainName"] in domain_names
        assert domain["ElasticsearchVersion"] == "7.10"
        assert "AdvancedSecurityOptions" in domain.keys()
        assert "AdvancedOptions" in domain.keys()

    # Test for invalid domain name
    resp = client.describe_elasticsearch_domains(DomainNames=["invalid"])
    assert len(resp["DomainStatusList"]) == 0


@mock_aws
def test_list_domain_names_opensearch():
    opensearch_client = boto3.client("opensearch", region_name="us-east-2")
    status = opensearch_client.create_domain(DomainName="moto-opensearch")[
        "DomainStatus"
    ]
    assert status["Created"]
    assert "DomainId" in status
    assert "DomainName" in status
    assert status["DomainName"] == "moto-opensearch"

    # ensure that elasticsearch client can describe opensearch domains as well
    es_client = boto3.client("es", region_name="us-east-2")
    domain_names = es_client.list_domain_names()["DomainNames"]
    assert len(domain_names) == 1
    assert domain_names[0]["DomainName"] == "moto-opensearch"
    assert domain_names[0]["EngineType"] == "OpenSearch"


@mock_aws
def test_list_domain_names_opensearch_deleted():
    opensearch_client = boto3.client("opensearch", region_name="us-east-2")
    opensearch_client.create_domain(DomainName="moto-opensearch")
    opensearch_client.delete_domain(DomainName="moto-opensearch")

    # ensure that elasticsearch client can describe opensearch domains as well
    es_client = boto3.client("es", region_name="us-east-2")
    assert es_client.list_domain_names()["DomainNames"] == []