From f9d5067faae8f1b91bd76a8d947c87dba868f8f9 Mon Sep 17 00:00:00 2001 From: Ross Strickland <75338502+rkstrickland@users.noreply.github.com> Date: Wed, 7 Jul 2021 06:36:26 -0500 Subject: [PATCH] Bugfix for put_scaling_policy in applicationautoscaling. (#4054) * Bugfix for put_scaling_policy in applicationautoscaling. * Formatting. --- moto/applicationautoscaling/models.py | 2 +- .../test_applicationautoscaling.py | 41 ++++++++++++++----- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/moto/applicationautoscaling/models.py b/moto/applicationautoscaling/models.py index 1d9fb5be2..3b26c2e04 100644 --- a/moto/applicationautoscaling/models.py +++ b/moto/applicationautoscaling/models.py @@ -320,7 +320,7 @@ class FakeApplicationAutoscalingPolicy(BaseModel): self.step_scaling_policy_configuration = None self.target_tracking_scaling_policy_configuration = None - if "policy_type" == "StepScaling": + if policy_type == "StepScaling": self.step_scaling_policy_configuration = policy_body self.target_tracking_scaling_policy_configuration = None elif policy_type == "TargetTrackingScaling": diff --git a/tests/test_applicationautoscaling/test_applicationautoscaling.py b/tests/test_applicationautoscaling/test_applicationautoscaling.py index 6eff526a2..199ed7758 100644 --- a/tests/test_applicationautoscaling/test_applicationautoscaling.py +++ b/tests/test_applicationautoscaling/test_applicationautoscaling.py @@ -179,7 +179,7 @@ def test_describe_scalable_targets_next_token_success(): def register_scalable_target(client, **kwargs): - """ Build a default scalable target object for use in tests. """ + """Build a default scalable target object for use in tests.""" return client.register_scalable_target( ServiceNamespace=kwargs.get("ServiceNamespace", DEFAULT_SERVICE_NAMESPACE), ResourceId=kwargs.get("ResourceId", DEFAULT_RESOURCE_ID), @@ -315,8 +315,34 @@ def test_register_scalable_target_updates_existing_target(): ) +@pytest.mark.parametrize( + ["policy_type", "policy_body_kwargs"], + [ + [ + "TargetTrackingScaling", + { + "TargetTrackingScalingPolicyConfiguration": { + "TargetValue": 70.0, + "PredefinedMetricSpecification": { + "PredefinedMetricType": "SageMakerVariantInvocationsPerInstance" + }, + } + }, + ], + [ + "TargetTrackingScaling", + { + "StepScalingPolicyConfiguration": { + "AdjustmentType": "ChangeCapacity", + "StepAdjustments": [{"ScalingAdjustment": 10,},], + "MinAdjustmentMagnitude": 2, + }, + }, + ], + ], +) @mock_applicationautoscaling -def test_put_scaling_policy(): +def test_put_scaling_policy(policy_type, policy_body_kwargs): client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION) namespace = "sagemaker" resource_id = "endpoint/MyEndPoint/variant/MyVariant" @@ -331,13 +357,6 @@ def test_put_scaling_policy(): ) policy_name = "MyPolicy" - policy_type = "TargetTrackingScaling" - policy_body = { - "TargetValue": 70.0, - "PredefinedMetricSpecification": { - "PredefinedMetricType": "SageMakerVariantInvocationsPerInstance" - }, - } with pytest.raises(client.exceptions.ValidationException) as e: client.put_scaling_policy( @@ -346,7 +365,7 @@ def test_put_scaling_policy(): ResourceId=resource_id, ScalableDimension=scalable_dimension, PolicyType="ABCDEFG", - TargetTrackingScalingPolicyConfiguration=policy_body, + **policy_body_kwargs ) e.value.response["Error"]["Message"].should.match( r"Unknown policy type .* specified." @@ -358,7 +377,7 @@ def test_put_scaling_policy(): ResourceId=resource_id, ScalableDimension=scalable_dimension, PolicyType=policy_type, - TargetTrackingScalingPolicyConfiguration=policy_body, + **policy_body_kwargs ) response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200) response["PolicyARN"].should.match(