File: test_service_names.py

package info (click to toggle)
python-botocore 1.40.72%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 128,088 kB
  • sloc: python: 76,667; xml: 14,037; javascript: 181; makefile: 157
file content (69 lines) | stat: -rw-r--r-- 2,470 bytes parent folder | download | duplicates (3)
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
# 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

import pytest

from botocore.session import get_session

BLOCKLIST = []

# 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(
    (
        '[a-z]'  # Starts with a letter
        '[a-z0-9]*'  # Followed by any number of letters or digits
        '(-[a-z0-9]+)*$'  # Dashes are allowed as long as they aren't
        # consecutive or at the end
    ),
    re.M,
)
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'
)
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.'
)
MIN_SERVICE_NAME_LENGTH = 2
MAX_SERVICE_NAME_LENGTH = 50


def _service_names():
    session = get_session()
    loader = session.get_component('data_loader')
    return loader.list_available_services('service-2')


@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

        assert is_not_too_short, MIN_NAME_LENGTH_EXPLANATION
        assert is_not_too_long, MAX_NAME_LENGTH_EXPLANATION


@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