python-botocore/tests/functional/test_service_names.py

70 lines
2.4 KiB
Python
Raw Normal View History

2018-01-15 17:34:17 +01:00
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import re
2021-10-04 18:33:37 +02:00
import pytest
2018-01-15 17:34:17 +01:00
2021-10-04 18:33:37 +02:00
from botocore.session import get_session
2018-01-15 17:34:17 +01:00
2021-10-04 18:33:37 +02:00
BLOCKLIST = []
2018-01-15 17:34:17 +01:00
# Service names are limited here to 50 characters here as that seems like a
# reasonable limit in the general case. Services can be added to the
# blacklist above to be given an exception.
VALID_NAME_REGEX = re.compile(
(
2022-05-26 00:10:07 +02:00
'[a-z]' # Starts with a letter
'[a-z0-9]*' # Followed by any number of letters or digits
2018-01-15 17:34:17 +01:00
'(-[a-z0-9]+)*$' # Dashes are allowed as long as they aren't
2022-05-26 00:10:07 +02:00
# consecutive or at the end
),
re.M,
)
2018-01-15 17:34:17 +01:00
VALID_NAME_EXPLANATION = (
'Service names must be made up entirely of lowercase alphanumeric '
'characters and dashes. The name must start with a letter and may not end '
'with a dash'
)
2021-10-04 18:33:37 +02:00
MIN_NAME_LENGTH_EXPLANATION = (
'Service name must be greater than or equal to 2 characters in length.'
)
MAX_NAME_LENGTH_EXPLANATION = (
'Service name must be less than or equal to 50 characters in length.'
)
2018-01-15 17:34:17 +01:00
MIN_SERVICE_NAME_LENGTH = 2
MAX_SERVICE_NAME_LENGTH = 50
2021-10-04 18:33:37 +02:00
def _service_names():
session = get_session()
loader = session.get_component('data_loader')
return loader.list_available_services('service-2')
2018-01-15 17:34:17 +01:00
2021-10-04 18:33:37 +02:00
@pytest.mark.parametrize("service_name", _service_names())
def test_service_names_are_valid_length(service_name):
if service_name not in BLOCKLIST:
service_name_length = len(service_name)
is_not_too_short = service_name_length >= MIN_SERVICE_NAME_LENGTH
is_not_too_long = service_name_length <= MAX_SERVICE_NAME_LENGTH
2018-01-15 17:34:17 +01:00
2021-10-04 18:33:37 +02:00
assert is_not_too_short, MIN_NAME_LENGTH_EXPLANATION
assert is_not_too_long, MAX_NAME_LENGTH_EXPLANATION
2018-01-15 17:34:17 +01:00
2021-10-04 18:33:37 +02:00
@pytest.mark.parametrize("service_name", _service_names())
def test_service_names_are_valid_pattern(service_name):
if service_name not in BLOCKLIST:
valid = VALID_NAME_REGEX.match(service_name) is not None
assert valid, VALID_NAME_EXPLANATION