def test_url_request_helper_fetch_metadata_token_config_item_not_present(mocker): config = get_config() get_aws_ec2_metadata_token_mock = mocker.patch('watchdog.get_aws_ec2_metadata_token', return_value='ABCDEFG=') url_open_mock = mocker.patch('watchdog.urlopen') watchdog.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)
def test_url_request_helper_does_not_fetch_metadata_token_due_to_url_not_instance_metadata_service(mocker): config_section = watchdog.MOUNT_CONFIG_SECTION config_item = watchdog.DISABLE_FETCH_EC2_METADATA_TOKEN_ITEM config = get_config(config_section, config_item, 'false') get_aws_ec2_metadata_token_mock = mocker.patch('watchdog.get_aws_ec2_metadata_token') url_open_mock = mocker.patch('watchdog.urlopen') watchdog.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)
def test_url_request_helper_fetch_metadata_token_config_item_present(mocker): config_section = watchdog.MOUNT_CONFIG_SECTION config_item = watchdog.DISABLE_FETCH_EC2_METADATA_TOKEN_ITEM config = get_config(config_section, config_item, 'false') get_aws_ec2_metadata_token_mock = mocker.patch('watchdog.get_aws_ec2_metadata_token', return_value='ABCDEFG=') url_open_mock = mocker.patch('watchdog.urlopen') watchdog.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)
def test_url_request_helper_does_not_fetch_metadata_token_due_to_token_fetch_disabled_in_config_file(mocker): config_section = watchdog.MOUNT_CONFIG_SECTION config_item = watchdog.DISABLE_FETCH_EC2_METADATA_TOKEN_ITEM config = get_config(config_section, config_item, 'true') get_aws_ec2_metadata_token_mock = mocker.patch('watchdog.get_aws_ec2_metadata_token') url_open_mock = mocker.patch('watchdog.urlopen') watchdog.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)
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('watchdog.get_aws_ec2_metadata_token') url_open_mock = mocker.patch('watchdog.urlopen', side_effect=HTTPError('url', 401, 'Unauthorized', None, None)) resp = watchdog.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]