File: test_fsx.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 (239 lines) | stat: -rw-r--r-- 7,380 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
"""Unit tests for fsx-supported APIs."""

import re

import boto3
import pytest
from botocore.exceptions import ClientError

from moto import mock_aws

# 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

TEST_REGION_NAME = "us-east-1"
FAKE_SUBNET_ID = "subnet-012345678"
FAKE_SECURITY_GROUP_IDS = ["sg-0123456789abcdef0", "sg-0123456789abcdef1"]


@pytest.fixture(name="client")
def fixture_fsx_client():
    with mock_aws():
        yield boto3.client("fsx", region_name=TEST_REGION_NAME)


def test_create_filesystem(client):
    resp = client.create_file_system(
        FileSystemType="LUSTRE",
        StorageCapacity=1200,
        StorageType="SSD",
        SubnetIds=[FAKE_SUBNET_ID],
        SecurityGroupIds=FAKE_SECURITY_GROUP_IDS,
    )

    file_system = resp["FileSystem"]

    assert re.match(r"^fs-[0-9a-f]{8,}$", file_system["FileSystemId"])
    assert file_system["FileSystemType"] == "LUSTRE"


@mock_aws
def test_describe_filesystems():
    client = boto3.client("fsx", region_name=TEST_REGION_NAME)
    # Create a LUSTRE file system
    client.create_file_system(
        FileSystemType="LUSTRE",
        StorageCapacity=1200,
        StorageType="SSD",
        SubnetIds=[FAKE_SUBNET_ID],
        SecurityGroupIds=FAKE_SECURITY_GROUP_IDS,
    )

    # Create another WINDOWS file system
    client.create_file_system(
        FileSystemType="WINDOWS",
        StorageCapacity=1200,
        StorageType="SSD",
        SubnetIds=[FAKE_SUBNET_ID],
        SecurityGroupIds=FAKE_SECURITY_GROUP_IDS,
    )

    resp = client.describe_file_systems()
    file_systems = resp["FileSystems"]

    assert len(file_systems) == 2
    assert file_systems[0]["FileSystemType"] == "LUSTRE"
    assert file_systems[1]["FileSystemType"] == "WINDOWS"


@mock_aws
def test_describe_filesystems_does_not_exist():
    client = boto3.client("fsx", region_name=TEST_REGION_NAME)
    resp = client.describe_file_systems(FileSystemIds=["fs-1234567890"])
    file_systems = resp["FileSystems"]

    assert len(file_systems) == 0


@mock_aws
def test_delete_file_system():
    client = boto3.client("fsx", region_name=TEST_REGION_NAME)
    # Create a LUSTRE file system
    fs = client.create_file_system(
        FileSystemType="LUSTRE",
        StorageCapacity=1200,
        StorageType="SSD",
        SubnetIds=[FAKE_SUBNET_ID],
        SecurityGroupIds=FAKE_SECURITY_GROUP_IDS,
    )

    file_system_id = fs["FileSystem"]["FileSystemId"]
    resp = client.delete_file_system(FileSystemId=file_system_id)
    assert resp["FileSystemId"] == file_system_id
    assert resp["Lifecycle"] == "DELETING"
    assert resp["LustreResponse"] is not None


@mock_aws
def test_create_backup():
    client = boto3.client("fsx", region_name=TEST_REGION_NAME)
    tags = [
        {"Key": "Moto", "Value": "Hello"},
        {"Key": "Environment", "Value": "Dev"},
    ]
    fs = client.create_file_system(
        FileSystemType="LUSTRE",
        StorageCapacity=1200,
        StorageType="SSD",
        SubnetIds=[FAKE_SUBNET_ID],
        SecurityGroupIds=FAKE_SECURITY_GROUP_IDS,
    )
    resp = client.create_backup(
        FileSystemId=fs["FileSystem"]["FileSystemId"], Tags=tags
    )

    backup = resp["Backup"]
    assert backup["BackupId"].startswith("backup-")
    assert backup["ResourceARN"].startswith("arn:aws:fsx:")
    assert backup["Tags"] == tags


@mock_aws
def test_nonexistent_file_system():
    client = boto3.client("fsx", region_name=TEST_REGION_NAME)

    with pytest.raises(ClientError) as exc:
        client.create_backup(
            FileSystemId="NONEXISTENTFILESYSTEMID",
        )

    err = exc.value.response["Error"]
    assert err["Code"] == "ResourceNotFoundException"
    assert err["Message"] == "FSx resource, NONEXISTENTFILESYSTEMID does not exist"


@mock_aws
def test_delete_backup():
    client = boto3.client("fsx", region_name=TEST_REGION_NAME)
    fs = client.create_file_system(
        FileSystemType="LUSTRE",
        StorageCapacity=1200,
        StorageType="SSD",
        SubnetIds=[FAKE_SUBNET_ID],
        SecurityGroupIds=FAKE_SECURITY_GROUP_IDS,
    )
    resp = client.create_backup(FileSystemId=fs["FileSystem"]["FileSystemId"])

    backup = resp["Backup"]
    assert backup["BackupId"].startswith("backup-")
    assert backup["ResourceARN"].startswith("arn:aws:fsx:")

    resp = client.delete_backup(BackupId=backup["BackupId"])
    assert resp["BackupId"] == backup["BackupId"]
    assert resp["Lifecycle"] == "DELETED"


@mock_aws
def test_backup_not_found():
    client = boto3.client("fsx", region_name=TEST_REGION_NAME)
    with pytest.raises(ClientError) as exc:
        client.delete_backup(BackupId="NONEXISTENTBACKUPID")

    err = exc.value.response["Error"]
    assert err["Code"] == "ResourceNotFoundException"
    assert err["Message"] == "FSx resource, NONEXISTENTBACKUPID does not exist"


@mock_aws
def test_tag_resource():
    client = boto3.client("fsx", region_name=TEST_REGION_NAME)
    fs = client.create_file_system(
        FileSystemType="LUSTRE",
        StorageCapacity=1200,
        StorageType="SSD",
        SubnetIds=[FAKE_SUBNET_ID],
        SecurityGroupIds=FAKE_SECURITY_GROUP_IDS,
    )
    file_system_id = fs["FileSystem"]["FileSystemId"]

    resp = client.describe_file_systems(FileSystemIds=[file_system_id])
    resource_arn = resp["FileSystems"][0]["ResourceARN"]
    tags = [
        {"Key": "Moto", "Value": "Hello"},
        {"Key": "Environment", "Value": "Dev"},
    ]

    client.tag_resource(ResourceARN=resource_arn, Tags=tags)

    resp = client.describe_file_systems(FileSystemIds=[file_system_id])
    assert resp["FileSystems"][0]["Tags"] == tags


@mock_aws
def test_untag_resource():
    client = boto3.client("fsx", region_name=TEST_REGION_NAME)
    fs = client.create_file_system(
        FileSystemType="LUSTRE",
        StorageCapacity=1200,
        StorageType="SSD",
        SubnetIds=[FAKE_SUBNET_ID],
        SecurityGroupIds=FAKE_SECURITY_GROUP_IDS,
        Tags=[
            {"Key": "Moto", "Value": "Hello"},
            {"Key": "Environment", "Value": "Dev"},
        ],
    )
    file_system_id = fs["FileSystem"]["FileSystemId"]

    resp = client.describe_file_systems(FileSystemIds=[file_system_id])
    resource_arn = resp["FileSystems"][0]["ResourceARN"]

    tag_keys = ["Moto"]

    client.untag_resource(ResourceARN=resource_arn, TagKeys=tag_keys)

    resp = client.describe_file_systems(FileSystemIds=[file_system_id])
    assert len(resp["FileSystems"][0]["Tags"]) == 1
    assert resp["FileSystems"][0]["Tags"] == [{"Key": "Environment", "Value": "Dev"}]


@mock_aws
def test_list_tags_for_resource():
    client = boto3.client("fsx", region_name=TEST_REGION_NAME)
    fs = client.create_file_system(
        FileSystemType="LUSTRE",
        StorageCapacity=1200,
        StorageType="SSD",
        SubnetIds=[FAKE_SUBNET_ID],
        SecurityGroupIds=FAKE_SECURITY_GROUP_IDS,
    )

    tags = [
        {"Key": "Moto", "Value": "Hello"},
        {"Key": "Environment", "Value": "Dev"},
    ]
    resource_arn = fs["FileSystem"]["ResourceARN"]
    client.tag_resource(ResourceARN=resource_arn, Tags=tags)
    resp = client.list_tags_for_resource(ResourceARN=resource_arn)
    assert len(resp["Tags"]) == 2
    assert resp["Tags"] == tags