test_get_dns_name.py 5.79 KB
Newer Older
Max Beckett's avatar
Max Beckett committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#
# Copyright 2017-2018 Amazon.com, Inc. and its affiliates. All Rights Reserved.
#
# Licensed under the MIT License. See the LICENSE accompanying this file
# for the specific language governing permissions and limitations under
# the License.
#

import mount_efs
import socket

import pytest

from mock import MagicMock

16
17
from .. import utils

Max Beckett's avatar
Max Beckett committed
18
19
FS_ID = 'fs-deadbeef'
DEFAULT_REGION = 'us-east-1'
20
DEFAULT_AZ = 'us-east-1a'
21
22
23
24
25
26
27
SPECIAL_REGION_DNS_DICT = {
    "cn-north-1": "amazonaws.com.cn",
    "cn-northwest-1": "amazonaws.com.cn",
    "us-iso-east-1": "c2s.ic.gov",
    "us-isob-east-1": "sc2s.sgov.gov"
}
SPECIAL_REGIONS = ["cn-north-1", "cn-northwest-1", "us-iso-east-1", "us-isob-east-1"]
28
29
DEFAULT_NFS_OPTIONS = {}
OPTIONS_WITH_AZ = {'az': DEFAULT_AZ}
Max Beckett's avatar
Max Beckett committed
30
31
32
33


@pytest.fixture(autouse=True)
def setup(mocker):
34
    mocker.patch('mount_efs.get_target_region', return_value=DEFAULT_REGION)
Max Beckett's avatar
Max Beckett committed
35
36
37
    mocker.patch('socket.gethostbyname')


38
def _get_mock_config(dns_name_format='{az}.{fs_id}.efs.{region}.{dns_name_suffix}', dns_name_suffix='amazonaws.com',
39
40
41
42
43
44
45
46
47
                     config_section='mount'):
    def config_get_side_effect(section, field):
        if section == mount_efs.CONFIG_SECTION and field == 'dns_name_format':
            return dns_name_format
        elif section == config_section and field == 'dns_name_suffix':
            return dns_name_suffix
        else:
            raise ValueError('Unexpected arguments')

Max Beckett's avatar
Max Beckett committed
48
    mock_config = MagicMock()
49
50
    mock_config.get.side_effect = config_get_side_effect
    mock_config.has_section.return_value = False
Max Beckett's avatar
Max Beckett committed
51
52
53
54
55
56
    return mock_config


def test_get_dns_name(mocker):
    config = _get_mock_config()

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    dns_name = mount_efs.get_dns_name(config, FS_ID, DEFAULT_NFS_OPTIONS)

    assert '%s.efs.%s.amazonaws.com' % (FS_ID, DEFAULT_REGION) == dns_name


def test_get_dns_name_with_az_in_options(mocker):
    config = _get_mock_config('{az}.{fs_id}.efs.{region}.amazonaws.com')

    dns_name = mount_efs.get_dns_name(config, FS_ID, OPTIONS_WITH_AZ)

    assert '%s.%s.efs.%s.amazonaws.com' % (DEFAULT_AZ, FS_ID, DEFAULT_REGION) == dns_name


def test_get_dns_name_without_az_in_options(mocker):
    config = _get_mock_config('{az}.{fs_id}.efs.{region}.amazonaws.com')

    dns_name = mount_efs.get_dns_name(config, FS_ID, DEFAULT_NFS_OPTIONS)
Max Beckett's avatar
Max Beckett committed
74
75
76
77

    assert '%s.efs.%s.amazonaws.com' % (FS_ID, DEFAULT_REGION) == dns_name


78
def test_get_dns_name_suffix_hardcoded(mocker):
79
    config = _get_mock_config('{az}.{fs_id}.efs.{region}.amazonaws.com')
Max Beckett's avatar
Max Beckett committed
80

81
    dns_name = mount_efs.get_dns_name(config, FS_ID, DEFAULT_NFS_OPTIONS)
Max Beckett's avatar
Max Beckett committed
82

83
    assert '%s.efs.%s.amazonaws.com' % (FS_ID, DEFAULT_REGION) == dns_name
Max Beckett's avatar
Max Beckett committed
84
85
86


def test_get_dns_name_region_hardcoded(mocker):
87
    get_target_region_mock = mocker.patch('mount_efs.get_target_region')
Max Beckett's avatar
Max Beckett committed
88

89
    config = _get_mock_config('{az}.{fs_id}.efs.%s.{dns_name_suffix}' % DEFAULT_REGION)
90

91
    dns_name = mount_efs.get_dns_name(config, FS_ID, DEFAULT_NFS_OPTIONS)
92

93
    utils.assert_not_called(get_target_region_mock)
94
95
96
97
98

    assert '%s.efs.%s.amazonaws.com' % (FS_ID, DEFAULT_REGION) == dns_name


def test_get_dns_name_region_and_suffix_hardcoded(mocker):
99
    get_target_region_mock = mocker.patch('mount_efs.get_target_region')
100

101
    config = _get_mock_config('{az}.{fs_id}.efs.us-west-2.amazonaws.com')
Max Beckett's avatar
Max Beckett committed
102

103
    dns_name = mount_efs.get_dns_name(config, FS_ID, DEFAULT_NFS_OPTIONS)
Max Beckett's avatar
Max Beckett committed
104

105
    utils.assert_not_called(get_target_region_mock)
Max Beckett's avatar
Max Beckett committed
106

107
    assert '%s.efs.us-west-2.amazonaws.com' % FS_ID == dns_name
Max Beckett's avatar
Max Beckett committed
108
109
110
111
112
113


def test_get_dns_name_bad_format_wrong_specifiers(mocker):
    config = _get_mock_config('{foo}.efs.{bar}')

    with pytest.raises(ValueError) as ex:
114
        mount_efs.get_dns_name(config, FS_ID, DEFAULT_NFS_OPTIONS)
Max Beckett's avatar
Max Beckett committed
115

Yuan Gao's avatar
Yuan Gao committed
116
    assert 'must include' in str(ex.value)
Max Beckett's avatar
Max Beckett committed
117
118
119
120
121
122


def test_get_dns_name_bad_format_too_many_specifiers_1(mocker):
    config = _get_mock_config('{fs_id}.efs.{foo}')

    with pytest.raises(ValueError) as ex:
123
        mount_efs.get_dns_name(config, FS_ID, DEFAULT_NFS_OPTIONS)
Max Beckett's avatar
Max Beckett committed
124

Yuan Gao's avatar
Yuan Gao committed
125
    assert 'incorrect number' in str(ex.value)
Max Beckett's avatar
Max Beckett committed
126
127
128
129
130
131


def test_get_dns_name_bad_format_too_many_specifiers_2(mocker):
    config = _get_mock_config('{fs_id}.efs.{region}.{foo}')

    with pytest.raises(ValueError) as ex:
132
        mount_efs.get_dns_name(config, FS_ID, DEFAULT_NFS_OPTIONS)
Max Beckett's avatar
Max Beckett committed
133

Yuan Gao's avatar
Yuan Gao committed
134
    assert 'incorrect number' in str(ex.value)
Max Beckett's avatar
Max Beckett committed
135
136
137
138
139
140
141
142


def test_get_dns_name_unresolvable(mocker, capsys):
    config = _get_mock_config()

    mocker.patch('socket.gethostbyname', side_effect=socket.gaierror)

    with pytest.raises(SystemExit) as ex:
143
        mount_efs.get_dns_name(config, FS_ID, DEFAULT_NFS_OPTIONS)
Max Beckett's avatar
Max Beckett committed
144
145
146
147
148

    assert 0 != ex.value.code

    out, err = capsys.readouterr()
    assert 'Failed to resolve' in err
149
150
151


def test_get_dns_name_special_region(mocker):
152
    for special_region in SPECIAL_REGIONS:
153
        mocker.patch('mount_efs.get_target_region', return_value=special_region)
154

155
156
        config_section = 'mount.%s' % special_region
        special_dns_name_suffix = SPECIAL_REGION_DNS_DICT[special_region]
157

158
159
160
        config = _get_mock_config(dns_name_suffix=special_dns_name_suffix, config_section=config_section)
        config.has_section.return_value = True

161
        dns_name = mount_efs.get_dns_name(config, FS_ID, DEFAULT_NFS_OPTIONS)
162

163
        assert '%s.efs.%s.%s' % (FS_ID, special_region, special_dns_name_suffix) == dns_name
164
165
166


def test_get_dns_name_region_in_suffix(mocker):
167
    get_target_region_mock = mocker.patch('mount_efs.get_target_region')
168

169
170
171
    for special_region in SPECIAL_REGIONS:
        special_dns_name_suffix = SPECIAL_REGION_DNS_DICT[special_region]
        dns_name_suffix = '%s.%s' % (special_region, special_dns_name_suffix)
172

173
174
        config = _get_mock_config('{fs_id}.efs.{dns_name_suffix}', dns_name_suffix=dns_name_suffix)

175
        dns_name = mount_efs.get_dns_name(config, FS_ID, DEFAULT_NFS_OPTIONS)
176

177
        utils.assert_not_called(get_target_region_mock)
178

179
        assert '%s.efs.%s.%s' % (FS_ID, special_region, special_dns_name_suffix) == dns_name