ApplicationAutoscaling: support autoscaling policies, deregister_scalable_target (#3350)

* ApplicationAutoscaling: support autoscaling policies, deregister_scalable_target.

* PR3350 comment changes: drop unnecessary pass statements, unit test three exception cases.

Co-authored-by: Joseph Weitekamp <jweite@amazon.com>
This commit is contained in:
jweite 2020-10-30 11:21:34 -04:00 committed by GitHub
commit cbd4efb42d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 439 additions and 21 deletions

View file

@ -1,8 +1,10 @@
from __future__ import unicode_literals
import botocore
import boto3
from moto import mock_applicationautoscaling, mock_ecs
import sure # noqa
from nose.tools import with_setup
from nose.tools import assert_raises
from moto import mock_applicationautoscaling, mock_ecs
from moto.applicationautoscaling.exceptions import AWSValidationException
DEFAULT_REGION = "us-east-1"
DEFAULT_ECS_CLUSTER = "default"
@ -250,6 +252,8 @@ def test_register_scalable_target_resource_id_variations():
ServiceNamespace=namespace,
ResourceId=resource_id,
ScalableDimension=scalable_dimension,
MinCapacity=1,
MaxCapacity=8,
)
response = client.describe_scalable_targets(ServiceNamespace=namespace)
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
@ -304,3 +308,211 @@ def test_register_scalable_target_updates_existing_target():
t["SuspendedState"]["ScheduledScalingSuspended"].should.equal(
updated_suspended_state["ScheduledScalingSuspended"]
)
@mock_applicationautoscaling
def test_put_scaling_policy():
client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)
namespace = "sagemaker"
resource_id = "endpoint/MyEndPoint/variant/MyVariant"
scalable_dimension = "sagemaker:variant:DesiredInstanceCount"
client.register_scalable_target(
ServiceNamespace=namespace,
ResourceId=resource_id,
ScalableDimension=scalable_dimension,
MinCapacity=1,
MaxCapacity=8,
)
policy_name = "MyPolicy"
policy_type = "TargetTrackingScaling"
policy_body = {
"TargetValue": 70.0,
"PredefinedMetricSpecification": {
"PredefinedMetricType": "SageMakerVariantInvocationsPerInstance"
},
}
with assert_raises(client.exceptions.ValidationException) as e:
client.put_scaling_policy(
PolicyName=policy_name,
ServiceNamespace=namespace,
ResourceId=resource_id,
ScalableDimension=scalable_dimension,
PolicyType="ABCDEFG",
TargetTrackingScalingPolicyConfiguration=policy_body,
)
e.exception.response["Error"]["Message"].should.match(
r"Unknown policy type .* specified."
)
response = client.put_scaling_policy(
PolicyName=policy_name,
ServiceNamespace=namespace,
ResourceId=resource_id,
ScalableDimension=scalable_dimension,
PolicyType=policy_type,
TargetTrackingScalingPolicyConfiguration=policy_body,
)
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
response["PolicyARN"].should.match(
r"arn:aws:autoscaling:.*1:scalingPolicy:.*:resource/{}/{}:policyName/{}".format(
namespace, resource_id, policy_name
)
)
@mock_applicationautoscaling
def test_describe_scaling_policies():
client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)
namespace = "sagemaker"
resource_id = "endpoint/MyEndPoint/variant/MyVariant"
scalable_dimension = "sagemaker:variant:DesiredInstanceCount"
client.register_scalable_target(
ServiceNamespace=namespace,
ResourceId=resource_id,
ScalableDimension=scalable_dimension,
MinCapacity=1,
MaxCapacity=8,
)
policy_name = "MyPolicy"
policy_type = "TargetTrackingScaling"
policy_body = {
"TargetValue": 70.0,
"PredefinedMetricSpecification": {
"PredefinedMetricType": "SageMakerVariantInvocationsPerInstance"
},
}
response = client.put_scaling_policy(
PolicyName=policy_name,
ServiceNamespace=namespace,
ResourceId=resource_id,
ScalableDimension=scalable_dimension,
PolicyType=policy_type,
TargetTrackingScalingPolicyConfiguration=policy_body,
)
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
response = client.describe_scaling_policies(
PolicyNames=[policy_name],
ServiceNamespace=namespace,
ResourceId=resource_id,
ScalableDimension=scalable_dimension,
)
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
policy = response["ScalingPolicies"][0]
policy["PolicyName"].should.equal(policy_name)
policy["ServiceNamespace"].should.equal(namespace)
policy["ResourceId"].should.equal(resource_id)
policy["ScalableDimension"].should.equal(scalable_dimension)
policy["PolicyType"].should.equal(policy_type)
policy["TargetTrackingScalingPolicyConfiguration"].should.equal(policy_body)
policy["PolicyARN"].should.match(
r"arn:aws:autoscaling:.*1:scalingPolicy:.*:resource/{}/{}:policyName/{}".format(
namespace, resource_id, policy_name
)
)
policy.should.have.key("CreationTime").which.should.be.a("datetime.datetime")
@mock_applicationautoscaling
def test_delete_scaling_policies():
client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)
namespace = "sagemaker"
resource_id = "endpoint/MyEndPoint/variant/MyVariant"
scalable_dimension = "sagemaker:variant:DesiredInstanceCount"
client.register_scalable_target(
ServiceNamespace=namespace,
ResourceId=resource_id,
ScalableDimension=scalable_dimension,
MinCapacity=1,
MaxCapacity=8,
)
policy_name = "MyPolicy"
policy_type = "TargetTrackingScaling"
policy_body = {
"TargetValue": 70.0,
"PredefinedMetricSpecification": {
"PredefinedMetricType": "SageMakerVariantInvocationsPerInstance"
},
}
with assert_raises(client.exceptions.ValidationException) as e:
client.delete_scaling_policy(
PolicyName=policy_name,
ServiceNamespace=namespace,
ResourceId=resource_id,
ScalableDimension=scalable_dimension,
)
e.exception.response["Error"]["Message"].should.match(r"No scaling policy found .*")
response = client.put_scaling_policy(
PolicyName=policy_name,
ServiceNamespace=namespace,
ResourceId=resource_id,
ScalableDimension=scalable_dimension,
PolicyType=policy_type,
TargetTrackingScalingPolicyConfiguration=policy_body,
)
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
response = client.delete_scaling_policy(
PolicyName=policy_name,
ServiceNamespace=namespace,
ResourceId=resource_id,
ScalableDimension=scalable_dimension,
)
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
response = client.describe_scaling_policies(
PolicyNames=[policy_name],
ServiceNamespace=namespace,
ResourceId=resource_id,
ScalableDimension=scalable_dimension,
)
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
len(response["ScalingPolicies"]).should.equal(0)
@mock_applicationautoscaling
def test_deregister_scalable_target():
client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)
namespace = "sagemaker"
resource_id = "endpoint/MyEndPoint/variant/MyVariant"
scalable_dimension = "sagemaker:variant:DesiredInstanceCount"
client.register_scalable_target(
ServiceNamespace=namespace,
ResourceId=resource_id,
ScalableDimension=scalable_dimension,
MinCapacity=1,
MaxCapacity=8,
)
response = client.describe_scalable_targets(ServiceNamespace=namespace)
len(response["ScalableTargets"]).should.equal(1)
client.deregister_scalable_target(
ServiceNamespace=namespace,
ResourceId=resource_id,
ScalableDimension=scalable_dimension,
)
response = client.describe_scalable_targets(ServiceNamespace=namespace)
len(response["ScalableTargets"]).should.equal(0)
with assert_raises(client.exceptions.ValidationException) as e:
client.deregister_scalable_target(
ServiceNamespace=namespace,
ResourceId=resource_id,
ScalableDimension=scalable_dimension,
)
e.exception.response["Error"]["Message"].should.match(
r"No scalable target found .*"
)