Esempio n. 1
0
def test_url_request_helper_fetch_metadata_token_config_item_not_present(
        mocker):
    config = get_config()
    get_aws_ec2_metadata_token_mock = mocker.patch(
        'mount_efs.get_aws_ec2_metadata_token', return_value='ABCDEFG=')
    url_open_mock = mocker.patch('mount_efs.urlopen')
    mount_efs.url_request_helper(config,
                                 mount_efs.INSTANCE_METADATA_SERVICE_URL, '',
                                 '')
    utils.assert_called(get_aws_ec2_metadata_token_mock)
    utils.assert_called(url_open_mock)
Esempio n. 2
0
def test_url_request_helper_fetch_metadata_token_config_item_present(mocker):
    config_section = mount_efs.CONFIG_SECTION
    config_item = mount_efs.DISABLE_FETCH_EC2_METADATA_TOKEN_ITEM
    config = get_config(config_section, config_item, 'false')
    get_aws_ec2_metadata_token_mock = mocker.patch(
        'mount_efs.get_aws_ec2_metadata_token', return_value='ABCDEFG=')
    url_open_mock = mocker.patch('mount_efs.urlopen')
    mount_efs.url_request_helper(config,
                                 mount_efs.INSTANCE_METADATA_SERVICE_URL, '',
                                 '')
    utils.assert_called(get_aws_ec2_metadata_token_mock)
    utils.assert_called(url_open_mock)
Esempio n. 3
0
def test_url_request_helper_does_not_fetch_metadata_token_due_to_url_not_instance_metadata_service(
        mocker):
    config_section = mount_efs.CONFIG_SECTION
    config_item = mount_efs.DISABLE_FETCH_EC2_METADATA_TOKEN_ITEM
    config = get_config(config_section, config_item, 'false')
    get_aws_ec2_metadata_token_mock = mocker.patch(
        'mount_efs.get_aws_ec2_metadata_token')
    url_open_mock = mocker.patch('mount_efs.urlopen')
    mount_efs.url_request_helper(config, mount_efs.ECS_TASK_METADATA_API, '',
                                 '')
    utils.assert_not_called(get_aws_ec2_metadata_token_mock)
    utils.assert_called(url_open_mock)
Esempio n. 4
0
def test_url_request_helper_does_not_fetch_metadata_token_due_to_token_fetch_disabled_in_config_file(
        mocker):
    config_section = mount_efs.CONFIG_SECTION
    config_item = mount_efs.DISABLE_FETCH_EC2_METADATA_TOKEN_ITEM
    config = get_config(config_section, config_item, 'true')
    get_aws_ec2_metadata_token_mock = mocker.patch(
        'mount_efs.get_aws_ec2_metadata_token')
    url_open_mock = mocker.patch('mount_efs.urlopen')
    mount_efs.url_request_helper(config,
                                 mount_efs.INSTANCE_METADATA_SERVICE_URL, '',
                                 '')
    utils.assert_not_called(get_aws_ec2_metadata_token_mock)
    utils.assert_called(url_open_mock)
Esempio n. 5
0
def test_url_request_helper_unauthorized_error(mocker, caplog):
    caplog.set_level(logging.WARNING)

    config_section = mount_efs.CONFIG_SECTION
    config_item = mount_efs.DISABLE_FETCH_EC2_METADATA_TOKEN_ITEM
    config = get_config(config_section, config_item, "true")

    get_aws_ec2_metadata_token_mock = mocker.patch(
        "mount_efs.get_aws_ec2_metadata_token"
    )
    url_open_mock = mocker.patch(
        "mount_efs.urlopen",
        side_effect=HTTPError("url", 401, "Unauthorized", None, None),
    )
    resp = mount_efs.url_request_helper(
        config, mount_efs.INSTANCE_METADATA_SERVICE_URL, "", ""
    )

    assert None == resp
    utils.assert_called(url_open_mock)
    utils.assert_not_called(get_aws_ec2_metadata_token_mock)

    assert "Unauthorized request" in [rec.message for rec in caplog.records][0]
    assert (
        "ec2 metadata token is disabled" in [rec.message for rec in caplog.records][0]
    )