Merge branch 'master' into ssm_docs

This commit is contained in:
Alex Bainbridge 2020-07-03 10:15:56 -04:00
commit bedcc83995
17 changed files with 727 additions and 7 deletions

View file

@ -0,0 +1 @@
from __future__ import unicode_literals

View file

@ -0,0 +1,189 @@
from __future__ import unicode_literals
import boto3
from moto import mock_applicationautoscaling, mock_ecs
import sure # noqa
from nose.tools import with_setup
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 = "service/{}/{}".format(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"
DEFAULT_SUSPENDED_STATE = {
"DynamicScalingInSuspended": True,
"DynamicScalingOutSuspended": True,
"ScheduledScalingSuspended": True,
}
def _create_ecs_defaults(ecs, create_service=True):
_ = ecs.create_cluster(clusterName=DEFAULT_ECS_CLUSTER)
_ = ecs.register_task_definition(
family=DEFAULT_ECS_TASK,
containerDefinitions=[
{
"name": "hello_world",
"image": "docker/hello-world:latest",
"cpu": 1024,
"memory": 400,
"essential": True,
"environment": [
{"name": "AWS_ACCESS_KEY_ID", "value": "SOME_ACCESS_KEY"}
],
"logConfiguration": {"logDriver": "json-file"},
}
],
)
if create_service:
_ = ecs.create_service(
cluster=DEFAULT_ECS_CLUSTER,
serviceName=DEFAULT_ECS_SERVICE,
taskDefinition=DEFAULT_ECS_TASK,
desiredCount=2,
)
@mock_ecs
@mock_applicationautoscaling
def test_describe_scalable_targets_one_basic_ecs_success():
ecs = boto3.client("ecs", region_name=DEFAULT_REGION)
_create_ecs_defaults(ecs)
client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)
client.register_scalable_target(
ServiceNamespace=DEFAULT_SERVICE_NAMESPACE,
ResourceId=DEFAULT_RESOURCE_ID,
ScalableDimension=DEFAULT_SCALABLE_DIMENSION,
)
response = client.describe_scalable_targets(
ServiceNamespace=DEFAULT_SERVICE_NAMESPACE
)
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
len(response["ScalableTargets"]).should.equal(1)
t = response["ScalableTargets"][0]
t.should.have.key("ServiceNamespace").which.should.equal(DEFAULT_SERVICE_NAMESPACE)
t.should.have.key("ResourceId").which.should.equal(DEFAULT_RESOURCE_ID)
t.should.have.key("ScalableDimension").which.should.equal(
DEFAULT_SCALABLE_DIMENSION
)
t.should.have.key("CreationTime").which.should.be.a("datetime.datetime")
@mock_ecs
@mock_applicationautoscaling
def test_describe_scalable_targets_one_full_ecs_success():
ecs = boto3.client("ecs", region_name=DEFAULT_REGION)
_create_ecs_defaults(ecs)
client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)
register_scalable_target(client)
response = client.describe_scalable_targets(
ServiceNamespace=DEFAULT_SERVICE_NAMESPACE
)
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
len(response["ScalableTargets"]).should.equal(1)
t = response["ScalableTargets"][0]
t.should.have.key("ServiceNamespace").which.should.equal(DEFAULT_SERVICE_NAMESPACE)
t.should.have.key("ResourceId").which.should.equal(DEFAULT_RESOURCE_ID)
t.should.have.key("ScalableDimension").which.should.equal(
DEFAULT_SCALABLE_DIMENSION
)
t.should.have.key("MinCapacity").which.should.equal(DEFAULT_MIN_CAPACITY)
t.should.have.key("MaxCapacity").which.should.equal(DEFAULT_MAX_CAPACITY)
t.should.have.key("RoleARN").which.should.equal(DEFAULT_ROLE_ARN)
t.should.have.key("CreationTime").which.should.be.a("datetime.datetime")
t.should.have.key("SuspendedState")
t["SuspendedState"]["DynamicScalingInSuspended"].should.equal(
DEFAULT_SUSPENDED_STATE["DynamicScalingInSuspended"]
)
@mock_ecs
@mock_applicationautoscaling
def test_describe_scalable_targets_only_return_ecs_targets():
ecs = boto3.client("ecs", region_name=DEFAULT_REGION)
_create_ecs_defaults(ecs, create_service=False)
_ = ecs.create_service(
cluster=DEFAULT_ECS_CLUSTER,
serviceName="test1",
taskDefinition=DEFAULT_ECS_TASK,
desiredCount=2,
)
_ = ecs.create_service(
cluster=DEFAULT_ECS_CLUSTER,
serviceName="test2",
taskDefinition=DEFAULT_ECS_TASK,
desiredCount=2,
)
client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)
register_scalable_target(
client,
ServiceNamespace="ecs",
ResourceId="service/{}/test1".format(DEFAULT_ECS_CLUSTER),
)
register_scalable_target(
client,
ServiceNamespace="ecs",
ResourceId="service/{}/test2".format(DEFAULT_ECS_CLUSTER),
)
register_scalable_target(
client,
ServiceNamespace="elasticmapreduce",
ResourceId="instancegroup/j-2EEZNYKUA1NTV/ig-1791Y4E1L8YI0",
ScalableDimension="elasticmapreduce:instancegroup:InstanceCount",
)
response = client.describe_scalable_targets(
ServiceNamespace=DEFAULT_SERVICE_NAMESPACE
)
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
len(response["ScalableTargets"]).should.equal(2)
@mock_ecs
@mock_applicationautoscaling
def test_describe_scalable_targets_next_token_success():
ecs = boto3.client("ecs", region_name=DEFAULT_REGION)
_create_ecs_defaults(ecs, create_service=False)
client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)
for i in range(0, 100):
_ = ecs.create_service(
cluster=DEFAULT_ECS_CLUSTER,
serviceName=str(i),
taskDefinition=DEFAULT_ECS_TASK,
desiredCount=2,
)
register_scalable_target(
client,
ServiceNamespace="ecs",
ResourceId="service/{}/{}".format(DEFAULT_ECS_CLUSTER, i),
)
response = client.describe_scalable_targets(
ServiceNamespace=DEFAULT_SERVICE_NAMESPACE
)
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
len(response["ScalableTargets"]).should.equal(50)
response["ScalableTargets"][0]["ResourceId"].should.equal("service/default/0")
response.should.have.key("NextToken").which.should.equal("49")
response = client.describe_scalable_targets(
ServiceNamespace=DEFAULT_SERVICE_NAMESPACE, NextToken=str(response["NextToken"])
)
response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)
len(response["ScalableTargets"]).should.equal(50)
response["ScalableTargets"][0]["ResourceId"].should.equal("service/default/50")
response.should_not.have.key("NextToken")
def register_scalable_target(client, **kwargs):
""" 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),
ScalableDimension=kwargs.get("ScalableDimension", DEFAULT_SCALABLE_DIMENSION),
MinCapacity=kwargs.get("MinCapacity", DEFAULT_MIN_CAPACITY),
MaxCapacity=kwargs.get("MaxCapacity", DEFAULT_MAX_CAPACITY),
RoleARN=kwargs.get("RoleARN", DEFAULT_ROLE_ARN),
SuspendedState=kwargs.get("SuspendedState", DEFAULT_SUSPENDED_STATE),
)

View file

@ -0,0 +1,123 @@
from __future__ import unicode_literals
import boto3
from moto import mock_applicationautoscaling, mock_ecs
from moto.applicationautoscaling import models
from moto.applicationautoscaling.exceptions import AWSValidationException
from botocore.exceptions import ParamValidationError
from nose.tools import assert_raises
import sure # noqa
from botocore.exceptions import ClientError
from parameterized import parameterized
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 = "service/{}/{}".format(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_applicationautoscaling
def test_describe_scalable_targets_no_params_should_raise_param_validation_errors():
client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)
with assert_raises(ParamValidationError):
client.describe_scalable_targets()
@mock_applicationautoscaling
def test_register_scalable_target_no_params_should_raise_param_validation_errors():
client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)
with assert_raises(ParamValidationError):
client.register_scalable_target()
@mock_applicationautoscaling
def test_register_scalable_target_with_none_service_namespace_should_raise_param_validation_errors():
client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)
with assert_raises(ParamValidationError):
register_scalable_target(client, ServiceNamespace=None)
@mock_applicationautoscaling
def test_describe_scalable_targets_with_invalid_scalable_dimension_should_return_validation_exception():
client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)
with assert_raises(ClientError) as err:
response = client.describe_scalable_targets(
ServiceNamespace=DEFAULT_SERVICE_NAMESPACE, ScalableDimension="foo",
)
err.response["Error"]["Code"].should.equal("ValidationException")
err.response["Error"]["Message"].split(":")[0].should.look_like(
"1 validation error detected"
)
err.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
@mock_applicationautoscaling
def test_describe_scalable_targets_with_invalid_service_namespace_should_return_validation_exception():
client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)
with assert_raises(ClientError) as err:
response = client.describe_scalable_targets(
ServiceNamespace="foo", ScalableDimension=DEFAULT_SCALABLE_DIMENSION,
)
err.response["Error"]["Code"].should.equal("ValidationException")
err.response["Error"]["Message"].split(":")[0].should.look_like(
"1 validation error detected"
)
err.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
@mock_applicationautoscaling
def test_describe_scalable_targets_with_multiple_invalid_parameters_should_return_validation_exception():
client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)
with assert_raises(ClientError) as err:
response = client.describe_scalable_targets(
ServiceNamespace="foo", ScalableDimension="bar",
)
err.response["Error"]["Code"].should.equal("ValidationException")
err.response["Error"]["Message"].split(":")[0].should.look_like(
"2 validation errors detected"
)
err.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
@mock_ecs
@mock_applicationautoscaling
def test_register_scalable_target_ecs_with_non_existent_service_should_return_validation_exception():
client = boto3.client("application-autoscaling", region_name=DEFAULT_REGION)
resource_id = "service/{}/foo".format(DEFAULT_ECS_CLUSTER)
with assert_raises(ClientError) as err:
register_scalable_target(client, ServiceNamespace="ecs", ResourceId=resource_id)
err.response["Error"]["Code"].should.equal("ValidationException")
err.response["Error"]["Message"].should.equal(
"ECS service doesn't exist: {}".format(resource_id)
)
err.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)
@parameterized(
[
("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:
models._target_params_are_valid(namespace, r_id, dimension).should.equal(
expected
)
else:
with assert_raises(AWSValidationException):
models._target_params_are_valid(namespace, r_id, dimension)
# TODO add a test for not-supplied MinCapacity or MaxCapacity (ValidationException)

View file

@ -1243,6 +1243,38 @@ def test_change_password():
result["AuthenticationResult"].should_not.be.none
@mock_cognitoidp
def test_change_password__using_custom_user_agent_header():
# https://github.com/spulec/moto/issues/3098
# As the admin_initiate_auth-method is unauthenticated, we use the user-agent header to pass in the region
# This test verifies this works, even if we pass in our own user-agent header
from botocore.config import Config
my_config = Config(user_agent_extra="more/info", signature_version="v4")
conn = boto3.client("cognito-idp", "us-west-2", config=my_config)
outputs = authentication_flow(conn)
# Take this opportunity to test change_password, which requires an access token.
newer_password = str(uuid.uuid4())
conn.change_password(
AccessToken=outputs["access_token"],
PreviousPassword=outputs["password"],
ProposedPassword=newer_password,
)
# Log in again, which should succeed without a challenge because the user is no
# longer in the force-new-password state.
result = conn.admin_initiate_auth(
UserPoolId=outputs["user_pool_id"],
ClientId=outputs["client_id"],
AuthFlow="ADMIN_NO_SRP_AUTH",
AuthParameters={"USERNAME": outputs["username"], "PASSWORD": newer_password},
)
result["AuthenticationResult"].should_not.be.none
@mock_cognitoidp
def test_forgot_password():
conn = boto3.client("cognito-idp", "us-west-2")