File: test_validation.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (94 lines) | stat: -rw-r--r-- 3,742 bytes parent folder | download | duplicates (2)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import boto3
import pytest
from botocore.exceptions import ClientError

from moto import mock_aws
from moto.applicationautoscaling import models
from moto.applicationautoscaling.exceptions import AWSValidationException

from .test_applicationautoscaling import register_scalable_target

DEFAULT_REGION = "us-east-1"
DEFAULT_ECS_CLUSTER = "default"
DEFAULT_ECS_TASK = "test_ecs_task"
DEFAULT_ECS_SERVICE = "sample-webapp"
DEFAULT_SERVICE_NAMESPACE = "ecs"
DEFAULT_RESOURCE_ID = f"service/{DEFAULT_ECS_CLUSTER}/{DEFAULT_ECS_SERVICE}"
DEFAULT_SCALABLE_DIMENSION = "ecs:service:DesiredCount"
DEFAULT_MIN_CAPACITY = 1
DEFAULT_MAX_CAPACITY = 1
DEFAULT_ROLE_ARN = "test:arn"


@mock_aws
def test_describe_scalable_targets_with_invalid_scalable_dimension_should_return_validation_exception():
    client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)

    with pytest.raises(ClientError) as ex:
        client.describe_scalable_targets(
            ServiceNamespace=DEFAULT_SERVICE_NAMESPACE, ScalableDimension="foo"
        )
    err = ex.value.response
    assert err["Error"]["Code"] == "ValidationException"
    assert "1 validation error detected" in err["Error"]["Message"]
    assert err["ResponseMetadata"]["HTTPStatusCode"] == 400


@mock_aws
def test_describe_scalable_targets_with_invalid_service_namespace_should_return_validation_exception():
    client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)

    with pytest.raises(ClientError) as ex:
        client.describe_scalable_targets(
            ServiceNamespace="foo", ScalableDimension=DEFAULT_SCALABLE_DIMENSION
        )
    err = ex.value.response
    assert err["Error"]["Code"] == "ValidationException"
    assert "1 validation error detected" in err["Error"]["Message"]
    assert err["ResponseMetadata"]["HTTPStatusCode"] == 400


@mock_aws
def test_describe_scalable_targets_with_multiple_invalid_parameters_should_return_validation_exception():
    client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)

    with pytest.raises(ClientError) as ex:
        client.describe_scalable_targets(
            ServiceNamespace="foo", ScalableDimension="bar"
        )
    err = ex.value.response
    assert err["Error"]["Code"] == "ValidationException"
    assert "2 validation errors detected" in err["Error"]["Message"]
    assert err["ResponseMetadata"]["HTTPStatusCode"] == 400


@mock_aws
def test_register_scalable_target_ecs_with_non_existent_service_should_return_clusternotfound_exception():
    client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)
    resource_id = f"service/{DEFAULT_ECS_CLUSTER}/foo"

    with pytest.raises(ClientError) as ex:
        register_scalable_target(client, ServiceNamespace="ecs", ResourceId=resource_id)
    err = ex.value.response
    assert err["Error"]["Code"] == "ClusterNotFoundException"
    assert err["Error"]["Message"] == "Cluster not found."
    assert err["ResponseMetadata"]["HTTPStatusCode"] == 400


@pytest.mark.parametrize(
    "namespace,r_id,dimension,expected",
    [
        ("ecs", "service/default/test-svc", "ecs:service:DesiredCount", True),
        ("ecs", "banana/default/test-svc", "ecs:service:DesiredCount", False),
        ("rds", "service/default/test-svc", "ecs:service:DesiredCount", False),
    ],
)
def test_target_params_are_valid_success(namespace, r_id, dimension, expected):
    if expected is True:
        assert models._target_params_are_valid(namespace, r_id, dimension) == expected
    else:
        with pytest.raises(AWSValidationException):
            models._target_params_are_valid(namespace, r_id, dimension)


# TODO add a test for not-supplied MinCapacity or MaxCapacity (ValidationException)