File: test_efs_cloudformation.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 (189 lines) | stat: -rw-r--r-- 6,664 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
import json

import boto3

from moto import mock_aws

template_fs_simple = {
    "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": {
        "FileSystemResource": {"Type": "AWS::EFS::FileSystem", "Properties": {}},
    },
}


template_complete = {
    "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": {
        "MountTargetVPC": {
            "Type": "AWS::EC2::VPC",
            "Properties": {"CidrBlock": "172.31.0.0/16"},
        },
        "MountTargetSubnetOne": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "CidrBlock": "172.31.1.0/24",
                "VpcId": {"Ref": "MountTargetVPC"},
                "AvailabilityZone": "us-east-1a",
            },
        },
        "MountTargetSubnetTwo": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "CidrBlock": "172.31.2.0/24",
                "VpcId": {"Ref": "MountTargetVPC"},
                "AvailabilityZone": "us-east-1b",
            },
        },
        "MountTargetSubnetThree": {
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "CidrBlock": "172.31.3.0/24",
                "VpcId": {"Ref": "MountTargetVPC"},
                "AvailabilityZone": "us-east-1c",
            },
        },
        "FileSystemResource": {
            "Type": "AWS::EFS::FileSystem",
            "Properties": {
                "PerformanceMode": "maxIO",
                "LifecyclePolicies": [
                    {"TransitionToIA": "AFTER_30_DAYS"},
                    {"TransitionToPrimaryStorageClass": "AFTER_1_ACCESS"},
                ],
                "Encrypted": True,
                "FileSystemTags": [{"Key": "Name", "Value": "TestFileSystem"}],
                "FileSystemPolicy": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Action": ["elasticfilesystem:ClientMount"],
                            "Principal": {
                                "AWS": "arn:aws:iam::111122223333:role/EfsReadOnly"
                            },
                        }
                    ],
                },
                "BackupPolicy": {"Status": "ENABLED"},
                "KmsKeyId": {"Fn::GetAtt": ["key", "Arn"]},
            },
        },
        "key": {
            "Type": "AWS::KMS::Key",
            "Properties": {
                "KeyPolicy": {
                    "Version": "2012-10-17",
                    "Id": "key-default-1",
                    "Statement": [
                        {
                            "Sid": "Allow administration of the key",
                            "Effect": "Allow",
                            "Principal": {
                                "AWS": {
                                    "Fn::Join": [
                                        "",
                                        [
                                            "arn:aws:iam::",
                                            {"Ref": "AWS::AccountId"},
                                            ":root",
                                        ],
                                    ]
                                }
                            },
                            "Action": ["kms:*"],
                            "Resource": "*",
                        }
                    ],
                }
            },
        },
        "MountTargetResource1": {
            "Type": "AWS::EFS::MountTarget",
            "Properties": {
                "FileSystemId": {"Ref": "FileSystemResource"},
                "SubnetId": {"Ref": "MountTargetSubnetOne"},
                "SecurityGroups": [
                    {"Fn::GetAtt": ["MountTargetVPC", "DefaultSecurityGroup"]}
                ],
            },
        },
        "MountTargetResource2": {
            "Type": "AWS::EFS::MountTarget",
            "Properties": {
                "FileSystemId": {"Ref": "FileSystemResource"},
                "SubnetId": {"Ref": "MountTargetSubnetTwo"},
                "SecurityGroups": [
                    {"Fn::GetAtt": ["MountTargetVPC", "DefaultSecurityGroup"]}
                ],
            },
        },
        "MountTargetResource3": {
            "Type": "AWS::EFS::MountTarget",
            "Properties": {
                "FileSystemId": {"Ref": "FileSystemResource"},
                "SubnetId": {"Ref": "MountTargetSubnetThree"},
                "SecurityGroups": [
                    {"Fn::GetAtt": ["MountTargetVPC", "DefaultSecurityGroup"]}
                ],
            },
        },
        "AccessPointResource": {
            "Type": "AWS::EFS::AccessPoint",
            "Properties": {
                "FileSystemId": {"Ref": "FileSystemResource"},
                "PosixUser": {
                    "Uid": "13234",
                    "Gid": "1322",
                    "SecondaryGids": ["1344", "1452"],
                },
                "RootDirectory": {
                    "CreationInfo": {
                        "OwnerGid": "708798",
                        "OwnerUid": "7987987",
                        "Permissions": "0755",
                    },
                    "Path": "/testcfn/abc",
                },
            },
        },
    },
}


@mock_aws
def test_simple_template():
    region = "us-east-1"
    cf = boto3.client("cloudformation", region_name=region)
    cf.create_stack(StackName="teststack", TemplateBody=json.dumps(template_fs_simple))

    efs = boto3.client("efs", region)
    fs = efs.describe_file_systems()["FileSystems"][0]
    assert fs["PerformanceMode"] == "generalPurpose"
    assert fs["Encrypted"] is False
    assert fs["ThroughputMode"] == "bursting"


@mock_aws
def test_full_template():
    region = "us-east-1"
    cf = boto3.client("cloudformation", region_name=region)
    cf.create_stack(StackName="teststack", TemplateBody=json.dumps(template_complete))

    efs = boto3.client("efs", region)
    fs = efs.describe_file_systems()["FileSystems"][0]
    fs_id = fs["FileSystemId"]
    assert fs["Name"] == "TestFileSystem"
    assert fs["KmsKeyId"]

    lc = efs.describe_lifecycle_configuration(FileSystemId=fs_id)["LifecyclePolicies"]
    assert {"TransitionToIA": "AFTER_30_DAYS"} in lc
    assert {"TransitionToPrimaryStorageClass": "AFTER_1_ACCESS"} in lc

    aps = efs.describe_access_points()["AccessPoints"][0]
    assert aps["FileSystemId"] == fs_id

    cf.delete_stack(StackName="teststack")

    assert efs.describe_file_systems()["FileSystems"] == []
    assert efs.describe_access_points()["AccessPoints"] == []