From aa70ee254da5c44a364db36ccf0f041ee17109f0 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Tue, 18 Jan 2022 14:18:57 -0100 Subject: [PATCH] Remove deprecated decorators + boto dependency (#4378) --- docs/docs/boto.rst | 38 - docs/index.rst | 1 - moto/__init__.py | 37 - moto/apigateway/__init__.py | 3 +- moto/athena/__init__.py | 3 +- moto/autoscaling/__init__.py | 3 +- moto/awslambda/__init__.py | 3 +- moto/cloudformation/__init__.py | 3 +- moto/cloudformation/parsing.py | 12 +- moto/cloudwatch/__init__.py | 3 +- moto/cognitoidentity/__init__.py | 3 +- moto/cognitoidp/__init__.py | 3 +- moto/core/models.py | 36 +- moto/core/utils.py | 24 +- moto/datapipeline/__init__.py | 3 +- moto/datasync/__init__.py | 3 +- moto/dynamodb/__init__.py | 1 - moto/dynamodb2/__init__.py | 3 +- moto/dynamodb2/parsing/tokens.py | 10 +- moto/ec2/__init__.py | 3 +- moto/ecr/__init__.py | 3 +- moto/ecs/__init__.py | 3 +- moto/elb/__init__.py | 3 +- moto/elb/models.py | 67 +- moto/elb/responses.py | 66 +- moto/emr/__init__.py | 3 +- moto/glacier/__init__.py | 3 +- moto/iam/__init__.py | 1 - moto/kinesis/__init__.py | 3 +- moto/kms/__init__.py | 3 +- moto/logs/__init__.py | 3 +- moto/managedblockchain/__init__.py | 5 +- moto/opsworks/__init__.py | 3 +- moto/packages/boto/cloudformation/__init__.py | 0 moto/packages/boto/cloudformation/stack.py | 9 - moto/packages/boto/ec2/elb/attributes.py | 100 - moto/packages/httpretty/__init__.py | 61 - moto/packages/httpretty/compat.py | 106 - moto/packages/httpretty/core.py | 1147 ----------- moto/packages/httpretty/errors.py | 39 - moto/packages/httpretty/http.py | 154 -- moto/packages/httpretty/utils.py | 46 - moto/rds/__init__.py | 3 +- moto/rds2/__init__.py | 3 +- moto/redshift/__init__.py | 3 +- moto/route53/__init__.py | 1 - moto/s3/__init__.py | 1 - moto/s3/models.py | 17 - moto/s3/responses.py | 78 +- moto/server.py | 4 +- moto/ses/__init__.py | 1 - moto/sns/__init__.py | 3 +- moto/sqs/__init__.py | 3 +- moto/sts/__init__.py | 1 - moto/swf/__init__.py | 3 +- requirements-dev.txt | 1 - setup.py | 1 - tests/helpers.py | 26 - tests/test_autoscaling/test_autoscaling.py | 469 ----- .../test_launch_configurations.py | 220 --- tests/test_autoscaling/test_policies.py | 195 +- tests/test_autoscaling/utils.py | 12 +- .../test_cloudformation_stack_crud.py | 772 -------- .../test_cloudformation_stack_crud_boto3.py | 32 +- .../test_cloudformation_stack_integration.py | 1723 ----------------- .../test_cloudformation/test_import_value.py | 2 +- .../test_cloudformation/test_stack_parsing.py | 2 +- tests/test_cloudwatch/test_cloudwatch.py | 225 --- .../test_cloudwatch/test_cloudwatch_boto3.py | 42 +- tests/test_core/test_decorator_calls.py | 99 +- tests/test_core/test_nested.py | 26 - tests/test_core/test_socket.py | 30 +- tests/test_core/test_url_base_regex.py | 5 +- tests/test_datapipeline/test_datapipeline.py | 134 +- tests/test_dynamodb/test_dynamodb.py | 49 +- .../test_dynamodb_table_with_range_key.py | 485 ----- .../test_dynamodb_table_without_range_key.py | 405 ---- tests/test_dynamodb2/test_dynamodb.py | 84 +- .../test_dynamodb_table_with_range_key.py | 955 +-------- .../test_dynamodb_table_without_range_key.py | 530 +---- tests/test_ec2/test_amis.py | 632 +----- .../test_availability_zones_and_regions.py | 27 +- tests/test_ec2/test_customer_gateways.py | 54 +- tests/test_ec2/test_dhcp_options.py | 280 +-- tests/test_ec2/test_ec2_cloudformation.py | 30 - tests/test_ec2/test_elastic_block_store.py | 658 +------ tests/test_ec2/test_elastic_ip_addresses.py | 388 +--- .../test_elastic_network_interfaces.py | 214 +- tests/test_ec2/test_general.py | 26 +- tests/test_ec2/test_instances.py | 1026 +--------- tests/test_ec2/test_internet_gateways.py | 272 +-- tests/test_ec2/test_key_pairs.py | 190 +- tests/test_ec2/test_network_acls.py | 192 +- tests/test_ec2/test_regions.py | 147 -- tests/test_ec2/test_route_tables.py | 540 +----- tests/test_ec2/test_security_groups.py | 656 +------ tests/test_ec2/test_spot_instances.py | 122 +- tests/test_ec2/test_subnets.py | 183 +- tests/test_ec2/test_tags.py | 410 +--- .../test_ec2/test_virtual_private_gateways.py | 172 +- tests/test_ec2/test_vpc_peering.py | 95 +- tests/test_ec2/test_vpcs.py | 264 +-- tests/test_ec2/test_vpn_connections.py | 54 +- tests/test_elb/test_elb.py | 538 +---- tests/test_emr/test_emr.py | 678 ------- tests/test_glacier/test_glacier_archives.py | 18 +- tests/test_glacier/test_glacier_jobs.py | 91 +- tests/test_glacier/test_glacier_vaults.py | 30 +- tests/test_iam/test_iam.py | 485 +---- tests/test_iam/test_iam_groups.py | 115 +- tests/test_kinesis/test_kinesis.py | 430 +--- tests/test_kms/test_kms.py | 793 -------- tests/test_kms/test_kms_boto3.py | 67 + tests/test_packages/__init__.py | 6 - tests/test_packages/test_httpretty.py | 41 - tests/test_rds/test_rds.py | 301 +-- tests/test_redshift/test_redshift.py | 433 ----- tests/test_route53/test_route53.py | 375 +--- tests/test_s3/test_s3.py | 1211 +----------- tests/test_s3/test_s3_lifecycle.py | 103 +- tests/test_s3/test_s3_lock.py | 2 +- .../test_s3bucket_path/test_s3bucket_path.py | 321 --- .../test_s3bucket_path_combo.py | 23 - tests/test_ses/test_ses.py | 185 -- tests/test_sns/test_application.py | 319 --- tests/test_sns/test_publishing.py | 106 - tests/test_sns/test_subscriptions.py | 164 -- tests/test_sns/test_topics.py | 190 -- tests/test_sqs/test_integration.py | 1 - tests/test_sqs/test_sqs.py | 385 +--- tests/test_sts/test_sts.py | 85 +- .../test_swf/responses/test_activity_tasks.py | 229 +-- .../test_swf/responses/test_activity_types.py | 144 -- .../test_swf/responses/test_decision_tasks.py | 385 +--- tests/test_swf/responses/test_domains.py | 121 -- tests/test_swf/responses/test_timeouts.py | 127 +- .../responses/test_workflow_executions.py | 283 +-- .../test_swf/responses/test_workflow_types.py | 150 -- tests/test_swf/utils.py | 22 - 139 files changed, 560 insertions(+), 23697 deletions(-) delete mode 100644 docs/docs/boto.rst delete mode 100644 moto/packages/boto/cloudformation/__init__.py delete mode 100644 moto/packages/boto/cloudformation/stack.py delete mode 100644 moto/packages/boto/ec2/elb/attributes.py delete mode 100644 moto/packages/httpretty/__init__.py delete mode 100644 moto/packages/httpretty/compat.py delete mode 100644 moto/packages/httpretty/core.py delete mode 100644 moto/packages/httpretty/errors.py delete mode 100644 moto/packages/httpretty/http.py delete mode 100644 moto/packages/httpretty/utils.py delete mode 100644 tests/test_cloudformation/test_cloudformation_stack_crud.py delete mode 100644 tests/test_cloudwatch/test_cloudwatch.py delete mode 100644 tests/test_dynamodb/test_dynamodb_table_with_range_key.py delete mode 100644 tests/test_dynamodb/test_dynamodb_table_without_range_key.py delete mode 100644 tests/test_emr/test_emr.py delete mode 100644 tests/test_kms/test_kms.py delete mode 100644 tests/test_packages/__init__.py delete mode 100644 tests/test_packages/test_httpretty.py delete mode 100644 tests/test_s3bucket_path/test_s3bucket_path.py delete mode 100644 tests/test_s3bucket_path/test_s3bucket_path_combo.py delete mode 100644 tests/test_ses/test_ses.py delete mode 100644 tests/test_sns/test_application.py delete mode 100644 tests/test_sns/test_publishing.py delete mode 100644 tests/test_sns/test_subscriptions.py delete mode 100644 tests/test_sns/test_topics.py diff --git a/docs/docs/boto.rst b/docs/docs/boto.rst deleted file mode 100644 index 068262865..000000000 --- a/docs/docs/boto.rst +++ /dev/null @@ -1,38 +0,0 @@ -.. _boto: - -============= -Boto vs Boto3 -============= - -Boto3 is the latest Python SDK, and as such the SDK targeted by Moto. All our `@mock_`-decorators should be usable against any boto3-version. - -Still stuck on boto, the former SDK? Moto does have some support, in the form of our deprecated services: - -.. sourcecode:: python - - from moto import mock_ec2_deprecated - import boto - - @mock_ec2_deprecated - def test_something_with_ec2(): - ec2_conn = boto.ec2.connect_to_region('us-east-1') - ec2_conn.get_only_instances(instance_ids='i-123456') - - - -When using both boto2 and boto3, one can do this to avoid confusion: - -.. sourcecode:: python - - from moto import mock_ec2_deprecated as mock_ec2_b2 - from moto import mock_ec2 - -If you want to use Server Mode, the easiest way is to create a boto config file (`~/.boto`) with the following values: - -.. code-block:: bash - - [Boto] - is_secure = False - https_validate_certificates = False - proxy_port = 5000 - proxy = 127.0.0.1 diff --git a/docs/index.rst b/docs/index.rst index c9af8c2a4..22e1e4cb9 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -33,7 +33,6 @@ Additional Resources docs/getting_started docs/server_mode docs/faq - docs/boto docs/iam docs/aws_config diff --git a/moto/__init__.py b/moto/__init__.py index 5bc951479..dfba3ea92 100644 --- a/moto/__init__.py +++ b/moto/__init__.py @@ -26,68 +26,47 @@ def lazy_load( mock_acm = lazy_load(".acm", "mock_acm") mock_apigateway = lazy_load(".apigateway", "mock_apigateway") -mock_apigateway_deprecated = lazy_load(".apigateway", "mock_apigateway_deprecated") mock_appsync = lazy_load(".appsync", "mock_appsync", boto3_name="appsync") mock_athena = lazy_load(".athena", "mock_athena") mock_applicationautoscaling = lazy_load( ".applicationautoscaling", "mock_applicationautoscaling" ) mock_autoscaling = lazy_load(".autoscaling", "mock_autoscaling") -mock_autoscaling_deprecated = lazy_load(".autoscaling", "mock_autoscaling_deprecated") mock_lambda = lazy_load( ".awslambda", "mock_lambda", boto3_name="lambda", backend="lambda_backends" ) -mock_lambda_deprecated = lazy_load(".awslambda", "mock_lambda_deprecated") mock_batch = lazy_load(".batch", "mock_batch") mock_budgets = lazy_load(".budgets", "mock_budgets") mock_cloudformation = lazy_load(".cloudformation", "mock_cloudformation") -mock_cloudformation_deprecated = lazy_load( - ".cloudformation", "mock_cloudformation_deprecated" -) mock_cloudfront = lazy_load(".cloudfront", "mock_cloudfront") mock_cloudtrail = lazy_load(".cloudtrail", "mock_cloudtrail", boto3_name="cloudtrail") mock_cloudwatch = lazy_load(".cloudwatch", "mock_cloudwatch") -mock_cloudwatch_deprecated = lazy_load(".cloudwatch", "mock_cloudwatch_deprecated") mock_codecommit = lazy_load(".codecommit", "mock_codecommit") mock_codepipeline = lazy_load(".codepipeline", "mock_codepipeline") mock_cognitoidentity = lazy_load( ".cognitoidentity", "mock_cognitoidentity", boto3_name="cognito-identity" ) -mock_cognitoidentity_deprecated = lazy_load( - ".cognitoidentity", "mock_cognitoidentity_deprecated" -) mock_cognitoidp = lazy_load(".cognitoidp", "mock_cognitoidp", boto3_name="cognito-idp") -mock_cognitoidp_deprecated = lazy_load(".cognitoidp", "mock_cognitoidp_deprecated") mock_config = lazy_load(".config", "mock_config") mock_datapipeline = lazy_load(".datapipeline", "mock_datapipeline") -mock_datapipeline_deprecated = lazy_load( - ".datapipeline", "mock_datapipeline_deprecated" -) mock_datasync = lazy_load(".datasync", "mock_datasync") mock_dax = lazy_load(".dax", "mock_dax") mock_dms = lazy_load(".dms", "mock_dms") mock_ds = lazy_load(".ds", "mock_ds", boto3_name="ds") mock_dynamodb = lazy_load(".dynamodb", "mock_dynamodb", warn_repurpose=True) -mock_dynamodb_deprecated = lazy_load(".dynamodb", "mock_dynamodb_deprecated") mock_dynamodb2 = lazy_load(".dynamodb2", "mock_dynamodb2", backend="dynamodb_backends2") -mock_dynamodb2_deprecated = lazy_load(".dynamodb2", "mock_dynamodb2_deprecated") mock_dynamodbstreams = lazy_load(".dynamodbstreams", "mock_dynamodbstreams") mock_elasticbeanstalk = lazy_load( ".elasticbeanstalk", "mock_elasticbeanstalk", backend="eb_backends" ) mock_ec2 = lazy_load(".ec2", "mock_ec2") -mock_ec2_deprecated = lazy_load(".ec2", "mock_ec2_deprecated") mock_ec2instanceconnect = lazy_load(".ec2instanceconnect", "mock_ec2instanceconnect") mock_ecr = lazy_load(".ecr", "mock_ecr") -mock_ecr_deprecated = lazy_load(".ecr", "mock_ecr_deprecated") mock_ecs = lazy_load(".ecs", "mock_ecs") -mock_ecs_deprecated = lazy_load(".ecs", "mock_ecs_deprecated") mock_elastictranscoder = lazy_load(".elastictranscoder", "mock_elastictranscoder") mock_elb = lazy_load(".elb", "mock_elb") -mock_elb_deprecated = lazy_load(".elb", "mock_elb_deprecated") mock_elbv2 = lazy_load(".elbv2", "mock_elbv2") mock_emr = lazy_load(".emr", "mock_emr") -mock_emr_deprecated = lazy_load(".emr", "mock_emr_deprecated") mock_emrcontainers = lazy_load( ".emrcontainers", "mock_emrcontainers", boto3_name="emr-containers" ) @@ -96,31 +75,22 @@ mock_events = lazy_load(".events", "mock_events") mock_firehose = lazy_load(".firehose", "mock_firehose") mock_forecast = lazy_load(".forecast", "mock_forecast") mock_glacier = lazy_load(".glacier", "mock_glacier") -mock_glacier_deprecated = lazy_load(".glacier", "mock_glacier_deprecated") mock_glue = lazy_load(".glue", "mock_glue") mock_guardduty = lazy_load(".guardduty", "mock_guardduty") mock_iam = lazy_load(".iam", "mock_iam") -mock_iam_deprecated = lazy_load(".iam", "mock_iam_deprecated") mock_iot = lazy_load(".iot", "mock_iot") mock_iotdata = lazy_load(".iotdata", "mock_iotdata", boto3_name="iot-data") mock_kinesis = lazy_load(".kinesis", "mock_kinesis") -mock_kinesis_deprecated = lazy_load(".kinesis", "mock_kinesis_deprecated") mock_kms = lazy_load(".kms", "mock_kms") -mock_kms_deprecated = lazy_load(".kms", "mock_kms_deprecated") mock_logs = lazy_load(".logs", "mock_logs") -mock_logs_deprecated = lazy_load(".logs", "mock_logs_deprecated") mock_managedblockchain = lazy_load(".managedblockchain", "mock_managedblockchain") mock_opsworks = lazy_load(".opsworks", "mock_opsworks") -mock_opsworks_deprecated = lazy_load(".opsworks", "mock_opsworks_deprecated") mock_organizations = lazy_load(".organizations", "mock_organizations") mock_polly = lazy_load(".polly", "mock_polly") mock_ram = lazy_load(".ram", "mock_ram") mock_rds = lazy_load(".rds", "mock_rds", warn_repurpose=True) -mock_rds_deprecated = lazy_load(".rds", "mock_rds_deprecated") mock_rds2 = lazy_load(".rds2", "mock_rds2", boto3_name="rds") -mock_rds2_deprecated = lazy_load(".rds2", "mock_rds2_deprecated") mock_redshift = lazy_load(".redshift", "mock_redshift") -mock_redshift_deprecated = lazy_load(".redshift", "mock_redshift_deprecated") mock_resourcegroups = lazy_load( ".resourcegroups", "mock_resourcegroups", boto3_name="resource-groups" ) @@ -128,29 +98,22 @@ mock_resourcegroupstaggingapi = lazy_load( ".resourcegroupstaggingapi", "mock_resourcegroupstaggingapi" ) mock_route53 = lazy_load(".route53", "mock_route53") -mock_route53_deprecated = lazy_load(".route53", "mock_route53_deprecated") mock_route53resolver = lazy_load( ".route53resolver", "mock_route53resolver", boto3_name="route53resolver" ) mock_s3 = lazy_load(".s3", "mock_s3") -mock_s3_deprecated = lazy_load(".s3", "mock_s3_deprecated") mock_sagemaker = lazy_load(".sagemaker", "mock_sagemaker") mock_secretsmanager = lazy_load(".secretsmanager", "mock_secretsmanager") mock_ses = lazy_load(".ses", "mock_ses") -mock_ses_deprecated = lazy_load(".ses", "mock_ses_deprecated") mock_sns = lazy_load(".sns", "mock_sns") -mock_sns_deprecated = lazy_load(".sns", "mock_sns_deprecated") mock_sqs = lazy_load(".sqs", "mock_sqs") -mock_sqs_deprecated = lazy_load(".sqs", "mock_sqs_deprecated") mock_ssm = lazy_load(".ssm", "mock_ssm") mock_ssoadmin = lazy_load(".ssoadmin", "mock_ssoadmin", boto3_name="sso-admin") mock_stepfunctions = lazy_load( ".stepfunctions", "mock_stepfunctions", backend="stepfunction_backends" ) mock_sts = lazy_load(".sts", "mock_sts") -mock_sts_deprecated = lazy_load(".sts", "mock_sts_deprecated") mock_swf = lazy_load(".swf", "mock_swf") -mock_swf_deprecated = lazy_load(".swf", "mock_swf_deprecated") mock_timestreamwrite = lazy_load( ".timestreamwrite", "mock_timestreamwrite", boto3_name="timestream-write" ) diff --git a/moto/apigateway/__init__.py b/moto/apigateway/__init__.py index 785e7f2cd..436ec6fec 100644 --- a/moto/apigateway/__init__.py +++ b/moto/apigateway/__init__.py @@ -1,6 +1,5 @@ from .models import apigateway_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator apigateway_backend = apigateway_backends["us-east-1"] mock_apigateway = base_decorator(apigateway_backends) -mock_apigateway_deprecated = deprecated_base_decorator(apigateway_backends) diff --git a/moto/athena/__init__.py b/moto/athena/__init__.py index 6252967ec..0e5ca2628 100644 --- a/moto/athena/__init__.py +++ b/moto/athena/__init__.py @@ -1,6 +1,5 @@ from .models import athena_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator athena_backend = athena_backends["us-east-1"] mock_athena = base_decorator(athena_backends) -mock_athena_deprecated = deprecated_base_decorator(athena_backends) diff --git a/moto/autoscaling/__init__.py b/moto/autoscaling/__init__.py index 151c89ea9..14ba8f3d9 100644 --- a/moto/autoscaling/__init__.py +++ b/moto/autoscaling/__init__.py @@ -1,6 +1,5 @@ from .models import autoscaling_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator autoscaling_backend = autoscaling_backends["us-east-1"] mock_autoscaling = base_decorator(autoscaling_backends) -mock_autoscaling_deprecated = deprecated_base_decorator(autoscaling_backends) diff --git a/moto/awslambda/__init__.py b/moto/awslambda/__init__.py index e8b1e49f9..3954aa95e 100644 --- a/moto/awslambda/__init__.py +++ b/moto/awslambda/__init__.py @@ -1,6 +1,5 @@ from .models import lambda_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator lambda_backend = lambda_backends["us-east-1"] mock_lambda = base_decorator(lambda_backends) -mock_lambda_deprecated = deprecated_base_decorator(lambda_backends) diff --git a/moto/cloudformation/__init__.py b/moto/cloudformation/__init__.py index b1e0571b2..7fb7471d0 100644 --- a/moto/cloudformation/__init__.py +++ b/moto/cloudformation/__init__.py @@ -1,6 +1,5 @@ from .models import cloudformation_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator cloudformation_backend = cloudformation_backends["us-east-1"] mock_cloudformation = base_decorator(cloudformation_backends) -mock_cloudformation_deprecated = deprecated_base_decorator(cloudformation_backends) diff --git a/moto/cloudformation/parsing.py b/moto/cloudformation/parsing.py index ad64d1f14..f930e80e0 100644 --- a/moto/cloudformation/parsing.py +++ b/moto/cloudformation/parsing.py @@ -57,7 +57,6 @@ from .exceptions import ( ValidationError, UnsupportedAttribute, ) -from moto.packages.boto.cloudformation.stack import Output # List of supported CloudFormation models MODEL_LIST = CloudFormationModel.__subclasses__() @@ -78,6 +77,17 @@ DEFAULT_REGION = "us-east-1" logger = logging.getLogger("moto") +class Output(object): + def __init__(self, connection=None): + self.connection = connection + self.description = None + self.key = None + self.value = None + + def __repr__(self): + return 'Output:"%s"="%s"' % (self.key, self.value) + + class LazyDict(dict): def __getitem__(self, key): val = dict.__getitem__(self, key) diff --git a/moto/cloudwatch/__init__.py b/moto/cloudwatch/__init__.py index 3f7b4ca53..8114b6838 100644 --- a/moto/cloudwatch/__init__.py +++ b/moto/cloudwatch/__init__.py @@ -1,5 +1,4 @@ from .models import cloudwatch_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator mock_cloudwatch = base_decorator(cloudwatch_backends) -mock_cloudwatch_deprecated = deprecated_base_decorator(cloudwatch_backends) diff --git a/moto/cognitoidentity/__init__.py b/moto/cognitoidentity/__init__.py index 7bb03429f..4ea132a84 100644 --- a/moto/cognitoidentity/__init__.py +++ b/moto/cognitoidentity/__init__.py @@ -1,5 +1,4 @@ from .models import cognitoidentity_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator mock_cognitoidentity = base_decorator(cognitoidentity_backends) -mock_cognitoidentity_deprecated = deprecated_base_decorator(cognitoidentity_backends) diff --git a/moto/cognitoidp/__init__.py b/moto/cognitoidp/__init__.py index 5fa400112..09d8043c1 100644 --- a/moto/cognitoidp/__init__.py +++ b/moto/cognitoidp/__init__.py @@ -1,5 +1,4 @@ from .models import cognitoidp_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator mock_cognitoidp = base_decorator(cognitoidp_backends) -mock_cognitoidp_deprecated = deprecated_base_decorator(cognitoidp_backends) diff --git a/moto/core/models.py b/moto/core/models.py index 0a95bf62e..b1d2cc1fb 100644 --- a/moto/core/models.py +++ b/moto/core/models.py @@ -16,7 +16,6 @@ from botocore.awsrequest import AWSResponse from moto import settings import responses -from moto.packages.httpretty import HTTPretty from unittest.mock import patch from .custom_responses_mock import ( get_response_mock, @@ -25,7 +24,6 @@ from .custom_responses_mock import ( reset_responses_mock, ) from .utils import ( - convert_httpretty_response, convert_regex_to_flask_path, convert_flask_to_responses_response, ) @@ -177,28 +175,6 @@ class BaseMockAWS: del os.environ[k] -class HttprettyMockAWS(BaseMockAWS): - def reset(self): - HTTPretty.reset() - - def enable_patching(self): - if not HTTPretty.is_enabled(): - HTTPretty.enable() - - for method in HTTPretty.METHODS: - for backend in self.backends_for_urls.values(): - for key, value in backend.urls.items(): - HTTPretty.register_uri( - method=method, - uri=re.compile(key), - body=convert_httpretty_response(value), - ) - - def disable_patching(self): - HTTPretty.disable() - HTTPretty.reset() - - RESPONSES_METHODS = [ responses.GET, responses.DELETE, @@ -681,12 +657,6 @@ class BaseBackend: else: return mocked_backend - def deprecated_decorator(self, func=None): - if func: - return HttprettyMockAWS({"global": self})(func) - else: - return HttprettyMockAWS({"global": self}) - # def list_config_service_resources(self, resource_ids, resource_name, limit, next_token): # """For AWS Config. This will list all of the resources of the given type and optional resource name and region""" # raise NotImplementedError() @@ -792,7 +762,7 @@ class base_decorator: self.backends = backends def __call__(self, func=None): - if self.mock_backend != HttprettyMockAWS and settings.TEST_SERVER_MODE: + if settings.TEST_SERVER_MODE: mocked_backend = ServerModeMockAWS(self.backends) else: mocked_backend = self.mock_backend(self.backends) @@ -803,10 +773,6 @@ class base_decorator: return mocked_backend -class deprecated_base_decorator(base_decorator): - mock_backend = HttprettyMockAWS - - class MotoAPIBackend(BaseBackend): def reset(self): import moto.backends as backends diff --git a/moto/core/utils.py b/moto/core/utils.py index 4a818ffef..97c4752f3 100644 --- a/moto/core/utils.py +++ b/moto/core/utils.py @@ -106,29 +106,7 @@ def convert_regex_to_flask_path(url_path): return url_path -class convert_httpretty_response(object): - def __init__(self, callback): - self.callback = callback - - @property - def __name__(self): - # For instance methods, use class and method names. Otherwise - # use module and method name - if inspect.ismethod(self.callback): - outer = self.callback.__self__.__class__.__name__ - else: - outer = self.callback.__module__ - return "{0}.{1}".format(outer, self.callback.__name__) - - def __call__(self, request, url, headers, **kwargs): - result = self.callback(request, url, headers) - status, headers, response = result - if "server" not in headers: - headers["server"] = "amazon.com" - return status, headers, response - - -class convert_flask_to_httpretty_response(object): +class convert_to_flask_response(object): def __init__(self, callback): self.callback = callback diff --git a/moto/datapipeline/__init__.py b/moto/datapipeline/__init__.py index 3a855295b..4287cee03 100644 --- a/moto/datapipeline/__init__.py +++ b/moto/datapipeline/__init__.py @@ -1,5 +1,4 @@ from .models import datapipeline_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator mock_datapipeline = base_decorator(datapipeline_backends) -mock_datapipeline_deprecated = deprecated_base_decorator(datapipeline_backends) diff --git a/moto/datasync/__init__.py b/moto/datasync/__init__.py index c4c821429..f9b9ff1aa 100644 --- a/moto/datasync/__init__.py +++ b/moto/datasync/__init__.py @@ -1,6 +1,5 @@ -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator from .models import datasync_backends datasync_backend = datasync_backends["us-east-1"] mock_datasync = base_decorator(datasync_backends) -mock_datasync_deprecated = deprecated_base_decorator(datasync_backends) diff --git a/moto/dynamodb/__init__.py b/moto/dynamodb/__init__.py index b58e70dbc..b040e49dd 100644 --- a/moto/dynamodb/__init__.py +++ b/moto/dynamodb/__init__.py @@ -2,4 +2,3 @@ from .models import dynamodb_backend dynamodb_backends = {"global": dynamodb_backend} mock_dynamodb = dynamodb_backend.decorator -mock_dynamodb_deprecated = dynamodb_backend.deprecated_decorator diff --git a/moto/dynamodb2/__init__.py b/moto/dynamodb2/__init__.py index 476f04fdf..87e738e06 100644 --- a/moto/dynamodb2/__init__.py +++ b/moto/dynamodb2/__init__.py @@ -1,6 +1,5 @@ from moto.dynamodb2.models import dynamodb_backends as dynamodb_backends2 -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator dynamodb_backend2 = dynamodb_backends2["us-east-1"] mock_dynamodb2 = base_decorator(dynamodb_backends2) -mock_dynamodb2_deprecated = deprecated_base_decorator(dynamodb_backends2) diff --git a/moto/dynamodb2/parsing/tokens.py b/moto/dynamodb2/parsing/tokens.py index 34c3151ef..33a25d5c0 100644 --- a/moto/dynamodb2/parsing/tokens.py +++ b/moto/dynamodb2/parsing/tokens.py @@ -1,5 +1,4 @@ import re -import sys from moto.dynamodb2.exceptions import ( InvalidTokenException, @@ -148,16 +147,9 @@ class ExpressionTokenizer(object): self.token_list = [] self.staged_characters = "" - @classmethod - def is_py2(cls): - return sys.version_info[0] == 2 - @classmethod def make_list(cls, input_expression_str): - if cls.is_py2(): - pass - else: - assert isinstance(input_expression_str, str) + assert isinstance(input_expression_str, str) return ExpressionTokenizer(input_expression_str)._make_list() diff --git a/moto/ec2/__init__.py b/moto/ec2/__init__.py index 1a59762e7..1025a0d60 100644 --- a/moto/ec2/__init__.py +++ b/moto/ec2/__init__.py @@ -1,6 +1,5 @@ from .models import ec2_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator ec2_backend = ec2_backends["us-east-1"] mock_ec2 = base_decorator(ec2_backends) -mock_ec2_deprecated = deprecated_base_decorator(ec2_backends) diff --git a/moto/ecr/__init__.py b/moto/ecr/__init__.py index a3a0bc6bd..c85a33085 100644 --- a/moto/ecr/__init__.py +++ b/moto/ecr/__init__.py @@ -1,6 +1,5 @@ from .models import ecr_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator ecr_backend = ecr_backends["us-east-1"] mock_ecr = base_decorator(ecr_backends) -mock_ecr_deprecated = deprecated_base_decorator(ecr_backends) diff --git a/moto/ecs/__init__.py b/moto/ecs/__init__.py index 2258fe378..cf9b335db 100644 --- a/moto/ecs/__init__.py +++ b/moto/ecs/__init__.py @@ -1,6 +1,5 @@ from .models import ecs_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator ecs_backend = ecs_backends["us-east-1"] mock_ecs = base_decorator(ecs_backends) -mock_ecs_deprecated = deprecated_base_decorator(ecs_backends) diff --git a/moto/elb/__init__.py b/moto/elb/__init__.py index b035a5e1d..5fc8a33e8 100644 --- a/moto/elb/__init__.py +++ b/moto/elb/__init__.py @@ -1,6 +1,5 @@ from .models import elb_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator elb_backend = elb_backends["us-east-1"] mock_elb = base_decorator(elb_backends) -mock_elb_deprecated = deprecated_base_decorator(elb_backends) diff --git a/moto/elb/models.py b/moto/elb/models.py index 459353aa6..99fbd11e0 100644 --- a/moto/elb/models.py +++ b/moto/elb/models.py @@ -2,13 +2,6 @@ import datetime import pytz -from moto.packages.boto.ec2.elb.attributes import ( - LbAttributes, - ConnectionSettingAttribute, - ConnectionDrainingAttribute, - AccessLogAttribute, - CrossZoneLoadBalancingAttribute, -) from moto.packages.boto.ec2.elb.policies import Policies, OtherPolicy from collections import OrderedDict from moto.core import BaseBackend, BaseModel, CloudFormationModel @@ -248,23 +241,11 @@ class FakeLoadBalancer(CloudFormationModel): @classmethod def get_default_attributes(cls): - attributes = LbAttributes() - - cross_zone_load_balancing = CrossZoneLoadBalancingAttribute() - cross_zone_load_balancing.enabled = False - attributes.cross_zone_load_balancing = cross_zone_load_balancing - - connection_draining = ConnectionDrainingAttribute() - connection_draining.enabled = False - attributes.connection_draining = connection_draining - - access_log = AccessLogAttribute() - access_log.enabled = False - attributes.access_log = access_log - - connection_settings = ConnectionSettingAttribute() - connection_settings.idle_timeout = 60 - attributes.connecting_settings = connection_settings + attributes = dict() + attributes["cross_zone_load_balancing"] = {"enabled": False} + attributes["connection_draining"] = {"enabled": False} + attributes["access_log"] = {"enabled": False} + attributes["connection_settings"] = {"idle_timeout": 60} return attributes @@ -468,25 +449,27 @@ class ELBBackend(BaseBackend): return load_balancer - def set_cross_zone_load_balancing_attribute(self, load_balancer_name, attribute): + def modify_load_balancer_attributes( + self, + load_balancer_name, + cross_zone=None, + connection_settings=None, + connection_draining=None, + access_log=None, + ): load_balancer = self.get_load_balancer(load_balancer_name) - load_balancer.attributes.cross_zone_load_balancing = attribute - return load_balancer - - def set_access_log_attribute(self, load_balancer_name, attribute): - load_balancer = self.get_load_balancer(load_balancer_name) - load_balancer.attributes.access_log = attribute - return load_balancer - - def set_connection_draining_attribute(self, load_balancer_name, attribute): - load_balancer = self.get_load_balancer(load_balancer_name) - load_balancer.attributes.connection_draining = attribute - return load_balancer - - def set_connection_settings_attribute(self, load_balancer_name, attribute): - load_balancer = self.get_load_balancer(load_balancer_name) - load_balancer.attributes.connecting_settings = attribute - return load_balancer + if cross_zone: + load_balancer.attributes["cross_zone_load_balancing"] = cross_zone + if connection_settings: + load_balancer.attributes["connection_settings"] = connection_settings + if connection_draining: + load_balancer.attributes["connection_draining"] = connection_draining + if "timeout" not in connection_draining: + load_balancer.attributes["connection_draining"][ + "timeout" + ] = 300 # default + if access_log: + load_balancer.attributes["access_log"] = access_log def create_lb_other_policy(self, load_balancer_name, other_policy): load_balancer = self.get_load_balancer(load_balancer_name) diff --git a/moto/elb/responses.py b/moto/elb/responses.py index 03b046354..ddd6e2689 100644 --- a/moto/elb/responses.py +++ b/moto/elb/responses.py @@ -1,9 +1,3 @@ -from moto.packages.boto.ec2.elb.attributes import ( - ConnectionSettingAttribute, - ConnectionDrainingAttribute, - AccessLogAttribute, - CrossZoneLoadBalancingAttribute, -) from moto.packages.boto.ec2.elb.policies import AppCookieStickinessPolicy, OtherPolicy from moto.core.responses import BaseResponse @@ -153,40 +147,30 @@ class ELBResponse(BaseResponse): "LoadBalancerAttributes.CrossZoneLoadBalancing." ) if cross_zone: - attribute = CrossZoneLoadBalancingAttribute() - attribute.enabled = cross_zone["enabled"] == "true" - self.elb_backend.set_cross_zone_load_balancing_attribute( - load_balancer_name, attribute + self.elb_backend.modify_load_balancer_attributes( + load_balancer_name, cross_zone=cross_zone ) access_log = self._get_dict_param("LoadBalancerAttributes.AccessLog.") if access_log: - attribute = AccessLogAttribute() - attribute.enabled = access_log["enabled"] == "true" - attribute.s3_bucket_name = access_log["s3_bucket_name"] - attribute.s3_bucket_prefix = access_log["s3_bucket_prefix"] - attribute.emit_interval = access_log["emit_interval"] - self.elb_backend.set_access_log_attribute(load_balancer_name, attribute) + self.elb_backend.modify_load_balancer_attributes( + load_balancer_name, access_log=access_log + ) connection_draining = self._get_dict_param( "LoadBalancerAttributes.ConnectionDraining." ) if connection_draining: - attribute = ConnectionDrainingAttribute() - attribute.enabled = connection_draining["enabled"] == "true" - attribute.timeout = connection_draining.get("timeout", 300) - self.elb_backend.set_connection_draining_attribute( - load_balancer_name, attribute + self.elb_backend.modify_load_balancer_attributes( + load_balancer_name, connection_draining=connection_draining ) connection_settings = self._get_dict_param( "LoadBalancerAttributes.ConnectionSettings." ) if connection_settings: - attribute = ConnectionSettingAttribute() - attribute.idle_timeout = connection_settings["idle_timeout"] - self.elb_backend.set_connection_settings_attribute( - load_balancer_name, attribute + self.elb_backend.modify_load_balancer_attributes( + load_balancer_name, connection_settings=connection_settings ) template = self.response_template(MODIFY_ATTRIBUTES_TEMPLATE) @@ -628,23 +612,23 @@ DESCRIBE_ATTRIBUTES_TEMPLATE = """ - {{ attributes.access_log.enabled }} - {% if attributes.access_log.enabled %} - {{ attributes.access_log.s3_bucket_name }} - {{ attributes.access_log.s3_bucket_prefix }} - {{ attributes.access_log.emit_interval }} + {{ attributes["access_log"]["enabled"] }} + {% if attributes["access_log"]["enabled"] == 'true' %} + {{ attributes["access_log"]["s3_bucket_name"] }} + {{ attributes["access_log"]["s3_bucket_prefix"] }} + {{ attributes["access_log"]["emit_interval"] }} {% endif %} - {{ attributes.connecting_settings.idle_timeout }} + {{ attributes["connection_settings"]["idle_timeout"] }} {{ attributes.cross_zone_load_balancing.enabled }} - {% if attributes.connection_draining.enabled %} + {% if attributes["connection_draining"]["enabled"] == 'true' %} true - {{ attributes.connection_draining.timeout }} + {{ attributes["connection_draining"]["timeout"] }} {% else %} false {% endif %} @@ -662,23 +646,23 @@ MODIFY_ATTRIBUTES_TEMPLATE = """ + {% if attributes["access_log"]["enabled"] == 'true' %} + {{ attributes["access_log"]["s3_bucket_name"] }} + {{ attributes["access_log"]["s3_bucket_prefix"] }} + {{ attributes["access_log"]["emit_interval"] }} {% endif %} - {{ attributes.connecting_settings.idle_timeout }} + {{ attributes["connection_settings"]["idle_timeout"] }} {{ attributes.cross_zone_load_balancing.enabled }} - {% if attributes.connection_draining.enabled %} + {% if attributes["connection_draining"]["enabled"] == 'true' %} true - {{ attributes.connection_draining.timeout }} + {{ attributes["connection_draining"]["timeout"] }} {% else %} false {% endif %} diff --git a/moto/emr/__init__.py b/moto/emr/__init__.py index d8116fc14..dc95b003a 100644 --- a/moto/emr/__init__.py +++ b/moto/emr/__init__.py @@ -1,6 +1,5 @@ from .models import emr_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator emr_backend = emr_backends["us-east-1"] mock_emr = base_decorator(emr_backends) -mock_emr_deprecated = deprecated_base_decorator(emr_backends) diff --git a/moto/glacier/__init__.py b/moto/glacier/__init__.py index 15f9e7448..4dec88933 100644 --- a/moto/glacier/__init__.py +++ b/moto/glacier/__init__.py @@ -1,6 +1,5 @@ from .models import glacier_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator glacier_backend = glacier_backends["us-east-1"] mock_glacier = base_decorator(glacier_backends) -mock_glacier_deprecated = deprecated_base_decorator(glacier_backends) diff --git a/moto/iam/__init__.py b/moto/iam/__init__.py index 86e570143..81b188d96 100644 --- a/moto/iam/__init__.py +++ b/moto/iam/__init__.py @@ -2,4 +2,3 @@ from .models import iam_backend iam_backends = {"global": iam_backend} mock_iam = iam_backend.decorator -mock_iam_deprecated = iam_backend.deprecated_decorator diff --git a/moto/kinesis/__init__.py b/moto/kinesis/__init__.py index 4f65a3d14..a706a0101 100644 --- a/moto/kinesis/__init__.py +++ b/moto/kinesis/__init__.py @@ -1,6 +1,5 @@ from .models import kinesis_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator kinesis_backend = kinesis_backends["us-east-1"] mock_kinesis = base_decorator(kinesis_backends) -mock_kinesis_deprecated = deprecated_base_decorator(kinesis_backends) diff --git a/moto/kms/__init__.py b/moto/kms/__init__.py index 2bb6f8c11..cb055437c 100644 --- a/moto/kms/__init__.py +++ b/moto/kms/__init__.py @@ -1,6 +1,5 @@ from .models import kms_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator kms_backend = kms_backends["us-east-1"] mock_kms = base_decorator(kms_backends) -mock_kms_deprecated = deprecated_base_decorator(kms_backends) diff --git a/moto/logs/__init__.py b/moto/logs/__init__.py index f325243fc..bbe77cd9d 100644 --- a/moto/logs/__init__.py +++ b/moto/logs/__init__.py @@ -1,5 +1,4 @@ from .models import logs_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator mock_logs = base_decorator(logs_backends) -mock_logs_deprecated = deprecated_base_decorator(logs_backends) diff --git a/moto/managedblockchain/__init__.py b/moto/managedblockchain/__init__.py index d7fb14f6e..3199f4fb7 100644 --- a/moto/managedblockchain/__init__.py +++ b/moto/managedblockchain/__init__.py @@ -1,8 +1,5 @@ from .models import managedblockchain_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator managedblockchain_backend = managedblockchain_backends["us-east-1"] mock_managedblockchain = base_decorator(managedblockchain_backends) -mock_managedblockchain_deprecated = deprecated_base_decorator( - managedblockchain_backends -) diff --git a/moto/opsworks/__init__.py b/moto/opsworks/__init__.py index ce8c4a223..ef945994d 100644 --- a/moto/opsworks/__init__.py +++ b/moto/opsworks/__init__.py @@ -1,6 +1,5 @@ from .models import opsworks_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator opsworks_backend = opsworks_backends["us-east-1"] mock_opsworks = base_decorator(opsworks_backends) -mock_opsworks_deprecated = deprecated_base_decorator(opsworks_backends) diff --git a/moto/packages/boto/cloudformation/__init__.py b/moto/packages/boto/cloudformation/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/moto/packages/boto/cloudformation/stack.py b/moto/packages/boto/cloudformation/stack.py deleted file mode 100644 index 26c4bfdf7..000000000 --- a/moto/packages/boto/cloudformation/stack.py +++ /dev/null @@ -1,9 +0,0 @@ -class Output(object): - def __init__(self, connection=None): - self.connection = connection - self.description = None - self.key = None - self.value = None - - def __repr__(self): - return 'Output:"%s"="%s"' % (self.key, self.value) diff --git a/moto/packages/boto/ec2/elb/attributes.py b/moto/packages/boto/ec2/elb/attributes.py deleted file mode 100644 index fbb387ec6..000000000 --- a/moto/packages/boto/ec2/elb/attributes.py +++ /dev/null @@ -1,100 +0,0 @@ -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the -# "Software"), to deal in the Software without restriction, including -# without limitation the rights to use, copy, modify, merge, publish, dis- -# tribute, sublicense, and/or sell copies of the Software, and to permit -# persons to whom the Software is furnished to do so, subject to the fol- -# lowing conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- -# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT -# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -# IN THE SOFTWARE. -# -# Created by Chris Huegle for TellApart, Inc. - - -class ConnectionSettingAttribute(object): - """ - Represents the ConnectionSetting segment of ELB Attributes. - """ - - def __init__(self, connection=None): - self.idle_timeout = None - - def __repr__(self): - return "ConnectionSettingAttribute(%s)" % (self.idle_timeout) - - -class CrossZoneLoadBalancingAttribute(object): - """ - Represents the CrossZoneLoadBalancing segement of ELB Attributes. - """ - - def __init__(self, connection=None): - self.enabled = None - - def __repr__(self): - return "CrossZoneLoadBalancingAttribute(%s)" % (self.enabled) - - -class AccessLogAttribute(object): - """ - Represents the AccessLog segment of ELB attributes. - """ - - def __init__(self, connection=None): - self.enabled = None - self.s3_bucket_name = None - self.s3_bucket_prefix = None - self.emit_interval = None - - def __repr__(self): - return "AccessLog(%s, %s, %s, %s)" % ( - self.enabled, - self.s3_bucket_name, - self.s3_bucket_prefix, - self.emit_interval, - ) - - -class ConnectionDrainingAttribute(object): - """ - Represents the ConnectionDraining segment of ELB attributes. - """ - - def __init__(self, connection=None): - self.enabled = None - self.timeout = None - - def __repr__(self): - return "ConnectionDraining(%s, %s)" % (self.enabled, self.timeout) - - -class LbAttributes(object): - """ - Represents the Attributes of an Elastic Load Balancer. - """ - - def __init__(self, connection=None): - self.connection = connection - self.cross_zone_load_balancing = CrossZoneLoadBalancingAttribute( - self.connection - ) - self.access_log = AccessLogAttribute(self.connection) - self.connection_draining = ConnectionDrainingAttribute(self.connection) - self.connecting_settings = ConnectionSettingAttribute(self.connection) - - def __repr__(self): - return "LbAttributes(%s, %s, %s, %s)" % ( - repr(self.cross_zone_load_balancing), - repr(self.access_log), - repr(self.connection_draining), - repr(self.connecting_settings), - ) diff --git a/moto/packages/httpretty/__init__.py b/moto/packages/httpretty/__init__.py deleted file mode 100644 index c6a78526f..000000000 --- a/moto/packages/httpretty/__init__.py +++ /dev/null @@ -1,61 +0,0 @@ -# #!/usr/bin/env python -# -*- coding: utf-8 -*- -# -# Copyright (C) <2011-2013> Gabriel Falcão -# -# Permission is hereby granted, free of charge, to any person -# obtaining a copy of this software and associated documentation -# files (the "Software"), to deal in the Software without -# restriction, including without limitation the rights to use, -# copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following -# conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -# OTHER DEALINGS IN THE SOFTWARE. -from __future__ import unicode_literals - -__version__ = version = "0.8.10" - -from .core import httpretty, httprettified, EmptyRequestHeaders -from .errors import HTTPrettyError, UnmockedError -from .core import URIInfo - -HTTPretty = httpretty -activate = httprettified - -enable = httpretty.enable -register_uri = httpretty.register_uri -disable = httpretty.disable -is_enabled = httpretty.is_enabled -reset = httpretty.reset -Response = httpretty.Response - -GET = httpretty.GET -PUT = httpretty.PUT -POST = httpretty.POST -DELETE = httpretty.DELETE -HEAD = httpretty.HEAD -PATCH = httpretty.PATCH -OPTIONS = httpretty.OPTIONS -CONNECT = httpretty.CONNECT - - -def last_request(): - """returns the last request""" - return httpretty.last_request - - -def has_request(): - """returns a boolean indicating whether any request has been made""" - return not isinstance(httpretty.last_request.headers, EmptyRequestHeaders) diff --git a/moto/packages/httpretty/compat.py b/moto/packages/httpretty/compat.py deleted file mode 100644 index c452dec0e..000000000 --- a/moto/packages/httpretty/compat.py +++ /dev/null @@ -1,106 +0,0 @@ -# #!/usr/bin/env python -# -*- coding: utf-8 -*- -# -# -# Copyright (C) <2011-2013> Gabriel Falcão -# -# Permission is hereby granted, free of charge, to any person -# obtaining a copy of this software and associated documentation -# files (the "Software"), to deal in the Software without -# restriction, including without limitation the rights to use, -# copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following -# conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -# OTHER DEALINGS IN THE SOFTWARE. -from __future__ import unicode_literals - -import sys -import types - -PY3 = sys.version_info[0] == 3 -if PY3: # pragma: no cover - text_type = str - byte_type = bytes - import io - - StringIO = io.BytesIO - basestring = (str, bytes) - - class BaseClass(object): - def __repr__(self): - return self.__str__() - - -else: # pragma: no cover - text_type = unicode - byte_type = str - import StringIO - - StringIO = StringIO.StringIO - basestring = basestring - - -class BaseClass(object): - def __repr__(self): - ret = self.__str__() - if PY3: # pragma: no cover - return ret - else: - return ret.encode("utf-8") - - -try: # pragma: no cover - from urllib.parse import urlsplit, urlunsplit, parse_qs, quote, quote_plus, unquote - - unquote_utf8 = unquote -except ImportError: # pragma: no cover - from urlparse import urlsplit, urlunsplit, parse_qs, unquote - from urllib import quote, quote_plus - - def unquote_utf8(qs): - if isinstance(qs, text_type): - qs = qs.encode("utf-8") - s = unquote(qs) - if isinstance(s, byte_type): - return s.decode("utf-8") - else: - return s - - -try: # pragma: no cover - from http.server import BaseHTTPRequestHandler -except ImportError: # pragma: no cover - from BaseHTTPServer import BaseHTTPRequestHandler - - -ClassTypes = (type,) -if not PY3: # pragma: no cover - ClassTypes = (type, types.ClassType) - - -__all__ = [ - "PY3", - "StringIO", - "text_type", - "byte_type", - "BaseClass", - "BaseHTTPRequestHandler", - "quote", - "quote_plus", - "urlunsplit", - "urlsplit", - "parse_qs", - "ClassTypes", -] diff --git a/moto/packages/httpretty/core.py b/moto/packages/httpretty/core.py deleted file mode 100644 index 22fe21c81..000000000 --- a/moto/packages/httpretty/core.py +++ /dev/null @@ -1,1147 +0,0 @@ -# #!/usr/bin/env python -# -*- coding: utf-8 -*- -# -# Copyright (C) <2011-2013> Gabriel Falcão -# -# Permission is hereby granted, free of charge, to any person -# obtaining a copy of this software and associated documentation -# files (the "Software"), to deal in the Software without -# restriction, including without limitation the rights to use, -# copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following -# conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -# OTHER DEALINGS IN THE SOFTWARE. -from __future__ import unicode_literals - -import re -import codecs -import inspect -import socket -import functools -import itertools -import warnings -import logging -import traceback -import json -import contextlib - - -from .compat import ( - PY3, - StringIO, - text_type, - BaseClass, - BaseHTTPRequestHandler, - quote, - quote_plus, - urlunsplit, - urlsplit, - parse_qs, - unquote, - unquote_utf8, - ClassTypes, - basestring, -) -from .http import STATUSES, HttpBaseClass, parse_requestline, last_requestline - -from .utils import utf8, decode_utf8 - -from .errors import HTTPrettyError, UnmockedError - -from datetime import datetime -from datetime import timedelta -from errno import EAGAIN - -# Some versions of python internally shadowed the -# SocketType variable incorrectly https://bugs.python.org/issue20386 -BAD_SOCKET_SHADOW = socket.socket != socket.SocketType - -old_socket = socket.socket -old_create_connection = socket.create_connection -old_gethostbyname = socket.gethostbyname -old_gethostname = socket.gethostname -old_getaddrinfo = socket.getaddrinfo -old_socksocket = None -old_ssl_wrap_socket = None -old_sslwrap_simple = None -old_sslsocket = None -old_sslcontext_wrap_socket = None - -if PY3: # pragma: no cover - basestring = (bytes, str) -try: # pragma: no cover - import socks - - old_socksocket = socks.socksocket -except ImportError: - socks = None - -try: # pragma: no cover - import ssl - - old_ssl_wrap_socket = ssl.wrap_socket - if not PY3: - old_sslwrap_simple = ssl.sslwrap_simple - old_sslsocket = ssl.SSLSocket - try: - old_sslcontext_wrap_socket = ssl.SSLContext.wrap_socket - except AttributeError: - pass -except ImportError: # pragma: no cover - ssl = None - -try: # pragma: no cover - from requests.packages.urllib3.contrib.pyopenssl import ( - inject_into_urllib3, - extract_from_urllib3, - ) - - pyopenssl_override = True -except: - pyopenssl_override = False - - -DEFAULT_HTTP_PORTS = frozenset([80]) -POTENTIAL_HTTP_PORTS = set(DEFAULT_HTTP_PORTS) -DEFAULT_HTTPS_PORTS = frozenset([443]) -POTENTIAL_HTTPS_PORTS = set(DEFAULT_HTTPS_PORTS) - - -class HTTPrettyRequest(BaseHTTPRequestHandler, BaseClass): - """Represents a HTTP request. It takes a valid multi-line, `\r\n` - separated string with HTTP headers and parse them out using the - internal `parse_request` method. - - It also replaces the `rfile` and `wfile` attributes with StringIO - instances so that we guarantee that it won't make any I/O, neighter - for writing nor reading. - - It has some convenience attributes: - - `headers` -> a mimetype object that can be cast into a dictionary, - contains all the request headers - - `method` -> the HTTP method used in this request - - `querystring` -> a dictionary containing lists with the - attributes. Please notice that if you need a single value from a - query string you will need to get it manually like: - - ```python - >>> request.querystring - {'name': ['Gabriel Falcao']} - >>> print request.querystring['name'][0] - ``` - - `parsed_body` -> a dictionary containing parsed request body or - None if HTTPrettyRequest doesn't know how to parse it. It - currently supports parsing body data that was sent under the - `content-type` headers values: 'application/json' or - 'application/x-www-form-urlencoded' - """ - - def __init__(self, headers, body=""): - # first of all, lets make sure that if headers or body are - # unicode strings, it must be converted into a utf-8 encoded - # byte string - self.raw_headers = utf8(headers.strip()) - self.body = utf8(body) - - # Now let's concatenate the headers with the body, and create - # `rfile` based on it - self.rfile = StringIO(b"\r\n\r\n".join([self.raw_headers, self.body])) - self.wfile = StringIO() # Creating `wfile` as an empty - # StringIO, just to avoid any real - # I/O calls - - # parsing the request line preemptively - self.raw_requestline = self.rfile.readline() - - # initiating the error attributes with None - self.error_code = None - self.error_message = None - - # Parse the request based on the attributes above - if not self.parse_request(): - return - - # making the HTTP method string available as the command - self.method = self.command - - # Now 2 convenient attributes for the HTTPretty API: - - # `querystring` holds a dictionary with the parsed query string - try: - self.path = self.path.encode("iso-8859-1") - except UnicodeDecodeError: - pass - - self.path = decode_utf8(self.path) - - qstring = self.path.split("?", 1)[-1] - self.querystring = self.parse_querystring(qstring) - - # And the body will be attempted to be parsed as - # `application/json` or `application/x-www-form-urlencoded` - self.parsed_body = self.parse_request_body(self.body) - - def __str__(self): - return ''.format( - self.headers.get("content-type", ""), len(self.headers), len(self.body) - ) - - def parse_querystring(self, qs): - expanded = unquote_utf8(qs) - parsed = parse_qs(expanded) - result = {} - for k in parsed: - result[k] = list(map(decode_utf8, parsed[k])) - - return result - - def parse_request_body(self, body): - """Attempt to parse the post based on the content-type passed. Return the regular body if not""" - - PARSING_FUNCTIONS = { - "application/json": json.loads, - "text/json": json.loads, - "application/x-www-form-urlencoded": self.parse_querystring, - } - FALLBACK_FUNCTION = lambda x: x - - content_type = self.headers.get("content-type", "") - - do_parse = PARSING_FUNCTIONS.get(content_type, FALLBACK_FUNCTION) - try: - body = decode_utf8(body) - return do_parse(body) - except: - return body - - -class EmptyRequestHeaders(dict): - pass - - -class HTTPrettyRequestEmpty(object): - body = "" - headers = EmptyRequestHeaders() - - -class FakeSockFile(StringIO): - def close(self): - self.socket.close() - StringIO.close(self) - - -class FakeSSLSocket(object): - def __init__(self, sock, *args, **kw): - self._httpretty_sock = sock - - def __getattr__(self, attr): - return getattr(self._httpretty_sock, attr) - - -class fakesock(object): - class socket(object): - _entry = None - debuglevel = 0 - _sent_data = [] - - def __init__( - self, - family=socket.AF_INET, - type=socket.SOCK_STREAM, - proto=0, - fileno=None, - _sock=None, - ): - """ - Matches both the Python 2 API: - def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, _sock=None): - https://github.com/python/cpython/blob/2.7/Lib/socket.py - - and the Python 3 API: - def __init__(self, family=-1, type=-1, proto=-1, fileno=None): - https://github.com/python/cpython/blob/3.5/Lib/socket.py - """ - if httpretty.allow_net_connect: - if PY3: - self.truesock = old_socket(family, type, proto, fileno) - else: - # If Python 2, if parameters are passed as arguments, instead of kwargs, - # the 4th argument `_sock` will be interpreted as the `fileno`. - # Check if _sock is none, and if so, pass fileno. - self.truesock = old_socket(family, type, proto, fileno or _sock) - else: - self.truesock = None - self._closed = True - self.fd = FakeSockFile() - self.fd.socket = self - self.timeout = socket._GLOBAL_DEFAULT_TIMEOUT - self._sock = self - self.is_http = False - self._bufsize = 1024 - - def getpeercert(self, *a, **kw): - now = datetime.now() - shift = now + timedelta(days=30 * 12) - return { - "notAfter": shift.strftime("%b %d %H:%M:%S GMT"), - "subjectAltName": ( - ("DNS", "*.%s" % self._host), - ("DNS", self._host), - ("DNS", "*"), - ), - "subject": ( - (("organizationName", "*.%s" % self._host),), - (("organizationalUnitName", "Domain Control Validated"),), - (("commonName", "*.%s" % self._host),), - ), - } - - def ssl(self, sock, *args, **kw): - return sock - - def setsockopt(self, level, optname, value): - if self.truesock: - self.truesock.setsockopt(level, optname, value) - - def connect(self, address): - self._closed = False - - try: - self._address = (self._host, self._port) = address - except ValueError: - # We get here when the address is just a string pointing to a - # unix socket path/file - # - # See issue #206 - self.is_http = False - else: - self.is_http = ( - self._port in POTENTIAL_HTTP_PORTS | POTENTIAL_HTTPS_PORTS - ) - - if not self.is_http: - if self.truesock: - self.truesock.connect(self._address) - else: - raise UnmockedError() - - def close(self): - if not (self.is_http and self._closed): - if self.truesock: - self.truesock.close() - self._closed = True - - def makefile(self, mode="r", bufsize=-1): - """Returns this fake socket's own StringIO buffer. - - If there is an entry associated with the socket, the file - descriptor gets filled in with the entry data before being - returned. - """ - self._mode = mode - self._bufsize = bufsize - - if self._entry: - self._entry.fill_filekind(self.fd) - - return self.fd - - def real_sendall(self, data, *args, **kw): - """Sends data to the remote server. This method is called - when HTTPretty identifies that someone is trying to send - non-http data. - - The received bytes are written in this socket's StringIO - buffer so that HTTPretty can return it accordingly when - necessary. - """ - - if not self.truesock: - raise UnmockedError() - - if not self.is_http: - return self.truesock.sendall(data, *args, **kw) - - self.truesock.connect(self._address) - - self.truesock.setblocking(1) - self.truesock.sendall(data, *args, **kw) - - should_continue = True - while should_continue: - try: - received = self.truesock.recv(self._bufsize) - self.fd.write(received) - should_continue = len(received) == self._bufsize - - except socket.error as e: - if e.errno == EAGAIN: - continue - break - - self.fd.seek(0) - - def sendall(self, data, *args, **kw): - self._sent_data.append(data) - self.fd = FakeSockFile() - self.fd.socket = self - try: - requestline, _ = data.split(b"\r\n", 1) - method, path, version = parse_requestline(decode_utf8(requestline)) - is_parsing_headers = True - except ValueError: - is_parsing_headers = False - - if not self._entry: - # If the previous request wasn't mocked, don't mock the - # subsequent sending of data - return self.real_sendall(data, *args, **kw) - - self.fd.seek(0) - - if not is_parsing_headers: - if len(self._sent_data) > 1: - headers = utf8(last_requestline(self._sent_data)) - meta = self._entry.request.headers - body = utf8(self._sent_data[-1]) - if meta.get("transfer-encoding", "") == "chunked": - if ( - not body.isdigit() - and body != b"\r\n" - and body != b"0\r\n\r\n" - ): - self._entry.request.body += body - else: - self._entry.request.body += body - - httpretty.historify_request(headers, body, False) - return - - # path might come with - s = urlsplit(path) - POTENTIAL_HTTP_PORTS.add(int(s.port or 80)) - headers, body = list(map(utf8, data.split(b"\r\n\r\n", 1))) - - request = httpretty.historify_request(headers, body) - - info = URIInfo( - hostname=self._host, - port=self._port, - path=s.path, - query=s.query, - last_request=request, - ) - - matcher, entries = httpretty.match_uriinfo(info) - - if not entries: - self._entry = None - self.real_sendall(data) - return - - self._entry = matcher.get_next_entry(method, info, request) - - def debug(self, truesock_func, *a, **kw): - if self.is_http: - frame = inspect.stack()[0][0] - lines = list(map(utf8, traceback.format_stack(frame))) - - message = [ - "HTTPretty intercepted and unexpected socket method call.", - ( - "Please open an issue at " - "'https://github.com/gabrielfalcao/HTTPretty/issues'" - ), - "And paste the following traceback:\n", - "".join(decode_utf8(lines)), - ] - raise RuntimeError("\n".join(message)) - if not self.truesock: - raise UnmockedError() - return getattr(self.truesock, truesock_func)(*a, **kw) - - def settimeout(self, new_timeout): - self.timeout = new_timeout - - def send(self, *args, **kwargs): - return self.debug("send", *args, **kwargs) - - def sendto(self, *args, **kwargs): - return self.debug("sendto", *args, **kwargs) - - def recvfrom_into(self, *args, **kwargs): - return self.debug("recvfrom_into", *args, **kwargs) - - def recv_into(self, *args, **kwargs): - return self.debug("recv_into", *args, **kwargs) - - def recvfrom(self, *args, **kwargs): - return self.debug("recvfrom", *args, **kwargs) - - def recv(self, *args, **kwargs): - return self.debug("recv", *args, **kwargs) - - def __getattr__(self, name): - if not self.truesock: - raise UnmockedError() - return getattr(self.truesock, name) - - -def fake_wrap_socket(s, *args, **kw): - return s - - -def create_fake_connection( - address, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None -): - s = fakesock.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) - if timeout is not socket._GLOBAL_DEFAULT_TIMEOUT: - s.settimeout(timeout) - if source_address: - s.bind(source_address) - s.connect(address) - return s - - -def fake_gethostbyname(host): - return "127.0.0.1" - - -def fake_gethostname(): - return "localhost" - - -def fake_getaddrinfo(host, port, family=None, socktype=None, proto=None, flags=None): - return [(2, 1, 6, "", (host, port))] - - -class Entry(BaseClass): - def __init__( - self, - method, - uri, - body, - adding_headers=None, - forcing_headers=None, - status=200, - streaming=False, - **headers - ): - - self.method = method - self.uri = uri - self.info = None - self.request = None - - self.body_is_callable = False - if hasattr(body, "__call__"): - self.callable_body = body - self.body = None - self.body_is_callable = True - elif isinstance(body, text_type): - self.body = utf8(body) - else: - self.body = body - - self.streaming = streaming - if not streaming and not self.body_is_callable: - self.body_length = len(self.body or "") - else: - self.body_length = 0 - - self.adding_headers = adding_headers or {} - self.forcing_headers = forcing_headers or {} - self.status = int(status) - - for k, v in headers.items(): - name = "-".join(k.split("_")).title() - self.adding_headers[name] = v - - self.validate() - - def validate(self): - content_length_keys = "Content-Length", "content-length" - for key in content_length_keys: - got = self.adding_headers.get(key, self.forcing_headers.get(key, None)) - - if got is None: - continue - - try: - igot = int(got) - except ValueError: - warnings.warn( - "HTTPretty got to register the Content-Length header " - 'with "%r" which is not a number' % got - ) - - if igot > self.body_length: - raise HTTPrettyError( - "HTTPretty got inconsistent parameters. The header " - 'Content-Length you registered expects size "%d" but ' - "the body you registered for that has actually length " - '"%d".' % (igot, self.body_length) - ) - - def __str__(self): - return r"" % (self.method, self.uri, self.status) - - def normalize_headers(self, headers): - new = {} - for k in headers: - new_k = "-".join([s.lower() for s in k.split("-")]) - new[new_k] = headers[k] - - return new - - def fill_filekind(self, fk): - now = datetime.utcnow() - - headers = { - "status": self.status, - "date": now.strftime("%a, %d %b %Y %H:%M:%S GMT"), - "server": "Python/HTTPretty", - "connection": "close", - } - - if self.forcing_headers: - headers = self.forcing_headers - - if self.adding_headers: - headers.update(self.normalize_headers(self.adding_headers)) - - headers = self.normalize_headers(headers) - status = headers.get("status", self.status) - if self.body_is_callable: - status, headers, self.body = self.callable_body( - self.request, self.info.full_url(), headers - ) - headers = self.normalize_headers(headers) - if self.request.method != "HEAD": - headers.update({"content-length": len(self.body)}) - - string_list = ["HTTP/1.1 %d %s" % (status, STATUSES[status])] - - if "date" in headers: - string_list.append("date: %s" % headers.pop("date")) - - if not self.forcing_headers: - content_type = headers.pop("content-type", "text/plain; charset=utf-8") - - content_length = headers.pop("content-length", self.body_length) - - string_list.append("content-type: %s" % content_type) - if not self.streaming: - string_list.append("content-length: %s" % content_length) - - string_list.append("server: %s" % headers.pop("server")) - - for k, v in headers.items(): - string_list.append("{0}: {1}".format(k, v)) - - for item in string_list: - fk.write(utf8(item) + b"\n") - - fk.write(b"\r\n") - - if self.streaming: - self.body, body = itertools.tee(self.body) - for chunk in body: - fk.write(utf8(chunk)) - else: - fk.write(utf8(self.body)) - - fk.seek(0) - - -def url_fix(s, charset="utf-8"): - scheme, netloc, path, querystring, fragment = urlsplit(s) - path = quote(path, b"/%") - querystring = quote_plus(querystring, b":&=") - return urlunsplit((scheme, netloc, path, querystring, fragment)) - - -class URIInfo(BaseClass): - def __init__( - self, - username="", - password="", - hostname="", - port=80, - path="/", - query="", - fragment="", - scheme="", - last_request=None, - ): - - self.username = username or "" - self.password = password or "" - self.hostname = hostname or "" - - if port: - port = int(port) - - elif scheme == "https": - port = 443 - - self.port = port or 80 - self.path = path or "" - self.query = query or "" - if scheme: - self.scheme = scheme - elif self.port in POTENTIAL_HTTPS_PORTS: - self.scheme = "https" - else: - self.scheme = "http" - self.fragment = fragment or "" - self.last_request = last_request - - def __str__(self): - attrs = ("username", "password", "hostname", "port", "path") - fmt = ", ".join(['%s="%s"' % (k, getattr(self, k, "")) for k in attrs]) - return r"" % fmt - - def __hash__(self): - return hash(text_type(self)) - - def __eq__(self, other): - self_tuple = ( - self.port, - decode_utf8(self.hostname.lower()), - url_fix(decode_utf8(self.path)), - ) - other_tuple = ( - other.port, - decode_utf8(other.hostname.lower()), - url_fix(decode_utf8(other.path)), - ) - return self_tuple == other_tuple - - def full_url(self, use_querystring=True): - credentials = "" - if self.password: - credentials = "{0}:{1}@".format(self.username, self.password) - - query = "" - if use_querystring and self.query: - query = "?{0}".format(decode_utf8(self.query)) - - result = "{scheme}://{credentials}{domain}{path}{query}".format( - scheme=self.scheme, - credentials=credentials, - domain=self.get_full_domain(), - path=decode_utf8(self.path), - query=query, - ) - return result - - def get_full_domain(self): - hostname = decode_utf8(self.hostname) - # Port 80/443 should not be appended to the url - if self.port not in DEFAULT_HTTP_PORTS | DEFAULT_HTTPS_PORTS: - return ":".join([hostname, str(self.port)]) - - return hostname - - @classmethod - def from_uri(cls, uri, entry): - result = urlsplit(uri) - if result.scheme == "https": - POTENTIAL_HTTPS_PORTS.add(int(result.port or 443)) - else: - POTENTIAL_HTTP_PORTS.add(int(result.port or 80)) - return cls( - result.username, - result.password, - result.hostname, - result.port, - result.path, - result.query, - result.fragment, - result.scheme, - entry, - ) - - -class URIMatcher(object): - regex = None - info = None - - def __init__(self, uri, entries, match_querystring=False): - self._match_querystring = match_querystring - if type(uri).__name__ in ("SRE_Pattern", "Pattern"): - self.regex = uri - result = urlsplit(uri.pattern) - if result.scheme == "https": - POTENTIAL_HTTPS_PORTS.add(int(result.port or 443)) - else: - POTENTIAL_HTTP_PORTS.add(int(result.port or 80)) - else: - self.info = URIInfo.from_uri(uri, entries) - - self.entries = entries - - # hash of current_entry pointers, per method. - self.current_entries = {} - - def matches(self, info): - if self.info: - return self.info == info - else: - return self.regex.search( - info.full_url(use_querystring=self._match_querystring) - ) - - def __str__(self): - wrap = "URLMatcher({0})" - if self.info: - return wrap.format(text_type(self.info)) - else: - return wrap.format(self.regex.pattern) - - def get_next_entry(self, method, info, request): - """Cycle through available responses, but only once. - Any subsequent requests will receive the last response""" - - if method not in self.current_entries: - self.current_entries[method] = 0 - - # restrict selection to entries that match the requested method - entries_for_method = [e for e in self.entries if e.method == method] - - if self.current_entries[method] >= len(entries_for_method): - self.current_entries[method] = -1 - - if not self.entries or not entries_for_method: - raise ValueError("I have no entries for method %s: %s" % (method, self)) - - entry = entries_for_method[self.current_entries[method]] - if self.current_entries[method] != -1: - self.current_entries[method] += 1 - - # Attach more info to the entry - # So the callback can be more clever about what to do - # This does also fix the case where the callback - # would be handed a compiled regex as uri instead of the - # real uri - entry.info = info - entry.request = request - return entry - - def __hash__(self): - return hash(text_type(self)) - - def __eq__(self, other): - return text_type(self) == text_type(other) - - -class httpretty(HttpBaseClass): - """The URI registration class""" - - _entries = {} - latest_requests = [] - - last_request = HTTPrettyRequestEmpty() - _is_enabled = False - allow_net_connect = True - - @classmethod - def match_uriinfo(cls, info): - for matcher, value in cls._entries.items(): - if matcher.matches(info): - return (matcher, info) - - return (None, []) - - @classmethod - @contextlib.contextmanager - def record(cls, filename, indentation=4, encoding="utf-8"): - try: - import urllib3 - except ImportError: - raise RuntimeError( - "HTTPretty requires urllib3 installed for recording actual requests." - ) - - http = urllib3.PoolManager() - - cls.enable() - calls = [] - - def record_request(request, uri, headers): - cls.disable() - - response = http.request(request.method, uri) - calls.append( - { - "request": { - "uri": uri, - "method": request.method, - "headers": dict(request.headers), - "body": decode_utf8(request.body), - "querystring": request.querystring, - }, - "response": { - "status": response.status, - "body": decode_utf8(response.data), - "headers": dict(response.headers), - }, - } - ) - cls.enable() - return response.status, response.headers, response.data - - for method in cls.METHODS: - cls.register_uri(method, re.compile(r".*", re.M), body=record_request) - - yield - cls.disable() - with codecs.open(filename, "w", encoding) as f: - f.write(json.dumps(calls, indent=indentation)) - - @classmethod - @contextlib.contextmanager - def playback(cls, origin): - cls.enable() - - data = json.loads(open(origin).read()) - for item in data: - uri = item["request"]["uri"] - method = item["request"]["method"] - cls.register_uri( - method, - uri, - body=item["response"]["body"], - forcing_headers=item["response"]["headers"], - ) - - yield - cls.disable() - - @classmethod - def reset(cls): - POTENTIAL_HTTP_PORTS.intersection_update(DEFAULT_HTTP_PORTS) - POTENTIAL_HTTPS_PORTS.intersection_update(DEFAULT_HTTPS_PORTS) - cls._entries.clear() - cls.latest_requests = [] - cls.last_request = HTTPrettyRequestEmpty() - - @classmethod - def historify_request(cls, headers, body="", append=True): - request = HTTPrettyRequest(headers, body) - cls.last_request = request - if append or not cls.latest_requests: - cls.latest_requests.append(request) - else: - cls.latest_requests[-1] = request - return request - - @classmethod - def register_uri( - cls, - method, - uri, - body="HTTPretty :)", - adding_headers=None, - forcing_headers=None, - status=200, - responses=None, - match_querystring=False, - **headers - ): - - uri_is_string = isinstance(uri, basestring) - - if uri_is_string and re.search(r"^\w+://[^/]+[.]\w{2,}$", uri): - uri += "/" - - if isinstance(responses, list) and len(responses) > 0: - for response in responses: - response.uri = uri - response.method = method - entries_for_this_uri = responses - else: - headers[str("body")] = body - headers[str("adding_headers")] = adding_headers - headers[str("forcing_headers")] = forcing_headers - headers[str("status")] = status - - entries_for_this_uri = [cls.Response(method=method, uri=uri, **headers)] - - matcher = URIMatcher(uri, entries_for_this_uri, match_querystring) - if matcher in cls._entries: - matcher.entries.extend(cls._entries[matcher]) - del cls._entries[matcher] - - cls._entries[matcher] = entries_for_this_uri - - def __str__(self): - return "" % len(self._entries) - - @classmethod - def Response( - cls, - body, - method=None, - uri=None, - adding_headers=None, - forcing_headers=None, - status=200, - streaming=False, - **headers - ): - - headers[str("body")] = body - headers[str("adding_headers")] = adding_headers - headers[str("forcing_headers")] = forcing_headers - headers[str("status")] = int(status) - headers[str("streaming")] = streaming - return Entry(method, uri, **headers) - - @classmethod - def disable(cls): - cls._is_enabled = False - socket.socket = old_socket - if not BAD_SOCKET_SHADOW: - socket.SocketType = old_socket - socket._socketobject = old_socket - - socket.create_connection = old_create_connection - socket.gethostname = old_gethostname - socket.gethostbyname = old_gethostbyname - socket.getaddrinfo = old_getaddrinfo - - socket.__dict__["socket"] = old_socket - socket.__dict__["_socketobject"] = old_socket - if not BAD_SOCKET_SHADOW: - socket.__dict__["SocketType"] = old_socket - - socket.__dict__["create_connection"] = old_create_connection - socket.__dict__["gethostname"] = old_gethostname - socket.__dict__["gethostbyname"] = old_gethostbyname - socket.__dict__["getaddrinfo"] = old_getaddrinfo - - if socks: - socks.socksocket = old_socksocket - socks.__dict__["socksocket"] = old_socksocket - - if ssl: - ssl.wrap_socket = old_ssl_wrap_socket - ssl.SSLSocket = old_sslsocket - try: - ssl.SSLContext.wrap_socket = old_sslcontext_wrap_socket - except AttributeError: - pass - ssl.__dict__["wrap_socket"] = old_ssl_wrap_socket - ssl.__dict__["SSLSocket"] = old_sslsocket - - if not PY3: - ssl.sslwrap_simple = old_sslwrap_simple - ssl.__dict__["sslwrap_simple"] = old_sslwrap_simple - - if pyopenssl_override: - inject_into_urllib3() - - @classmethod - def is_enabled(cls): - return cls._is_enabled - - @classmethod - def enable(cls): - cls._is_enabled = True - - socket.socket = fakesock.socket - socket._socketobject = fakesock.socket - if not BAD_SOCKET_SHADOW: - socket.SocketType = fakesock.socket - - socket.create_connection = create_fake_connection - socket.gethostname = fake_gethostname - socket.gethostbyname = fake_gethostbyname - socket.getaddrinfo = fake_getaddrinfo - - socket.__dict__["socket"] = fakesock.socket - socket.__dict__["_socketobject"] = fakesock.socket - if not BAD_SOCKET_SHADOW: - socket.__dict__["SocketType"] = fakesock.socket - - socket.__dict__["create_connection"] = create_fake_connection - socket.__dict__["gethostname"] = fake_gethostname - socket.__dict__["gethostbyname"] = fake_gethostbyname - socket.__dict__["getaddrinfo"] = fake_getaddrinfo - - if socks: - socks.socksocket = fakesock.socket - socks.__dict__["socksocket"] = fakesock.socket - - if ssl: - ssl.wrap_socket = fake_wrap_socket - ssl.SSLSocket = FakeSSLSocket - - try: - - def fake_sslcontext_wrap_socket(cls, *args, **kwargs): - return fake_wrap_socket(*args, **kwargs) - - ssl.SSLContext.wrap_socket = fake_sslcontext_wrap_socket - except AttributeError: - pass - - ssl.__dict__["wrap_socket"] = fake_wrap_socket - ssl.__dict__["SSLSocket"] = FakeSSLSocket - - if not PY3: - ssl.sslwrap_simple = fake_wrap_socket - ssl.__dict__["sslwrap_simple"] = fake_wrap_socket - - if pyopenssl_override: - extract_from_urllib3() - - -def httprettified(test): - "A decorator tests that use HTTPretty" - - def decorate_class(klass): - for attr in dir(klass): - if not attr.startswith("test_"): - continue - - attr_value = getattr(klass, attr) - if not hasattr(attr_value, "__call__"): - continue - - setattr(klass, attr, decorate_callable(attr_value)) - return klass - - def decorate_callable(test): - @functools.wraps(test) - def wrapper(*args, **kw): - httpretty.reset() - httpretty.enable() - try: - return test(*args, **kw) - finally: - httpretty.disable() - - return wrapper - - if isinstance(test, ClassTypes): - return decorate_class(test) - return decorate_callable(test) diff --git a/moto/packages/httpretty/errors.py b/moto/packages/httpretty/errors.py deleted file mode 100644 index 8221e5f66..000000000 --- a/moto/packages/httpretty/errors.py +++ /dev/null @@ -1,39 +0,0 @@ -# #!/usr/bin/env python -# -*- coding: utf-8 -*- -# -# -# Copyright (C) <2011-2013> Gabriel Falcão -# -# Permission is hereby granted, free of charge, to any person -# obtaining a copy of this software and associated documentation -# files (the "Software"), to deal in the Software without -# restriction, including without limitation the rights to use, -# copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following -# conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -# OTHER DEALINGS IN THE SOFTWARE. -from __future__ import unicode_literals - - -class HTTPrettyError(Exception): - pass - - -class UnmockedError(HTTPrettyError): - def __init__(self): - super(UnmockedError, self).__init__( - "No mocking was registered, and real connections are " - "not allowed (httpretty.allow_net_connect = False)." - ) diff --git a/moto/packages/httpretty/http.py b/moto/packages/httpretty/http.py deleted file mode 100644 index 1b4379f5b..000000000 --- a/moto/packages/httpretty/http.py +++ /dev/null @@ -1,154 +0,0 @@ -# #!/usr/bin/env python -# -*- coding: utf-8 -*- -# -# Copyright (C) <2011-2013> Gabriel Falcão -# -# Permission is hereby granted, free of charge, to any person -# obtaining a copy of this software and associated documentation -# files (the "Software"), to deal in the Software without -# restriction, including without limitation the rights to use, -# copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following -# conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -# OTHER DEALINGS IN THE SOFTWARE. -from __future__ import unicode_literals - -import re -from .compat import BaseClass -from .utils import decode_utf8 - -STATUSES = { - 100: "Continue", - 101: "Switching Protocols", - 102: "Processing", - 200: "OK", - 201: "Created", - 202: "Accepted", - 203: "Non-Authoritative Information", - 204: "No Content", - 205: "Reset Content", - 206: "Partial Content", - 207: "Multi-Status", - 208: "Already Reported", - 226: "IM Used", - 300: "Multiple Choices", - 301: "Moved Permanently", - 302: "Found", - 303: "See Other", - 304: "Not Modified", - 305: "Use Proxy", - 306: "Switch Proxy", - 307: "Temporary Redirect", - 308: "Permanent Redirect", - 400: "Bad Request", - 401: "Unauthorized", - 402: "Payment Required", - 403: "Forbidden", - 404: "Not Found", - 405: "Method Not Allowed", - 406: "Not Acceptable", - 407: "Proxy Authentication Required", - 408: "Request a Timeout", - 409: "Conflict", - 410: "Gone", - 411: "Length Required", - 412: "Precondition Failed", - 413: "Request Entity Too Large", - 414: "Request-URI Too Long", - 415: "Unsupported Media Type", - 416: "Requested Range Not Satisfiable", - 417: "Expectation Failed", - 418: "I'm a teapot", - 420: "Enhance Your Calm", - 422: "Unprocessable Entity", - 423: "Locked", - 424: "Failed Dependency", - 424: "Method Failure", - 425: "Unordered Collection", - 426: "Upgrade Required", - 428: "Precondition Required", - 429: "Too Many Requests", - 431: "Request Header Fields Too Large", - 444: "No Response", - 449: "Retry With", - 450: "Blocked by Windows Parental Controls", - 451: "Unavailable For Legal Reasons", - 451: "Redirect", - 494: "Request Header Too Large", - 495: "Cert Error", - 496: "No Cert", - 497: "HTTP to HTTPS", - 499: "Client Closed Request", - 500: "Internal Server Error", - 501: "Not Implemented", - 502: "Bad Gateway", - 503: "Service Unavailable", - 504: "Gateway Timeout", - 505: "HTTP Version Not Supported", - 506: "Variant Also Negotiates", - 507: "Insufficient Storage", - 508: "Loop Detected", - 509: "Bandwidth Limit Exceeded", - 510: "Not Extended", - 511: "Network Authentication Required", - 598: "Network read timeout error", - 599: "Network connect timeout error", -} - - -class HttpBaseClass(BaseClass): - GET = "GET" - PUT = "PUT" - POST = "POST" - DELETE = "DELETE" - HEAD = "HEAD" - PATCH = "PATCH" - OPTIONS = "OPTIONS" - CONNECT = "CONNECT" - METHODS = (GET, PUT, POST, DELETE, HEAD, PATCH, OPTIONS, CONNECT) - - -def parse_requestline(s): - """ - http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5 - - >>> parse_requestline('GET / HTTP/1.0') - ('GET', '/', '1.0') - >>> parse_requestline('post /testurl htTP/1.1') - ('POST', '/testurl', '1.1') - >>> parse_requestline('Im not a RequestLine') - Traceback (most recent call last): - ... - ValueError: Not a Request-Line - """ - methods = "|".join(HttpBaseClass.METHODS) - m = re.match(r"({})\s+(.*)\s+HTTP/(1.[0|1])".format(methods), s, re.I) - if m: - return m.group(1).upper(), m.group(2), m.group(3) - else: - raise ValueError("Not a Request-Line") - - -def last_requestline(sent_data): - """ - Find the last line in sent_data that can be parsed with parse_requestline - """ - for line in reversed(sent_data): - try: - parse_requestline(decode_utf8(line)) - except ValueError: - pass - else: - return line diff --git a/moto/packages/httpretty/utils.py b/moto/packages/httpretty/utils.py deleted file mode 100644 index 2bf5d0829..000000000 --- a/moto/packages/httpretty/utils.py +++ /dev/null @@ -1,46 +0,0 @@ -# #!/usr/bin/env python -# -*- coding: utf-8 -*- -# -# Copyright (C) <2011-2013> Gabriel Falcão -# -# Permission is hereby granted, free of charge, to any person -# obtaining a copy of this software and associated documentation -# files (the "Software"), to deal in the Software without -# restriction, including without limitation the rights to use, -# copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following -# conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -# OTHER DEALINGS IN THE SOFTWARE. -from __future__ import unicode_literals - -from .compat import byte_type, text_type - - -def utf8(s): - if isinstance(s, text_type): - s = s.encode("utf-8") - elif s is None: - return byte_type() - - return byte_type(s) - - -def decode_utf8(s): - if isinstance(s, byte_type): - s = s.decode("utf-8") - elif s is None: - return text_type() - - return text_type(s) diff --git a/moto/rds/__init__.py b/moto/rds/__init__.py index 8483e1841..d7347b544 100644 --- a/moto/rds/__init__.py +++ b/moto/rds/__init__.py @@ -1,5 +1,4 @@ from .models import rds_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator mock_rds = base_decorator(rds_backends) -mock_rds_deprecated = deprecated_base_decorator(rds_backends) diff --git a/moto/rds2/__init__.py b/moto/rds2/__init__.py index 0b7fbeaa6..fbba2910f 100644 --- a/moto/rds2/__init__.py +++ b/moto/rds2/__init__.py @@ -1,6 +1,5 @@ from .models import rds2_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator rds2_backend = rds2_backends["us-west-1"] mock_rds2 = base_decorator(rds2_backends) -mock_rds2_deprecated = deprecated_base_decorator(rds2_backends) diff --git a/moto/redshift/__init__.py b/moto/redshift/__init__.py index 063ee2cc9..0bd3d0b8e 100644 --- a/moto/redshift/__init__.py +++ b/moto/redshift/__init__.py @@ -1,5 +1,4 @@ from .models import redshift_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator mock_redshift = base_decorator(redshift_backends) -mock_redshift_deprecated = deprecated_base_decorator(redshift_backends) diff --git a/moto/route53/__init__.py b/moto/route53/__init__.py index 7674e8808..8ba7fcd8a 100644 --- a/moto/route53/__init__.py +++ b/moto/route53/__init__.py @@ -2,4 +2,3 @@ from .models import route53_backend route53_backends = {"global": route53_backend} mock_route53 = route53_backend.decorator -mock_route53_deprecated = route53_backend.deprecated_decorator diff --git a/moto/s3/__init__.py b/moto/s3/__init__.py index ccb3168be..ccd6ccb7e 100644 --- a/moto/s3/__init__.py +++ b/moto/s3/__init__.py @@ -2,4 +2,3 @@ from .models import s3_backend s3_backends = {"global": s3_backend} mock_s3 = s3_backend.decorator -mock_s3_deprecated = s3_backend.deprecated_decorator diff --git a/moto/s3/models.py b/moto/s3/models.py index d3b8b38c4..8f9e5ece4 100644 --- a/moto/s3/models.py +++ b/moto/s3/models.py @@ -196,18 +196,6 @@ class FakeKey(BaseModel): def set_acl(self, acl): self.acl = acl - def append_to_value(self, value): - self.contentsize += len(value) - self._value_buffer.seek(0, os.SEEK_END) - self._value_buffer.write(value) - - self.last_modified = datetime.datetime.utcnow() - self._etag = None # must recalculate etag - if self._is_versioned: - self._version_id = str(uuid.uuid4()) - else: - self._version_id = None - def restore(self, days): self._expiry = datetime.datetime.utcnow() + datetime.timedelta(days) @@ -1674,11 +1662,6 @@ class S3Backend(BaseBackend, CloudWatchMetricProvider): key.lock_mode = retention[0] key.lock_until = retention[1] - def append_to_key(self, bucket_name, key_name, value): - key = self.get_object(bucket_name, key_name) - key.append_to_value(value) - return key - def get_object(self, bucket_name, key_name, version_id=None, part_number=None): key_name = clean_key_name(key_name) bucket = self.get_bucket(bucket_name) diff --git a/moto/s3/responses.py b/moto/s3/responses.py index f1a979ee5..1238c423c 100644 --- a/moto/s3/responses.py +++ b/moto/s3/responses.py @@ -5,10 +5,10 @@ from typing import List, Union from botocore.awsrequest import AWSPreparedRequest +from moto import settings from moto.core.utils import amzn_request_id, str_to_rfc_1123_datetime from urllib.parse import ( parse_qs, - parse_qsl, urlparse, unquote, urlencode, @@ -17,8 +17,6 @@ from urllib.parse import ( import xmltodict -from moto import settings -from moto.packages.httpretty.core import HTTPrettyRequest from moto.core.responses import _TemplateEnvironmentMixin, ActionAuthenticatorMixin from moto.core.utils import path_url from moto.core import ACCOUNT_ID @@ -972,13 +970,7 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin): self._authenticate_and_authorize_s3_action() # POST to bucket-url should create file from form - if hasattr(request, "form"): - # Not HTTPretty - form = request.form - else: - # HTTPretty, build new form object - body = body.decode() - form = dict(parse_qsl(body)) + form = request.form key = form["key"] if "file" in form: @@ -1028,15 +1020,11 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin): @staticmethod def _get_path(request): - if isinstance(request, HTTPrettyRequest): - path = request.path - else: - path = ( - request.full_path - if hasattr(request, "full_path") - else path_url(request.url) - ) - return path + return ( + request.full_path + if hasattr(request, "full_path") + else path_url(request.url) + ) def _bucket_response_delete_keys(self, request, body, bucket_name): template = self.response_template(S3_DELETE_KEYS_RESPONSE) @@ -1587,39 +1575,29 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin): template = self.response_template(S3_OBJECT_COPY_RESPONSE) response_headers.update(new_key.response_dict) return 200, response_headers, template.render(key=new_key) - streaming_request = hasattr(request, "streaming") and request.streaming - closing_connection = headers.get("connection") == "close" - if closing_connection and streaming_request: - # Closing the connection of a streaming request. No more data - new_key = self.backend.get_object(bucket_name, key_name) - elif streaming_request: - # Streaming request, more data - new_key = self.backend.append_to_key(bucket_name, key_name, body) - else: - # Initial data - new_key = self.backend.put_object( - bucket_name, - key_name, - body, - storage=storage_class, - encryption=encryption, - kms_key_id=kms_key_id, - bucket_key_enabled=bucket_key_enabled, - lock_mode=lock_mode, - lock_legal_status=legal_hold, - lock_until=lock_until, - ) + # Initial data + new_key = self.backend.put_object( + bucket_name, + key_name, + body, + storage=storage_class, + encryption=encryption, + kms_key_id=kms_key_id, + bucket_key_enabled=bucket_key_enabled, + lock_mode=lock_mode, + lock_legal_status=legal_hold, + lock_until=lock_until, + ) - request.streaming = True - metadata = metadata_from_headers(request.headers) - metadata.update(metadata_from_headers(query)) - new_key.set_metadata(metadata) - new_key.set_acl(acl) - new_key.website_redirect_location = request.headers.get( - "x-amz-website-redirect-location" - ) - self.backend.set_key_tags(new_key, tagging) + metadata = metadata_from_headers(request.headers) + metadata.update(metadata_from_headers(query)) + new_key.set_metadata(metadata) + new_key.set_acl(acl) + new_key.website_redirect_location = request.headers.get( + "x-amz-website-redirect-location" + ) + self.backend.set_key_tags(new_key, tagging) response_headers.update(new_key.response_dict) return 200, response_headers, "" diff --git a/moto/server.py b/moto/server.py index 2d6279d8a..0b4ab32fa 100644 --- a/moto/server.py +++ b/moto/server.py @@ -17,7 +17,7 @@ from werkzeug.serving import run_simple import moto.backends as backends import moto.backend_index as backend_index -from moto.core.utils import convert_flask_to_httpretty_response +from moto.core.utils import convert_to_flask_response HTTP_METHODS = ["GET", "POST", "PUT", "DELETE", "HEAD", "PATCH", "OPTIONS"] @@ -291,7 +291,7 @@ def create_backend_app(service): backend = backend_dict["global"] for url_path, handler in backend.flask_paths.items(): - view_func = convert_flask_to_httpretty_response(handler) + view_func = convert_to_flask_response(handler) if handler.__name__ == "dispatch": endpoint = "{0}.dispatch".format(handler.__self__.__name__) else: diff --git a/moto/ses/__init__.py b/moto/ses/__init__.py index fef560572..61a4863f8 100644 --- a/moto/ses/__init__.py +++ b/moto/ses/__init__.py @@ -2,4 +2,3 @@ from .models import ses_backend ses_backends = {"global": ses_backend} mock_ses = ses_backend.decorator -mock_ses_deprecated = ses_backend.deprecated_decorator diff --git a/moto/sns/__init__.py b/moto/sns/__init__.py index bc9e5dc43..45fdc48d7 100644 --- a/moto/sns/__init__.py +++ b/moto/sns/__init__.py @@ -1,6 +1,5 @@ from .models import sns_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator sns_backend = sns_backends["us-east-1"] mock_sns = base_decorator(sns_backends) -mock_sns_deprecated = deprecated_base_decorator(sns_backends) diff --git a/moto/sqs/__init__.py b/moto/sqs/__init__.py index 21264d09e..4a9293103 100644 --- a/moto/sqs/__init__.py +++ b/moto/sqs/__init__.py @@ -1,6 +1,5 @@ from .models import sqs_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator sqs_backend = sqs_backends["us-east-1"] mock_sqs = base_decorator(sqs_backends) -mock_sqs_deprecated = deprecated_base_decorator(sqs_backends) diff --git a/moto/sts/__init__.py b/moto/sts/__init__.py index d3839a8c0..062096832 100644 --- a/moto/sts/__init__.py +++ b/moto/sts/__init__.py @@ -2,4 +2,3 @@ from .models import sts_backend sts_backends = {"global": sts_backend} mock_sts = sts_backend.decorator -mock_sts_deprecated = sts_backend.deprecated_decorator diff --git a/moto/swf/__init__.py b/moto/swf/__init__.py index 12fc6a9a8..fa39f9d74 100644 --- a/moto/swf/__init__.py +++ b/moto/swf/__init__.py @@ -1,6 +1,5 @@ from .models import swf_backends -from ..core.models import base_decorator, deprecated_base_decorator +from ..core.models import base_decorator swf_backend = swf_backends["us-east-1"] mock_swf = base_decorator(swf_backends) -mock_swf_deprecated = deprecated_base_decorator(swf_backends) diff --git a/requirements-dev.txt b/requirements-dev.txt index cdab55017..2a3eaf73c 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -4,7 +4,6 @@ black==19.10b0 regex==2019.11.1 flake8==3.7.8 -boto>=2.45.0 click inflection==0.3.1 lxml diff --git a/setup.py b/setup.py index 80da71b54..89658dc52 100755 --- a/setup.py +++ b/setup.py @@ -3,7 +3,6 @@ from io import open import os import re from setuptools import setup, find_packages -import sys import moto.__init__ as service_list # Borrowed from pip at https://github.com/pypa/pip/blob/62c27dee45625e1b63d1e023b0656310f276e050/setup.py#L11-L15 diff --git a/tests/helpers.py b/tests/helpers.py index 93f4db41a..47d1793db 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -1,33 +1,7 @@ -import boto -from unittest import SkipTest from collections.abc import Iterable, Mapping from sure import assertion -def version_tuple(v): - return tuple(map(int, (v.split(".")))) - - -# Note: See https://github.com/spulec/moto/issues/201 for why this is a -# separate method. -def skip_test(): - raise SkipTest - - -class requires_boto_gte(object): - """Decorator for requiring boto version greater than or equal to 'version'""" - - def __init__(self, version): - self.version = version - - def __call__(self, test): - boto_version = version_tuple(boto.__version__) - required = version_tuple(self.version) - if boto_version >= required: - return test - return skip_test - - @assertion def containing_item_with_attributes(context, **kwargs): contains = False diff --git a/tests/test_autoscaling/test_autoscaling.py b/tests/test_autoscaling/test_autoscaling.py index 784b47a47..c012d3a72 100644 --- a/tests/test_autoscaling/test_autoscaling.py +++ b/tests/test_autoscaling/test_autoscaling.py @@ -1,123 +1,22 @@ -import boto import boto3 -import boto.ec2.autoscale -from boto.ec2.autoscale.launchconfig import LaunchConfiguration -from boto.ec2.autoscale.group import AutoScalingGroup -from boto.ec2.autoscale import Tag -import boto.ec2.elb import sure # noqa # pylint: disable=unused-import from botocore.exceptions import ClientError import pytest from moto import ( mock_autoscaling, - mock_ec2_deprecated, - mock_elb_deprecated, mock_elb, - mock_autoscaling_deprecated, mock_ec2, ) from moto.core import ACCOUNT_ID -from tests.helpers import requires_boto_gte from .utils import ( setup_networking, - setup_networking_deprecated, setup_instance_with_networking, ) from tests import EXAMPLE_AMI_ID -# Has boto3 equivalent -@mock_autoscaling_deprecated -@mock_elb_deprecated -@mock_ec2_deprecated -def test_create_autoscaling_group(): - mocked_networking = setup_networking_deprecated() - elb_conn = boto.ec2.elb.connect_to_region("us-east-1") - elb_conn.create_load_balancer("test_lb", zones=[], listeners=[(80, 8080, "http")]) - - # we attach a couple of machines to the load balancer - # that are not managed by the auto scaling group - INSTANCE_COUNT_START = 3 - INSTANCE_COUNT_GROUP = 2 - ec2 = boto3.resource("ec2", region_name="us-east-1") - instances = ec2.create_instances( - ImageId=EXAMPLE_AMI_ID, - InstanceType="t1.micro", - MaxCount=INSTANCE_COUNT_START, - MinCount=INSTANCE_COUNT_START, - SubnetId=mocked_networking["subnet1"], - ) - - instances_ids = [_.id for _ in instances] - elb_conn.register_instances( - "test_lb", instances_ids, - ) - - conn = boto.ec2.autoscale.connect_to_region("us-east-1") - config = LaunchConfiguration( - name="tester", image_id=EXAMPLE_AMI_ID, instance_type="t2.medium" - ) - conn.create_launch_configuration(config) - - group = AutoScalingGroup( - name="tester_group", - availability_zones=["us-east-1a", "us-east-1b"], - default_cooldown=60, - desired_capacity=INSTANCE_COUNT_GROUP, - health_check_period=100, - health_check_type="EC2", - max_size=INSTANCE_COUNT_GROUP, - min_size=INSTANCE_COUNT_GROUP, - launch_config=config, - load_balancers=["test_lb"], - placement_group="test_placement", - vpc_zone_identifier="{subnet1},{subnet2}".format( - subnet1=mocked_networking["subnet1"], subnet2=mocked_networking["subnet2"] - ), - termination_policies=["OldestInstance", "NewestInstance"], - tags=[ - Tag( - resource_id="tester_group", - key="test_key", - value="test_value", - propagate_at_launch=True, - ) - ], - ) - conn.create_auto_scaling_group(group) - - group = conn.get_all_groups()[0] - group.name.should.equal("tester_group") - set(group.availability_zones).should.equal(set(["us-east-1a", "us-east-1b"])) - group.desired_capacity.should.equal(2) - group.max_size.should.equal(INSTANCE_COUNT_GROUP) - group.min_size.should.equal(INSTANCE_COUNT_GROUP) - group.instances.should.have.length_of(INSTANCE_COUNT_GROUP) - group.vpc_zone_identifier.should.equal( - "{subnet1},{subnet2}".format( - subnet1=mocked_networking["subnet1"], subnet2=mocked_networking["subnet2"] - ) - ) - group.launch_config_name.should.equal("tester") - group.default_cooldown.should.equal(60) - group.health_check_period.should.equal(100) - group.health_check_type.should.equal("EC2") - list(group.load_balancers).should.equal(["test_lb"]) - group.placement_group.should.equal("test_placement") - list(group.termination_policies).should.equal(["OldestInstance", "NewestInstance"]) - len(list(group.tags)).should.equal(1) - tag = list(group.tags)[0] - tag.resource_id.should.equal("tester_group") - tag.key.should.equal("test_key") - tag.value.should.equal("test_value") - tag.propagate_at_launch.should.equal(True) - - instances_attached = elb_conn.describe_instance_health("test_lb") - len(instances_attached).should.equal(INSTANCE_COUNT_START + INSTANCE_COUNT_GROUP) - - @mock_autoscaling @mock_ec2 @mock_elb @@ -222,47 +121,6 @@ def test_create_autoscaling_group_boto3_within_elb(): attached_ids.should.contain(ec2_instance_id) -# Has boto3 equivalent -@mock_autoscaling_deprecated -def test_create_autoscaling_groups_defaults(): - """Test with the minimum inputs and check that all of the proper defaults - are assigned for the other attributes""" - - mocked_networking = setup_networking_deprecated() - conn = boto.connect_autoscale() - config = LaunchConfiguration( - name="tester", image_id=EXAMPLE_AMI_ID, instance_type="t2.medium" - ) - conn.create_launch_configuration(config) - - group = AutoScalingGroup( - name="tester_group", - max_size=2, - min_size=2, - launch_config=config, - vpc_zone_identifier=mocked_networking["subnet1"], - ) - conn.create_auto_scaling_group(group) - - group = conn.get_all_groups()[0] - group.name.should.equal("tester_group") - group.max_size.should.equal(2) - group.min_size.should.equal(2) - group.launch_config_name.should.equal("tester") - - # Defaults - list(group.availability_zones).should.equal(["us-east-1a"]) # subnet1 - group.desired_capacity.should.equal(2) - group.vpc_zone_identifier.should.equal(mocked_networking["subnet1"]) - group.default_cooldown.should.equal(300) - group.health_check_period.should.equal(300) - group.health_check_type.should.equal("EC2") - list(group.load_balancers).should.equal([]) - group.placement_group.should.equal(None) - list(group.termination_policies).should.equal([]) - list(group.tags).should.equal([]) - - @mock_autoscaling def test_create_autoscaling_groups_defaults_boto3(): """Test with the minimum inputs and check that all of the proper defaults @@ -371,142 +229,6 @@ def test_propogate_tags(): tags.should.contain({"Value": "TestGroup1", "Key": "aws:autoscaling:groupName"}) -# Has boto3 equivalent -@mock_autoscaling_deprecated -def test_autoscaling_group_describe_filter(): - mocked_networking = setup_networking_deprecated() - conn = boto.connect_autoscale() - config = LaunchConfiguration( - name="tester", image_id=EXAMPLE_AMI_ID, instance_type="t2.medium" - ) - conn.create_launch_configuration(config) - - group = AutoScalingGroup( - name="tester_group", - max_size=2, - min_size=2, - launch_config=config, - vpc_zone_identifier=mocked_networking["subnet1"], - ) - conn.create_auto_scaling_group(group) - group.name = "tester_group2" - conn.create_auto_scaling_group(group) - group.name = "tester_group3" - conn.create_auto_scaling_group(group) - - conn.get_all_groups(names=["tester_group", "tester_group2"]).should.have.length_of( - 2 - ) - conn.get_all_groups().should.have.length_of(3) - - -# Has boto3 equivalent -@mock_autoscaling_deprecated -def test_autoscaling_update(): - mocked_networking = setup_networking_deprecated() - conn = boto.connect_autoscale() - config = LaunchConfiguration( - name="tester", image_id=EXAMPLE_AMI_ID, instance_type="t2.medium" - ) - conn.create_launch_configuration(config) - - group = AutoScalingGroup( - name="tester_group", - desired_capacity=2, - max_size=2, - min_size=2, - launch_config=config, - vpc_zone_identifier=mocked_networking["subnet1"], - ) - conn.create_auto_scaling_group(group) - - group = conn.get_all_groups()[0] - group.availability_zones.should.equal(["us-east-1a"]) - group.vpc_zone_identifier.should.equal(mocked_networking["subnet1"]) - - group.availability_zones = ["us-east-1b"] - group.vpc_zone_identifier = mocked_networking["subnet2"] - group.update() - - group = conn.get_all_groups()[0] - group.availability_zones.should.equal(["us-east-1b"]) - group.vpc_zone_identifier.should.equal(mocked_networking["subnet2"]) - - -# Has boto3 equivalent -@mock_autoscaling_deprecated -def test_autoscaling_tags_update(): - mocked_networking = setup_networking_deprecated() - conn = boto.connect_autoscale() - config = LaunchConfiguration( - name="tester", image_id=EXAMPLE_AMI_ID, instance_type="t2.medium" - ) - conn.create_launch_configuration(config) - - group = AutoScalingGroup( - name="tester_group", - availability_zones=["us-east-1a"], - desired_capacity=2, - max_size=2, - min_size=2, - launch_config=config, - tags=[ - Tag( - resource_id="tester_group", - key="test_key", - value="test_value", - propagate_at_launch=True, - ) - ], - vpc_zone_identifier=mocked_networking["subnet1"], - ) - conn.create_auto_scaling_group(group) - - conn.create_or_update_tags( - tags=[ - Tag( - resource_id="tester_group", - key="test_key", - value="new_test_value", - propagate_at_launch=True, - ), - Tag( - resource_id="tester_group", - key="test_key2", - value="test_value2", - propagate_at_launch=True, - ), - ] - ) - group = conn.get_all_groups()[0] - group.tags.should.have.length_of(2) - - -# Has boto3 equivalent -@mock_autoscaling_deprecated -def test_autoscaling_group_delete(): - mocked_networking = setup_networking_deprecated() - conn = boto.connect_autoscale() - config = LaunchConfiguration( - name="tester", image_id=EXAMPLE_AMI_ID, instance_type="t2.medium" - ) - conn.create_launch_configuration(config) - - group = AutoScalingGroup( - name="tester_group", - max_size=2, - min_size=2, - launch_config=config, - vpc_zone_identifier=mocked_networking["subnet1"], - ) - conn.create_auto_scaling_group(group) - - conn.get_all_groups().should.have.length_of(1) - - conn.delete_auto_scaling_group("tester_group") - conn.get_all_groups().should.have.length_of(0) - - @mock_autoscaling def test_autoscaling_group_delete_boto3(): mocked_networking = setup_networking() @@ -535,197 +257,6 @@ def test_autoscaling_group_delete_boto3(): ) -# Has boto3 equivalent -@mock_ec2_deprecated -@mock_autoscaling_deprecated -def test_autoscaling_group_describe_instances(): - mocked_networking = setup_networking_deprecated() - conn = boto.ec2.autoscale.connect_to_region("us-east-1") - config = LaunchConfiguration( - name="tester", image_id=EXAMPLE_AMI_ID, instance_type="t2.medium" - ) - conn.create_launch_configuration(config) - - group = AutoScalingGroup( - name="tester_group", - max_size=2, - min_size=2, - launch_config=config, - vpc_zone_identifier=mocked_networking["subnet1"], - ) - conn.create_auto_scaling_group(group) - - instances = list(conn.get_all_autoscaling_instances()) - instances.should.have.length_of(2) - instances[0].launch_config_name.should.equal("tester") - instances[0].health_status.should.equal("Healthy") - autoscale_instance_ids = [instance.instance_id for instance in instances] - - ec2_conn = boto.ec2.connect_to_region("us-east-1") - reservations = ec2_conn.get_all_reservations() - instances = reservations[0].instances - instances.should.have.length_of(2) - instance_ids = [instance.id for instance in instances] - set(autoscale_instance_ids).should.equal(set(instance_ids)) - instances[0].instance_type.should.equal("t2.medium") - - -# Has boto3 equivalent -@requires_boto_gte("2.8") -@mock_autoscaling_deprecated -def test_set_desired_capacity_up(): - mocked_networking = setup_networking_deprecated() - conn = boto.connect_autoscale() - config = LaunchConfiguration( - name="tester", image_id=EXAMPLE_AMI_ID, instance_type="t2.medium" - ) - conn.create_launch_configuration(config) - - group = AutoScalingGroup( - name="tester_group", - availability_zones=["us-east-1a"], - desired_capacity=2, - max_size=2, - min_size=2, - launch_config=config, - vpc_zone_identifier=mocked_networking["subnet1"], - ) - conn.create_auto_scaling_group(group) - - group = conn.get_all_groups()[0] - group.desired_capacity.should.equal(2) - instances = list(conn.get_all_autoscaling_instances()) - instances.should.have.length_of(2) - - conn.set_desired_capacity("tester_group", 3) - group = conn.get_all_groups()[0] - group.desired_capacity.should.equal(3) - - instances = list(conn.get_all_autoscaling_instances()) - instances.should.have.length_of(3) - - -# Has boto3 equivalent -@requires_boto_gte("2.8") -@mock_autoscaling_deprecated -def test_set_desired_capacity_down(): - mocked_networking = setup_networking_deprecated() - conn = boto.connect_autoscale() - config = LaunchConfiguration( - name="tester", image_id=EXAMPLE_AMI_ID, instance_type="t2.medium" - ) - conn.create_launch_configuration(config) - - group = AutoScalingGroup( - name="tester_group", - availability_zones=["us-east-1a"], - desired_capacity=2, - max_size=2, - min_size=2, - launch_config=config, - vpc_zone_identifier=mocked_networking["subnet1"], - ) - conn.create_auto_scaling_group(group) - - group = conn.get_all_groups()[0] - group.desired_capacity.should.equal(2) - instances = list(conn.get_all_autoscaling_instances()) - instances.should.have.length_of(2) - - conn.set_desired_capacity("tester_group", 1) - group = conn.get_all_groups()[0] - group.desired_capacity.should.equal(1) - - instances = list(conn.get_all_autoscaling_instances()) - instances.should.have.length_of(1) - - -# Has boto3 equivalent -@requires_boto_gte("2.8") -@mock_autoscaling_deprecated -def test_set_desired_capacity_the_same(): - mocked_networking = setup_networking_deprecated() - conn = boto.connect_autoscale() - config = LaunchConfiguration( - name="tester", image_id=EXAMPLE_AMI_ID, instance_type="t2.medium" - ) - conn.create_launch_configuration(config) - - group = AutoScalingGroup( - name="tester_group", - availability_zones=["us-east-1a"], - desired_capacity=2, - max_size=2, - min_size=2, - launch_config=config, - vpc_zone_identifier=mocked_networking["subnet1"], - ) - conn.create_auto_scaling_group(group) - - group = conn.get_all_groups()[0] - group.desired_capacity.should.equal(2) - instances = list(conn.get_all_autoscaling_instances()) - instances.should.have.length_of(2) - - conn.set_desired_capacity("tester_group", 2) - group = conn.get_all_groups()[0] - group.desired_capacity.should.equal(2) - - instances = list(conn.get_all_autoscaling_instances()) - instances.should.have.length_of(2) - - -# Has boto3 equivalent -@mock_autoscaling_deprecated -@mock_elb_deprecated -def test_autoscaling_group_with_elb(): - mocked_networking = setup_networking_deprecated() - elb_conn = boto.connect_elb() - zones = ["us-east-1a", "us-east-1b"] - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - elb_conn.create_load_balancer("my-lb", zones, ports) - instances_health = elb_conn.describe_instance_health("my-lb") - instances_health.should.be.empty - - conn = boto.connect_autoscale() - config = LaunchConfiguration( - name="tester", image_id=EXAMPLE_AMI_ID, instance_type="t2.medium" - ) - conn.create_launch_configuration(config) - group = AutoScalingGroup( - name="tester_group", - max_size=2, - min_size=2, - launch_config=config, - load_balancers=["my-lb"], - vpc_zone_identifier=mocked_networking["subnet1"], - ) - conn.create_auto_scaling_group(group) - group = conn.get_all_groups()[0] - elb = elb_conn.get_all_load_balancers()[0] - group.desired_capacity.should.equal(2) - elb.instances.should.have.length_of(2) - - autoscale_instance_ids = set(instance.instance_id for instance in group.instances) - elb_instace_ids = set(instance.id for instance in elb.instances) - autoscale_instance_ids.should.equal(elb_instace_ids) - - conn.set_desired_capacity("tester_group", 3) - group = conn.get_all_groups()[0] - elb = elb_conn.get_all_load_balancers()[0] - group.desired_capacity.should.equal(3) - elb.instances.should.have.length_of(3) - - autoscale_instance_ids = set(instance.instance_id for instance in group.instances) - elb_instace_ids = set(instance.id for instance in elb.instances) - autoscale_instance_ids.should.equal(elb_instace_ids) - - conn.delete_auto_scaling_group("tester_group") - conn.get_all_groups().should.have.length_of(0) - elb = elb_conn.get_all_load_balancers()[0] - elb.instances.should.have.length_of(0) - - @mock_autoscaling @mock_elb def test_describe_load_balancers(): diff --git a/tests/test_autoscaling/test_launch_configurations.py b/tests/test_autoscaling/test_launch_configurations.py index d5dcaf280..7ee79a8cb 100644 --- a/tests/test_autoscaling/test_launch_configurations.py +++ b/tests/test_autoscaling/test_launch_configurations.py @@ -1,54 +1,15 @@ import base64 - -import boto import boto3 -from boto.ec2.autoscale.launchconfig import LaunchConfiguration -from boto.ec2.blockdevicemapping import BlockDeviceType, BlockDeviceMapping from botocore.exceptions import ClientError import pytest import sure # noqa # pylint: disable=unused-import -from moto import mock_autoscaling_deprecated from moto import mock_autoscaling from moto.core import ACCOUNT_ID -from tests.helpers import requires_boto_gte from tests import EXAMPLE_AMI_ID -# Has boto3 equivalent -@mock_autoscaling_deprecated -def test_create_launch_configuration(): - conn = boto.connect_autoscale() - config = LaunchConfiguration( - name="tester", - image_id="ami-abcd1234", - instance_type="t1.micro", - key_name="the_keys", - security_groups=["default", "default2"], - user_data=b"This is some user_data", - instance_monitoring=True, - instance_profile_name="arn:aws:iam::{}:instance-profile/testing".format( - ACCOUNT_ID - ), - spot_price=0.1, - ) - conn.create_launch_configuration(config) - - launch_config = conn.get_all_launch_configurations()[0] - launch_config.name.should.equal("tester") - launch_config.image_id.should.equal("ami-abcd1234") - launch_config.instance_type.should.equal("t1.micro") - launch_config.key_name.should.equal("the_keys") - set(launch_config.security_groups).should.equal(set(["default", "default2"])) - launch_config.user_data.should.equal(b"This is some user_data") - launch_config.instance_monitoring.enabled.should.equal("true") - launch_config.instance_profile_name.should.equal( - "arn:aws:iam::{}:instance-profile/testing".format(ACCOUNT_ID) - ) - launch_config.spot_price.should.equal(0.1) - - @mock_autoscaling def test_create_launch_configuration_boto3(): client = boto3.client("autoscaling", region_name="us-east-1") @@ -84,76 +45,6 @@ def test_create_launch_configuration_boto3(): launch_config["BlockDeviceMappings"].should.equal([]) -# Has boto3 equivalent -@requires_boto_gte("2.27.0") -@mock_autoscaling_deprecated -def test_create_launch_configuration_with_block_device_mappings(): - block_device_mapping = BlockDeviceMapping() - - ephemeral_drive = BlockDeviceType() - ephemeral_drive.ephemeral_name = "ephemeral0" - block_device_mapping["/dev/xvdb"] = ephemeral_drive - - snapshot_drive = BlockDeviceType() - snapshot_drive.snapshot_id = "snap-1234abcd" - snapshot_drive.volume_type = "standard" - block_device_mapping["/dev/xvdp"] = snapshot_drive - - ebs_drive = BlockDeviceType() - ebs_drive.volume_type = "io1" - ebs_drive.size = 100 - ebs_drive.iops = 1000 - ebs_drive.delete_on_termination = False - block_device_mapping["/dev/xvdh"] = ebs_drive - - conn = boto.connect_autoscale(use_block_device_types=True) - config = LaunchConfiguration( - name="tester", - image_id="ami-abcd1234", - instance_type="m1.small", - key_name="the_keys", - security_groups=["default", "default2"], - user_data=b"This is some user_data", - instance_monitoring=True, - instance_profile_name="arn:aws:iam::{}:instance-profile/testing".format( - ACCOUNT_ID - ), - spot_price=0.1, - block_device_mappings=[block_device_mapping], - ) - conn.create_launch_configuration(config) - - launch_config = conn.get_all_launch_configurations()[0] - launch_config.name.should.equal("tester") - launch_config.image_id.should.equal("ami-abcd1234") - launch_config.instance_type.should.equal("m1.small") - launch_config.key_name.should.equal("the_keys") - set(launch_config.security_groups).should.equal(set(["default", "default2"])) - launch_config.user_data.should.equal(b"This is some user_data") - launch_config.instance_monitoring.enabled.should.equal("true") - launch_config.instance_profile_name.should.equal( - "arn:aws:iam::{}:instance-profile/testing".format(ACCOUNT_ID) - ) - launch_config.spot_price.should.equal(0.1) - len(launch_config.block_device_mappings).should.equal(3) - - returned_mapping = launch_config.block_device_mappings - - set(returned_mapping.keys()).should.equal( - set(["/dev/xvdb", "/dev/xvdp", "/dev/xvdh"]) - ) - - returned_mapping["/dev/xvdh"].iops.should.equal(1000) - returned_mapping["/dev/xvdh"].size.should.equal(100) - returned_mapping["/dev/xvdh"].volume_type.should.equal("io1") - returned_mapping["/dev/xvdh"].delete_on_termination.should.be.false - - returned_mapping["/dev/xvdp"].snapshot_id.should.equal("snap-1234abcd") - returned_mapping["/dev/xvdp"].volume_type.should.equal("standard") - - returned_mapping["/dev/xvdb"].ephemeral_name.should.equal("ephemeral0") - - @mock_autoscaling def test_create_launch_configuration_with_block_device_mappings_boto3(): client = boto3.client("autoscaling", region_name="us-east-1") @@ -214,34 +105,6 @@ def test_create_launch_configuration_with_block_device_mappings_boto3(): xvdb.shouldnt.have.key("Ebs") -# Has boto3 equivalent -@requires_boto_gte("2.12") -@mock_autoscaling_deprecated -def test_create_launch_configuration_for_2_12(): - conn = boto.connect_autoscale() - config = LaunchConfiguration( - name="tester", image_id="ami-abcd1234", ebs_optimized=True - ) - conn.create_launch_configuration(config) - - launch_config = conn.get_all_launch_configurations()[0] - launch_config.ebs_optimized.should.equal(True) - - -# Has boto3 equivalent -@requires_boto_gte("2.25.0") -@mock_autoscaling_deprecated -def test_create_launch_configuration_using_ip_association(): - conn = boto.connect_autoscale() - config = LaunchConfiguration( - name="tester", image_id="ami-abcd1234", associate_public_ip_address=True - ) - conn.create_launch_configuration(config) - - launch_config = conn.get_all_launch_configurations()[0] - launch_config.associate_public_ip_address.should.equal(True) - - @mock_autoscaling def test_create_launch_configuration_additional_parameters(): client = boto3.client("autoscaling", region_name="us-east-1") @@ -258,18 +121,6 @@ def test_create_launch_configuration_additional_parameters(): launch_config["AssociatePublicIpAddress"].should.equal(True) -# Has boto3 equivalent -@requires_boto_gte("2.25.0") -@mock_autoscaling_deprecated -def test_create_launch_configuration_using_ip_association_should_default_to_false(): - conn = boto.connect_autoscale() - config = LaunchConfiguration(name="tester", image_id="ami-abcd1234") - conn.create_launch_configuration(config) - - launch_config = conn.get_all_launch_configurations()[0] - launch_config.associate_public_ip_address.should.equal(False) - - @mock_autoscaling def test_create_launch_configuration_additional_params_default_to_false(): client = boto3.client("autoscaling", region_name="us-east-1") @@ -284,31 +135,6 @@ def test_create_launch_configuration_additional_params_default_to_false(): launch_config["AssociatePublicIpAddress"].should.equal(False) -# Has boto3 equivalent -@mock_autoscaling_deprecated -def test_create_launch_configuration_defaults(): - """Test with the minimum inputs and check that all of the proper defaults - are assigned for the other attributes""" - conn = boto.connect_autoscale() - config = LaunchConfiguration( - name="tester", image_id="ami-abcd1234", instance_type="m1.small" - ) - conn.create_launch_configuration(config) - - launch_config = conn.get_all_launch_configurations()[0] - launch_config.name.should.equal("tester") - launch_config.image_id.should.equal("ami-abcd1234") - launch_config.instance_type.should.equal("m1.small") - - # Defaults - launch_config.key_name.should.equal("") - list(launch_config.security_groups).should.equal([]) - launch_config.user_data.should.equal(b"") - launch_config.instance_monitoring.enabled.should.equal("false") - launch_config.instance_profile_name.should.equal(None) - launch_config.spot_price.should.equal(None) - - @mock_autoscaling def test_create_launch_configuration_defaults_boto3(): """Test with the minimum inputs and check that all of the proper defaults @@ -331,37 +157,6 @@ def test_create_launch_configuration_defaults_boto3(): launch_config.shouldnt.have.key("SpotPrice") -# Has boto3 equivalent -@requires_boto_gte("2.12") -@mock_autoscaling_deprecated -def test_create_launch_configuration_defaults_for_2_12(): - conn = boto.connect_autoscale() - config = LaunchConfiguration(name="tester", image_id="ami-abcd1234") - conn.create_launch_configuration(config) - - launch_config = conn.get_all_launch_configurations()[0] - launch_config.ebs_optimized.should.equal(False) - - -# Has boto3 equivalent -@mock_autoscaling_deprecated -def test_launch_configuration_describe_filter(): - conn = boto.connect_autoscale() - config = LaunchConfiguration( - name="tester", image_id="ami-abcd1234", instance_type="m1.small" - ) - conn.create_launch_configuration(config) - config.name = "tester2" - conn.create_launch_configuration(config) - config.name = "tester3" - conn.create_launch_configuration(config) - - conn.get_all_launch_configurations( - names=["tester", "tester2"] - ).should.have.length_of(2) - conn.get_all_launch_configurations().should.have.length_of(3) - - @mock_autoscaling def test_launch_configuration_describe_filter_boto3(): client = boto3.client("autoscaling", region_name="us-east-1") @@ -404,21 +199,6 @@ def test_launch_configuration_describe_paginated(): assert "NextToken" not in response2.keys() -# Has boto3 equivalent -@mock_autoscaling_deprecated -def test_launch_configuration_delete(): - conn = boto.connect_autoscale() - config = LaunchConfiguration( - name="tester", image_id="ami-abcd1234", instance_type="m1.small" - ) - conn.create_launch_configuration(config) - - conn.get_all_launch_configurations().should.have.length_of(1) - - conn.delete_launch_configuration("tester") - conn.get_all_launch_configurations().should.have.length_of(0) - - @mock_autoscaling def test_launch_configuration_delete_boto3(): client = boto3.client("autoscaling", region_name="us-east-1") diff --git a/tests/test_autoscaling/test_policies.py b/tests/test_autoscaling/test_policies.py index 43d40ddd6..761f99133 100644 --- a/tests/test_autoscaling/test_policies.py +++ b/tests/test_autoscaling/test_policies.py @@ -1,36 +1,13 @@ -import boto import boto3 -from boto.ec2.autoscale.launchconfig import LaunchConfiguration -from boto.ec2.autoscale.group import AutoScalingGroup -from boto.ec2.autoscale.policy import ScalingPolicy import sure # noqa # pylint: disable=unused-import import pytest -from moto import mock_autoscaling_deprecated, mock_autoscaling +from moto import mock_autoscaling -from .utils import setup_networking_deprecated, setup_networking +from .utils import setup_networking from tests import EXAMPLE_AMI_ID -def setup_autoscale_group(): - mocked_networking = setup_networking_deprecated() - conn = boto.connect_autoscale() - config = LaunchConfiguration( - name="tester", image_id=EXAMPLE_AMI_ID, instance_type="m1.small" - ) - conn.create_launch_configuration(config) - - group = AutoScalingGroup( - name="tester_group", - max_size=2, - min_size=2, - launch_config=config, - vpc_zone_identifier=mocked_networking["subnet1"], - ) - conn.create_auto_scaling_group(group) - return group - - def setup_autoscale_group_boto3(): mocked_networking = setup_networking() client = boto3.client("autoscaling", region_name="us-east-1") @@ -49,28 +26,6 @@ def setup_autoscale_group_boto3(): ) -# Has boto3 equivalent -@mock_autoscaling_deprecated -def test_create_policy(): - setup_autoscale_group() - conn = boto.connect_autoscale() - policy = ScalingPolicy( - name="ScaleUp", - adjustment_type="ExactCapacity", - as_name="tester_group", - scaling_adjustment=3, - cooldown=60, - ) - conn.create_scaling_policy(policy) - - policy = conn.get_all_policies()[0] - policy.name.should.equal("ScaleUp") - policy.adjustment_type.should.equal("ExactCapacity") - policy.as_name.should.equal("tester_group") - policy.scaling_adjustment.should.equal(3) - policy.cooldown.should.equal(60) - - @mock_autoscaling def test_create_policy_boto3(): setup_autoscale_group_boto3() @@ -91,26 +46,6 @@ def test_create_policy_boto3(): policy["Cooldown"].should.equal(60) -# Has boto3 equivalent -@mock_autoscaling_deprecated -def test_create_policy_default_values(): - setup_autoscale_group() - conn = boto.connect_autoscale() - policy = ScalingPolicy( - name="ScaleUp", - adjustment_type="ExactCapacity", - as_name="tester_group", - scaling_adjustment=3, - ) - conn.create_scaling_policy(policy) - - policy = conn.get_all_policies()[0] - policy.name.should.equal("ScaleUp") - - # Defaults - policy.cooldown.should.equal(300) - - @mock_autoscaling def test_create_policy_default_values_boto3(): setup_autoscale_group_boto3() @@ -129,34 +64,6 @@ def test_create_policy_default_values_boto3(): policy["Cooldown"].should.equal(300) -# Has boto3 equivalent -@mock_autoscaling_deprecated -def test_update_policy(): - setup_autoscale_group() - conn = boto.connect_autoscale() - policy = ScalingPolicy( - name="ScaleUp", - adjustment_type="ExactCapacity", - as_name="tester_group", - scaling_adjustment=3, - ) - conn.create_scaling_policy(policy) - - policy = conn.get_all_policies()[0] - policy.scaling_adjustment.should.equal(3) - - # Now update it by creating another with the same name - policy = ScalingPolicy( - name="ScaleUp", - adjustment_type="ExactCapacity", - as_name="tester_group", - scaling_adjustment=2, - ) - conn.create_scaling_policy(policy) - policy = conn.get_all_policies()[0] - policy.scaling_adjustment.should.equal(2) - - @mock_autoscaling def test_update_policy_boto3(): setup_autoscale_group_boto3() @@ -184,25 +91,6 @@ def test_update_policy_boto3(): policy["ScalingAdjustment"].should.equal(2) -# Has boto3 equivalent -@mock_autoscaling_deprecated -def test_delete_policy(): - setup_autoscale_group() - conn = boto.connect_autoscale() - policy = ScalingPolicy( - name="ScaleUp", - adjustment_type="ExactCapacity", - as_name="tester_group", - scaling_adjustment=3, - ) - conn.create_scaling_policy(policy) - - conn.get_all_policies().should.have.length_of(1) - - conn.delete_policy("ScaleUp") - conn.get_all_policies().should.have.length_of(0) - - @mock_autoscaling def test_delete_policy_boto3(): setup_autoscale_group_boto3() @@ -220,25 +108,6 @@ def test_delete_policy_boto3(): client.describe_policies()["ScalingPolicies"].should.have.length_of(0) -# Has boto3 equivalent -@mock_autoscaling_deprecated -def test_execute_policy_exact_capacity(): - setup_autoscale_group() - conn = boto.connect_autoscale() - policy = ScalingPolicy( - name="ScaleUp", - adjustment_type="ExactCapacity", - as_name="tester_group", - scaling_adjustment=3, - ) - conn.create_scaling_policy(policy) - - conn.execute_policy("ScaleUp") - - instances = list(conn.get_all_autoscaling_instances()) - instances.should.have.length_of(3) - - @mock_autoscaling def test_execute_policy_exact_capacity_boto3(): setup_autoscale_group_boto3() @@ -256,25 +125,6 @@ def test_execute_policy_exact_capacity_boto3(): instances["AutoScalingInstances"].should.have.length_of(3) -# Has boto3 equivalent -@mock_autoscaling_deprecated -def test_execute_policy_positive_change_in_capacity(): - setup_autoscale_group() - conn = boto.connect_autoscale() - policy = ScalingPolicy( - name="ScaleUp", - adjustment_type="ChangeInCapacity", - as_name="tester_group", - scaling_adjustment=3, - ) - conn.create_scaling_policy(policy) - - conn.execute_policy("ScaleUp") - - instances = list(conn.get_all_autoscaling_instances()) - instances.should.have.length_of(5) - - @mock_autoscaling def test_execute_policy_positive_change_in_capacity_boto3(): setup_autoscale_group_boto3() @@ -292,25 +142,6 @@ def test_execute_policy_positive_change_in_capacity_boto3(): instances["AutoScalingInstances"].should.have.length_of(5) -# Has boto3 equivalent -@mock_autoscaling_deprecated -def test_execute_policy_percent_change_in_capacity(): - setup_autoscale_group() - conn = boto.connect_autoscale() - policy = ScalingPolicy( - name="ScaleUp", - adjustment_type="PercentChangeInCapacity", - as_name="tester_group", - scaling_adjustment=50, - ) - conn.create_scaling_policy(policy) - - conn.execute_policy("ScaleUp") - - instances = list(conn.get_all_autoscaling_instances()) - instances.should.have.length_of(3) - - @pytest.mark.parametrize( "adjustment,nr_of_instances", [(1, 3), (50, 3), (100, 4), (250, 7)], ) @@ -332,25 +163,3 @@ def test_execute_policy_percent_change_in_capacity_boto3(adjustment, nr_of_insta instances = client.describe_auto_scaling_instances() instances["AutoScalingInstances"].should.have.length_of(nr_of_instances) - - -# Has boto3 equivalent -@mock_autoscaling_deprecated -def test_execute_policy_small_percent_change_in_capacity(): - """http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/as-scale-based-on-demand.html - If PercentChangeInCapacity returns a value between 0 and 1, - Auto Scaling will round it off to 1.""" - setup_autoscale_group() - conn = boto.connect_autoscale() - policy = ScalingPolicy( - name="ScaleUp", - adjustment_type="PercentChangeInCapacity", - as_name="tester_group", - scaling_adjustment=1, - ) - conn.create_scaling_policy(policy) - - conn.execute_policy("ScaleUp") - - instances = list(conn.get_all_autoscaling_instances()) - instances.should.have.length_of(3) diff --git a/tests/test_autoscaling/utils.py b/tests/test_autoscaling/utils.py index 96a192747..914bc2b47 100644 --- a/tests/test_autoscaling/utils.py +++ b/tests/test_autoscaling/utils.py @@ -1,6 +1,5 @@ import boto3 -from boto import vpc as boto_vpc -from moto import mock_ec2, mock_ec2_deprecated +from moto import mock_ec2 @mock_ec2 @@ -16,15 +15,6 @@ def setup_networking(region_name="us-east-1"): return {"vpc": vpc.id, "subnet1": subnet1.id, "subnet2": subnet2.id} -@mock_ec2_deprecated -def setup_networking_deprecated(): - conn = boto_vpc.connect_to_region("us-east-1") - vpc = conn.create_vpc("10.11.0.0/16") - subnet1 = conn.create_subnet(vpc.id, "10.11.1.0/24", availability_zone="us-east-1a") - subnet2 = conn.create_subnet(vpc.id, "10.11.2.0/24", availability_zone="us-east-1b") - return {"vpc": vpc.id, "subnet1": subnet1.id, "subnet2": subnet2.id} - - @mock_ec2 def setup_instance_with_networking(image_id, instance_type): mock_data = setup_networking() diff --git a/tests/test_cloudformation/test_cloudformation_stack_crud.py b/tests/test_cloudformation/test_cloudformation_stack_crud.py deleted file mode 100644 index 82086434b..000000000 --- a/tests/test_cloudformation/test_cloudformation_stack_crud.py +++ /dev/null @@ -1,772 +0,0 @@ -import os -import json - -import boto -import boto.dynamodb2 -import boto.iam -import boto.s3 -import boto.s3.key -import boto.cloudformation -from boto.exception import BotoServerError -import sure # noqa # pylint: disable=unused-import -from freezegun import freeze_time - -import pytest - -from moto import ( - mock_cloudformation_deprecated, - mock_s3_deprecated, - mock_sns_deprecated, - mock_sqs_deprecated, - mock_route53_deprecated, - mock_iam_deprecated, - mock_dynamodb2_deprecated, -) -from moto.cloudformation import cloudformation_backends - -dummy_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Description": "Stack 1", - "Resources": {}, -} - -dummy_template2 = { - "AWSTemplateFormatVersion": "2010-09-09", - "Description": "Stack 2", - "Resources": {}, -} - -# template with resource which has no delete attribute defined -dummy_template3 = { - "AWSTemplateFormatVersion": "2010-09-09", - "Description": "Stack 3", - "Resources": { - "VPC": {"Properties": {"CidrBlock": "192.168.0.0/16"}, "Type": "AWS::EC2::VPC"} - }, -} - -dummy_template4 = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "myDynamoDBTable": { - "Type": "AWS::DynamoDB::Table", - "Properties": { - "AttributeDefinitions": [ - {"AttributeName": "Name", "AttributeType": "S"}, - {"AttributeName": "Age", "AttributeType": "S"}, - ], - "KeySchema": [ - {"AttributeName": "Name", "KeyType": "HASH"}, - {"AttributeName": "Age", "KeyType": "RANGE"}, - ], - "ProvisionedThroughput": { - "ReadCapacityUnits": 5, - "WriteCapacityUnits": 5, - }, - "TableName": "Person", - }, - } - }, -} - -dummy_template_json = json.dumps(dummy_template) -dummy_template_json2 = json.dumps(dummy_template2) -dummy_template_json3 = json.dumps(dummy_template3) -dummy_template_json4 = json.dumps(dummy_template4) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_create_stack(): - conn = boto.connect_cloudformation() - conn.create_stack("test_stack", template_body=dummy_template_json) - - stack = conn.describe_stacks()[0] - stack.stack_id.should.contain( - "arn:aws:cloudformation:us-east-1:123456789:stack/test_stack/" - ) - stack.stack_name.should.equal("test_stack") - stack.get_template().should.equal( - { - "GetTemplateResponse": { - "GetTemplateResult": { - "TemplateBody": dummy_template_json, - "ResponseMetadata": { - "RequestId": "2d06e36c-ac1d-11e0-a958-f9382b6eb86bEXAMPLE" - }, - } - } - } - ) - - -@mock_cloudformation_deprecated -def test_create_stack_with_other_region(): - conn = boto.cloudformation.connect_to_region("us-west-2") - conn.create_stack("test_stack", template_body=dummy_template_json) - - stack = conn.describe_stacks()[0] - stack.stack_id.should.contain( - "arn:aws:cloudformation:us-west-2:123456789:stack/test_stack/" - ) - stack.stack_name.should.equal("test_stack") - stack.get_template().should.equal( - { - "GetTemplateResponse": { - "GetTemplateResult": { - "TemplateBody": dummy_template_json, - "ResponseMetadata": { - "RequestId": "2d06e36c-ac1d-11e0-a958-f9382b6eb86bEXAMPLE" - }, - } - } - } - ) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -@mock_route53_deprecated -def test_create_stack_hosted_zone_by_id(): - conn = boto.connect_cloudformation() - dummy_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Description": "Stack 1", - "Parameters": {}, - "Resources": { - "Bar": { - "Type": "AWS::Route53::HostedZone", - "Properties": {"Name": "foo.bar.baz"}, - } - }, - } - dummy_template2 = { - "AWSTemplateFormatVersion": "2010-09-09", - "Description": "Stack 2", - "Parameters": {"ZoneId": {"Type": "String"}}, - "Resources": { - "Foo": { - "Properties": {"HostedZoneId": {"Ref": "ZoneId"}, "RecordSets": []}, - "Type": "AWS::Route53::RecordSetGroup", - } - }, - } - conn.create_stack( - "test_stack1", template_body=json.dumps(dummy_template), parameters={}.items() - ) - r53_conn = boto.connect_route53() - zone_id = r53_conn.get_zones()[0].id - conn.create_stack( - "test_stack2", - template_body=json.dumps(dummy_template2), - parameters={"ZoneId": zone_id}.items(), - ) - - stack = conn.describe_stacks()[0] - assert stack.list_resources() - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_creating_stacks_across_regions(): - west1_conn = boto.cloudformation.connect_to_region("us-west-1") - west1_conn.create_stack("test_stack", template_body=dummy_template_json) - - west2_conn = boto.cloudformation.connect_to_region("us-west-2") - west2_conn.create_stack("test_stack", template_body=dummy_template_json) - - list(west1_conn.describe_stacks()).should.have.length_of(1) - list(west2_conn.describe_stacks()).should.have.length_of(1) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -@mock_sns_deprecated -@mock_sqs_deprecated -def test_create_stack_with_notification_arn(): - sqs_conn = boto.connect_sqs() - queue = sqs_conn.create_queue("fake-queue", visibility_timeout=3) - queue_arn = queue.get_attributes()["QueueArn"] - - sns_conn = boto.connect_sns() - topic = sns_conn.create_topic("fake-topic") - topic_arn = topic["CreateTopicResponse"]["CreateTopicResult"]["TopicArn"] - - sns_conn.subscribe(topic_arn, "sqs", queue_arn) - - conn = boto.connect_cloudformation() - with freeze_time("2015-01-01 12:00:00"): - conn.create_stack( - "test_stack_with_notifications", - template_body=dummy_template_json, - notification_arns=topic_arn, - ) - - stack = conn.describe_stacks()[0] - [n.value for n in stack.notification_arns].should.contain(topic_arn) - - with freeze_time("2015-01-01 12:00:01"): - message = queue.read(1) - - msg = json.loads(message.get_body()) - msg["Subject"].should.equal("AWS CloudFormation Notification") - msg["Message"].should.contain("StackId='{}'\n".format(stack.stack_id)) - msg["Message"].should.contain("Timestamp='2015-01-01T12:00:00.000Z'\n") - msg["Message"].should.contain("LogicalResourceId='test_stack_with_notifications'\n") - msg["Message"].should.contain("ResourceStatus='CREATE_IN_PROGRESS'\n") - msg["Message"].should.contain("ResourceStatusReason='User Initiated'\n") - msg["Message"].should.contain("ResourceType='AWS::CloudFormation::Stack'\n") - msg["Message"].should.contain("StackName='test_stack_with_notifications'\n") - msg.should.have.key("MessageId") - msg.should.have.key("Signature") - msg.should.have.key("SignatureVersion") - msg.should.have.key("Subject") - msg["Timestamp"].should.equal("2015-01-01T12:00:00.000Z") - msg["TopicArn"].should.equal(topic_arn) - msg.should.have.key("Type") - msg.should.have.key("UnsubscribeURL") - - with freeze_time("2015-01-01 12:00:02"): - message = queue.read(1) - - msg = json.loads(message.get_body()) - msg["Message"].should.contain("StackId='{}'\n".format(stack.stack_id)) - msg["Message"].should.contain("Timestamp='2015-01-01T12:00:00.000Z'\n") - msg["Message"].should.contain("LogicalResourceId='test_stack_with_notifications'\n") - msg["Message"].should.contain("ResourceStatus='CREATE_COMPLETE'\n") - msg["Message"].should.contain("ResourceStatusReason='None'\n") - msg["Message"].should.contain("ResourceType='AWS::CloudFormation::Stack'\n") - msg["Message"].should.contain("StackName='test_stack_with_notifications'\n") - msg.should.have.key("MessageId") - msg.should.have.key("Signature") - msg.should.have.key("SignatureVersion") - msg.should.have.key("Subject") - msg["Timestamp"].should.equal("2015-01-01T12:00:00.000Z") - msg["TopicArn"].should.equal(topic_arn) - msg.should.have.key("Type") - msg.should.have.key("UnsubscribeURL") - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -@mock_s3_deprecated -def test_create_stack_from_s3_url(): - s3_conn = boto.s3.connect_to_region("us-west-1") - bucket = s3_conn.create_bucket("foobar", location="us-west-1") - key = boto.s3.key.Key(bucket) - key.key = "template-key" - key.set_contents_from_string(dummy_template_json) - key_url = key.generate_url(expires_in=0, query_auth=False) - - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("new-stack", template_url=key_url) - - stack = conn.describe_stacks()[0] - stack.stack_name.should.equal("new-stack") - stack.get_template().should.equal( - { - "GetTemplateResponse": { - "GetTemplateResult": { - "TemplateBody": dummy_template_json, - "ResponseMetadata": { - "RequestId": "2d06e36c-ac1d-11e0-a958-f9382b6eb86bEXAMPLE" - }, - } - } - } - ) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_describe_stack_by_name(): - conn = boto.connect_cloudformation() - conn.create_stack("test_stack", template_body=dummy_template_json) - - stack = conn.describe_stacks("test_stack")[0] - stack.stack_name.should.equal("test_stack") - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_describe_stack_by_stack_id(): - conn = boto.connect_cloudformation() - conn.create_stack("test_stack", template_body=dummy_template_json) - - stack = conn.describe_stacks("test_stack")[0] - stack_by_id = conn.describe_stacks(stack.stack_id)[0] - stack_by_id.stack_id.should.equal(stack.stack_id) - stack_by_id.stack_name.should.equal("test_stack") - - -@mock_dynamodb2_deprecated -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_delete_stack_dynamo_template(): - conn = boto.connect_cloudformation() - db_conn = boto.dynamodb2.connect_to_region("us-east-1") - # - conn.create_stack("test_stack", template_body=dummy_template_json4) - db_conn.list_tables()["TableNames"].should.have.length_of(1) - # - conn.delete_stack("test_stack") - db_conn.list_tables()["TableNames"].should.have.length_of(0) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_describe_deleted_stack(): - conn = boto.connect_cloudformation() - conn.create_stack("test_stack", template_body=dummy_template_json) - - stack = conn.describe_stacks("test_stack")[0] - stack_id = stack.stack_id - conn.delete_stack(stack.stack_id) - stack_by_id = conn.describe_stacks(stack_id)[0] - stack_by_id.stack_id.should.equal(stack.stack_id) - stack_by_id.stack_name.should.equal("test_stack") - stack_by_id.stack_status.should.equal("DELETE_COMPLETE") - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_get_template_by_name(): - conn = boto.connect_cloudformation() - conn.create_stack("test_stack", template_body=dummy_template_json) - - template = conn.get_template("test_stack") - template.should.equal( - { - "GetTemplateResponse": { - "GetTemplateResult": { - "TemplateBody": dummy_template_json, - "ResponseMetadata": { - "RequestId": "2d06e36c-ac1d-11e0-a958-f9382b6eb86bEXAMPLE" - }, - } - } - } - ) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_list_stacks(): - conn = boto.connect_cloudformation() - conn.create_stack("test_stack", template_body=dummy_template_json) - conn.create_stack("test_stack2", template_body=dummy_template_json) - - stacks = conn.list_stacks() - stacks.should.have.length_of(2) - stacks[0].template_description.should.equal("Stack 1") - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_list_stacks_with_filter(): - conn = boto.connect_cloudformation() - conn.create_stack("test_stack", template_body=dummy_template_json) - conn.create_stack("test_stack2", template_body=dummy_template_json) - conn.update_stack("test_stack", template_body=dummy_template_json2) - stacks = conn.list_stacks("CREATE_COMPLETE") - stacks.should.have.length_of(1) - stacks[0].template_description.should.equal("Stack 1") - stacks = conn.list_stacks("UPDATE_COMPLETE") - stacks.should.have.length_of(1) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_delete_stack_by_name(): - conn = boto.connect_cloudformation() - conn.create_stack("test_stack", template_body=dummy_template_json) - - conn.describe_stacks().should.have.length_of(1) - conn.delete_stack("test_stack") - conn.describe_stacks().should.have.length_of(0) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_delete_stack_by_id(): - conn = boto.connect_cloudformation() - stack_id = conn.create_stack("test_stack", template_body=dummy_template_json) - - conn.describe_stacks().should.have.length_of(1) - conn.delete_stack(stack_id) - conn.describe_stacks().should.have.length_of(0) - with pytest.raises(BotoServerError): - conn.describe_stacks("test_stack") - - conn.describe_stacks(stack_id).should.have.length_of(1) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_delete_stack_with_resource_missing_delete_attr(): - conn = boto.connect_cloudformation() - conn.create_stack("test_stack", template_body=dummy_template_json3) - - conn.describe_stacks().should.have.length_of(1) - conn.delete_stack("test_stack") - conn.describe_stacks().should.have.length_of(0) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_bad_describe_stack(): - conn = boto.connect_cloudformation() - with pytest.raises(BotoServerError): - conn.describe_stacks("bad_stack") - - -# Has boto3 equivalent -@mock_cloudformation_deprecated() -def test_cloudformation_params(): - dummy_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Description": "Stack 1", - "Resources": {}, - "Parameters": { - "APPNAME": { - "Default": "app-name", - "Description": "The name of the app", - "Type": "String", - } - }, - } - dummy_template_json = json.dumps(dummy_template) - cfn = boto.connect_cloudformation() - cfn.create_stack( - "test_stack1", - template_body=dummy_template_json, - parameters=[("APPNAME", "testing123")], - ) - stack = cfn.describe_stacks("test_stack1")[0] - stack.parameters.should.have.length_of(1) - param = stack.parameters[0] - param.key.should.equal("APPNAME") - param.value.should.equal("testing123") - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_cloudformation_params_conditions_and_resources_are_distinct(): - dummy_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Description": "Stack 1", - "Conditions": { - "FooEnabled": {"Fn::Equals": [{"Ref": "FooEnabled"}, "true"]}, - "FooDisabled": { - "Fn::Not": [{"Fn::Equals": [{"Ref": "FooEnabled"}, "true"]}] - }, - }, - "Parameters": { - "FooEnabled": {"Type": "String", "AllowedValues": ["true", "false"]} - }, - "Resources": { - "Bar": { - "Properties": {"CidrBlock": "192.168.0.0/16"}, - "Condition": "FooDisabled", - "Type": "AWS::EC2::VPC", - } - }, - } - dummy_template_json = json.dumps(dummy_template) - cfn = boto.connect_cloudformation() - cfn.create_stack( - "test_stack1", - template_body=dummy_template_json, - parameters=[("FooEnabled", "true")], - ) - stack = cfn.describe_stacks("test_stack1")[0] - resources = stack.list_resources() - assert not [ - resource for resource in resources if resource.logical_resource_id == "Bar" - ] - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_stack_tags(): - conn = boto.connect_cloudformation() - conn.create_stack( - "test_stack", - template_body=dummy_template_json, - tags={"foo": "bar", "baz": "bleh"}, - ) - - stack = conn.describe_stacks()[0] - dict(stack.tags).should.equal({"foo": "bar", "baz": "bleh"}) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_update_stack(): - conn = boto.connect_cloudformation() - conn.create_stack("test_stack", template_body=dummy_template_json) - - conn.update_stack("test_stack", dummy_template_json2) - - stack = conn.describe_stacks()[0] - stack.stack_status.should.equal("UPDATE_COMPLETE") - stack.get_template().should.equal( - { - "GetTemplateResponse": { - "GetTemplateResult": { - "TemplateBody": dummy_template_json2, - "ResponseMetadata": { - "RequestId": "2d06e36c-ac1d-11e0-a958-f9382b6eb86bEXAMPLE" - }, - } - } - } - ) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_update_stack_with_previous_template(): - conn = boto.connect_cloudformation() - conn.create_stack("test_stack", template_body=dummy_template_json) - conn.update_stack("test_stack", use_previous_template=True) - - stack = conn.describe_stacks()[0] - stack.stack_status.should.equal("UPDATE_COMPLETE") - stack.get_template().should.equal( - { - "GetTemplateResponse": { - "GetTemplateResult": { - "TemplateBody": dummy_template_json, - "ResponseMetadata": { - "RequestId": "2d06e36c-ac1d-11e0-a958-f9382b6eb86bEXAMPLE" - }, - } - } - } - ) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_update_stack_with_parameters(): - dummy_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Description": "Stack", - "Resources": { - "VPC": { - "Properties": {"CidrBlock": {"Ref": "Bar"}}, - "Type": "AWS::EC2::VPC", - } - }, - "Parameters": {"Bar": {"Type": "String"}}, - } - dummy_template_json = json.dumps(dummy_template) - conn = boto.connect_cloudformation() - conn.create_stack( - "test_stack", - template_body=dummy_template_json, - parameters=[("Bar", "192.168.0.0/16")], - ) - conn.update_stack( - "test_stack", - template_body=dummy_template_json, - parameters=[("Bar", "192.168.0.1/16")], - ) - - stack = conn.describe_stacks()[0] - assert stack.parameters[0].value == "192.168.0.1/16" - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_update_stack_replace_tags(): - conn = boto.connect_cloudformation() - conn.create_stack( - "test_stack", template_body=dummy_template_json, tags={"foo": "bar"} - ) - conn.update_stack( - "test_stack", template_body=dummy_template_json, tags={"foo": "baz"} - ) - - stack = conn.describe_stacks()[0] - stack.stack_status.should.equal("UPDATE_COMPLETE") - # since there is one tag it doesn't come out as a list - dict(stack.tags).should.equal({"foo": "baz"}) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_update_stack_when_rolled_back(): - conn = boto.connect_cloudformation() - stack_id = conn.create_stack("test_stack", template_body=dummy_template_json) - - cloudformation_backends[conn.region.name].stacks[ - stack_id - ].status = "ROLLBACK_COMPLETE" - - with pytest.raises(BotoServerError) as err: - conn.update_stack("test_stack", dummy_template_json) - - ex = err.value - ex.body.should.match(r"is in ROLLBACK_COMPLETE state and can not be updated") - ex.error_code.should.equal("ValidationError") - ex.reason.should.equal("Bad Request") - ex.status.should.equal(400) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_describe_stack_events_shows_create_update_and_delete(): - conn = boto.connect_cloudformation() - stack_id = conn.create_stack("test_stack", template_body=dummy_template_json) - conn.update_stack(stack_id, template_body=dummy_template_json2) - conn.delete_stack(stack_id) - - # assert begins and ends with stack events - events = conn.describe_stack_events(stack_id) - events[0].resource_type.should.equal("AWS::CloudFormation::Stack") - events[-1].resource_type.should.equal("AWS::CloudFormation::Stack") - - # testing ordering of stack events without assuming resource events will not exist - # the AWS API returns events in reverse chronological order - stack_events_to_look_for = iter( - [ - ("DELETE_COMPLETE", None), - ("DELETE_IN_PROGRESS", "User Initiated"), - ("UPDATE_COMPLETE", None), - ("UPDATE_IN_PROGRESS", "User Initiated"), - ("CREATE_COMPLETE", None), - ("CREATE_IN_PROGRESS", "User Initiated"), - ] - ) - try: - for event in events: - event.stack_id.should.equal(stack_id) - event.stack_name.should.equal("test_stack") - event.event_id.should.match(r"[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}") - - if event.resource_type == "AWS::CloudFormation::Stack": - event.logical_resource_id.should.equal("test_stack") - event.physical_resource_id.should.equal(stack_id) - - status_to_look_for, reason_to_look_for = next(stack_events_to_look_for) - event.resource_status.should.equal(status_to_look_for) - if reason_to_look_for is not None: - event.resource_status_reason.should.equal(reason_to_look_for) - except StopIteration: - assert False, "Too many stack events" - - list(stack_events_to_look_for).should.be.empty - - with pytest.raises(BotoServerError) as exp: - conn.describe_stack_events("non_existing_stack") - err = exp.value - err.message.should.equal("Stack with id non_existing_stack does not exist") - err.body.should.match(r"Stack with id non_existing_stack does not exist") - err.error_code.should.equal("ValidationError") - err.reason.should.equal("Bad Request") - err.status.should.equal(400) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_create_stack_lambda_and_dynamodb(): - conn = boto.connect_cloudformation() - dummy_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Description": "Stack Lambda Test 1", - "Parameters": {}, - "Resources": { - "func1": { - "Type": "AWS::Lambda::Function", - "Properties": { - "Code": {"S3Bucket": "bucket_123", "S3Key": "key_123"}, - "FunctionName": "func1", - "Handler": "handler.handler", - "Role": get_role_name(), - "Runtime": "python2.7", - "Description": "descr", - "MemorySize": 12345, - }, - }, - "func1version": { - "Type": "AWS::Lambda::Version", - "Properties": {"FunctionName": {"Ref": "func1"}}, - }, - "tab1": { - "Type": "AWS::DynamoDB::Table", - "Properties": { - "TableName": "tab1", - "KeySchema": [{"AttributeName": "attr1", "KeyType": "HASH"}], - "AttributeDefinitions": [ - {"AttributeName": "attr1", "AttributeType": "string"} - ], - "ProvisionedThroughput": { - "ReadCapacityUnits": 10, - "WriteCapacityUnits": 10, - }, - "StreamSpecification": {"StreamViewType": "KEYS_ONLY"}, - }, - }, - "func1mapping": { - "Type": "AWS::Lambda::EventSourceMapping", - "Properties": { - "FunctionName": {"Ref": "func1"}, - "EventSourceArn": {"Fn::GetAtt": ["tab1", "StreamArn"]}, - "StartingPosition": "0", - "BatchSize": 100, - "Enabled": True, - }, - }, - }, - } - validate_s3_before = os.environ.get("VALIDATE_LAMBDA_S3", "") - try: - os.environ["VALIDATE_LAMBDA_S3"] = "false" - conn.create_stack( - "test_stack_lambda_1", - template_body=json.dumps(dummy_template), - parameters={}.items(), - ) - finally: - os.environ["VALIDATE_LAMBDA_S3"] = validate_s3_before - - stack = conn.describe_stacks()[0] - resources = stack.list_resources() - assert len(resources) == 4 - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -def test_create_stack_kinesis(): - conn = boto.connect_cloudformation() - dummy_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Description": "Stack Kinesis Test 1", - "Parameters": {}, - "Resources": { - "stream1": { - "Type": "AWS::Kinesis::Stream", - "Properties": {"Name": "stream1", "ShardCount": 2}, - } - }, - } - conn.create_stack( - "test_stack_kinesis_1", - template_body=json.dumps(dummy_template), - parameters={}.items(), - ) - - stack = conn.describe_stacks()[0] - resources = stack.list_resources() - assert len(resources) == 1 - - -def get_role_name(): - with mock_iam_deprecated(): - iam = boto.connect_iam() - role = iam.create_role("my-role")["create_role_response"]["create_role_result"][ - "role" - ]["arn"] - return role diff --git a/tests/test_cloudformation/test_cloudformation_stack_crud_boto3.py b/tests/test_cloudformation/test_cloudformation_stack_crud_boto3.py index ea2341ff5..21f2d29fd 100644 --- a/tests/test_cloudformation/test_cloudformation_stack_crud_boto3.py +++ b/tests/test_cloudformation/test_cloudformation_stack_crud_boto3.py @@ -25,7 +25,6 @@ from moto import ( from moto import settings from moto.core import ACCOUNT_ID from moto.cloudformation import cloudformation_backends -from .test_cloudformation_stack_crud import dummy_template_json2, dummy_template_json4 from tests import EXAMPLE_AMI_ID @@ -48,6 +47,12 @@ dummy_template = { }, } +dummy_template2 = { + "AWSTemplateFormatVersion": "2010-09-09", + "Description": "Stack 2", + "Resources": {}, +} + dummy_template3 = { "AWSTemplateFormatVersion": "2010-09-09", "Description": "Stack 3", @@ -56,6 +61,29 @@ dummy_template3 = { }, } +dummy_template4 = { + "AWSTemplateFormatVersion": "2010-09-09", + "Resources": { + "myDynamoDBTable": { + "Type": "AWS::DynamoDB::Table", + "Properties": { + "AttributeDefinitions": [ + {"AttributeName": "Name", "AttributeType": "S"}, + {"AttributeName": "Age", "AttributeType": "S"}, + ], + "KeySchema": [ + {"AttributeName": "Name", "KeyType": "HASH"}, + {"AttributeName": "Age", "KeyType": "RANGE"}, + ], + "ProvisionedThroughput": { + "ReadCapacityUnits": 5, + "WriteCapacityUnits": 5, + }, + "TableName": "Person", + }, + } + }, +} dummy_template_with_parameters = { "AWSTemplateFormatVersion": "2010-09-09", @@ -278,6 +306,8 @@ dummy_update_template_json = json.dumps(dummy_update_template) dummy_output_template_json = json.dumps(dummy_output_template) dummy_import_template_json = json.dumps(dummy_import_template) dummy_redrive_template_json = json.dumps(dummy_redrive_template) +dummy_template_json2 = json.dumps(dummy_template2) +dummy_template_json4 = json.dumps(dummy_template4) dummy_unknown_template_json = json.dumps(dummy_unknown_template) diff --git a/tests/test_cloudformation/test_cloudformation_stack_integration.py b/tests/test_cloudformation/test_cloudformation_stack_integration.py index 63ec03959..d865cfce0 100644 --- a/tests/test_cloudformation/test_cloudformation_stack_integration.py +++ b/tests/test_cloudformation/test_cloudformation_stack_integration.py @@ -4,1013 +4,34 @@ import zipfile from decimal import Decimal -import boto -import boto.cloudformation -import boto.datapipeline -import boto.ec2 -import boto.ec2.autoscale -import boto.ec2.elb -from boto.exception import BotoServerError from botocore.exceptions import ClientError -import boto.iam -import boto.rds -import boto.redshift -import boto.sns -import boto.sqs -import boto.vpc import boto3 import sure # noqa # pylint: disable=unused-import import pytest -from copy import deepcopy from string import Template from moto import ( - mock_autoscaling_deprecated, mock_autoscaling, mock_cloudformation, - mock_cloudformation_deprecated, - mock_datapipeline_deprecated, mock_dynamodb2, mock_ec2, - mock_ec2_deprecated, - mock_elb_deprecated, mock_events, - mock_iam_deprecated, mock_kms, mock_lambda, mock_logs, - mock_rds_deprecated, - mock_redshift_deprecated, - mock_route53_deprecated, mock_s3, - mock_sns_deprecated, mock_sqs, - mock_sqs_deprecated, mock_elbv2, ) from moto.core import ACCOUNT_ID from tests import EXAMPLE_AMI_ID, EXAMPLE_AMI_ID2 from tests.test_cloudformation.fixtures import ( - ec2_classic_eip, fn_join, - rds_mysql_with_read_replica, - redshift, - route53_ec2_instance_with_public_ip, - route53_health_check, - route53_roundrobin, single_instance_with_ebs_volume, - vpc_eip, - vpc_single_instance_in_subnet, ) -# Has boto3 equivalent -@mock_cloudformation_deprecated() -def test_stack_sqs_integration(): - sqs_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "QueueGroup": { - "Type": "AWS::SQS::Queue", - "Properties": {"QueueName": "my-queue", "VisibilityTimeout": 60}, - } - }, - } - sqs_template_json = json.dumps(sqs_template) - - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("test_stack", template_body=sqs_template_json) - - stack = conn.describe_stacks()[0] - queue = stack.describe_resources()[0] - queue.resource_type.should.equal("AWS::SQS::Queue") - queue.logical_resource_id.should.equal("QueueGroup") - queue.physical_resource_id.should.equal("my-queue") - - -# Has boto3 equivalent -@mock_cloudformation_deprecated() -def test_stack_list_resources(): - sqs_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "QueueGroup": { - "Type": "AWS::SQS::Queue", - "Properties": {"QueueName": "my-queue", "VisibilityTimeout": 60}, - } - }, - } - sqs_template_json = json.dumps(sqs_template) - - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("test_stack", template_body=sqs_template_json) - - resources = conn.list_stack_resources("test_stack") - assert len(resources) == 1 - queue = resources[0] - queue.resource_type.should.equal("AWS::SQS::Queue") - queue.logical_resource_id.should.equal("QueueGroup") - queue.physical_resource_id.should.equal("my-queue") - - -# Has boto3 equivalent -@mock_cloudformation_deprecated() -@mock_sqs_deprecated() -def test_update_stack(): - sqs_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "QueueGroup": { - "Type": "AWS::SQS::Queue", - "Properties": {"QueueName": "my-queue", "VisibilityTimeout": 60}, - } - }, - } - sqs_template_json = json.dumps(sqs_template) - - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("test_stack", template_body=sqs_template_json) - - sqs_conn = boto.sqs.connect_to_region("us-west-1") - queues = sqs_conn.get_all_queues() - queues.should.have.length_of(1) - queues[0].get_attributes("VisibilityTimeout")["VisibilityTimeout"].should.equal( - "60" - ) - - sqs_template["Resources"]["QueueGroup"]["Properties"]["VisibilityTimeout"] = 100 - sqs_template_json = json.dumps(sqs_template) - conn.update_stack("test_stack", sqs_template_json) - - queues = sqs_conn.get_all_queues() - queues.should.have.length_of(1) - queues[0].get_attributes("VisibilityTimeout")["VisibilityTimeout"].should.equal( - "100" - ) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated() -@mock_sqs_deprecated() -def test_update_stack_and_remove_resource(): - sqs_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "QueueGroup": { - "Type": "AWS::SQS::Queue", - "Properties": {"QueueName": "my-queue", "VisibilityTimeout": 60}, - } - }, - } - sqs_template_json = json.dumps(sqs_template) - - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("test_stack", template_body=sqs_template_json) - - sqs_conn = boto.sqs.connect_to_region("us-west-1") - queues = sqs_conn.get_all_queues() - queues.should.have.length_of(1) - - sqs_template["Resources"].pop("QueueGroup") - sqs_template_json = json.dumps(sqs_template) - conn.update_stack("test_stack", sqs_template_json) - - queues = sqs_conn.get_all_queues() - queues.should.have.length_of(0) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated() -@mock_sqs_deprecated() -def test_update_stack_and_add_resource(): - sqs_template = {"AWSTemplateFormatVersion": "2010-09-09", "Resources": {}} - sqs_template_json = json.dumps(sqs_template) - - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("test_stack", template_body=sqs_template_json) - - sqs_conn = boto.sqs.connect_to_region("us-west-1") - queues = sqs_conn.get_all_queues() - queues.should.have.length_of(0) - - sqs_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "QueueGroup": { - "Type": "AWS::SQS::Queue", - "Properties": {"QueueName": "my-queue", "VisibilityTimeout": 60}, - } - }, - } - sqs_template_json = json.dumps(sqs_template) - conn.update_stack("test_stack", sqs_template_json) - - queues = sqs_conn.get_all_queues() - queues.should.have.length_of(1) - - -# Has boto3 equivalent -@mock_ec2_deprecated() -@mock_cloudformation_deprecated() -def test_stack_ec2_integration(): - ec2_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "WebServerGroup": { - "Type": "AWS::EC2::Instance", - "Properties": {"ImageId": EXAMPLE_AMI_ID, "UserData": "some user data"}, - } - }, - } - ec2_template_json = json.dumps(ec2_template) - - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("ec2_stack", template_body=ec2_template_json) - - ec2_conn = boto.ec2.connect_to_region("us-west-1") - reservation = ec2_conn.get_all_reservations()[0] - ec2_instance = reservation.instances[0] - - stack = conn.describe_stacks()[0] - instance = stack.describe_resources()[0] - instance.resource_type.should.equal("AWS::EC2::Instance") - instance.logical_resource_id.should.contain("WebServerGroup") - instance.physical_resource_id.should.equal(ec2_instance.id) - - -# Has boto3 equivalent -@mock_ec2_deprecated() -@mock_elb_deprecated() -@mock_cloudformation_deprecated() -def test_stack_elb_integration_with_attached_ec2_instances(): - elb_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "MyELB": { - "Type": "AWS::ElasticLoadBalancing::LoadBalancer", - "Properties": { - "Instances": [{"Ref": "Ec2Instance1"}], - "LoadBalancerName": "test-elb", - "AvailabilityZones": ["us-east-1"], - "Listeners": [ - { - "InstancePort": "80", - "LoadBalancerPort": "80", - "Protocol": "HTTP", - } - ], - }, - }, - "Ec2Instance1": { - "Type": "AWS::EC2::Instance", - "Properties": {"ImageId": EXAMPLE_AMI_ID, "UserData": "some user data"}, - }, - }, - } - elb_template_json = json.dumps(elb_template) - - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("elb_stack", template_body=elb_template_json) - - elb_conn = boto.ec2.elb.connect_to_region("us-west-1") - load_balancer = elb_conn.get_all_load_balancers()[0] - - ec2_conn = boto.ec2.connect_to_region("us-west-1") - reservation = ec2_conn.get_all_reservations()[0] - ec2_instance = reservation.instances[0] - - load_balancer.instances[0].id.should.equal(ec2_instance.id) - list(load_balancer.availability_zones).should.equal(["us-east-1"]) - - -# Has boto3 equivalent -@mock_elb_deprecated() -@mock_cloudformation_deprecated() -def test_stack_elb_integration_with_health_check(): - elb_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "MyELB": { - "Type": "AWS::ElasticLoadBalancing::LoadBalancer", - "Properties": { - "LoadBalancerName": "test-elb", - "AvailabilityZones": ["us-west-1"], - "HealthCheck": { - "HealthyThreshold": "3", - "Interval": "5", - "Target": "HTTP:80/healthcheck", - "Timeout": "4", - "UnhealthyThreshold": "2", - }, - "Listeners": [ - { - "InstancePort": "80", - "LoadBalancerPort": "80", - "Protocol": "HTTP", - } - ], - }, - } - }, - } - elb_template_json = json.dumps(elb_template) - - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("elb_stack", template_body=elb_template_json) - - elb_conn = boto.ec2.elb.connect_to_region("us-west-1") - load_balancer = elb_conn.get_all_load_balancers()[0] - health_check = load_balancer.health_check - - health_check.healthy_threshold.should.equal(3) - health_check.interval.should.equal(5) - health_check.target.should.equal("HTTP:80/healthcheck") - health_check.timeout.should.equal(4) - health_check.unhealthy_threshold.should.equal(2) - - -# Has boto3 equivalent -@mock_elb_deprecated() -@mock_cloudformation_deprecated() -def test_stack_elb_integration_with_update(): - elb_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "MyELB": { - "Type": "AWS::ElasticLoadBalancing::LoadBalancer", - "Properties": { - "LoadBalancerName": "test-elb", - "AvailabilityZones": ["us-west-1a"], - "Listeners": [ - { - "InstancePort": "80", - "LoadBalancerPort": "80", - "Protocol": "HTTP", - } - ], - "Policies": {"Ref": "AWS::NoValue"}, - }, - } - }, - } - elb_template_json = json.dumps(elb_template) - - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("elb_stack", template_body=elb_template_json) - - elb_conn = boto.ec2.elb.connect_to_region("us-west-1") - load_balancer = elb_conn.get_all_load_balancers()[0] - load_balancer.availability_zones[0].should.equal("us-west-1a") - - elb_template["Resources"]["MyELB"]["Properties"]["AvailabilityZones"] = [ - "us-west-1b" - ] - elb_template_json = json.dumps(elb_template) - conn.update_stack("elb_stack", template_body=elb_template_json) - load_balancer = elb_conn.get_all_load_balancers()[0] - load_balancer.availability_zones[0].should.equal("us-west-1b") - - -# Has boto3 equivalent -@mock_ec2_deprecated() -@mock_redshift_deprecated() -@mock_cloudformation_deprecated() -def test_redshift_stack(): - redshift_template_json = json.dumps(redshift.template) - - vpc_conn = boto.vpc.connect_to_region("us-west-2") - conn = boto.cloudformation.connect_to_region("us-west-2") - conn.create_stack( - "redshift_stack", - template_body=redshift_template_json, - parameters=[ - ("DatabaseName", "mydb"), - ("ClusterType", "multi-node"), - ("NumberOfNodes", 2), - ("NodeType", "dw1.xlarge"), - ("MasterUsername", "myuser"), - ("MasterUserPassword", "mypass"), - ("InboundTraffic", "10.0.0.1/16"), - ("PortNumber", 5439), - ], - ) - - redshift_conn = boto.redshift.connect_to_region("us-west-2") - - cluster_res = redshift_conn.describe_clusters() - clusters = cluster_res["DescribeClustersResponse"]["DescribeClustersResult"][ - "Clusters" - ] - clusters.should.have.length_of(1) - cluster = clusters[0] - cluster["DBName"].should.equal("mydb") - cluster["NumberOfNodes"].should.equal(2) - cluster["NodeType"].should.equal("dw1.xlarge") - cluster["MasterUsername"].should.equal("myuser") - cluster["Port"].should.equal(5439) - cluster["VpcSecurityGroups"].should.have.length_of(1) - security_group_id = cluster["VpcSecurityGroups"][0]["VpcSecurityGroupId"] - - groups = vpc_conn.get_all_security_groups(group_ids=[security_group_id]) - groups.should.have.length_of(1) - group = groups[0] - group.rules.should.have.length_of(1) - group.rules[0].grants[0].cidr_ip.should.equal("10.0.0.1/16") - - -# Has boto3 equivalent -@mock_ec2_deprecated() -@mock_cloudformation_deprecated() -def test_stack_security_groups(): - security_group_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "my-security-group": { - "Type": "AWS::EC2::SecurityGroup", - "Properties": {"GroupDescription": "My other group"}, - }, - "Ec2Instance2": { - "Type": "AWS::EC2::Instance", - "Properties": { - "SecurityGroups": [{"Ref": "InstanceSecurityGroup"}], - "ImageId": EXAMPLE_AMI_ID, - }, - }, - "InstanceSecurityGroup": { - "Type": "AWS::EC2::SecurityGroup", - "Properties": { - "GroupDescription": "My security group", - "Tags": [{"Key": "bar", "Value": "baz"}], - "SecurityGroupIngress": [ - { - "IpProtocol": "tcp", - "FromPort": "22", - "ToPort": "22", - "CidrIp": "123.123.123.123/32", - }, - { - "IpProtocol": "tcp", - "FromPort": "80", - "ToPort": "8000", - "SourceSecurityGroupId": {"Ref": "my-security-group"}, - }, - ], - }, - }, - }, - } - security_group_template_json = json.dumps(security_group_template) - - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack( - "security_group_stack", - template_body=security_group_template_json, - tags={"foo": "bar"}, - ) - - ec2_conn = boto.ec2.connect_to_region("us-west-1") - instance_group = ec2_conn.get_all_security_groups( - filters={"description": ["My security group"]} - )[0] - other_group = ec2_conn.get_all_security_groups( - filters={"description": ["My other group"]} - )[0] - - reservation = ec2_conn.get_all_reservations()[0] - ec2_instance = reservation.instances[0] - - ec2_instance.groups[0].id.should.equal(instance_group.id) - instance_group.description.should.equal("My security group") - instance_group.tags.should.have.key("foo").which.should.equal("bar") - instance_group.tags.should.have.key("bar").which.should.equal("baz") - rule1, rule2 = instance_group.rules - int(rule1.to_port).should.equal(22) - int(rule1.from_port).should.equal(22) - rule1.grants[0].cidr_ip.should.equal("123.123.123.123/32") - rule1.ip_protocol.should.equal("tcp") - - int(rule2.to_port).should.equal(8000) - int(rule2.from_port).should.equal(80) - rule2.ip_protocol.should.equal("tcp") - rule2.grants[0].group_id.should.equal(other_group.id) - - -# Has boto3 equivalent -@mock_autoscaling_deprecated() -@mock_elb_deprecated() -@mock_cloudformation_deprecated() -@mock_ec2_deprecated() -def test_autoscaling_group_with_elb(): - web_setup_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "my-as-group": { - "Type": "AWS::AutoScaling::AutoScalingGroup", - "Properties": { - "AvailabilityZones": ["us-east-1a"], - "LaunchConfigurationName": {"Ref": "my-launch-config"}, - "MinSize": "2", - "MaxSize": "2", - "DesiredCapacity": "2", - "LoadBalancerNames": [{"Ref": "my-elb"}], - "Tags": [ - { - "Key": "propagated-test-tag", - "Value": "propagated-test-tag-value", - "PropagateAtLaunch": True, - }, - { - "Key": "not-propagated-test-tag", - "Value": "not-propagated-test-tag-value", - "PropagateAtLaunch": False, - }, - ], - }, - }, - "my-launch-config": { - "Type": "AWS::AutoScaling::LaunchConfiguration", - "Properties": { - "ImageId": EXAMPLE_AMI_ID, - "InstanceType": "t2.medium", - "UserData": "some user data", - }, - }, - "my-elb": { - "Type": "AWS::ElasticLoadBalancing::LoadBalancer", - "Properties": { - "AvailabilityZones": ["us-east-1a"], - "Listeners": [ - { - "LoadBalancerPort": "80", - "InstancePort": "80", - "Protocol": "HTTP", - } - ], - "LoadBalancerName": "my-elb", - "HealthCheck": { - "Target": "HTTP:80", - "HealthyThreshold": "3", - "UnhealthyThreshold": "5", - "Interval": "30", - "Timeout": "5", - }, - }, - }, - }, - } - - web_setup_template_json = json.dumps(web_setup_template) - - conn = boto.cloudformation.connect_to_region("us-east-1") - conn.create_stack("web_stack", template_body=web_setup_template_json) - - autoscale_conn = boto.ec2.autoscale.connect_to_region("us-east-1") - autoscale_group = autoscale_conn.get_all_groups()[0] - autoscale_group.launch_config_name.should.contain("my-launch-config") - autoscale_group.load_balancers[0].should.equal("my-elb") - - # Confirm the Launch config was actually created - autoscale_conn.get_all_launch_configurations().should.have.length_of(1) - - # Confirm the ELB was actually created - elb_conn = boto.ec2.elb.connect_to_region("us-east-1") - elb_conn.get_all_load_balancers().should.have.length_of(1) - - stack = conn.describe_stacks()[0] - resources = stack.describe_resources() - as_group_resource = [ - resource - for resource in resources - if resource.resource_type == "AWS::AutoScaling::AutoScalingGroup" - ][0] - as_group_resource.physical_resource_id.should.contain("my-as-group") - - launch_config_resource = [ - resource - for resource in resources - if resource.resource_type == "AWS::AutoScaling::LaunchConfiguration" - ][0] - launch_config_resource.physical_resource_id.should.contain("my-launch-config") - - elb_resource = [ - resource - for resource in resources - if resource.resource_type == "AWS::ElasticLoadBalancing::LoadBalancer" - ][0] - elb_resource.physical_resource_id.should.contain("my-elb") - - # confirm the instances were created with the right tags - ec2_conn = boto.ec2.connect_to_region("us-east-1") - reservations = ec2_conn.get_all_reservations() - len(reservations).should.equal(1) - reservation = reservations[0] - len(reservation.instances).should.equal(2) - for instance in reservation.instances: - instance.tags["propagated-test-tag"].should.equal("propagated-test-tag-value") - instance.tags.keys().should_not.contain("not-propagated-test-tag") - - -# Has boto3 equivalent -@mock_autoscaling_deprecated() -@mock_cloudformation_deprecated() -@mock_ec2_deprecated() -def test_autoscaling_group_update(): - asg_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "my-as-group": { - "Type": "AWS::AutoScaling::AutoScalingGroup", - "Properties": { - "AvailabilityZones": ["us-west-1a"], - "LaunchConfigurationName": {"Ref": "my-launch-config"}, - "MinSize": "2", - "MaxSize": "2", - "DesiredCapacity": "2", - }, - }, - "my-launch-config": { - "Type": "AWS::AutoScaling::LaunchConfiguration", - "Properties": { - "ImageId": EXAMPLE_AMI_ID, - "InstanceType": "t2.medium", - "UserData": "some user data", - }, - }, - }, - } - asg_template_json = json.dumps(asg_template) - - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("asg_stack", template_body=asg_template_json) - - autoscale_conn = boto.ec2.autoscale.connect_to_region("us-west-1") - asg = autoscale_conn.get_all_groups()[0] - asg.min_size.should.equal(2) - asg.max_size.should.equal(2) - asg.desired_capacity.should.equal(2) - - asg_template["Resources"]["my-as-group"]["Properties"]["MaxSize"] = 3 - asg_template["Resources"]["my-as-group"]["Properties"]["Tags"] = [ - { - "Key": "propagated-test-tag", - "Value": "propagated-test-tag-value", - "PropagateAtLaunch": True, - }, - { - "Key": "not-propagated-test-tag", - "Value": "not-propagated-test-tag-value", - "PropagateAtLaunch": False, - }, - ] - asg_template_json = json.dumps(asg_template) - conn.update_stack("asg_stack", template_body=asg_template_json) - asg = autoscale_conn.get_all_groups()[0] - asg.min_size.should.equal(2) - asg.max_size.should.equal(3) - asg.desired_capacity.should.equal(2) - - # confirm the instances were created with the right tags - ec2_conn = boto.ec2.connect_to_region("us-west-1") - reservations = ec2_conn.get_all_reservations() - running_instance_count = 0 - for res in reservations: - for instance in res.instances: - if instance.state == "running": - running_instance_count += 1 - instance.tags["propagated-test-tag"].should.equal( - "propagated-test-tag-value" - ) - instance.tags.keys().should_not.contain("not-propagated-test-tag") - running_instance_count.should.equal(2) - - -# Has boto3 equivalent -@mock_ec2_deprecated() -@mock_cloudformation_deprecated() -def test_vpc_single_instance_in_subnet(): - template_json = json.dumps(vpc_single_instance_in_subnet.template) - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack( - "test_stack", template_body=template_json, parameters=[("KeyName", "my_key")] - ) - - vpc_conn = boto.vpc.connect_to_region("us-west-1") - - vpc = vpc_conn.get_all_vpcs(filters={"cidrBlock": "10.0.0.0/16"})[0] - vpc.cidr_block.should.equal("10.0.0.0/16") - - # Add this once we implement the endpoint - # vpc_conn.get_all_internet_gateways().should.have.length_of(1) - - subnet = vpc_conn.get_all_subnets(filters={"vpcId": vpc.id})[0] - subnet.vpc_id.should.equal(vpc.id) - - ec2_conn = boto.ec2.connect_to_region("us-west-1") - reservation = ec2_conn.get_all_reservations()[0] - instance = reservation.instances[0] - instance.tags["Foo"].should.equal("Bar") - # Check that the EIP is attached the the EC2 instance - eip = ec2_conn.get_all_addresses()[0] - eip.domain.should.equal("vpc") - eip.instance_id.should.equal(instance.id) - - security_group = ec2_conn.get_all_security_groups(filters={"vpc_id": [vpc.id]})[0] - security_group.vpc_id.should.equal(vpc.id) - - stack = conn.describe_stacks()[0] - - vpc.tags.should.have.key("Application").which.should.equal(stack.stack_id) - - resources = stack.describe_resources() - vpc_resource = [ - resource for resource in resources if resource.resource_type == "AWS::EC2::VPC" - ][0] - vpc_resource.physical_resource_id.should.equal(vpc.id) - - subnet_resource = [ - resource - for resource in resources - if resource.resource_type == "AWS::EC2::Subnet" - ][0] - subnet_resource.physical_resource_id.should.equal(subnet.id) - - eip_resource = [ - resource for resource in resources if resource.resource_type == "AWS::EC2::EIP" - ][0] - eip_resource.physical_resource_id.should.equal(eip.public_ip) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated() -@mock_ec2_deprecated() -@mock_rds_deprecated() -def test_rds_mysql_with_read_replica(): - ec2_conn = boto.ec2.connect_to_region("us-west-1") - ec2_conn.create_security_group("application", "Our Application Group") - - template_json = json.dumps(rds_mysql_with_read_replica.template) - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack( - "test_stack", - template_body=template_json, - parameters=[ - ("DBInstanceIdentifier", "master_db"), - ("DBName", "my_db"), - ("DBUser", "my_user"), - ("DBPassword", "my_password"), - ("DBAllocatedStorage", "20"), - ("DBInstanceClass", "db.m1.medium"), - ("EC2SecurityGroup", "application"), - ("MultiAZ", "true"), - ], - ) - - rds_conn = boto.rds.connect_to_region("us-west-1") - - primary = rds_conn.get_all_dbinstances("master_db")[0] - primary.master_username.should.equal("my_user") - primary.allocated_storage.should.equal(20) - primary.instance_class.should.equal("db.m1.medium") - primary.multi_az.should.equal(True) - list(primary.read_replica_dbinstance_identifiers).should.have.length_of(1) - replica_id = primary.read_replica_dbinstance_identifiers[0] - - replica = rds_conn.get_all_dbinstances(replica_id)[0] - replica.instance_class.should.equal("db.m1.medium") - - security_group_name = primary.security_groups[0].name - security_group = rds_conn.get_all_dbsecurity_groups(security_group_name)[0] - security_group.ec2_groups[0].name.should.equal("application") - - -# Has boto3 equivalent -@mock_cloudformation_deprecated() -@mock_ec2_deprecated() -@mock_rds_deprecated() -def test_rds_mysql_with_read_replica_in_vpc(): - template_json = json.dumps(rds_mysql_with_read_replica.template) - conn = boto.cloudformation.connect_to_region("eu-central-1") - conn.create_stack( - "test_stack", - template_body=template_json, - parameters=[ - ("DBInstanceIdentifier", "master_db"), - ("DBName", "my_db"), - ("DBUser", "my_user"), - ("DBPassword", "my_password"), - ("DBAllocatedStorage", "20"), - ("DBInstanceClass", "db.m1.medium"), - ("MultiAZ", "true"), - ], - ) - - rds_conn = boto.rds.connect_to_region("eu-central-1") - primary = rds_conn.get_all_dbinstances("master_db")[0] - - subnet_group_name = primary.subnet_group.name - subnet_group = rds_conn.get_all_db_subnet_groups(subnet_group_name)[0] - subnet_group.description.should.equal("my db subnet group") - - -# Has boto3 equivalent -@mock_autoscaling_deprecated() -@mock_iam_deprecated() -@mock_cloudformation_deprecated() -def test_iam_roles(): - iam_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "my-launch-config": { - "Properties": { - "IamInstanceProfile": {"Ref": "my-instance-profile-with-path"}, - "ImageId": EXAMPLE_AMI_ID, - "InstanceType": "t2.medium", - }, - "Type": "AWS::AutoScaling::LaunchConfiguration", - }, - "my-instance-profile-with-path": { - "Properties": { - "Path": "my-path", - "Roles": [{"Ref": "my-role-with-path"}], - }, - "Type": "AWS::IAM::InstanceProfile", - }, - "my-instance-profile-no-path": { - "Properties": {"Roles": [{"Ref": "my-role-no-path"}]}, - "Type": "AWS::IAM::InstanceProfile", - }, - "my-role-with-path": { - "Properties": { - "AssumeRolePolicyDocument": { - "Statement": [ - { - "Action": ["sts:AssumeRole"], - "Effect": "Allow", - "Principal": {"Service": ["ec2.amazonaws.com"]}, - } - ] - }, - "Path": "/my-path/", - "Policies": [ - { - "PolicyDocument": { - "Statement": [ - { - "Action": [ - "ec2:CreateTags", - "ec2:DescribeInstances", - "ec2:DescribeTags", - ], - "Effect": "Allow", - "Resource": ["*"], - } - ], - "Version": "2012-10-17", - }, - "PolicyName": "EC2_Tags", - }, - { - "PolicyDocument": { - "Statement": [ - { - "Action": ["sqs:*"], - "Effect": "Allow", - "Resource": ["*"], - } - ], - "Version": "2012-10-17", - }, - "PolicyName": "SQS", - }, - ], - }, - "Type": "AWS::IAM::Role", - }, - "my-role-no-path": { - "Properties": { - "RoleName": "my-role-no-path-name", - "AssumeRolePolicyDocument": { - "Statement": [ - { - "Action": ["sts:AssumeRole"], - "Effect": "Allow", - "Principal": {"Service": ["ec2.amazonaws.com"]}, - } - ] - }, - }, - "Type": "AWS::IAM::Role", - }, - }, - } - - iam_template_json = json.dumps(iam_template) - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("test_stack", template_body=iam_template_json) - - iam_conn = boto.iam.connect_to_region("us-west-1") - - role_results = iam_conn.list_roles()["list_roles_response"]["list_roles_result"][ - "roles" - ] - role_name_to_id = {} - role_names = [] - for role_result in role_results: - role = iam_conn.get_role(role_result.role_name) - # Role name is not specified, so randomly generated - can't check exact name - if "with-path" in role.role_name: - role_name_to_id["with-path"] = role.role_id - role.path.should.equal("/my-path/") - else: - role_name_to_id["no-path"] = role.role_id - role.role_name.should.equal("my-role-no-path-name") - role.path.should.equal("/") - role_names.append(role.role_name) - - instance_profile_responses = iam_conn.list_instance_profiles()[ - "list_instance_profiles_response" - ]["list_instance_profiles_result"]["instance_profiles"] - instance_profile_responses.should.have.length_of(2) - instance_profile_names = [] - - for instance_profile_response in instance_profile_responses: - instance_profile = iam_conn.get_instance_profile( - instance_profile_response.instance_profile_name - ) - instance_profile_names.append(instance_profile.instance_profile_name) - instance_profile.instance_profile_name.should.contain("my-instance-profile") - if "with-path" in instance_profile.instance_profile_name: - instance_profile.path.should.equal("my-path") - instance_profile.role_id.should.equal(role_name_to_id["with-path"]) - else: - instance_profile.instance_profile_name.should.contain("no-path") - instance_profile.role_id.should.equal(role_name_to_id["no-path"]) - instance_profile.path.should.equal("/") - - autoscale_conn = boto.ec2.autoscale.connect_to_region("us-west-1") - launch_config = autoscale_conn.get_all_launch_configurations()[0] - launch_config.instance_profile_name.should.contain("my-instance-profile-with-path") - - stack = conn.describe_stacks()[0] - resources = stack.describe_resources() - instance_profile_resources = [ - resource - for resource in resources - if resource.resource_type == "AWS::IAM::InstanceProfile" - ] - {ip.physical_resource_id for ip in instance_profile_resources}.should.equal( - set(instance_profile_names) - ) - - role_resources = [ - resource for resource in resources if resource.resource_type == "AWS::IAM::Role" - ] - {r.physical_resource_id for r in role_resources}.should.equal(set(role_names)) - - -# Has boto3 equivalent -@mock_ec2_deprecated() -@mock_cloudformation_deprecated() -def test_single_instance_with_ebs_volume(): - template_json = json.dumps(single_instance_with_ebs_volume.template) - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack( - "test_stack", template_body=template_json, parameters=[("KeyName", "key_name")] - ) - - ec2_conn = boto.ec2.connect_to_region("us-west-1") - reservation = ec2_conn.get_all_reservations()[0] - ec2_instance = reservation.instances[0] - - volumes = ec2_conn.get_all_volumes() - # Grab the mounted drive - volume = [volume for volume in volumes if volume.attach_data.device == "/dev/sdh"][ - 0 - ] - volume.volume_state().should.equal("in-use") - volume.attach_data.instance_id.should.equal(ec2_instance.id) - - stack = conn.describe_stacks()[0] - resources = stack.describe_resources() - ebs_volumes = [ - resource - for resource in resources - if resource.resource_type == "AWS::EC2::Volume" - ] - ebs_volumes[0].physical_resource_id.should.equal(volume.id) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated() -def test_create_template_without_required_param(): - template_json = json.dumps(single_instance_with_ebs_volume.template) - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack.when.called_with( - "test_stack", template_body=template_json - ).should.throw(BotoServerError) - - @mock_cloudformation def test_create_template_without_required_param_boto3(): template_json = json.dumps(single_instance_with_ebs_volume.template) @@ -1022,57 +43,6 @@ def test_create_template_without_required_param_boto3(): err.should.have.key("Message").equal("Missing parameter KeyName") -# Has boto3 equivalent -@mock_ec2_deprecated() -@mock_cloudformation_deprecated() -def test_classic_eip(): - template_json = json.dumps(ec2_classic_eip.template) - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("test_stack", template_body=template_json) - ec2_conn = boto.ec2.connect_to_region("us-west-1") - eip = ec2_conn.get_all_addresses()[0] - - stack = conn.describe_stacks()[0] - resources = stack.describe_resources() - cfn_eip = [ - resource for resource in resources if resource.resource_type == "AWS::EC2::EIP" - ][0] - cfn_eip.physical_resource_id.should.equal(eip.public_ip) - - -# Has boto3 equivalent -@mock_ec2_deprecated() -@mock_cloudformation_deprecated() -def test_vpc_eip(): - template_json = json.dumps(vpc_eip.template) - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("test_stack", template_body=template_json) - ec2_conn = boto.ec2.connect_to_region("us-west-1") - eip = ec2_conn.get_all_addresses()[0] - - stack = conn.describe_stacks()[0] - resources = stack.describe_resources() - cfn_eip = [ - resource for resource in resources if resource.resource_type == "AWS::EC2::EIP" - ][0] - cfn_eip.physical_resource_id.should.equal(eip.public_ip) - - -# Has boto3 equivalent -@mock_ec2_deprecated() -@mock_cloudformation_deprecated() -def test_fn_join(): - template_json = json.dumps(fn_join.template) - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("test_stack", template_body=template_json) - ec2_conn = boto.ec2.connect_to_region("us-west-1") - eip = ec2_conn.get_all_addresses()[0] - - stack = conn.describe_stacks()[0] - fn_join_output = stack.outputs[0] - fn_join_output.value.should.equal("test eip:{0}".format(eip.public_ip)) - - @mock_ec2 @mock_cloudformation def test_fn_join_boto3(): @@ -1087,45 +57,6 @@ def test_fn_join_boto3(): fn_join_output["OutputValue"].should.equal("test eip:{0}".format(eip["PublicIp"])) -# Has boto3 equivalent -@mock_cloudformation_deprecated() -@mock_sqs_deprecated() -def test_conditional_resources(): - sqs_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Parameters": { - "EnvType": {"Description": "Environment type.", "Type": "String"} - }, - "Conditions": {"CreateQueue": {"Fn::Equals": [{"Ref": "EnvType"}, "prod"]}}, - "Resources": { - "QueueGroup": { - "Condition": "CreateQueue", - "Type": "AWS::SQS::Queue", - "Properties": {"QueueName": "my-queue", "VisibilityTimeout": 60}, - } - }, - } - sqs_template_json = json.dumps(sqs_template) - - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack( - "test_stack_without_queue", - template_body=sqs_template_json, - parameters=[("EnvType", "staging")], - ) - sqs_conn = boto.sqs.connect_to_region("us-west-1") - list(sqs_conn.get_all_queues()).should.have.length_of(0) - - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack( - "test_stack_with_queue", - template_body=sqs_template_json, - parameters=[("EnvType", "prod")], - ) - sqs_conn = boto.sqs.connect_to_region("us-west-1") - list(sqs_conn.get_all_queues()).should.have.length_of(1) - - @mock_cloudformation @mock_sqs def test_conditional_resources_boto3(): @@ -1162,52 +93,6 @@ def test_conditional_resources_boto3(): sqs.list_queues()["QueueUrls"].should.have.length_of(1) -# Has boto3 equivalent -@mock_cloudformation_deprecated() -@mock_ec2_deprecated() -def test_conditional_if_handling(): - dummy_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Conditions": {"EnvEqualsPrd": {"Fn::Equals": [{"Ref": "ENV"}, "prd"]}}, - "Parameters": { - "ENV": { - "Default": "dev", - "Description": "Deployment environment for the stack (dev/prd)", - "Type": "String", - } - }, - "Description": "Stack 1", - "Resources": { - "App1": { - "Properties": { - "ImageId": { - "Fn::If": ["EnvEqualsPrd", EXAMPLE_AMI_ID, EXAMPLE_AMI_ID2] - } - }, - "Type": "AWS::EC2::Instance", - } - }, - } - dummy_template_json = json.dumps(dummy_template) - - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("test_stack1", template_body=dummy_template_json) - ec2_conn = boto.ec2.connect_to_region("us-west-1") - reservation = ec2_conn.get_all_reservations()[0] - ec2_instance = reservation.instances[0] - ec2_instance.image_id.should.equal(EXAMPLE_AMI_ID2) - ec2_instance.terminate() - - conn = boto.cloudformation.connect_to_region("us-west-2") - conn.create_stack( - "test_stack1", template_body=dummy_template_json, parameters=[("ENV", "prd")] - ) - ec2_conn = boto.ec2.connect_to_region("us-west-2") - reservation = ec2_conn.get_all_reservations()[0] - ec2_instance = reservation.instances[0] - ec2_instance.image_id.should.equal(EXAMPLE_AMI_ID) - - @mock_cloudformation @mock_ec2 def test_conditional_if_handling_boto3(): @@ -1252,51 +137,6 @@ def test_conditional_if_handling_boto3(): ec2_instance["ImageId"].should.equal(EXAMPLE_AMI_ID) -# Has boto3 equivalent -@mock_cloudformation_deprecated() -@mock_ec2_deprecated() -def test_cloudformation_mapping(): - dummy_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Mappings": { - "RegionMap": { - "us-east-1": {"32": EXAMPLE_AMI_ID, "64": EXAMPLE_AMI_ID2}, - "us-west-1": {"32": EXAMPLE_AMI_ID, "64": EXAMPLE_AMI_ID2}, - "eu-west-1": {"32": EXAMPLE_AMI_ID, "64": EXAMPLE_AMI_ID2}, - "ap-southeast-1": {"32": EXAMPLE_AMI_ID, "64": EXAMPLE_AMI_ID2}, - "ap-northeast-1": {"32": EXAMPLE_AMI_ID, "64": EXAMPLE_AMI_ID2}, - } - }, - "Resources": { - "WebServer": { - "Type": "AWS::EC2::Instance", - "Properties": { - "ImageId": { - "Fn::FindInMap": ["RegionMap", {"Ref": "AWS::Region"}, "32"] - }, - "InstanceType": "m1.small", - }, - } - }, - } - - dummy_template_json = json.dumps(dummy_template) - - conn = boto.cloudformation.connect_to_region("us-east-1") - conn.create_stack("test_stack1", template_body=dummy_template_json) - ec2_conn = boto.ec2.connect_to_region("us-east-1") - reservation = ec2_conn.get_all_reservations()[0] - ec2_instance = reservation.instances[0] - ec2_instance.image_id.should.equal(EXAMPLE_AMI_ID) - - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("test_stack1", template_body=dummy_template_json) - ec2_conn = boto.ec2.connect_to_region("us-west-1") - reservation = ec2_conn.get_all_reservations()[0] - ec2_instance = reservation.instances[0] - ec2_instance.image_id.should.equal(EXAMPLE_AMI_ID) - - @mock_cloudformation @mock_ec2 def test_cloudformation_mapping_boto3(): @@ -1339,569 +179,6 @@ def test_cloudformation_mapping_boto3(): ec2_instance["ImageId"].should.equal(EXAMPLE_AMI_ID2) -# Has boto3 equivalent -@mock_cloudformation_deprecated() -@mock_route53_deprecated() -def test_route53_roundrobin(): - route53_conn = boto.connect_route53() - - template_json = json.dumps(route53_roundrobin.template) - conn = boto.cloudformation.connect_to_region("us-west-1") - stack = conn.create_stack("test_stack", template_body=template_json) - - zones = route53_conn.get_all_hosted_zones()["ListHostedZonesResponse"][ - "HostedZones" - ] - list(zones).should.have.length_of(1) - zone_id = zones[0]["Id"] - zone_id = zone_id.split("/") - zone_id = zone_id[2] - - rrsets = route53_conn.get_all_rrsets(zone_id) - rrsets.hosted_zone_id.should.equal(zone_id) - rrsets.should.have.length_of(2) - record_set1 = rrsets[0] - record_set1.name.should.equal("test_stack.us-west-1.my_zone.") - record_set1.identifier.should.equal("test_stack AWS") - record_set1.type.should.equal("CNAME") - record_set1.ttl.should.equal("900") - record_set1.weight.should.equal("3") - record_set1.resource_records[0].should.equal("aws.amazon.com") - - record_set2 = rrsets[1] - record_set2.name.should.equal("test_stack.us-west-1.my_zone.") - record_set2.identifier.should.equal("test_stack Amazon") - record_set2.type.should.equal("CNAME") - record_set2.ttl.should.equal("900") - record_set2.weight.should.equal("1") - record_set2.resource_records[0].should.equal("www.amazon.com") - - stack = conn.describe_stacks()[0] - output = stack.outputs[0] - output.key.should.equal("DomainName") - output.value.should.equal("arn:aws:route53:::hostedzone/{0}".format(zone_id)) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated() -@mock_ec2_deprecated() -@mock_route53_deprecated() -def test_route53_ec2_instance_with_public_ip(): - route53_conn = boto.connect_route53() - ec2_conn = boto.ec2.connect_to_region("us-west-1") - - template_json = json.dumps(route53_ec2_instance_with_public_ip.template) - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("test_stack", template_body=template_json) - - instance_id = ec2_conn.get_all_reservations()[0].instances[0].id - - zones = route53_conn.get_all_hosted_zones()["ListHostedZonesResponse"][ - "HostedZones" - ] - list(zones).should.have.length_of(1) - zone_id = zones[0]["Id"] - zone_id = zone_id.split("/") - zone_id = zone_id[2] - - rrsets = route53_conn.get_all_rrsets(zone_id) - rrsets.should.have.length_of(1) - - record_set1 = rrsets[0] - record_set1.name.should.equal("{0}.us-west-1.my_zone.".format(instance_id)) - record_set1.identifier.should.equal(None) - record_set1.type.should.equal("A") - record_set1.ttl.should.equal("900") - record_set1.weight.should.equal(None) - record_set1.resource_records[0].should.equal("10.0.0.25") - - -# Has boto3 equivalent -@mock_cloudformation_deprecated() -@mock_route53_deprecated() -def test_route53_associate_health_check(): - route53_conn = boto.connect_route53() - - template_json = json.dumps(route53_health_check.template) - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("test_stack", template_body=template_json) - - checks = route53_conn.get_list_health_checks()["ListHealthChecksResponse"][ - "HealthChecks" - ] - list(checks).should.have.length_of(1) - check = checks[0] - health_check_id = check["Id"] - config = check["HealthCheckConfig"] - config["FailureThreshold"].should.equal("3") - config["IPAddress"].should.equal("10.0.0.4") - config["Port"].should.equal("80") - config["RequestInterval"].should.equal("10") - config["ResourcePath"].should.equal("/") - config["Type"].should.equal("HTTP") - - zones = route53_conn.get_all_hosted_zones()["ListHostedZonesResponse"][ - "HostedZones" - ] - list(zones).should.have.length_of(1) - zone_id = zones[0]["Id"] - zone_id = zone_id.split("/") - zone_id = zone_id[2] - - rrsets = route53_conn.get_all_rrsets(zone_id) - rrsets.should.have.length_of(1) - - record_set = rrsets[0] - record_set.health_check.should.equal(health_check_id) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated() -@mock_route53_deprecated() -def test_route53_with_update(): - route53_conn = boto.connect_route53() - - template_json = json.dumps(route53_health_check.template) - cf_conn = boto.cloudformation.connect_to_region("us-west-1") - cf_conn.create_stack("test_stack", template_body=template_json) - - zones = route53_conn.get_all_hosted_zones()["ListHostedZonesResponse"][ - "HostedZones" - ] - list(zones).should.have.length_of(1) - zone_id = zones[0]["Id"] - zone_id = zone_id.split("/") - zone_id = zone_id[2] - - rrsets = route53_conn.get_all_rrsets(zone_id) - rrsets.should.have.length_of(1) - - record_set = rrsets[0] - record_set.resource_records.should.equal(["my.example.com"]) - - template = deepcopy(route53_health_check.template) - template["Resources"]["myDNSRecord"]["Properties"]["ResourceRecords"] = [ - "my_other.example.com" - ] - template_json = json.dumps(template) - cf_conn.update_stack("test_stack", template_body=template_json) - - zones = route53_conn.get_all_hosted_zones()["ListHostedZonesResponse"][ - "HostedZones" - ] - list(zones).should.have.length_of(1) - zone_id = zones[0]["Id"] - zone_id = zone_id.split("/") - zone_id = zone_id[2] - - rrsets = route53_conn.get_all_rrsets(zone_id) - rrsets.should.have.length_of(1) - - record_set = rrsets[0] - record_set.resource_records.should.equal(["my_other.example.com"]) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated() -@mock_sns_deprecated() -def test_sns_topic(): - dummy_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "MySNSTopic": { - "Type": "AWS::SNS::Topic", - "Properties": { - "Subscription": [ - {"Endpoint": "https://example.com", "Protocol": "https"} - ], - "TopicName": "my_topics", - }, - } - }, - "Outputs": { - "topic_name": {"Value": {"Fn::GetAtt": ["MySNSTopic", "TopicName"]}}, - "topic_arn": {"Value": {"Ref": "MySNSTopic"}}, - }, - } - template_json = json.dumps(dummy_template) - conn = boto.cloudformation.connect_to_region("us-west-1") - stack = conn.create_stack("test_stack", template_body=template_json) - - sns_conn = boto.sns.connect_to_region("us-west-1") - topics = sns_conn.get_all_topics()["ListTopicsResponse"]["ListTopicsResult"][ - "Topics" - ] - topics.should.have.length_of(1) - topic_arn = topics[0]["TopicArn"] - topic_arn.should.contain("my_topics") - - subscriptions = sns_conn.get_all_subscriptions()["ListSubscriptionsResponse"][ - "ListSubscriptionsResult" - ]["Subscriptions"] - subscriptions.should.have.length_of(1) - subscription = subscriptions[0] - subscription["TopicArn"].should.equal(topic_arn) - subscription["Protocol"].should.equal("https") - subscription["SubscriptionArn"].should.contain(topic_arn) - subscription["Endpoint"].should.equal("https://example.com") - - stack = conn.describe_stacks()[0] - topic_name_output = [x for x in stack.outputs if x.key == "topic_name"][0] - topic_name_output.value.should.equal("my_topics") - topic_arn_output = [x for x in stack.outputs if x.key == "topic_arn"][0] - topic_arn_output.value.should.equal(topic_arn) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -@mock_ec2_deprecated -def test_vpc_gateway_attachment_creation_should_attach_itself_to_vpc(): - template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "internetgateway": {"Type": "AWS::EC2::InternetGateway"}, - "testvpc": { - "Type": "AWS::EC2::VPC", - "Properties": { - "CidrBlock": "10.0.0.0/16", - "EnableDnsHostnames": "true", - "EnableDnsSupport": "true", - "InstanceTenancy": "default", - }, - }, - "vpcgatewayattachment": { - "Type": "AWS::EC2::VPCGatewayAttachment", - "Properties": { - "InternetGatewayId": {"Ref": "internetgateway"}, - "VpcId": {"Ref": "testvpc"}, - }, - }, - }, - } - - template_json = json.dumps(template) - cf_conn = boto.cloudformation.connect_to_region("us-west-1") - cf_conn.create_stack("test_stack", template_body=template_json) - - vpc_conn = boto.vpc.connect_to_region("us-west-1") - vpc = vpc_conn.get_all_vpcs(filters={"cidrBlock": "10.0.0.0/16"})[0] - igws = vpc_conn.get_all_internet_gateways(filters={"attachment.vpc-id": vpc.id}) - - igws.should.have.length_of(1) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -@mock_ec2_deprecated -def test_vpc_peering_creation(): - vpc_conn = boto.vpc.connect_to_region("us-west-1") - vpc_source = vpc_conn.create_vpc("10.0.0.0/16") - peer_vpc = vpc_conn.create_vpc("10.1.0.0/16") - template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "vpcpeeringconnection": { - "Type": "AWS::EC2::VPCPeeringConnection", - "Properties": {"PeerVpcId": peer_vpc.id, "VpcId": vpc_source.id}, - } - }, - } - - template_json = json.dumps(template) - cf_conn = boto.cloudformation.connect_to_region("us-west-1") - cf_conn.create_stack("test_stack", template_body=template_json) - - peering_connections = vpc_conn.get_all_vpc_peering_connections() - peering_connections.should.have.length_of(1) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -@mock_ec2_deprecated -def test_multiple_security_group_ingress_separate_from_security_group_by_id(): - template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "test-security-group1": { - "Type": "AWS::EC2::SecurityGroup", - "Properties": { - "GroupDescription": "test security group", - "Tags": [{"Key": "sg-name", "Value": "sg1"}], - }, - }, - "test-security-group2": { - "Type": "AWS::EC2::SecurityGroup", - "Properties": { - "GroupDescription": "test security group", - "Tags": [{"Key": "sg-name", "Value": "sg2"}], - }, - }, - "test-sg-ingress": { - "Type": "AWS::EC2::SecurityGroupIngress", - "Properties": { - "GroupId": {"Ref": "test-security-group1"}, - "IpProtocol": "tcp", - "FromPort": "80", - "ToPort": "8080", - "SourceSecurityGroupId": {"Ref": "test-security-group2"}, - }, - }, - }, - } - - template_json = json.dumps(template) - cf_conn = boto.cloudformation.connect_to_region("us-west-1") - cf_conn.create_stack("test_stack", template_body=template_json) - ec2_conn = boto.ec2.connect_to_region("us-west-1") - - security_group1 = ec2_conn.get_all_security_groups(filters={"tag:sg-name": "sg1"})[ - 0 - ] - security_group2 = ec2_conn.get_all_security_groups(filters={"tag:sg-name": "sg2"})[ - 0 - ] - - security_group1.rules.should.have.length_of(1) - security_group1.rules[0].grants.should.have.length_of(1) - security_group1.rules[0].grants[0].group_id.should.equal(security_group2.id) - security_group1.rules[0].ip_protocol.should.equal("tcp") - security_group1.rules[0].from_port.should.equal("80") - security_group1.rules[0].to_port.should.equal("8080") - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -@mock_ec2_deprecated -def test_security_group_ingress_separate_from_security_group_by_id(): - ec2_conn = boto.ec2.connect_to_region("us-west-1") - ec2_conn.create_security_group("test-security-group1", "test security group") - - template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "test-security-group2": { - "Type": "AWS::EC2::SecurityGroup", - "Properties": { - "GroupDescription": "test security group", - "Tags": [{"Key": "sg-name", "Value": "sg2"}], - }, - }, - "test-sg-ingress": { - "Type": "AWS::EC2::SecurityGroupIngress", - "Properties": { - "GroupName": "test-security-group1", - "IpProtocol": "tcp", - "FromPort": "80", - "ToPort": "8080", - "SourceSecurityGroupId": {"Ref": "test-security-group2"}, - }, - }, - }, - } - - template_json = json.dumps(template) - cf_conn = boto.cloudformation.connect_to_region("us-west-1") - cf_conn.create_stack("test_stack", template_body=template_json) - security_group1 = ec2_conn.get_all_security_groups( - groupnames=["test-security-group1"] - )[0] - security_group2 = ec2_conn.get_all_security_groups(filters={"tag:sg-name": "sg2"})[ - 0 - ] - - security_group1.rules.should.have.length_of(1) - security_group1.rules[0].grants.should.have.length_of(1) - security_group1.rules[0].grants[0].group_id.should.equal(security_group2.id) - security_group1.rules[0].ip_protocol.should.equal("tcp") - security_group1.rules[0].from_port.should.equal("80") - security_group1.rules[0].to_port.should.equal("8080") - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -@mock_ec2_deprecated -def test_security_group_ingress_separate_from_security_group_by_id_using_vpc(): - vpc_conn = boto.vpc.connect_to_region("us-west-1") - vpc = vpc_conn.create_vpc("10.0.0.0/16") - - template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "test-security-group1": { - "Type": "AWS::EC2::SecurityGroup", - "Properties": { - "GroupDescription": "test security group", - "VpcId": vpc.id, - "Tags": [{"Key": "sg-name", "Value": "sg1"}], - }, - }, - "test-security-group2": { - "Type": "AWS::EC2::SecurityGroup", - "Properties": { - "GroupDescription": "test security group", - "VpcId": vpc.id, - "Tags": [{"Key": "sg-name", "Value": "sg2"}], - }, - }, - "test-sg-ingress": { - "Type": "AWS::EC2::SecurityGroupIngress", - "Properties": { - "GroupId": {"Ref": "test-security-group1"}, - "VpcId": vpc.id, - "IpProtocol": "tcp", - "FromPort": "80", - "ToPort": "8080", - "SourceSecurityGroupId": {"Ref": "test-security-group2"}, - }, - }, - }, - } - - template_json = json.dumps(template) - cf_conn = boto.cloudformation.connect_to_region("us-west-1") - cf_conn.create_stack("test_stack", template_body=template_json) - security_group1 = vpc_conn.get_all_security_groups(filters={"tag:sg-name": "sg1"})[ - 0 - ] - security_group2 = vpc_conn.get_all_security_groups(filters={"tag:sg-name": "sg2"})[ - 0 - ] - - security_group1.rules.should.have.length_of(1) - security_group1.rules[0].grants.should.have.length_of(1) - security_group1.rules[0].grants[0].group_id.should.equal(security_group2.id) - security_group1.rules[0].ip_protocol.should.equal("tcp") - security_group1.rules[0].from_port.should.equal("80") - security_group1.rules[0].to_port.should.equal("8080") - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -@mock_ec2_deprecated -def test_security_group_with_update(): - vpc_conn = boto.vpc.connect_to_region("us-west-1") - vpc1 = vpc_conn.create_vpc("10.0.0.0/16") - - template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "test-security-group": { - "Type": "AWS::EC2::SecurityGroup", - "Properties": { - "GroupDescription": "test security group", - "VpcId": vpc1.id, - "Tags": [{"Key": "sg-name", "Value": "sg"}], - }, - } - }, - } - - template_json = json.dumps(template) - cf_conn = boto.cloudformation.connect_to_region("us-west-1") - cf_conn.create_stack("test_stack", template_body=template_json) - security_group = vpc_conn.get_all_security_groups(filters={"tag:sg-name": "sg"})[0] - security_group.vpc_id.should.equal(vpc1.id) - - vpc2 = vpc_conn.create_vpc("10.1.0.0/16") - template["Resources"]["test-security-group"]["Properties"]["VpcId"] = vpc2.id - template_json = json.dumps(template) - cf_conn.update_stack("test_stack", template_body=template_json) - security_group = vpc_conn.get_all_security_groups(filters={"tag:sg-name": "sg"})[0] - security_group.vpc_id.should.equal(vpc2.id) - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -@mock_ec2_deprecated -def test_subnets_should_be_created_with_availability_zone(): - vpc_conn = boto.vpc.connect_to_region("us-west-1") - vpc = vpc_conn.create_vpc("10.0.0.0/16") - - subnet_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "testSubnet": { - "Type": "AWS::EC2::Subnet", - "Properties": { - "VpcId": vpc.id, - "CidrBlock": "10.0.0.0/24", - "AvailabilityZone": "us-west-1b", - }, - } - }, - } - cf_conn = boto.cloudformation.connect_to_region("us-west-1") - template_json = json.dumps(subnet_template) - cf_conn.create_stack("test_stack", template_body=template_json) - subnet = vpc_conn.get_all_subnets(filters={"cidrBlock": "10.0.0.0/24"})[0] - subnet.availability_zone.should.equal("us-west-1b") - - -# Has boto3 equivalent -@mock_cloudformation_deprecated -@mock_datapipeline_deprecated -def test_datapipeline(): - dp_template = { - "AWSTemplateFormatVersion": "2010-09-09", - "Resources": { - "dataPipeline": { - "Properties": { - "Activate": "true", - "Name": "testDataPipeline", - "PipelineObjects": [ - { - "Fields": [ - { - "Key": "failureAndRerunMode", - "StringValue": "CASCADE", - }, - {"Key": "scheduleType", "StringValue": "cron"}, - {"Key": "schedule", "RefValue": "DefaultSchedule"}, - { - "Key": "pipelineLogUri", - "StringValue": "s3://bucket/logs", - }, - {"Key": "type", "StringValue": "Default"}, - ], - "Id": "Default", - "Name": "Default", - }, - { - "Fields": [ - { - "Key": "startDateTime", - "StringValue": "1970-01-01T01:00:00", - }, - {"Key": "period", "StringValue": "1 Day"}, - {"Key": "type", "StringValue": "Schedule"}, - ], - "Id": "DefaultSchedule", - "Name": "RunOnce", - }, - ], - "PipelineTags": [], - }, - "Type": "AWS::DataPipeline::Pipeline", - } - }, - } - cf_conn = boto.cloudformation.connect_to_region("us-east-1") - template_json = json.dumps(dp_template) - stack_id = cf_conn.create_stack("test_stack", template_body=template_json) - - dp_conn = boto.datapipeline.connect_to_region("us-east-1") - data_pipelines = dp_conn.list_pipelines() - - data_pipelines["pipelineIdList"].should.have.length_of(1) - data_pipelines["pipelineIdList"][0]["name"].should.equal("testDataPipeline") - - stack_resources = cf_conn.list_stack_resources(stack_id) - stack_resources.should.have.length_of(1) - stack_resources[0].physical_resource_id.should.equal( - data_pipelines["pipelineIdList"][0]["id"] - ) - - @mock_cloudformation @mock_lambda def test_lambda_function(): diff --git a/tests/test_cloudformation/test_import_value.py b/tests/test_cloudformation/test_import_value.py index 6701a3523..82538cba3 100644 --- a/tests/test_cloudformation/test_import_value.py +++ b/tests/test_cloudformation/test_import_value.py @@ -1,4 +1,4 @@ -from __future__ import absolute_import, division, print_function +from __future__ import absolute_import, division # Standard library modules import unittest diff --git a/tests/test_cloudformation/test_stack_parsing.py b/tests/test_cloudformation/test_stack_parsing.py index 3e6ec321e..e6383e61e 100644 --- a/tests/test_cloudformation/test_stack_parsing.py +++ b/tests/test_cloudformation/test_stack_parsing.py @@ -12,12 +12,12 @@ from moto.cloudformation.models import FakeStack from moto.cloudformation.parsing import ( resource_class_from_type, parse_condition, + Output, ) from moto import mock_cloudformation, mock_sqs, mock_ssm, settings from moto.sqs.models import Queue from moto.s3.models import FakeBucket from moto.cloudformation.utils import yaml_tag_constructor -from moto.packages.boto.cloudformation.stack import Output dummy_template = { diff --git a/tests/test_cloudwatch/test_cloudwatch.py b/tests/test_cloudwatch/test_cloudwatch.py deleted file mode 100644 index 769cdd57c..000000000 --- a/tests/test_cloudwatch/test_cloudwatch.py +++ /dev/null @@ -1,225 +0,0 @@ -import boto -from boto.ec2.cloudwatch.alarm import MetricAlarm -from boto.s3.key import Key -from datetime import datetime -import sure # noqa # pylint: disable=unused-import - -from moto import mock_cloudwatch_deprecated, mock_s3_deprecated - - -def alarm_fixture(name="tester", action=None): - action = action or ["arn:alarm"] - return MetricAlarm( - name=name, - namespace="{0}_namespace".format(name), - metric="{0}_metric".format(name), - comparison=">=", - threshold=2.0, - period=60, - evaluation_periods=5, - statistic="Average", - description="A test", - dimensions={"InstanceId": ["i-0123456,i-0123457"]}, - alarm_actions=action, - ok_actions=["arn:ok"], - insufficient_data_actions=["arn:insufficient"], - unit="Seconds", - ) - - -# Has boto3 equivalent -@mock_cloudwatch_deprecated -def test_create_alarm(): - conn = boto.connect_cloudwatch() - - alarm = alarm_fixture() - conn.create_alarm(alarm) - - alarms = conn.describe_alarms() - alarms.should.have.length_of(1) - alarm = alarms[0] - alarm.name.should.equal("tester") - alarm.namespace.should.equal("tester_namespace") - alarm.metric.should.equal("tester_metric") - alarm.comparison.should.equal(">=") - alarm.threshold.should.equal(2.0) - alarm.period.should.equal(60) - alarm.evaluation_periods.should.equal(5) - alarm.statistic.should.equal("Average") - alarm.description.should.equal("A test") - dict(alarm.dimensions).should.equal({"InstanceId": ["i-0123456,i-0123457"]}) - list(alarm.alarm_actions).should.equal(["arn:alarm"]) - list(alarm.ok_actions).should.equal(["arn:ok"]) - list(alarm.insufficient_data_actions).should.equal(["arn:insufficient"]) - alarm.unit.should.equal("Seconds") - assert "tester" in alarm.alarm_arn - - -# Has boto3 equivalent -@mock_cloudwatch_deprecated -def test_delete_alarm(): - conn = boto.connect_cloudwatch() - - alarms = conn.describe_alarms() - alarms.should.have.length_of(0) - - alarm = alarm_fixture() - conn.create_alarm(alarm) - - alarms = conn.describe_alarms() - alarms.should.have.length_of(1) - - alarms[0].delete() - - alarms = conn.describe_alarms() - alarms.should.have.length_of(0) - - -# Has boto3 equivalent -@mock_cloudwatch_deprecated -def test_put_metric_data(): - conn = boto.connect_cloudwatch() - - conn.put_metric_data( - namespace="tester", - name="metric", - value=1.5, - dimensions={"InstanceId": ["i-0123456,i-0123457"]}, - ) - - metrics = conn.list_metrics() - metric_names = [m for m in metrics if m.name == "metric"] - metric_names.should.have(1) - metric = metrics[0] - metric.namespace.should.equal("tester") - metric.name.should.equal("metric") - dict(metric.dimensions).should.equal({"InstanceId": ["i-0123456,i-0123457"]}) - - -# Has boto3 equivalent -@mock_cloudwatch_deprecated -def test_describe_alarms(): - conn = boto.connect_cloudwatch() - - alarms = conn.describe_alarms() - alarms.should.have.length_of(0) - - conn.create_alarm(alarm_fixture(name="nfoobar", action="afoobar")) - conn.create_alarm(alarm_fixture(name="nfoobaz", action="afoobaz")) - conn.create_alarm(alarm_fixture(name="nbarfoo", action="abarfoo")) - conn.create_alarm(alarm_fixture(name="nbazfoo", action="abazfoo")) - - enabled = alarm_fixture(name="enabled1", action=["abarfoo"]) - enabled.add_alarm_action("arn:alarm") - conn.create_alarm(enabled) - - alarms = conn.describe_alarms() - alarms.should.have.length_of(5) - alarms = conn.describe_alarms(alarm_name_prefix="nfoo") - alarms.should.have.length_of(2) - alarms = conn.describe_alarms(alarm_names=["nfoobar", "nbarfoo", "nbazfoo"]) - alarms.should.have.length_of(3) - alarms = conn.describe_alarms(action_prefix="afoo") - alarms.should.have.length_of(2) - alarms = conn.describe_alarms(alarm_name_prefix="enabled") - alarms.should.have.length_of(1) - alarms[0].actions_enabled.should.equal("true") - - for alarm in conn.describe_alarms(): - alarm.delete() - - alarms = conn.describe_alarms() - alarms.should.have.length_of(0) - - -# Has boto3 equivalent -@mock_cloudwatch_deprecated -def test_describe_alarms_for_metric(): - conn = boto.connect_cloudwatch() - - conn.create_alarm(alarm_fixture(name="nfoobar", action="afoobar")) - conn.create_alarm(alarm_fixture(name="nfoobaz", action="afoobaz")) - conn.create_alarm(alarm_fixture(name="nbarfoo", action="abarfoo")) - conn.create_alarm(alarm_fixture(name="nbazfoo", action="abazfoo")) - - alarms = conn.describe_alarms_for_metric("nbarfoo_metric", "nbarfoo_namespace") - alarms.should.have.length_of(1) - - alarms = conn.describe_alarms_for_metric("nbazfoo_metric", "nbazfoo_namespace") - alarms.should.have.length_of(1) - - -# Has boto3 equivalent -@mock_cloudwatch_deprecated -def test_get_metric_statistics(): - conn = boto.connect_cloudwatch() - - metric_timestamp = datetime(2018, 4, 9, 13, 0, 0, 0) - - conn.put_metric_data( - namespace="tester", - name="metric", - value=1.5, - dimensions={"InstanceId": ["i-0123456,i-0123457"]}, - timestamp=metric_timestamp, - unit="Count", - ) - - metric_kwargs = dict( - namespace="tester", - metric_name="metric", - start_time=metric_timestamp, - end_time=datetime.now(), - period=3600, - statistics=["Minimum"], - unit="Count", - ) - - datapoints = conn.get_metric_statistics(**metric_kwargs) - datapoints.should.have.length_of(1) - datapoint = datapoints[0] - datapoint.should.have.key("Minimum").which.should.equal(1.5) - datapoint.should.have.key("Timestamp").which.should.equal(metric_timestamp) - - metric_kwargs = dict( - namespace="tester", - metric_name="metric", - start_time=metric_timestamp, - end_time=datetime.now(), - period=3600, - statistics=["Minimum"], - unit="Percent", - ) - - datapoints = conn.get_metric_statistics(**metric_kwargs) - datapoints.should.have.length_of(0) - - -@mock_s3_deprecated -@mock_cloudwatch_deprecated -def test_cloudwatch_return_s3_metrics(): - - region = "us-east-1" - - cw = boto.ec2.cloudwatch.connect_to_region(region) - s3 = boto.s3.connect_to_region(region) - bucket_name_1 = "test-bucket-1" - bucket_name_2 = "test-bucket-2" - - bucket1 = s3.create_bucket(bucket_name=bucket_name_1) - key = Key(bucket1) - key.key = "the-key" - key.set_contents_from_string("foobar" * 4) - s3.create_bucket(bucket_name=bucket_name_2) - - metrics_s3_bucket_1 = cw.list_metrics(dimensions={"BucketName": bucket_name_1}) - - # Verify that the OOTB S3 metrics are available for the created buckets - len(metrics_s3_bucket_1).should.be(2) - metric_names = [m.name for m in metrics_s3_bucket_1] - sorted(metric_names).should.equal(["BucketSizeBytes", "NumberOfObjects"]) - - # Delete everything, to make sure it's not picked up in later tests - bucket1.delete_key("the-key") - s3.delete_bucket("test-bucket-1") - s3.delete_bucket("test-bucket-2") diff --git a/tests/test_cloudwatch/test_cloudwatch_boto3.py b/tests/test_cloudwatch/test_cloudwatch_boto3.py index e27008980..35f4db783 100644 --- a/tests/test_cloudwatch/test_cloudwatch_boto3.py +++ b/tests/test_cloudwatch/test_cloudwatch_boto3.py @@ -24,10 +24,9 @@ def test_put_metric_data_no_dimensions(): ) metrics = conn.list_metrics()["Metrics"] - metrics.should.have.length_of(1) - metric = metrics[0] - metric["Namespace"].should.equal("tester") - metric["MetricName"].should.equal("metric") + metrics.should.contain( + {"Namespace": "tester", "MetricName": "metric", "Dimensions": []} + ) @mock_cloudwatch @@ -75,10 +74,9 @@ def test_put_metric_data_with_statistics(): ) metrics = conn.list_metrics()["Metrics"] - metrics.should.have.length_of(1) - metric = metrics[0] - metric["Namespace"].should.equal("tester") - metric["MetricName"].should.equal("statmetric") + metrics.should.contain( + {"Namespace": "tester", "MetricName": "statmetric", "Dimensions": []} + ) # TODO: test statistics - https://github.com/spulec/moto/issues/1615 @@ -170,7 +168,6 @@ def test_get_metric_statistics_dimensions(): Statistics=["Average", "Sum"], **params[0], ) - print(stats) stats["Datapoints"].should.have.length_of(1) datapoint = stats["Datapoints"][0] datapoint["Sum"].should.equal(params[1]) @@ -319,14 +316,14 @@ def test_list_metrics(): create_metrics(cloudwatch, namespace="list_test_2/", metrics=4, data_points=2) # Verify we can retrieve everything res = cloudwatch.list_metrics()["Metrics"] - len(res).should.equal(16) # 2 namespaces * 4 metrics * 2 data points + assert len(res) >= 16 # 2 namespaces * 4 metrics * 2 data points # Verify we can filter by namespace/metric name res = cloudwatch.list_metrics(Namespace="list_test_1/")["Metrics"] - len(res).should.equal(8) # 1 namespace * 4 metrics * 2 data points + res.should.have.length_of(8) # 1 namespace * 4 metrics * 2 data points res = cloudwatch.list_metrics(Namespace="list_test_1/", MetricName="metric1")[ "Metrics" ] - len(res).should.equal(2) # 1 namespace * 1 metrics * 2 data points + res.should.have.length_of(2) # 1 namespace * 1 metrics * 2 data points # Verify format res.should.equal( [ @@ -353,29 +350,34 @@ def test_list_metrics_paginated(): # Add a boatload of metrics create_metrics(cloudwatch, namespace="test", metrics=100, data_points=1) # Verify that a single page is returned until we've reached 500 - first_page = cloudwatch.list_metrics() + first_page = cloudwatch.list_metrics(Namespace="test") first_page["Metrics"].shouldnt.be.empty + len(first_page["Metrics"]).should.equal(100) create_metrics(cloudwatch, namespace="test", metrics=200, data_points=2) - first_page = cloudwatch.list_metrics() + first_page = cloudwatch.list_metrics(Namespace="test") len(first_page["Metrics"]).should.equal(500) first_page.shouldnt.contain("NextToken") # Verify that adding more data points results in pagination create_metrics(cloudwatch, namespace="test", metrics=60, data_points=10) - first_page = cloudwatch.list_metrics() + first_page = cloudwatch.list_metrics(Namespace="test") len(first_page["Metrics"]).should.equal(500) first_page["NextToken"].shouldnt.be.empty # Retrieve second page - and verify there's more where that came from - second_page = cloudwatch.list_metrics(NextToken=first_page["NextToken"]) + second_page = cloudwatch.list_metrics( + Namespace="test", NextToken=first_page["NextToken"] + ) len(second_page["Metrics"]).should.equal(500) second_page.should.contain("NextToken") # Last page should only have the last 100 results, and no NextToken (indicating that pagination is finished) - third_page = cloudwatch.list_metrics(NextToken=second_page["NextToken"]) + third_page = cloudwatch.list_metrics( + Namespace="test", NextToken=second_page["NextToken"] + ) len(third_page["Metrics"]).should.equal(100) third_page.shouldnt.contain("NextToken") # Verify that we can't reuse an existing token with pytest.raises(ClientError) as e: - cloudwatch.list_metrics(NextToken=first_page["NextToken"]) + cloudwatch.list_metrics(Namespace="test", NextToken=first_page["NextToken"]) e.value.response["Error"]["Message"].should.equal( "Request parameter NextToken is invalid" ) @@ -427,7 +429,7 @@ def test_list_metrics_with_same_dimensions_different_metric_name(): ], ) - results = cloudwatch.list_metrics()["Metrics"] + results = cloudwatch.list_metrics(Namespace="unique/")["Metrics"] results.should.have.length_of(2) # duplicating existing metric @@ -443,7 +445,7 @@ def test_list_metrics_with_same_dimensions_different_metric_name(): ) # asserting only unique values are returned - results = cloudwatch.list_metrics()["Metrics"] + results = cloudwatch.list_metrics(Namespace="unique/")["Metrics"] results.should.have.length_of(2) diff --git a/tests/test_core/test_decorator_calls.py b/tests/test_core/test_decorator_calls.py index ad0109ef2..ed70f7f5a 100644 --- a/tests/test_core/test_decorator_calls.py +++ b/tests/test_core/test_decorator_calls.py @@ -1,60 +1,70 @@ -import boto -from boto.exception import EC2ResponseError +import boto3 import sure # noqa # pylint: disable=unused-import +import os import unittest import pytest -from moto import mock_ec2_deprecated, mock_s3_deprecated +from botocore.exceptions import ClientError +from moto import mock_ec2, mock_s3, settings +from unittest import SkipTest """ Test the different ways that the decorator can be used """ -@mock_ec2_deprecated -def test_basic_connect(): - boto.connect_ec2() - - -@mock_ec2_deprecated +@mock_ec2 def test_basic_decorator(): - conn = boto.connect_ec2("the_key", "the_secret") - list(conn.get_all_reservations()).should.equal([]) + client = boto3.client("ec2", region_name="us-west-1") + client.describe_addresses()["Addresses"].should.equal([]) + + +@pytest.fixture +def aws_credentials(): + """Mocked AWS Credentials for moto.""" + os.environ["AWS_ACCESS_KEY_ID"] = "testing" + os.environ["AWS_SECRET_ACCESS_KEY"] = "testing" + os.environ["AWS_SECURITY_TOKEN"] = "testing" + os.environ["AWS_SESSION_TOKEN"] = "testing" @pytest.mark.network -def test_context_manager(): - conn = boto.connect_ec2("the_key", "the_secret") - with pytest.raises(EC2ResponseError): - conn.get_all_reservations() +def test_context_manager(aws_credentials): + client = boto3.client("ec2", region_name="us-west-1") + with pytest.raises(ClientError) as exc: + client.describe_addresses() + err = exc.value.response["Error"] + err["Code"].should.equal("AuthFailure") + err["Message"].should.equal( + "AWS was not able to validate the provided access credentials" + ) - with mock_ec2_deprecated(): - conn = boto.connect_ec2("the_key", "the_secret") - list(conn.get_all_reservations()).should.equal([]) - - with pytest.raises(EC2ResponseError): - conn = boto.connect_ec2("the_key", "the_secret") - conn.get_all_reservations() + with mock_ec2(): + client = boto3.client("ec2", region_name="us-west-1") + client.describe_addresses()["Addresses"].should.equal([]) @pytest.mark.network def test_decorator_start_and_stop(): - conn = boto.connect_ec2("the_key", "the_secret") - with pytest.raises(EC2ResponseError): - conn.get_all_reservations() - - mock = mock_ec2_deprecated() + if settings.TEST_SERVER_MODE: + raise SkipTest("Authentication always works in ServerMode") + mock = mock_ec2() mock.start() - conn = boto.connect_ec2("the_key", "the_secret") - list(conn.get_all_reservations()).should.equal([]) + client = boto3.client("ec2", region_name="us-west-1") + client.describe_addresses()["Addresses"].should.equal([]) mock.stop() - with pytest.raises(EC2ResponseError): - conn.get_all_reservations() + with pytest.raises(ClientError) as exc: + client.describe_addresses() + err = exc.value.response["Error"] + err["Code"].should.equal("AuthFailure") + err["Message"].should.equal( + "AWS was not able to validate the provided access credentials" + ) -@mock_ec2_deprecated +@mock_ec2 def test_decorater_wrapped_gets_set(): """ Moto decorator's __wrapped__ should get set to the tests function @@ -64,29 +74,32 @@ def test_decorater_wrapped_gets_set(): ) -@mock_ec2_deprecated +@mock_ec2 class Tester(object): def test_the_class(self): - conn = boto.connect_ec2() - list(conn.get_all_reservations()).should.have.length_of(0) + client = boto3.client("ec2", region_name="us-west-1") + client.describe_addresses()["Addresses"].should.equal([]) def test_still_the_same(self): - conn = boto.connect_ec2() - list(conn.get_all_reservations()).should.have.length_of(0) + client = boto3.client("ec2", region_name="us-west-1") + client.describe_addresses()["Addresses"].should.equal([]) -@mock_s3_deprecated +@mock_s3 class TesterWithSetup(unittest.TestCase): def setUp(self): - self.conn = boto.connect_s3() - self.conn.create_bucket("mybucket") + self.client = boto3.client("s3") + self.client.create_bucket(Bucket="mybucket") def test_still_the_same(self): - bucket = self.conn.get_bucket("mybucket") - bucket.name.should.equal("mybucket") + buckets = self.client.list_buckets()["Buckets"] + bucket_names = [b["Name"] for b in buckets] + # There is a potential bug in the class-decorator, where the reset API is not called on start. + # This leads to a situation where 'bucket_names' may contain buckets created by earlier tests + bucket_names.should.contain("mybucket") -@mock_s3_deprecated +@mock_s3 class TesterWithStaticmethod(object): @staticmethod def static(*args): diff --git a/tests/test_core/test_nested.py b/tests/test_core/test_nested.py index f061ea8f6..7af3f64d4 100644 --- a/tests/test_core/test_nested.py +++ b/tests/test_core/test_nested.py @@ -1,38 +1,12 @@ import sure # noqa # pylint: disable=unused-import import unittest -from boto.sqs.connection import SQSConnection -from boto.sqs.message import Message -from boto.ec2 import EC2Connection import boto3 -from moto import mock_sqs_deprecated, mock_ec2_deprecated from moto import mock_sqs, mock_ec2 from tests import EXAMPLE_AMI_ID -class TestNestedDecorators(unittest.TestCase): - # Has boto3 equivalent - @mock_sqs_deprecated - def setup_sqs_queue(self): - conn = SQSConnection() - q = conn.create_queue("some-queue") - - m = Message() - m.set_body("This is my first message.") - q.write(m) - - self.assertEqual(q.count(), 1) - - # Has boto3 equivalent - @mock_ec2_deprecated - def test_nested(self): - self.setup_sqs_queue() - - conn = EC2Connection() - conn.run_instances(EXAMPLE_AMI_ID) - - class TestNestedDecoratorsBoto3(unittest.TestCase): @mock_sqs def setup_sqs_queue(self): diff --git a/tests/test_core/test_socket.py b/tests/test_core/test_socket.py index c8b829ed4..c0ad836aa 100644 --- a/tests/test_core/test_socket.py +++ b/tests/test_core/test_socket.py @@ -1,37 +1,9 @@ import unittest -from moto import mock_dynamodb2_deprecated, mock_dynamodb2 +from moto import mock_dynamodb2 import socket class TestSocketPair(unittest.TestCase): - @mock_dynamodb2_deprecated - def test_asyncio_deprecated(self): - self.assertIn( - "httpretty.core.fakesock.socket", - str(socket.socket), - "Our mock should be present", - ) - import asyncio - - self.assertIsNotNone(asyncio.get_event_loop()) - - # Has boto3 equivalent - @mock_dynamodb2_deprecated - def test_socket_pair_deprecated(self): - - self.assertIn( - "httpretty.core.fakesock.socket", - str(socket.socket), - "Our mock should be present", - ) - a, b = socket.socketpair() - self.assertIsNotNone(a) - self.assertIsNotNone(b) - if a: - a.close() - if b: - b.close() - @mock_dynamodb2 def test_socket_pair(self): a, b = socket.socketpair() diff --git a/tests/test_core/test_url_base_regex.py b/tests/test_core/test_url_base_regex.py index a9a1bf278..1d201218d 100644 --- a/tests/test_core/test_url_base_regex.py +++ b/tests/test_core/test_url_base_regex.py @@ -7,10 +7,7 @@ from moto import mock_s3 service_names = [ (d[5:], "") for d in dir(moto) - if d.startswith("mock_") - and not d.endswith("_deprecated") - and not d == "mock_xray_client" - and not d == "mock_all" + if d.startswith("mock_") and not d == "mock_xray_client" and not d == "mock_all" ] diff --git a/tests/test_datapipeline/test_datapipeline.py b/tests/test_datapipeline/test_datapipeline.py index 60f8edf95..b9eeeaee0 100644 --- a/tests/test_datapipeline/test_datapipeline.py +++ b/tests/test_datapipeline/test_datapipeline.py @@ -1,9 +1,7 @@ -import boto.datapipeline -import sure # noqa # pylint: disable=unused-import import boto3 +import sure # noqa # pylint: disable=unused-import from moto import mock_datapipeline -from moto import mock_datapipeline_deprecated from moto.datapipeline.utils import remove_capitalization_of_dict_keys @@ -13,28 +11,6 @@ def get_value_from_fields(key, fields): return field["stringValue"] -# Has boto3 equivalent -@mock_datapipeline_deprecated -def test_create_pipeline(): - conn = boto.datapipeline.connect_to_region("us-west-2") - - res = conn.create_pipeline("mypipeline", "some-unique-id") - - pipeline_id = res["pipelineId"] - pipeline_descriptions = conn.describe_pipelines([pipeline_id])[ - "pipelineDescriptionList" - ] - pipeline_descriptions.should.have.length_of(1) - - pipeline_description = pipeline_descriptions[0] - pipeline_description["name"].should.equal("mypipeline") - pipeline_description["pipelineId"].should.equal(pipeline_id) - fields = pipeline_description["fields"] - - get_value_from_fields("@pipelineState", fields).should.equal("PENDING") - get_value_from_fields("uniqueId", fields).should.equal("some-unique-id") - - @mock_datapipeline def test_create_pipeline_boto3(): conn = boto3.client("datapipeline", region_name="us-west-2") @@ -85,25 +61,6 @@ PIPELINE_OBJECTS = [ ] -# Has boto3 equivalent -@mock_datapipeline_deprecated -def test_creating_pipeline_definition(): - conn = boto.datapipeline.connect_to_region("us-west-2") - res = conn.create_pipeline("mypipeline", "some-unique-id") - pipeline_id = res["pipelineId"] - - conn.put_pipeline_definition(PIPELINE_OBJECTS, pipeline_id) - - pipeline_definition = conn.get_pipeline_definition(pipeline_id) - pipeline_definition["pipelineObjects"].should.have.length_of(3) - default_object = pipeline_definition["pipelineObjects"][0] - default_object["name"].should.equal("Default") - default_object["id"].should.equal("Default") - default_object["fields"].should.equal( - [{"key": "workerGroup", "stringValue": "workerGroup"}] - ) - - @mock_datapipeline def test_creating_pipeline_definition_boto3(): conn = boto3.client("datapipeline", region_name="us-west-2") @@ -124,27 +81,6 @@ def test_creating_pipeline_definition_boto3(): ) -# Has boto3 equivalent -@mock_datapipeline_deprecated -def test_describing_pipeline_objects(): - conn = boto.datapipeline.connect_to_region("us-west-2") - res = conn.create_pipeline("mypipeline", "some-unique-id") - pipeline_id = res["pipelineId"] - - conn.put_pipeline_definition(PIPELINE_OBJECTS, pipeline_id) - - objects = conn.describe_objects(["Schedule", "Default"], pipeline_id)[ - "pipelineObjects" - ] - - objects.should.have.length_of(2) - default_object = [x for x in objects if x["id"] == "Default"][0] - default_object["name"].should.equal("Default") - default_object["fields"].should.equal( - [{"key": "workerGroup", "stringValue": "workerGroup"}] - ) - - @mock_datapipeline def test_describing_pipeline_objects_boto3(): conn = boto3.client("datapipeline", region_name="us-west-2") @@ -167,26 +103,6 @@ def test_describing_pipeline_objects_boto3(): ) -# Has boto3 equivalent -@mock_datapipeline_deprecated -def test_activate_pipeline(): - conn = boto.datapipeline.connect_to_region("us-west-2") - - res = conn.create_pipeline("mypipeline", "some-unique-id") - - pipeline_id = res["pipelineId"] - conn.activate_pipeline(pipeline_id) - - pipeline_descriptions = conn.describe_pipelines([pipeline_id])[ - "pipelineDescriptionList" - ] - pipeline_descriptions.should.have.length_of(1) - pipeline_description = pipeline_descriptions[0] - fields = pipeline_description["fields"] - - get_value_from_fields("@pipelineState", fields).should.equal("SCHEDULED") - - @mock_datapipeline def test_activate_pipeline_boto3(): conn = boto3.client("datapipeline", region_name="us-west-2") @@ -205,20 +121,6 @@ def test_activate_pipeline_boto3(): get_value_from_fields("@pipelineState", fields).should.equal("SCHEDULED") -# Has boto3 equivalent -@mock_datapipeline_deprecated -def test_delete_pipeline(): - conn = boto.datapipeline.connect_to_region("us-west-2") - res = conn.create_pipeline("mypipeline", "some-unique-id") - pipeline_id = res["pipelineId"] - - conn.delete_pipeline(pipeline_id) - - response = conn.list_pipelines() - - response["pipelineIdList"].should.have.length_of(0) - - @mock_datapipeline def test_delete_pipeline_boto3(): conn = boto3.client("datapipeline", region_name="us-west-2") @@ -232,26 +134,6 @@ def test_delete_pipeline_boto3(): response["pipelineIdList"].should.have.length_of(0) -# Has boto3 equivalent -@mock_datapipeline_deprecated -def test_listing_pipelines(): - conn = boto.datapipeline.connect_to_region("us-west-2") - res1 = conn.create_pipeline("mypipeline1", "some-unique-id1") - res2 = conn.create_pipeline("mypipeline2", "some-unique-id2") - - response = conn.list_pipelines() - - response["hasMoreResults"].should.be(False) - response["marker"].should.be.none - response["pipelineIdList"].should.have.length_of(2) - response["pipelineIdList"].should.contain( - {"id": res1["pipelineId"], "name": "mypipeline1"} - ) - response["pipelineIdList"].should.contain( - {"id": res2["pipelineId"], "name": "mypipeline2"} - ) - - @mock_datapipeline def test_listing_pipelines_boto3(): conn = boto3.client("datapipeline", region_name="us-west-2") @@ -271,20 +153,6 @@ def test_listing_pipelines_boto3(): ) -# Has boto3 equivalent -@mock_datapipeline_deprecated -def test_listing_paginated_pipelines(): - conn = boto.datapipeline.connect_to_region("us-west-2") - for i in range(100): - conn.create_pipeline("mypipeline%d" % i, "some-unique-id%d" % i) - - response = conn.list_pipelines() - - response["hasMoreResults"].should.be(True) - response["marker"].should.equal(response["pipelineIdList"][-1]["id"]) - response["pipelineIdList"].should.have.length_of(50) - - @mock_datapipeline def test_listing_paginated_pipelines_boto3(): conn = boto3.client("datapipeline", region_name="us-west-2") diff --git a/tests/test_dynamodb/test_dynamodb.py b/tests/test_dynamodb/test_dynamodb.py index 7a36446be..fcf165909 100644 --- a/tests/test_dynamodb/test_dynamodb.py +++ b/tests/test_dynamodb/test_dynamodb.py @@ -1,12 +1,7 @@ -import boto -import boto.dynamodb import sure # noqa # pylint: disable=unused-import import pytest -from moto import mock_dynamodb, mock_dynamodb_deprecated -from moto.dynamodb import dynamodb_backend - -from boto.exception import DynamoDBResponseError +from moto import mock_dynamodb def test_deprecation_warning(): @@ -15,45 +10,3 @@ def test_deprecation_warning(): str(record[0].message).should.contain( "Module mock_dynamodb has been deprecated, and will be repurposed in a later release" ) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_list_tables(): - name = "TestTable" - dynamodb_backend.create_table(name, hash_key_attr="name", hash_key_type="S") - conn = boto.connect_dynamodb("the_key", "the_secret") - assert conn.list_tables() == ["TestTable"] - - -@mock_dynamodb_deprecated -def test_list_tables_layer_1(): - dynamodb_backend.create_table("test_1", hash_key_attr="name", hash_key_type="S") - dynamodb_backend.create_table("test_2", hash_key_attr="name", hash_key_type="S") - conn = boto.connect_dynamodb("the_key", "the_secret") - res = conn.layer1.list_tables(limit=1) - expected = {"TableNames": ["test_1"], "LastEvaluatedTableName": "test_1"} - res.should.equal(expected) - - res = conn.layer1.list_tables(limit=1, start_table="test_1") - expected = {"TableNames": ["test_2"]} - res.should.equal(expected) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_describe_missing_table(): - conn = boto.connect_dynamodb("the_key", "the_secret") - with pytest.raises(DynamoDBResponseError): - conn.describe_table("messages") - - -# Has boto3 equivalent -# This test is pointless though, as we treat DynamoDB as a global resource -@mock_dynamodb_deprecated -def test_dynamodb_with_connect_to_region(): - # this will work if connected with boto.connect_dynamodb() - dynamodb = boto.dynamodb.connect_to_region("us-west-2") - - schema = dynamodb.create_schema("column1", str(), "column2", int()) - dynamodb.create_table("table1", schema, 200, 200) diff --git a/tests/test_dynamodb/test_dynamodb_table_with_range_key.py b/tests/test_dynamodb/test_dynamodb_table_with_range_key.py deleted file mode 100644 index 64d626471..000000000 --- a/tests/test_dynamodb/test_dynamodb_table_with_range_key.py +++ /dev/null @@ -1,485 +0,0 @@ -import boto -import sure # noqa # pylint: disable=unused-import -from freezegun import freeze_time - -from moto import mock_dynamodb_deprecated - -from boto.dynamodb import condition -from boto.dynamodb.exceptions import DynamoDBKeyNotFoundError, DynamoDBValidationError -from boto.exception import DynamoDBResponseError - - -def create_table(conn): - message_table_schema = conn.create_schema( - hash_key_name="forum_name", - hash_key_proto_value=str, - range_key_name="subject", - range_key_proto_value=str, - ) - - table = conn.create_table( - name="messages", schema=message_table_schema, read_units=10, write_units=10 - ) - return table - - -# Has boto3 equivalent -@freeze_time("2012-01-14") -@mock_dynamodb_deprecated -def test_create_table(): - conn = boto.connect_dynamodb() - create_table(conn) - - expected = { - "Table": { - "CreationDateTime": 1326499200.0, - "ItemCount": 0, - "KeySchema": { - "HashKeyElement": {"AttributeName": "forum_name", "AttributeType": "S"}, - "RangeKeyElement": {"AttributeName": "subject", "AttributeType": "S"}, - }, - "ProvisionedThroughput": { - "ReadCapacityUnits": 10, - "WriteCapacityUnits": 10, - }, - "TableName": "messages", - "TableSizeBytes": 0, - "TableStatus": "ACTIVE", - } - } - conn.describe_table("messages").should.equal(expected) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_delete_table(): - conn = boto.connect_dynamodb() - create_table(conn) - conn.list_tables().should.have.length_of(1) - - conn.layer1.delete_table("messages") - conn.list_tables().should.have.length_of(0) - - conn.layer1.delete_table.when.called_with("messages").should.throw( - DynamoDBResponseError - ) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_update_table_throughput(): - conn = boto.connect_dynamodb() - table = create_table(conn) - table.read_units.should.equal(10) - table.write_units.should.equal(10) - - table.update_throughput(5, 6) - table.refresh() - - table.read_units.should.equal(5) - table.write_units.should.equal(6) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_item_add_and_describe_and_update(): - conn = boto.connect_dynamodb() - table = create_table(conn) - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item = table.new_item( - hash_key="LOLCat Forum", range_key="Check this out!", attrs=item_data - ) - item.put() - - table.has_item("LOLCat Forum", "Check this out!").should.equal(True) - - returned_item = table.get_item( - hash_key="LOLCat Forum", - range_key="Check this out!", - attributes_to_get=["Body", "SentBy"], - ) - dict(returned_item).should.equal( - { - "forum_name": "LOLCat Forum", - "subject": "Check this out!", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - } - ) - - item["SentBy"] = "User B" - item.put() - - returned_item = table.get_item( - hash_key="LOLCat Forum", - range_key="Check this out!", - attributes_to_get=["Body", "SentBy"], - ) - dict(returned_item).should.equal( - { - "forum_name": "LOLCat Forum", - "subject": "Check this out!", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - } - ) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_item_put_without_table(): - conn = boto.connect_dynamodb() - - conn.layer1.put_item.when.called_with( - table_name="undeclared-table", - item=dict(hash_key="LOLCat Forum", range_key="Check this out!"), - ).should.throw(DynamoDBResponseError) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_get_missing_item(): - conn = boto.connect_dynamodb() - table = create_table(conn) - - table.get_item.when.called_with(hash_key="tester", range_key="other").should.throw( - DynamoDBKeyNotFoundError - ) - table.has_item("foobar", "more").should.equal(False) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_get_item_with_undeclared_table(): - conn = boto.connect_dynamodb() - - conn.layer1.get_item.when.called_with( - table_name="undeclared-table", - key={"HashKeyElement": {"S": "tester"}, "RangeKeyElement": {"S": "test-range"}}, - ).should.throw(DynamoDBKeyNotFoundError) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_get_item_without_range_key(): - conn = boto.connect_dynamodb() - message_table_schema = conn.create_schema( - hash_key_name="test_hash", - hash_key_proto_value=int, - range_key_name="test_range", - range_key_proto_value=int, - ) - table = conn.create_table( - name="messages", schema=message_table_schema, read_units=10, write_units=10 - ) - - hash_key = 3241526475 - range_key = 1234567890987 - new_item = table.new_item(hash_key=hash_key, range_key=range_key) - new_item.put() - - table.get_item.when.called_with(hash_key=hash_key).should.throw( - DynamoDBValidationError - ) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_delete_item(): - conn = boto.connect_dynamodb() - table = create_table(conn) - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item = table.new_item( - hash_key="LOLCat Forum", range_key="Check this out!", attrs=item_data - ) - item.put() - - table.refresh() - table.item_count.should.equal(1) - - response = item.delete() - response.should.equal({"Attributes": [], "ConsumedCapacityUnits": 0.5}) - table.refresh() - table.item_count.should.equal(0) - - item.delete.when.called_with().should.throw(DynamoDBResponseError) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_delete_item_with_attribute_response(): - conn = boto.connect_dynamodb() - table = create_table(conn) - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item = table.new_item( - hash_key="LOLCat Forum", range_key="Check this out!", attrs=item_data - ) - item.put() - - table.refresh() - table.item_count.should.equal(1) - - response = item.delete(return_values="ALL_OLD") - response.should.equal( - { - "Attributes": { - "Body": "http://url_to_lolcat.gif", - "forum_name": "LOLCat Forum", - "ReceivedTime": "12/9/2011 11:36:03 PM", - "SentBy": "User A", - "subject": "Check this out!", - }, - "ConsumedCapacityUnits": 0.5, - } - ) - table.refresh() - table.item_count.should.equal(0) - - item.delete.when.called_with().should.throw(DynamoDBResponseError) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_delete_item_with_undeclared_table(): - conn = boto.connect_dynamodb() - - conn.layer1.delete_item.when.called_with( - table_name="undeclared-table", - key={"HashKeyElement": {"S": "tester"}, "RangeKeyElement": {"S": "test-range"}}, - ).should.throw(DynamoDBResponseError) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_query(): - conn = boto.connect_dynamodb() - table = create_table(conn) - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item = table.new_item(hash_key="the-key", range_key="456", attrs=item_data) - item.put() - - item = table.new_item(hash_key="the-key", range_key="123", attrs=item_data) - item.put() - - item = table.new_item(hash_key="the-key", range_key="789", attrs=item_data) - item.put() - - results = table.query(hash_key="the-key", range_key_condition=condition.GT("1")) - results.response["Items"].should.have.length_of(3) - - results = table.query(hash_key="the-key", range_key_condition=condition.GT("234")) - results.response["Items"].should.have.length_of(2) - - results = table.query(hash_key="the-key", range_key_condition=condition.GT("9999")) - results.response["Items"].should.have.length_of(0) - - results = table.query( - hash_key="the-key", range_key_condition=condition.CONTAINS("12") - ) - results.response["Items"].should.have.length_of(1) - - results = table.query( - hash_key="the-key", range_key_condition=condition.BEGINS_WITH("7") - ) - results.response["Items"].should.have.length_of(1) - - results = table.query( - hash_key="the-key", range_key_condition=condition.BETWEEN("567", "890") - ) - results.response["Items"].should.have.length_of(1) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_query_with_undeclared_table(): - conn = boto.connect_dynamodb() - - conn.layer1.query.when.called_with( - table_name="undeclared-table", - hash_key_value={"S": "the-key"}, - range_key_conditions={ - "AttributeValueList": [{"S": "User B"}], - "ComparisonOperator": "EQ", - }, - ).should.throw(DynamoDBResponseError) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_scan(): - conn = boto.connect_dynamodb() - table = create_table(conn) - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item = table.new_item(hash_key="the-key", range_key="456", attrs=item_data) - item.put() - - item = table.new_item(hash_key="the-key", range_key="123", attrs=item_data) - item.put() - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - "ReceivedTime": "12/9/2011 11:36:03 PM", - "Ids": set([1, 2, 3]), - "PK": 7, - } - item = table.new_item(hash_key="the-key", range_key="789", attrs=item_data) - item.put() - - results = table.scan() - results.response["Items"].should.have.length_of(3) - - results = table.scan(scan_filter={"SentBy": condition.EQ("User B")}) - results.response["Items"].should.have.length_of(1) - - results = table.scan(scan_filter={"Body": condition.BEGINS_WITH("http")}) - results.response["Items"].should.have.length_of(3) - - results = table.scan(scan_filter={"Ids": condition.CONTAINS(2)}) - results.response["Items"].should.have.length_of(1) - - results = table.scan(scan_filter={"Ids": condition.NOT_NULL()}) - results.response["Items"].should.have.length_of(1) - - results = table.scan(scan_filter={"Ids": condition.NULL()}) - results.response["Items"].should.have.length_of(2) - - results = table.scan(scan_filter={"PK": condition.BETWEEN(8, 9)}) - results.response["Items"].should.have.length_of(0) - - results = table.scan(scan_filter={"PK": condition.BETWEEN(5, 8)}) - results.response["Items"].should.have.length_of(1) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_scan_with_undeclared_table(): - conn = boto.connect_dynamodb() - - conn.layer1.scan.when.called_with( - table_name="undeclared-table", - scan_filter={ - "SentBy": { - "AttributeValueList": [{"S": "User B"}], - "ComparisonOperator": "EQ", - } - }, - ).should.throw(DynamoDBResponseError) - - -@mock_dynamodb_deprecated -def test_scan_after_has_item(): - conn = boto.connect_dynamodb() - table = create_table(conn) - list(table.scan()).should.equal([]) - - table.has_item(hash_key="the-key", range_key="123") - - list(table.scan()).should.equal([]) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_write_batch(): - conn = boto.connect_dynamodb() - table = create_table(conn) - - batch_list = conn.new_batch_write_list() - - items = [] - items.append( - table.new_item( - hash_key="the-key", - range_key="123", - attrs={ - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - }, - ) - ) - - items.append( - table.new_item( - hash_key="the-key", - range_key="789", - attrs={ - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - "ReceivedTime": "12/9/2011 11:36:03 PM", - "Ids": set([1, 2, 3]), - "PK": 7, - }, - ) - ) - - batch_list.add_batch(table, puts=items) - conn.batch_write_item(batch_list) - - table.refresh() - table.item_count.should.equal(2) - - batch_list = conn.new_batch_write_list() - batch_list.add_batch(table, deletes=[("the-key", "789")]) - conn.batch_write_item(batch_list) - - table.refresh() - table.item_count.should.equal(1) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_batch_read(): - conn = boto.connect_dynamodb() - table = create_table(conn) - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item = table.new_item(hash_key="the-key", range_key="456", attrs=item_data) - item.put() - - item = table.new_item(hash_key="the-key", range_key="123", attrs=item_data) - item.put() - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - "ReceivedTime": "12/9/2011 11:36:03 PM", - "Ids": set([1, 2, 3]), - "PK": 7, - } - item = table.new_item(hash_key="another-key", range_key="789", attrs=item_data) - item.put() - - items = table.batch_get_item([("the-key", "123"), ("another-key", "789")]) - # Iterate through so that batch_item gets called - count = len([x for x in items]) - count.should.equal(2) diff --git a/tests/test_dynamodb/test_dynamodb_table_without_range_key.py b/tests/test_dynamodb/test_dynamodb_table_without_range_key.py deleted file mode 100644 index 336a11682..000000000 --- a/tests/test_dynamodb/test_dynamodb_table_without_range_key.py +++ /dev/null @@ -1,405 +0,0 @@ -import boto -import sure # noqa # pylint: disable=unused-import -from freezegun import freeze_time - -from moto import mock_dynamodb_deprecated - -from boto.dynamodb import condition -from boto.dynamodb.exceptions import DynamoDBKeyNotFoundError -from boto.exception import DynamoDBResponseError - - -def create_table(conn): - message_table_schema = conn.create_schema( - hash_key_name="forum_name", hash_key_proto_value=str - ) - - table = conn.create_table( - name="messages", schema=message_table_schema, read_units=10, write_units=10 - ) - return table - - -# Has boto3 equivalent -@freeze_time("2012-01-14") -@mock_dynamodb_deprecated -def test_create_table(): - conn = boto.connect_dynamodb() - create_table(conn) - - expected = { - "Table": { - "CreationDateTime": 1326499200.0, - "ItemCount": 0, - "KeySchema": { - "HashKeyElement": {"AttributeName": "forum_name", "AttributeType": "S"} - }, - "ProvisionedThroughput": { - "ReadCapacityUnits": 10, - "WriteCapacityUnits": 10, - }, - "TableName": "messages", - "TableSizeBytes": 0, - "TableStatus": "ACTIVE", - } - } - conn.describe_table("messages").should.equal(expected) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_delete_table(): - conn = boto.connect_dynamodb() - create_table(conn) - conn.list_tables().should.have.length_of(1) - - conn.layer1.delete_table("messages") - conn.list_tables().should.have.length_of(0) - - conn.layer1.delete_table.when.called_with("messages").should.throw( - DynamoDBResponseError - ) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_update_table_throughput(): - conn = boto.connect_dynamodb() - table = create_table(conn) - table.read_units.should.equal(10) - table.write_units.should.equal(10) - - table.update_throughput(5, 6) - table.refresh() - - table.read_units.should.equal(5) - table.write_units.should.equal(6) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_item_add_and_describe_and_update(): - conn = boto.connect_dynamodb() - table = create_table(conn) - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item = table.new_item(hash_key="LOLCat Forum", attrs=item_data) - item.put() - - returned_item = table.get_item( - hash_key="LOLCat Forum", attributes_to_get=["Body", "SentBy"] - ) - dict(returned_item).should.equal( - { - "forum_name": "LOLCat Forum", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - } - ) - - item["SentBy"] = "User B" - item.put() - - returned_item = table.get_item( - hash_key="LOLCat Forum", attributes_to_get=["Body", "SentBy"] - ) - dict(returned_item).should.equal( - { - "forum_name": "LOLCat Forum", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - } - ) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_item_put_without_table(): - conn = boto.connect_dynamodb() - - conn.layer1.put_item.when.called_with( - table_name="undeclared-table", item=dict(hash_key="LOLCat Forum") - ).should.throw(DynamoDBResponseError) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_get_missing_item(): - conn = boto.connect_dynamodb() - table = create_table(conn) - - table.get_item.when.called_with(hash_key="tester").should.throw( - DynamoDBKeyNotFoundError - ) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_get_item_with_undeclared_table(): - conn = boto.connect_dynamodb() - - conn.layer1.get_item.when.called_with( - table_name="undeclared-table", key={"HashKeyElement": {"S": "tester"}} - ).should.throw(DynamoDBKeyNotFoundError) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_delete_item(): - conn = boto.connect_dynamodb() - table = create_table(conn) - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item = table.new_item(hash_key="LOLCat Forum", attrs=item_data) - item.put() - - table.refresh() - table.item_count.should.equal(1) - - response = item.delete() - response.should.equal({"Attributes": [], "ConsumedCapacityUnits": 0.5}) - table.refresh() - table.item_count.should.equal(0) - - item.delete.when.called_with().should.throw(DynamoDBResponseError) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_delete_item_with_attribute_response(): - conn = boto.connect_dynamodb() - table = create_table(conn) - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item = table.new_item(hash_key="LOLCat Forum", attrs=item_data) - item.put() - - table.refresh() - table.item_count.should.equal(1) - - response = item.delete(return_values="ALL_OLD") - response.should.equal( - { - "Attributes": { - "Body": "http://url_to_lolcat.gif", - "forum_name": "LOLCat Forum", - "ReceivedTime": "12/9/2011 11:36:03 PM", - "SentBy": "User A", - }, - "ConsumedCapacityUnits": 0.5, - } - ) - table.refresh() - table.item_count.should.equal(0) - - item.delete.when.called_with().should.throw(DynamoDBResponseError) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_delete_item_with_undeclared_table(): - conn = boto.connect_dynamodb() - - conn.layer1.delete_item.when.called_with( - table_name="undeclared-table", key={"HashKeyElement": {"S": "tester"}} - ).should.throw(DynamoDBResponseError) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_query(): - conn = boto.connect_dynamodb() - table = create_table(conn) - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item = table.new_item(hash_key="the-key", attrs=item_data) - item.put() - - results = table.query(hash_key="the-key") - results.response["Items"].should.have.length_of(1) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_query_with_undeclared_table(): - conn = boto.connect_dynamodb() - - conn.layer1.query.when.called_with( - table_name="undeclared-table", hash_key_value={"S": "the-key"} - ).should.throw(DynamoDBResponseError) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_scan(): - conn = boto.connect_dynamodb() - table = create_table(conn) - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item = table.new_item(hash_key="the-key", attrs=item_data) - item.put() - - item = table.new_item(hash_key="the-key2", attrs=item_data) - item.put() - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - "ReceivedTime": "12/9/2011 11:36:03 PM", - "Ids": set([1, 2, 3]), - "PK": 7, - } - item = table.new_item(hash_key="the-key3", attrs=item_data) - item.put() - - results = table.scan() - results.response["Items"].should.have.length_of(3) - - results = table.scan(scan_filter={"SentBy": condition.EQ("User B")}) - results.response["Items"].should.have.length_of(1) - - results = table.scan(scan_filter={"Body": condition.BEGINS_WITH("http")}) - results.response["Items"].should.have.length_of(3) - - results = table.scan(scan_filter={"Ids": condition.CONTAINS(2)}) - results.response["Items"].should.have.length_of(1) - - results = table.scan(scan_filter={"Ids": condition.NOT_NULL()}) - results.response["Items"].should.have.length_of(1) - - results = table.scan(scan_filter={"Ids": condition.NULL()}) - results.response["Items"].should.have.length_of(2) - - results = table.scan(scan_filter={"PK": condition.BETWEEN(8, 9)}) - results.response["Items"].should.have.length_of(0) - - results = table.scan(scan_filter={"PK": condition.BETWEEN(5, 8)}) - results.response["Items"].should.have.length_of(1) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_scan_with_undeclared_table(): - conn = boto.connect_dynamodb() - - conn.layer1.scan.when.called_with( - table_name="undeclared-table", - scan_filter={ - "SentBy": { - "AttributeValueList": [{"S": "User B"}], - "ComparisonOperator": "EQ", - } - }, - ).should.throw(DynamoDBResponseError) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_scan_after_has_item(): - conn = boto.connect_dynamodb() - table = create_table(conn) - list(table.scan()).should.equal([]) - - table.has_item("the-key") - - list(table.scan()).should.equal([]) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_write_batch(): - conn = boto.connect_dynamodb() - table = create_table(conn) - - batch_list = conn.new_batch_write_list() - - items = [] - items.append( - table.new_item( - hash_key="the-key", - attrs={ - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - }, - ) - ) - - items.append( - table.new_item( - hash_key="the-key2", - attrs={ - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - "ReceivedTime": "12/9/2011 11:36:03 PM", - "Ids": set([1, 2, 3]), - "PK": 7, - }, - ) - ) - - batch_list.add_batch(table, puts=items) - conn.batch_write_item(batch_list) - - table.refresh() - table.item_count.should.equal(2) - - batch_list = conn.new_batch_write_list() - batch_list.add_batch(table, deletes=[("the-key")]) - conn.batch_write_item(batch_list) - - table.refresh() - table.item_count.should.equal(1) - - -# Has boto3 equivalent -@mock_dynamodb_deprecated -def test_batch_read(): - conn = boto.connect_dynamodb() - table = create_table(conn) - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item = table.new_item(hash_key="the-key1", attrs=item_data) - item.put() - - item = table.new_item(hash_key="the-key2", attrs=item_data) - item.put() - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - "ReceivedTime": "12/9/2011 11:36:03 PM", - "Ids": set([1, 2, 3]), - "PK": 7, - } - item = table.new_item(hash_key="another-key", attrs=item_data) - item.put() - - items = table.batch_get_item([("the-key1"), ("another-key")]) - # Iterate through so that batch_item gets called - count = len([x for x in items]) - count.should.have.equal(2) diff --git a/tests/test_dynamodb2/test_dynamodb.py b/tests/test_dynamodb2/test_dynamodb.py index 69217d7f7..20f43224c 100644 --- a/tests/test_dynamodb2/test_dynamodb.py +++ b/tests/test_dynamodb2/test_dynamodb.py @@ -1,53 +1,20 @@ -from __future__ import print_function - import uuid from datetime import datetime from decimal import Decimal -import boto import boto3 from boto3.dynamodb.conditions import Attr, Key import re import sure # noqa # pylint: disable=unused-import -from moto import mock_dynamodb2, mock_dynamodb2_deprecated -from moto.dynamodb2 import dynamodb_backend2, dynamodb_backends2 -from boto.exception import JSONResponseError +from moto import mock_dynamodb2 +from moto.dynamodb2 import dynamodb_backends2 from botocore.exceptions import ClientError -from tests.helpers import requires_boto_gte import moto.dynamodb2.comparisons import moto.dynamodb2.models import pytest -try: - import boto.dynamodb2 -except ImportError: - print("This boto version is not supported") - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_list_tables(): - name = "TestTable" - # Should make tables properly with boto - dynamodb_backend2.create_table( - name, - attr=[ - {"AttributeType": "S", "AttributeName": "forum_name"}, - {"AttributeType": "S", "AttributeName": "subject"}, - ], - schema=[ - {"KeyType": "HASH", "AttributeName": "forum_name"}, - {"KeyType": "RANGE", "AttributeName": "subject"}, - ], - ) - conn = boto.dynamodb2.connect_to_region( - "us-east-1", aws_access_key_id="ak", aws_secret_access_key="sk" - ) - assert conn.list_tables()["TableNames"] == [name] - @mock_dynamodb2 @pytest.mark.parametrize( @@ -67,34 +34,6 @@ def test_list_tables_boto3(names): conn.list_tables()["TableNames"].should.equal(names) -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_list_tables_layer_1(): - # Should make tables properly with boto - dynamodb_backend2.create_table( - "test_1", - attr=[{"AttributeType": "S", "AttributeName": "name"},], - schema=[{"KeyType": "HASH", "AttributeName": "name"}], - ) - dynamodb_backend2.create_table( - "test_2", - attr=[{"AttributeType": "S", "AttributeName": "name"}], - schema=[{"KeyType": "HASH", "AttributeName": "name"}], - ) - conn = boto.dynamodb2.connect_to_region( - "us-east-1", aws_access_key_id="ak", aws_secret_access_key="sk" - ) - - res = conn.list_tables(limit=1) - expected = {"TableNames": ["test_1"], "LastEvaluatedTableName": "test_1"} - res.should.equal(expected) - - res = conn.list_tables(limit=1, exclusive_start_table_name="test_1") - expected = {"TableNames": ["test_2"]} - res.should.equal(expected) - - @mock_dynamodb2 def test_list_tables_paginated(): conn = boto3.client("dynamodb", region_name="us-west-2") @@ -119,17 +58,6 @@ def test_list_tables_paginated(): res.shouldnt.have.key("LastEvaluatedTableName") -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_describe_missing_table(): - conn = boto.dynamodb2.connect_to_region( - "us-west-2", aws_access_key_id="ak", aws_secret_access_key="sk" - ) - with pytest.raises(JSONResponseError): - conn.describe_table("messages") - - @mock_dynamodb2 def test_describe_missing_table_boto3(): conn = boto3.client("dynamodb", region_name="us-west-2") @@ -140,7 +68,6 @@ def test_describe_missing_table_boto3(): ex.value.response["Error"]["Message"].should.equal("Requested resource not found") -@requires_boto_gte("2.9") @mock_dynamodb2 def test_list_table_tags(): name = "TestTable" @@ -178,7 +105,6 @@ def test_list_table_tags(): assert resp["Tags"] == [{"Key": "TestTag2", "Value": "TestValue2"}] -@requires_boto_gte("2.9") @mock_dynamodb2 def test_list_table_tags_empty(): name = "TestTable" @@ -200,7 +126,6 @@ def test_list_table_tags_empty(): assert resp["Tags"] == [] -@requires_boto_gte("2.9") @mock_dynamodb2 def test_list_table_tags_paginated(): name = "TestTable" @@ -229,7 +154,6 @@ def test_list_table_tags_paginated(): assert "NextToken" not in resp2.keys() -@requires_boto_gte("2.9") @mock_dynamodb2 def test_list_not_found_table_tags(): conn = boto3.client( @@ -384,7 +308,6 @@ def test_update_item_with_empty_string_attr_no_exception(): ) -@requires_boto_gte("2.9") @mock_dynamodb2 def test_query_invalid_table(): conn = boto3.client( @@ -403,7 +326,6 @@ def test_query_invalid_table(): assert exception.response["Error"]["Code"] == "ResourceNotFoundException" -@requires_boto_gte("2.9") @mock_dynamodb2 def test_put_item_with_special_chars(): name = "TestTable" @@ -434,7 +356,6 @@ def test_put_item_with_special_chars(): ) -@requires_boto_gte("2.9") @mock_dynamodb2 def test_put_item_with_streams(): name = "TestTable" @@ -3513,7 +3434,6 @@ def test_update_supports_list_append_maps(): ) -@requires_boto_gte("2.9") @mock_dynamodb2 def test_update_supports_nested_update_if_nested_value_not_exists(): dynamodb = boto3.resource("dynamodb", region_name="us-east-1") diff --git a/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py b/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py index 361acc6d4..53e1d576b 100644 --- a/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py +++ b/tests/test_dynamodb2/test_dynamodb_table_with_range_key.py @@ -1,94 +1,15 @@ from decimal import Decimal -import boto import boto3 from boto3.dynamodb.conditions import Key from botocore.exceptions import ClientError import sure # noqa # pylint: disable=unused-import from datetime import datetime -from freezegun import freeze_time import pytest -from moto import mock_dynamodb2, mock_dynamodb2_deprecated -from boto.exception import JSONResponseError -from tests.helpers import requires_boto_gte +from moto import mock_dynamodb2 from uuid import uuid4 -try: - from boto.dynamodb2.fields import GlobalAllIndex, HashKey, RangeKey, AllIndex - from boto.dynamodb2.table import Item, Table - from boto.dynamodb2.types import STRING, NUMBER - from boto.dynamodb2.exceptions import ValidationException - from boto.dynamodb2.exceptions import ConditionalCheckFailedException -except ImportError: - pass - - -def create_table(): - table = Table.create( - "messages", - schema=[HashKey("forum_name"), RangeKey("subject")], - throughput={"read": 10, "write": 10}, - ) - return table - - -def create_table_with_local_indexes(): - table = Table.create( - "messages", - schema=[HashKey("forum_name"), RangeKey("subject")], - throughput={"read": 10, "write": 10}, - indexes=[ - AllIndex( - "threads_index", - parts=[ - HashKey("forum_name", data_type=STRING), - RangeKey("threads", data_type=NUMBER), - ], - ) - ], - ) - return table - - -def iterate_results(res): - for _ in res: - pass - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -@freeze_time("2012-01-14") -def test_create_table(): - table = create_table() - expected = { - "Table": { - "AttributeDefinitions": [ - {"AttributeName": "forum_name", "AttributeType": "S"}, - {"AttributeName": "subject", "AttributeType": "S"}, - ], - "ProvisionedThroughput": { - "NumberOfDecreasesToday": 0, - "WriteCapacityUnits": 10, - "ReadCapacityUnits": 10, - }, - "TableSizeBytes": 0, - "TableName": "messages", - "TableStatus": "ACTIVE", - "TableArn": "arn:aws:dynamodb:us-east-1:123456789011:table/messages", - "KeySchema": [ - {"KeyType": "HASH", "AttributeName": "forum_name"}, - {"KeyType": "RANGE", "AttributeName": "subject"}, - ], - "LocalSecondaryIndexes": [], - "ItemCount": 0, - "CreationDateTime": 1326499200.0, - "GlobalSecondaryIndexes": [], - } - } - table.describe().should.equal(expected) - @mock_dynamodb2 def test_create_table_boto3(): @@ -134,50 +55,6 @@ def test_create_table_boto3(): actual.should.have.key("ItemCount").equal(0) -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -@freeze_time("2012-01-14") -def test_create_table_with_local_index(): - table = create_table_with_local_indexes() - expected = { - "Table": { - "AttributeDefinitions": [ - {"AttributeName": "forum_name", "AttributeType": "S"}, - {"AttributeName": "subject", "AttributeType": "S"}, - {"AttributeName": "threads", "AttributeType": "N"}, - ], - "ProvisionedThroughput": { - "NumberOfDecreasesToday": 0, - "WriteCapacityUnits": 10, - "ReadCapacityUnits": 10, - }, - "TableSizeBytes": 0, - "TableName": "messages", - "TableStatus": "ACTIVE", - "TableArn": "arn:aws:dynamodb:us-east-1:123456789011:table/messages", - "KeySchema": [ - {"KeyType": "HASH", "AttributeName": "forum_name"}, - {"KeyType": "RANGE", "AttributeName": "subject"}, - ], - "LocalSecondaryIndexes": [ - { - "IndexName": "threads_index", - "KeySchema": [ - {"AttributeName": "forum_name", "KeyType": "HASH"}, - {"AttributeName": "threads", "KeyType": "RANGE"}, - ], - "Projection": {"ProjectionType": "ALL"}, - } - ], - "ItemCount": 0, - "CreationDateTime": 1326499200.0, - "GlobalSecondaryIndexes": [], - } - } - table.describe().should.equal(expected) - - @mock_dynamodb2 def test_create_table_with_local_index_boto3(): client = boto3.client("dynamodb", region_name="us-east-1") @@ -245,169 +122,6 @@ def test_create_table_with_local_index_boto3(): actual.should.have.key("ItemCount").equal(0) -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_delete_table(): - conn = boto.dynamodb2.layer1.DynamoDBConnection() - table = create_table() - conn.list_tables()["TableNames"].should.have.length_of(1) - - table.delete() - conn.list_tables()["TableNames"].should.have.length_of(0) - conn.delete_table.when.called_with("messages").should.throw(JSONResponseError) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_update_table_throughput(): - table = create_table() - table.throughput["read"].should.equal(10) - table.throughput["write"].should.equal(10) - table.update(throughput={"read": 5, "write": 15}) - - table.throughput["read"].should.equal(5) - table.throughput["write"].should.equal(15) - - table.update(throughput={"read": 5, "write": 6}) - - table.describe() - - table.throughput["read"].should.equal(5) - table.throughput["write"].should.equal(6) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_item_add_and_describe_and_update(): - table = create_table() - ok = table.put_item( - data={ - "forum_name": "LOLCat Forum", - "subject": "Check this out!", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - ) - ok.should.equal(True) - - table.get_item( - forum_name="LOLCat Forum", subject="Check this out!" - ).should_not.be.none - - returned_item = table.get_item(forum_name="LOLCat Forum", subject="Check this out!") - dict(returned_item).should.equal( - { - "forum_name": "LOLCat Forum", - "subject": "Check this out!", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - ) - - returned_item["SentBy"] = "User B" - returned_item.save(overwrite=True) - - returned_item = table.get_item(forum_name="LOLCat Forum", subject="Check this out!") - dict(returned_item).should.equal( - { - "forum_name": "LOLCat Forum", - "subject": "Check this out!", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - ) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_item_partial_save(): - table = create_table() - - data = { - "forum_name": "LOLCat Forum", - "subject": "The LOLz", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - } - - table.put_item(data=data) - returned_item = table.get_item(forum_name="LOLCat Forum", subject="The LOLz") - - returned_item["SentBy"] = "User B" - returned_item.partial_save() - - returned_item = table.get_item(forum_name="LOLCat Forum", subject="The LOLz") - dict(returned_item).should.equal( - { - "forum_name": "LOLCat Forum", - "subject": "The LOLz", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - } - ) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_item_put_without_table(): - table = Table("undeclared-table") - item_data = { - "forum_name": "LOLCat Forum", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item = Item(table, item_data) - item.save.when.called_with().should.throw(JSONResponseError) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_get_missing_item(): - table = create_table() - - table.get_item.when.called_with(hash_key="tester", range_key="other").should.throw( - ValidationException - ) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_get_item_with_undeclared_table(): - table = Table("undeclared-table") - table.get_item.when.called_with(test_hash=3241526475).should.throw( - JSONResponseError - ) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_get_item_without_range_key(): - table = Table.create( - "messages", - schema=[HashKey("test_hash"), RangeKey("test_range")], - throughput={"read": 10, "write": 10}, - ) - - hash_key = "3241526475" - range_key = "1234567890987" - table.put_item(data={"test_hash": hash_key, "test_range": range_key}) - table.get_item.when.called_with(test_hash=hash_key).should.throw( - ValidationException - ) - - @mock_dynamodb2 def test_get_item_without_range_key_boto3(): client = boto3.resource("dynamodb", region_name="us-east-1") @@ -435,544 +149,6 @@ def test_get_item_without_range_key_boto3(): ex.value.response["Error"]["Message"].should.equal("Validation Exception") -@requires_boto_gte("2.30.0") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_delete_item(): - table = create_table() - item_data = { - "forum_name": "LOLCat Forum", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item = Item(table, item_data) - item["subject"] = "Check this out!" - item.save() - table.count().should.equal(1) - - response = item.delete() - response.should.equal(True) - - table.count().should.equal(0) - # Deletes are idempotent - item.delete().should.equal(True) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_delete_item_with_undeclared_table(): - table = Table("undeclared-table") - item_data = { - "forum_name": "LOLCat Forum", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item = Item(table, item_data) - item.delete.when.called_with().should.throw(JSONResponseError) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_query(): - table = create_table() - - item_data = { - "forum_name": "LOLCat Forum", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - "subject": "Check this out!", - } - item = Item(table, item_data) - item.save(overwrite=True) - - item["forum_name"] = "the-key" - item["subject"] = "456" - item.save(overwrite=True) - - item["forum_name"] = "the-key" - item["subject"] = "123" - item.save(overwrite=True) - - item["forum_name"] = "the-key" - item["subject"] = "789" - item.save(overwrite=True) - - table.count().should.equal(4) - - results = table.query_2(forum_name__eq="the-key", subject__gt="1", consistent=True) - expected = ["123", "456", "789"] - for index, item in enumerate(results): - item["subject"].should.equal(expected[index]) - - results = table.query_2(forum_name__eq="the-key", subject__gt="1", reverse=True) - for index, item in enumerate(results): - item["subject"].should.equal(expected[len(expected) - 1 - index]) - - results = table.query_2(forum_name__eq="the-key", subject__gt="1", consistent=True) - sum(1 for _ in results).should.equal(3) - - results = table.query_2( - forum_name__eq="the-key", subject__gt="234", consistent=True - ) - sum(1 for _ in results).should.equal(2) - - results = table.query_2(forum_name__eq="the-key", subject__gt="9999") - sum(1 for _ in results).should.equal(0) - - results = table.query_2(forum_name__eq="the-key", subject__beginswith="12") - sum(1 for _ in results).should.equal(1) - - results = table.query_2(forum_name__eq="the-key", subject__beginswith="7") - sum(1 for _ in results).should.equal(1) - - results = table.query_2(forum_name__eq="the-key", subject__between=["567", "890"]) - sum(1 for _ in results).should.equal(1) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_query_with_undeclared_table(): - table = Table("undeclared") - results = table.query( - forum_name__eq="Amazon DynamoDB", subject__beginswith="DynamoDB", limit=1 - ) - iterate_results.when.called_with(results).should.throw(JSONResponseError) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_scan(): - table = create_table() - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item_data["forum_name"] = "the-key" - item_data["subject"] = "456" - - item = Item(table, item_data) - item.save() - - item["forum_name"] = "the-key" - item["subject"] = "123" - item.save() - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - "ReceivedTime": "12/9/2011 11:36:09 PM", - "Ids": set([1, 2, 3]), - "PK": 7, - } - - item_data["forum_name"] = "the-key" - item_data["subject"] = "789" - - item = Item(table, item_data) - item.save() - - results = table.scan() - sum(1 for _ in results).should.equal(3) - - results = table.scan(SentBy__eq="User B") - sum(1 for _ in results).should.equal(1) - - results = table.scan(Body__beginswith="http") - sum(1 for _ in results).should.equal(3) - - results = table.scan(Ids__null=False) - sum(1 for _ in results).should.equal(1) - - results = table.scan(Ids__null=True) - sum(1 for _ in results).should.equal(2) - - results = table.scan(PK__between=[8, 9]) - sum(1 for _ in results).should.equal(0) - - results = table.scan(PK__between=[5, 8]) - sum(1 for _ in results).should.equal(1) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_scan_with_undeclared_table(): - conn = boto.dynamodb2.layer1.DynamoDBConnection() - conn.scan.when.called_with( - table_name="undeclared-table", - scan_filter={ - "SentBy": { - "AttributeValueList": [{"S": "User B"}], - "ComparisonOperator": "EQ", - } - }, - ).should.throw(JSONResponseError) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_write_batch(): - table = create_table() - with table.batch_write() as batch: - batch.put_item( - data={ - "forum_name": "the-key", - "subject": "123", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - ) - batch.put_item( - data={ - "forum_name": "the-key", - "subject": "789", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - ) - - table.count().should.equal(2) - with table.batch_write() as batch: - batch.delete_item(forum_name="the-key", subject="789") - - table.count().should.equal(1) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_batch_read(): - table = create_table() - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - - item_data["forum_name"] = "the-key" - item_data["subject"] = "456" - - item = Item(table, item_data) - item.save() - - item = Item(table, item_data) - item_data["forum_name"] = "the-key" - item_data["subject"] = "123" - item.save() - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - "ReceivedTime": "12/9/2011 11:36:03 PM", - "Ids": set([1, 2, 3]), - "PK": 7, - } - item = Item(table, item_data) - item_data["forum_name"] = "another-key" - item_data["subject"] = "789" - item.save() - results = table.batch_get( - keys=[ - {"forum_name": "the-key", "subject": "123"}, - {"forum_name": "another-key", "subject": "789"}, - ] - ) - - # Iterate through so that batch_item gets called - count = len([x for x in results]) - count.should.equal(2) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_get_key_fields(): - table = create_table() - kf = table.get_key_fields() - kf.should.equal(["forum_name", "subject"]) - - -@mock_dynamodb2_deprecated -# Has boto3 equivalent -def test_create_with_global_indexes(): - conn = boto.dynamodb2.layer1.DynamoDBConnection() - - Table.create( - "messages", - schema=[HashKey("subject"), RangeKey("version")], - global_indexes=[ - GlobalAllIndex( - "topic-created_at-index", - parts=[HashKey("topic"), RangeKey("created_at", data_type="N")], - throughput={"read": 6, "write": 1}, - ) - ], - ) - - table_description = conn.describe_table("messages") - table_description["Table"]["GlobalSecondaryIndexes"].should.equal( - [ - { - "IndexName": "topic-created_at-index", - "KeySchema": [ - {"AttributeName": "topic", "KeyType": "HASH"}, - {"AttributeName": "created_at", "KeyType": "RANGE"}, - ], - "Projection": {"ProjectionType": "ALL"}, - "ProvisionedThroughput": { - "ReadCapacityUnits": 6, - "WriteCapacityUnits": 1, - }, - "IndexStatus": "ACTIVE", - } - ] - ) - - -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_query_with_global_indexes(): - table = Table.create( - "messages", - schema=[HashKey("subject"), RangeKey("version")], - global_indexes=[ - GlobalAllIndex( - "topic-created_at-index", - parts=[HashKey("topic"), RangeKey("created_at", data_type="N")], - throughput={"read": 6, "write": 1}, - ), - GlobalAllIndex( - "status-created_at-index", - parts=[HashKey("status"), RangeKey("created_at", data_type="N")], - throughput={"read": 2, "write": 1}, - ), - ], - ) - - item_data = { - "subject": "Check this out!", - "version": "1", - "created_at": 0, - "status": "inactive", - } - item = Item(table, item_data) - item.save(overwrite=True) - - item["version"] = "2" - item.save(overwrite=True) - - results = table.query(status__eq="active") - list(results).should.have.length_of(0) - - -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_query_with_local_indexes(): - table = create_table_with_local_indexes() - item_data = { - "forum_name": "Cool Forum", - "subject": "Check this out!", - "version": "1", - "threads": 1, - "status": "inactive", - } - item = Item(table, item_data) - item.save(overwrite=True) - - item["version"] = "2" - item.save(overwrite=True) - results = table.query( - forum_name__eq="Cool Forum", index="threads_index", threads__eq=1 - ) - list(results).should.have.length_of(1) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_query_filter_eq(): - table = create_table_with_local_indexes() - item_data = [ - { - "forum_name": "Cool Forum", - "subject": "Check this out!", - "version": "1", - "threads": 1, - }, - { - "forum_name": "Cool Forum", - "subject": "Read this now!", - "version": "1", - "threads": 5, - }, - { - "forum_name": "Cool Forum", - "subject": "Please read this... please", - "version": "1", - "threads": 0, - }, - ] - for data in item_data: - item = Item(table, data) - item.save(overwrite=True) - results = table.query_2( - forum_name__eq="Cool Forum", index="threads_index", threads__eq=5 - ) - list(results).should.have.length_of(1) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_query_filter_lt(): - table = create_table_with_local_indexes() - item_data = [ - { - "forum_name": "Cool Forum", - "subject": "Check this out!", - "version": "1", - "threads": 1, - }, - { - "forum_name": "Cool Forum", - "subject": "Read this now!", - "version": "1", - "threads": 5, - }, - { - "forum_name": "Cool Forum", - "subject": "Please read this... please", - "version": "1", - "threads": 0, - }, - ] - for data in item_data: - item = Item(table, data) - item.save(overwrite=True) - - results = table.query( - forum_name__eq="Cool Forum", index="threads_index", threads__lt=5 - ) - results = list(results) - results.should.have.length_of(2) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_query_filter_gt(): - table = create_table_with_local_indexes() - item_data = [ - { - "forum_name": "Cool Forum", - "subject": "Check this out!", - "version": "1", - "threads": 1, - }, - { - "forum_name": "Cool Forum", - "subject": "Read this now!", - "version": "1", - "threads": 5, - }, - { - "forum_name": "Cool Forum", - "subject": "Please read this... please", - "version": "1", - "threads": 0, - }, - ] - for data in item_data: - item = Item(table, data) - item.save(overwrite=True) - - results = table.query( - forum_name__eq="Cool Forum", index="threads_index", threads__gt=1 - ) - list(results).should.have.length_of(1) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_query_filter_lte(): - table = create_table_with_local_indexes() - item_data = [ - { - "forum_name": "Cool Forum", - "subject": "Check this out!", - "version": "1", - "threads": 1, - }, - { - "forum_name": "Cool Forum", - "subject": "Read this now!", - "version": "1", - "threads": 5, - }, - { - "forum_name": "Cool Forum", - "subject": "Please read this... please", - "version": "1", - "threads": 0, - }, - ] - for data in item_data: - item = Item(table, data) - item.save(overwrite=True) - - results = table.query( - forum_name__eq="Cool Forum", index="threads_index", threads__lte=5 - ) - list(results).should.have.length_of(3) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_query_filter_gte(): - table = create_table_with_local_indexes() - item_data = [ - { - "forum_name": "Cool Forum", - "subject": "Check this out!", - "version": "1", - "threads": 1, - }, - { - "forum_name": "Cool Forum", - "subject": "Read this now!", - "version": "1", - "threads": 5, - }, - { - "forum_name": "Cool Forum", - "subject": "Please read this... please", - "version": "1", - "threads": 0, - }, - ] - for data in item_data: - item = Item(table, data) - item.save(overwrite=True) - - results = table.query( - forum_name__eq="Cool Forum", index="threads_index", threads__gte=1 - ) - list(results).should.have.length_of(2) - - @mock_dynamodb2 def test_query_filter_boto3(): table_schema = { @@ -1016,135 +192,6 @@ def test_query_filter_boto3(): res["Items"].should.equal([{"pk": "pk", "sk": "sk-1"}, {"pk": "pk", "sk": "sk-2"}]) -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_query_non_hash_range_key(): - table = create_table_with_local_indexes() - item_data = [ - { - "forum_name": "Cool Forum", - "subject": "Check this out!", - "version": "1", - "threads": 1, - }, - { - "forum_name": "Cool Forum", - "subject": "Read this now!", - "version": "3", - "threads": 5, - }, - { - "forum_name": "Cool Forum", - "subject": "Please read this... please", - "version": "2", - "threads": 0, - }, - ] - for data in item_data: - item = Item(table, data) - item.save(overwrite=True) - - results = table.query(forum_name__eq="Cool Forum", version__gt="2") - results = list(results) - results.should.have.length_of(1) - - results = table.query(forum_name__eq="Cool Forum", version__lt="3") - results = list(results) - results.should.have.length_of(2) - - -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_reverse_query(): - boto.dynamodb2.layer1.DynamoDBConnection() - - table = Table.create( - "messages", schema=[HashKey("subject"), RangeKey("created_at", data_type="N")] - ) - - for i in range(10): - table.put_item({"subject": "Hi", "created_at": i}) - - results = table.query_2(subject__eq="Hi", created_at__lt=6, limit=4, reverse=True) - - expected = [Decimal(5), Decimal(4), Decimal(3), Decimal(2)] - [r["created_at"] for r in results].should.equal(expected) - - -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_lookup(): - table = Table.create( - "messages", - schema=[ - HashKey("test_hash", data_type=NUMBER), - RangeKey("test_range", data_type=NUMBER), - ], - throughput={"read": 10, "write": 10}, - ) - - hash_key = 3241526475 - range_key = 1234567890987 - data = {"test_hash": hash_key, "test_range": range_key} - table.put_item(data=data) - message = table.lookup(hash_key, range_key) - message.get("test_hash").should.equal(Decimal(hash_key)) - message.get("test_range").should.equal(Decimal(range_key)) - - -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_failed_overwrite(): - table = Table.create( - "messages", - schema=[HashKey("id"), RangeKey("range")], - throughput={"read": 7, "write": 3}, - ) - - data1 = {"id": "123", "range": "abc", "data": "678"} - table.put_item(data=data1) - - data2 = {"id": "123", "range": "abc", "data": "345"} - table.put_item(data=data2, overwrite=True) - - data3 = {"id": "123", "range": "abc", "data": "812"} - table.put_item.when.called_with(data=data3).should.throw( - ConditionalCheckFailedException - ) - - returned_item = table.lookup("123", "abc") - dict(returned_item).should.equal(data2) - - data4 = {"id": "123", "range": "ghi", "data": 812} - table.put_item(data=data4) - - returned_item = table.lookup("123", "ghi") - dict(returned_item).should.equal(data4) - - -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_conflicting_writes(): - table = Table.create("messages", schema=[HashKey("id"), RangeKey("range")]) - - item_data = {"id": "123", "range": "abc", "data": "678"} - item1 = Item(table, item_data) - item2 = Item(table, item_data) - item1.save() - - item1["data"] = "579" - item2["data"] = "912" - - item1.save() - item2.save.when.called_with().should.throw(ConditionalCheckFailedException) - - -""" -boto3 -""" - - @mock_dynamodb2 def test_boto3_create_table_with_gsi(): dynamodb = boto3.client("dynamodb", region_name="us-east-1") diff --git a/tests/test_dynamodb2/test_dynamodb_table_without_range_key.py b/tests/test_dynamodb2/test_dynamodb_table_without_range_key.py index cf943a3c5..b94bd688a 100644 --- a/tests/test_dynamodb2/test_dynamodb_table_without_range_key.py +++ b/tests/test_dynamodb2/test_dynamodb_table_without_range_key.py @@ -1,66 +1,12 @@ -import boto import boto3 from boto3.dynamodb.conditions import Key import sure # noqa # pylint: disable=unused-import import pytest from datetime import datetime -from freezegun import freeze_time -from boto.exception import JSONResponseError from botocore.exceptions import ClientError -from moto import mock_dynamodb2, mock_dynamodb2_deprecated -from tests.helpers import requires_boto_gte +from moto import mock_dynamodb2 import botocore -try: - from boto.dynamodb2.types import NUMBER - from boto.dynamodb2.fields import HashKey - from boto.dynamodb2.table import Table - from boto.dynamodb2.table import Item - from boto.dynamodb2.exceptions import ConditionalCheckFailedException, ItemNotFound -except ImportError: - pass - - -def create_table(): - table = Table.create( - "messages", schema=[HashKey("forum_name")], throughput={"read": 10, "write": 10} - ) - return table - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -@freeze_time("2012-01-14") -def test_create_table(): - create_table() - expected = { - "Table": { - "AttributeDefinitions": [ - {"AttributeName": "forum_name", "AttributeType": "S"} - ], - "ProvisionedThroughput": { - "NumberOfDecreasesToday": 0, - "WriteCapacityUnits": 10, - "ReadCapacityUnits": 10, - }, - "TableSizeBytes": 0, - "TableName": "messages", - "TableStatus": "ACTIVE", - "TableArn": "arn:aws:dynamodb:us-east-1:123456789011:table/messages", - "KeySchema": [{"KeyType": "HASH", "AttributeName": "forum_name"}], - "ItemCount": 0, - "CreationDateTime": 1326499200.0, - "GlobalSecondaryIndexes": [], - "LocalSecondaryIndexes": [], - } - } - conn = boto.dynamodb2.connect_to_region( - "us-east-1", aws_access_key_id="ak", aws_secret_access_key="sk" - ) - - conn.describe_table("messages").should.equal(expected) - @mock_dynamodb2 def test_create_table_boto3(): @@ -125,20 +71,6 @@ def test_create_table_boto3(): actual.should.have.key("ItemCount").equal(0) -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_delete_table(): - create_table() - conn = boto.dynamodb2.layer1.DynamoDBConnection() - conn.list_tables()["TableNames"].should.have.length_of(1) - - conn.delete_table("messages") - conn.list_tables()["TableNames"].should.have.length_of(0) - - conn.delete_table.when.called_with("messages").should.throw(JSONResponseError) - - @mock_dynamodb2 def test_delete_table_boto3(): conn = boto3.client("dynamodb", region_name="us-west-2") @@ -161,20 +93,6 @@ def test_delete_table_boto3(): ex.value.response["Error"]["Message"].should.equal("Requested resource not found") -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_update_table_throughput(): - table = create_table() - table.throughput["read"].should.equal(10) - table.throughput["write"].should.equal(10) - - table.update(throughput={"read": 5, "write": 6}) - - table.throughput["read"].should.equal(5) - table.throughput["write"].should.equal(6) - - @mock_dynamodb2 def test_update_table_throughput_boto3(): conn = boto3.resource("dynamodb", region_name="us-west-2") @@ -195,43 +113,6 @@ def test_update_table_throughput_boto3(): table.provisioned_throughput["WriteCapacityUnits"].should.equal(6) -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_item_add_and_describe_and_update(): - table = create_table() - - data = { - "forum_name": "LOLCat Forum", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - } - - table.put_item(data=data) - returned_item = table.get_item(forum_name="LOLCat Forum") - returned_item.should_not.be.none - - dict(returned_item).should.equal( - { - "forum_name": "LOLCat Forum", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - } - ) - - returned_item["SentBy"] = "User B" - returned_item.save(overwrite=True) - - returned_item = table.get_item(forum_name="LOLCat Forum") - dict(returned_item).should.equal( - { - "forum_name": "LOLCat Forum", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - } - ) - - @mock_dynamodb2 def test_item_add_and_describe_and_update_boto3(): conn = boto3.resource("dynamodb", region_name="us-west-2") @@ -268,50 +149,6 @@ def test_item_add_and_describe_and_update_boto3(): ) -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_item_partial_save(): - table = create_table() - - data = { - "forum_name": "LOLCat Forum", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - } - - table.put_item(data=data) - returned_item = table.get_item(forum_name="LOLCat Forum") - - returned_item["SentBy"] = "User B" - returned_item.partial_save() - - returned_item = table.get_item(forum_name="LOLCat Forum") - dict(returned_item).should.equal( - { - "forum_name": "LOLCat Forum", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - } - ) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_item_put_without_table(): - conn = boto.dynamodb2.layer1.DynamoDBConnection() - - conn.put_item.when.called_with( - table_name="undeclared-table", - item={ - "forum_name": "LOLCat Forum", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - }, - ).should.throw(JSONResponseError) - - @mock_dynamodb2 def test_item_put_without_table_boto3(): conn = boto3.client("dynamodb", region_name="us-west-2") @@ -331,17 +168,6 @@ def test_item_put_without_table_boto3(): ex.value.response["Error"]["Message"].should.equal("Requested resource not found") -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_get_item_with_undeclared_table(): - conn = boto.dynamodb2.layer1.DynamoDBConnection() - - conn.get_item.when.called_with( - table_name="undeclared-table", key={"forum_name": {"S": "LOLCat Forum"}} - ).should.throw(JSONResponseError) - - @mock_dynamodb2 def test_get_item_with_undeclared_table_boto3(): conn = boto3.client("dynamodb", region_name="us-west-2") @@ -354,32 +180,6 @@ def test_get_item_with_undeclared_table_boto3(): ex.value.response["Error"]["Message"].should.equal("Requested resource not found") -@requires_boto_gte("2.30.0") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_delete_item(): - table = create_table() - - item_data = { - "forum_name": "LOLCat Forum", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item = Item(table, item_data) - item.save() - table.count().should.equal(1) - - response = item.delete() - - response.should.equal(True) - - table.count().should.equal(0) - - # Deletes are idempotent and 'False' here would imply an error condition - item.delete().should.equal(True) - - @mock_dynamodb2 def test_delete_item_boto3(): conn = boto3.resource("dynamodb", region_name="us-west-2") @@ -407,17 +207,6 @@ def test_delete_item_boto3(): table.delete_item(Key={"id": "LOLCat Forum"}) -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_delete_item_with_undeclared_table(): - conn = boto.dynamodb2.layer1.DynamoDBConnection() - - conn.delete_item.when.called_with( - table_name="undeclared-table", key={"forum_name": {"S": "LOLCat Forum"}} - ).should.throw(JSONResponseError) - - @mock_dynamodb2 def test_delete_item_with_undeclared_table_boto3(): conn = boto3.client("dynamodb", region_name="us-west-2") @@ -434,113 +223,6 @@ def test_delete_item_with_undeclared_table_boto3(): ) -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_query(): - table = create_table() - - item_data = { - "forum_name": "the-key", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item = Item(table, item_data) - item.save(overwrite=True) - table.count().should.equal(1) - table = Table("messages") - - results = table.query(forum_name__eq="the-key") - sum(1 for _ in results).should.equal(1) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_query_with_undeclared_table(): - conn = boto.dynamodb2.layer1.DynamoDBConnection() - - conn.query.when.called_with( - table_name="undeclared-table", - key_conditions={ - "forum_name": { - "ComparisonOperator": "EQ", - "AttributeValueList": [{"S": "the-key"}], - } - }, - ).should.throw(JSONResponseError) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_scan(): - table = create_table() - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item_data["forum_name"] = "the-key" - - item = Item(table, item_data) - item.save() - - item["forum_name"] = "the-key2" - item.save(overwrite=True) - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - "ReceivedTime": "12/9/2011 11:36:03 PM", - "Ids": set([1, 2, 3]), - "PK": 7, - } - item_data["forum_name"] = "the-key3" - item = Item(table, item_data) - item.save() - - results = table.scan() - sum(1 for _ in results).should.equal(3) - - results = table.scan(SentBy__eq="User B") - sum(1 for _ in results).should.equal(1) - - results = table.scan(Body__beginswith="http") - sum(1 for _ in results).should.equal(3) - - results = table.scan(Ids__null=False) - sum(1 for _ in results).should.equal(1) - - results = table.scan(Ids__null=True) - sum(1 for _ in results).should.equal(2) - - results = table.scan(PK__between=[8, 9]) - sum(1 for _ in results).should.equal(0) - - results = table.scan(PK__between=[5, 8]) - sum(1 for _ in results).should.equal(1) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_scan_with_undeclared_table(): - conn = boto.dynamodb2.layer1.DynamoDBConnection() - - conn.scan.when.called_with( - table_name="undeclared-table", - scan_filter={ - "SentBy": { - "AttributeValueList": [{"S": "User B"}], - "ComparisonOperator": "EQ", - } - }, - ).should.throw(JSONResponseError) - - @mock_dynamodb2 def test_scan_with_undeclared_table_boto3(): conn = boto3.client("dynamodb", region_name="us-west-2") @@ -553,87 +235,6 @@ def test_scan_with_undeclared_table_boto3(): ex.value.response["Error"]["Message"].should.equal("Requested resource not found") -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_write_batch(): - table = create_table() - - with table.batch_write() as batch: - batch.put_item( - data={ - "forum_name": "the-key", - "subject": "123", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - ) - batch.put_item( - data={ - "forum_name": "the-key2", - "subject": "789", - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - ) - - table.count().should.equal(2) - with table.batch_write() as batch: - batch.delete_item(forum_name="the-key", subject="789") - - table.count().should.equal(1) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_batch_read(): - table = create_table() - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User A", - "ReceivedTime": "12/9/2011 11:36:03 PM", - } - item_data["forum_name"] = "the-key1" - item = Item(table, item_data) - item.save() - - item = Item(table, item_data) - item_data["forum_name"] = "the-key2" - item.save(overwrite=True) - - item_data = { - "Body": "http://url_to_lolcat.gif", - "SentBy": "User B", - "ReceivedTime": "12/9/2011 11:36:03 PM", - "Ids": set([1, 2, 3]), - "PK": 7, - } - item = Item(table, item_data) - item_data["forum_name"] = "another-key" - item.save(overwrite=True) - - results = table.batch_get( - keys=[{"forum_name": "the-key1"}, {"forum_name": "another-key"}] - ) - - # Iterate through so that batch_item gets called - count = len([x for x in results]) - count.should.equal(2) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_get_key_fields(): - table = create_table() - kf = table.get_key_fields() - kf[0].should.equal("forum_name") - - @mock_dynamodb2 def test_get_key_schema(): conn = boto3.resource("dynamodb", region_name="us-west-2") @@ -647,64 +248,6 @@ def test_get_key_schema(): table.key_schema.should.equal([{"AttributeName": "id", "KeyType": "HASH"}]) -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_get_missing_item(): - table = create_table() - table.get_item.when.called_with(forum_name="missing").should.throw(ItemNotFound) - - -@requires_boto_gte("2.9") -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_get_special_item(): - table = Table.create( - "messages", - schema=[HashKey("date-joined", data_type=NUMBER)], - throughput={"read": 10, "write": 10}, - ) - - data = {"date-joined": 127549192, "SentBy": "User A"} - table.put_item(data=data) - returned_item = table.get_item(**{"date-joined": 127549192}) - dict(returned_item).should.equal(data) - - -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_update_item_remove(): - conn = boto.dynamodb2.connect_to_region("us-east-1") - table = Table.create("messages", schema=[HashKey("username")]) - - data = {"username": "steve", "SentBy": "User A", "SentTo": "User B"} - table.put_item(data=data) - key_map = {"username": {"S": "steve"}} - - # Then remove the SentBy field - conn.update_item("messages", key_map, update_expression="REMOVE SentBy, SentTo") - - returned_item = table.get_item(username="steve") - dict(returned_item).should.equal({"username": "steve"}) - - -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_update_item_nested_remove(): - conn = boto.dynamodb2.connect_to_region("us-east-1") - table = Table.create("messages", schema=[HashKey("username")]) - - data = {"username": "steve", "Meta": {"FullName": "Steve Urkel"}} - table.put_item(data=data) - key_map = {"username": {"S": "steve"}} - - # Then remove the Meta.FullName field - conn.update_item("messages", key_map, update_expression="REMOVE Meta.FullName") - - returned_item = table.get_item(username="steve") - dict(returned_item).should.equal({"username": "steve", "Meta": {}}) - - @mock_dynamodb2 def test_update_item_double_nested_remove(): conn = boto3.client("dynamodb", region_name="us-east-1") @@ -740,27 +283,6 @@ def test_update_item_double_nested_remove(): dict(returned_item["Item"]).should.equal(expected_item) -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_update_item_set(): - conn = boto.dynamodb2.connect_to_region("us-east-1") - table = Table.create("messages", schema=[HashKey("username")]) - - data = {"username": "steve", "SentBy": "User A"} - table.put_item(data=data) - key_map = {"username": {"S": "steve"}} - - conn.update_item( - "messages", - key_map, - update_expression="SET foo=:bar, blah=:baz REMOVE SentBy", - expression_attribute_values={":bar": {"S": "bar"}, ":baz": {"S": "baz"}}, - ) - - returned_item = table.get_item(username="steve") - dict(returned_item).should.equal({"username": "steve", "foo": "bar", "blah": "baz"}) - - @mock_dynamodb2 def test_update_item_set_boto3(): conn = boto3.resource("dynamodb", region_name="us-east-1") @@ -785,56 +307,6 @@ def test_update_item_set_boto3(): dict(returned_item).should.equal({"username": "steve", "foo": "bar", "blah": "baz"}) -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_failed_overwrite(): - table = Table.create( - "messages", schema=[HashKey("id")], throughput={"read": 7, "write": 3} - ) - - data1 = {"id": "123", "data": "678"} - table.put_item(data=data1) - - data2 = {"id": "123", "data": "345"} - table.put_item(data=data2, overwrite=True) - - data3 = {"id": "123", "data": "812"} - table.put_item.when.called_with(data=data3).should.throw( - ConditionalCheckFailedException - ) - - returned_item = table.lookup("123") - dict(returned_item).should.equal(data2) - - data4 = {"id": "124", "data": 812} - table.put_item(data=data4) - - returned_item = table.lookup("124") - dict(returned_item).should.equal(data4) - - -# Has boto3 equivalent -@mock_dynamodb2_deprecated -def test_conflicting_writes(): - table = Table.create("messages", schema=[HashKey("id")]) - - item_data = {"id": "123", "data": "678"} - item1 = Item(table, item_data) - item2 = Item(table, item_data) - item1.save() - - item1["data"] = "579" - item2["data"] = "912" - - item1.save() - item2.save.when.called_with().should.throw(ConditionalCheckFailedException) - - -""" -boto3 -""" - - @mock_dynamodb2 def test_boto3_create_table(): dynamodb = boto3.resource("dynamodb", region_name="us-east-1") diff --git a/tests/test_ec2/test_amis.py b/tests/test_ec2/test_amis.py index a1cad0379..775e22191 100644 --- a/tests/test_ec2/test_amis.py +++ b/tests/test_ec2/test_amis.py @@ -1,96 +1,17 @@ -import boto -import boto.ec2 import boto3 -from boto.exception import EC2ResponseError from botocore.exceptions import ClientError import pytest import sure # noqa # pylint: disable=unused-import import random -from moto import mock_ec2_deprecated, mock_ec2 +from moto import mock_ec2 from moto.ec2.models import AMIS, OWNER_ID from moto.core import ACCOUNT_ID from tests import EXAMPLE_AMI_ID, EXAMPLE_AMI_PARAVIRTUAL -from tests.helpers import requires_boto_gte from uuid import uuid4 -# Has boto3 equivalent -@mock_ec2_deprecated -def test_ami_create_and_delete(): - conn = boto.connect_ec2("the_key", "the_secret") - - initial_ami_count = len(AMIS) - conn.get_all_volumes().should.have.length_of(0) - conn.get_all_snapshots().should.have.length_of(initial_ami_count) - - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - with pytest.raises(EC2ResponseError) as ex: - conn.create_image(instance.id, "test-ami", "this is a test ami", dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the CreateImage operation: Request would have succeeded, but DryRun flag is set" - ) - - image_id = conn.create_image(instance.id, "test-ami", "this is a test ami") - - all_images = conn.get_all_images() - set([i.id for i in all_images]).should.contain(image_id) - - retrieved_image = [i for i in all_images if i.id == image_id][0] - - retrieved_image.id.should.equal(image_id) - retrieved_image.virtualization_type.should.equal(instance.virtualization_type) - retrieved_image.architecture.should.equal(instance.architecture) - retrieved_image.kernel_id.should.equal(instance.kernel) - retrieved_image.platform.should.equal(instance.platform) - retrieved_image.creationDate.should_not.be.none - instance.terminate() - - # Ensure we're no longer creating a volume - volumes = conn.get_all_volumes() - volumes.should.have.length_of(0) - - # Validate auto-created snapshot - snapshots = conn.get_all_snapshots() - snapshots.should.have.length_of(initial_ami_count + 1) - - retrieved_image_snapshot_id = ( - retrieved_image.block_device_mapping.current_value.snapshot_id - ) - [s.id for s in snapshots].should.contain(retrieved_image_snapshot_id) - snapshot = [s for s in snapshots if s.id == retrieved_image_snapshot_id][0] - snapshot.description.should.match("Created by CreateImage") - - # root device should be in AMI's block device mappings - root_mapping = retrieved_image.block_device_mapping.get( - retrieved_image.root_device_name - ) - root_mapping.should_not.be.none - - # Deregister - with pytest.raises(EC2ResponseError) as ex: - success = conn.deregister_image(image_id, dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the DeregisterImage operation: Request would have succeeded, but DryRun flag is set" - ) - - success = conn.deregister_image(image_id) - success.should.be.true - - with pytest.raises(EC2ResponseError) as cm: - conn.deregister_image(image_id) - cm.value.code.should.equal("InvalidAMIID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_snapshots_for_initial_amis(): ec2 = boto3.client("ec2", region_name="us-east-1") @@ -199,89 +120,6 @@ def test_ami_create_and_delete_boto3(): ex.value.response["ResponseMetadata"]["RequestId"].should_not.be.none -# Has boto3 equivalent -@requires_boto_gte("2.14.0") -@mock_ec2_deprecated -def test_ami_copy(): - conn = boto.ec2.connect_to_region("us-west-1") - - initial_ami_count = len(AMIS) - conn.get_all_volumes().should.have.length_of(0) - conn.get_all_snapshots().should.have.length_of(initial_ami_count) - - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - source_image_id = conn.create_image(instance.id, "test-ami", "this is a test ami") - instance.terminate() - source_image = conn.get_all_images(image_ids=[source_image_id])[0] - - # Boto returns a 'CopyImage' object with an image_id attribute here. Use - # the image_id to fetch the full info. - with pytest.raises(EC2ResponseError) as ex: - copy_image_ref = conn.copy_image( - source_image.region.name, - source_image.id, - "test-copy-ami", - "this is a test copy ami", - dry_run=True, - ) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the CopyImage operation: Request would have succeeded, but DryRun flag is set" - ) - - copy_image_ref = conn.copy_image( - source_image.region.name, - source_image.id, - "test-copy-ami", - "this is a test copy ami", - ) - copy_image_id = copy_image_ref.image_id - copy_image = conn.get_all_images(image_ids=[copy_image_id])[0] - - copy_image.id.should.equal(copy_image_id) - copy_image.virtualization_type.should.equal(source_image.virtualization_type) - copy_image.architecture.should.equal(source_image.architecture) - copy_image.kernel_id.should.equal(source_image.kernel_id) - copy_image.platform.should.equal(source_image.platform) - - # Ensure we're no longer creating a volume - conn.get_all_volumes().should.have.length_of(0) - - # Validate auto-created snapshot - conn.get_all_snapshots().should.have.length_of(initial_ami_count + 2) - - copy_image.block_device_mapping.current_value.snapshot_id.should_not.equal( - source_image.block_device_mapping.current_value.snapshot_id - ) - - # Copy from non-existent source ID. - with pytest.raises(EC2ResponseError) as cm: - conn.copy_image( - source_image.region.name, - "ami-abcd1234", - "test-copy-ami", - "this is a test copy ami", - ) - cm.value.code.should.equal("InvalidAMIID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - # Copy from non-existent source region. - with pytest.raises(EC2ResponseError) as cm: - invalid_region = ( - "us-east-1" if (source_image.region.name != "us-east-1") else "us-west-1" - ) - conn.copy_image( - invalid_region, source_image.id, "test-copy-ami", "this is a test copy ami" - ) - cm.value.code.should.equal("InvalidAMIID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_ami_copy_boto3_dryrun(): ec2 = boto3.client("ec2", region_name="us-west-1") @@ -430,35 +268,6 @@ def test_copy_image_changes_owner_id(): describe_resp[0]["ImageId"].should.equal(copy_resp["ImageId"]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_ami_tagging(): - conn = boto.connect_vpc("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - conn.create_image(instance.id, "test-ami", "this is a test ami") - image = conn.get_all_images()[0] - - with pytest.raises(EC2ResponseError) as ex: - image.add_tag("a key", "some value", dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the CreateTags operation: Request would have succeeded, but DryRun flag is set" - ) - - image.add_tag("a key", "some value") - - tag = conn.get_all_tags()[0] - tag.name.should.equal("a key") - tag.value.should.equal("some value") - - # Refresh the DHCP options - image = conn.get_all_images()[0] - image.tags.should.have.length_of(1) - image.tags["a key"].should.equal("some value") - - @mock_ec2 def test_ami_tagging_boto3(): ec2 = boto3.client("ec2", region_name="us-east-1") @@ -488,19 +297,6 @@ def test_ami_tagging_boto3(): image["Tags"].should.equal([{"Value": "some value", "Key": "a key"}]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_ami_create_from_missing_instance(): - conn = boto.connect_ec2("the_key", "the_secret") - args = ["i-abcdefg", "test-ami", "this is a test ami"] - - with pytest.raises(EC2ResponseError) as cm: - conn.create_image(*args) - cm.value.code.should.equal("InvalidInstanceID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_ami_create_from_missing_instance_boto3(): ec2 = boto3.client("ec2", region_name="us-east-1") @@ -514,19 +310,6 @@ def test_ami_create_from_missing_instance_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidInstanceID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_ami_pulls_attributes_from_instance(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - instance.modify_attribute("kernel", "test-kernel") - - image_id = conn.create_image(instance.id, "test-ami", "this is a test ami") - image = conn.get_image(image_id) - image.kernel_id.should.equal("test-kernel") - - @mock_ec2 def test_ami_pulls_attributes_from_instance_boto3(): ec2 = boto3.client("ec2", region_name="us-east-1") @@ -543,20 +326,6 @@ def test_ami_pulls_attributes_from_instance_boto3(): image.kernel_id.should.equal("test-kernel") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_ami_uses_account_id_if_valid_access_key_is_supplied(): - access_key = "AKIAXXXXXXXXXXXXXXXX" - conn = boto.connect_ec2(access_key, "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - instance.modify_attribute("kernel", "test-kernel") - - image_id = conn.create_image(instance.id, "test-ami", "this is a test ami") - images = conn.get_all_images(owners=["self"]) - [(ami.id, ami.owner_id) for ami in images].should.equal([(image_id, ACCOUNT_ID)]) - - @mock_ec2 def test_ami_uses_account_id_if_valid_access_key_is_supplied_boto3(): # The boto-equivalent required an access_key to be passed in, but Moto will always mock this in boto3 @@ -575,68 +344,6 @@ def test_ami_uses_account_id_if_valid_access_key_is_supplied_boto3(): ) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_ami_filters(): - conn = boto.connect_ec2("the_key", "the_secret") - - reservationA = conn.run_instances(EXAMPLE_AMI_ID) - instanceA = reservationA.instances[0] - instanceA.modify_attribute("architecture", "i386") - instanceA.modify_attribute("kernel", "k-1234abcd") - instanceA.modify_attribute("platform", "windows") - instanceA.modify_attribute("virtualization_type", "hvm") - imageA_id = conn.create_image(instanceA.id, "test-ami-A", "this is a test ami") - imageA = conn.get_image(imageA_id) - - reservationB = conn.run_instances(EXAMPLE_AMI_ID) - instanceB = reservationB.instances[0] - instanceB.modify_attribute("architecture", "x86_64") - instanceB.modify_attribute("kernel", "k-abcd1234") - instanceB.modify_attribute("platform", "linux") - instanceB.modify_attribute("virtualization_type", "paravirtual") - imageB_id = conn.create_image(instanceB.id, "test-ami-B", "this is a test ami") - imageB = conn.get_image(imageB_id) - imageB.set_launch_permissions(group_names=("all")) - - amis_by_architecture = conn.get_all_images(filters={"architecture": "x86_64"}) - set([ami.id for ami in amis_by_architecture]).should.contain(imageB.id) - len(amis_by_architecture).should.equal(39) - - amis_by_kernel = conn.get_all_images(filters={"kernel-id": "k-abcd1234"}) - set([ami.id for ami in amis_by_kernel]).should.equal(set([imageB.id])) - - amis_by_virtualization = conn.get_all_images( - filters={"virtualization-type": "paravirtual"} - ) - set([ami.id for ami in amis_by_virtualization]).should.contain(imageB.id) - len(amis_by_virtualization).should.equal(3) - - amis_by_platform = conn.get_all_images(filters={"platform": "windows"}) - set([ami.id for ami in amis_by_platform]).should.contain(imageA.id) - len(amis_by_platform).should.equal(25) - - amis_by_id = conn.get_all_images(filters={"image-id": imageA.id}) - set([ami.id for ami in amis_by_id]).should.equal(set([imageA.id])) - - amis_by_state = conn.get_all_images(filters={"state": "available"}) - ami_ids_by_state = [ami.id for ami in amis_by_state] - ami_ids_by_state.should.contain(imageA.id) - ami_ids_by_state.should.contain(imageB.id) - len(amis_by_state).should.equal(40) - - amis_by_name = conn.get_all_images(filters={"name": imageA.name}) - set([ami.id for ami in amis_by_name]).should.equal(set([imageA.id])) - - amis_by_public = conn.get_all_images(filters={"is-public": "true"}) - set([ami.id for ami in amis_by_public]).should.contain(imageB.id) - len(amis_by_public).should.equal(39) - - amis_by_nonpublic = conn.get_all_images(filters={"is-public": "false"}) - set([ami.id for ami in amis_by_nonpublic]).should.contain(imageA.id) - len(amis_by_nonpublic).should.equal(1) - - @mock_ec2 def test_ami_filters_boto3(): image_name_A = f"test-ami-{str(uuid4())[0:6]}" @@ -723,30 +430,6 @@ def test_ami_filters_boto3(): assert len(amis_by_nonpublic) >= 2, "Should have at least 2 non-public images" -# Has boto3 equivalent -@mock_ec2_deprecated -def test_ami_filtering_via_tag(): - conn = boto.connect_vpc("the_key", "the_secret") - - reservationA = conn.run_instances(EXAMPLE_AMI_ID) - instanceA = reservationA.instances[0] - imageA_id = conn.create_image(instanceA.id, "test-ami-A", "this is a test ami") - imageA = conn.get_image(imageA_id) - imageA.add_tag("a key", "some value") - - reservationB = conn.run_instances(EXAMPLE_AMI_ID) - instanceB = reservationB.instances[0] - imageB_id = conn.create_image(instanceB.id, "test-ami-B", "this is a test ami") - imageB = conn.get_image(imageB_id) - imageB.add_tag("another key", "some other value") - - amis_by_tagA = conn.get_all_images(filters={"tag:a key": "some value"}) - set([ami.id for ami in amis_by_tagA]).should.equal(set([imageA.id])) - - amis_by_tagB = conn.get_all_images(filters={"tag:another key": "some other value"}) - set([ami.id for ami in amis_by_tagB]).should.equal(set([imageB.id])) - - @mock_ec2 def test_ami_filtering_via_tag_boto3(): tag_value = f"value {str(uuid4())}" @@ -780,18 +463,6 @@ def test_ami_filtering_via_tag_boto3(): [ami["ImageId"] for ami in amis_by_tagB].should.equal([imageB_id]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_getting_missing_ami(): - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as cm: - conn.get_image("ami-missing") - cm.value.code.should.equal("InvalidAMIID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_getting_missing_ami_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -803,18 +474,6 @@ def test_getting_missing_ami_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidAMIID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_getting_malformed_ami(): - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as cm: - conn.get_image("foo-missing") - cm.value.code.should.equal("InvalidAMIID.Malformed") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_getting_malformed_ami_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -826,70 +485,6 @@ def test_getting_malformed_ami_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidAMIID.Malformed") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_ami_attribute_group_permissions(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - image_id = conn.create_image(instance.id, "test-ami", "this is a test ami") - image = conn.get_image(image_id) - - # Baseline - attributes = conn.get_image_attribute(image.id, attribute="launchPermission") - attributes.name.should.equal("launch_permission") - attributes.attrs.should.have.length_of(0) - - ADD_GROUP_ARGS = { - "image_id": image.id, - "attribute": "launchPermission", - "operation": "add", - "groups": "all", - } - - REMOVE_GROUP_ARGS = { - "image_id": image.id, - "attribute": "launchPermission", - "operation": "remove", - "groups": "all", - } - - # Add 'all' group and confirm - with pytest.raises(EC2ResponseError) as ex: - conn.modify_image_attribute(**dict(ADD_GROUP_ARGS, **{"dry_run": True})) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the ModifyImageAttribute operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.modify_image_attribute(**ADD_GROUP_ARGS) - - attributes = conn.get_image_attribute(image.id, attribute="launchPermission") - attributes.attrs["groups"].should.have.length_of(1) - attributes.attrs["groups"].should.equal(["all"]) - image = conn.get_image(image_id) - image.is_public.should.equal(True) - - # Add is idempotent - conn.modify_image_attribute.when.called_with(**ADD_GROUP_ARGS).should_not.throw( - EC2ResponseError - ) - - # Remove 'all' group and confirm - conn.modify_image_attribute(**REMOVE_GROUP_ARGS) - - attributes = conn.get_image_attribute(image.id, attribute="launchPermission") - attributes.attrs.should.have.length_of(0) - image = conn.get_image(image_id) - image.is_public.should.equal(False) - - # Remove is idempotent - conn.modify_image_attribute.when.called_with(**REMOVE_GROUP_ARGS).should_not.throw( - EC2ResponseError - ) - - @mock_ec2 def test_ami_attribute_group_permissions_boto3(): ec2 = boto3.client("ec2", region_name="us-east-1") @@ -956,82 +551,6 @@ def test_ami_attribute_group_permissions_boto3(): image.modify_attribute(**REMOVE_GROUP_ARGS) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_ami_attribute_user_permissions(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - image_id = conn.create_image(instance.id, "test-ami", "this is a test ami") - image = conn.get_image(image_id) - - # Baseline - attributes = conn.get_image_attribute(image.id, attribute="launchPermission") - attributes.name.should.equal("launch_permission") - attributes.attrs.should.have.length_of(0) - - # Both str and int values should work. - USER1 = "123456789011" - USER2 = 123456789022 - - ADD_USERS_ARGS = { - "image_id": image.id, - "attribute": "launchPermission", - "operation": "add", - "user_ids": [USER1, USER2], - } - - REMOVE_USERS_ARGS = { - "image_id": image.id, - "attribute": "launchPermission", - "operation": "remove", - "user_ids": [USER1, USER2], - } - - REMOVE_SINGLE_USER_ARGS = { - "image_id": image.id, - "attribute": "launchPermission", - "operation": "remove", - "user_ids": [USER1], - } - - # Add multiple users and confirm - conn.modify_image_attribute(**ADD_USERS_ARGS) - - attributes = conn.get_image_attribute(image.id, attribute="launchPermission") - attributes.attrs["user_ids"].should.have.length_of(2) - set(attributes.attrs["user_ids"]).should.equal(set([str(USER1), str(USER2)])) - image = conn.get_image(image_id) - image.is_public.should.equal(False) - - # Add is idempotent - conn.modify_image_attribute.when.called_with(**ADD_USERS_ARGS).should_not.throw( - EC2ResponseError - ) - - # Remove single user and confirm - conn.modify_image_attribute(**REMOVE_SINGLE_USER_ARGS) - - attributes = conn.get_image_attribute(image.id, attribute="launchPermission") - attributes.attrs["user_ids"].should.have.length_of(1) - set(attributes.attrs["user_ids"]).should.equal(set([str(USER2)])) - image = conn.get_image(image_id) - image.is_public.should.equal(False) - - # Remove multiple users and confirm - conn.modify_image_attribute(**REMOVE_USERS_ARGS) - - attributes = conn.get_image_attribute(image.id, attribute="launchPermission") - attributes.attrs.should.have.length_of(0) - image = conn.get_image(image_id) - image.is_public.should.equal(False) - - # Remove is idempotent - conn.modify_image_attribute.when.called_with(**REMOVE_USERS_ARGS).should_not.throw( - EC2ResponseError - ) - - @mock_ec2 def test_ami_attribute_user_permissions_boto3(): ec2 = boto3.client("ec2", region_name="us-east-1") @@ -1216,63 +735,6 @@ def test_ami_describe_executable_users_and_filter(): images[0]["ImageId"].should.equal(image_id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_ami_attribute_user_and_group_permissions(): - """ - Boto supports adding/removing both users and groups at the same time. - Just spot-check this -- input variations, idempotency, etc are validated - via user-specific and group-specific tests above. - """ - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - image_id = conn.create_image(instance.id, "test-ami", "this is a test ami") - image = conn.get_image(image_id) - - # Baseline - attributes = conn.get_image_attribute(image.id, attribute="launchPermission") - attributes.name.should.equal("launch_permission") - attributes.attrs.should.have.length_of(0) - - USER1 = "123456789011" - USER2 = "123456789022" - - ADD_ARGS = { - "image_id": image.id, - "attribute": "launchPermission", - "operation": "add", - "groups": ["all"], - "user_ids": [USER1, USER2], - } - - REMOVE_ARGS = { - "image_id": image.id, - "attribute": "launchPermission", - "operation": "remove", - "groups": ["all"], - "user_ids": [USER1, USER2], - } - - # Add and confirm - conn.modify_image_attribute(**ADD_ARGS) - - attributes = conn.get_image_attribute(image.id, attribute="launchPermission") - attributes.attrs["user_ids"].should.have.length_of(2) - set(attributes.attrs["user_ids"]).should.equal(set([USER1, USER2])) - set(attributes.attrs["groups"]).should.equal(set(["all"])) - image = conn.get_image(image_id) - image.is_public.should.equal(True) - - # Remove and confirm - conn.modify_image_attribute(**REMOVE_ARGS) - - attributes = conn.get_image_attribute(image.id, attribute="launchPermission") - attributes.attrs.should.have.length_of(0) - image = conn.get_image(image_id) - image.is_public.should.equal(False) - - @mock_ec2 def test_ami_attribute_user_and_group_permissions_boto3(): """ @@ -1363,98 +825,6 @@ def test_filter_description(): resp.should.have.length_of(1) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_ami_attribute_error_cases(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - image_id = conn.create_image(instance.id, "test-ami", "this is a test ami") - image = conn.get_image(image_id) - - # Error: Add with group != 'all' - with pytest.raises(EC2ResponseError) as cm: - conn.modify_image_attribute( - image.id, attribute="launchPermission", operation="add", groups="everyone" - ) - cm.value.code.should.equal("InvalidAMIAttributeItemValue") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - # Error: Add with user ID that isn't an integer. - with pytest.raises(EC2ResponseError) as cm: - conn.modify_image_attribute( - image.id, - attribute="launchPermission", - operation="add", - user_ids="12345678901A", - ) - cm.value.code.should.equal("InvalidAMIAttributeItemValue") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - # Error: Add with user ID that is > length 12. - with pytest.raises(EC2ResponseError) as cm: - conn.modify_image_attribute( - image.id, - attribute="launchPermission", - operation="add", - user_ids="1234567890123", - ) - cm.value.code.should.equal("InvalidAMIAttributeItemValue") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - # Error: Add with user ID that is < length 12. - with pytest.raises(EC2ResponseError) as cm: - conn.modify_image_attribute( - image.id, - attribute="launchPermission", - operation="add", - user_ids="12345678901", - ) - cm.value.code.should.equal("InvalidAMIAttributeItemValue") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - # Error: Add with one invalid user ID among other valid IDs, ensure no - # partial changes. - with pytest.raises(EC2ResponseError) as cm: - conn.modify_image_attribute( - image.id, - attribute="launchPermission", - operation="add", - user_ids=["123456789011", "foo", "123456789022"], - ) - cm.value.code.should.equal("InvalidAMIAttributeItemValue") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - attributes = conn.get_image_attribute(image.id, attribute="launchPermission") - attributes.attrs.should.have.length_of(0) - - # Error: Add with invalid image ID - with pytest.raises(EC2ResponseError) as cm: - conn.modify_image_attribute( - "ami-abcd1234", attribute="launchPermission", operation="add", groups="all" - ) - cm.value.code.should.equal("InvalidAMIID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - # Error: Remove with invalid image ID - with pytest.raises(EC2ResponseError) as cm: - conn.modify_image_attribute( - "ami-abcd1234", - attribute="launchPermission", - operation="remove", - groups="all", - ) - cm.value.code.should.equal("InvalidAMIID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_ami_attribute_error_cases_boto3(): ec2 = boto3.client("ec2", region_name="us-east-1") diff --git a/tests/test_ec2/test_availability_zones_and_regions.py b/tests/test_ec2/test_availability_zones_and_regions.py index f158e4b3e..60691e584 100644 --- a/tests/test_ec2/test_availability_zones_and_regions.py +++ b/tests/test_ec2/test_availability_zones_and_regions.py @@ -1,34 +1,9 @@ -import boto -import boto.ec2 import boto3 import sure # noqa # pylint: disable=unused-import import pytest from botocore.exceptions import ClientError -from moto import mock_ec2, mock_ec2_deprecated - - -# Has boto3 equivalent -@mock_ec2_deprecated -def test_describe_regions(): - conn = boto.connect_ec2("the_key", "the_secret") - regions = conn.get_all_regions() - len(regions).should.be.greater_than(1) - for region in regions: - region.endpoint.should.contain(region.name) - - -# Has boto3 equivalent -@mock_ec2_deprecated -def test_availability_zones(): - conn = boto.connect_ec2("the_key", "the_secret") - regions = conn.get_all_regions() - for region in regions: - conn = boto.ec2.connect_to_region(region.name) - if conn is None: - continue - for zone in conn.get_all_zones(): - zone.name.should.contain(region.name) +from moto import mock_ec2 @mock_ec2 diff --git a/tests/test_ec2/test_customer_gateways.py b/tests/test_ec2/test_customer_gateways.py index 5c32f6f17..087fb18bf 100644 --- a/tests/test_ec2/test_customer_gateways.py +++ b/tests/test_ec2/test_customer_gateways.py @@ -1,24 +1,9 @@ -import boto -import sure # noqa # pylint: disable=unused-import import boto3 import pytest -from boto.exception import EC2ResponseError +import sure # noqa # pylint: disable=unused-import + from botocore.exceptions import ClientError - -from moto import mock_ec2_deprecated, mock_ec2 - - -# Has boto3 equivalent -@mock_ec2_deprecated -def test_create_customer_gateways(): - conn = boto.connect_vpc("the_key", "the_secret") - - customer_gateway = conn.create_customer_gateway("ipsec.1", "205.251.242.54", 65534) - customer_gateway.should_not.be.none - customer_gateway.id.should.match(r"cgw-\w+") - customer_gateway.type.should.equal("ipsec.1") - customer_gateway.bgp_asn.should.equal(65534) - customer_gateway.ip_address.should.equal("205.251.242.54") +from moto import mock_ec2 @mock_ec2 @@ -32,16 +17,6 @@ def test_create_customer_gateways_boto3(): customer_gateway.should.have.key("IpAddress").equal("205.251.242.54") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_describe_customer_gateways(): - conn = boto.connect_vpc("the_key", "the_secret") - customer_gateway = conn.create_customer_gateway("ipsec.1", "205.251.242.54", 65534) - cgws = conn.get_all_customer_gateways() - cgws.should.have.length_of(1) - cgws[0].id.should.match(customer_gateway.id) - - @mock_ec2 def test_describe_customer_gateways_dryrun(): client = boto3.client("ec2", region_name="us-east-1") @@ -81,21 +56,6 @@ def test_describe_customer_gateways_boto3(): ), "Should have at least the one CustomerGateway we just created" -# Has boto3 equivalent -@mock_ec2_deprecated -def test_delete_customer_gateways(): - conn = boto.connect_vpc("the_key", "the_secret") - - customer_gateway = conn.create_customer_gateway("ipsec.1", "205.251.242.54", 65534) - customer_gateway.should_not.be.none - cgws = conn.get_all_customer_gateways() - cgws[0].id.should.match(customer_gateway.id) - conn.delete_customer_gateway(customer_gateway.id) - cgws = conn.get_all_customer_gateways() - cgws[0].state.should.equal("deleted") - cgws.should.have.length_of(1) - - @mock_ec2 def test_delete_customer_gateways_boto3(): ec2 = boto3.client("ec2", region_name="us-east-1") @@ -118,14 +78,6 @@ def test_delete_customer_gateways_boto3(): cgws[0].should.have.key("State").equal("deleted") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_delete_customer_gateways_bad_id(): - conn = boto.connect_vpc("the_key", "the_secret") - with pytest.raises(EC2ResponseError): - conn.delete_customer_gateway("cgw-0123abcd") - - @mock_ec2 def test_delete_customer_gateways_bad_id_boto3(): ec2 = boto3.client("ec2", region_name="us-east-1") diff --git a/tests/test_ec2/test_dhcp_options.py b/tests/test_ec2/test_dhcp_options.py index 673224be7..b695c52c8 100644 --- a/tests/test_ec2/test_dhcp_options.py +++ b/tests/test_ec2/test_dhcp_options.py @@ -1,33 +1,19 @@ import pytest import boto3 -import boto -from boto.exception import EC2ResponseError from botocore.exceptions import ClientError import sure # noqa # pylint: disable=unused-import import random import uuid -from moto import mock_ec2, mock_ec2_deprecated, settings +from moto import mock_ec2, settings from unittest import SkipTest SAMPLE_DOMAIN_NAME = "example.com" SAMPLE_NAME_SERVERS = ["10.0.0.6", "10.0.0.7"] -# Has boto3 equivalent -@mock_ec2_deprecated -def test_dhcp_options_associate(): - """associate dhcp option""" - conn = boto.connect_vpc("the_key", "the_secret") - dhcp_options = conn.create_dhcp_options(SAMPLE_DOMAIN_NAME, SAMPLE_NAME_SERVERS) - vpc = conn.create_vpc("10.0.0.0/16") - - rval = conn.associate_dhcp_options(dhcp_options.id, vpc.id) - rval.should.be.equal(True) - - @mock_ec2 def test_dhcp_options_associate_boto3(): """ associate dhcp option """ @@ -48,20 +34,6 @@ def test_dhcp_options_associate_boto3(): vpc.dhcp_options_id.should.equal(dhcp_options.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_dhcp_options_associate_invalid_dhcp_id(): - """associate dhcp option bad dhcp options id""" - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - - with pytest.raises(EC2ResponseError) as cm: - conn.associate_dhcp_options("foo", vpc.id) - cm.value.code.should.equal("InvalidDhcpOptionID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_dhcp_options_associate_invalid_dhcp_id_boto3(): """ associate dhcp option bad dhcp options id """ @@ -76,20 +48,6 @@ def test_dhcp_options_associate_invalid_dhcp_id_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidDhcpOptionID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_dhcp_options_associate_invalid_vpc_id(): - """associate dhcp option invalid vpc id""" - conn = boto.connect_vpc("the_key", "the_secret") - dhcp_options = conn.create_dhcp_options(SAMPLE_DOMAIN_NAME, SAMPLE_NAME_SERVERS) - - with pytest.raises(EC2ResponseError) as cm: - conn.associate_dhcp_options(dhcp_options.id, "foo") - cm.value.code.should.equal("InvalidVpcID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_dhcp_options_associate_invalid_vpc_id_boto3(): """ associate dhcp option invalid vpc id """ @@ -109,33 +67,6 @@ def test_dhcp_options_associate_invalid_vpc_id_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidVpcID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_dhcp_options_delete_with_vpc(): - """Test deletion of dhcp options with vpc""" - conn = boto.connect_vpc("the_key", "the_secret") - dhcp_options = conn.create_dhcp_options(SAMPLE_DOMAIN_NAME, SAMPLE_NAME_SERVERS) - dhcp_options_id = dhcp_options.id - vpc = conn.create_vpc("10.0.0.0/16") - - rval = conn.associate_dhcp_options(dhcp_options_id, vpc.id) - rval.should.be.equal(True) - - with pytest.raises(EC2ResponseError) as cm: - conn.delete_dhcp_options(dhcp_options_id) - cm.value.code.should.equal("DependencyViolation") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - vpc.delete() - - with pytest.raises(EC2ResponseError) as cm: - conn.get_all_dhcp_options([dhcp_options_id]) - cm.value.code.should.equal("InvalidDhcpOptionID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_dhcp_options_delete_with_vpc_boto3(): """Test deletion of dhcp options with vpc""" @@ -166,22 +97,6 @@ def test_dhcp_options_delete_with_vpc_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidDhcpOptionID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_create_dhcp_options(): - """Create most basic dhcp option""" - conn = boto.connect_vpc("the_key", "the_secret") - - dhcp_option = conn.create_dhcp_options(SAMPLE_DOMAIN_NAME, SAMPLE_NAME_SERVERS) - dhcp_option.options["domain-name"][0].should.be.equal(SAMPLE_DOMAIN_NAME) - dhcp_option.options["domain-name-servers"][0].should.be.equal( - SAMPLE_NAME_SERVERS[0] - ) - dhcp_option.options["domain-name-servers"][1].should.be.equal( - SAMPLE_NAME_SERVERS[1] - ) - - @mock_ec2 def test_create_dhcp_options_boto3(): """Create most basic dhcp option""" @@ -206,26 +121,6 @@ def test_create_dhcp_options_boto3(): ) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_create_dhcp_options_invalid_options(): - """Create invalid dhcp options""" - conn = boto.connect_vpc("the_key", "the_secret") - servers = ["f", "f", "f", "f", "f"] - - with pytest.raises(EC2ResponseError) as cm: - conn.create_dhcp_options(ntp_servers=servers) - cm.value.code.should.equal("InvalidParameterValue") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - with pytest.raises(EC2ResponseError) as cm: - conn.create_dhcp_options(netbios_node_type="0") - cm.value.code.should.equal("InvalidParameterValue") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_create_dhcp_options_invalid_options_boto3(): """Create invalid dhcp options""" @@ -249,20 +144,6 @@ def test_create_dhcp_options_invalid_options_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidParameterValue") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_describe_dhcp_options(): - """Test dhcp options lookup by id""" - conn = boto.connect_vpc("the_key", "the_secret") - - dhcp_option = conn.create_dhcp_options() - dhcp_options = conn.get_all_dhcp_options([dhcp_option.id]) - dhcp_options.should.be.length_of(1) - - dhcp_options = conn.get_all_dhcp_options() - dhcp_options.should.be.length_of(1) - - @mock_ec2 def test_describe_dhcp_options_boto3(): """Test dhcp options lookup by id""" @@ -299,19 +180,6 @@ def test_describe_dhcp_options_boto3(): ) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_describe_dhcp_options_invalid_id(): - """get error on invalid dhcp_option_id lookup""" - conn = boto.connect_vpc("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as cm: - conn.get_all_dhcp_options(["1"]) - cm.value.code.should.equal("InvalidDhcpOptionID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_describe_dhcp_options_invalid_id_boto3(): """get error on invalid dhcp_option_id lookup""" @@ -324,25 +192,6 @@ def test_describe_dhcp_options_invalid_id_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidDhcpOptionID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_delete_dhcp_options(): - """delete dhcp option""" - conn = boto.connect_vpc("the_key", "the_secret") - - dhcp_option = conn.create_dhcp_options() - dhcp_options = conn.get_all_dhcp_options([dhcp_option.id]) - dhcp_options.should.be.length_of(1) - - conn.delete_dhcp_options(dhcp_option.id) # .should.be.equal(True) - - with pytest.raises(EC2ResponseError) as cm: - conn.get_all_dhcp_options([dhcp_option.id]) - cm.value.code.should.equal("InvalidDhcpOptionID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_delete_dhcp_options_boto3(): """delete dhcp option""" @@ -365,20 +214,6 @@ def test_delete_dhcp_options_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidDhcpOptionID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_delete_dhcp_options_invalid_id(): - conn = boto.connect_vpc("the_key", "the_secret") - - conn.create_dhcp_options() - - with pytest.raises(EC2ResponseError) as cm: - conn.delete_dhcp_options("dopt-abcd1234") - cm.value.code.should.equal("InvalidDhcpOptionID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_delete_dhcp_options_invalid_id_boto3(): client = boto3.client("ec2", region_name="us-west-1") @@ -390,20 +225,6 @@ def test_delete_dhcp_options_invalid_id_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidDhcpOptionID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_delete_dhcp_options_malformed_id(): - conn = boto.connect_vpc("the_key", "the_secret") - - conn.create_dhcp_options() - - with pytest.raises(EC2ResponseError) as cm: - conn.delete_dhcp_options("foo-abcd1234") - cm.value.code.should.equal("InvalidDhcpOptionsId.Malformed") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_delete_dhcp_options_malformed_id_boto3(): client = boto3.client("ec2", region_name="us-west-1") @@ -415,24 +236,6 @@ def test_delete_dhcp_options_malformed_id_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidDhcpOptionsId.Malformed") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_dhcp_tagging(): - conn = boto.connect_vpc("the_key", "the_secret") - dhcp_option = conn.create_dhcp_options() - - dhcp_option.add_tag("a key", "some value") - - tag = conn.get_all_tags()[0] - tag.name.should.equal("a key") - tag.value.should.equal("some value") - - # Refresh the DHCP options - dhcp_option = conn.get_all_dhcp_options()[0] - dhcp_option.tags.should.have.length_of(1) - dhcp_option.tags["a key"].should.equal("some value") - - @mock_ec2 def test_dhcp_tagging_boto3(): ec2 = boto3.resource("ec2", region_name="us-west-1") @@ -463,43 +266,6 @@ def test_dhcp_tagging_boto3(): dhcp_option["Tags"].should.equal([{"Key": "a tag", "Value": tag_value}]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_dhcp_options_get_by_tag(): - conn = boto.connect_vpc("the_key", "the_secret") - - dhcp1 = conn.create_dhcp_options("example.com", ["10.0.10.2"]) - dhcp1.add_tag("Name", "TestDhcpOptions1") - dhcp1.add_tag("test-tag", "test-value") - - dhcp2 = conn.create_dhcp_options("example.com", ["10.0.20.2"]) - dhcp2.add_tag("Name", "TestDhcpOptions2") - dhcp2.add_tag("test-tag", "test-value") - - filters = {"tag:Name": "TestDhcpOptions1", "tag:test-tag": "test-value"} - dhcp_options_sets = conn.get_all_dhcp_options(filters=filters) - - dhcp_options_sets.should.have.length_of(1) - dhcp_options_sets[0].options["domain-name"][0].should.be.equal("example.com") - dhcp_options_sets[0].options["domain-name-servers"][0].should.be.equal("10.0.10.2") - dhcp_options_sets[0].tags["Name"].should.equal("TestDhcpOptions1") - dhcp_options_sets[0].tags["test-tag"].should.equal("test-value") - - filters = {"tag:Name": "TestDhcpOptions2", "tag:test-tag": "test-value"} - dhcp_options_sets = conn.get_all_dhcp_options(filters=filters) - - dhcp_options_sets.should.have.length_of(1) - dhcp_options_sets[0].options["domain-name"][0].should.be.equal("example.com") - dhcp_options_sets[0].options["domain-name-servers"][0].should.be.equal("10.0.20.2") - dhcp_options_sets[0].tags["Name"].should.equal("TestDhcpOptions2") - dhcp_options_sets[0].tags["test-tag"].should.equal("test-value") - - filters = {"tag:test-tag": "test-value"} - dhcp_options_sets = conn.get_all_dhcp_options(filters=filters) - - dhcp_options_sets.should.have.length_of(2) - - @mock_ec2 def test_dhcp_options_get_by_tag_boto3(): ec2 = boto3.resource("ec2", region_name="us-west-1") @@ -580,37 +346,6 @@ def test_dhcp_options_get_by_tag_boto3(): dhcp_options_sets.should.have.length_of(2) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_dhcp_options_get_by_id(): - conn = boto.connect_vpc("the_key", "the_secret") - - dhcp1 = conn.create_dhcp_options("test1.com", ["10.0.10.2"]) - dhcp1.add_tag("Name", "TestDhcpOptions1") - dhcp1.add_tag("test-tag", "test-value") - dhcp1_id = dhcp1.id - - dhcp2 = conn.create_dhcp_options("test2.com", ["10.0.20.2"]) - dhcp2.add_tag("Name", "TestDhcpOptions2") - dhcp2.add_tag("test-tag", "test-value") - dhcp2_id = dhcp2.id - - dhcp_options_sets = conn.get_all_dhcp_options() - dhcp_options_sets.should.have.length_of(2) - - dhcp_options_sets = conn.get_all_dhcp_options(filters={"dhcp-options-id": dhcp1_id}) - - dhcp_options_sets.should.have.length_of(1) - dhcp_options_sets[0].options["domain-name"][0].should.be.equal("test1.com") - dhcp_options_sets[0].options["domain-name-servers"][0].should.be.equal("10.0.10.2") - - dhcp_options_sets = conn.get_all_dhcp_options(filters={"dhcp-options-id": dhcp2_id}) - - dhcp_options_sets.should.have.length_of(1) - dhcp_options_sets[0].options["domain-name"][0].should.be.equal("test2.com") - dhcp_options_sets[0].options["domain-name-servers"][0].should.be.equal("10.0.20.2") - - @mock_ec2 def test_dhcp_options_get_by_id_boto3(): ec2 = boto3.resource("ec2", region_name="us-west-1") @@ -724,19 +459,6 @@ def test_dhcp_options_get_by_key_filter(): servers.should.contain({"Value": "example.com"}) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_dhcp_options_get_by_invalid_filter(): - conn = boto.connect_vpc("the_key", "the_secret") - - conn.create_dhcp_options(SAMPLE_DOMAIN_NAME, SAMPLE_NAME_SERVERS) - filters = {"invalid-filter": "invalid-value"} - - conn.get_all_dhcp_options.when.called_with(filters=filters).should.throw( - NotImplementedError - ) - - @mock_ec2 def test_dhcp_options_get_by_invalid_filter_boto3(): if settings.TEST_SERVER_MODE: diff --git a/tests/test_ec2/test_ec2_cloudformation.py b/tests/test_ec2/test_ec2_cloudformation.py index afaec395f..d12c3f3ca 100644 --- a/tests/test_ec2/test_ec2_cloudformation.py +++ b/tests/test_ec2/test_ec2_cloudformation.py @@ -1,5 +1,4 @@ from botocore.exceptions import ClientError -from moto import mock_cloudformation_deprecated, mock_ec2_deprecated from moto import mock_cloudformation, mock_ec2 from tests import EXAMPLE_AMI_ID from tests.test_cloudformation.fixtures import ec2_classic_eip @@ -8,10 +7,6 @@ from tests.test_cloudformation.fixtures import vpc_eip from tests.test_cloudformation.fixtures import vpc_eni from tests.test_cloudformation.fixtures import vpc_single_instance_in_subnet from uuid import uuid4 -import boto -import boto.ec2 -import boto.cloudformation -import boto.vpc import boto3 import json import sure # noqa # pylint: disable=unused-import @@ -113,31 +108,6 @@ def test_delete_stack_with_resource_missing_delete_attr(): ec2.describe_vpcs(VpcIds=[vpc_id])["Vpcs"].should.have.length_of(1) -# Has boto3 equivalent -@mock_ec2_deprecated -@mock_cloudformation_deprecated -def test_elastic_network_interfaces_cloudformation(): - template = vpc_eni.template - template_json = json.dumps(template) - conn = boto.cloudformation.connect_to_region("us-west-1") - conn.create_stack("test_stack", template_body=template_json) - ec2_conn = boto.ec2.connect_to_region("us-west-1") - eni = ec2_conn.get_all_network_interfaces()[0] - eni.private_ip_addresses.should.have.length_of(1) - - stack = conn.describe_stacks()[0] - resources = stack.describe_resources() - cfn_eni = [ - resource - for resource in resources - if resource.resource_type == "AWS::EC2::NetworkInterface" - ][0] - cfn_eni.physical_resource_id.should.equal(eni.id) - - outputs = {output.key: output.value for output in stack.outputs} - outputs["ENIIpAddress"].should.equal(eni.private_ip_addresses[0].private_ip_address) - - @mock_ec2 @mock_cloudformation def test_elastic_network_interfaces_cloudformation_boto3(): diff --git a/tests/test_ec2/test_elastic_block_store.py b/tests/test_ec2/test_elastic_block_store.py index 8ffee9d04..c6d4cc5e6 100644 --- a/tests/test_ec2/test_elastic_block_store.py +++ b/tests/test_ec2/test_elastic_block_store.py @@ -1,57 +1,15 @@ -import boto -import boto.ec2 import boto3 import pytest import sure # noqa # pylint: disable=unused-import -from boto.exception import EC2ResponseError from botocore.exceptions import ClientError -from moto import mock_ec2, mock_ec2_deprecated -from moto.ec2 import ec2_backends +from moto import mock_ec2 from moto.ec2.models import OWNER_ID from moto.kms import mock_kms from tests import EXAMPLE_AMI_ID from uuid import uuid4 -# Has boto3 equivalent -@mock_ec2_deprecated -def test_create_and_delete_volume(): - conn = boto.ec2.connect_to_region("us-east-1") - volume = conn.create_volume(80, "us-east-1a") - - all_volumes = conn.get_all_volumes() - - current_volume = [item for item in all_volumes if item.id == volume.id] - current_volume.should.have.length_of(1) - current_volume[0].size.should.equal(80) - current_volume[0].zone.should.equal("us-east-1a") - current_volume[0].encrypted.should.be(False) - - volume = current_volume[0] - - with pytest.raises(EC2ResponseError) as ex: - volume.delete(dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the DeleteVolume operation: Request would have succeeded, but DryRun flag is set" - ) - - volume.delete() - - all_volumes = conn.get_all_volumes() - my_volume = [item for item in all_volumes if item.id == volume.id] - my_volume.should.have.length_of(0) - - # Deleting something that was already deleted should throw an error - with pytest.raises(EC2ResponseError) as cm: - volume.delete() - cm.value.code.should.equal("InvalidVolume.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_create_and_delete_volume_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -88,46 +46,6 @@ def test_create_and_delete_volume_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidVolume.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_delete_attached_volume(): - conn = boto.ec2.connect_to_region("us-east-1") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - # create an instance - instance = reservation.instances[0] - # create a volume - volume = conn.create_volume(80, "us-east-1a") - # attach volume to instance - volume.attach(instance.id, "/dev/sdh") - - volume.update() - volume.volume_state().should.equal("in-use") - volume.attachment_state().should.equal("attached") - - volume.attach_data.instance_id.should.equal(instance.id) - - # attempt to delete volume - # assert raises VolumeInUseError - with pytest.raises(EC2ResponseError) as ex: - volume.delete() - ex.value.error_code.should.equal("VolumeInUse") - ex.value.status.should.equal(400) - ex.value.message.should.equal( - "Volume {0} is currently attached to {1}".format(volume.id, instance.id) - ) - - volume.detach() - - volume.update() - volume.volume_state().should.equal("available") - - volume.delete() - - all_volumes = conn.get_all_volumes() - my_volume = [item for item in all_volumes if item.id == volume.id] - my_volume.should.have.length_of(0) - - @mock_ec2 def test_delete_attached_volume_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -168,19 +86,6 @@ def test_delete_attached_volume_boto3(): [v["VolumeId"] for v in all_volumes].shouldnt.contain(volume.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_create_encrypted_volume_dryrun(): - conn = boto.ec2.connect_to_region("us-east-1") - with pytest.raises(EC2ResponseError) as ex: - conn.create_volume(80, "us-east-1a", encrypted=True, dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the CreateVolume operation: Request would have succeeded, but DryRun flag is set" - ) - - @mock_ec2 def test_create_encrypted_volume_dryrun_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -193,24 +98,6 @@ def test_create_encrypted_volume_dryrun_boto3(): ) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_create_encrypted_volume(): - conn = boto.ec2.connect_to_region("us-east-1") - volume = conn.create_volume(80, "us-east-1a", encrypted=True) - - with pytest.raises(EC2ResponseError) as ex: - conn.create_volume(80, "us-east-1a", encrypted=True, dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the CreateVolume operation: Request would have succeeded, but DryRun flag is set" - ) - - all_volumes = [vol for vol in conn.get_all_volumes() if vol.id == volume.id] - all_volumes[0].encrypted.should.be(True) - - @mock_ec2 def test_create_encrypted_volume_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -221,27 +108,6 @@ def test_create_encrypted_volume_boto3(): all_volumes[0]["Encrypted"].should.be(True) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_filter_volume_by_id(): - conn = boto.ec2.connect_to_region("us-east-1") - volume1 = conn.create_volume(80, "us-east-1a") - volume2 = conn.create_volume(36, "us-east-1b") - volume3 = conn.create_volume(20, "us-east-1c") - vol1 = conn.get_all_volumes(volume_ids=volume3.id) - vol1.should.have.length_of(1) - vol1[0].size.should.equal(20) - vol1[0].zone.should.equal("us-east-1c") - vol2 = conn.get_all_volumes(volume_ids=[volume1.id, volume2.id]) - vol2.should.have.length_of(2) - - with pytest.raises(EC2ResponseError) as cm: - conn.get_all_volumes(volume_ids=["vol-does_not_exist"]) - cm.value.code.should.equal("InvalidVolume.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_filter_volume_by_id_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -264,115 +130,6 @@ def test_filter_volume_by_id_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidVolume.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_volume_filters(): - conn = boto.ec2.connect_to_region("us-east-1") - - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - instance.update() - - volume1 = conn.create_volume(80, "us-east-1a", encrypted=True) - volume2 = conn.create_volume(36, "us-east-1b", encrypted=False) - volume3 = conn.create_volume(20, "us-east-1c", encrypted=True) - - snapshot = volume3.create_snapshot(description="testsnap") - volume4 = conn.create_volume(25, "us-east-1a", snapshot=snapshot) - - conn.create_tags([volume1.id], {"testkey1": "testvalue1"}) - conn.create_tags([volume2.id], {"testkey2": "testvalue2"}) - - volume1.update() - volume2.update() - volume3.update() - volume4.update() - - block_mapping = instance.block_device_mapping["/dev/sda1"] - - volume_ids = ( - volume1.id, - volume2.id, - volume3.id, - volume4.id, - block_mapping.volume_id, - ) - - volumes_by_attach_time = conn.get_all_volumes( - filters={"attachment.attach-time": block_mapping.attach_time} - ) - set([vol.id for vol in volumes_by_attach_time]).should.equal( - {block_mapping.volume_id} - ) - - volumes_by_attach_device = conn.get_all_volumes( - filters={"attachment.device": "/dev/sda1"} - ) - set([vol.id for vol in volumes_by_attach_device]).should.equal( - {block_mapping.volume_id} - ) - - volumes_by_attach_instance_id = conn.get_all_volumes( - filters={"attachment.instance-id": instance.id} - ) - set([vol.id for vol in volumes_by_attach_instance_id]).should.equal( - {block_mapping.volume_id} - ) - - volumes_by_attach_status = conn.get_all_volumes( - filters={"attachment.status": "attached"} - ) - set([vol.id for vol in volumes_by_attach_status]).should.equal( - {block_mapping.volume_id} - ) - - volumes_by_create_time = conn.get_all_volumes( - filters={"create-time": volume4.create_time} - ) - set([vol.create_time for vol in volumes_by_create_time]).should.equal( - {volume4.create_time} - ) - - volumes_by_size = conn.get_all_volumes(filters={"size": volume2.size}) - set([vol.id for vol in volumes_by_size]).should.equal({volume2.id}) - - volumes_by_snapshot_id = conn.get_all_volumes(filters={"snapshot-id": snapshot.id}) - set([vol.id for vol in volumes_by_snapshot_id]).should.equal({volume4.id}) - - volumes_by_status = conn.get_all_volumes(filters={"status": "in-use"}) - set([vol.id for vol in volumes_by_status]).should.equal({block_mapping.volume_id}) - - volumes_by_id = conn.get_all_volumes(filters={"volume-id": volume1.id}) - set([vol.id for vol in volumes_by_id]).should.equal({volume1.id}) - - volumes_by_tag_key = conn.get_all_volumes(filters={"tag-key": "testkey1"}) - set([vol.id for vol in volumes_by_tag_key]).should.equal({volume1.id}) - - volumes_by_tag_value = conn.get_all_volumes(filters={"tag-value": "testvalue1"}) - set([vol.id for vol in volumes_by_tag_value]).should.equal({volume1.id}) - - volumes_by_tag = conn.get_all_volumes(filters={"tag:testkey1": "testvalue1"}) - set([vol.id for vol in volumes_by_tag]).should.equal({volume1.id}) - - volumes_by_unencrypted = conn.get_all_volumes(filters={"encrypted": "false"}) - set( - [vol.id for vol in volumes_by_unencrypted if vol.id in volume_ids] - ).should.equal({block_mapping.volume_id, volume2.id}) - - volumes_by_encrypted = conn.get_all_volumes(filters={"encrypted": "true"}) - set([vol.id for vol in volumes_by_encrypted if vol.id in volume_ids]).should.equal( - {volume1.id, volume3.id, volume4.id} - ) - - volumes_by_availability_zone = conn.get_all_volumes( - filters={"availability-zone": "us-east-1b"} - ) - set( - [vol.id for vol in volumes_by_availability_zone if vol.id in volume_ids] - ).should.equal({volume2.id}) - - @mock_ec2 def test_volume_filters_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -496,65 +253,6 @@ def test_volume_filters_boto3(): [vol["VolumeId"] for vol in volumes_by_attach_device].should.contain(volume4.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_volume_attach_and_detach(): - conn = boto.ec2.connect_to_region("us-east-1") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - volume = conn.create_volume(80, "us-east-1a") - - volume.update() - volume.volume_state().should.equal("available") - - with pytest.raises(EC2ResponseError) as ex: - volume.attach(instance.id, "/dev/sdh", dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the AttachVolume operation: Request would have succeeded, but DryRun flag is set" - ) - - volume.attach(instance.id, "/dev/sdh") - - volume.update() - volume.volume_state().should.equal("in-use") - volume.attachment_state().should.equal("attached") - - volume.attach_data.instance_id.should.equal(instance.id) - - with pytest.raises(EC2ResponseError) as ex: - volume.detach(dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the DetachVolume operation: Request would have succeeded, but DryRun flag is set" - ) - - volume.detach() - - volume.update() - volume.volume_state().should.equal("available") - - with pytest.raises(EC2ResponseError) as cm1: - volume.attach("i-1234abcd", "/dev/sdh") - cm1.value.code.should.equal("InvalidInstanceID.NotFound") - cm1.value.status.should.equal(400) - cm1.value.request_id.should_not.be.none - - with pytest.raises(EC2ResponseError) as cm2: - conn.detach_volume(volume.id, instance.id, "/dev/sdh") - cm2.value.code.should.equal("InvalidAttachment.NotFound") - cm2.value.status.should.equal(400) - cm2.value.request_id.should_not.be.none - - with pytest.raises(EC2ResponseError) as cm3: - conn.detach_volume(volume.id, "i-1234abcd", "/dev/sdh") - cm3.value.code.should.equal("InvalidInstanceID.NotFound") - cm3.value.status.should.equal(400) - cm3.value.request_id.should_not.be.none - - @mock_ec2 def test_volume_attach_and_detach_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -621,47 +319,6 @@ def test_volume_attach_and_detach_boto3(): ex3.value.response["Error"]["Code"].should.equal("InvalidInstanceID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_create_snapshot(): - conn = boto.ec2.connect_to_region("us-east-1") - volume = conn.create_volume(80, "us-east-1a") - - with pytest.raises(EC2ResponseError) as ex: - snapshot = volume.create_snapshot("a dryrun snapshot", dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the CreateSnapshot operation: Request would have succeeded, but DryRun flag is set" - ) - - snapshot = volume.create_snapshot("a test snapshot") - snapshot.update() - snapshot.status.should.equal("completed") - - snapshots = [snap for snap in conn.get_all_snapshots() if snap.id == snapshot.id] - snapshots.should.have.length_of(1) - snapshots[0].description.should.equal("a test snapshot") - snapshots[0].start_time.should_not.be.none - snapshots[0].encrypted.should.be(False) - - # Create snapshot without description - num_snapshots = len(conn.get_all_snapshots()) - - snapshot = volume.create_snapshot() - conn.get_all_snapshots().should.have.length_of(num_snapshots + 1) - - snapshot.delete() - conn.get_all_snapshots().should.have.length_of(num_snapshots) - - # Deleting something that was already deleted should throw an error - with pytest.raises(EC2ResponseError) as cm: - snapshot.delete() - cm.value.code.should.equal("InvalidSnapshot.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_create_snapshot_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -706,22 +363,6 @@ def test_create_snapshot_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidSnapshot.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_create_encrypted_snapshot(): - conn = boto.ec2.connect_to_region("us-east-1") - volume = conn.create_volume(80, "us-east-1a", encrypted=True) - snapshot = volume.create_snapshot("a test snapshot") - snapshot.update() - snapshot.status.should.equal("completed") - - snapshots = [snap for snap in conn.get_all_snapshots() if snap.id == snapshot.id] - snapshots.should.have.length_of(1) - snapshots[0].description.should.equal("a test snapshot") - snapshots[0].start_time.should_not.be.none - snapshots[0].encrypted.should.be(True) - - @mock_ec2 @pytest.mark.parametrize("encrypted", [True, False]) def test_create_encrypted_snapshot_boto3(encrypted): @@ -746,34 +387,6 @@ def test_create_encrypted_snapshot_boto3(encrypted): snapshots[0]["Encrypted"].should.be(encrypted) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_filter_snapshot_by_id(): - conn = boto.ec2.connect_to_region("us-east-1") - volume1 = conn.create_volume(36, "us-east-1a") - volume1.create_snapshot("a test snapshot 1") - volume2 = conn.create_volume(42, "us-east-1a") - snap2 = volume2.create_snapshot("a test snapshot 2") - volume3 = conn.create_volume(84, "us-east-1a") - snap3 = volume3.create_snapshot("a test snapshot 3") - snapshots1 = conn.get_all_snapshots(snapshot_ids=snap2.id) - snapshots1.should.have.length_of(1) - snapshots1[0].volume_id.should.equal(volume2.id) - snapshots1[0].region.name.should.equal(conn.region.name) - snapshots2 = conn.get_all_snapshots(snapshot_ids=[snap2.id, snap3.id]) - snapshots2.should.have.length_of(2) - for s in snapshots2: - s.start_time.should_not.be.none - s.volume_id.should.be.within([volume2.id, volume3.id]) - s.region.name.should.equal(conn.region.name) - - with pytest.raises(EC2ResponseError) as cm: - conn.get_all_snapshots(snapshot_ids=["snap-does_not_exist"]) - cm.value.code.should.equal("InvalidSnapshot.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_filter_snapshot_by_id_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -802,71 +415,6 @@ def test_filter_snapshot_by_id_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidSnapshot.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_snapshot_filters(): - conn = boto.ec2.connect_to_region("us-east-1") - volume1 = conn.create_volume(20, "us-east-1a", encrypted=False) - volume2 = conn.create_volume(25, "us-east-1a", encrypted=True) - - snapshot1 = volume1.create_snapshot(description="testsnapshot1") - snapshot2 = volume1.create_snapshot(description="testsnapshot2") - snapshot3 = volume2.create_snapshot(description="testsnapshot3") - - conn.create_tags([snapshot1.id], {"testkey1": "testvalue1"}) - conn.create_tags([snapshot2.id], {"testkey2": "testvalue2"}) - - snapshots_by_description = conn.get_all_snapshots( - filters={"description": "testsnapshot1"} - ) - set([snap.id for snap in snapshots_by_description]).should.equal({snapshot1.id}) - - snapshots_by_id = conn.get_all_snapshots(filters={"snapshot-id": snapshot1.id}) - set([snap.id for snap in snapshots_by_id]).should.equal({snapshot1.id}) - - snapshots_by_start_time = conn.get_all_snapshots( - filters={"start-time": snapshot1.start_time} - ) - set([snap.start_time for snap in snapshots_by_start_time]).should.equal( - {snapshot1.start_time} - ) - - snapshots_by_volume_id = conn.get_all_snapshots(filters={"volume-id": volume1.id}) - set([snap.id for snap in snapshots_by_volume_id]).should.equal( - {snapshot1.id, snapshot2.id} - ) - - snapshots_by_status = conn.get_all_snapshots(filters={"status": "completed"}) - ( - {snapshot1.id, snapshot2.id, snapshot3.id} - - {snap.id for snap in snapshots_by_status} - ).should.have.length_of(0) - - snapshots_by_volume_size = conn.get_all_snapshots( - filters={"volume-size": volume1.size} - ) - set([snap.id for snap in snapshots_by_volume_size]).should.equal( - {snapshot1.id, snapshot2.id} - ) - - snapshots_by_tag_key = conn.get_all_snapshots(filters={"tag-key": "testkey1"}) - set([snap.id for snap in snapshots_by_tag_key]).should.equal({snapshot1.id}) - - snapshots_by_tag_value = conn.get_all_snapshots(filters={"tag-value": "testvalue1"}) - set([snap.id for snap in snapshots_by_tag_value]).should.equal({snapshot1.id}) - - snapshots_by_tag = conn.get_all_snapshots(filters={"tag:testkey1": "testvalue1"}) - set([snap.id for snap in snapshots_by_tag]).should.equal({snapshot1.id}) - - snapshots_by_encrypted = conn.get_all_snapshots(filters={"encrypted": "true"}) - set([snap.id for snap in snapshots_by_encrypted]).should.equal({snapshot3.id}) - - snapshots_by_owner_id = conn.get_all_snapshots(filters={"owner-id": OWNER_ID}) - set([snap.id for snap in snapshots_by_owner_id]).should.equal( - {snapshot1.id, snapshot2.id, snapshot3.id} - ) - - @mock_ec2 def test_snapshot_filters_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -937,115 +485,6 @@ def test_snapshot_filters_boto3(): [s["SnapshotId"] for s in snapshots].should.contain(snapshot3.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_snapshot_attribute(): - conn = boto.ec2.connect_to_region("us-east-1") - volume = conn.create_volume(80, "us-east-1a") - snapshot = volume.create_snapshot() - - # Baseline - attributes = conn.get_snapshot_attribute( - snapshot.id, attribute="createVolumePermission" - ) - attributes.name.should.equal("create_volume_permission") - attributes.attrs.should.have.length_of(0) - - ADD_GROUP_ARGS = { - "snapshot_id": snapshot.id, - "attribute": "createVolumePermission", - "operation": "add", - "groups": "all", - } - - REMOVE_GROUP_ARGS = { - "snapshot_id": snapshot.id, - "attribute": "createVolumePermission", - "operation": "remove", - "groups": "all", - } - - # Add 'all' group and confirm - - with pytest.raises(EC2ResponseError) as ex: - conn.modify_snapshot_attribute(**dict(ADD_GROUP_ARGS, **{"dry_run": True})) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the ModifySnapshotAttribute operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.modify_snapshot_attribute(**ADD_GROUP_ARGS) - - attributes = conn.get_snapshot_attribute( - snapshot.id, attribute="createVolumePermission" - ) - attributes.attrs["groups"].should.have.length_of(1) - attributes.attrs["groups"].should.equal(["all"]) - - # Add is idempotent - conn.modify_snapshot_attribute.when.called_with(**ADD_GROUP_ARGS).should_not.throw( - EC2ResponseError - ) - - # Remove 'all' group and confirm - with pytest.raises(EC2ResponseError) as ex: - conn.modify_snapshot_attribute(**dict(REMOVE_GROUP_ARGS, **{"dry_run": True})) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the ModifySnapshotAttribute operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.modify_snapshot_attribute(**REMOVE_GROUP_ARGS) - - attributes = conn.get_snapshot_attribute( - snapshot.id, attribute="createVolumePermission" - ) - attributes.attrs.should.have.length_of(0) - - # Remove is idempotent - conn.modify_snapshot_attribute.when.called_with( - **REMOVE_GROUP_ARGS - ).should_not.throw(EC2ResponseError) - - # Error: Add with group != 'all' - with pytest.raises(EC2ResponseError) as cm: - conn.modify_snapshot_attribute( - snapshot.id, - attribute="createVolumePermission", - operation="add", - groups="everyone", - ) - cm.value.code.should.equal("InvalidAMIAttributeItemValue") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - # Error: Add with invalid snapshot ID - with pytest.raises(EC2ResponseError) as cm: - conn.modify_snapshot_attribute( - "snapshot-abcd1234", - attribute="createVolumePermission", - operation="add", - groups="all", - ) - cm.value.code.should.equal("InvalidSnapshot.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - # Error: Remove with invalid snapshot ID - with pytest.raises(EC2ResponseError) as cm: - conn.modify_snapshot_attribute( - "snapshot-abcd1234", - attribute="createVolumePermission", - operation="remove", - groups="all", - ) - cm.value.code.should.equal("InvalidSnapshot.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_modify_snapshot_attribute(): ec2_client = boto3.client("ec2", region_name="us-east-1") @@ -1215,46 +654,6 @@ def test_modify_snapshot_attribute(): assert len(attributes["CreateVolumePermissions"]) == 0 -# Has boto3 equivalent -@mock_ec2_deprecated -def test_create_volume_from_snapshot(): - conn = boto.ec2.connect_to_region("us-east-1") - volume = conn.create_volume(80, "us-east-1a") - snapshot = volume.create_snapshot("a test snapshot") - - with pytest.raises(EC2ResponseError) as ex: - snapshot = volume.create_snapshot("a test snapshot", dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the CreateSnapshot operation: Request would have succeeded, but DryRun flag is set" - ) - - snapshot = volume.create_snapshot("a test snapshot") - snapshot.update() - snapshot.status.should.equal("completed") - - new_volume = snapshot.create_volume("us-east-1a") - new_volume.size.should.equal(80) - new_volume.snapshot_id.should.equal(snapshot.id) - - -# Has boto3 equivalent -@mock_ec2_deprecated -def test_create_volume_from_encrypted_snapshot(): - conn = boto.ec2.connect_to_region("us-east-1") - volume = conn.create_volume(80, "us-east-1a", encrypted=True) - - snapshot = volume.create_snapshot("a test snapshot") - snapshot.update() - snapshot.status.should.equal("completed") - - new_volume = snapshot.create_volume("us-east-1a") - new_volume.size.should.equal(80) - new_volume.snapshot_id.should.equal(snapshot.id) - new_volume.encrypted.should.be(True) - - @mock_ec2 @pytest.mark.parametrize("encrypted", [True, False]) def test_create_volume_from_snapshot_boto3(encrypted): @@ -1275,38 +674,6 @@ def test_create_volume_from_snapshot_boto3(encrypted): new_volume["Encrypted"].should.equal(encrypted) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_modify_attribute_blockDeviceMapping(): - """ - Reproduces the missing feature explained at [0], where we want to mock a - call to modify an instance attribute of type: blockDeviceMapping. - - [0] https://github.com/spulec/moto/issues/160 - """ - conn = boto.ec2.connect_to_region("us-east-1") - - reservation = conn.run_instances(EXAMPLE_AMI_ID) - - instance = reservation.instances[0] - - with pytest.raises(EC2ResponseError) as ex: - instance.modify_attribute( - "blockDeviceMapping", {"/dev/sda1": True}, dry_run=True - ) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the ModifyInstanceAttribute operation: Request would have succeeded, but DryRun flag is set" - ) - - instance.modify_attribute("blockDeviceMapping", {"/dev/sda1": True}) - - instance = ec2_backends[conn.region.name].get_instance(instance.id) - instance.block_device_mapping.should.have.key("/dev/sda1") - instance.block_device_mapping["/dev/sda1"].delete_on_termination.should.be(True) - - @mock_ec2 def test_modify_attribute_blockDeviceMapping_boto3(): """ @@ -1346,29 +713,6 @@ def test_modify_attribute_blockDeviceMapping_boto3(): mapping["Ebs"]["DeleteOnTermination"].should.be(True) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_volume_tag_escaping(): - conn = boto.ec2.connect_to_region("us-east-1") - vol = conn.create_volume(10, "us-east-1a") - snapshot = conn.create_snapshot(vol.id, "Desc") - - with pytest.raises(EC2ResponseError) as ex: - snapshot.add_tags({"key": ""}, dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the CreateTags operation: Request would have succeeded, but DryRun flag is set" - ) - snaps = [snap for snap in conn.get_all_snapshots() if snap.id == snapshot.id] - dict(snaps[0].tags).should_not.be.equal({"key": ""}) - - snapshot.add_tags({"key": ""}) - - snaps = [snap for snap in conn.get_all_snapshots() if snap.id == snapshot.id] - dict(snaps[0].tags).should.equal({"key": ""}) - - @mock_ec2 def test_volume_tag_escaping_boto3(): client = boto3.client("ec2", region_name="us-east-1") diff --git a/tests/test_ec2/test_elastic_ip_addresses.py b/tests/test_ec2/test_elastic_ip_addresses.py index 364455f11..6aec0d6f4 100644 --- a/tests/test_ec2/test_elastic_ip_addresses.py +++ b/tests/test_ec2/test_elastic_ip_addresses.py @@ -1,51 +1,17 @@ import pytest -import boto import boto3 -from boto.exception import EC2ResponseError from botocore.exceptions import ClientError from uuid import uuid4 import sure # noqa # pylint: disable=unused-import -from moto import mock_ec2, mock_ec2_deprecated +from moto import mock_ec2 from tests import EXAMPLE_AMI_ID import logging -# Has boto3 equivalent -@mock_ec2_deprecated -def test_eip_allocate_classic(): - """Allocate/release Classic EIP""" - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as ex: - standard = conn.allocate_address(dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the AllocateAddress operation: Request would have succeeded, but DryRun flag is set" - ) - - standard = conn.allocate_address() - standard.should.be.a(boto.ec2.address.Address) - standard.public_ip.should.be.a(str) - standard.instance_id.should.be.none - standard.domain.should.be.equal("standard") - - with pytest.raises(EC2ResponseError) as ex: - standard.release(dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the ReleaseAddress operation: Request would have succeeded, but DryRun flag is set" - ) - - standard.release() - standard.should_not.be.within(conn.get_all_addresses()) - - @mock_ec2 def test_eip_allocate_classic_boto3(): """Allocate/release Classic EIP""" @@ -83,27 +49,6 @@ def test_eip_allocate_classic_boto3(): [a["PublicIp"] for a in all_addresses].shouldnt.contain(public_ip) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_eip_allocate_vpc(): - """Allocate/release VPC EIP""" - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as ex: - vpc = conn.allocate_address(domain="vpc", dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the AllocateAddress operation: Request would have succeeded, but DryRun flag is set" - ) - - vpc = conn.allocate_address(domain="vpc") - vpc.should.be.a(boto.ec2.address.Address) - vpc.domain.should.be.equal("vpc") - logging.debug("vpc alloc_id:".format(vpc.allocation_id)) - vpc.release() - - @mock_ec2 def test_describe_addresses_dryrun(): client = boto3.client("ec2", region_name="us-east-1") @@ -162,19 +107,6 @@ def test_specific_eip_allocate_vpc(): logging.debug("vpc alloc_id:".format(vpc["AllocationId"])) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_eip_allocate_invalid_domain(): - """Allocate EIP invalid domain""" - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as cm: - conn.allocate_address(domain="bogus") - cm.value.code.should.equal("InvalidParameterValue") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_eip_allocate_invalid_domain_boto3(): """Allocate EIP invalid domain""" @@ -187,58 +119,6 @@ def test_eip_allocate_invalid_domain_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidParameterValue") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_eip_associate_classic(): - """Associate/Disassociate EIP to classic instance""" - conn = boto.connect_ec2("the_key", "the_secret") - - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - eip = conn.allocate_address() - eip.instance_id.should.be.none - - with pytest.raises(EC2ResponseError) as cm: - conn.associate_address(public_ip=eip.public_ip) - cm.value.code.should.equal("MissingParameter") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - with pytest.raises(EC2ResponseError) as ex: - conn.associate_address( - instance_id=instance.id, public_ip=eip.public_ip, dry_run=True - ) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the AssociateAddress operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.associate_address(instance_id=instance.id, public_ip=eip.public_ip) - # no .update() on address ): - eip = conn.get_all_addresses(addresses=[eip.public_ip])[0] - eip.instance_id.should.be.equal(instance.id) - - with pytest.raises(EC2ResponseError) as ex: - conn.disassociate_address(public_ip=eip.public_ip, dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the DisAssociateAddress operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.disassociate_address(public_ip=eip.public_ip) - # no .update() on address ): - eip = conn.get_all_addresses(addresses=[eip.public_ip])[0] - eip.instance_id.should.be.equal("") - eip.release() - eip.should_not.be.within(conn.get_all_addresses()) - eip = None - - instance.terminate() - - @mock_ec2 def test_eip_associate_classic_boto3(): """Associate/Disassociate EIP to classic instance""" @@ -297,48 +177,6 @@ def test_eip_associate_classic_boto3(): instance.terminate() -# Has boto3 equivalent -@mock_ec2_deprecated -def test_eip_associate_vpc(): - """Associate/Disassociate EIP to VPC instance""" - conn = boto.connect_ec2("the_key", "the_secret") - - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - eip = conn.allocate_address(domain="vpc") - eip.instance_id.should.be.none - - with pytest.raises(EC2ResponseError) as cm: - conn.associate_address(allocation_id=eip.allocation_id) - cm.value.code.should.equal("MissingParameter") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - conn.associate_address(instance_id=instance.id, allocation_id=eip.allocation_id) - # no .update() on address ): - eip = conn.get_all_addresses(addresses=[eip.public_ip])[0] - eip.instance_id.should.be.equal(instance.id) - conn.disassociate_address(association_id=eip.association_id) - # no .update() on address ): - eip = conn.get_all_addresses(addresses=[eip.public_ip])[0] - eip.instance_id.should.be.equal("") - eip.association_id.should.be.none - - with pytest.raises(EC2ResponseError) as ex: - eip.release(dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the ReleaseAddress operation: Request would have succeeded, but DryRun flag is set" - ) - - eip.release() - eip = None - - instance.terminate() - - @mock_ec2 def test_eip_associate_vpc_boto3(): """Associate/Disassociate EIP to VPC instance""" @@ -430,38 +268,6 @@ def test_eip_boto3_vpc_association(): address.instance_id.should.be.empty -# Has boto3 equivalent -@mock_ec2_deprecated -def test_eip_associate_network_interface(): - """Associate/Disassociate EIP to NIC""" - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") - eni = conn.create_network_interface(subnet.id) - - eip = conn.allocate_address(domain="vpc") - eip.network_interface_id.should.be.none - - with pytest.raises(EC2ResponseError) as cm: - conn.associate_address(network_interface_id=eni.id) - cm.value.code.should.equal("MissingParameter") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - conn.associate_address(network_interface_id=eni.id, allocation_id=eip.allocation_id) - # no .update() on address ): - eip = conn.get_all_addresses(addresses=[eip.public_ip])[0] - eip.network_interface_id.should.be.equal(eni.id) - - conn.disassociate_address(association_id=eip.association_id) - # no .update() on address ): - eip = conn.get_all_addresses(addresses=[eip.public_ip])[0] - eip.network_interface_id.should.be.equal("") - eip.association_id.should.be.none - eip.release() - eip = None - - @mock_ec2 def test_eip_associate_network_interface_boto3(): """Associate/Disassociate EIP to NIC""" @@ -497,40 +303,6 @@ def test_eip_associate_network_interface_boto3(): eip.release() -# Has boto3 equivalent -@mock_ec2_deprecated -def test_eip_reassociate(): - """reassociate EIP""" - conn = boto.connect_ec2("the_key", "the_secret") - - reservation = conn.run_instances(EXAMPLE_AMI_ID, min_count=2) - instance1, instance2 = reservation.instances - - eip = conn.allocate_address() - conn.associate_address(instance_id=instance1.id, public_ip=eip.public_ip) - - # Same ID is idempotent - conn.associate_address(instance_id=instance1.id, public_ip=eip.public_ip) - - # Different ID detects resource association - with pytest.raises(EC2ResponseError) as cm: - conn.associate_address( - instance_id=instance2.id, public_ip=eip.public_ip, allow_reassociation=False - ) - cm.value.code.should.equal("Resource.AlreadyAssociated") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - conn.associate_address.when.called_with( - instance_id=instance2.id, public_ip=eip.public_ip, allow_reassociation=True - ).should_not.throw(EC2ResponseError) - - eip.release() - - instance1.terminate() - instance2.terminate() - - @mock_ec2 def test_eip_reassociate_boto3(): """reassociate EIP""" @@ -572,38 +344,6 @@ def test_eip_reassociate_boto3(): instance2.terminate() -# Has boto3 equivalent -@mock_ec2_deprecated -def test_eip_reassociate_nic(): - """reassociate EIP""" - conn = boto.connect_vpc("the_key", "the_secret") - - vpc = conn.create_vpc("10.0.0.0/16") - subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") - eni1 = conn.create_network_interface(subnet.id) - eni2 = conn.create_network_interface(subnet.id) - - eip = conn.allocate_address() - conn.associate_address(network_interface_id=eni1.id, public_ip=eip.public_ip) - - # Same ID is idempotent - conn.associate_address(network_interface_id=eni1.id, public_ip=eip.public_ip) - - # Different ID detects resource association - with pytest.raises(EC2ResponseError) as cm: - conn.associate_address(network_interface_id=eni2.id, public_ip=eip.public_ip) - cm.value.code.should.equal("Resource.AlreadyAssociated") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - conn.associate_address.when.called_with( - network_interface_id=eni2.id, public_ip=eip.public_ip, allow_reassociation=True - ).should_not.throw(EC2ResponseError) - - eip.release() - eip = None - - @mock_ec2 def test_eip_reassociate_nic_boto3(): """reassociate EIP""" @@ -641,26 +381,6 @@ def test_eip_reassociate_nic_boto3(): eip.release() -# Has boto3 equivalent -@mock_ec2_deprecated -def test_eip_associate_invalid_args(): - """Associate EIP, invalid args""" - conn = boto.connect_ec2("the_key", "the_secret") - - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - conn.allocate_address() - - with pytest.raises(EC2ResponseError) as cm: - conn.associate_address(instance_id=instance.id) - cm.value.code.should.equal("MissingParameter") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - instance.terminate() - - @mock_ec2 def test_eip_associate_invalid_args_boto3(): """Associate EIP, invalid args """ @@ -681,19 +401,6 @@ def test_eip_associate_invalid_args_boto3(): instance.terminate() -# Has boto3 equivalent -@mock_ec2_deprecated -def test_eip_disassociate_bogus_association(): - """Disassociate bogus EIP""" - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as cm: - conn.disassociate_address(association_id="bogus") - cm.value.code.should.equal("InvalidAssociationID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_eip_disassociate_bogus_association_boto3(): """Disassociate bogus EIP""" @@ -706,19 +413,6 @@ def test_eip_disassociate_bogus_association_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidAssociationID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_eip_release_bogus_eip(): - """Release bogus EIP""" - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as cm: - conn.release_address(allocation_id="bogus") - cm.value.code.should.equal("InvalidAllocationID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_eip_release_bogus_eip_boto3(): """Release bogus EIP""" @@ -731,19 +425,6 @@ def test_eip_release_bogus_eip_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidAllocationID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_eip_disassociate_arg_error(): - """Invalid arguments disassociate address""" - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as cm: - conn.disassociate_address() - cm.value.code.should.equal("MissingParameter") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_eip_disassociate_arg_error_boto3(): """Invalid arguments disassociate address""" @@ -756,19 +437,6 @@ def test_eip_disassociate_arg_error_boto3(): ex.value.response["Error"]["Code"].should.equal("MissingParameter") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_eip_release_arg_error(): - """Invalid arguments release address""" - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as cm: - conn.release_address() - cm.value.code.should.equal("MissingParameter") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_eip_release_arg_error_boto3(): """Invalid arguments release address""" @@ -781,47 +449,6 @@ def test_eip_release_arg_error_boto3(): ex.value.response["Error"]["Code"].should.equal("MissingParameter") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_eip_describe(): - """Listing of allocated Elastic IP Addresses.""" - conn = boto.connect_ec2("the_key", "the_secret") - eips = [] - number_of_classic_ips = 2 - number_of_vpc_ips = 2 - - # allocate some IPs - for _ in range(number_of_classic_ips): - eips.append(conn.allocate_address()) - for _ in range(number_of_vpc_ips): - eips.append(conn.allocate_address(domain="vpc")) - len(eips).should.be.equal(number_of_classic_ips + number_of_vpc_ips) - - # Can we find each one individually? - for eip in eips: - if eip.allocation_id: - lookup_addresses = conn.get_all_addresses( - allocation_ids=[eip.allocation_id] - ) - else: - lookup_addresses = conn.get_all_addresses(addresses=[eip.public_ip]) - len(lookup_addresses).should.be.equal(1) - lookup_addresses[0].public_ip.should.be.equal(eip.public_ip) - - # Can we find first two when we search for them? - lookup_addresses = conn.get_all_addresses( - addresses=[eips[0].public_ip, eips[1].public_ip] - ) - len(lookup_addresses).should.be.equal(2) - lookup_addresses[0].public_ip.should.be.equal(eips[0].public_ip) - lookup_addresses[1].public_ip.should.be.equal(eips[1].public_ip) - - # Release all IPs - for eip in eips: - eip.release() - len(conn.get_all_addresses()).should.be.equal(0) - - @mock_ec2 def test_eip_describe_boto3(): """Listing of allocated Elastic IP Addresses.""" @@ -868,19 +495,6 @@ def test_eip_describe_boto3(): [a["PublicIp"] for a in all_addresses].shouldnt.contain(eips[1].public_ip) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_eip_describe_none(): - """Error when search for bogus IP""" - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as cm: - conn.get_all_addresses(addresses=["256.256.256.256"]) - cm.value.code.should.equal("InvalidAddress.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_eip_describe_none_boto3(): """Error when search for bogus IP""" diff --git a/tests/test_ec2/test_elastic_network_interfaces.py b/tests/test_ec2/test_elastic_network_interfaces.py index 0eebe17c2..236bad670 100644 --- a/tests/test_ec2/test_elastic_network_interfaces.py +++ b/tests/test_ec2/test_elastic_network_interfaces.py @@ -3,62 +3,14 @@ import random import boto3 from botocore.exceptions import ClientError -import boto -import boto.ec2 -from boto.exception import EC2ResponseError import sure # noqa # pylint: disable=unused-import -from moto import mock_ec2, mock_ec2_deprecated, settings +from moto import mock_ec2, settings from moto.ec2.utils import random_private_ip from tests import EXAMPLE_AMI_ID -from tests.helpers import requires_boto_gte from uuid import uuid4 -# Has boto3 equivalent -@mock_ec2_deprecated -def test_elastic_network_interfaces(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") - - with pytest.raises(EC2ResponseError) as ex: - eni = conn.create_network_interface(subnet.id, dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the CreateNetworkInterface operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.create_network_interface(subnet.id) - - all_enis = conn.get_all_network_interfaces() - all_enis.should.have.length_of(1) - eni = all_enis[0] - eni.groups.should.have.length_of(1) - eni.private_ip_addresses.should.have.length_of(1) - eni.private_ip_addresses[0].private_ip_address.startswith("10.").should.be.true - - with pytest.raises(EC2ResponseError) as ex: - conn.delete_network_interface(eni.id, dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the DeleteNetworkInterface operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.delete_network_interface(eni.id) - - all_enis = conn.get_all_network_interfaces() - all_enis.should.have.length_of(0) - - with pytest.raises(EC2ResponseError) as cm: - conn.delete_network_interface(eni.id) - cm.value.error_code.should.equal("InvalidNetworkInterfaceID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_elastic_network_interfaces_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -116,18 +68,6 @@ def test_elastic_network_interfaces_boto3(): ) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_elastic_network_interfaces_subnet_validation(): - conn = boto.connect_vpc("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as cm: - conn.create_network_interface("subnet-abcd1234") - cm.value.error_code.should.equal("InvalidSubnetID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_elastic_network_interfaces_subnet_validation_boto3(): client = boto3.client("ec2", "us-east-1") @@ -139,25 +79,6 @@ def test_elastic_network_interfaces_subnet_validation_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidSubnetID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_elastic_network_interfaces_with_private_ip(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") - private_ip = "54.0.0.1" - eni = conn.create_network_interface(subnet.id, private_ip) - - all_enis = conn.get_all_network_interfaces() - all_enis.should.have.length_of(1) - - eni = all_enis[0] - eni.groups.should.have.length_of(1) - - eni.private_ip_addresses.should.have.length_of(1) - eni.private_ip_addresses[0].private_ip_address.should.equal(private_ip) - - @mock_ec2 def test_elastic_network_interfaces_with_private_ip_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -183,32 +104,6 @@ def test_elastic_network_interfaces_with_private_ip_boto3(): eni["PrivateIpAddresses"][0]["PrivateIpAddress"].should.equal(private_ip) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_elastic_network_interfaces_with_groups(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") - security_group1 = conn.create_security_group( - "test security group #1", "this is a test security group" - ) - security_group2 = conn.create_security_group( - "test security group #2", "this is a test security group" - ) - conn.create_network_interface( - subnet.id, groups=[security_group1.id, security_group2.id] - ) - - all_enis = conn.get_all_network_interfaces() - all_enis.should.have.length_of(1) - - eni = all_enis[0] - eni.groups.should.have.length_of(2) - set([group.id for group in eni.groups]).should.equal( - set([security_group1.id, security_group2.id]) - ) - - @mock_ec2 def test_elastic_network_interfaces_with_groups_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -250,51 +145,6 @@ def test_elastic_network_interfaces_without_group(): my_eni["Groups"][0]["GroupName"].should.equal("default") -# Has boto3 equivalent -@requires_boto_gte("2.12.0") -@mock_ec2_deprecated -def test_elastic_network_interfaces_modify_attribute(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") - security_group1 = conn.create_security_group( - "test security group #1", "this is a test security group" - ) - security_group2 = conn.create_security_group( - "test security group #2", "this is a test security group" - ) - conn.create_network_interface(subnet.id, groups=[security_group1.id]) - - all_enis = conn.get_all_network_interfaces() - all_enis.should.have.length_of(1) - - eni = all_enis[0] - eni.groups.should.have.length_of(1) - eni.groups[0].id.should.equal(security_group1.id) - - with pytest.raises(EC2ResponseError) as ex: - conn.modify_network_interface_attribute( - eni.id, "groupset", [security_group1.id, security_group2.id], dry_run=True - ) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the ModifyNetworkInterface operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.modify_network_interface_attribute( - eni.id, "groupset", [security_group1.id, security_group2.id] - ) - - all_enis = conn.get_all_network_interfaces() - all_enis.should.have.length_of(1) - - eni = all_enis[0] - eni.groups.should.have.length_of(2) - eni.groups[0].id.should.equal(security_group1.id) - eni.groups[1].id.should.equal(security_group2.id) - - @mock_ec2 def test_elastic_network_interfaces_modify_attribute_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -334,68 +184,6 @@ def test_elastic_network_interfaces_modify_attribute_boto3(): my_eni["Groups"][0]["GroupId"].should.equal(sec_group2.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_elastic_network_interfaces_filtering(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") - - security_group1 = conn.create_security_group( - "test security group #1", "this is a test security group" - ) - security_group2 = conn.create_security_group( - "test security group #2", "this is a test security group" - ) - - eni1 = conn.create_network_interface( - subnet.id, groups=[security_group1.id, security_group2.id] - ) - eni2 = conn.create_network_interface(subnet.id, groups=[security_group1.id]) - eni3 = conn.create_network_interface(subnet.id, description="test description") - - all_enis = conn.get_all_network_interfaces() - all_enis.should.have.length_of(3) - - # Filter by NetworkInterfaceId - enis_by_id = conn.get_all_network_interfaces([eni1.id]) - enis_by_id.should.have.length_of(1) - set([eni.id for eni in enis_by_id]).should.equal(set([eni1.id])) - - # Filter by ENI ID - enis_by_id = conn.get_all_network_interfaces( - filters={"network-interface-id": eni1.id} - ) - enis_by_id.should.have.length_of(1) - set([eni.id for eni in enis_by_id]).should.equal(set([eni1.id])) - - # Filter by Security Group - enis_by_group = conn.get_all_network_interfaces( - filters={"group-id": security_group1.id} - ) - enis_by_group.should.have.length_of(2) - set([eni.id for eni in enis_by_group]).should.equal(set([eni1.id, eni2.id])) - - # Filter by ENI ID and Security Group - enis_by_group = conn.get_all_network_interfaces( - filters={"network-interface-id": eni1.id, "group-id": security_group1.id} - ) - enis_by_group.should.have.length_of(1) - set([eni.id for eni in enis_by_group]).should.equal(set([eni1.id])) - - # Filter by Description - enis_by_description = conn.get_all_network_interfaces( - filters={"description": eni3.description} - ) - enis_by_description.should.have.length_of(1) - enis_by_description[0].description.should.equal(eni3.description) - - # Unsupported filter - conn.get_all_network_interfaces.when.called_with( - filters={"not-implemented-filter": "foobar"} - ).should.throw(NotImplementedError) - - @mock_ec2 def test_elastic_network_interfaces_filtering_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") diff --git a/tests/test_ec2/test_general.py b/tests/test_ec2/test_general.py index 4a216d6fb..3b5868aea 100644 --- a/tests/test_ec2/test_general.py +++ b/tests/test_ec2/test_general.py @@ -1,37 +1,13 @@ import pytest -import boto import boto3 -from boto.exception import EC2ResponseError import sure # noqa # pylint: disable=unused-import from botocore.exceptions import ClientError -from moto import mock_ec2_deprecated, mock_ec2 +from moto import mock_ec2 from tests import EXAMPLE_AMI_ID -# Has boto3 equivalent -@mock_ec2_deprecated -def test_console_output(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance_id = reservation.instances[0].id - output = conn.get_console_output(instance_id) - output.output.should_not.equal(None) - - -# Has boto3 equivalent -@mock_ec2_deprecated -def test_console_output_without_instance(): - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as cm: - conn.get_console_output("i-1234abcd") - cm.value.error_code.should.equal("InvalidInstanceID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_console_output_boto3(): conn = boto3.resource("ec2", "us-east-1") diff --git a/tests/test_ec2/test_instances.py b/tests/test_ec2/test_instances.py index 4ed659be6..b108c4731 100644 --- a/tests/test_ec2/test_instances.py +++ b/tests/test_ec2/test_instances.py @@ -6,40 +6,18 @@ from unittest import SkipTest import base64 import ipaddress -import boto import boto3 -from boto.ec2.instance import Reservation, InstanceAttribute -from boto.exception import EC2ResponseError from freezegun import freeze_time import sure # noqa # pylint: disable=unused-import -from moto import mock_ec2_deprecated, mock_ec2, settings +from moto import mock_ec2, settings from moto.core import ACCOUNT_ID from tests import EXAMPLE_AMI_ID -from tests.helpers import requires_boto_gte from uuid import uuid4 decode_method = base64.decodebytes -################ Test Readme ############### -def add_servers(ami_id, count): - conn = boto.connect_ec2() - for _ in range(count): - conn.run_instances(ami_id) - - -# Has boto3 equivalent -@mock_ec2_deprecated -def test_add_servers(): - add_servers(EXAMPLE_AMI_ID, 2) - - conn = boto.connect_ec2() - reservations = conn.get_all_reservations() - assert len(reservations) == 2 - instance1 = reservations[0].instances[0] - assert instance1.image_id == EXAMPLE_AMI_ID - @mock_ec2 def test_add_servers_boto3(): @@ -56,65 +34,6 @@ def test_add_servers_boto3(): i["ImageId"].should.equal(EXAMPLE_AMI_ID) -############################################ - - -# Has boto3 equivalent -@freeze_time("2014-01-01 05:00:00") -@mock_ec2_deprecated -def test_instance_launch_and_terminate(): - conn = boto.ec2.connect_to_region("us-east-1") - - with pytest.raises(EC2ResponseError) as ex: - reservation = conn.run_instances(EXAMPLE_AMI_ID, dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the RunInstance operation: Request would have succeeded, but DryRun flag is set" - ) - - reservation = conn.run_instances(EXAMPLE_AMI_ID) - reservation.should.be.a(Reservation) - reservation.instances.should.have.length_of(1) - instance = reservation.instances[0] - instance.state.should.equal("pending") - - reservations = conn.get_all_reservations() - reservations.should.have.length_of(1) - reservations[0].id.should.equal(reservation.id) - instances = reservations[0].instances - instances.should.have.length_of(1) - instance = instances[0] - instance.id.should.equal(instance.id) - instance.state.should.equal("running") - instance.launch_time.should.equal("2014-01-01T05:00:00.000Z") - instance.vpc_id.shouldnt.equal(None) - instance.placement.should.equal("us-east-1a") - - root_device_name = instance.root_device_name - instance.block_device_mapping[root_device_name].status.should.equal("in-use") - volume_id = instance.block_device_mapping[root_device_name].volume_id - volume_id.should.match(r"vol-\w+") - - volume = conn.get_all_volumes(volume_ids=[volume_id])[0] - volume.attach_data.instance_id.should.equal(instance.id) - volume.status.should.equal("in-use") - - with pytest.raises(EC2ResponseError) as ex: - conn.terminate_instances([instance.id], dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the TerminateInstance operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.terminate_instances([instance.id]) - - reservations = conn.get_all_reservations() - instance = reservations[0].instances[0] - instance.state.should.equal("terminated") - - @freeze_time("2014-01-01 05:00:00") @mock_ec2 def test_instance_launch_and_terminate_boto3(): @@ -313,13 +232,6 @@ def test_instance_detach_volume_wrong_path(): ) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_terminate_empty_instances(): - conn = boto.connect_ec2("the_key", "the_secret") - conn.terminate_instances.when.called_with([]).should.throw(EC2ResponseError) - - @mock_ec2 def test_terminate_empty_instances_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -331,41 +243,6 @@ def test_terminate_empty_instances_boto3(): ex.value.response["Error"]["Message"].should.equal("No instances specified") -# Has boto3 equivalent -@freeze_time("2014-01-01 05:00:00") -@mock_ec2_deprecated -def test_instance_attach_volume(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - vol1 = conn.create_volume(size=36, zone=conn.region.name) - vol1.attach(instance.id, "/dev/sda1") - vol1.update() - vol2 = conn.create_volume(size=65, zone=conn.region.name) - vol2.attach(instance.id, "/dev/sdb1") - vol2.update() - vol3 = conn.create_volume(size=130, zone=conn.region.name) - vol3.attach(instance.id, "/dev/sdc1") - vol3.update() - - reservations = conn.get_all_reservations() - instance = reservations[0].instances[0] - - instance.block_device_mapping.should.have.length_of(3) - - for v in conn.get_all_volumes( - volume_ids=[instance.block_device_mapping["/dev/sdc1"].volume_id] - ): - v.attach_data.instance_id.should.equal(instance.id) - # can do due to freeze_time decorator. - v.attach_data.attach_time.should.equal(instance.launch_time) - # can do due to freeze_time decorator. - v.create_time.should.equal(instance.launch_time) - v.region.name.should.equal(instance.region.name) - v.status.should.equal("in-use") - - @freeze_time("2014-01-01 05:00:00") @mock_ec2 def test_instance_attach_volume_boto3(): @@ -400,34 +277,6 @@ def test_instance_attach_volume_boto3(): expected_vol3.create_time.should.equal(instance.launch_time) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_instances_by_id(): - conn = boto.connect_ec2() - reservation = conn.run_instances(EXAMPLE_AMI_ID, min_count=2) - instance1, instance2 = reservation.instances - - reservations = conn.get_all_reservations(instance_ids=[instance1.id]) - reservations.should.have.length_of(1) - reservation = reservations[0] - reservation.instances.should.have.length_of(1) - reservation.instances[0].id.should.equal(instance1.id) - - reservations = conn.get_all_reservations(instance_ids=[instance1.id, instance2.id]) - reservations.should.have.length_of(1) - reservation = reservations[0] - reservation.instances.should.have.length_of(2) - instance_ids = [instance.id for instance in reservation.instances] - instance_ids.should.equal([instance1.id, instance2.id]) - - # Call get_all_reservations with a bad id should raise an error - with pytest.raises(EC2ResponseError) as cm: - conn.get_all_reservations(instance_ids=[instance1.id, "i-1234abcd"]) - cm.value.code.should.equal("InvalidInstanceID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_get_instances_by_id_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -540,43 +389,6 @@ def test_create_with_volume_tags(): sorted(volume["Tags"], key=lambda i: i["Key"]).should.equal(volume_tags) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_instances_filtering_by_state(): - conn = boto.connect_ec2() - reservation = conn.run_instances(EXAMPLE_AMI_ID, min_count=3) - instance1, instance2, instance3 = reservation.instances - - conn.terminate_instances([instance1.id]) - - reservations = conn.get_all_reservations(filters={"instance-state-name": "running"}) - reservations.should.have.length_of(1) - # Since we terminated instance1, only instance2 and instance3 should be - # returned - instance_ids = [instance.id for instance in reservations[0].instances] - set(instance_ids).should.equal(set([instance2.id, instance3.id])) - - reservations = conn.get_all_reservations( - [instance2.id], filters={"instance-state-name": "running"} - ) - reservations.should.have.length_of(1) - instance_ids = [instance.id for instance in reservations[0].instances] - instance_ids.should.equal([instance2.id]) - - reservations = conn.get_all_reservations( - [instance2.id], filters={"instance-state-name": "terminated"} - ) - list(reservations).should.equal([]) - - # get_all_reservations should still return all 3 - reservations = conn.get_all_reservations() - reservations[0].instances.should.have.length_of(3) - - conn.get_all_reservations.when.called_with( - filters={"not-implemented-filter": "foobar"} - ).should.throw(NotImplementedError) - - @mock_ec2 def test_get_instances_filtering_by_state_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -625,28 +437,6 @@ def test_get_instances_filtering_by_state_boto3(): ) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_instances_filtering_by_instance_id(): - conn = boto.connect_ec2() - reservation = conn.run_instances(EXAMPLE_AMI_ID, min_count=3) - instance1, instance2, _ = reservation.instances - - reservations = conn.get_all_reservations(filters={"instance-id": instance1.id}) - # get_all_reservations should return just instance1 - reservations[0].instances.should.have.length_of(1) - reservations[0].instances[0].id.should.equal(instance1.id) - - reservations = conn.get_all_reservations( - filters={"instance-id": [instance1.id, instance2.id]} - ) - # get_all_reservations should return two - reservations[0].instances.should.have.length_of(2) - - reservations = conn.get_all_reservations(filters={"instance-id": "non-existing-id"}) - reservations.should.have.length_of(0) - - @mock_ec2 def test_get_instances_filtering_by_instance_id_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -669,50 +459,6 @@ def test_get_instances_filtering_by_instance_id_boto3(): _filter(values=["non-existing-id"], exists=False) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_instances_filtering_by_instance_type(): - conn = boto.connect_ec2() - reservation1 = conn.run_instances(EXAMPLE_AMI_ID, instance_type="m1.small") - instance1 = reservation1.instances[0] - reservation2 = conn.run_instances(EXAMPLE_AMI_ID, instance_type="m1.small") - instance2 = reservation2.instances[0] - reservation3 = conn.run_instances(EXAMPLE_AMI_ID, instance_type="t1.micro") - instance3 = reservation3.instances[0] - - reservations = conn.get_all_reservations(filters={"instance-type": "m1.small"}) - # get_all_reservations should return instance1,2 - reservations.should.have.length_of(2) - reservations[0].instances.should.have.length_of(1) - reservations[1].instances.should.have.length_of(1) - instance_ids = [reservations[0].instances[0].id, reservations[1].instances[0].id] - set(instance_ids).should.equal(set([instance1.id, instance2.id])) - - reservations = conn.get_all_reservations(filters={"instance-type": "t1.micro"}) - # get_all_reservations should return one - reservations.should.have.length_of(1) - reservations[0].instances.should.have.length_of(1) - reservations[0].instances[0].id.should.equal(instance3.id) - - reservations = conn.get_all_reservations( - filters={"instance-type": ["t1.micro", "m1.small"]} - ) - reservations.should.have.length_of(3) - reservations[0].instances.should.have.length_of(1) - reservations[1].instances.should.have.length_of(1) - reservations[2].instances.should.have.length_of(1) - instance_ids = [ - reservations[0].instances[0].id, - reservations[1].instances[0].id, - reservations[2].instances[0].id, - ] - set(instance_ids).should.equal(set([instance1.id, instance2.id, instance3.id])) - - reservations = conn.get_all_reservations(filters={"instance-type": "bogus"}) - # bogus instance-type should return none - reservations.should.have.length_of(0) - - @mock_ec2 def test_get_instances_filtering_by_instance_type_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -754,30 +500,6 @@ def test_get_instances_filtering_by_instance_type_boto3(): res["Reservations"].should.have.length_of(0) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_instances_filtering_by_reason_code(): - conn = boto.connect_ec2() - reservation = conn.run_instances(EXAMPLE_AMI_ID, min_count=3) - instance1, instance2, instance3 = reservation.instances - instance1.stop() - instance2.terminate() - - reservations = conn.get_all_reservations( - filters={"state-reason-code": "Client.UserInitiatedShutdown"} - ) - # get_all_reservations should return instance1 and instance2 - reservations[0].instances.should.have.length_of(2) - set([instance1.id, instance2.id]).should.equal( - set([i.id for i in reservations[0].instances]) - ) - - reservations = conn.get_all_reservations(filters={"state-reason-code": ""}) - # get_all_reservations should return instance 3 - reservations[0].instances.should.have.length_of(1) - reservations[0].instances[0].id.should.equal(instance3.id) - - @mock_ec2 def test_get_instances_filtering_by_reason_code_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -805,30 +527,6 @@ def test_get_instances_filtering_by_reason_code_boto3(): instance_ids.shouldnt.contain(instance2.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_instances_filtering_by_source_dest_check(): - conn = boto.connect_ec2() - reservation = conn.run_instances(EXAMPLE_AMI_ID, min_count=2) - instance1, instance2 = reservation.instances - conn.modify_instance_attribute( - instance1.id, attribute="sourceDestCheck", value=False - ) - - source_dest_check_false = conn.get_all_reservations( - filters={"source-dest-check": "false"} - ) - source_dest_check_true = conn.get_all_reservations( - filters={"source-dest-check": "true"} - ) - - source_dest_check_false[0].instances.should.have.length_of(1) - source_dest_check_false[0].instances[0].id.should.equal(instance1.id) - - source_dest_check_true[0].instances.should.have.length_of(1) - source_dest_check_true[0].instances[0].id.should.equal(instance2.id) - - @mock_ec2 def test_get_instances_filtering_by_source_dest_check_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -853,35 +551,6 @@ def test_get_instances_filtering_by_source_dest_check_boto3(): [i["InstanceId"] for i in instances_true].should.contain(instance2.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_instances_filtering_by_vpc_id(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc1 = conn.create_vpc("10.0.0.0/16") - subnet1 = conn.create_subnet(vpc1.id, "10.0.0.0/27") - reservation1 = conn.run_instances(EXAMPLE_AMI_ID, min_count=1, subnet_id=subnet1.id) - instance1 = reservation1.instances[0] - - vpc2 = conn.create_vpc("10.1.0.0/16") - subnet2 = conn.create_subnet(vpc2.id, "10.1.0.0/27") - reservation2 = conn.run_instances(EXAMPLE_AMI_ID, min_count=1, subnet_id=subnet2.id) - instance2 = reservation2.instances[0] - - reservations1 = conn.get_all_reservations(filters={"vpc-id": vpc1.id}) - reservations1.should.have.length_of(1) - reservations1[0].instances.should.have.length_of(1) - reservations1[0].instances[0].id.should.equal(instance1.id) - reservations1[0].instances[0].vpc_id.should.equal(vpc1.id) - reservations1[0].instances[0].subnet_id.should.equal(subnet1.id) - - reservations2 = conn.get_all_reservations(filters={"vpc-id": vpc2.id}) - reservations2.should.have.length_of(1) - reservations2[0].instances.should.have.length_of(1) - reservations2[0].instances[0].id.should.equal(instance2.id) - reservations2[0].instances[0].vpc_id.should.equal(vpc2.id) - reservations2[0].instances[0].subnet_id.should.equal(subnet2.id) - - @mock_ec2 def test_get_instances_filtering_by_vpc_id_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -953,17 +622,6 @@ def test_get_instances_filtering_by_dns_name(): res2[0]["Instances"][0]["InstanceId"].should.equal(instance2.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_instances_filtering_by_architecture(): - conn = boto.connect_ec2() - conn.run_instances(EXAMPLE_AMI_ID, min_count=1) - - reservations = conn.get_all_reservations(filters={"architecture": "x86_64"}) - # get_all_reservations should return the instance - reservations[0].instances.should.have.length_of(1) - - @mock_ec2 def test_get_instances_filtering_by_architecture_boto3(): ec2 = boto3.resource("ec2", region_name="us-west-1") @@ -1087,54 +745,6 @@ def test_get_instances_filtering_by_subnet_id(): reservations.should.have.length_of(1) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_instances_filtering_by_tag(): - conn = boto.connect_ec2() - reservation = conn.run_instances(EXAMPLE_AMI_ID, min_count=3) - instance1, instance2, instance3 = reservation.instances - instance1.add_tag("tag1", "value1") - instance1.add_tag("tag2", "value2") - instance2.add_tag("tag1", "value1") - instance2.add_tag("tag2", "wrong value") - instance3.add_tag("tag2", "value2") - - reservations = conn.get_all_reservations(filters={"tag:tag0": "value0"}) - # get_all_reservations should return no instances - reservations.should.have.length_of(0) - - reservations = conn.get_all_reservations(filters={"tag:tag1": "value1"}) - # get_all_reservations should return both instances with this tag value - reservations.should.have.length_of(1) - reservations[0].instances.should.have.length_of(2) - reservations[0].instances[0].id.should.equal(instance1.id) - reservations[0].instances[1].id.should.equal(instance2.id) - - reservations = conn.get_all_reservations( - filters={"tag:tag1": "value1", "tag:tag2": "value2"} - ) - # get_all_reservations should return the instance with both tag values - reservations.should.have.length_of(1) - reservations[0].instances.should.have.length_of(1) - reservations[0].instances[0].id.should.equal(instance1.id) - - reservations = conn.get_all_reservations( - filters={"tag:tag1": "value1", "tag:tag2": "value2"} - ) - # get_all_reservations should return the instance with both tag values - reservations.should.have.length_of(1) - reservations[0].instances.should.have.length_of(1) - reservations[0].instances[0].id.should.equal(instance1.id) - - reservations = conn.get_all_reservations(filters={"tag:tag2": ["value2", "bogus"]}) - # get_all_reservations should return both instances with one of the - # acceptable tag values - reservations.should.have.length_of(1) - reservations[0].instances.should.have.length_of(2) - reservations[0].instances[0].id.should.equal(instance1.id) - reservations[0].instances[1].id.should.equal(instance3.id) - - @mock_ec2 def test_get_instances_filtering_by_tag_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -1190,49 +800,6 @@ def test_get_instances_filtering_by_tag_boto3(): res["Reservations"][0]["Instances"][1]["InstanceId"].should.equal(instance3.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_instances_filtering_by_tag_value(): - conn = boto.connect_ec2() - reservation = conn.run_instances(EXAMPLE_AMI_ID, min_count=3) - instance1, instance2, instance3 = reservation.instances - instance1.add_tag("tag1", "value1") - instance1.add_tag("tag2", "value2") - instance2.add_tag("tag1", "value1") - instance2.add_tag("tag2", "wrong value") - instance3.add_tag("tag2", "value2") - - reservations = conn.get_all_reservations(filters={"tag-value": "value0"}) - # get_all_reservations should return no instances - reservations.should.have.length_of(0) - - reservations = conn.get_all_reservations(filters={"tag-value": "value1"}) - # get_all_reservations should return both instances with this tag value - reservations.should.have.length_of(1) - reservations[0].instances.should.have.length_of(2) - reservations[0].instances[0].id.should.equal(instance1.id) - reservations[0].instances[1].id.should.equal(instance2.id) - - reservations = conn.get_all_reservations( - filters={"tag-value": ["value2", "value1"]} - ) - # get_all_reservations should return both instances with one of the - # acceptable tag values - reservations.should.have.length_of(1) - reservations[0].instances.should.have.length_of(3) - reservations[0].instances[0].id.should.equal(instance1.id) - reservations[0].instances[1].id.should.equal(instance2.id) - reservations[0].instances[2].id.should.equal(instance3.id) - - reservations = conn.get_all_reservations(filters={"tag-value": ["value2", "bogus"]}) - # get_all_reservations should return both instances with one of the - # acceptable tag values - reservations.should.have.length_of(1) - reservations[0].instances.should.have.length_of(2) - reservations[0].instances[0].id.should.equal(instance1.id) - reservations[0].instances[1].id.should.equal(instance3.id) - - @mock_ec2 def test_get_instances_filtering_by_tag_value_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -1287,39 +854,6 @@ def test_get_instances_filtering_by_tag_value_boto3(): res["Reservations"][0]["Instances"][1]["InstanceId"].should.equal(instance3.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_instances_filtering_by_tag_name(): - conn = boto.connect_ec2() - reservation = conn.run_instances(EXAMPLE_AMI_ID, min_count=3) - instance1, instance2, instance3 = reservation.instances - instance1.add_tag("tag1") - instance1.add_tag("tag2") - instance2.add_tag("tag1") - instance2.add_tag("tag2X") - instance3.add_tag("tag3") - - reservations = conn.get_all_reservations(filters={"tag-key": "tagX"}) - # get_all_reservations should return no instances - reservations.should.have.length_of(0) - - reservations = conn.get_all_reservations(filters={"tag-key": "tag1"}) - # get_all_reservations should return both instances with this tag value - reservations.should.have.length_of(1) - reservations[0].instances.should.have.length_of(2) - reservations[0].instances[0].id.should.equal(instance1.id) - reservations[0].instances[1].id.should.equal(instance2.id) - - reservations = conn.get_all_reservations(filters={"tag-key": ["tag1", "tag3"]}) - # get_all_reservations should return both instances with one of the - # acceptable tag values - reservations.should.have.length_of(1) - reservations[0].instances.should.have.length_of(3) - reservations[0].instances[0].id.should.equal(instance1.id) - reservations[0].instances[1].id.should.equal(instance2.id) - reservations[0].instances[2].id.should.equal(instance3.id) - - @mock_ec2 def test_get_instances_filtering_by_tag_name_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -1359,41 +893,6 @@ def test_get_instances_filtering_by_tag_name_boto3(): res["Reservations"][0]["Instances"][2]["InstanceId"].should.equal(instance3.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_instance_start_and_stop(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID, min_count=2) - instances = reservation.instances - instances.should.have.length_of(2) - - instance_ids = [instance.id for instance in instances] - - with pytest.raises(EC2ResponseError) as ex: - stopped_instances = conn.stop_instances(instance_ids, dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the StopInstance operation: Request would have succeeded, but DryRun flag is set" - ) - - stopped_instances = conn.stop_instances(instance_ids) - - for instance in stopped_instances: - instance.state.should.equal("stopping") - - with pytest.raises(EC2ResponseError) as ex: - started_instances = conn.start_instances([instances[0].id], dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the StartInstance operation: Request would have succeeded, but DryRun flag is set" - ) - - started_instances = conn.start_instances([instances[0].id]) - started_instances[0].state.should.equal("pending") - - @mock_ec2 def test_instance_start_and_stop_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -1442,25 +941,6 @@ def test_instance_start_and_stop_boto3(): # started_instances[0]["PreviousState"].should.equal({'Code': 80, 'Name': 'stopped'}) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_instance_reboot(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - with pytest.raises(EC2ResponseError) as ex: - instance.reboot(dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the RebootInstance operation: Request would have succeeded, but DryRun flag is set" - ) - - instance.reboot() - instance.state.should.equal("pending") - - @mock_ec2 def test_instance_reboot_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -1484,28 +964,6 @@ def test_instance_reboot_boto3(): instance.state.should.equal({"Code": 16, "Name": "running"}) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_instance_attribute_instance_type(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - with pytest.raises(EC2ResponseError) as ex: - instance.modify_attribute("instanceType", "m1.small", dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the ModifyInstanceType operation: Request would have succeeded, but DryRun flag is set" - ) - - instance.modify_attribute("instanceType", "m1.small") - - instance_attribute = instance.get_attribute("instanceType") - instance_attribute.should.be.a(InstanceAttribute) - instance_attribute.get("instanceType").should.equal("m1.small") - - @mock_ec2 def test_instance_attribute_instance_type_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -1529,37 +987,6 @@ def test_instance_attribute_instance_type_boto3(): ) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_modify_instance_attribute_security_groups(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - sg_id = conn.create_security_group( - "test security group", "this is a test security group" - ).id - sg_id2 = conn.create_security_group( - "test security group 2", "this is a test security group 2" - ).id - - with pytest.raises(EC2ResponseError) as ex: - instance.modify_attribute("groupSet", [sg_id, sg_id2], dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the ModifyInstanceSecurityGroups operation: Request would have succeeded, but DryRun flag is set" - ) - - instance.modify_attribute("groupSet", [sg_id, sg_id2]) - - instance_attribute = instance.get_attribute("groupSet") - instance_attribute.should.be.a(InstanceAttribute) - group_list = instance_attribute.get("groupSet") - any(g.id == sg_id for g in group_list).should.be.ok - any(g.id == sg_id2 for g in group_list).should.be.ok - - @mock_ec2 def test_modify_instance_attribute_security_groups_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -1591,28 +1018,6 @@ def test_modify_instance_attribute_security_groups_boto3(): new_groups.should.contain({"GroupId": sg_id2}) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_instance_attribute_user_data(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - with pytest.raises(EC2ResponseError) as ex: - instance.modify_attribute("userData", "this is my user data", dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the ModifyUserData operation: Request would have succeeded, but DryRun flag is set" - ) - - instance.modify_attribute("userData", "this is my user data") - - instance_attribute = instance.get_attribute("userData") - instance_attribute.should.be.a(InstanceAttribute) - instance_attribute.get("userData").should.equal("this is my user data") - - @mock_ec2 def test_instance_attribute_user_data_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -1636,50 +1041,6 @@ def test_instance_attribute_user_data_boto3(): decode_method(retrieved_user_data).should.equal(b"this is my user data") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_instance_attribute_source_dest_check(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - # Default value is true - instance.sourceDestCheck.should.equal("true") - - instance_attribute = instance.get_attribute("sourceDestCheck") - instance_attribute.should.be.a(InstanceAttribute) - instance_attribute.get("sourceDestCheck").should.equal(True) - - # Set to false (note: Boto converts bool to string, eg 'false') - - with pytest.raises(EC2ResponseError) as ex: - instance.modify_attribute("sourceDestCheck", False, dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the ModifySourceDestCheck operation: Request would have succeeded, but DryRun flag is set" - ) - - instance.modify_attribute("sourceDestCheck", False) - - instance.update() - instance.sourceDestCheck.should.equal("false") - - instance_attribute = instance.get_attribute("sourceDestCheck") - instance_attribute.should.be.a(InstanceAttribute) - instance_attribute.get("sourceDestCheck").should.equal(False) - - # Set back to true - instance.modify_attribute("sourceDestCheck", True) - - instance.update() - instance.sourceDestCheck.should.equal("true") - - instance_attribute = instance.get_attribute("sourceDestCheck") - instance_attribute.should.be.a(InstanceAttribute) - instance_attribute.get("sourceDestCheck").should.equal(True) - - @mock_ec2 def test_instance_attribute_source_dest_check_boto3(): ec2 = boto3.resource("ec2", region_name="us-west-1") @@ -1710,21 +1071,6 @@ def test_instance_attribute_source_dest_check_boto3(): instance_attribute.get("SourceDestCheck").should.equal({"Value": True}) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_user_data_with_run_instance(): - user_data = b"some user data" - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID, user_data=user_data) - instance = reservation.instances[0] - - instance_attribute = instance.get_attribute("userData") - instance_attribute.should.be.a(InstanceAttribute) - retrieved_user_data = instance_attribute.get("userData").encode("utf-8") - decoded_user_data = decode_method(retrieved_user_data) - decoded_user_data.should.equal(b"some user data") - - @mock_ec2 def test_user_data_with_run_instance_boto3(): user_data = b"some user data" @@ -1739,28 +1085,6 @@ def test_user_data_with_run_instance_boto3(): decoded_user_data.should.equal(b"some user data") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_run_instance_with_security_group_name(): - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as ex: - group = conn.create_security_group("group1", "some description", dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the CreateSecurityGroup operation: Request would have succeeded, but DryRun flag is set" - ) - - group = conn.create_security_group("group1", "some description") - - reservation = conn.run_instances(EXAMPLE_AMI_ID, security_groups=["group1"]) - instance = reservation.instances[0] - - instance.groups[0].id.should.equal(group.id) - instance.groups[0].name.should.equal("group1") - - @mock_ec2 def test_run_instance_with_security_group_name_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -1791,18 +1115,6 @@ def test_run_instance_with_security_group_name_boto3(): ) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_run_instance_with_security_group_id(): - conn = boto.connect_ec2("the_key", "the_secret") - group = conn.create_security_group("group1", "some description") - reservation = conn.run_instances(EXAMPLE_AMI_ID, security_group_ids=[group.id]) - instance = reservation.instances[0] - - instance.groups[0].id.should.equal(group.id) - instance.groups[0].name.should.equal("group1") - - @mock_ec2 def test_run_instance_with_security_group_id_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -1819,16 +1131,6 @@ def test_run_instance_with_security_group_id_boto3(): ) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_run_instance_with_instance_type(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID, instance_type="t1.micro") - instance = reservation.instances[0] - - instance.instance_type.should.equal("t1.micro") - - @mock_ec2 def test_run_instance_with_instance_type_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -1839,16 +1141,6 @@ def test_run_instance_with_instance_type_boto3(): instance.instance_type.should.equal("t1.micro") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_run_instance_with_default_placement(): - conn = boto.ec2.connect_to_region("us-east-1") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - instance.placement.should.equal("us-east-1a") - - @mock_ec2 def test_run_instance_with_default_placement_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -1857,16 +1149,6 @@ def test_run_instance_with_default_placement_boto3(): instance.placement.should.have.key("AvailabilityZone").equal("us-east-1a") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_run_instance_with_placement(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID, placement="us-east-1b") - instance = reservation.instances[0] - - instance.placement.should.equal("us-east-1b") - - @mock_ec2 def test_run_instance_with_placement_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -1976,51 +1258,6 @@ def test_run_instance_mapped_public_ipv4(): len(instance["PublicIpAddress"]).should.be.greater_than(0) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_run_instance_with_nic_autocreated(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") - security_group1 = conn.create_security_group( - "test security group #1", "this is a test security group" - ) - security_group2 = conn.create_security_group( - "test security group #2", "this is a test security group" - ) - private_ip = "10.0.0.1" - - reservation = conn.run_instances( - EXAMPLE_AMI_ID, - subnet_id=subnet.id, - security_groups=[security_group1.name], - security_group_ids=[security_group2.id], - private_ip_address=private_ip, - ) - instance = reservation.instances[0] - - all_enis = conn.get_all_network_interfaces() - all_enis.should.have.length_of(1) - eni = all_enis[0] - - instance.interfaces.should.have.length_of(1) - instance.interfaces[0].id.should.equal(eni.id) - - instance.subnet_id.should.equal(subnet.id) - instance.groups.should.have.length_of(2) - set([group.id for group in instance.groups]).should.equal( - set([security_group1.id, security_group2.id]) - ) - - eni.subnet_id.should.equal(subnet.id) - eni.groups.should.have.length_of(2) - set([group.id for group in eni.groups]).should.equal( - set([security_group1.id, security_group2.id]) - ) - eni.private_ip_addresses.should.have.length_of(1) - eni.private_ip_addresses[0].private_ip_address.should.equal(private_ip) - - @mock_ec2 def test_run_instance_with_nic_autocreated_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -2071,61 +1308,6 @@ def test_run_instance_with_nic_autocreated_boto3(): eni["PrivateIpAddresses"][0]["PrivateIpAddress"].should.equal(private_ip) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_run_instance_with_nic_preexisting(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") - security_group1 = conn.create_security_group( - "test security group #1", "this is a test security group" - ) - security_group2 = conn.create_security_group( - "test security group #2", "this is a test security group" - ) - private_ip = "54.0.0.1" - eni = conn.create_network_interface( - subnet.id, private_ip, groups=[security_group1.id] - ) - - # Boto requires NetworkInterfaceCollection of NetworkInterfaceSpecifications... - # annoying, but generates the desired querystring. - from boto.ec2.networkinterface import ( - NetworkInterfaceSpecification, - NetworkInterfaceCollection, - ) - - interface = NetworkInterfaceSpecification( - network_interface_id=eni.id, device_index=0 - ) - interfaces = NetworkInterfaceCollection(interface) - # end Boto objects - - reservation = conn.run_instances( - EXAMPLE_AMI_ID, - network_interfaces=interfaces, - security_group_ids=[security_group2.id], - ) - instance = reservation.instances[0] - - instance.subnet_id.should.equal(subnet.id) - - all_enis = conn.get_all_network_interfaces() - all_enis.should.have.length_of(1) - - instance.interfaces.should.have.length_of(1) - instance_eni = instance.interfaces[0] - instance_eni.id.should.equal(eni.id) - - instance_eni.subnet_id.should.equal(subnet.id) - instance_eni.groups.should.have.length_of(2) - set([group.id for group in instance_eni.groups]).should.equal( - set([security_group1.id, security_group2.id]) - ) - instance_eni.private_ip_addresses.should.have.length_of(1) - instance_eni.private_ip_addresses[0].private_ip_address.should.equal(private_ip) - - @mock_ec2 def test_run_instance_with_nic_preexisting_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -2176,88 +1358,6 @@ def test_run_instance_with_nic_preexisting_boto3(): instance_eni["PrivateIpAddresses"][0]["PrivateIpAddress"].should.equal(private_ip) -# Has boto3 equivalent -@requires_boto_gte("2.32.0") -@mock_ec2_deprecated -def test_instance_with_nic_attach_detach(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") - - security_group1 = conn.create_security_group( - "test security group #1", "this is a test security group" - ) - security_group2 = conn.create_security_group( - "test security group #2", "this is a test security group" - ) - - reservation = conn.run_instances( - EXAMPLE_AMI_ID, security_group_ids=[security_group1.id] - ) - instance = reservation.instances[0] - - eni = conn.create_network_interface(subnet.id, groups=[security_group2.id]) - - # Check initial instance and ENI data - instance.interfaces.should.have.length_of(1) - - eni.groups.should.have.length_of(1) - set([group.id for group in eni.groups]).should.equal(set([security_group2.id])) - - # Attach - with pytest.raises(EC2ResponseError) as ex: - conn.attach_network_interface(eni.id, instance.id, device_index=1, dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the AttachNetworkInterface operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.attach_network_interface(eni.id, instance.id, device_index=1) - - # Check attached instance and ENI data - instance.update() - instance.interfaces.should.have.length_of(2) - instance_eni = instance.interfaces[1] - instance_eni.id.should.equal(eni.id) - instance_eni.groups.should.have.length_of(2) - set([group.id for group in instance_eni.groups]).should.equal( - set([security_group1.id, security_group2.id]) - ) - - eni = conn.get_all_network_interfaces(filters={"network-interface-id": eni.id})[0] - eni.groups.should.have.length_of(2) - set([group.id for group in eni.groups]).should.equal( - set([security_group1.id, security_group2.id]) - ) - - # Detach - with pytest.raises(EC2ResponseError) as ex: - conn.detach_network_interface(instance_eni.attachment.id, dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the DetachNetworkInterface operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.detach_network_interface(instance_eni.attachment.id) - - # Check detached instance and ENI data - instance.update() - instance.interfaces.should.have.length_of(1) - - eni = conn.get_all_network_interfaces(filters={"network-interface-id": eni.id})[0] - eni.groups.should.have.length_of(1) - set([group.id for group in eni.groups]).should.equal(set([security_group2.id])) - - # Detach with invalid attachment ID - with pytest.raises(EC2ResponseError) as cm: - conn.detach_network_interface("eni-attach-1234abcd") - cm.value.code.should.equal("InvalidAttachmentID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_instance_with_nic_attach_detach_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -2360,20 +1460,6 @@ def test_instance_with_nic_attach_detach_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidAttachmentID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_ec2_classic_has_public_ip_address(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID, key_name="keypair_name") - instance = reservation.instances[0] - instance.ip_address.should_not.equal(None) - instance.public_dns_name.should.contain(instance.ip_address.replace(".", "-")) - instance.private_ip_address.should_not.equal(None) - instance.private_dns_name.should.contain( - instance.private_ip_address.replace(".", "-") - ) - - @mock_ec2 def test_ec2_classic_has_public_ip_address_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -2388,16 +1474,6 @@ def test_ec2_classic_has_public_ip_address_boto3(): ) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_run_instance_with_keypair(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID, key_name="keypair_name") - instance = reservation.instances[0] - - instance.key_name.should.equal("keypair_name") - - @mock_ec2 def test_run_instance_with_keypair_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -2514,14 +1590,6 @@ def test_run_instance_with_block_device_mappings_from_snapshot(): volumes["Volumes"][0]["SnapshotId"].should.equal(snapshot.snapshot_id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_describe_instance_status_no_instances(): - conn = boto.connect_ec2("the_key", "the_secret") - all_status = conn.get_all_instance_status() - len(all_status).should.equal(0) - - @mock_ec2 def test_describe_instance_status_no_instances_boto3(): if settings.TEST_SERVER_MODE: @@ -2531,18 +1599,6 @@ def test_describe_instance_status_no_instances_boto3(): all_status.should.have.length_of(0) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_describe_instance_status_with_instances(): - conn = boto.connect_ec2("the_key", "the_secret") - conn.run_instances(EXAMPLE_AMI_ID, key_name="keypair_name") - - all_status = conn.get_all_instance_status() - len(all_status).should.equal(1) - all_status[0].instance_status.status.should.equal("ok") - all_status[0].system_status.status.should.equal("ok") - - @mock_ec2 def test_describe_instance_status_with_instances_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -2558,30 +1614,6 @@ def test_describe_instance_status_with_instances_boto3(): my_status["SystemStatus"]["Status"].should.equal("ok") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_describe_instance_status_with_instance_filter_deprecated(): - conn = boto.connect_ec2("the_key", "the_secret") - - # We want to filter based on this one - reservation = conn.run_instances(EXAMPLE_AMI_ID, key_name="keypair_name") - instance = reservation.instances[0] - - # This is just to setup the test - conn.run_instances(EXAMPLE_AMI_ID, key_name="keypair_name") - - all_status = conn.get_all_instance_status(instance_ids=[instance.id]) - len(all_status).should.equal(1) - all_status[0].id.should.equal(instance.id) - - # Call get_all_instance_status with a bad id should raise an error - with pytest.raises(EC2ResponseError) as cm: - conn.get_all_instance_status(instance_ids=[instance.id, "i-1234abcd"]) - cm.value.code.should.equal("InvalidInstanceID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_describe_instance_status_with_instance_filter_deprecated_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -2705,34 +1737,6 @@ def test_describe_instance_status_with_instance_filter(): found_instance_ids.shouldnt.contain(_id) -# Has boto3 equivalent -@requires_boto_gte("2.32.0") -@mock_ec2_deprecated -def test_describe_instance_status_with_non_running_instances(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID, min_count=3) - instance1, instance2, instance3 = reservation.instances - instance1.stop() - instance2.terminate() - - all_running_status = conn.get_all_instance_status() - all_running_status.should.have.length_of(1) - all_running_status[0].id.should.equal(instance3.id) - all_running_status[0].state_name.should.equal("running") - - all_status = conn.get_all_instance_status(include_all_instances=True) - all_status.should.have.length_of(3) - - status1 = next((s for s in all_status if s.id == instance1.id), None) - status1.state_name.should.equal("stopped") - - status2 = next((s for s in all_status if s.id == instance2.id), None) - status2.state_name.should.equal("terminated") - - status3 = next((s for s in all_status if s.id == instance3.id), None) - status3.state_name.should.equal("running") - - @mock_ec2 def test_describe_instance_status_with_non_running_instances_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -2771,34 +1775,6 @@ def test_describe_instance_status_with_non_running_instances_boto3(): status3["InstanceState"].should.equal({"Code": 16, "Name": "running"}) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_instance_by_security_group(): - conn = boto.connect_ec2("the_key", "the_secret") - - conn.run_instances(EXAMPLE_AMI_ID) - instance = conn.get_only_instances()[0] - - security_group = conn.create_security_group("test", "test") - - with pytest.raises(EC2ResponseError) as ex: - conn.modify_instance_attribute( - instance.id, "groupSet", [security_group.id], dry_run=True - ) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the ModifyInstanceSecurityGroups operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.modify_instance_attribute(instance.id, "groupSet", [security_group.id]) - - security_group_instances = security_group.instances() - - assert len(security_group_instances) == 1 - assert security_group_instances[0].id == instance.id - - @mock_ec2 def test_get_instance_by_security_group_boto3(): client = boto3.client("ec2", region_name="us-east-1") diff --git a/tests/test_ec2/test_internet_gateways.py b/tests/test_ec2/test_internet_gateways.py index 0677c89fa..ecf54a3a5 100644 --- a/tests/test_ec2/test_internet_gateways.py +++ b/tests/test_ec2/test_internet_gateways.py @@ -1,14 +1,12 @@ import pytest -import boto import boto3 -from boto.exception import EC2ResponseError from botocore.exceptions import ClientError import sure # noqa # pylint: disable=unused-import -from moto import mock_ec2_deprecated, mock_ec2 +from moto import mock_ec2 from uuid import uuid4 @@ -17,30 +15,6 @@ BAD_VPC = "vpc-deadbeef" BAD_IGW = "igw-deadbeef" -# Has boto3 equivalent -@mock_ec2_deprecated -def test_igw_create(): - """internet gateway create""" - conn = boto.connect_vpc("the_key", "the_secret") - - conn.get_all_internet_gateways().should.have.length_of(0) - - with pytest.raises(EC2ResponseError) as ex: - igw = conn.create_internet_gateway(dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the CreateInternetGateway operation: Request would have succeeded, but DryRun flag is set" - ) - - igw = conn.create_internet_gateway() - conn.get_all_internet_gateways().should.have.length_of(1) - igw.id.should.match(r"igw-[0-9a-f]+") - - igw = conn.get_all_internet_gateways()[0] - igw.attachments.should.have.length_of(0) - - @mock_ec2 def test_igw_create_boto3(): """ internet gateway create """ @@ -64,28 +38,6 @@ def test_igw_create_boto3(): igw["Attachments"].should.have.length_of(0) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_igw_attach(): - """internet gateway attach""" - conn = boto.connect_vpc("the_key", "the_secret") - igw = conn.create_internet_gateway() - vpc = conn.create_vpc(VPC_CIDR) - - with pytest.raises(EC2ResponseError) as ex: - conn.attach_internet_gateway(igw.id, vpc.id, dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the AttachInternetGateway operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.attach_internet_gateway(igw.id, vpc.id) - - igw = conn.get_all_internet_gateways()[0] - igw.attachments[0].vpc_id.should.be.equal(vpc.id) - - @mock_ec2 def test_igw_attach_boto3(): """ internet gateway attach """ @@ -111,20 +63,6 @@ def test_igw_attach_boto3(): igw["Attachments"].should.equal([{"State": "available", "VpcId": vpc.id}]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_igw_attach_bad_vpc(): - """internet gateway fail to attach w/ bad vpc""" - conn = boto.connect_vpc("the_key", "the_secret") - igw = conn.create_internet_gateway() - - with pytest.raises(EC2ResponseError) as cm: - conn.attach_internet_gateway(igw.id, BAD_VPC) - cm.value.code.should.equal("InvalidVpcID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_igw_attach_bad_vpc_boto3(): """ internet gateway fail to attach w/ bad vpc """ @@ -138,23 +76,6 @@ def test_igw_attach_bad_vpc_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidVpcID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_igw_attach_twice(): - """internet gateway fail to attach twice""" - conn = boto.connect_vpc("the_key", "the_secret") - igw = conn.create_internet_gateway() - vpc1 = conn.create_vpc(VPC_CIDR) - vpc2 = conn.create_vpc(VPC_CIDR) - conn.attach_internet_gateway(igw.id, vpc1.id) - - with pytest.raises(EC2ResponseError) as cm: - conn.attach_internet_gateway(igw.id, vpc2.id) - cm.value.code.should.equal("Resource.AlreadyAssociated") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_igw_attach_twice_boto3(): """ internet gateway fail to attach twice """ @@ -172,28 +93,6 @@ def test_igw_attach_twice_boto3(): ex.value.response["Error"]["Code"].should.equal("Resource.AlreadyAssociated") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_igw_detach(): - """internet gateway detach""" - conn = boto.connect_vpc("the_key", "the_secret") - igw = conn.create_internet_gateway() - vpc = conn.create_vpc(VPC_CIDR) - conn.attach_internet_gateway(igw.id, vpc.id) - - with pytest.raises(EC2ResponseError) as ex: - conn.detach_internet_gateway(igw.id, vpc.id, dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the DetachInternetGateway operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.detach_internet_gateway(igw.id, vpc.id) - igw = conn.get_all_internet_gateways()[0] - igw.attachments.should.have.length_of(0) - - @mock_ec2 def test_igw_detach_boto3(): """ internet gateway detach""" @@ -220,23 +119,6 @@ def test_igw_detach_boto3(): igw["Attachments"].should.have.length_of(0) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_igw_detach_wrong_vpc(): - """internet gateway fail to detach w/ wrong vpc""" - conn = boto.connect_vpc("the_key", "the_secret") - igw = conn.create_internet_gateway() - vpc1 = conn.create_vpc(VPC_CIDR) - vpc2 = conn.create_vpc(VPC_CIDR) - conn.attach_internet_gateway(igw.id, vpc1.id) - - with pytest.raises(EC2ResponseError) as cm: - conn.detach_internet_gateway(igw.id, vpc2.id) - cm.value.code.should.equal("Gateway.NotAttached") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_igw_detach_wrong_vpc_boto3(): """ internet gateway fail to detach w/ wrong vpc """ @@ -254,22 +136,6 @@ def test_igw_detach_wrong_vpc_boto3(): ex.value.response["Error"]["Code"].should.equal("Gateway.NotAttached") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_igw_detach_invalid_vpc(): - """internet gateway fail to detach w/ invalid vpc""" - conn = boto.connect_vpc("the_key", "the_secret") - igw = conn.create_internet_gateway() - vpc = conn.create_vpc(VPC_CIDR) - conn.attach_internet_gateway(igw.id, vpc.id) - - with pytest.raises(EC2ResponseError) as cm: - conn.detach_internet_gateway(igw.id, BAD_VPC) - cm.value.code.should.equal("Gateway.NotAttached") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_igw_detach_invalid_vpc_boto3(): """ internet gateway fail to detach w/ invalid vpc """ @@ -286,21 +152,6 @@ def test_igw_detach_invalid_vpc_boto3(): ex.value.response["Error"]["Code"].should.equal("Gateway.NotAttached") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_igw_detach_unattached(): - """internet gateway fail to detach unattached""" - conn = boto.connect_vpc("the_key", "the_secret") - igw = conn.create_internet_gateway() - vpc = conn.create_vpc(VPC_CIDR) - - with pytest.raises(EC2ResponseError) as cm: - conn.detach_internet_gateway(igw.id, vpc.id) - cm.value.code.should.equal("Gateway.NotAttached") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_igw_detach_unattached_boto3(): """ internet gateway fail to detach unattached """ @@ -316,28 +167,6 @@ def test_igw_detach_unattached_boto3(): ex.value.response["Error"]["Code"].should.equal("Gateway.NotAttached") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_igw_delete(): - """internet gateway delete""" - conn = boto.connect_vpc("the_key", "the_secret") - conn.create_vpc(VPC_CIDR) - conn.get_all_internet_gateways().should.have.length_of(0) - igw = conn.create_internet_gateway() - conn.get_all_internet_gateways().should.have.length_of(1) - - with pytest.raises(EC2ResponseError) as ex: - conn.delete_internet_gateway(igw.id, dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the DeleteInternetGateway operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.delete_internet_gateway(igw.id) - conn.get_all_internet_gateways().should.have.length_of(0) - - @mock_ec2 def test_igw_delete_boto3(): """ internet gateway delete""" @@ -360,22 +189,6 @@ def test_igw_delete_boto3(): [i["InternetGatewayId"] for i in (retrieve_all(client))].shouldnt.contain(igw.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_igw_delete_attached(): - """internet gateway fail to delete attached""" - conn = boto.connect_vpc("the_key", "the_secret") - igw = conn.create_internet_gateway() - vpc = conn.create_vpc(VPC_CIDR) - conn.attach_internet_gateway(igw.id, vpc.id) - - with pytest.raises(EC2ResponseError) as cm: - conn.delete_internet_gateway(igw.id) - cm.value.code.should.equal("DependencyViolation") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_igw_delete_attached_boto3(): """ internet gateway fail to delete attached """ @@ -392,16 +205,6 @@ def test_igw_delete_attached_boto3(): ex.value.response["Error"]["Code"].should.equal("DependencyViolation") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_igw_desribe(): - """internet gateway fetch by id""" - conn = boto.connect_vpc("the_key", "the_secret") - igw = conn.create_internet_gateway() - igw_by_search = conn.get_all_internet_gateways([igw.id])[0] - igw.id.should.equal(igw_by_search.id) - - @mock_ec2 def test_igw_describe_boto3(): """ internet gateway fetch by id """ @@ -414,18 +217,6 @@ def test_igw_describe_boto3(): igw.id.should.equal(igw_by_search["InternetGatewayId"]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_igw_describe_bad_id(): - """internet gateway fail to fetch by bad id""" - conn = boto.connect_vpc("the_key", "the_secret") - with pytest.raises(EC2ResponseError) as cm: - conn.get_all_internet_gateways([BAD_IGW]) - cm.value.code.should.equal("InvalidInternetGatewayID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_igw_describe_bad_id_boto3(): """ internet gateway fail to fetch by bad id """ @@ -437,22 +228,6 @@ def test_igw_describe_bad_id_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidInternetGatewayID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_igw_filter_by_vpc_id(): - """internet gateway filter by vpc id""" - conn = boto.connect_vpc("the_key", "the_secret") - - igw1 = conn.create_internet_gateway() - conn.create_internet_gateway() - vpc = conn.create_vpc(VPC_CIDR) - conn.attach_internet_gateway(igw1.id, vpc.id) - - result = conn.get_all_internet_gateways(filters={"attachment.vpc-id": vpc.id}) - result.should.have.length_of(1) - result[0].id.should.equal(igw1.id) - - @mock_ec2 def test_igw_filter_by_vpc_id_boto3(): """ internet gateway filter by vpc id """ @@ -471,21 +246,6 @@ def test_igw_filter_by_vpc_id_boto3(): result["InternetGateways"][0]["InternetGatewayId"].should.equal(igw1.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_igw_filter_by_tags(): - """internet gateway filter by vpc id""" - conn = boto.connect_vpc("the_key", "the_secret") - - igw1 = conn.create_internet_gateway() - conn.create_internet_gateway() - igw1.add_tag("tests", "yes") - - result = conn.get_all_internet_gateways(filters={"tag:tests": "yes"}) - result.should.have.length_of(1) - result[0].id.should.equal(igw1.id) - - @mock_ec2 def test_igw_filter_by_tags_boto3(): """ internet gateway filter by vpc id """ @@ -502,20 +262,6 @@ def test_igw_filter_by_tags_boto3(): result[0]["InternetGatewayId"].should.equal(igw1.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_igw_filter_by_internet_gateway_id(): - """internet gateway filter by internet gateway id""" - conn = boto.connect_vpc("the_key", "the_secret") - - igw1 = conn.create_internet_gateway() - conn.create_internet_gateway() - - result = conn.get_all_internet_gateways(filters={"internet-gateway-id": igw1.id}) - result.should.have.length_of(1) - result[0].id.should.equal(igw1.id) - - @mock_ec2 def test_igw_filter_by_internet_gateway_id_boto3(): """ internet gateway filter by internet gateway id """ @@ -532,22 +278,6 @@ def test_igw_filter_by_internet_gateway_id_boto3(): result["InternetGateways"][0]["InternetGatewayId"].should.equal(igw1.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_igw_filter_by_attachment_state(): - """internet gateway filter by attachment state""" - conn = boto.connect_vpc("the_key", "the_secret") - - igw1 = conn.create_internet_gateway() - conn.create_internet_gateway() - vpc = conn.create_vpc(VPC_CIDR) - conn.attach_internet_gateway(igw1.id, vpc.id) - - result = conn.get_all_internet_gateways(filters={"attachment.state": "available"}) - result.should.have.length_of(1) - result[0].id.should.equal(igw1.id) - - @mock_ec2 def test_igw_filter_by_attachment_state_boto3(): """ internet gateway filter by attachment state """ diff --git a/tests/test_ec2/test_key_pairs.py b/tests/test_ec2/test_key_pairs.py index 013dd00da..6ad6e69ab 100644 --- a/tests/test_ec2/test_key_pairs.py +++ b/tests/test_ec2/test_key_pairs.py @@ -1,12 +1,10 @@ import pytest -import boto import sure # noqa # pylint: disable=unused-import import boto3 -from boto.exception import EC2ResponseError from botocore.exceptions import ClientError -from moto import mock_ec2, mock_ec2_deprecated, settings +from moto import mock_ec2, settings from uuid import uuid4 from unittest import SkipTest @@ -46,13 +44,6 @@ ffsm7UIHtCBYERr9Nx0u20ldfhkgB1lhaJb5o0ZJ3pmJ38KChfyHe5EUcqRdEFo89Mp72VI2Z6UHyL17 moto@github.com""" -# Has boto3 equivalent -@mock_ec2_deprecated -def test_key_pairs_empty(): - conn = boto.connect_ec2("the_key", "the_secret") - assert len(conn.get_all_key_pairs()) == 0 - - @mock_ec2 def test_key_pairs_empty_boto3(): if settings.TEST_SERVER_MODE: @@ -61,18 +52,6 @@ def test_key_pairs_empty_boto3(): client.describe_key_pairs()["KeyPairs"].should.be.empty -# Has boto3 equivalent -@mock_ec2_deprecated -def test_key_pairs_invalid_id(): - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as cm: - conn.get_all_key_pairs("foo") - cm.value.code.should.equal("InvalidKeyPair.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_key_pairs_invalid_id_boto3(): client = boto3.client("ec2", "us-west-1") @@ -84,49 +63,6 @@ def test_key_pairs_invalid_id_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidKeyPair.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_key_pairs_create(): - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as ex: - conn.create_key_pair("foo", dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the CreateKeyPair operation: Request would have succeeded, but DryRun flag is set" - ) - - kp = conn.create_key_pair("foo") - rsa_check_private_key(kp.material) - - kps = conn.get_all_key_pairs() - assert len(kps) == 1 - assert kps[0].name == "foo" - - -# Has boto3 equivalent -@mock_ec2_deprecated -def test_key_pairs_create_two(): - conn = boto.connect_ec2("the_key", "the_secret") - - kp1 = conn.create_key_pair("foo") - rsa_check_private_key(kp1.material) - - kp2 = conn.create_key_pair("bar") - rsa_check_private_key(kp2.material) - - assert kp1.material != kp2.material - - kps = conn.get_all_key_pairs() - kps.should.have.length_of(2) - assert {i.name for i in kps} == {"foo", "bar"} - - kps = conn.get_all_key_pairs("foo") - kps.should.have.length_of(1) - kps[0].name.should.equal("foo") - - @mock_ec2 def test_key_pairs_create_dryrun_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -166,20 +102,6 @@ def test_key_pairs_create_boto3(): kps[0].should.have.key("KeyFingerprint") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_key_pairs_create_exist(): - conn = boto.connect_ec2("the_key", "the_secret") - conn.create_key_pair("foo") - assert len(conn.get_all_key_pairs()) == 1 - - with pytest.raises(EC2ResponseError) as cm: - conn.create_key_pair("foo") - cm.value.code.should.equal("InvalidKeyPair.Duplicate") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_key_pairs_create_exist_boto3(): client = boto3.client("ec2", "us-west-1") @@ -193,40 +115,12 @@ def test_key_pairs_create_exist_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidKeyPair.Duplicate") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_key_pairs_delete_no_exist(): - conn = boto.connect_ec2("the_key", "the_secret") - assert len(conn.get_all_key_pairs()) == 0 - r = conn.delete_key_pair("foo") - r.should.be.ok - - @mock_ec2 def test_key_pairs_delete_no_exist_boto3(): client = boto3.client("ec2", "us-west-1") client.delete_key_pair(KeyName=str(uuid4())[0:6]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_key_pairs_delete_exist(): - conn = boto.connect_ec2("the_key", "the_secret") - conn.create_key_pair("foo") - - with pytest.raises(EC2ResponseError) as ex: - r = conn.delete_key_pair("foo", dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the DeleteKeyPair operation: Request would have succeeded, but DryRun flag is set" - ) - - r = conn.delete_key_pair("foo") - r.should.be.ok - assert len(conn.get_all_key_pairs()) == 0 - - @mock_ec2 def test_key_pairs_delete_exist_boto3(): client = boto3.client("ec2", "us-west-1") @@ -247,33 +141,6 @@ def test_key_pairs_delete_exist_boto3(): ) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_key_pairs_import(): - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as ex: - conn.import_key_pair("foo", RSA_PUBLIC_KEY_OPENSSH, dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the ImportKeyPair operation: Request would have succeeded, but DryRun flag is set" - ) - - kp1 = conn.import_key_pair("foo", RSA_PUBLIC_KEY_OPENSSH) - assert kp1.name == "foo" - assert kp1.fingerprint == RSA_PUBLIC_KEY_FINGERPRINT - - kp2 = conn.import_key_pair("foo2", RSA_PUBLIC_KEY_RFC4716) - assert kp2.name == "foo2" - assert kp2.fingerprint == RSA_PUBLIC_KEY_FINGERPRINT - - kps = conn.get_all_key_pairs() - assert len(kps) == 2 - assert kps[0].name == kp1.name - assert kps[1].name == kp2.name - - @mock_ec2 def test_key_pairs_import_boto3(): client = boto3.client("ec2", "us-west-1") @@ -309,21 +176,6 @@ def test_key_pairs_import_boto3(): all_names.should.contain(kp2["KeyName"]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_key_pairs_import_exist(): - conn = boto.connect_ec2("the_key", "the_secret") - kp = conn.import_key_pair("foo", RSA_PUBLIC_KEY_OPENSSH) - assert kp.name == "foo" - assert len(conn.get_all_key_pairs()) == 1 - - with pytest.raises(EC2ResponseError) as cm: - conn.create_key_pair("foo") - cm.value.code.should.equal("InvalidKeyPair.Duplicate") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_key_pairs_import_exist_boto3(): client = boto3.client("ec2", "us-west-1") @@ -343,30 +195,6 @@ def test_key_pairs_import_exist_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidKeyPair.Duplicate") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_key_pairs_invalid(): - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as ex: - conn.import_key_pair("foo", b"") - ex.value.error_code.should.equal("InvalidKeyPair.Format") - ex.value.status.should.equal(400) - ex.value.message.should.equal("Key is not in valid OpenSSH public key format") - - with pytest.raises(EC2ResponseError) as ex: - conn.import_key_pair("foo", b"garbage") - ex.value.error_code.should.equal("InvalidKeyPair.Format") - ex.value.status.should.equal(400) - ex.value.message.should.equal("Key is not in valid OpenSSH public key format") - - with pytest.raises(EC2ResponseError) as ex: - conn.import_key_pair("foo", DSA_PUBLIC_KEY_OPENSSH) - ex.value.error_code.should.equal("InvalidKeyPair.Format") - ex.value.status.should.equal(400) - ex.value.message.should.equal("Key is not in valid OpenSSH public key format") - - @mock_ec2 def test_key_pairs_invalid_boto3(): client = boto3.client("ec2", "us-west-1") @@ -393,22 +221,6 @@ def test_key_pairs_invalid_boto3(): err["Message"].should.equal("Key is not in valid OpenSSH public key format") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_key_pair_filters(): - conn = boto.connect_ec2("the_key", "the_secret") - - _ = conn.create_key_pair("kpfltr1") - kp2 = conn.create_key_pair("kpfltr2") - kp3 = conn.create_key_pair("kpfltr3") - - kp_by_name = conn.get_all_key_pairs(filters={"key-name": "kpfltr2"}) - set([kp.name for kp in kp_by_name]).should.equal(set([kp2.name])) - - kp_by_name = conn.get_all_key_pairs(filters={"fingerprint": kp3.fingerprint}) - set([kp.name for kp in kp_by_name]).should.equal(set([kp3.name])) - - @mock_ec2 def test_key_pair_filters_boto3(): ec2 = boto3.resource("ec2", "us-west-1") diff --git a/tests/test_ec2/test_network_acls.py b/tests/test_ec2/test_network_acls.py index aadfca125..9367a4811 100644 --- a/tests/test_ec2/test_network_acls.py +++ b/tests/test_ec2/test_network_acls.py @@ -1,24 +1,14 @@ -import boto import boto3 import sure # noqa # pylint: disable=unused-import import pytest from botocore.exceptions import ClientError -from moto import mock_ec2_deprecated, mock_ec2, settings +from moto import mock_ec2, settings from moto.ec2.models import OWNER_ID from random import randint from unittest import SkipTest -# Has boto3 equivalent -@mock_ec2_deprecated -def test_default_network_acl_created_with_vpc(): - conn = boto.connect_vpc("the_key", "the secret") - conn.create_vpc("10.0.0.0/16") - all_network_acls = conn.get_all_network_acls() - all_network_acls.should.have.length_of(2) - - @mock_ec2 def test_default_network_acl_created_with_vpc_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -31,16 +21,6 @@ def test_default_network_acl_created_with_vpc_boto3(): our_acl[0].should.have.key("IsDefault").equals(True) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_network_acls(): - conn = boto.connect_vpc("the_key", "the secret") - vpc = conn.create_vpc("10.0.0.0/16") - conn.create_network_acl(vpc.id) - all_network_acls = conn.get_all_network_acls() - all_network_acls.should.have.length_of(3) - - @mock_ec2 def test_network_create_and_list_acls_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -58,21 +38,6 @@ def test_network_create_and_list_acls_boto3(): acl_found["IsDefault"].should.equal(False) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_new_subnet_associates_with_default_network_acl(): - conn = boto.connect_vpc("the_key", "the secret") - vpc = conn.get_all_vpcs()[0] - - subnet = conn.create_subnet(vpc.id, "172.31.112.0/20") - all_network_acls = conn.get_all_network_acls() - all_network_acls.should.have.length_of(1) - - acl = all_network_acls[0] - acl.associations.should.have.length_of(7) - [a.subnet_id for a in acl.associations].should.contain(subnet.id) - - @mock_ec2 def test_new_subnet_associates_with_default_network_acl_boto3(): if settings.TEST_SERVER_MODE: @@ -90,36 +55,6 @@ def test_new_subnet_associates_with_default_network_acl_boto3(): [a["SubnetId"] for a in acl["Associations"]].should.contain(subnet.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_network_acl_entries(): - conn = boto.connect_vpc("the_key", "the secret") - vpc = conn.create_vpc("10.0.0.0/16") - - network_acl = conn.create_network_acl(vpc.id) - - conn.create_network_acl_entry( - network_acl.id, - 110, - 6, - "ALLOW", - "0.0.0.0/0", - False, - port_range_from="443", - port_range_to="443", - ) - - all_network_acls = conn.get_all_network_acls() - all_network_acls.should.have.length_of(3) - - test_network_acl = next(na for na in all_network_acls if na.id == network_acl.id) - entries = test_network_acl.network_acl_entries - entries.should.have.length_of(1) - entries[0].rule_number.should.equal("110") - entries[0].protocol.should.equal("6") - entries[0].rule_action.should.equal("ALLOW") - - @mock_ec2 def test_network_acl_entries_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -155,33 +90,6 @@ def test_network_acl_entries_boto3(): entries[0]["CidrBlock"].should.equal("0.0.0.0/0") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_delete_network_acl_entry(): - conn = boto.connect_vpc("the_key", "the secret") - vpc = conn.create_vpc("10.0.0.0/16") - - network_acl = conn.create_network_acl(vpc.id) - - conn.create_network_acl_entry( - network_acl.id, - 110, - 6, - "ALLOW", - "0.0.0.0/0", - False, - port_range_from="443", - port_range_to="443", - ) - conn.delete_network_acl_entry(network_acl.id, 110, False) - - all_network_acls = conn.get_all_network_acls() - - test_network_acl = next(na for na in all_network_acls if na.id == network_acl.id) - entries = test_network_acl.network_acl_entries - entries.should.have.length_of(0) - - @mock_ec2 def test_delete_network_acl_entry_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -211,45 +119,6 @@ def test_delete_network_acl_entry_boto3(): test_network_acl["Entries"].should.have.length_of(0) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_replace_network_acl_entry(): - conn = boto.connect_vpc("the_key", "the secret") - vpc = conn.create_vpc("10.0.0.0/16") - - network_acl = conn.create_network_acl(vpc.id) - - conn.create_network_acl_entry( - network_acl.id, - 110, - 6, - "ALLOW", - "0.0.0.0/0", - False, - port_range_from="443", - port_range_to="443", - ) - conn.replace_network_acl_entry( - network_acl.id, - 110, - -1, - "DENY", - "0.0.0.0/0", - False, - port_range_from="22", - port_range_to="22", - ) - - all_network_acls = conn.get_all_network_acls() - - test_network_acl = next(na for na in all_network_acls if na.id == network_acl.id) - entries = test_network_acl.network_acl_entries - entries.should.have.length_of(1) - entries[0].rule_number.should.equal("110") - entries[0].protocol.should.equal("-1") - entries[0].rule_action.should.equal("DENY") - - @mock_ec2 def test_replace_network_acl_entry_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -290,46 +159,6 @@ def test_replace_network_acl_entry_boto3(): entries[0]["PortRange"].should.equal({"To": 22, "From": 22}) -# TODO: How to convert 'associate_network_acl' to boto3? -@mock_ec2_deprecated -def test_associate_new_network_acl_with_subnet(): - conn = boto.connect_vpc("the_key", "the secret") - vpc = conn.create_vpc("10.0.0.0/16") - subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") - network_acl = conn.create_network_acl(vpc.id) - - conn.associate_network_acl(network_acl.id, subnet.id) - - all_network_acls = conn.get_all_network_acls() - all_network_acls.should.have.length_of(3) - - test_network_acl = next(na for na in all_network_acls if na.id == network_acl.id) - - test_network_acl.associations.should.have.length_of(1) - test_network_acl.associations[0].subnet_id.should.equal(subnet.id) - - -# Has boto3 equivalent -@mock_ec2_deprecated -def test_delete_network_acl(): - conn = boto.connect_vpc("the_key", "the secret") - vpc = conn.create_vpc("10.0.0.0/16") - conn.create_subnet(vpc.id, "10.0.0.0/18") - network_acl = conn.create_network_acl(vpc.id) - - all_network_acls = conn.get_all_network_acls() - all_network_acls.should.have.length_of(3) - - any(acl.id == network_acl.id for acl in all_network_acls).should.be.ok - - conn.delete_network_acl(network_acl.id) - - updated_network_acls = conn.get_all_network_acls() - updated_network_acls.should.have.length_of(2) - - any(acl.id == network_acl.id for acl in updated_network_acls).shouldnt.be.ok - - @mock_ec2 def test_delete_network_acl_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -350,25 +179,6 @@ def test_delete_network_acl_boto3(): ).shouldnt.be.ok -# Has boto3 equivalent -@mock_ec2_deprecated -def test_network_acl_tagging(): - conn = boto.connect_vpc("the_key", "the secret") - vpc = conn.create_vpc("10.0.0.0/16") - network_acl = conn.create_network_acl(vpc.id) - - network_acl.add_tag("a key", "some value") - - tag = conn.get_all_tags()[0] - tag.name.should.equal("a key") - tag.value.should.equal("some value") - - all_network_acls = conn.get_all_network_acls() - test_network_acl = next(na for na in all_network_acls if na.id == network_acl.id) - test_network_acl.tags.should.have.length_of(1) - test_network_acl.tags["a key"].should.equal("some value") - - @mock_ec2 def test_network_acl_tagging_boto3(): client = boto3.client("ec2", region_name="us-east-1") diff --git a/tests/test_ec2/test_regions.py b/tests/test_ec2/test_regions.py index 59e8493a4..2b76f7b5d 100644 --- a/tests/test_ec2/test_regions.py +++ b/tests/test_ec2/test_regions.py @@ -1,46 +1,20 @@ -import boto.ec2 -import boto.ec2.autoscale -import boto.ec2.elb import boto3 import pytest import sure # noqa # pylint: disable=unused-import from botocore.exceptions import ClientError -from moto import mock_ec2_deprecated, mock_autoscaling_deprecated, mock_elb_deprecated from moto import mock_autoscaling, mock_ec2, mock_elb -from moto.ec2 import ec2_backends from tests import EXAMPLE_AMI_ID, EXAMPLE_AMI_ID2 from uuid import uuid4 from .test_instances import retrieve_all_instances -def add_servers_to_region(ami_id, count, region): - conn = boto.ec2.connect_to_region(region) - for _ in range(count): - conn.run_instances(ami_id) - - def add_servers_to_region_boto3(ami_id, count, region): ec2 = boto3.resource("ec2", region_name=region) return ec2.create_instances(ImageId=ami_id, MinCount=count, MaxCount=count) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_add_servers_to_a_single_region(): - region = "ap-northeast-1" - add_servers_to_region(EXAMPLE_AMI_ID, 1, region) - add_servers_to_region(EXAMPLE_AMI_ID2, 1, region) - - conn = boto.ec2.connect_to_region(region) - reservations = conn.get_all_reservations() - len(reservations).should.equal(2) - - image_ids = [r.instances[0].image_id for r in reservations] - image_ids.should.equal([EXAMPLE_AMI_ID, EXAMPLE_AMI_ID2]) - - @mock_ec2 def test_add_servers_to_a_single_region_boto3(): region = "ap-northeast-1" @@ -56,26 +30,6 @@ def test_add_servers_to_a_single_region_boto3(): instance2["ImageId"].should.equal(EXAMPLE_AMI_ID2) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_add_servers_to_multiple_regions(): - region1 = "us-east-1" - region2 = "ap-northeast-1" - add_servers_to_region(EXAMPLE_AMI_ID, 1, region1) - add_servers_to_region(EXAMPLE_AMI_ID2, 1, region2) - - us_conn = boto.ec2.connect_to_region(region1) - ap_conn = boto.ec2.connect_to_region(region2) - us_reservations = us_conn.get_all_reservations() - ap_reservations = ap_conn.get_all_reservations() - - len(us_reservations).should.equal(1) - len(ap_reservations).should.equal(1) - - us_reservations[0].instances[0].image_id.should.equal(EXAMPLE_AMI_ID) - ap_reservations[0].instances[0].image_id.should.equal(EXAMPLE_AMI_ID2) - - @mock_ec2 def test_add_servers_to_multiple_regions_boto3(): region1 = "us-east-1" @@ -103,107 +57,6 @@ def test_add_servers_to_multiple_regions_boto3(): ap_instance["ImageId"].should.equal(EXAMPLE_AMI_ID2) -# Has boto3 equivalent -@mock_autoscaling_deprecated -@mock_elb_deprecated -def test_create_autoscaling_group(): - elb_conn = boto.ec2.elb.connect_to_region("us-east-1") - elb_conn.create_load_balancer( - "us_test_lb", zones=[], listeners=[(80, 8080, "http")] - ) - elb_conn = boto.ec2.elb.connect_to_region("ap-northeast-1") - elb_conn.create_load_balancer( - "ap_test_lb", zones=[], listeners=[(80, 8080, "http")] - ) - - us_conn = boto.ec2.autoscale.connect_to_region("us-east-1") - config = boto.ec2.autoscale.LaunchConfiguration( - name="us_tester", image_id=EXAMPLE_AMI_ID, instance_type="m1.small" - ) - us_conn.create_launch_configuration(config) - - us_subnet_id = list(ec2_backends["us-east-1"].subnets["us-east-1c"].keys())[0] - ap_subnet_id = list( - ec2_backends["ap-northeast-1"].subnets["ap-northeast-1a"].keys() - )[0] - group = boto.ec2.autoscale.AutoScalingGroup( - name="us_tester_group", - availability_zones=["us-east-1c"], - default_cooldown=60, - desired_capacity=2, - health_check_period=100, - health_check_type="EC2", - max_size=2, - min_size=2, - launch_config=config, - load_balancers=["us_test_lb"], - placement_group="us_test_placement", - vpc_zone_identifier=us_subnet_id, - termination_policies=["OldestInstance", "NewestInstance"], - ) - us_conn.create_auto_scaling_group(group) - - ap_conn = boto.ec2.autoscale.connect_to_region("ap-northeast-1") - config = boto.ec2.autoscale.LaunchConfiguration( - name="ap_tester", image_id=EXAMPLE_AMI_ID, instance_type="m1.small" - ) - ap_conn.create_launch_configuration(config) - - group = boto.ec2.autoscale.AutoScalingGroup( - name="ap_tester_group", - availability_zones=["ap-northeast-1a"], - default_cooldown=60, - desired_capacity=2, - health_check_period=100, - health_check_type="EC2", - max_size=2, - min_size=2, - launch_config=config, - load_balancers=["ap_test_lb"], - placement_group="ap_test_placement", - vpc_zone_identifier=ap_subnet_id, - termination_policies=["OldestInstance", "NewestInstance"], - ) - ap_conn.create_auto_scaling_group(group) - - len(us_conn.get_all_groups()).should.equal(1) - len(ap_conn.get_all_groups()).should.equal(1) - - us_group = us_conn.get_all_groups()[0] - us_group.name.should.equal("us_tester_group") - list(us_group.availability_zones).should.equal(["us-east-1c"]) - us_group.desired_capacity.should.equal(2) - us_group.max_size.should.equal(2) - us_group.min_size.should.equal(2) - us_group.vpc_zone_identifier.should.equal(us_subnet_id) - us_group.launch_config_name.should.equal("us_tester") - us_group.default_cooldown.should.equal(60) - us_group.health_check_period.should.equal(100) - us_group.health_check_type.should.equal("EC2") - list(us_group.load_balancers).should.equal(["us_test_lb"]) - us_group.placement_group.should.equal("us_test_placement") - list(us_group.termination_policies).should.equal( - ["OldestInstance", "NewestInstance"] - ) - - ap_group = ap_conn.get_all_groups()[0] - ap_group.name.should.equal("ap_tester_group") - list(ap_group.availability_zones).should.equal(["ap-northeast-1a"]) - ap_group.desired_capacity.should.equal(2) - ap_group.max_size.should.equal(2) - ap_group.min_size.should.equal(2) - ap_group.vpc_zone_identifier.should.equal(ap_subnet_id) - ap_group.launch_config_name.should.equal("ap_tester") - ap_group.default_cooldown.should.equal(60) - ap_group.health_check_period.should.equal(100) - ap_group.health_check_type.should.equal("EC2") - list(ap_group.load_balancers).should.equal(["ap_test_lb"]) - ap_group.placement_group.should.equal("ap_test_placement") - list(ap_group.termination_policies).should.equal( - ["OldestInstance", "NewestInstance"] - ) - - @mock_autoscaling @mock_elb @mock_ec2 diff --git a/tests/test_ec2/test_route_tables.py b/tests/test_ec2/test_route_tables.py index 98e4e72e8..f45735e1b 100644 --- a/tests/test_ec2/test_route_tables.py +++ b/tests/test_ec2/test_route_tables.py @@ -1,43 +1,14 @@ import pytest -import boto import boto3 -from boto.exception import EC2ResponseError from botocore.exceptions import ClientError import sure # noqa # pylint: disable=unused-import -from moto import mock_ec2, mock_ec2_deprecated, settings +from moto import mock_ec2, settings from tests import EXAMPLE_AMI_ID -from tests.helpers import requires_boto_gte from uuid import uuid4 -# Has boto3 equivalent -@mock_ec2_deprecated -def test_route_tables_defaults(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - - all_route_tables = conn.get_all_route_tables(filters={"vpc-id": vpc.id}) - all_route_tables.should.have.length_of(1) - - main_route_table = all_route_tables[0] - main_route_table.vpc_id.should.equal(vpc.id) - - routes = main_route_table.routes - routes.should.have.length_of(1) - - local_route = routes[0] - local_route.gateway_id.should.equal("local") - local_route.state.should.equal("active") - local_route.destination_cidr_block.should.equal(vpc.cidr_block) - - vpc.delete() - - all_route_tables = conn.get_all_route_tables(filters={"vpc-id": vpc.id}) - all_route_tables.should.have.length_of(0) - - @mock_ec2 def test_route_tables_defaults_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -68,47 +39,6 @@ def test_route_tables_defaults_boto3(): all_route_tables.should.have.length_of(0) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_route_tables_additional(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - route_table = conn.create_route_table(vpc.id) - - all_route_tables = conn.get_all_route_tables(filters={"vpc-id": vpc.id}) - all_route_tables.should.have.length_of(2) - all_route_tables[0].vpc_id.should.equal(vpc.id) - all_route_tables[1].vpc_id.should.equal(vpc.id) - - all_route_table_ids = [route_table.id for route_table in all_route_tables] - all_route_table_ids.should.contain(route_table.id) - - routes = route_table.routes - routes.should.have.length_of(1) - - local_route = routes[0] - local_route.gateway_id.should.equal("local") - local_route.state.should.equal("active") - local_route.destination_cidr_block.should.equal(vpc.cidr_block) - - with pytest.raises(EC2ResponseError) as cm: - conn.delete_vpc(vpc.id) - cm.value.code.should.equal("DependencyViolation") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - conn.delete_route_table(route_table.id) - - all_route_tables = conn.get_all_route_tables(filters={"vpc-id": vpc.id}) - all_route_tables.should.have.length_of(1) - - with pytest.raises(EC2ResponseError) as cm: - conn.delete_route_table("rtb-1234abcd") - cm.value.code.should.equal("InvalidRouteTableID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_route_tables_additional_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -154,51 +84,6 @@ def test_route_tables_additional_boto3(): ex.value.response["ResponseMetadata"].should.have.key("RequestId") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_route_tables_filters_standard(): - conn = boto.connect_vpc("the_key", "the_secret") - - vpc1 = conn.create_vpc("10.0.0.0/16") - route_table1 = conn.create_route_table(vpc1.id) - - vpc2 = conn.create_vpc("10.0.0.0/16") - route_table2 = conn.create_route_table(vpc2.id) - - all_route_tables = conn.get_all_route_tables() - all_route_tables.should.have.length_of(5) - - # Filter by main route table - main_route_tables = conn.get_all_route_tables(filters={"association.main": "true"}) - main_route_tables.should.have.length_of(3) - main_route_table_ids = [route_table.id for route_table in main_route_tables] - main_route_table_ids.should_not.contain(route_table1.id) - main_route_table_ids.should_not.contain(route_table2.id) - - # Filter by VPC - vpc1_route_tables = conn.get_all_route_tables(filters={"vpc-id": vpc1.id}) - vpc1_route_tables.should.have.length_of(2) - vpc1_route_table_ids = [route_table.id for route_table in vpc1_route_tables] - vpc1_route_table_ids.should.contain(route_table1.id) - vpc1_route_table_ids.should_not.contain(route_table2.id) - - # Filter by VPC and main route table - vpc2_main_route_tables = conn.get_all_route_tables( - filters={"association.main": "true", "vpc-id": vpc2.id} - ) - vpc2_main_route_tables.should.have.length_of(1) - vpc2_main_route_table_ids = [ - route_table.id for route_table in vpc2_main_route_tables - ] - vpc2_main_route_table_ids.should_not.contain(route_table1.id) - vpc2_main_route_table_ids.should_not.contain(route_table2.id) - - # Unsupported filter - conn.get_all_route_tables.when.called_with( - filters={"not-implemented-filter": "foobar"} - ).should.throw(NotImplementedError) - - @mock_ec2 def test_route_tables_filters_standard_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -259,50 +144,6 @@ def test_route_tables_filters_standard_boto3(): ) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_route_tables_filters_associations(): - conn = boto.connect_vpc("the_key", "the_secret") - - vpc = conn.create_vpc("10.0.0.0/16") - subnet1 = conn.create_subnet(vpc.id, "10.0.0.0/24") - subnet2 = conn.create_subnet(vpc.id, "10.0.1.0/24") - subnet3 = conn.create_subnet(vpc.id, "10.0.2.0/24") - route_table1 = conn.create_route_table(vpc.id) - route_table2 = conn.create_route_table(vpc.id) - - association_id1 = conn.associate_route_table(route_table1.id, subnet1.id) - conn.associate_route_table(route_table1.id, subnet2.id) - conn.associate_route_table(route_table2.id, subnet3.id) - - all_route_tables = conn.get_all_route_tables() - all_route_tables.should.have.length_of(4) - - # Filter by association ID - association1_route_tables = conn.get_all_route_tables( - filters={"association.route-table-association-id": association_id1} - ) - association1_route_tables.should.have.length_of(1) - association1_route_tables[0].id.should.equal(route_table1.id) - association1_route_tables[0].associations.should.have.length_of(3) - - # Filter by route table ID - route_table2_route_tables = conn.get_all_route_tables( - filters={"association.route-table-id": route_table2.id} - ) - route_table2_route_tables.should.have.length_of(1) - route_table2_route_tables[0].id.should.equal(route_table2.id) - route_table2_route_tables[0].associations.should.have.length_of(2) - - # Filter by subnet ID - subnet_route_tables = conn.get_all_route_tables( - filters={"association.subnet-id": subnet1.id} - ) - subnet_route_tables.should.have.length_of(1) - subnet_route_tables[0].id.should.equal(route_table1.id) - association1_route_tables[0].associations.should.have.length_of(3) - - @mock_ec2 def test_route_tables_filters_associations_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -351,73 +192,6 @@ def test_route_tables_filters_associations_boto3(): subnet_route_tables[0]["Associations"].should.have.length_of(3) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_route_table_associations(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") - route_table = conn.create_route_table(vpc.id) - - all_route_tables = conn.get_all_route_tables() - all_route_tables.should.have.length_of(3) - - # Refresh - route_table = conn.get_all_route_tables(route_table.id)[0] - route_table.associations.should.have.length_of(1) - - # Associate - association_id = conn.associate_route_table(route_table.id, subnet.id) - - # Refresh - route_table = conn.get_all_route_tables(route_table.id)[0] - route_table.associations.should.have.length_of(2) - - route_table.associations[1].id.should.equal(association_id) - route_table.associations[1].main.should.equal(False) - route_table.associations[1].route_table_id.should.equal(route_table.id) - route_table.associations[1].subnet_id.should.equal(subnet.id) - - # Associate is idempotent - association_id_idempotent = conn.associate_route_table(route_table.id, subnet.id) - association_id_idempotent.should.equal(association_id) - - # Error: Attempt delete associated route table. - with pytest.raises(EC2ResponseError) as cm: - conn.delete_route_table(route_table.id) - cm.value.code.should.equal("DependencyViolation") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - # Disassociate - conn.disassociate_route_table(association_id) - - # Refresh - route_table = conn.get_all_route_tables(route_table.id)[0] - route_table.associations.should.have.length_of(1) - - # Error: Disassociate with invalid association ID - with pytest.raises(EC2ResponseError) as cm: - conn.disassociate_route_table(association_id) - cm.value.code.should.equal("InvalidAssociationID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - # Error: Associate with invalid subnet ID - with pytest.raises(EC2ResponseError) as cm: - conn.associate_route_table(route_table.id, "subnet-1234abcd") - cm.value.code.should.equal("InvalidSubnetID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - # Error: Associate with invalid route table ID - with pytest.raises(EC2ResponseError) as cm: - conn.associate_route_table("rtb-1234abcd", subnet.id) - cm.value.code.should.equal("InvalidRouteTableID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_route_table_associations_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -497,84 +271,6 @@ def test_route_table_associations_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidRouteTableID.NotFound") -# Has boto3 equivalent -@requires_boto_gte("2.16.0") -@mock_ec2_deprecated -def test_route_table_replace_route_table_association(): - """ - Note: Boto has deprecated replace_route_table_association (which returns status) - and now uses replace_route_table_association_with_assoc (which returns association ID). - """ - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") - route_table1 = conn.create_route_table(vpc.id) - route_table2 = conn.create_route_table(vpc.id) - - all_route_tables = conn.get_all_route_tables() - all_route_tables.should.have.length_of(4) - - # Refresh - route_table1 = conn.get_all_route_tables(route_table1.id)[0] - route_table1.associations.should.have.length_of(1) - - # Associate - association_id1 = conn.associate_route_table(route_table1.id, subnet.id) - - # Refresh - route_table1 = conn.get_all_route_tables(route_table1.id)[0] - route_table2 = conn.get_all_route_tables(route_table2.id)[0] - - # Validate - route_table1.associations.should.have.length_of(2) - route_table2.associations.should.have.length_of(1) - - route_table1.associations[1].id.should.equal(association_id1) - route_table1.associations[1].main.should.equal(False) - route_table1.associations[1].route_table_id.should.equal(route_table1.id) - route_table1.associations[1].subnet_id.should.equal(subnet.id) - - # Replace Association - association_id2 = conn.replace_route_table_association_with_assoc( - association_id1, route_table2.id - ) - - # Refresh - route_table1 = conn.get_all_route_tables(route_table1.id)[0] - route_table2 = conn.get_all_route_tables(route_table2.id)[0] - - # Validate - route_table1.associations.should.have.length_of(1) - route_table2.associations.should.have.length_of(2) - - route_table2.associations[1].id.should.equal(association_id2) - route_table2.associations[1].main.should.equal(False) - route_table2.associations[1].route_table_id.should.equal(route_table2.id) - route_table2.associations[1].subnet_id.should.equal(subnet.id) - - # Replace Association is idempotent - association_id_idempotent = conn.replace_route_table_association_with_assoc( - association_id2, route_table2.id - ) - association_id_idempotent.should.equal(association_id2) - - # Error: Replace association with invalid association ID - with pytest.raises(EC2ResponseError) as cm: - conn.replace_route_table_association_with_assoc( - "rtbassoc-1234abcd", route_table1.id - ) - cm.value.code.should.equal("InvalidAssociationID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - # Error: Replace association with invalid route table ID - with pytest.raises(EC2ResponseError) as cm: - conn.replace_route_table_association_with_assoc(association_id2, "rtb-1234abcd") - cm.value.code.should.equal("InvalidRouteTableID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_route_table_replace_route_table_association_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -669,25 +365,6 @@ def test_route_table_replace_route_table_association_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidRouteTableID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_route_table_get_by_tag(): - conn = boto.connect_vpc("the_key", "the_secret") - - vpc = conn.create_vpc("10.0.0.0/16") - - route_table = conn.create_route_table(vpc.id) - route_table.add_tag("Name", "TestRouteTable") - - route_tables = conn.get_all_route_tables(filters={"tag:Name": "TestRouteTable"}) - - route_tables.should.have.length_of(1) - route_tables[0].vpc_id.should.equal(vpc.id) - route_tables[0].id.should.equal(route_table.id) - route_tables[0].tags.should.have.length_of(1) - route_tables[0].tags["Name"].should.equal("TestRouteTable") - - @mock_ec2 def test_route_table_get_by_tag_boto3(): ec2 = boto3.resource("ec2", region_name="eu-central-1") @@ -708,57 +385,6 @@ def test_route_table_get_by_tag_boto3(): route_tables[0].tags[0].should.equal({"Key": "Name", "Value": tag_value}) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_routes_additional(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - main_route_table = conn.get_all_route_tables(filters={"vpc-id": vpc.id})[0] - - igw = conn.create_internet_gateway() - ROUTE_CIDR = "10.0.0.4/24" - - conn.create_route(main_route_table.id, ROUTE_CIDR, gateway_id=igw.id) - - main_route_table = conn.get_all_route_tables(filters={"vpc-id": vpc.id})[ - 0 - ] # Refresh route table - - main_route_table.routes.should.have.length_of(2) - new_routes = [ - route - for route in main_route_table.routes - if route.destination_cidr_block != vpc.cidr_block - ] - new_routes.should.have.length_of(1) - - new_route = new_routes[0] - new_route.gateway_id.should.equal(igw.id) - new_route.instance_id.should.be.none - new_route.state.should.equal("active") - new_route.destination_cidr_block.should.equal(ROUTE_CIDR) - - conn.delete_route(main_route_table.id, ROUTE_CIDR) - - main_route_table = conn.get_all_route_tables(filters={"vpc-id": vpc.id})[ - 0 - ] # Refresh route table - - main_route_table.routes.should.have.length_of(1) - new_routes = [ - route - for route in main_route_table.routes - if route.destination_cidr_block != vpc.cidr_block - ] - new_routes.should.have.length_of(0) - - with pytest.raises(EC2ResponseError) as cm: - conn.delete_route(main_route_table.id, ROUTE_CIDR) - cm.value.code.should.equal("InvalidRoute.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_routes_additional_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -812,60 +438,6 @@ def test_routes_additional_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidRoute.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_routes_replace(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - main_route_table = conn.get_all_route_tables( - filters={"association.main": "true", "vpc-id": vpc.id} - )[0] - - ROUTE_CIDR = "10.0.0.4/24" - - # Various route targets - igw = conn.create_internet_gateway() - - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - # Create initial route - conn.create_route(main_route_table.id, ROUTE_CIDR, gateway_id=igw.id) - - # Replace... - def get_target_route(): - route_table = conn.get_all_route_tables(main_route_table.id)[0] - routes = [ - route - for route in route_table.routes - if route.destination_cidr_block != vpc.cidr_block - ] - routes.should.have.length_of(1) - return routes[0] - - conn.replace_route(main_route_table.id, ROUTE_CIDR, instance_id=instance.id) - - target_route = get_target_route() - target_route.gateway_id.should.be.none - target_route.instance_id.should.equal(instance.id) - target_route.state.should.equal("active") - target_route.destination_cidr_block.should.equal(ROUTE_CIDR) - - conn.replace_route(main_route_table.id, ROUTE_CIDR, gateway_id=igw.id) - - target_route = get_target_route() - target_route.gateway_id.should.equal(igw.id) - target_route.instance_id.should.be.none - target_route.state.should.equal("active") - target_route.destination_cidr_block.should.equal(ROUTE_CIDR) - - with pytest.raises(EC2ResponseError) as cm: - conn.replace_route("rtb-1234abcd", ROUTE_CIDR, gateway_id=igw.id) - cm.value.code.should.equal("InvalidRouteTableID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_routes_replace_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -937,30 +509,6 @@ def test_routes_replace_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidRouteTableID.NotFound") -# Has boto3 equivalent -@requires_boto_gte("2.19.0") -@mock_ec2_deprecated -def test_routes_not_supported(): - conn = boto.connect_vpc("the_key", "the_secret") - conn.create_vpc("10.0.0.0/16") - main_route_table = conn.get_all_route_tables()[0] - - conn.create_internet_gateway() - ROUTE_CIDR = "10.0.0.4/24" - - # Create - conn.create_route.when.called_with( - main_route_table.id, ROUTE_CIDR, interface_id="eni-1234abcd" - ).should.throw("InvalidNetworkInterfaceID.NotFound") - - # Replace - igw = conn.create_internet_gateway() - conn.create_route(main_route_table.id, ROUTE_CIDR, gateway_id=igw.id) - conn.replace_route.when.called_with( - main_route_table.id, ROUTE_CIDR, interface_id="eni-1234abcd" - ).should.throw(NotImplementedError) - - @mock_ec2 def test_routes_not_supported_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -1002,42 +550,6 @@ def test_routes_not_supported_boto3(): client.replace_route.when.called_with(**args).should.throw(NotImplementedError) -# Has boto3 equivalent -@requires_boto_gte("2.34.0") -@mock_ec2_deprecated -def test_routes_vpc_peering_connection(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - main_route_table = conn.get_all_route_tables( - filters={"association.main": "true", "vpc-id": vpc.id} - )[0] - - ROUTE_CIDR = "10.0.0.4/24" - - peer_vpc = conn.create_vpc("11.0.0.0/16") - vpc_pcx = conn.create_vpc_peering_connection(vpc.id, peer_vpc.id) - - conn.create_route( - main_route_table.id, ROUTE_CIDR, vpc_peering_connection_id=vpc_pcx.id - ) - - # Refresh route table - main_route_table = conn.get_all_route_tables(main_route_table.id)[0] - new_routes = [ - route - for route in main_route_table.routes - if route.destination_cidr_block != vpc.cidr_block - ] - new_routes.should.have.length_of(1) - - new_route = new_routes[0] - new_route.gateway_id.should.be.none - new_route.instance_id.should.be.none - new_route.vpc_peering_connection_id.should.equal(vpc_pcx.id) - new_route.state.should.equal("active") - new_route.destination_cidr_block.should.equal(ROUTE_CIDR) - - @mock_ec2 def test_routes_vpc_peering_connection_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -1076,36 +588,6 @@ def test_routes_vpc_peering_connection_boto3(): new_route.destination_cidr_block.should.equal(ROUTE_CIDR) -# Has boto3 equivalent -@requires_boto_gte("2.34.0") -@mock_ec2_deprecated -def test_routes_vpn_gateway(): - - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - main_route_table = conn.get_all_route_tables( - filters={"association.main": "true", "vpc-id": vpc.id} - )[0] - ROUTE_CIDR = "10.0.0.4/24" - - vpn_gw = conn.create_vpn_gateway(type="ipsec.1") - - conn.create_route(main_route_table.id, ROUTE_CIDR, gateway_id=vpn_gw.id) - - main_route_table = conn.get_all_route_tables(main_route_table.id)[0] - new_routes = [ - route - for route in main_route_table.routes - if route.destination_cidr_block != vpc.cidr_block - ] - new_routes.should.have.length_of(1) - - new_route = new_routes[0] - new_route.gateway_id.should.equal(vpn_gw.id) - new_route.instance_id.should.be.none - new_route.vpc_peering_connection_id.should.be.none - - @mock_ec2 def test_routes_vpn_gateway_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -1139,26 +621,6 @@ def test_routes_vpn_gateway_boto3(): new_route.vpc_peering_connection_id.should.be.none -# Has boto3 equivalent -@mock_ec2_deprecated -def test_network_acl_tagging(): - - conn = boto.connect_vpc("the_key", "the secret") - vpc = conn.create_vpc("10.0.0.0/16") - - route_table = conn.create_route_table(vpc.id) - route_table.add_tag("a key", "some value") - - tag = conn.get_all_tags()[0] - tag.name.should.equal("a key") - tag.value.should.equal("some value") - - all_route_tables = conn.get_all_route_tables() - test_route_table = next(na for na in all_route_tables if na.id == route_table.id) - test_route_table.tags.should.have.length_of(1) - test_route_table.tags["a key"].should.equal("some value") - - @mock_ec2 def test_network_acl_tagging_boto3(): client = boto3.client("ec2", region_name="us-east-1") diff --git a/tests/test_ec2/test_security_groups.py b/tests/test_ec2/test_security_groups.py index dddacf1c3..68c6d7a05 100644 --- a/tests/test_ec2/test_security_groups.py +++ b/tests/test_ec2/test_security_groups.py @@ -5,57 +5,16 @@ import unittest import pytest import boto3 -import boto -import boto.ec2 from botocore.exceptions import ClientError -from boto.exception import EC2ResponseError import sure # noqa # pylint: disable=unused-import -from moto import mock_ec2, mock_ec2_deprecated, settings +from moto import mock_ec2, settings from moto.ec2 import ec2_backend from random import randint from uuid import uuid4 from unittest import SkipTest -# Has boto3 equivalent -@mock_ec2_deprecated -def test_create_and_describe_security_group(): - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as ex: - security_group = conn.create_security_group( - "test security group", "this is a test security group", dry_run=True - ) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the CreateSecurityGroup operation: Request would have succeeded, but DryRun flag is set" - ) - - security_group = conn.create_security_group( - "test security group", "this is a test security group" - ) - - security_group.name.should.equal("test security group") - security_group.description.should.equal("this is a test security group") - - # Trying to create another group with the same name should throw an error - with pytest.raises(EC2ResponseError) as cm: - conn.create_security_group( - "test security group", "this is a test security group" - ) - cm.value.code.should.equal("InvalidGroup.Duplicate") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - all_groups = conn.get_all_security_groups() - # The default group gets created automatically - all_groups.should.have.length_of(2) - group_names = [group.name for group in all_groups] - set(group_names).should.equal(set(["default", "test security group"])) - - @mock_ec2 def test_create_and_describe_security_group_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -90,18 +49,6 @@ def test_create_and_describe_security_group_boto3(): group_names.should.contain(sec_name) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_create_security_group_without_description_raises_error(): - conn = boto.connect_ec2("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as cm: - conn.create_security_group("test security group", "") - cm.value.code.should.equal("MissingParameter") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_create_security_group_without_description_raises_error_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -113,15 +60,6 @@ def test_create_security_group_without_description_raises_error_boto3(): ex.value.response["Error"]["Code"].should.equal("MissingParameter") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_default_security_group(): - conn = boto.ec2.connect_to_region("us-east-1") - groups = conn.get_all_security_groups() - groups.should.have.length_of(1) - groups[0].name.should.equal("default") - - @mock_ec2 def test_default_security_group_boto3(): client = boto3.client("ec2", "us-west-1") @@ -129,38 +67,6 @@ def test_default_security_group_boto3(): [g["GroupName"] for g in groups].should.contain("default") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_create_and_describe_vpc_security_group(): - conn = boto.connect_ec2("the_key", "the_secret") - vpc_id = "vpc-5300000c" - security_group = conn.create_security_group( - "test security group", "this is a test security group", vpc_id=vpc_id - ) - - security_group.vpc_id.should.equal(vpc_id) - - security_group.name.should.equal("test security group") - security_group.description.should.equal("this is a test security group") - - # Trying to create another group with the same name in the same VPC should - # throw an error - with pytest.raises(EC2ResponseError) as cm: - conn.create_security_group( - "test security group", "this is a test security group", vpc_id - ) - cm.value.code.should.equal("InvalidGroup.Duplicate") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - all_groups = conn.get_all_security_groups(filters={"vpc-id": [vpc_id]}) - - all_groups[0].vpc_id.should.equal(vpc_id) - - all_groups.should.have.length_of(1) - all_groups[0].name.should.equal("test security group") - - @mock_ec2 def test_create_and_describe_vpc_security_group_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -203,28 +109,6 @@ def test_create_and_describe_vpc_security_group_boto3(): all_groups[0]["GroupName"].should.equal(name) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_create_two_security_groups_with_same_name_in_different_vpc(): - conn = boto.connect_ec2("the_key", "the_secret") - vpc_id = "vpc-5300000c" - vpc_id2 = "vpc-5300000d" - - conn.create_security_group( - "test security group", "this is a test security group", vpc_id - ) - conn.create_security_group( - "test security group", "this is a test security group", vpc_id2 - ) - - all_groups = conn.get_all_security_groups() - - all_groups.should.have.length_of(3) - group_names = [group.name for group in all_groups] - # The default group is created automatically - set(group_names).should.equal(set(["default", "test security group"])) - - @mock_ec2 def test_create_two_security_groups_with_same_name_in_different_vpc_boto3(): ec2 = boto3.resource("ec2", "us-east-1") @@ -259,39 +143,6 @@ def test_create_two_security_groups_in_vpc_with_ipv6_enabled(): security_group.ip_permissions_egress.should.have.length_of(2) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_deleting_security_groups(): - conn = boto.connect_ec2("the_key", "the_secret") - security_group1 = conn.create_security_group("test1", "test1") - conn.create_security_group("test2", "test2") - - conn.get_all_security_groups().should.have.length_of(3) - - # Deleting a group that doesn't exist should throw an error - with pytest.raises(EC2ResponseError) as cm: - conn.delete_security_group("foobar") - cm.value.code.should.equal("InvalidGroup.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - # Delete by name - with pytest.raises(EC2ResponseError) as ex: - conn.delete_security_group("test2", dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the DeleteSecurityGroup operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.delete_security_group("test2") - conn.get_all_security_groups().should.have.length_of(2) - - # Delete by group id - conn.delete_security_group(group_id=security_group1.id) - conn.get_all_security_groups().should.have.length_of(1) - - @mock_ec2 def test_deleting_security_groups_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -334,17 +185,6 @@ def test_deleting_security_groups_boto3(): [g["GroupId"] for g in all_groups].shouldnt.contain(group1.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_delete_security_group_in_vpc(): - conn = boto.connect_ec2("the_key", "the_secret") - vpc_id = "vpc-12345" - security_group1 = conn.create_security_group("test1", "test1", vpc_id) - - # this should not throw an exception - conn.delete_security_group(group_id=security_group1.id) - - @mock_ec2 def test_delete_security_group_in_vpc_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -364,144 +204,6 @@ def test_delete_security_group_in_vpc_boto3(): [g["GroupId"] for g in all_groups].shouldnt.contain(group.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_authorize_ip_range_and_revoke(): - conn = boto.connect_ec2("the_key", "the_secret") - security_group = conn.create_security_group("test", "test") - - with pytest.raises(EC2ResponseError) as ex: - success = security_group.authorize( - ip_protocol="tcp", - from_port="22", - to_port="2222", - cidr_ip="123.123.123.123/32", - dry_run=True, - ) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the GrantSecurityGroupIngress operation: Request would have succeeded, but DryRun flag is set" - ) - - success = security_group.authorize( - ip_protocol="tcp", from_port="22", to_port="2222", cidr_ip="123.123.123.123/32" - ) - assert success.should.be.true - - security_group = conn.get_all_security_groups(groupnames=["test"])[0] - int(security_group.rules[0].to_port).should.equal(2222) - security_group.rules[0].grants[0].cidr_ip.should.equal("123.123.123.123/32") - - # Wrong Cidr should throw error - with pytest.raises(EC2ResponseError) as cm: - security_group.revoke( - ip_protocol="tcp", - from_port="22", - to_port="2222", - cidr_ip="123.123.123.122/32", - ) - cm.value.code.should.equal("InvalidPermission.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - # Actually revoke - with pytest.raises(EC2ResponseError) as ex: - security_group.revoke( - ip_protocol="tcp", - from_port="22", - to_port="2222", - cidr_ip="123.123.123.123/32", - dry_run=True, - ) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the RevokeSecurityGroupIngress operation: Request would have succeeded, but DryRun flag is set" - ) - - security_group.revoke( - ip_protocol="tcp", from_port="22", to_port="2222", cidr_ip="123.123.123.123/32" - ) - - security_group = conn.get_all_security_groups()[0] - security_group.rules.should.have.length_of(0) - - # Test for egress as well - vpc_conn = boto.connect_vpc() - vpc = vpc_conn.create_vpc("10.0.0.0/16") - egress_security_group = conn.create_security_group( - "testegress", "testegress", vpc_id=vpc.id - ) - - with pytest.raises(EC2ResponseError) as ex: - success = conn.authorize_security_group_egress( - egress_security_group.id, - "tcp", - from_port="22", - to_port="2222", - cidr_ip="123.123.123.123/32", - dry_run=True, - ) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the GrantSecurityGroupEgress operation: Request would have succeeded, but DryRun flag is set" - ) - - success = conn.authorize_security_group_egress( - egress_security_group.id, - "tcp", - from_port="22", - to_port="2222", - cidr_ip="123.123.123.123/32", - ) - assert success.should.be.true - egress_security_group = conn.get_all_security_groups(groupnames="testegress")[0] - # There are two egress rules associated with the security group: - # the default outbound rule and the new one - int(egress_security_group.rules_egress[1].to_port).should.equal(2222) - actual_cidr = egress_security_group.rules_egress[1].grants[0].cidr_ip - actual_cidr.should.equal("123.123.123.123/32") - - # Wrong Cidr should throw error - egress_security_group.revoke.when.called_with( - ip_protocol="tcp", from_port="22", to_port="2222", cidr_ip="123.123.123.122/32" - ).should.throw(EC2ResponseError) - - # Actually revoke - with pytest.raises(EC2ResponseError) as ex: - conn.revoke_security_group_egress( - egress_security_group.id, - "tcp", - from_port="22", - to_port="2222", - cidr_ip="123.123.123.123/32", - dry_run=True, - ) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the RevokeSecurityGroupEgress operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.revoke_security_group_egress( - egress_security_group.id, - "tcp", - from_port="22", - to_port="2222", - cidr_ip="123.123.123.123/32", - ) - - egress_security_group = [ - group - for group in conn.get_all_security_groups() - if group.id == egress_security_group.id - ][0] - # There is still the default outbound rule - egress_security_group.rules_egress.should.have.length_of(1) - - @mock_ec2 def test_authorize_ip_range_and_revoke_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -623,51 +325,6 @@ def test_authorize_ip_range_and_revoke_boto3(): egress_security_group["IpPermissionsEgress"].should.have.length_of(1) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_authorize_other_group_and_revoke(): - conn = boto.connect_ec2("the_key", "the_secret") - security_group = conn.create_security_group("test", "test") - other_security_group = conn.create_security_group("other", "other") - wrong_group = conn.create_security_group("wrong", "wrong") - - success = security_group.authorize( - ip_protocol="tcp", - from_port="22", - to_port="2222", - src_group=other_security_group, - ) - assert success.should.be.true - - security_group = [ - group for group in conn.get_all_security_groups() if group.name == "test" - ][0] - int(security_group.rules[0].to_port).should.equal(2222) - security_group.rules[0].grants[0].group_id.should.equal(other_security_group.id) - - # Wrong source group should throw error - with pytest.raises(EC2ResponseError) as cm: - security_group.revoke( - ip_protocol="tcp", from_port="22", to_port="2222", src_group=wrong_group - ) - cm.value.code.should.equal("InvalidPermission.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - # Actually revoke - security_group.revoke( - ip_protocol="tcp", - from_port="22", - to_port="2222", - src_group=other_security_group, - ) - - security_group = [ - group for group in conn.get_all_security_groups() if group.name == "test" - ][0] - security_group.rules.should.have.length_of(0) - - @mock_ec2 def test_authorize_other_group_and_revoke_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -759,47 +416,6 @@ def test_authorize_other_group_egress_and_revoke(): sg01.ip_permissions_egress.should.have.length_of(1) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_authorize_group_in_vpc(): - conn = boto.connect_ec2("the_key", "the_secret") - vpc_id = "vpc-12345" - - # create 2 groups in a vpc - security_group = conn.create_security_group("test1", "test1", vpc_id) - other_security_group = conn.create_security_group("test2", "test2", vpc_id) - - success = security_group.authorize( - ip_protocol="tcp", - from_port="22", - to_port="2222", - src_group=other_security_group, - ) - success.should.be.true - - # Check that the rule is accurate - security_group = [ - group for group in conn.get_all_security_groups() if group.name == "test1" - ][0] - int(security_group.rules[0].to_port).should.equal(2222) - security_group.rules[0].grants[0].group_id.should.equal(other_security_group.id) - - # Now remove the rule - success = security_group.revoke( - ip_protocol="tcp", - from_port="22", - to_port="2222", - src_group=other_security_group, - ) - success.should.be.true - - # And check that it gets revoked - security_group = [ - group for group in conn.get_all_security_groups() if group.name == "test1" - ][0] - security_group.rules.should.have.length_of(0) - - @mock_ec2 def test_authorize_group_in_vpc_boto3(): ec2 = boto3.resource("ec2", "ap-south-1") @@ -851,44 +467,6 @@ def test_authorize_group_in_vpc_boto3(): found_sec_group["IpPermissions"].should.have.length_of(0) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_all_security_groups(): - conn = boto.connect_ec2() - sg1 = conn.create_security_group( - name="test1", description="test1", vpc_id="vpc-mjm05d27" - ) - conn.create_security_group(name="test2", description="test2") - - resp = conn.get_all_security_groups(groupnames=["test1"]) - resp.should.have.length_of(1) - resp[0].id.should.equal(sg1.id) - - with pytest.raises(EC2ResponseError) as cm: - conn.get_all_security_groups(groupnames=["does_not_exist"]) - cm.value.code.should.equal("InvalidGroup.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - resp.should.have.length_of(1) - resp[0].id.should.equal(sg1.id) - - resp = conn.get_all_security_groups(filters={"vpc-id": ["vpc-mjm05d27"]}) - resp.should.have.length_of(1) - resp[0].id.should.equal(sg1.id) - - resp = conn.get_all_security_groups(filters={"vpc-id": ["vpc-mjm05d27"]}) - resp.should.have.length_of(1) - resp[0].id.should.equal(sg1.id) - - resp = conn.get_all_security_groups(filters={"description": ["test1"]}) - resp.should.have.length_of(1) - resp[0].id.should.equal(sg1.id) - - resp = conn.get_all_security_groups() - resp.should.have.length_of(3) - - @mock_ec2 def test_describe_security_groups(): ec2 = boto3.resource("ec2", "us-west-1") @@ -927,20 +505,6 @@ def test_describe_security_groups(): sg_ids.should.contain(sg2.id) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_authorize_bad_cidr_throws_invalid_parameter_value(): - conn = boto.connect_ec2("the_key", "the_secret") - security_group = conn.create_security_group("test", "test") - with pytest.raises(EC2ResponseError) as cm: - security_group.authorize( - ip_protocol="tcp", from_port="22", to_port="2222", cidr_ip="123.123.123.123" - ) - cm.value.code.should.equal("InvalidParameterValue") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_authorize_bad_cidr_throws_invalid_parameter_value_boto3(): ec2 = boto3.resource("ec2", "us-west-1") @@ -960,44 +524,6 @@ def test_authorize_bad_cidr_throws_invalid_parameter_value_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidParameterValue") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_security_group_tagging(): - conn = boto.connect_vpc() - vpc = conn.create_vpc("10.0.0.0/16") - - sg = conn.create_security_group("test-sg", "Test SG", vpc.id) - - with pytest.raises(EC2ResponseError) as ex: - sg.add_tag("Test", "Tag", dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the CreateTags operation: Request would have succeeded, but DryRun flag is set" - ) - - sg.add_tag("Test", "Tag") - - tag = conn.get_all_tags()[0] - tag.name.should.equal("Test") - tag.value.should.equal("Tag") - - group = conn.get_all_security_groups("test-sg")[0] - group.tags.should.have.length_of(1) - group.tags["Test"].should.equal("Tag") - - -# Has boto3 equivalent -@mock_ec2_deprecated -def test_security_group_tag_filtering(): - conn = boto.connect_ec2() - sg = conn.create_security_group("test-sg", "Test SG") - sg.add_tag("test-tag", "test-value") - - groups = conn.get_all_security_groups(filters={"tag:test-tag": "test-value"}) - groups.should.have.length_of(1) - - @mock_ec2 def test_security_group_tag_filtering_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -1018,20 +544,6 @@ def test_security_group_tag_filtering_boto3(): groups.should.have.length_of(0) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_authorize_all_protocols_with_no_port_specification(): - conn = boto.connect_ec2() - sg = conn.create_security_group("test", "test") - - success = sg.authorize(ip_protocol="-1", cidr_ip="0.0.0.0/0") - success.should.be.true - - sg = conn.get_all_security_groups("test")[0] - sg.rules[0].from_port.should.equal(None) - sg.rules[0].to_port.should.equal(None) - - @mock_ec2 def test_authorize_all_protocols_with_no_port_specification_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -1050,72 +562,6 @@ def test_authorize_all_protocols_with_no_port_specification_boto3(): permission.shouldnt.have.key("ToPort") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_sec_group_rule_limit(): - ec2_conn = boto.connect_ec2() - sg = ec2_conn.create_security_group("test", "test") - other_sg = ec2_conn.create_security_group("test_2", "test_other") - - # INGRESS - with pytest.raises(EC2ResponseError) as cm: - ec2_conn.authorize_security_group( - group_id=sg.id, - ip_protocol="-1", - cidr_ip=["{0}.0.0.0/0".format(i) for i in range(110)], - ) - cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") - - sg.rules.should.be.empty - # authorize a rule targeting a different sec group (because this count too) - success = ec2_conn.authorize_security_group( - group_id=sg.id, ip_protocol="-1", src_security_group_group_id=other_sg.id - ) - success.should.be.true - # fill the rules up the limit - success = ec2_conn.authorize_security_group( - group_id=sg.id, - ip_protocol="-1", - cidr_ip=["{0}.0.0.0/0".format(i) for i in range(1, 60)], - ) - success.should.be.true - # verify that we cannot authorize past the limit for a CIDR IP - with pytest.raises(EC2ResponseError) as cm: - ec2_conn.authorize_security_group( - group_id=sg.id, ip_protocol="-1", cidr_ip=["100.0.0.0/0"] - ) - cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") - # verify that we cannot authorize past the limit for a different sec group - with pytest.raises(EC2ResponseError) as cm: - ec2_conn.authorize_security_group( - group_id=sg.id, ip_protocol="-1", src_security_group_group_id=other_sg.id - ) - cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") - - # EGRESS - # authorize a rule targeting a different sec group (because this count too) - ec2_conn.authorize_security_group_egress( - group_id=sg.id, ip_protocol="-1", src_group_id=other_sg.id - ) - # fill the rules up the limit, 59 + 1 default rule - for i in range(1, 59): - ec2_conn.authorize_security_group_egress( - group_id=sg.id, ip_protocol="-1", cidr_ip="{0}.0.0.0/0".format(i) - ) - # verify that we cannot authorize past the limit for a CIDR IP - with pytest.raises(EC2ResponseError) as cm: - ec2_conn.authorize_security_group_egress( - group_id=sg.id, ip_protocol="-1", cidr_ip="101.0.0.0/0" - ) - cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") - # verify that we cannot authorize past the limit for a different sec group - with pytest.raises(EC2ResponseError) as cm: - ec2_conn.authorize_security_group_egress( - group_id=sg.id, ip_protocol="-1", src_group_id=other_sg.id - ) - cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") - - @mock_ec2 @pytest.mark.parametrize( "use_vpc", [True, False], ids=["Use VPC", "Without VPC"], @@ -1232,83 +678,6 @@ def test_sec_group_rule_limit_boto3(use_vpc): ) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_sec_group_rule_limit_vpc(): - ec2_conn = boto.connect_ec2() - vpc_conn = boto.connect_vpc() - - vpc = vpc_conn.create_vpc("10.0.0.0/16") - - sg = ec2_conn.create_security_group("test", "test", vpc_id=vpc.id) - other_sg = ec2_conn.create_security_group("test_2", "test", vpc_id=vpc.id) - - # INGRESS - with pytest.raises(EC2ResponseError) as cm: - ec2_conn.authorize_security_group( - group_id=sg.id, - ip_protocol="-1", - cidr_ip=["{0}.0.0.0/0".format(i) for i in range(110)], - ) - cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") - - sg.rules.should.be.empty - # authorize a rule targeting a different sec group (because this count too) - success = ec2_conn.authorize_security_group( - group_id=sg.id, ip_protocol="-1", src_security_group_group_id=other_sg.id - ) - success.should.be.true - # fill the rules up the limit - success = ec2_conn.authorize_security_group( - group_id=sg.id, - ip_protocol="-1", - cidr_ip=["{0}.0.0.0/0".format(i) for i in range(59)], - ) - # verify that we cannot authorize past the limit for a CIDR IP - success.should.be.true - with pytest.raises(EC2ResponseError) as cm: - ec2_conn.authorize_security_group( - group_id=sg.id, ip_protocol="-1", cidr_ip=["100.0.0.0/0"] - ) - cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") - # verify that we cannot authorize past the limit for a different sec group - with pytest.raises(EC2ResponseError) as cm: - ec2_conn.authorize_security_group( - group_id=sg.id, ip_protocol="-1", src_security_group_group_id=other_sg.id - ) - cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") - - # EGRESS - # authorize a rule targeting a different sec group (because this count too) - ec2_conn.authorize_security_group_egress( - group_id=sg.id, ip_protocol="-1", src_group_id=other_sg.id - ) - # fill the rules up the limit - # remember that by default, when created a sec group contains 1 egress rule - # so our other_sg rule + 48 CIDR IP rules + 1 by default == 50 the limit - for i in range(1, 59): - ec2_conn.authorize_security_group_egress( - group_id=sg.id, ip_protocol="-1", cidr_ip="{0}.0.0.0/0".format(i) - ) - # verify that we cannot authorize past the limit for a CIDR IP - with pytest.raises(EC2ResponseError) as cm: - ec2_conn.authorize_security_group_egress( - group_id=sg.id, ip_protocol="-1", cidr_ip="50.0.0.0/0" - ) - cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") - # verify that we cannot authorize past the limit for a different sec group - with pytest.raises(EC2ResponseError) as cm: - ec2_conn.authorize_security_group_egress( - group_id=sg.id, ip_protocol="-1", src_group_id=other_sg.id - ) - cm.value.error_code.should.equal("RulesPerSecurityGroupLimitExceeded") - - -""" -Boto3 -""" - - @mock_ec2 def test_add_same_rule_twice_throws_error(): ec2 = boto3.resource("ec2", region_name="us-west-1") @@ -1669,29 +1038,6 @@ def test_security_group_ingress_without_multirule_after_reload(): assert len(sg_after.ip_permissions) == 1 -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_all_security_groups_filter_with_same_vpc_id(): - conn = boto.connect_ec2("the_key", "the_secret") - vpc_id = "vpc-5300000c" - security_group = conn.create_security_group("test1", "test1", vpc_id=vpc_id) - security_group2 = conn.create_security_group("test2", "test2", vpc_id=vpc_id) - - security_group.vpc_id.should.equal(vpc_id) - security_group2.vpc_id.should.equal(vpc_id) - - security_groups = conn.get_all_security_groups( - group_ids=[security_group.id], filters={"vpc-id": [vpc_id]} - ) - security_groups.should.have.length_of(1) - - with pytest.raises(EC2ResponseError) as cm: - conn.get_all_security_groups(group_ids=["does_not_exist"]) - cm.value.code.should.equal("InvalidGroup.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_get_all_security_groups_filter_with_same_vpc_id_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") diff --git a/tests/test_ec2/test_spot_instances.py b/tests/test_ec2/test_spot_instances.py index bc95a5b05..ee145a58c 100644 --- a/tests/test_ec2/test_spot_instances.py +++ b/tests/test_ec2/test_spot_instances.py @@ -1,15 +1,12 @@ import pytest import datetime -import boto -import boto.ec2 import boto3 -from boto.exception import EC2ResponseError from botocore.exceptions import ClientError import pytz import sure # noqa # pylint: disable=unused-import -from moto import mock_ec2, mock_ec2_deprecated, settings +from moto import mock_ec2, settings from moto.ec2.models import ec2_backends from moto.core.utils import iso_8601_datetime_with_milliseconds from tests import EXAMPLE_AMI_ID @@ -155,30 +152,6 @@ def test_request_spot_instances_default_arguments(): request.shouldnt.contain("SubnetId") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_cancel_spot_instance_request(): - conn = boto.connect_ec2() - - conn.request_spot_instances(price=0.5, image_id=EXAMPLE_AMI_ID) - - requests = conn.get_all_spot_instance_requests() - requests.should.have.length_of(1) - - with pytest.raises(EC2ResponseError) as ex: - conn.cancel_spot_instance_requests([requests[0].id], dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the CancelSpotInstance operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.cancel_spot_instance_requests([requests[0].id]) - - requests = conn.get_all_spot_instance_requests() - requests.should.have.length_of(0) - - @mock_ec2 def test_cancel_spot_instance_request_boto3(): client = boto3.client("ec2", region_name="us-west-1") @@ -219,32 +192,6 @@ def test_cancel_spot_instance_request_boto3(): requests.should.have.length_of(0) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_request_spot_instances_fulfilled(): - """ - Test that moto correctly fullfills a spot instance request - """ - conn = boto.ec2.connect_to_region("us-east-1") - - request = conn.request_spot_instances(price=0.5, image_id=EXAMPLE_AMI_ID) - - requests = conn.get_all_spot_instance_requests() - requests.should.have.length_of(1) - request = requests[0] - - request.state.should.equal("open") - - if not settings.TEST_SERVER_MODE: - ec2_backends["us-east-1"].spot_instance_requests[request.id].state = "active" - - requests = conn.get_all_spot_instance_requests() - requests.should.have.length_of(1) - request = requests[0] - - request.state.should.equal("active") - - @mock_ec2 def test_request_spot_instances_fulfilled_boto3(): """ @@ -277,26 +224,6 @@ def test_request_spot_instances_fulfilled_boto3(): request["State"].should.equal("active") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_tag_spot_instance_request(): - """ - Test that moto correctly tags a spot instance request - """ - conn = boto.connect_ec2() - - request = conn.request_spot_instances(price=0.5, image_id=EXAMPLE_AMI_ID) - request[0].add_tag("tag1", "value1") - request[0].add_tag("tag2", "value2") - - requests = conn.get_all_spot_instance_requests() - requests.should.have.length_of(1) - request = requests[0] - - tag_dict = dict(request.tags) - tag_dict.should.equal({"tag1": "value1", "tag2": "value2"}) - - @mock_ec2 def test_tag_spot_instance_request_boto3(): """ @@ -324,37 +251,6 @@ def test_tag_spot_instance_request_boto3(): request["Tags"].should.contain({"Key": "tag2", "Value": "value2"}) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_all_spot_instance_requests_filtering(): - """ - Test that moto correctly filters spot instance requests - """ - conn = boto.connect_ec2() - - request1 = conn.request_spot_instances(price=0.5, image_id=EXAMPLE_AMI_ID) - request2 = conn.request_spot_instances(price=0.5, image_id=EXAMPLE_AMI_ID) - conn.request_spot_instances(price=0.5, image_id=EXAMPLE_AMI_ID) - request1[0].add_tag("tag1", "value1") - request1[0].add_tag("tag2", "value2") - request2[0].add_tag("tag1", "value1") - request2[0].add_tag("tag2", "wrong") - - requests = conn.get_all_spot_instance_requests(filters={"state": "active"}) - requests.should.have.length_of(0) - - requests = conn.get_all_spot_instance_requests(filters={"state": "open"}) - requests.should.have.length_of(3) - - requests = conn.get_all_spot_instance_requests(filters={"tag:tag1": "value1"}) - requests.should.have.length_of(2) - - requests = conn.get_all_spot_instance_requests( - filters={"tag:tag1": "value1", "tag:tag2": "value2"} - ) - requests.should.have.length_of(1) - - @mock_ec2 def test_get_all_spot_instance_requests_filtering_boto3(): """ @@ -411,22 +307,6 @@ def test_get_all_spot_instance_requests_filtering_boto3(): requests.should.have.length_of(1) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_request_spot_instances_setting_instance_id(): - conn = boto.ec2.connect_to_region("us-east-1") - request = conn.request_spot_instances(price=0.5, image_id=EXAMPLE_AMI_ID) - - if not settings.TEST_SERVER_MODE: - req = ec2_backends["us-east-1"].spot_instance_requests[request[0].id] - req.state = "active" - req.instance_id = "i-12345678" - - request = conn.get_all_spot_instance_requests()[0] - assert request.state == "active" - assert request.instance_id == "i-12345678" - - @mock_ec2 def test_request_spot_instances_instance_lifecycle(): if settings.TEST_SERVER_MODE: diff --git a/tests/test_ec2/test_subnets.py b/tests/test_ec2/test_subnets.py index ec7f9fd16..5dbda493c 100644 --- a/tests/test_ec2/test_subnets.py +++ b/tests/test_ec2/test_subnets.py @@ -1,42 +1,16 @@ import random -import boto import boto3 -import boto.vpc import pytest import sure # noqa # pylint: disable=unused-import -from boto.exception import EC2ResponseError from botocore.exceptions import ClientError -from moto import mock_ec2, mock_ec2_deprecated, settings +from moto import mock_ec2, settings from tests import EXAMPLE_AMI_ID from uuid import uuid4 from unittest import SkipTest -# Has boto3 equivalent -@mock_ec2_deprecated -def test_subnets(): - ec2 = boto.connect_ec2("the_key", "the_secret") - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") - - all_subnets = conn.get_all_subnets() - all_subnets.should.have.length_of(1 + len(ec2.get_all_zones())) - - conn.delete_subnet(subnet.id) - - all_subnets = conn.get_all_subnets() - all_subnets.should.have.length_of(0 + len(ec2.get_all_zones())) - - with pytest.raises(EC2ResponseError) as cm: - conn.delete_subnet(subnet.id) - cm.value.code.should.equal("InvalidSubnetID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_subnets_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -61,18 +35,6 @@ def test_subnets_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidSubnetID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_subnet_create_vpc_validation(): - conn = boto.connect_vpc("the_key", "the_secret") - - with pytest.raises(EC2ResponseError) as cm: - conn.create_subnet("vpc-abcd1234", "10.0.0.0/18") - cm.value.code.should.equal("InvalidVpcID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_subnet_create_vpc_validation_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -84,25 +46,6 @@ def test_subnet_create_vpc_validation_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidVpcID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_subnet_tagging(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - subnet = conn.create_subnet(vpc.id, "10.0.0.0/18") - - subnet.add_tag("a key", "some value") - - tag = conn.get_all_tags()[0] - tag.name.should.equal("a key") - tag.value.should.equal("some value") - - # Refresh the subnet - subnet = conn.get_all_subnets(subnet_ids=[subnet.id])[0] - subnet.tags.should.have.length_of(1) - subnet.tags["a key"].should.equal("some value") - - @mock_ec2 def test_subnet_tagging_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -123,15 +66,6 @@ def test_subnet_tagging_boto3(): subnet["Tags"].should.equal([{"Key": "a key", "Value": "some value"}]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_subnet_should_have_proper_availability_zone_set(): - conn = boto.vpc.connect_to_region("us-west-1") - vpcA = conn.create_vpc("10.0.0.0/16") - subnetA = conn.create_subnet(vpcA.id, "10.0.0.0/24", availability_zone="us-west-1b") - subnetA.availability_zone.should.equal("us-west-1b") - - @mock_ec2 def test_subnet_should_have_proper_availability_zone_set_boto3(): ec2 = boto3.resource("ec2", region_name="us-west-1") @@ -172,20 +106,6 @@ def test_default_subnet(): subnet.map_public_ip_on_launch.shouldnt.be.ok -# Has boto3 equivalent -@mock_ec2_deprecated -def test_non_default_subnet(): - vpc_cli = boto.vpc.connect_to_region("us-west-1") - - # Create the non default VPC - vpc = vpc_cli.create_vpc("10.0.0.0/16") - vpc.is_default.shouldnt.be.ok - - subnet = vpc_cli.create_subnet(vpc.id, "10.0.0.0/24") - subnet = vpc_cli.get_all_subnets(subnet_ids=[subnet.id])[0] - subnet.mapPublicIpOnLaunch.should.equal("false") - - @mock_ec2 def test_boto3_non_default_subnet(): ec2 = boto3.resource("ec2", region_name="us-west-1") @@ -279,31 +199,6 @@ def test_modify_subnet_attribute_validation(): ) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_subnet_get_by_id(): - conn = boto.vpc.connect_to_region("us-west-1") - vpcA = conn.create_vpc("10.0.0.0/16") - subnetA = conn.create_subnet(vpcA.id, "10.0.0.0/24", availability_zone="us-west-1a") - vpcB = conn.create_vpc("10.0.0.0/16") - subnetB1 = conn.create_subnet( - vpcB.id, "10.0.0.0/24", availability_zone="us-west-1a" - ) - conn.create_subnet(vpcB.id, "10.0.1.0/24", availability_zone="us-west-1b") - - subnets_by_id = conn.get_all_subnets(subnet_ids=[subnetA.id, subnetB1.id]) - subnets_by_id.should.have.length_of(2) - subnets_by_id = tuple(map(lambda s: s.id, subnets_by_id)) - subnetA.id.should.be.within(subnets_by_id) - subnetB1.id.should.be.within(subnets_by_id) - - with pytest.raises(EC2ResponseError) as cm: - conn.get_all_subnets(subnet_ids=["subnet-does_not_exist"]) - cm.value.code.should.equal("InvalidSubnetID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_subnet_get_by_id_boto3(): ec2 = boto3.resource("ec2", region_name="us-west-1") @@ -335,82 +230,6 @@ def test_subnet_get_by_id_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidSubnetID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_subnets_filtering(): - ec2 = boto.ec2.connect_to_region("us-west-1") - conn = boto.vpc.connect_to_region("us-west-1") - vpcA = conn.create_vpc("10.0.0.0/16") - subnetA = conn.create_subnet(vpcA.id, "10.0.0.0/24", availability_zone="us-west-1a") - vpcB = conn.create_vpc("10.0.0.0/16") - subnetB1 = conn.create_subnet( - vpcB.id, "10.0.0.0/24", availability_zone="us-west-1a" - ) - subnetB2 = conn.create_subnet( - vpcB.id, "10.0.1.0/24", availability_zone="us-west-1b" - ) - - all_subnets = conn.get_all_subnets() - all_subnets.should.have.length_of(3 + len(ec2.get_all_zones())) - - # Filter by VPC ID - subnets_by_vpc = conn.get_all_subnets(filters={"vpc-id": vpcB.id}) - subnets_by_vpc.should.have.length_of(2) - set([subnet.id for subnet in subnets_by_vpc]).should.equal( - set([subnetB1.id, subnetB2.id]) - ) - - # Filter by CIDR variations - subnets_by_cidr1 = conn.get_all_subnets(filters={"cidr": "10.0.0.0/24"}) - subnets_by_cidr1.should.have.length_of(2) - set([subnet.id for subnet in subnets_by_cidr1]).should.equal( - set([subnetA.id, subnetB1.id]) - ) - - subnets_by_cidr2 = conn.get_all_subnets(filters={"cidr-block": "10.0.0.0/24"}) - subnets_by_cidr2.should.have.length_of(2) - set([subnet.id for subnet in subnets_by_cidr2]).should.equal( - set([subnetA.id, subnetB1.id]) - ) - - subnets_by_cidr3 = conn.get_all_subnets(filters={"cidrBlock": "10.0.0.0/24"}) - subnets_by_cidr3.should.have.length_of(2) - set([subnet.id for subnet in subnets_by_cidr3]).should.equal( - set([subnetA.id, subnetB1.id]) - ) - - # Filter by VPC ID and CIDR - subnets_by_vpc_and_cidr = conn.get_all_subnets( - filters={"vpc-id": vpcB.id, "cidr": "10.0.0.0/24"} - ) - subnets_by_vpc_and_cidr.should.have.length_of(1) - set([subnet.id for subnet in subnets_by_vpc_and_cidr]).should.equal( - set([subnetB1.id]) - ) - - # Filter by subnet ID - subnets_by_id = conn.get_all_subnets(filters={"subnet-id": subnetA.id}) - subnets_by_id.should.have.length_of(1) - set([subnet.id for subnet in subnets_by_id]).should.equal(set([subnetA.id])) - - # Filter by availabilityZone - subnets_by_az = conn.get_all_subnets( - filters={"availabilityZone": "us-west-1a", "vpc-id": vpcB.id} - ) - subnets_by_az.should.have.length_of(1) - set([subnet.id for subnet in subnets_by_az]).should.equal(set([subnetB1.id])) - - # Filter by defaultForAz - - subnets_by_az = conn.get_all_subnets(filters={"defaultForAz": "true"}) - subnets_by_az.should.have.length_of(len(conn.get_all_zones())) - - # Unsupported filter - conn.get_all_subnets.when.called_with( - filters={"not-implemented-filter": "foobar"} - ).should.throw(NotImplementedError) - - @mock_ec2 def test_get_subnets_filtering_boto3(): ec2 = boto3.resource("ec2", region_name="us-west-1") diff --git a/tests/test_ec2/test_tags.py b/tests/test_ec2/test_tags.py index 7dc5d7612..45b62f420 100644 --- a/tests/test_ec2/test_tags.py +++ b/tests/test_ec2/test_tags.py @@ -1,45 +1,15 @@ +import boto3 +import sure # noqa # pylint: disable=unused-import import pytest -import itertools - -import boto -import boto3 from botocore.exceptions import ClientError -from boto.exception import EC2ResponseError -from boto.ec2.instance import Reservation -import sure # noqa # pylint: disable=unused-import +from moto import mock_ec2 -from moto import mock_ec2_deprecated, mock_ec2 from tests import EXAMPLE_AMI_ID from .test_instances import retrieve_all_instances from uuid import uuid4 -# Has boto3 equivalent -@mock_ec2_deprecated -def test_add_tag(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - with pytest.raises(EC2ResponseError) as ex: - instance.add_tag("a key", "some value", dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the CreateTags operation: Request would have succeeded, but DryRun flag is set" - ) - - instance.add_tag("a key", "some value") - chain = itertools.chain.from_iterable - existing_instances = list( - chain([res.instances for res in conn.get_all_reservations()]) - ) - existing_instances.should.have.length_of(1) - existing_instance = existing_instances[0] - existing_instance.tags["a key"].should.equal("some value") - - @mock_ec2 def test_instance_create_tags(): ec2 = boto3.resource("ec2", "us-west-1") @@ -62,36 +32,6 @@ def test_instance_create_tags(): ours["Tags"].should.equal([{"Key": "a key", "Value": "some value"}]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_remove_tag(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - instance.add_tag("a key", "some value") - - tags = conn.get_all_tags() - tag = tags[0] - tag.name.should.equal("a key") - tag.value.should.equal("some value") - - with pytest.raises(EC2ResponseError) as ex: - instance.remove_tag("a key", dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the DeleteTags operation: Request would have succeeded, but DryRun flag is set" - ) - - instance.remove_tag("a key") - conn.get_all_tags().should.have.length_of(0) - - instance.add_tag("a key", "some value") - conn.get_all_tags().should.have.length_of(1) - instance.remove_tag("a key", "some value") - - @mock_ec2 def test_instance_delete_tags(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -133,36 +73,6 @@ def test_instance_delete_tags(): ].should.have.length_of(0) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_all_tags(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - instance.add_tag("a key", "some value") - - tags = conn.get_all_tags() - tag = tags[0] - tag.name.should.equal("a key") - tag.value.should.equal("some value") - - -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_all_tags_with_special_characters(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - instance.add_tag("a key", "some<> value") - - tags = conn.get_all_tags() - tag = tags[0] - tag.name.should.equal("a key") - tag.value.should.equal("some<> value") - - @mock_ec2 def test_get_all_tags_with_special_characters_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -179,34 +89,6 @@ def test_get_all_tags_with_special_characters_boto3(): tag.should.have.key("Value").equal("some<> value") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_create_tags(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - tag_dict = { - "a key": "some value", - "another key": "some other value", - "blank key": "", - } - - with pytest.raises(EC2ResponseError) as ex: - conn.create_tags(instance.id, tag_dict, dry_run=True) - ex.value.error_code.should.equal("DryRunOperation") - ex.value.status.should.equal(412) - ex.value.message.should.equal( - "An error occurred (DryRunOperation) when calling the CreateTags operation: Request would have succeeded, but DryRun flag is set" - ) - - conn.create_tags(instance.id, tag_dict) - tags = conn.get_all_tags() - set([key for key in tag_dict]).should.equal(set([tag.name for tag in tags])) - set([tag_dict[key] for key in tag_dict]).should.equal( - set([tag.value for tag in tags]) - ) - - @mock_ec2 def test_create_tags_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -242,36 +124,6 @@ def test_create_tags_boto3(): ) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_tag_limit_exceeded(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - tag_dict = {} - for i in range(51): - tag_dict["{0:02d}".format(i + 1)] = "" - - with pytest.raises(EC2ResponseError) as cm: - conn.create_tags(instance.id, tag_dict) - cm.value.code.should.equal("TagLimitExceeded") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - instance.add_tag("a key", "a value") - with pytest.raises(EC2ResponseError) as cm: - conn.create_tags(instance.id, tag_dict) - cm.value.code.should.equal("TagLimitExceeded") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - tags = conn.get_all_tags() - tag = tags[0] - tags.should.have.length_of(1) - tag.name.should.equal("a key") - tag.value.should.equal("a value") - - @mock_ec2 def test_tag_limit_exceeded_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -302,37 +154,6 @@ def test_tag_limit_exceeded_boto3(): tags[0].should.have.key("Value").equal("a value") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_invalid_parameter_tag_null(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - - with pytest.raises(EC2ResponseError) as cm: - instance.add_tag("a key", None) - cm.value.code.should.equal("InvalidParameterValue") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - -# Has boto3 equivalent -@mock_ec2_deprecated -def test_invalid_id(): - conn = boto.connect_ec2("the_key", "the_secret") - with pytest.raises(EC2ResponseError) as cm: - conn.create_tags("ami-blah", {"key": "tag"}) - cm.value.code.should.equal("InvalidID") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - with pytest.raises(EC2ResponseError) as cm: - conn.create_tags("blah-blah", {"key": "tag"}) - cm.value.code.should.equal("InvalidID") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_invalid_id_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -353,34 +174,6 @@ def test_invalid_id_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidID") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_all_tags_resource_id_filter(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - instance.add_tag("an instance key", "some value") - image_id = conn.create_image(instance.id, "test-ami", "this is a test ami") - image = conn.get_image(image_id) - image.add_tag("an image key", "some value") - - tags = conn.get_all_tags(filters={"resource-id": instance.id}) - tag = tags[0] - tags.should.have.length_of(1) - tag.res_id.should.equal(instance.id) - tag.res_type.should.equal("instance") - tag.name.should.equal("an instance key") - tag.value.should.equal("some value") - - tags = conn.get_all_tags(filters={"resource-id": image_id}) - tag = tags[0] - tags.should.have.length_of(1) - tag.res_id.should.equal(image_id) - tag.res_type.should.equal("image") - tag.name.should.equal("an image key") - tag.value.should.equal("some value") - - @mock_ec2 def test_get_all_tags_resource_filter_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -433,99 +226,6 @@ def test_get_all_tags_resource_filter_boto3(): tags.should.equal([]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_all_tags_resource_type_filter(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - instance.add_tag("an instance key", "some value") - image_id = conn.create_image(instance.id, "test-ami", "this is a test ami") - image = conn.get_image(image_id) - image.add_tag("an image key", "some value") - - tags = conn.get_all_tags(filters={"resource-type": "instance"}) - tag = tags[0] - tags.should.have.length_of(1) - tag.res_id.should.equal(instance.id) - tag.res_type.should.equal("instance") - tag.name.should.equal("an instance key") - tag.value.should.equal("some value") - - tags = conn.get_all_tags(filters={"resource-type": "image"}) - tag = tags[0] - tags.should.have.length_of(1) - tag.res_id.should.equal(image_id) - tag.res_type.should.equal("image") - tag.name.should.equal("an image key") - tag.value.should.equal("some value") - - -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_all_tags_key_filter(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - instance.add_tag("an instance key", "some value") - image_id = conn.create_image(instance.id, "test-ami", "this is a test ami") - image = conn.get_image(image_id) - image.add_tag("an image key", "some value") - - tags = conn.get_all_tags(filters={"key": "an instance key"}) - tag = tags[0] - tags.should.have.length_of(1) - tag.res_id.should.equal(instance.id) - tag.res_type.should.equal("instance") - tag.name.should.equal("an instance key") - tag.value.should.equal("some value") - - -# Has boto3 equivalent -@mock_ec2_deprecated -def test_get_all_tags_value_filter(): - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance = reservation.instances[0] - instance.add_tag("an instance key", "some value") - reservation_b = conn.run_instances(EXAMPLE_AMI_ID) - instance_b = reservation_b.instances[0] - instance_b.add_tag("an instance key", "some other value") - reservation_c = conn.run_instances(EXAMPLE_AMI_ID) - instance_c = reservation_c.instances[0] - instance_c.add_tag("an instance key", "other value*") - reservation_d = conn.run_instances(EXAMPLE_AMI_ID) - instance_d = reservation_d.instances[0] - instance_d.add_tag("an instance key", "other value**") - reservation_e = conn.run_instances(EXAMPLE_AMI_ID) - instance_e = reservation_e.instances[0] - instance_e.add_tag("an instance key", "other value*?") - image_id = conn.create_image(instance.id, "test-ami", "this is a test ami") - image = conn.get_image(image_id) - image.add_tag("an image key", "some value") - - tags = conn.get_all_tags(filters={"value": "some value"}) - tags.should.have.length_of(2) - - tags = conn.get_all_tags(filters={"value": "some*value"}) - tags.should.have.length_of(3) - - tags = conn.get_all_tags(filters={"value": "*some*value"}) - tags.should.have.length_of(3) - - tags = conn.get_all_tags(filters={"value": "*some*value*"}) - tags.should.have.length_of(3) - - tags = conn.get_all_tags(filters={"value": r"*value\*"}) - tags.should.have.length_of(1) - - tags = conn.get_all_tags(filters={"value": r"*value\*\*"}) - tags.should.have.length_of(1) - - tags = conn.get_all_tags(filters={"value": r"*value\*\?"}) - tags.should.have.length_of(1) - - @mock_ec2 def test_get_all_tags_value_filter_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -564,38 +264,6 @@ def test_get_all_tags_value_filter_boto3(): filter_by_value(r"*value\*\?", [instance_e.id]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_retrieved_instances_must_contain_their_tags(): - tag_key = "Tag name" - tag_value = "Tag value" - tags_to_be_set = {tag_key: tag_value} - - conn = boto.connect_ec2("the_key", "the_secret") - reservation = conn.run_instances(EXAMPLE_AMI_ID) - reservation.should.be.a(Reservation) - reservation.instances.should.have.length_of(1) - instance = reservation.instances[0] - - reservations = conn.get_all_reservations() - reservations.should.have.length_of(1) - reservations[0].id.should.equal(reservation.id) - instances = reservations[0].instances - instances.should.have.length_of(1) - instances[0].id.should.equal(instance.id) - - conn.create_tags([instance.id], tags_to_be_set) - reservations = conn.get_all_reservations() - instance = reservations[0].instances[0] - retrieved_tags = instance.tags - - # Cleanup of instance - conn.terminate_instances([instances[0].id]) - - # Check whether tag is present with correct value - retrieved_tags[tag_key].should.equal(tag_value) - - @mock_ec2 def test_retrieved_instances_must_contain_their_tags_boto3(): tag_key = "Tag name" @@ -622,30 +290,6 @@ def test_retrieved_instances_must_contain_their_tags_boto3(): retrieved_tags.should.equal([{"Key": tag_key, "Value": tag_value}]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_retrieved_volumes_must_contain_their_tags(): - tag_key = "Tag name" - tag_value = "Tag value" - tags_to_be_set = {tag_key: tag_value} - conn = boto.connect_ec2("the_key", "the_secret") - volume = conn.create_volume(80, "us-east-1a") - - all_volumes = conn.get_all_volumes() - volume = all_volumes[0] - conn.create_tags([volume.id], tags_to_be_set) - - # Fetch the volume again - all_volumes = conn.get_all_volumes() - volume = all_volumes[0] - retrieved_tags = volume.tags - - volume.delete() - - # Check whether tag is present with correct value - retrieved_tags[tag_key].should.equal(tag_value) - - @mock_ec2 def test_retrieved_volumes_must_contain_their_tags_boto3(): tag_key = "Tag name" @@ -663,31 +307,6 @@ def test_retrieved_volumes_must_contain_their_tags_boto3(): volume.tags.should.equal([{"Key": tag_key, "Value": tag_value}]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_retrieved_snapshots_must_contain_their_tags(): - tag_key = "Tag name" - tag_value = "Tag value" - tags_to_be_set = {tag_key: tag_value} - conn = boto.connect_ec2( - aws_access_key_id="the_key", aws_secret_access_key="the_secret" - ) - volume = conn.create_volume(80, "eu-west-1a") - snapshot = conn.create_snapshot(volume.id) - conn.create_tags([snapshot.id], tags_to_be_set) - - # Fetch the snapshot again - all_snapshots = conn.get_all_snapshots() - snapshot = [item for item in all_snapshots if item.id == snapshot.id][0] - retrieved_tags = snapshot.tags - - conn.delete_snapshot(snapshot.id) - volume.delete() - - # Check whether tag is present with correct value - retrieved_tags[tag_key].should.equal(tag_value) - - @mock_ec2 def test_retrieved_snapshots_must_contain_their_tags_boto3(): tag_key = "Tag name" @@ -705,29 +324,6 @@ def test_retrieved_snapshots_must_contain_their_tags_boto3(): snapshot["Tags"].should.equal([{"Key": tag_key, "Value": tag_value}]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_filter_instances_by_wildcard_tags(): - conn = boto.connect_ec2( - aws_access_key_id="the_key", aws_secret_access_key="the_secret" - ) - reservation = conn.run_instances(EXAMPLE_AMI_ID) - instance_a = reservation.instances[0] - instance_a.add_tag("Key1", "Value1") - reservation_b = conn.run_instances(EXAMPLE_AMI_ID) - instance_b = reservation_b.instances[0] - instance_b.add_tag("Key1", "Value2") - - reservations = conn.get_all_reservations(filters={"tag:Key1": "Value*"}) - reservations.should.have.length_of(2) - - reservations = conn.get_all_reservations(filters={"tag-key": "Key*"}) - reservations.should.have.length_of(2) - - reservations = conn.get_all_reservations(filters={"tag-value": "Value*"}) - reservations.should.have.length_of(2) - - @mock_ec2 def test_filter_instances_by_wildcard_tags_boto3(): ec2 = boto3.resource("ec2", region_name="eu-west-1") diff --git a/tests/test_ec2/test_virtual_private_gateways.py b/tests/test_ec2/test_virtual_private_gateways.py index 1d23f9e12..bbd222674 100644 --- a/tests/test_ec2/test_virtual_private_gateways.py +++ b/tests/test_ec2/test_virtual_private_gateways.py @@ -1,9 +1,8 @@ -import boto import boto3 import pytest import sure # noqa # pylint: disable=unused-import -from moto import mock_ec2_deprecated, mock_ec2 +from moto import mock_ec2 from botocore.exceptions import ClientError from .test_tags import retrieve_all_tagged @@ -97,6 +96,41 @@ def test_describe_vpn_connections_state_filter_attached(): my_gateway["VpcAttachments"].should.contain({"State": "attached", "VpcId": vpc_id}) +@mock_ec2 +def test_virtual_private_gateways_boto3(): + client = boto3.client("ec2", region_name="us-west-1") + + vpn_gateway = client.create_vpn_gateway( + Type="ipsec.1", AvailabilityZone="us-east-1a" + )["VpnGateway"] + + vpn_gateway["VpnGatewayId"].should.match(r"vgw-\w+") + vpn_gateway["Type"].should.equal("ipsec.1") + vpn_gateway["State"].should.equal("available") + vpn_gateway["AvailabilityZone"].should.equal("us-east-1a") + + +@mock_ec2 +def test_describe_vpn_gateway_boto3(): + client = boto3.client("ec2", region_name="us-west-1") + vpn_gateway = client.create_vpn_gateway( + Type="ipsec.1", AvailabilityZone="us-east-1a" + )["VpnGateway"] + + vgws = client.describe_vpn_gateways(VpnGatewayIds=[vpn_gateway["VpnGatewayId"]])[ + "VpnGateways" + ] + vgws.should.have.length_of(1) + + gateway = vgws[0] + gateway["VpnGatewayId"].should.match(r"vgw-\w+") + gateway["VpnGatewayId"].should.equal(vpn_gateway["VpnGatewayId"]) + # TODO: fixme. This currently returns the ID + # gateway["Type"].should.equal("ipsec.1") + gateway["State"].should.equal("available") + gateway["AvailabilityZone"].should.equal("us-east-1a") + + @mock_ec2 def test_describe_vpn_connections_state_filter_deatched(): """describe_vpn_gateways attachment.state filter - don't match detatched""" @@ -178,87 +212,6 @@ def test_describe_vpn_connections_type_filter_miss(): gateways["VpnGateways"].should.have.length_of(0) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_virtual_private_gateways(): - conn = boto.connect_vpc("the_key", "the_secret") - - vpn_gateway = conn.create_vpn_gateway("ipsec.1", "us-east-1a") - vpn_gateway.should_not.be.none - vpn_gateway.id.should.match(r"vgw-\w+") - vpn_gateway.type.should.equal("ipsec.1") - vpn_gateway.state.should.equal("available") - vpn_gateway.availability_zone.should.equal("us-east-1a") - - -@mock_ec2 -def test_virtual_private_gateways_boto3(): - client = boto3.client("ec2", region_name="us-west-1") - - vpn_gateway = client.create_vpn_gateway( - Type="ipsec.1", AvailabilityZone="us-east-1a" - )["VpnGateway"] - - vpn_gateway["VpnGatewayId"].should.match(r"vgw-\w+") - vpn_gateway["Type"].should.equal("ipsec.1") - vpn_gateway["State"].should.equal("available") - vpn_gateway["AvailabilityZone"].should.equal("us-east-1a") - - -# Has boto3 equivalent -@mock_ec2_deprecated -def test_describe_vpn_gateway(): - conn = boto.connect_vpc("the_key", "the_secret") - vpn_gateway = conn.create_vpn_gateway("ipsec.1", "us-east-1a") - - vgws = conn.get_all_vpn_gateways() - vgws.should.have.length_of(1) - - gateway = vgws[0] - gateway.id.should.match(r"vgw-\w+") - gateway.id.should.equal(vpn_gateway.id) - vpn_gateway.type.should.equal("ipsec.1") - vpn_gateway.state.should.equal("available") - vpn_gateway.availability_zone.should.equal("us-east-1a") - - -@mock_ec2 -def test_describe_vpn_gateway_boto3(): - client = boto3.client("ec2", region_name="us-west-1") - vpn_gateway = client.create_vpn_gateway( - Type="ipsec.1", AvailabilityZone="us-east-1a" - )["VpnGateway"] - - vgws = client.describe_vpn_gateways(VpnGatewayIds=[vpn_gateway["VpnGatewayId"]])[ - "VpnGateways" - ] - vgws.should.have.length_of(1) - - gateway = vgws[0] - gateway["VpnGatewayId"].should.match(r"vgw-\w+") - gateway["VpnGatewayId"].should.equal(vpn_gateway["VpnGatewayId"]) - # TODO: fixme. This currently returns the ID - # gateway["Type"].should.equal("ipsec.1") - gateway["State"].should.equal("available") - gateway["AvailabilityZone"].should.equal("us-east-1a") - - -# Has boto3 equivalent -@mock_ec2_deprecated -def test_vpn_gateway_vpc_attachment(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - vpn_gateway = conn.create_vpn_gateway("ipsec.1", "us-east-1a") - - conn.attach_vpn_gateway(vpn_gateway_id=vpn_gateway.id, vpc_id=vpc.id) - - gateway = conn.get_all_vpn_gateways()[0] - attachments = gateway.attachments - attachments.should.have.length_of(1) - attachments[0].vpc_id.should.equal(vpc.id) - attachments[0].state.should.equal("attached") - - @mock_ec2 def test_vpn_gateway_vpc_attachment_boto3(): ec2 = boto3.resource("ec2", region_name="us-west-1") @@ -276,18 +229,6 @@ def test_vpn_gateway_vpc_attachment_boto3(): attachments.should.equal([{"State": "attached", "VpcId": vpc.id}]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_delete_vpn_gateway(): - conn = boto.connect_vpc("the_key", "the_secret") - vpn_gateway = conn.create_vpn_gateway("ipsec.1", "us-east-1a") - - conn.delete_vpn_gateway(vpn_gateway.id) - vgws = conn.get_all_vpn_gateways() - vgws.should.have.length_of(1) - vgws[0].state.should.equal("deleted") - - @mock_ec2 def test_delete_vpn_gateway_boto3(): client = boto3.client("ec2", region_name="us-west-1") @@ -302,23 +243,6 @@ def test_delete_vpn_gateway_boto3(): gateways[0].should.have.key("State").equal("deleted") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_vpn_gateway_tagging(): - conn = boto.connect_vpc("the_key", "the_secret") - vpn_gateway = conn.create_vpn_gateway("ipsec.1", "us-east-1a") - vpn_gateway.add_tag("a key", "some value") - - tag = conn.get_all_tags()[0] - tag.name.should.equal("a key") - tag.value.should.equal("some value") - - # Refresh the subnet - vpn_gateway = conn.get_all_vpn_gateways()[0] - vpn_gateway.tags.should.have.length_of(1) - vpn_gateway.tags["a key"].should.equal("some value") - - @mock_ec2 def test_vpn_gateway_tagging_boto3(): client = boto3.client("ec2", region_name="us-west-1") @@ -340,30 +264,6 @@ def test_vpn_gateway_tagging_boto3(): # vpn_gateway["Tags"].should.equal([{'Key': 'a key', 'Value': 'some value'}]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_detach_vpn_gateway(): - - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - vpn_gateway = conn.create_vpn_gateway("ipsec.1", "us-east-1a") - - conn.attach_vpn_gateway(vpn_gateway_id=vpn_gateway.id, vpc_id=vpc.id) - - gateway = conn.get_all_vpn_gateways()[0] - attachments = gateway.attachments - attachments.should.have.length_of(1) - attachments[0].vpc_id.should.equal(vpc.id) - attachments[0].state.should.equal("attached") - - conn.detach_vpn_gateway(vpn_gateway_id=vpn_gateway.id, vpc_id=vpc.id) - - gateway = conn.get_all_vpn_gateways()[0] - attachments = gateway.attachments - attachments.should.have.length_of(1) - attachments[0].state.should.equal("detached") - - @mock_ec2 def test_detach_vpn_gateway_boto3(): ec2 = boto3.resource("ec2", region_name="us-west-1") diff --git a/tests/test_ec2/test_vpc_peering.py b/tests/test_ec2/test_vpc_peering.py index 656547bab..b7220d191 100644 --- a/tests/test_ec2/test_vpc_peering.py +++ b/tests/test_ec2/test_vpc_peering.py @@ -2,26 +2,9 @@ import pytest from botocore.exceptions import ClientError import boto3 -import boto -from boto.exception import EC2ResponseError import sure # noqa # pylint: disable=unused-import -from moto import mock_ec2, mock_ec2_deprecated -from tests.helpers import requires_boto_gte - - -# Has boto3 equivalent -@requires_boto_gte("2.32.0") -@mock_ec2_deprecated -def test_vpc_peering_connections(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - peer_vpc = conn.create_vpc("11.0.0.0/16") - - vpc_pcx = conn.create_vpc_peering_connection(vpc.id, peer_vpc.id) - vpc_pcx._status.code.should.equal("initiating-request") - - return vpc_pcx +from moto import mock_ec2 def create_vpx_pcx(ec2, client): @@ -43,19 +26,6 @@ def test_vpc_peering_connections_boto3(): vpc_pcx["Status"]["Code"].should.equal("initiating-request") -# Has boto3 equivalent -@requires_boto_gte("2.32.0") -@mock_ec2_deprecated -def test_vpc_peering_connections_get_all(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc_pcx = test_vpc_peering_connections() - vpc_pcx._status.code.should.equal("initiating-request") - - all_vpc_pcxs = conn.get_all_vpc_peering_connections() - all_vpc_pcxs.should.have.length_of(1) - all_vpc_pcxs[0]._status.code.should.equal("pending-acceptance") - - @mock_ec2 def test_vpc_peering_connections_get_all_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -75,27 +45,6 @@ def test_vpc_peering_connections_get_all_boto3(): my_vpc_pcx["Status"]["Code"].should.equal("pending-acceptance") -# Has boto3 equivalent -@requires_boto_gte("2.32.0") -@mock_ec2_deprecated -def test_vpc_peering_connections_accept(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc_pcx = test_vpc_peering_connections() - - vpc_pcx = conn.accept_vpc_peering_connection(vpc_pcx.id) - vpc_pcx._status.code.should.equal("active") - - with pytest.raises(EC2ResponseError) as cm: - conn.reject_vpc_peering_connection(vpc_pcx.id) - cm.value.code.should.equal("InvalidStateTransition") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - all_vpc_pcxs = conn.get_all_vpc_peering_connections() - all_vpc_pcxs.should.have.length_of(1) - all_vpc_pcxs[0]._status.code.should.equal("active") - - @mock_ec2 def test_vpc_peering_connections_accept_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -120,27 +69,6 @@ def test_vpc_peering_connections_accept_boto3(): my_vpc_pcxs[0]["Status"]["Code"].should.equal("active") -# Has boto3 equivalent -@requires_boto_gte("2.32.0") -@mock_ec2_deprecated -def test_vpc_peering_connections_reject(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc_pcx = test_vpc_peering_connections() - - verdict = conn.reject_vpc_peering_connection(vpc_pcx.id) - verdict.should.equal(True) - - with pytest.raises(EC2ResponseError) as cm: - conn.accept_vpc_peering_connection(vpc_pcx.id) - cm.value.code.should.equal("InvalidStateTransition") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - all_vpc_pcxs = conn.get_all_vpc_peering_connections() - all_vpc_pcxs.should.have.length_of(1) - all_vpc_pcxs[0]._status.code.should.equal("rejected") - - @mock_ec2 def test_vpc_peering_connections_reject_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -163,27 +91,6 @@ def test_vpc_peering_connections_reject_boto3(): my_pcxs[0]["Status"]["Code"].should.equal("rejected") -# Has boto3 equivalent -@requires_boto_gte("2.32.1") -@mock_ec2_deprecated -def test_vpc_peering_connections_delete(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc_pcx = test_vpc_peering_connections() - - verdict = vpc_pcx.delete() - verdict.should.equal(True) - - all_vpc_pcxs = conn.get_all_vpc_peering_connections() - all_vpc_pcxs.should.have.length_of(1) - all_vpc_pcxs[0]._status.code.should.equal("deleted") - - with pytest.raises(EC2ResponseError) as cm: - conn.delete_vpc_peering_connection("pcx-1234abcd") - cm.value.code.should.equal("InvalidVpcPeeringConnectionId.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_vpc_peering_connections_delete_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") diff --git a/tests/test_ec2/test_vpcs.py b/tests/test_ec2/test_vpcs.py index 240fabb9f..3802d6b02 100644 --- a/tests/test_ec2/test_vpcs.py +++ b/tests/test_ec2/test_vpcs.py @@ -2,13 +2,11 @@ import pytest from botocore.exceptions import ClientError import boto3 -import boto -from boto.exception import EC2ResponseError import sure # noqa # pylint: disable=unused-import import random -from moto import mock_ec2, mock_ec2_deprecated +from moto import mock_ec2 from uuid import uuid4 from .test_tags import retrieve_all_tagged @@ -16,28 +14,6 @@ SAMPLE_DOMAIN_NAME = "example.com" SAMPLE_NAME_SERVERS = ["10.0.0.6", "10.0.0.7"] -# Has boto3 equivalent -@mock_ec2_deprecated -def test_vpcs(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - vpc.cidr_block.should.equal("10.0.0.0/16") - - all_vpcs = conn.get_all_vpcs() - all_vpcs.should.have.length_of(2) - - vpc.delete() - - all_vpcs = conn.get_all_vpcs() - all_vpcs.should.have.length_of(1) - - with pytest.raises(EC2ResponseError) as cm: - conn.delete_vpc("vpc-1234abcd") - cm.value.code.should.equal("InvalidVpcID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_create_and_delete_vpc(): ec2 = boto3.resource("ec2", region_name="eu-north-1") @@ -60,23 +36,6 @@ def test_create_and_delete_vpc(): ex.value.response["Error"]["Code"].should.equal("InvalidVpcID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_vpc_defaults(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - - conn.get_all_vpcs().should.have.length_of(2) - conn.get_all_route_tables().should.have.length_of(2) - conn.get_all_security_groups(filters={"vpc-id": [vpc.id]}).should.have.length_of(1) - - vpc.delete() - - conn.get_all_vpcs().should.have.length_of(1) - conn.get_all_route_tables().should.have.length_of(1) - conn.get_all_security_groups(filters={"vpc-id": [vpc.id]}).should.have.length_of(0) - - @mock_ec2 def test_vpc_defaults_boto3(): ec2 = boto3.resource("ec2", region_name="eu-north-1") @@ -102,16 +61,6 @@ def test_vpc_defaults_boto3(): ].should.have.length_of(0) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_vpc_isdefault_filter(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - conn.get_all_vpcs(filters={"isDefault": "true"}).should.have.length_of(1) - vpc.delete() - conn.get_all_vpcs(filters={"isDefault": "true"}).should.have.length_of(1) - - @mock_ec2 def test_vpc_isdefault_filter_boto3(): ec2 = boto3.resource("ec2", region_name="eu-west-1") @@ -128,19 +77,6 @@ def test_vpc_isdefault_filter_boto3(): ].should.have.length_of(1) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_multiple_vpcs_default_filter(): - conn = boto.connect_vpc("the_key", "the_secret") - conn.create_vpc("10.8.0.0/16") - conn.create_vpc("10.0.0.0/16") - conn.create_vpc("192.168.0.0/16") - conn.get_all_vpcs().should.have.length_of(4) - vpc = conn.get_all_vpcs(filters={"isDefault": "true"}) - vpc.should.have.length_of(1) - vpc[0].cidr_block.should.equal("172.31.0.0/16") - - @mock_ec2 def test_multiple_vpcs_default_filter_boto3(): ec2 = boto3.resource("ec2", region_name="eu-west-1") @@ -154,17 +90,6 @@ def test_multiple_vpcs_default_filter_boto3(): [v["CidrBlock"] for v in default_vpcs].should.contain("172.31.0.0/16") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_vpc_state_available_filter(): - conn = boto.connect_vpc("the_key", "the_secret") - vpc = conn.create_vpc("10.0.0.0/16") - conn.create_vpc("10.1.0.0/16") - conn.get_all_vpcs(filters={"state": "available"}).should.have.length_of(3) - vpc.delete() - conn.get_all_vpcs(filters={"state": "available"}).should.have.length_of(2) - - @mock_ec2 def test_vpc_state_available_filter_boto3(): ec2 = boto3.resource("ec2", region_name="eu-west-1") @@ -194,23 +119,6 @@ def retrieve_all_vpcs(client, filters=[]): # pylint: disable=W0102 return all_vpcs -# Has boto3 equivalent -@mock_ec2_deprecated -def test_vpc_tagging(): - conn = boto.connect_vpc() - vpc = conn.create_vpc("10.0.0.0/16") - - vpc.add_tag("a key", "some value") - tag = conn.get_all_tags()[0] - tag.name.should.equal("a key") - tag.value.should.equal("some value") - - # Refresh the vpc - vpc = conn.get_all_vpcs(vpc_ids=[vpc.id])[0] - vpc.tags.should.have.length_of(1) - vpc.tags["a key"].should.equal("some value") - - @mock_ec2 def test_vpc_tagging_boto3(): ec2 = boto3.resource("ec2", region_name="eu-west-1") @@ -229,27 +137,6 @@ def test_vpc_tagging_boto3(): vpc["Tags"].should.equal([{"Key": "a key", "Value": "some value"}]) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_vpc_get_by_id(): - conn = boto.connect_vpc() - vpc1 = conn.create_vpc("10.0.0.0/16") - vpc2 = conn.create_vpc("10.0.0.0/16") - conn.create_vpc("10.0.0.0/16") - - vpcs = conn.get_all_vpcs(vpc_ids=[vpc1.id, vpc2.id]) - vpcs.should.have.length_of(2) - vpc_ids = tuple(map(lambda v: v.id, vpcs)) - vpc1.id.should.be.within(vpc_ids) - vpc2.id.should.be.within(vpc_ids) - - with pytest.raises(EC2ResponseError) as cm: - conn.get_all_vpcs(vpc_ids=["vpc-does_not_exist"]) - cm.value.code.should.equal("InvalidVpcID.NotFound") - cm.value.status.should.equal(400) - cm.value.request_id.should_not.be.none - - @mock_ec2 def test_vpc_get_by_id_boto3(): ec2 = boto3.resource("ec2", region_name="eu-west-1") @@ -271,21 +158,6 @@ def test_vpc_get_by_id_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidVpcID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_vpc_get_by_cidr_block(): - conn = boto.connect_vpc() - vpc1 = conn.create_vpc("10.0.0.0/16") - vpc2 = conn.create_vpc("10.0.0.0/16") - conn.create_vpc("10.0.0.0/24") - - vpcs = conn.get_all_vpcs(filters={"cidr": "10.0.0.0/16"}) - vpcs.should.have.length_of(2) - vpc_ids = tuple(map(lambda v: v.id, vpcs)) - vpc1.id.should.be.within(vpc_ids) - vpc2.id.should.be.within(vpc_ids) - - @mock_ec2 def test_vpc_get_by_cidr_block_boto3(): ec2 = boto3.resource("ec2", region_name="eu-west-1") @@ -302,25 +174,6 @@ def test_vpc_get_by_cidr_block_boto3(): set([vpc["VpcId"] for vpc in vpcs]).should.equal(set([vpc1.id, vpc2.id])) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_vpc_get_by_dhcp_options_id(): - conn = boto.connect_vpc() - dhcp_options = conn.create_dhcp_options(SAMPLE_DOMAIN_NAME, SAMPLE_NAME_SERVERS) - vpc1 = conn.create_vpc("10.0.0.0/16") - vpc2 = conn.create_vpc("10.0.0.0/16") - conn.create_vpc("10.0.0.0/24") - - conn.associate_dhcp_options(dhcp_options.id, vpc1.id) - conn.associate_dhcp_options(dhcp_options.id, vpc2.id) - - vpcs = conn.get_all_vpcs(filters={"dhcp-options-id": dhcp_options.id}) - vpcs.should.have.length_of(2) - vpc_ids = tuple(map(lambda v: v.id, vpcs)) - vpc1.id.should.be.within(vpc_ids) - vpc2.id.should.be.within(vpc_ids) - - @mock_ec2 def test_vpc_get_by_dhcp_options_id_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -347,25 +200,6 @@ def test_vpc_get_by_dhcp_options_id_boto3(): vpc2.id.should.be.within(vpc_ids) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_vpc_get_by_tag(): - conn = boto.connect_vpc() - vpc1 = conn.create_vpc("10.0.0.0/16") - vpc2 = conn.create_vpc("10.0.0.0/16") - vpc3 = conn.create_vpc("10.0.0.0/24") - - vpc1.add_tag("Name", "TestVPC") - vpc2.add_tag("Name", "TestVPC") - vpc3.add_tag("Name", "TestVPC2") - - vpcs = conn.get_all_vpcs(filters={"tag:Name": "TestVPC"}) - vpcs.should.have.length_of(2) - vpc_ids = tuple(map(lambda v: v.id, vpcs)) - vpc1.id.should.be.within(vpc_ids) - vpc2.id.should.be.within(vpc_ids) - - @mock_ec2 def test_vpc_get_by_tag_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -386,27 +220,6 @@ def test_vpc_get_by_tag_boto3(): set([vpc["VpcId"] for vpc in vpcs]).should.equal(set([vpc1.id, vpc2.id])) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_vpc_get_by_tag_key_superset(): - conn = boto.connect_vpc() - vpc1 = conn.create_vpc("10.0.0.0/16") - vpc2 = conn.create_vpc("10.0.0.0/16") - vpc3 = conn.create_vpc("10.0.0.0/24") - - vpc1.add_tag("Name", "TestVPC") - vpc1.add_tag("Key", "TestVPC2") - vpc2.add_tag("Name", "TestVPC") - vpc2.add_tag("Key", "TestVPC2") - vpc3.add_tag("Key", "TestVPC2") - - vpcs = conn.get_all_vpcs(filters={"tag-key": "Name"}) - vpcs.should.have.length_of(2) - vpc_ids = tuple(map(lambda v: v.id, vpcs)) - vpc1.id.should.be.within(vpc_ids) - vpc2.id.should.be.within(vpc_ids) - - @mock_ec2 def test_vpc_get_by_tag_key_superset_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -429,27 +242,6 @@ def test_vpc_get_by_tag_key_superset_boto3(): set([vpc["VpcId"] for vpc in vpcs]).should.equal(set([vpc1.id, vpc2.id])) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_vpc_get_by_tag_key_subset(): - conn = boto.connect_vpc() - vpc1 = conn.create_vpc("10.0.0.0/16") - vpc2 = conn.create_vpc("10.0.0.0/16") - vpc3 = conn.create_vpc("10.0.0.0/24") - - vpc1.add_tag("Name", "TestVPC") - vpc1.add_tag("Key", "TestVPC2") - vpc2.add_tag("Name", "TestVPC") - vpc2.add_tag("Key", "TestVPC2") - vpc3.add_tag("Test", "TestVPC2") - - vpcs = conn.get_all_vpcs(filters={"tag-key": ["Name", "Key"]}) - vpcs.should.have.length_of(2) - vpc_ids = tuple(map(lambda v: v.id, vpcs)) - vpc1.id.should.be.within(vpc_ids) - vpc2.id.should.be.within(vpc_ids) - - @mock_ec2 def test_vpc_get_by_tag_key_subset_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -473,27 +265,6 @@ def test_vpc_get_by_tag_key_subset_boto3(): set([vpc["VpcId"] for vpc in vpcs]).should.equal(set([vpc1.id, vpc2.id])) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_vpc_get_by_tag_value_superset(): - conn = boto.connect_vpc() - vpc1 = conn.create_vpc("10.0.0.0/16") - vpc2 = conn.create_vpc("10.0.0.0/16") - vpc3 = conn.create_vpc("10.0.0.0/24") - - vpc1.add_tag("Name", "TestVPC") - vpc1.add_tag("Key", "TestVPC2") - vpc2.add_tag("Name", "TestVPC") - vpc2.add_tag("Key", "TestVPC2") - vpc3.add_tag("Key", "TestVPC2") - - vpcs = conn.get_all_vpcs(filters={"tag-value": "TestVPC"}) - vpcs.should.have.length_of(2) - vpc_ids = tuple(map(lambda v: v.id, vpcs)) - vpc1.id.should.be.within(vpc_ids) - vpc2.id.should.be.within(vpc_ids) - - @mock_ec2 def test_vpc_get_by_tag_value_superset_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -516,26 +287,6 @@ def test_vpc_get_by_tag_value_superset_boto3(): set([vpc["VpcId"] for vpc in vpcs]).should.equal(set([vpc1.id, vpc2.id])) -# Has boto3 equivalent -@mock_ec2_deprecated -def test_vpc_get_by_tag_value_subset(): - conn = boto.connect_vpc() - vpc1 = conn.create_vpc("10.0.0.0/16") - vpc2 = conn.create_vpc("10.0.0.0/16") - conn.create_vpc("10.0.0.0/24") - - vpc1.add_tag("Name", "TestVPC") - vpc1.add_tag("Key", "TestVPC2") - vpc2.add_tag("Name", "TestVPC") - vpc2.add_tag("Key", "TestVPC2") - - vpcs = conn.get_all_vpcs(filters={"tag-value": ["TestVPC", "TestVPC2"]}) - vpcs.should.have.length_of(2) - vpc_ids = tuple(map(lambda v: v.id, vpcs)) - vpc1.id.should.be.within(vpc_ids) - vpc2.id.should.be.within(vpc_ids) - - @mock_ec2 def test_vpc_get_by_tag_value_subset_boto3(): ec2 = boto3.resource("ec2", region_name="us-east-1") @@ -693,19 +444,6 @@ def test_vpc_modify_enable_dns_hostnames(): attr.get("Value").should.be.ok -# Has boto3 equivalent -@mock_ec2_deprecated -def test_vpc_associate_dhcp_options(): - conn = boto.connect_vpc() - dhcp_options = conn.create_dhcp_options(SAMPLE_DOMAIN_NAME, SAMPLE_NAME_SERVERS) - vpc = conn.create_vpc("10.0.0.0/16") - - conn.associate_dhcp_options(dhcp_options.id, vpc.id) - - vpc.update() - dhcp_options.id.should.equal(vpc.dhcp_options_id) - - @mock_ec2 def test_vpc_associate_dhcp_options_boto3(): ec2 = boto3.resource("ec2", region_name="us-west-1") diff --git a/tests/test_ec2/test_vpn_connections.py b/tests/test_ec2/test_vpn_connections.py index fa15361de..fe3e51d26 100644 --- a/tests/test_ec2/test_vpn_connections.py +++ b/tests/test_ec2/test_vpn_connections.py @@ -1,22 +1,8 @@ -import boto import boto3 import pytest import sure # noqa # pylint: disable=unused-import -from boto.exception import EC2ResponseError from botocore.exceptions import ClientError -from moto import mock_ec2, mock_ec2_deprecated - - -# Has boto3 equivalent -@mock_ec2_deprecated -def test_create_vpn_connections(): - conn = boto.connect_vpc("the_key", "the_secret") - vpn_connection = conn.create_vpn_connection( - "ipsec.1", "vgw-0123abcd", "cgw-0123abcd" - ) - vpn_connection.should_not.be.none - vpn_connection.id.should.match(r"vpn-\w+") - vpn_connection.type.should.equal("ipsec.1") +from moto import mock_ec2 @mock_ec2 @@ -29,20 +15,6 @@ def test_create_vpn_connections_boto3(): vpn_connection["Type"].should.equal("ipsec.1") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_delete_vpn_connections(): - conn = boto.connect_vpc("the_key", "the_secret") - vpn_connection = conn.create_vpn_connection( - "ipsec.1", "vgw-0123abcd", "cgw-0123abcd" - ) - list_of_vpn_connections = conn.get_all_vpn_connections() - list_of_vpn_connections.should.have.length_of(1) - conn.delete_vpn_connection(vpn_connection.id) - list_of_vpn_connections = conn.get_all_vpn_connections() - list_of_vpn_connections[0].state.should.equal("deleted") - - @mock_ec2 def test_delete_vpn_connections_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -67,14 +39,6 @@ def test_delete_vpn_connections_boto3(): my_cnx.should.have.key("State").equal("deleted") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_delete_vpn_connections_bad_id(): - conn = boto.connect_vpc("the_key", "the_secret") - with pytest.raises(EC2ResponseError): - conn.delete_vpn_connection("vpn-0123abcd") - - @mock_ec2 def test_delete_vpn_connections_bad_id_boto3(): client = boto3.client("ec2", region_name="us-east-1") @@ -85,22 +49,6 @@ def test_delete_vpn_connections_bad_id_boto3(): ex.value.response["Error"]["Code"].should.equal("InvalidVpnConnectionID.NotFound") -# Has boto3 equivalent -@mock_ec2_deprecated -def test_describe_vpn_connections(): - conn = boto.connect_vpc("the_key", "the_secret") - list_of_vpn_connections = conn.get_all_vpn_connections() - list_of_vpn_connections.should.have.length_of(0) - conn.create_vpn_connection("ipsec.1", "vgw-0123abcd", "cgw-0123abcd") - list_of_vpn_connections = conn.get_all_vpn_connections() - list_of_vpn_connections.should.have.length_of(1) - vpn = conn.create_vpn_connection("ipsec.1", "vgw-1234abcd", "cgw-1234abcd") - list_of_vpn_connections = conn.get_all_vpn_connections() - list_of_vpn_connections.should.have.length_of(2) - list_of_vpn_connections = conn.get_all_vpn_connections(vpn.id) - list_of_vpn_connections.should.have.length_of(1) - - @mock_ec2 def test_create_vpn_connection_with_vpn_gateway(): client = boto3.client("ec2", region_name="us-east-1") diff --git a/tests/test_elb/test_elb.py b/tests/test_elb/test_elb.py index ed72f43f2..d0c304c46 100644 --- a/tests/test_elb/test_elb.py +++ b/tests/test_elb/test_elb.py @@ -1,54 +1,14 @@ import boto3 import botocore -import boto -import boto.ec2.elb -from boto.ec2.elb import HealthCheck -from boto.ec2.elb.attributes import ( - ConnectionSettingAttribute, - ConnectionDrainingAttribute, - AccessLogAttribute, -) from botocore.exceptions import ClientError -from boto.exception import BotoServerError import pytest import sure # noqa # pylint: disable=unused-import -from moto import mock_acm, mock_elb, mock_ec2, mock_elb_deprecated, mock_ec2_deprecated +from moto import mock_acm, mock_elb, mock_ec2 from tests import EXAMPLE_AMI_ID from uuid import uuid4 -# Has boto3 equivalent -@mock_elb_deprecated -@mock_ec2_deprecated -def test_create_load_balancer(): - conn = boto.connect_elb() - ec2 = boto.ec2.connect_to_region("us-east-1") - - security_group = ec2.create_security_group("sg-abc987", "description") - - zones = ["us-east-1a", "us-east-1b"] - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - conn.create_load_balancer( - "my-lb", zones, ports, scheme="internal", security_groups=[security_group.id] - ) - - balancers = conn.get_all_load_balancers() - balancer = balancers[0] - balancer.name.should.equal("my-lb") - balancer.scheme.should.equal("internal") - list(balancer.security_groups).should.equal([security_group.id]) - set(balancer.availability_zones).should.equal(set(["us-east-1a", "us-east-1b"])) - listener1 = balancer.listeners[0] - listener1.load_balancer_port.should.equal(80) - listener1.instance_port.should.equal(8080) - listener1.protocol.should.equal("HTTP") - listener2 = balancer.listeners[1] - listener2.load_balancer_port.should.equal(443) - listener2.instance_port.should.equal(8443) - listener2.protocol.should.equal("TCP") - - @pytest.mark.parametrize("region_name", ["us-east-1", "ap-south-1"]) @pytest.mark.parametrize( "zones", @@ -128,15 +88,6 @@ def test_create_load_balancer_boto3(zones, region_name): ) -# Has boto3 equivalent -@mock_elb_deprecated -def test_getting_missing_elb(): - conn = boto.connect_elb() - conn.get_all_load_balancers.when.called_with( - load_balancer_names="aaa" - ).should.throw(BotoServerError) - - @mock_elb def test_get_missing_elb_boto3(): client = boto3.client("elb", region_name="us-west-2") @@ -149,22 +100,6 @@ def test_get_missing_elb_boto3(): ) -# Has boto3 equivalent -@mock_elb_deprecated -def test_create_elb_in_multiple_region(): - zones = ["us-east-1a", "us-east-1b"] - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - - west1_conn = boto.ec2.elb.connect_to_region("us-west-1") - west1_conn.create_load_balancer("my-lb", zones, ports) - - west2_conn = boto.ec2.elb.connect_to_region("us-west-2") - west2_conn.create_load_balancer("my-lb", zones, ports) - - list(west1_conn.get_all_load_balancers()).should.have.length_of(1) - list(west2_conn.get_all_load_balancers()).should.have.length_of(1) - - @mock_elb def test_create_elb_in_multiple_region_boto3(): client_east = boto3.client("elb", region_name="us-east-2") @@ -349,45 +284,6 @@ def test_apply_security_groups_to_load_balancer(): ) -# Has boto3 equivalent -@mock_elb_deprecated -def test_add_listener(): - conn = boto.connect_elb() - zones = ["us-east-1a", "us-east-1b"] - ports = [(80, 8080, "http")] - conn.create_load_balancer("my-lb", zones, ports) - new_listener = (443, 8443, "tcp") - conn.create_load_balancer_listeners("my-lb", [new_listener]) - balancers = conn.get_all_load_balancers() - balancer = balancers[0] - listener1 = balancer.listeners[0] - listener1.load_balancer_port.should.equal(80) - listener1.instance_port.should.equal(8080) - listener1.protocol.should.equal("HTTP") - listener2 = balancer.listeners[1] - listener2.load_balancer_port.should.equal(443) - listener2.instance_port.should.equal(8443) - listener2.protocol.should.equal("TCP") - - -# Has boto3 equivalent -@mock_elb_deprecated -def test_delete_listener(): - conn = boto.connect_elb() - - zones = ["us-east-1a", "us-east-1b"] - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - conn.create_load_balancer("my-lb", zones, ports) - conn.delete_load_balancer_listeners("my-lb", [443]) - balancers = conn.get_all_load_balancers() - balancer = balancers[0] - listener1 = balancer.listeners[0] - listener1.load_balancer_port.should.equal(80) - listener1.instance_port.should.equal(8080) - listener1.protocol.should.equal("HTTP") - balancer.listeners.should.have.length_of(1) - - @mock_elb def test_create_and_delete_listener_boto3_support(): client = boto3.client("elb", region_name="us-east-1") @@ -541,24 +437,6 @@ def test_set_sslcertificate_boto3(): listener.should.have.key("SSLCertificateId").equals(certificate_arn) -# Has boto3 equivalent -@mock_elb_deprecated -def test_get_load_balancers_by_name(): - conn = boto.connect_elb() - - zones = ["us-east-1a", "us-east-1b"] - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - conn.create_load_balancer("my-lb1", zones, ports) - conn.create_load_balancer("my-lb2", zones, ports) - conn.create_load_balancer("my-lb3", zones, ports) - - conn.get_all_load_balancers().should.have.length_of(3) - conn.get_all_load_balancers(load_balancer_names=["my-lb1"]).should.have.length_of(1) - conn.get_all_load_balancers( - load_balancer_names=["my-lb1", "my-lb2"] - ).should.have.length_of(2) - - @mock_elb def test_get_load_balancers_by_name_boto3(): client = boto3.client("elb", region_name="us-east-1") @@ -602,23 +480,6 @@ def test_get_load_balancers_by_name_boto3(): err["Message"].should.match(f"The specified load balancer does not exist:") -# Has boto3 equivalent -@mock_elb_deprecated -def test_delete_load_balancer(): - conn = boto.connect_elb() - - zones = ["us-east-1a"] - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - conn.create_load_balancer("my-lb", zones, ports) - - balancers = conn.get_all_load_balancers() - balancers.should.have.length_of(1) - - conn.delete_load_balancer("my-lb") - balancers = conn.get_all_load_balancers() - balancers.should.have.length_of(0) - - @mock_elb def test_delete_load_balancer_boto3(): client = boto3.client("elb", region_name="us-east-1") @@ -650,32 +511,6 @@ def test_delete_load_balancer_boto3(): lb_names.should.contain(lb_name2) -# Has boto3 equivalent -@mock_elb_deprecated -def test_create_health_check(): - conn = boto.connect_elb() - - hc = HealthCheck( - interval=20, - healthy_threshold=3, - unhealthy_threshold=5, - target="HTTP:8080/health", - timeout=23, - ) - - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - lb = conn.create_load_balancer("my-lb", [], ports) - lb.configure_health_check(hc) - - balancer = conn.get_all_load_balancers()[0] - health_check = balancer.health_check - health_check.interval.should.equal(20) - health_check.healthy_threshold.should.equal(3) - health_check.unhealthy_threshold.should.equal(5) - health_check.target.should.equal("HTTP:8080/health") - health_check.timeout.should.equal(23) - - @mock_elb def test_create_health_check_boto3(): client = boto3.client("elb", region_name="us-east-1") @@ -704,26 +539,6 @@ def test_create_health_check_boto3(): balancer["HealthCheck"]["UnhealthyThreshold"].should.equal(5) -# Has boto3 equivalent -@mock_ec2_deprecated -@mock_elb_deprecated -def test_register_instances(): - ec2_conn = boto.connect_ec2() - reservation = ec2_conn.run_instances(EXAMPLE_AMI_ID, 2) - instance_id1 = reservation.instances[0].id - instance_id2 = reservation.instances[1].id - - conn = boto.connect_elb() - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - lb = conn.create_load_balancer("my-lb", [], ports) - - lb.register_instances([instance_id1, instance_id2]) - - balancer = conn.get_all_load_balancers()[0] - instance_ids = [instance.id for instance in balancer.instances] - set(instance_ids).should.equal(set([instance_id1, instance_id2])) - - @mock_ec2 @mock_elb def test_register_instances_boto3(): @@ -747,29 +562,6 @@ def test_register_instances_boto3(): set(instance_ids).should.equal(set([instance_id1, instance_id2])) -# Has boto3 equivalent -@mock_ec2_deprecated -@mock_elb_deprecated -def test_deregister_instances(): - ec2_conn = boto.connect_ec2() - reservation = ec2_conn.run_instances(EXAMPLE_AMI_ID, 2) - instance_id1 = reservation.instances[0].id - instance_id2 = reservation.instances[1].id - - conn = boto.connect_elb() - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - lb = conn.create_load_balancer("my-lb", [], ports) - - lb.register_instances([instance_id1, instance_id2]) - - balancer = conn.get_all_load_balancers()[0] - balancer.instances.should.have.length_of(2) - balancer.deregister_instances([instance_id1]) - - balancer.instances.should.have.length_of(1) - balancer.instances[0].id.should.equal(instance_id2) - - @mock_ec2 @mock_elb def test_deregister_instances_boto3(): @@ -801,20 +593,6 @@ def test_deregister_instances_boto3(): balancer["Instances"][0]["InstanceId"].should.equal(instance_id2) -# Has boto3 equivalent -@mock_elb_deprecated -def test_default_attributes(): - conn = boto.connect_elb() - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - lb = conn.create_load_balancer("my-lb", [], ports) - attributes = lb.get_attributes() - - attributes.cross_zone_load_balancing.enabled.should.be.false - attributes.connection_draining.enabled.should.be.false - attributes.access_log.enabled.should.be.false - attributes.connecting_settings.idle_timeout.should.equal(60) - - @mock_elb def test_default_attributes_boto3(): lb_name = str(uuid4())[0:6] @@ -835,22 +613,6 @@ def test_default_attributes_boto3(): attributes.should.have.key("ConnectionSettings").equal({"IdleTimeout": 60}) -# Has boto3 equivalent -@mock_elb_deprecated -def test_cross_zone_load_balancing_attribute(): - conn = boto.connect_elb() - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - lb = conn.create_load_balancer("my-lb", [], ports) - - conn.modify_lb_attribute("my-lb", "CrossZoneLoadBalancing", True) - attributes = lb.get_attributes(force=True) - attributes.cross_zone_load_balancing.enabled.should.be.true - - conn.modify_lb_attribute("my-lb", "CrossZoneLoadBalancing", False) - attributes = lb.get_attributes(force=True) - attributes.cross_zone_load_balancing.enabled.should.be.false - - @mock_elb def test_cross_zone_load_balancing_attribute_boto3(): lb_name = str(uuid4())[0:6] @@ -870,8 +632,7 @@ def test_cross_zone_load_balancing_attribute_boto3(): attributes = client.describe_load_balancer_attributes(LoadBalancerName=lb_name)[ "LoadBalancerAttributes" ] - # Bug: This property is not properly propagated - attributes.should.have.key("CrossZoneLoadBalancing").equal({"Enabled": False}) + attributes.should.have.key("CrossZoneLoadBalancing").equal({"Enabled": True}) attributes.should.have.key("AccessLog").equal({"Enabled": False}) attributes.should.have.key("ConnectionDraining").equal({"Enabled": False}) attributes.should.have.key("ConnectionSettings").equal({"IdleTimeout": 60}) @@ -887,33 +648,6 @@ def test_cross_zone_load_balancing_attribute_boto3(): attributes.should.have.key("CrossZoneLoadBalancing").equal({"Enabled": False}) -# Has boto3 equivalent -@mock_elb_deprecated -def test_connection_draining_attribute(): - conn = boto.connect_elb() - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - lb = conn.create_load_balancer("my-lb", [], ports) - - connection_draining = ConnectionDrainingAttribute() - connection_draining.enabled = True - connection_draining.timeout = 60 - - conn.modify_lb_attribute("my-lb", "ConnectionDraining", connection_draining) - attributes = lb.get_attributes(force=True) - attributes.connection_draining.enabled.should.be.true - attributes.connection_draining.timeout.should.equal(60) - - connection_draining.timeout = 30 - conn.modify_lb_attribute("my-lb", "ConnectionDraining", connection_draining) - attributes = lb.get_attributes(force=True) - attributes.connection_draining.timeout.should.equal(30) - - connection_draining.enabled = False - conn.modify_lb_attribute("my-lb", "ConnectionDraining", connection_draining) - attributes = lb.get_attributes(force=True) - attributes.connection_draining.enabled.should.be.false - - @mock_elb def test_connection_draining_attribute_boto3(): lb_name = str(uuid4())[0:6] @@ -948,78 +682,87 @@ def test_connection_draining_attribute_boto3(): attributes.should.have.key("ConnectionDraining").equal({"Enabled": False}) -# This does not work in Boto3, so we can't write a equivalent test -# Moto always looks for attribute 's3_bucket_name', but Boto3 sends 'S3BucketName' -# We'll need to rewrite this feature completely anyway, to get rid of the boto-objects -@mock_elb_deprecated -def test_access_log_attribute(): - conn = boto.connect_elb() - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - lb = conn.create_load_balancer("my-lb", [], ports) +@mock_elb +def test_access_log_attribute_boto3(): + lb_name = str(uuid4())[0:6] - access_log = AccessLogAttribute() - access_log.enabled = True - access_log.s3_bucket_name = "bucket" - access_log.s3_bucket_prefix = "prefix" - access_log.emit_interval = 60 + client = boto3.client("elb", region_name="us-east-1") + client.create_load_balancer( + LoadBalancerName=lb_name, + Listeners=[{"Protocol": "http", "LoadBalancerPort": 80, "InstancePort": 8080}], + AvailabilityZones=["us-east-1a"], + ) - conn.modify_lb_attribute("my-lb", "AccessLog", access_log) - attributes = lb.get_attributes(force=True) - attributes.access_log.enabled.should.be.true - attributes.access_log.s3_bucket_name.should.equal("bucket") - attributes.access_log.s3_bucket_prefix.should.equal("prefix") - attributes.access_log.emit_interval.should.equal(60) + lb_attrs = client.describe_load_balancer_attributes(LoadBalancerName=lb_name)[ + "LoadBalancerAttributes" + ] + access_log = lb_attrs["AccessLog"] + access_log.should.equal({"Enabled": False}) - access_log.enabled = False - conn.modify_lb_attribute("my-lb", "AccessLog", access_log) - attributes = lb.get_attributes(force=True) - attributes.access_log.enabled.should.be.false + # Specify our AccessLog attributes + client.modify_load_balancer_attributes( + LoadBalancerName=lb_name, + LoadBalancerAttributes={ + "AccessLog": { + "Enabled": True, + "S3BucketName": "mb", + "EmitInterval": 42, + "S3BucketPrefix": "s3bf", + } + }, + ) + lb_attrs = client.describe_load_balancer_attributes(LoadBalancerName=lb_name)[ + "LoadBalancerAttributes" + ] + access_log = lb_attrs["AccessLog"] + access_log.should.equal( + { + "Enabled": True, + "S3BucketName": "mb", + "EmitInterval": 42, + "S3BucketPrefix": "s3bf", + } + ) + + # Verify the attribute can be reset + client.modify_load_balancer_attributes( + LoadBalancerName=lb_name, + LoadBalancerAttributes={"AccessLog": {"Enabled": False}}, + ) + lb_attrs = client.describe_load_balancer_attributes(LoadBalancerName=lb_name)[ + "LoadBalancerAttributes" + ] + access_log = lb_attrs["AccessLog"] + access_log.should.equal({"Enabled": False}) -# This does not work in Boto3, so we can't write a equivalent test -# Moto always looks for attribute 'idle_timeout', but Boto3 sends 'IdleTimeout' -# We'll need to rewrite this feature completely anyway, to get rid of the boto-objects -@mock_elb_deprecated -def test_connection_settings_attribute(): - conn = boto.connect_elb() - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - lb = conn.create_load_balancer("my-lb", [], ports) +@mock_elb +def test_connection_settings_attribute_boto3(): + lb_name = str(uuid4())[0:6] - connection_settings = ConnectionSettingAttribute(conn) - connection_settings.idle_timeout = 120 + client = boto3.client("elb", region_name="us-east-1") + client.create_load_balancer( + LoadBalancerName=lb_name, + Listeners=[{"Protocol": "http", "LoadBalancerPort": 80, "InstancePort": 8080}], + AvailabilityZones=["us-east-1a"], + ) - conn.modify_lb_attribute("my-lb", "ConnectingSettings", connection_settings) - attributes = lb.get_attributes(force=True) - attributes.connecting_settings.idle_timeout.should.equal(120) + lb_attrs = client.describe_load_balancer_attributes(LoadBalancerName=lb_name)[ + "LoadBalancerAttributes" + ] + conn_settings = lb_attrs["ConnectionSettings"] + conn_settings.should.equal({"IdleTimeout": 60}) - connection_settings.idle_timeout = 60 - conn.modify_lb_attribute("my-lb", "ConnectingSettings", connection_settings) - attributes = lb.get_attributes(force=True) - attributes.connecting_settings.idle_timeout.should.equal(60) - - -# Has boto3 equivalent -@mock_elb_deprecated -def test_create_lb_cookie_stickiness_policy(): - conn = boto.connect_elb() - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - lb = conn.create_load_balancer("my-lb", [], ports) - cookie_expiration_period = 60 - policy_name = "LBCookieStickinessPolicy" - - lb.create_cookie_stickiness_policy(cookie_expiration_period, policy_name) - - lb = conn.get_all_load_balancers()[0] - # There appears to be a quirk about boto, whereby it returns a unicode - # string for cookie_expiration_period, despite being stated in - # documentation to be a long numeric. - # - # To work around that, this value is converted to an int and checked. - cookie_expiration_period_response_str = lb.policies.lb_cookie_stickiness_policies[ - 0 - ].cookie_expiration_period - int(cookie_expiration_period_response_str).should.equal(cookie_expiration_period) - lb.policies.lb_cookie_stickiness_policies[0].policy_name.should.equal(policy_name) + # Specify our AccessLog attributes + client.modify_load_balancer_attributes( + LoadBalancerName=lb_name, + LoadBalancerAttributes={"ConnectionSettings": {"IdleTimeout": 123}}, + ) + lb_attrs = client.describe_load_balancer_attributes(LoadBalancerName=lb_name)[ + "LoadBalancerAttributes" + ] + conn_settings = lb_attrs["ConnectionSettings"] + conn_settings.should.equal({"IdleTimeout": 123}) @mock_elb @@ -1052,21 +795,6 @@ def test_create_lb_cookie_stickiness_policy_boto3(): lbc_policies[0].should.equal({"PolicyName": "pname", "CookieExpirationPeriod": 42}) -# Has boto3 equivalent -@mock_elb_deprecated -def test_create_lb_cookie_stickiness_policy_no_expiry(): - conn = boto.connect_elb() - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - lb = conn.create_load_balancer("my-lb", [], ports) - policy_name = "LBCookieStickinessPolicy" - - lb.create_cookie_stickiness_policy(None, policy_name) - - lb = conn.get_all_load_balancers()[0] - lb.policies.lb_cookie_stickiness_policies[0].cookie_expiration_period.should.be.none - lb.policies.lb_cookie_stickiness_policies[0].policy_name.should.equal(policy_name) - - @mock_elb def test_create_lb_cookie_stickiness_policy_no_expiry_boto3(): lb_name = str(uuid4())[0:6] @@ -1097,22 +825,6 @@ def test_create_lb_cookie_stickiness_policy_no_expiry_boto3(): lbc_policies[0].should.equal({"PolicyName": "pname"}) -# Has boto3 equivalent -@mock_elb_deprecated -def test_create_app_cookie_stickiness_policy(): - conn = boto.connect_elb() - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - lb = conn.create_load_balancer("my-lb", [], ports) - cookie_name = "my-stickiness-policy" - policy_name = "AppCookieStickinessPolicy" - - lb.create_app_cookie_stickiness_policy(cookie_name, policy_name) - - lb = conn.get_all_load_balancers()[0] - lb.policies.app_cookie_stickiness_policies[0].cookie_name.should.equal(cookie_name) - lb.policies.app_cookie_stickiness_policies[0].policy_name.should.equal(policy_name) - - @mock_elb def test_create_app_cookie_stickiness_policy_boto3(): lb_name = str(uuid4())[0:6] @@ -1143,20 +855,6 @@ def test_create_app_cookie_stickiness_policy_boto3(): lbc_policies[0].should.equal({"CookieName": "cname", "PolicyName": "pname"}) -# Has boto3 equivalent -@mock_elb_deprecated -def test_create_lb_policy(): - conn = boto.connect_elb() - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - lb = conn.create_load_balancer("my-lb", [], ports) - policy_name = "ProxyPolicy" - - lb.create_lb_policy(policy_name, "ProxyProtocolPolicyType", {"ProxyProtocol": True}) - - lb = conn.get_all_load_balancers()[0] - lb.policies.other_policies[0].policy_name.should.equal(policy_name) - - @mock_elb def test_create_lb_policy_boto3(): lb_name = str(uuid4())[0:6] @@ -1184,30 +882,6 @@ def test_create_lb_policy_boto3(): policies.should.have.key("OtherPolicies").equal(["ProxyPolicy"]) -# Has boto3 equivalent -@mock_elb_deprecated -def test_set_policies_of_listener(): - conn = boto.connect_elb() - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - lb = conn.create_load_balancer("my-lb", [], ports) - listener_port = 80 - policy_name = "my-stickiness-policy" - - # boto docs currently state that zero or one policy may be associated - # with a given listener - - # in a real flow, it is necessary first to create a policy, - # then to set that policy to the listener - lb.create_cookie_stickiness_policy(None, policy_name) - lb.set_policies_of_listener(listener_port, [policy_name]) - - lb = conn.get_all_load_balancers()[0] - listener = lb.listeners[0] - listener.load_balancer_port.should.equal(listener_port) - # by contrast to a backend, a listener stores only policy name strings - listener.policy_names[0].should.equal(policy_name) - - @mock_elb def test_set_policies_of_listener_boto3(): lb_name = str(uuid4())[0:6] @@ -1249,27 +923,6 @@ def test_set_policies_of_listener_boto3(): https_l.should.have.key("PolicyNames").should.equal(["pname"]) -# Has boto3 equivalent -@mock_elb_deprecated -def test_set_policies_of_backend_server(): - conn = boto.connect_elb() - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - lb = conn.create_load_balancer("my-lb", [], ports) - instance_port = 8080 - policy_name = "ProxyPolicy" - - # in a real flow, it is necessary first to create a policy, - # then to set that policy to the backend - lb.create_lb_policy(policy_name, "ProxyProtocolPolicyType", {"ProxyProtocol": True}) - lb.set_policies_of_backend_server(instance_port, [policy_name]) - - lb = conn.get_all_load_balancers()[0] - backend = lb.backends[0] - backend.instance_port.should.equal(instance_port) - # by contrast to a listener, a backend stores OtherPolicy objects - backend.policies[0].policy_name.should.equal(policy_name) - - @mock_elb def test_set_policies_of_backend_server_boto3(): lb_name = str(uuid4())[0:6] @@ -1301,37 +954,6 @@ def test_set_policies_of_backend_server_boto3(): desc[0].should.equal({"InstancePort": 8081, "PolicyNames": ["pname"]}) -# Has boto3 equivalent -@mock_ec2_deprecated -@mock_elb_deprecated -def test_describe_instance_health(): - ec2_conn = boto.connect_ec2() - reservation = ec2_conn.run_instances(EXAMPLE_AMI_ID, 2) - instance_id1 = reservation.instances[0].id - instance_id2 = reservation.instances[1].id - - conn = boto.connect_elb() - zones = ["us-east-1a", "us-east-1b"] - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - lb = conn.create_load_balancer("my-lb", zones, ports) - - instances_health = conn.describe_instance_health("my-lb") - instances_health.should.be.empty - - lb.register_instances([instance_id1, instance_id2]) - - instances_health = conn.describe_instance_health("my-lb") - instances_health.should.have.length_of(2) - for instance_health in instances_health: - instance_health.instance_id.should.be.within([instance_id1, instance_id2]) - instance_health.state.should.equal("InService") - - instances_health = conn.describe_instance_health("my-lb", [instance_id1]) - instances_health.should.have.length_of(1) - instances_health[0].instance_id.should.equal(instance_id1) - instances_health[0].state.should.equal("InService") - - @mock_ec2 @mock_elb def test_describe_instance_health_boto3(): @@ -1559,16 +1181,6 @@ def test_subnets(): lb.should.have.key("VPCId").which.should.equal(vpc.id) -@mock_elb_deprecated -def test_create_load_balancer_duplicate(): - conn = boto.connect_elb() - ports = [(80, 8080, "http"), (443, 8443, "tcp")] - conn.create_load_balancer("my-lb", [], ports) - conn.create_load_balancer.when.called_with("my-lb", [], ports).should.throw( - BotoServerError - ) - - @mock_elb def test_create_load_balancer_duplicate_boto3(): lb_name = str(uuid4())[0:6] diff --git a/tests/test_emr/test_emr.py b/tests/test_emr/test_emr.py deleted file mode 100644 index 79c167ad9..000000000 --- a/tests/test_emr/test_emr.py +++ /dev/null @@ -1,678 +0,0 @@ -import time -from datetime import datetime - -import boto -import pytz -from boto.emr.bootstrap_action import BootstrapAction -from boto.emr.instance_group import InstanceGroup -from boto.emr.step import StreamingStep - -import sure # noqa # pylint: disable=unused-import - -from moto import mock_emr_deprecated -from tests.helpers import requires_boto_gte - - -run_jobflow_args = dict( - job_flow_role="EMR_EC2_DefaultRole", - keep_alive=True, - log_uri="s3://some_bucket/jobflow_logs", - master_instance_type="c1.medium", - name="My jobflow", - num_instances=2, - service_role="EMR_DefaultRole", - slave_instance_type="c1.medium", -) - - -input_instance_groups = [ - InstanceGroup(1, "MASTER", "c1.medium", "ON_DEMAND", "master"), - InstanceGroup(3, "CORE", "c1.medium", "ON_DEMAND", "core"), - InstanceGroup(6, "TASK", "c1.large", "SPOT", "task-1", "0.07"), - InstanceGroup(10, "TASK", "c1.xlarge", "SPOT", "task-2", "0.05"), -] - - -# Has boto3 equivalent -@mock_emr_deprecated -def test_describe_cluster(): - conn = boto.connect_emr() - args = run_jobflow_args.copy() - args.update( - dict( - api_params={ - "Applications.member.1.Name": "Spark", - "Applications.member.1.Version": "2.4.2", - "Configurations.member.1.Classification": "yarn-site", - "Configurations.member.1.Properties.entry.1.key": "someproperty", - "Configurations.member.1.Properties.entry.1.value": "somevalue", - "Configurations.member.1.Properties.entry.2.key": "someotherproperty", - "Configurations.member.1.Properties.entry.2.value": "someothervalue", - "Instances.EmrManagedMasterSecurityGroup": "master-security-group", - "Instances.Ec2SubnetId": "subnet-8be41cec", - }, - availability_zone="us-east-2b", - ec2_keyname="mykey", - job_flow_role="EMR_EC2_DefaultRole", - keep_alive=False, - log_uri="s3://some_bucket/jobflow_logs", - name="My jobflow", - service_role="EMR_DefaultRole", - visible_to_all_users=True, - ) - ) - cluster_id = conn.run_jobflow(**args) - input_tags = {"tag1": "val1", "tag2": "val2"} - conn.add_tags(cluster_id, input_tags) - - cluster = conn.describe_cluster(cluster_id) - cluster.applications[0].name.should.equal("Spark") - cluster.applications[0].version.should.equal("2.4.2") - cluster.autoterminate.should.equal("true") - - # configurations appear not be supplied as attributes? - - attrs = cluster.ec2instanceattributes - # AdditionalMasterSecurityGroups - # AdditionalSlaveSecurityGroups - attrs.ec2availabilityzone.should.equal(args["availability_zone"]) - attrs.ec2keyname.should.equal(args["ec2_keyname"]) - attrs.ec2subnetid.should.equal(args["api_params"]["Instances.Ec2SubnetId"]) - # EmrManagedMasterSecurityGroups - # EmrManagedSlaveSecurityGroups - attrs.iaminstanceprofile.should.equal(args["job_flow_role"]) - # ServiceAccessSecurityGroup - - cluster.id.should.equal(cluster_id) - cluster.loguri.should.equal(args["log_uri"]) - cluster.masterpublicdnsname.should.be.a(str) - cluster.name.should.equal(args["name"]) - int(cluster.normalizedinstancehours).should.equal(0) - # cluster.release_label - cluster.shouldnt.have.property("requestedamiversion") - cluster.runningamiversion.should.equal("1.0.0") - # cluster.securityconfiguration - cluster.servicerole.should.equal(args["service_role"]) - - cluster.status.state.should.equal("TERMINATED") - cluster.status.statechangereason.message.should.be.a(str) - cluster.status.statechangereason.code.should.be.a(str) - cluster.status.timeline.creationdatetime.should.be.a(str) - # cluster.status.timeline.enddatetime.should.be.a(str) - # cluster.status.timeline.readydatetime.should.be.a(str) - - dict((item.key, item.value) for item in cluster.tags).should.equal(input_tags) - - cluster.terminationprotected.should.equal("false") - cluster.visibletoallusers.should.equal("true") - - -# Has boto3 equivalent -@mock_emr_deprecated -def test_describe_jobflows(): - conn = boto.connect_emr() - args = run_jobflow_args.copy() - expected = {} - - for idx in range(4): - cluster_name = "cluster" + str(idx) - args["name"] = cluster_name - cluster_id = conn.run_jobflow(**args) - expected[cluster_id] = { - "id": cluster_id, - "name": cluster_name, - "state": "WAITING", - } - - # need sleep since it appears the timestamp is always rounded to - # the nearest second internally - time.sleep(1) - timestamp = datetime.now(pytz.utc) - time.sleep(1) - - for idx in range(4, 6): - cluster_name = "cluster" + str(idx) - args["name"] = cluster_name - cluster_id = conn.run_jobflow(**args) - conn.terminate_jobflow(cluster_id) - expected[cluster_id] = { - "id": cluster_id, - "name": cluster_name, - "state": "TERMINATED", - } - jobs = conn.describe_jobflows() - jobs.should.have.length_of(6) - - for cluster_id in expected: - resp = conn.describe_jobflows(jobflow_ids=[cluster_id]) - resp.should.have.length_of(1) - resp[0].jobflowid.should.equal(cluster_id) - - resp = conn.describe_jobflows(states=["WAITING"]) - resp.should.have.length_of(4) - for x in resp: - x.state.should.equal("WAITING") - - resp = conn.describe_jobflows(created_before=timestamp) - resp.should.have.length_of(4) - - resp = conn.describe_jobflows(created_after=timestamp) - resp.should.have.length_of(2) - - -# Has boto3 equivalent -@mock_emr_deprecated -def test_describe_jobflow(): - conn = boto.connect_emr() - args = run_jobflow_args.copy() - args.update( - dict( - ami_version="3.8.1", - api_params={ - #'Applications.member.1.Name': 'Spark', - #'Applications.member.1.Version': '2.4.2', - #'Configurations.member.1.Classification': 'yarn-site', - #'Configurations.member.1.Properties.entry.1.key': 'someproperty', - #'Configurations.member.1.Properties.entry.1.value': 'somevalue', - #'Instances.EmrManagedMasterSecurityGroup': 'master-security-group', - "Instances.Ec2SubnetId": "subnet-8be41cec" - }, - ec2_keyname="mykey", - hadoop_version="2.4.0", - name="My jobflow", - log_uri="s3://some_bucket/jobflow_logs", - keep_alive=True, - master_instance_type="c1.medium", - slave_instance_type="c1.medium", - num_instances=2, - availability_zone="us-west-2b", - job_flow_role="EMR_EC2_DefaultRole", - service_role="EMR_DefaultRole", - visible_to_all_users=True, - ) - ) - - cluster_id = conn.run_jobflow(**args) - jf = conn.describe_jobflow(cluster_id) - jf.amiversion.should.equal(args["ami_version"]) - jf.bootstrapactions.should.equal(None) - jf.creationdatetime.should.be.a(str) - jf.should.have.property("laststatechangereason") - jf.readydatetime.should.be.a(str) - jf.startdatetime.should.be.a(str) - jf.state.should.equal("WAITING") - - jf.ec2keyname.should.equal(args["ec2_keyname"]) - # Ec2SubnetId - jf.hadoopversion.should.equal(args["hadoop_version"]) - int(jf.instancecount).should.equal(2) - - for ig in jf.instancegroups: - ig.creationdatetime.should.be.a(str) - # ig.enddatetime.should.be.a(str) - ig.should.have.property("instancegroupid").being.a(str) - int(ig.instancerequestcount).should.equal(1) - ig.instancerole.should.be.within(["MASTER", "CORE"]) - int(ig.instancerunningcount).should.equal(1) - ig.instancetype.should.equal("c1.medium") - ig.laststatechangereason.should.be.a(str) - ig.market.should.equal("ON_DEMAND") - ig.name.should.be.a(str) - ig.readydatetime.should.be.a(str) - ig.startdatetime.should.be.a(str) - ig.state.should.equal("RUNNING") - - jf.keepjobflowalivewhennosteps.should.equal("true") - jf.masterinstanceid.should.be.a(str) - jf.masterinstancetype.should.equal(args["master_instance_type"]) - jf.masterpublicdnsname.should.be.a(str) - int(jf.normalizedinstancehours).should.equal(0) - jf.availabilityzone.should.equal(args["availability_zone"]) - jf.slaveinstancetype.should.equal(args["slave_instance_type"]) - jf.terminationprotected.should.equal("false") - - jf.jobflowid.should.equal(cluster_id) - # jf.jobflowrole.should.equal(args['job_flow_role']) - jf.loguri.should.equal(args["log_uri"]) - jf.name.should.equal(args["name"]) - # jf.servicerole.should.equal(args['service_role']) - - jf.steps.should.have.length_of(0) - - list(i.value for i in jf.supported_products).should.equal([]) - jf.visibletoallusers.should.equal("true") - - -# Has boto3 equivalent -@mock_emr_deprecated -def test_list_clusters(): - conn = boto.connect_emr() - args = run_jobflow_args.copy() - expected = {} - - for idx in range(40): - cluster_name = "jobflow" + str(idx) - args["name"] = cluster_name - cluster_id = conn.run_jobflow(**args) - expected[cluster_id] = { - "id": cluster_id, - "name": cluster_name, - "normalizedinstancehours": "0", - "state": "WAITING", - } - - # need sleep since it appears the timestamp is always rounded to - # the nearest second internally - time.sleep(1) - timestamp = datetime.now(pytz.utc) - time.sleep(1) - - for idx in range(40, 70): - cluster_name = "jobflow" + str(idx) - args["name"] = cluster_name - cluster_id = conn.run_jobflow(**args) - conn.terminate_jobflow(cluster_id) - expected[cluster_id] = { - "id": cluster_id, - "name": cluster_name, - "normalizedinstancehours": "0", - "state": "TERMINATED", - } - - args = {} - while 1: - resp = conn.list_clusters(**args) - clusters = resp.clusters - len(clusters).should.be.lower_than_or_equal_to(50) - for x in clusters: - y = expected[x.id] - x.id.should.equal(y["id"]) - x.name.should.equal(y["name"]) - x.normalizedinstancehours.should.equal(y["normalizedinstancehours"]) - x.status.state.should.equal(y["state"]) - x.status.timeline.creationdatetime.should.be.a(str) - if y["state"] == "TERMINATED": - x.status.timeline.enddatetime.should.be.a(str) - else: - x.status.timeline.shouldnt.have.property("enddatetime") - x.status.timeline.readydatetime.should.be.a(str) - if not hasattr(resp, "marker"): - break - args = {"marker": resp.marker} - - resp = conn.list_clusters(cluster_states=["TERMINATED"]) - resp.clusters.should.have.length_of(30) - for x in resp.clusters: - x.status.state.should.equal("TERMINATED") - - resp = conn.list_clusters(created_before=timestamp) - resp.clusters.should.have.length_of(40) - - resp = conn.list_clusters(created_after=timestamp) - resp.clusters.should.have.length_of(30) - - -# Has boto3 equivalent -@mock_emr_deprecated -def test_run_jobflow(): - conn = boto.connect_emr() - args = run_jobflow_args.copy() - job_id = conn.run_jobflow(**args) - job_flow = conn.describe_jobflow(job_id) - job_flow.state.should.equal("WAITING") - job_flow.jobflowid.should.equal(job_id) - job_flow.name.should.equal(args["name"]) - job_flow.masterinstancetype.should.equal(args["master_instance_type"]) - job_flow.slaveinstancetype.should.equal(args["slave_instance_type"]) - job_flow.loguri.should.equal(args["log_uri"]) - job_flow.visibletoallusers.should.equal("false") - int(job_flow.normalizedinstancehours).should.equal(0) - job_flow.steps.should.have.length_of(0) - - -# Has boto3 equivalent -@mock_emr_deprecated -def test_run_jobflow_in_multiple_regions(): - regions = {} - for region in ["us-east-1", "eu-west-1"]: - conn = boto.emr.connect_to_region(region) - args = run_jobflow_args.copy() - args["name"] = region - cluster_id = conn.run_jobflow(**args) - regions[region] = {"conn": conn, "cluster_id": cluster_id} - - for region in regions.keys(): - conn = regions[region]["conn"] - jf = conn.describe_jobflow(regions[region]["cluster_id"]) - jf.name.should.equal(region) - - -# Has boto3 equivalent -@requires_boto_gte("2.8") -@mock_emr_deprecated -def test_run_jobflow_with_new_params(): - # Test that run_jobflow works with newer params - conn = boto.connect_emr() - conn.run_jobflow(**run_jobflow_args) - - -# Has boto3 equivalent -@requires_boto_gte("2.8") -@mock_emr_deprecated -def test_run_jobflow_with_visible_to_all_users(): - conn = boto.connect_emr() - for expected in (True, False): - job_id = conn.run_jobflow(visible_to_all_users=expected, **run_jobflow_args) - job_flow = conn.describe_jobflow(job_id) - job_flow.visibletoallusers.should.equal(str(expected).lower()) - - -# Has boto3 equivalent -@requires_boto_gte("2.8") -@mock_emr_deprecated -def test_run_jobflow_with_instance_groups(): - input_groups = dict((g.name, g) for g in input_instance_groups) - conn = boto.connect_emr() - job_id = conn.run_jobflow(instance_groups=input_instance_groups, **run_jobflow_args) - job_flow = conn.describe_jobflow(job_id) - int(job_flow.instancecount).should.equal( - sum(g.num_instances for g in input_instance_groups) - ) - for instance_group in job_flow.instancegroups: - expected = input_groups[instance_group.name] - instance_group.should.have.property("instancegroupid") - int(instance_group.instancerunningcount).should.equal(expected.num_instances) - instance_group.instancerole.should.equal(expected.role) - instance_group.instancetype.should.equal(expected.type) - instance_group.market.should.equal(expected.market) - if hasattr(expected, "bidprice"): - instance_group.bidprice.should.equal(expected.bidprice) - - -# Has boto3 equivalent -@requires_boto_gte("2.8") -@mock_emr_deprecated -def test_set_termination_protection(): - conn = boto.connect_emr() - job_id = conn.run_jobflow(**run_jobflow_args) - job_flow = conn.describe_jobflow(job_id) - job_flow.terminationprotected.should.equal("false") - - conn.set_termination_protection(job_id, True) - job_flow = conn.describe_jobflow(job_id) - job_flow.terminationprotected.should.equal("true") - - conn.set_termination_protection(job_id, False) - job_flow = conn.describe_jobflow(job_id) - job_flow.terminationprotected.should.equal("false") - - -# Has boto3 equivalent -@requires_boto_gte("2.8") -@mock_emr_deprecated -def test_set_visible_to_all_users(): - conn = boto.connect_emr() - args = run_jobflow_args.copy() - args["visible_to_all_users"] = False - job_id = conn.run_jobflow(**args) - job_flow = conn.describe_jobflow(job_id) - job_flow.visibletoallusers.should.equal("false") - - conn.set_visible_to_all_users(job_id, True) - job_flow = conn.describe_jobflow(job_id) - job_flow.visibletoallusers.should.equal("true") - - conn.set_visible_to_all_users(job_id, False) - job_flow = conn.describe_jobflow(job_id) - job_flow.visibletoallusers.should.equal("false") - - -# Has boto3 equivalent -@mock_emr_deprecated -def test_terminate_jobflow(): - conn = boto.connect_emr() - job_id = conn.run_jobflow(**run_jobflow_args) - flow = conn.describe_jobflows()[0] - flow.state.should.equal("WAITING") - - conn.terminate_jobflow(job_id) - flow = conn.describe_jobflows()[0] - flow.state.should.equal("TERMINATED") - - -# testing multiple end points for each feature - - -# Has boto3 equivalent -@mock_emr_deprecated -def test_bootstrap_actions(): - bootstrap_actions = [ - BootstrapAction( - name="bs1", - path="path/to/script", - bootstrap_action_args=["arg1", "arg2&arg3"], - ), - BootstrapAction( - name="bs2", path="path/to/anotherscript", bootstrap_action_args=[] - ), - ] - - conn = boto.connect_emr() - cluster_id = conn.run_jobflow( - bootstrap_actions=bootstrap_actions, **run_jobflow_args - ) - - jf = conn.describe_jobflow(cluster_id) - for x, y in zip(jf.bootstrapactions, bootstrap_actions): - x.name.should.equal(y.name) - x.path.should.equal(y.path) - list(o.value for o in x.args).should.equal(y.args()) - - resp = conn.list_bootstrap_actions(cluster_id) - for i, y in enumerate(bootstrap_actions): - x = resp.actions[i] - x.name.should.equal(y.name) - x.scriptpath.should.equal(y.path) - list(arg.value for arg in x.args).should.equal(y.args()) - - -# Has boto3 equivalent -@mock_emr_deprecated -def test_instance_groups(): - input_groups = dict((g.name, g) for g in input_instance_groups) - - conn = boto.connect_emr() - args = run_jobflow_args.copy() - for key in ["master_instance_type", "slave_instance_type", "num_instances"]: - del args[key] - args["instance_groups"] = input_instance_groups[:2] - job_id = conn.run_jobflow(**args) - - jf = conn.describe_jobflow(job_id) - base_instance_count = int(jf.instancecount) - - conn.add_instance_groups(job_id, input_instance_groups[2:]) - - jf = conn.describe_jobflow(job_id) - int(jf.instancecount).should.equal( - sum(g.num_instances for g in input_instance_groups) - ) - for x in jf.instancegroups: - y = input_groups[x.name] - if hasattr(y, "bidprice"): - x.bidprice.should.equal(y.bidprice) - x.creationdatetime.should.be.a(str) - # x.enddatetime.should.be.a(str) - x.should.have.property("instancegroupid") - int(x.instancerequestcount).should.equal(y.num_instances) - x.instancerole.should.equal(y.role) - int(x.instancerunningcount).should.equal(y.num_instances) - x.instancetype.should.equal(y.type) - x.laststatechangereason.should.be.a(str) - x.market.should.equal(y.market) - x.name.should.be.a(str) - x.readydatetime.should.be.a(str) - x.startdatetime.should.be.a(str) - x.state.should.equal("RUNNING") - - for x in conn.list_instance_groups(job_id).instancegroups: - y = input_groups[x.name] - if hasattr(y, "bidprice"): - x.bidprice.should.equal(y.bidprice) - # Configurations - # EbsBlockDevices - # EbsOptimized - x.should.have.property("id") - x.instancegrouptype.should.equal(y.role) - x.instancetype.should.equal(y.type) - x.market.should.equal(y.market) - x.name.should.equal(y.name) - int(x.requestedinstancecount).should.equal(y.num_instances) - int(x.runninginstancecount).should.equal(y.num_instances) - # ShrinkPolicy - x.status.state.should.equal("RUNNING") - x.status.statechangereason.code.should.be.a(str) - x.status.statechangereason.message.should.be.a(str) - x.status.timeline.creationdatetime.should.be.a(str) - # x.status.timeline.enddatetime.should.be.a(str) - x.status.timeline.readydatetime.should.be.a(str) - - igs = dict((g.name, g) for g in jf.instancegroups) - - conn.modify_instance_groups( - [igs["task-1"].instancegroupid, igs["task-2"].instancegroupid], [2, 3] - ) - jf = conn.describe_jobflow(job_id) - int(jf.instancecount).should.equal(base_instance_count + 5) - igs = dict((g.name, g) for g in jf.instancegroups) - int(igs["task-1"].instancerunningcount).should.equal(2) - int(igs["task-2"].instancerunningcount).should.equal(3) - - -# Has boto3 equivalent -@mock_emr_deprecated -def test_steps(): - input_steps = [ - StreamingStep( - name="My wordcount example", - mapper="s3n://elasticmapreduce/samples/wordcount/wordSplitter.py", - reducer="aggregate", - input="s3n://elasticmapreduce/samples/wordcount/input", - output="s3n://output_bucket/output/wordcount_output", - ), - StreamingStep( - name="My wordcount example & co.", - mapper="s3n://elasticmapreduce/samples/wordcount/wordSplitter2.py", - reducer="aggregate", - input="s3n://elasticmapreduce/samples/wordcount/input2", - output="s3n://output_bucket/output/wordcount_output2", - ), - ] - - # TODO: implementation and test for cancel_steps - - conn = boto.connect_emr() - cluster_id = conn.run_jobflow(steps=[input_steps[0]], **run_jobflow_args) - - jf = conn.describe_jobflow(cluster_id) - jf.steps.should.have.length_of(1) - - conn.add_jobflow_steps(cluster_id, [input_steps[1]]) - - jf = conn.describe_jobflow(cluster_id) - jf.steps.should.have.length_of(2) - for step in jf.steps: - step.actiononfailure.should.equal("TERMINATE_JOB_FLOW") - list(arg.value for arg in step.args).should.have.length_of(8) - step.creationdatetime.should.be.a(str) - # step.enddatetime.should.be.a(str) - step.jar.should.equal("/home/hadoop/contrib/streaming/hadoop-streaming.jar") - step.laststatechangereason.should.be.a(str) - step.mainclass.should.equal("") - step.name.should.be.a(str) - # step.readydatetime.should.be.a(str) - # step.startdatetime.should.be.a(str) - step.state.should.be.within(["RUNNING", "PENDING"]) - - expected = dict((s.name, s) for s in input_steps) - - steps = conn.list_steps(cluster_id).steps - for x in steps: - y = expected[x.name] - # actiononfailure - list(arg.value for arg in x.config.args).should.equal( - [ - "-mapper", - y.mapper, - "-reducer", - y.reducer, - "-input", - y.input, - "-output", - y.output, - ] - ) - x.config.jar.should.equal("/home/hadoop/contrib/streaming/hadoop-streaming.jar") - x.config.mainclass.should.equal("") - # properties - x.should.have.property("id").should.be.a(str) - x.name.should.equal(y.name) - x.status.state.should.be.within(["RUNNING", "PENDING"]) - # x.status.statechangereason - x.status.timeline.creationdatetime.should.be.a(str) - # x.status.timeline.enddatetime.should.be.a(str) - # x.status.timeline.startdatetime.should.be.a(str) - - x = conn.describe_step(cluster_id, x.id) - list(arg.value for arg in x.config.args).should.equal( - [ - "-mapper", - y.mapper, - "-reducer", - y.reducer, - "-input", - y.input, - "-output", - y.output, - ] - ) - x.config.jar.should.equal("/home/hadoop/contrib/streaming/hadoop-streaming.jar") - x.config.mainclass.should.equal("") - # properties - x.should.have.property("id").should.be.a(str) - x.name.should.equal(y.name) - x.status.state.should.be.within(["RUNNING", "PENDING"]) - # x.status.statechangereason - x.status.timeline.creationdatetime.should.be.a(str) - # x.status.timeline.enddatetime.should.be.a(str) - # x.status.timeline.startdatetime.should.be.a(str) - - @requires_boto_gte("2.39") - def test_list_steps_with_states(): - # boto's list_steps prior to 2.39 has a bug that ignores - # step_states argument. - steps = conn.list_steps(cluster_id).steps - step_id = steps[0].id - steps = conn.list_steps(cluster_id, step_states=["RUNNING"]).steps - steps.should.have.length_of(1) - steps[0].id.should.equal(step_id) - - test_list_steps_with_states() - - -# Has boto3 equivalent -@mock_emr_deprecated -def test_tags(): - input_tags = {"tag1": "val1", "tag2": "val2"} - - conn = boto.connect_emr() - cluster_id = conn.run_jobflow(**run_jobflow_args) - - conn.add_tags(cluster_id, input_tags) - cluster = conn.describe_cluster(cluster_id) - cluster.tags.should.have.length_of(2) - dict((t.key, t.value) for t in cluster.tags).should.equal(input_tags) - - conn.remove_tags(cluster_id, list(input_tags.keys())) - cluster = conn.describe_cluster(cluster_id) - cluster.tags.should.have.length_of(0) diff --git a/tests/test_glacier/test_glacier_archives.py b/tests/test_glacier/test_glacier_archives.py index 0f884059e..935c83d70 100644 --- a/tests/test_glacier/test_glacier_archives.py +++ b/tests/test_glacier/test_glacier_archives.py @@ -1,24 +1,8 @@ -from tempfile import NamedTemporaryFile import boto3 -import boto.glacier import sure # noqa # pylint: disable=unused-import import pytest -from moto import mock_glacier_deprecated, mock_glacier - - -@mock_glacier_deprecated -def test_create_and_delete_archive(): - the_file = NamedTemporaryFile(delete=False) - the_file.write(b"some stuff") - the_file.close() - - conn = boto.glacier.connect_to_region("us-west-2") - vault = conn.create_vault("my_vault") - - archive_id = vault.upload_archive(the_file.name) - - vault.delete_archive(archive_id) +from moto import mock_glacier @mock_glacier diff --git a/tests/test_glacier/test_glacier_jobs.py b/tests/test_glacier/test_glacier_jobs.py index b8645c14c..d4b99a571 100644 --- a/tests/test_glacier/test_glacier_jobs.py +++ b/tests/test_glacier/test_glacier_jobs.py @@ -1,31 +1,11 @@ import boto3 -import json - -from boto.glacier.layer1 import Layer1 import sure # noqa # pylint: disable=unused-import import time -from moto import mock_glacier_deprecated, mock_glacier +from moto import mock_glacier from moto.core import ACCOUNT_ID -# Has boto3 equivalent -@mock_glacier_deprecated -def test_init_glacier_job(): - conn = Layer1(region_name="us-west-2") - vault_name = "my_vault" - conn.create_vault(vault_name) - archive_id = conn.upload_archive( - vault_name, "some stuff", "", "", "some description" - ) - - job_response = conn.initiate_job( - vault_name, {"ArchiveId": archive_id, "Type": "archive-retrieval"} - ) - job_id = job_response["JobId"] - job_response["Location"].should.equal("//vaults/my_vault/jobs/{0}".format(job_id)) - - @mock_glacier def test_initiate_job(): client = boto3.client("glacier", region_name="us-west-2") @@ -52,30 +32,6 @@ def test_initiate_job(): job.should.have.key("location") -# Has boto3 equivalent -@mock_glacier_deprecated -def test_describe_job(): - conn = Layer1(region_name="us-west-2") - vault_name = "my_vault" - conn.create_vault(vault_name) - archive_id = conn.upload_archive( - vault_name, "some stuff", "", "", "some description" - ) - job_response = conn.initiate_job( - vault_name, {"ArchiveId": archive_id, "Type": "archive-retrieval"} - ) - job_id = job_response["JobId"] - - job = conn.describe_job(vault_name, job_id) - joboutput = json.loads(job.read().decode("utf-8")) - - joboutput.should.have.key("Tier").which.should.equal("Standard") - joboutput.should.have.key("StatusCode").which.should.equal("InProgress") - joboutput.should.have.key("VaultARN").which.should.equal( - f"arn:aws:glacier:us-west-2:{ACCOUNT_ID}:vaults/my_vault" - ) - - @mock_glacier def test_describe_job_boto3(): client = boto3.client("glacier", region_name="us-west-2") @@ -104,30 +60,6 @@ def test_describe_job_boto3(): describe.should.have.key("Tier").equal("Standard") -# Has boto3 equivalent -@mock_glacier_deprecated -def test_list_glacier_jobs(): - conn = Layer1(region_name="us-west-2") - vault_name = "my_vault" - conn.create_vault(vault_name) - archive_id1 = conn.upload_archive( - vault_name, "some stuff", "", "", "some description" - )["ArchiveId"] - archive_id2 = conn.upload_archive( - vault_name, "some other stuff", "", "", "some description" - )["ArchiveId"] - - conn.initiate_job( - vault_name, {"ArchiveId": archive_id1, "Type": "archive-retrieval"} - ) - conn.initiate_job( - vault_name, {"ArchiveId": archive_id2, "Type": "archive-retrieval"} - ) - - jobs = conn.list_jobs(vault_name) - len(jobs["JobList"]).should.equal(2) - - @mock_glacier def test_list_jobs(): client = boto3.client("glacier", region_name="us-west-2") @@ -171,27 +103,6 @@ def test_list_jobs(): job.should.have.key("Tier") -# Has boto3 equivalent -@mock_glacier_deprecated -def test_get_job_output(): - conn = Layer1(region_name="us-west-2") - vault_name = "my_vault" - conn.create_vault(vault_name) - archive_response = conn.upload_archive( - vault_name, "some stuff", "", "", "some description" - ) - archive_id = archive_response["ArchiveId"] - job_response = conn.initiate_job( - vault_name, {"ArchiveId": archive_id, "Type": "archive-retrieval"} - ) - job_id = job_response["JobId"] - - time.sleep(6) - - output = conn.get_job_output(vault_name, job_id) - output.read().decode("utf-8").should.equal("some stuff") - - @mock_glacier def test_get_job_output_boto3(): client = boto3.client("glacier", region_name="us-west-2") diff --git a/tests/test_glacier/test_glacier_vaults.py b/tests/test_glacier/test_glacier_vaults.py index f8d7d6751..550ddfc28 100644 --- a/tests/test_glacier/test_glacier_vaults.py +++ b/tests/test_glacier/test_glacier_vaults.py @@ -1,25 +1,12 @@ -import boto.glacier import boto3 import sure # noqa # pylint: disable=unused-import import pytest -from moto import mock_glacier_deprecated, mock_glacier +from moto import mock_glacier from moto.core import ACCOUNT_ID from uuid import uuid4 -# Has boto3 equivalent -@mock_glacier_deprecated -def test_create_vault(): - conn = boto.glacier.connect_to_region("us-west-2") - - conn.create_vault("my_vault") - - vaults = conn.list_vaults() - vaults.should.have.length_of(1) - vaults[0].name.should.equal("my_vault") - - @mock_glacier def test_describe_vault(): client = boto3.client("glacier", region_name="us-west-2") @@ -37,21 +24,6 @@ def test_describe_vault(): ) -# Has boto3 equivalent -@mock_glacier_deprecated -def test_delete_vault(): - conn = boto.glacier.connect_to_region("us-west-2") - - conn.create_vault("my_vault") - - vaults = conn.list_vaults() - vaults.should.have.length_of(1) - - conn.delete_vault("my_vault") - vaults = conn.list_vaults() - vaults.should.have.length_of(0) - - @mock_glacier def test_delete_vault_boto3(): client = boto3.client("glacier", region_name="us-west-2") diff --git a/tests/test_iam/test_iam.py b/tests/test_iam/test_iam.py index bb6bc16cd..3caa9f47d 100644 --- a/tests/test_iam/test_iam.py +++ b/tests/test_iam/test_iam.py @@ -1,21 +1,17 @@ -import base64 import json -import boto import boto3 import csv import sure # noqa # pylint: disable=unused-import -from boto.exception import BotoServerError from botocore.exceptions import ClientError -from moto import mock_config, mock_iam, mock_iam_deprecated, settings +from moto import mock_config, mock_iam, settings from moto.core import ACCOUNT_ID from moto.iam.models import aws_managed_policies from moto.backends import get_backend import pytest from datetime import datetime -from tests.helpers import requires_boto_gte from uuid import uuid4 from urllib import parse @@ -71,82 +67,6 @@ MOCK_POLICY_3 = """ """ -# Has boto3 equivalent -@mock_iam_deprecated() -def test_get_all_server_certs(): - conn = boto.connect_iam() - - conn.upload_server_cert("certname", "certbody", "privatekey") - certs = conn.get_all_server_certs()["list_server_certificates_response"][ - "list_server_certificates_result" - ]["server_certificate_metadata_list"] - certs.should.have.length_of(1) - cert1 = certs[0] - cert1.server_certificate_name.should.equal("certname") - cert1.arn.should.equal( - "arn:aws:iam::{}:server-certificate/certname".format(ACCOUNT_ID) - ) - - -# Has boto3 equivalent -@mock_iam_deprecated() -def test_get_server_cert_doesnt_exist(): - conn = boto.connect_iam() - - with pytest.raises(BotoServerError): - conn.get_server_certificate("NonExistant") - - -# Has boto3 equivalent -@mock_iam_deprecated() -def test_get_server_cert(): - conn = boto.connect_iam() - - conn.upload_server_cert("certname", "certbody", "privatekey") - cert = conn.get_server_certificate("certname") - cert.server_certificate_name.should.equal("certname") - cert.arn.should.equal( - "arn:aws:iam::{}:server-certificate/certname".format(ACCOUNT_ID) - ) - - -# Has boto3 equivalent -@mock_iam_deprecated() -def test_upload_server_cert(): - conn = boto.connect_iam() - - conn.upload_server_cert("certname", "certbody", "privatekey") - cert = conn.get_server_certificate("certname") - cert.server_certificate_name.should.equal("certname") - cert.arn.should.equal( - "arn:aws:iam::{}:server-certificate/certname".format(ACCOUNT_ID) - ) - - -# Has boto3 equivalent -@mock_iam_deprecated() -def test_delete_server_cert(): - conn = boto.connect_iam() - - conn.upload_server_cert("certname", "certbody", "privatekey") - conn.get_server_certificate("certname") - conn.delete_server_cert("certname") - with pytest.raises(BotoServerError): - conn.get_server_certificate("certname") - with pytest.raises(BotoServerError): - conn.delete_server_cert("certname") - - -# Has boto3 equivalent -@mock_iam_deprecated() -def test_get_role__should_throw__when_role_does_not_exist(): - conn = boto.connect_iam() - with pytest.raises(BotoServerError) as ex: - conn.get_role("unexisting_role") - ex.value.error_code.should.equal("NoSuchEntity") - ex.value.message.should.contain("not found") - - @mock_iam def test_get_role__should_throw__when_role_does_not_exist_boto3(): conn = boto3.client("iam", region_name="us-east-1") @@ -157,16 +77,6 @@ def test_get_role__should_throw__when_role_does_not_exist_boto3(): err["Message"].should.contain("not found") -# Has boto3 equivalent -@mock_iam_deprecated() -def test_get_instance_profile__should_throw__when_instance_profile_does_not_exist(): - conn = boto.connect_iam() - with pytest.raises(BotoServerError) as ex: - conn.get_instance_profile("unexisting_instance_profile") - ex.value.error_code.should.equal("NoSuchEntity") - ex.value.message.should.contain("not found") - - @mock_iam def test_get_instance_profile__should_throw__when_instance_profile_does_not_exist_boto3(): conn = boto3.client("iam", region_name="us-east-1") @@ -177,34 +87,6 @@ def test_get_instance_profile__should_throw__when_instance_profile_does_not_exis err["Message"].should.contain("not found") -# Has boto3 equivalent -@mock_iam_deprecated() -def test_create_role_and_instance_profile(): - conn = boto.connect_iam() - conn.create_instance_profile("my-profile", path="my-path") - conn.create_role( - "my-role", assume_role_policy_document="some policy", path="/my-path/" - ) - - conn.add_role_to_instance_profile("my-profile", "my-role") - - role = conn.get_role("my-role") - role.path.should.equal("/my-path/") - role.assume_role_policy_document.should.equal("some policy") - - profile = conn.get_instance_profile("my-profile") - profile.path.should.equal("my-path") - role_from_profile = list(profile.roles.values())[0] - role_from_profile["role_id"].should.equal(role.role_id) - role_from_profile["role_name"].should.equal("my-role") - - conn.list_roles().roles[0].role_name.should.equal("my-role") - - # Test with an empty path: - profile = conn.create_instance_profile("my-other-profile") - profile.path.should.equal("/") - - @mock_iam def test_create_role_and_instance_profile_boto3(): conn = boto3.client("iam", region_name="us-east-1") @@ -246,26 +128,6 @@ def test_create_instance_profile_should_throw_when_name_is_not_unique(): conn.create_instance_profile(InstanceProfileName="unique-instance-profile") -# Has boto3 equivalent -@mock_iam_deprecated() -def test_remove_role_from_instance_profile(): - conn = boto.connect_iam() - conn.create_instance_profile("my-profile", path="my-path") - conn.create_role( - "my-role", assume_role_policy_document="some policy", path="my-path" - ) - conn.add_role_to_instance_profile("my-profile", "my-role") - - profile = conn.get_instance_profile("my-profile") - role_from_profile = list(profile.roles.values())[0] - role_from_profile["role_name"].should.equal("my-role") - - conn.remove_role_from_instance_profile("my-profile", "my-role") - - profile = conn.get_instance_profile("my-profile") - dict(profile.roles).should.be.empty - - @mock_iam def test_remove_role_from_instance_profile_boto3(): conn = boto3.client("iam", region_name="us-east-1") @@ -400,22 +262,6 @@ def test_delete_role(): conn.get_role(RoleName="my-role") -# Has boto3 equivalent -@mock_iam_deprecated() -def test_list_instance_profiles(): - conn = boto.connect_iam() - conn.create_instance_profile("my-profile", path="my-path") - conn.create_role("my-role", path="my-path") - - conn.add_role_to_instance_profile("my-profile", "my-role") - - profiles = conn.list_instance_profiles().instance_profiles - - len(profiles).should.equal(1) - profiles[0].instance_profile_name.should.equal("my-profile") - profiles[0].roles.role_name.should.equal("my-role") - - @mock_iam def test_list_instance_profiles_boto3(): conn = boto3.client("iam", region_name="us-east-1") @@ -435,51 +281,6 @@ def test_list_instance_profiles_boto3(): profiles[0]["Roles"][0]["RoleName"].should.equal("my-role") -# Has boto3 equivalent -@mock_iam_deprecated() -def test_list_instance_profiles_for_role(): - conn = boto.connect_iam() - - conn.create_role( - role_name="my-role", assume_role_policy_document="some policy", path="my-path", - ) - conn.create_role( - role_name="my-role2", - assume_role_policy_document="some policy2", - path="my-path2", - ) - - profile_name_list = ["my-profile", "my-profile2"] - profile_path_list = ["my-path", "my-path2"] - for profile_count in range(0, 2): - conn.create_instance_profile( - profile_name_list[profile_count], path=profile_path_list[profile_count], - ) - - for profile_count in range(0, 2): - conn.add_role_to_instance_profile(profile_name_list[profile_count], "my-role") - - profile_dump = conn.list_instance_profiles_for_role(role_name="my-role") - profile_list = profile_dump["list_instance_profiles_for_role_response"][ - "list_instance_profiles_for_role_result" - ]["instance_profiles"] - for profile_count in range(0, len(profile_list)): - profile_name_list.remove(profile_list[profile_count]["instance_profile_name"]) - profile_path_list.remove(profile_list[profile_count]["path"]) - profile_list[profile_count]["roles"]["member"]["role_name"].should.equal( - "my-role" - ) - - len(profile_name_list).should.equal(0) - len(profile_path_list).should.equal(0) - - profile_dump2 = conn.list_instance_profiles_for_role(role_name="my-role2") - profile_list = profile_dump2["list_instance_profiles_for_role_response"][ - "list_instance_profiles_for_role_result" - ]["instance_profiles"] - len(profile_list).should.equal(0) - - @mock_iam def test_list_instance_profiles_for_role_boto3(): conn = boto3.client("iam", region_name="us-east-1") @@ -519,29 +320,6 @@ def test_list_instance_profiles_for_role_boto3(): profile_list.should.have.length_of(0) -# Has boto3 equivalent -@mock_iam_deprecated() -def test_list_role_policies(): - conn = boto.connect_iam() - conn.create_role("my-role") - conn.put_role_policy("my-role", "test policy", MOCK_POLICY) - role = conn.list_role_policies("my-role") - role.policy_names.should.have.length_of(1) - role.policy_names[0].should.equal("test policy") - - conn.put_role_policy("my-role", "test policy 2", MOCK_POLICY) - role = conn.list_role_policies("my-role") - role.policy_names.should.have.length_of(2) - - conn.delete_role_policy("my-role", "test policy") - role = conn.list_role_policies("my-role") - role.policy_names.should.have.length_of(1) - role.policy_names[0].should.equal("test policy 2") - - with pytest.raises(BotoServerError): - conn.delete_role_policy("my-role", "test policy") - - @mock_iam def test_list_role_policies_boto3(): conn = boto3.client("iam", region_name="us-east-1") @@ -574,20 +352,6 @@ def test_list_role_policies_boto3(): ) -# Has boto3 equivalent -@mock_iam_deprecated() -def test_put_role_policy(): - conn = boto.connect_iam() - conn.create_role( - "my-role", assume_role_policy_document="some policy", path="my-path" - ) - conn.put_role_policy("my-role", "test policy", MOCK_POLICY) - policy = conn.get_role_policy("my-role", "test policy")["get_role_policy_response"][ - "get_role_policy_result" - ]["policy_name"] - policy.should.equal("test policy") - - @mock_iam def test_put_role_policy_boto3(): conn = boto3.client("iam", region_name="us-east-1") @@ -612,16 +376,6 @@ def test_get_role_policy(): conn.get_role_policy(RoleName="my-role", PolicyName="does-not-exist") -# Has boto3 equivalent -@mock_iam_deprecated() -def test_update_assume_role_policy(): - conn = boto.connect_iam() - role = conn.create_role("my-role") - conn.update_assume_role_policy(role.role_name, "my-policy") - role = conn.get_role("my-role") - role.assume_role_policy_document.should.equal("my-policy") - - @mock_iam def test_update_assume_role_policy_boto3(): conn = boto3.client("iam", region_name="us-east-1") @@ -1497,15 +1251,6 @@ def test_untag_policy(): ) -# Has boto3 equivalent -@mock_iam_deprecated() -def test_create_user(): - conn = boto.connect_iam() - conn.create_user("my-user") - with pytest.raises(BotoServerError): - conn.create_user("my-user") - - @mock_iam def test_create_user_boto(): conn = boto3.client("iam", region_name="us-east-1") @@ -1523,16 +1268,6 @@ def test_create_user_boto(): err["Message"].should.equal("User my-user already exists") -# Has boto3 equivalent -@mock_iam_deprecated() -def test_get_user(): - conn = boto.connect_iam() - with pytest.raises(BotoServerError): - conn.get_user("my-user") - conn.create_user("my-user") - conn.get_user("my-user") - - @mock_iam def test_get_user_boto3(): conn = boto3.client("iam", region_name="us-east-1") @@ -1565,15 +1300,6 @@ def test_update_user(): conn.get_user(UserName="my-user") -# Has boto3 equivalent -@mock_iam_deprecated() -def test_get_current_user(): - """If no user is specific, IAM returns the current user""" - conn = boto.connect_iam() - user = conn.get_user()["get_user_response"]["get_user_result"]["user"] - user["user_name"].should.equal("default_user") - - @mock_iam def test_get_current_user_boto3(): """If no user is specific, IAM returns the current user""" @@ -1625,18 +1351,6 @@ def test_user_policies(): len(policies["PolicyNames"]).should.equal(0) -# Has boto3 equivalent -@mock_iam_deprecated() -def test_create_login_profile(): - conn = boto.connect_iam() - with pytest.raises(BotoServerError): - conn.create_login_profile("my-user", "my-pass") - conn.create_user("my-user") - conn.create_login_profile("my-user", "my-pass") - with pytest.raises(BotoServerError): - conn.create_login_profile("my-user", "my-pass") - - @mock_iam def test_create_login_profile_with_unknown_user(): conn = boto3.client("iam", region_name="us-east-1") @@ -1660,17 +1374,6 @@ def test_create_login_profile_boto3(): err["Message"].should.equal(None) -# Has boto3 equivalent -@mock_iam_deprecated() -def test_delete_login_profile(): - conn = boto.connect_iam() - conn.create_user("my-user") - with pytest.raises(BotoServerError): - conn.delete_login_profile("my-user") - conn.create_login_profile("my-user", "my-pass") - conn.delete_login_profile("my-user") - - @mock_iam def test_delete_login_profile_with_unknown_user(): conn = boto3.client("iam", region_name="us-east-1") @@ -1732,29 +1435,6 @@ def test_create_access_key(): assert access_key["AccessKeyId"].startswith("AKIA") -# Has boto3 equivalent -@mock_iam_deprecated() -def test_get_all_access_keys(): - """If no access keys exist there should be none in the response, - if an access key is present it should have the correct fields present""" - conn = boto.connect_iam() - conn.create_user("my-user") - response = conn.get_all_access_keys("my-user") - assert ( - response["list_access_keys_response"]["list_access_keys_result"][ - "access_key_metadata" - ] - == [] - ) - conn.create_access_key("my-user") - response = conn.get_all_access_keys("my-user") - assert sorted( - response["list_access_keys_response"]["list_access_keys_result"][ - "access_key_metadata" - ][0].keys() - ) == sorted(["status", "create_date", "user_name", "access_key_id"]) - - @mock_iam def test_list_access_keys(): conn = boto3.client("iam", region_name="us-east-1") @@ -1778,17 +1458,6 @@ def test_list_access_keys(): ) -# Has boto3 equivalent -@mock_iam_deprecated() -def test_delete_access_key_deprecated(): - conn = boto.connect_iam() - conn.create_user("my-user") - access_key_id = conn.create_access_key("my-user")["create_access_key_response"][ - "create_access_key_result" - ]["access_key"]["access_key_id"] - conn.delete_access_key(access_key_id, "my-user") - - @mock_iam def test_delete_access_key(): conn = boto3.client("iam", region_name="us-east-1") @@ -2024,16 +1693,6 @@ def test_enable_virtual_mfa_device(): response["IsTruncated"].should_not.be.ok -# Has boto3 equivalent -@mock_iam_deprecated() -def test_delete_user_deprecated(): - conn = boto.connect_iam() - with pytest.raises(BotoServerError): - conn.delete_user("my-user") - conn.create_user("my-user") - conn.delete_user("my-user") - - @mock_iam() def test_delete_user(): conn = boto3.client("iam", region_name="us-east-1") @@ -2073,20 +1732,6 @@ def test_delete_user(): conn.get_user(UserName="my-user") -# Has boto3 equivalent -@mock_iam_deprecated() -def test_generate_credential_report(): - conn = boto.connect_iam() - result = conn.generate_credential_report() - result["generate_credential_report_response"]["generate_credential_report_result"][ - "state" - ].should.equal("STARTED") - result = conn.generate_credential_report() - result["generate_credential_report_response"]["generate_credential_report_result"][ - "state" - ].should.equal("COMPLETE") - - @mock_iam def test_boto3_generate_credential_report(): conn = boto3.client("iam", region_name="us-east-1") @@ -2096,30 +1741,6 @@ def test_boto3_generate_credential_report(): result["State"].should.equal("COMPLETE") -# Has boto3 equivalent -@mock_iam_deprecated() -def test_get_credential_report(): - conn = boto.connect_iam() - conn.create_user("my-user") - with pytest.raises(BotoServerError): - conn.get_credential_report() - result = conn.generate_credential_report() - while ( - result["generate_credential_report_response"][ - "generate_credential_report_result" - ]["state"] - != "COMPLETE" - ): - result = conn.generate_credential_report() - result = conn.get_credential_report() - report = base64.b64decode( - result["get_credential_report_response"]["get_credential_report_result"][ - "content" - ].encode("ascii") - ).decode("ascii") - report.should.match(r".*my-user.*") - - @mock_iam def test_boto3_get_credential_report(): conn = boto3.client("iam", region_name="us-east-1") @@ -2197,110 +1818,6 @@ def test_get_access_key_last_used_when_used(): resp["AccessKeyLastUsed"].should_not.contain("LastUsedDate") -# Has boto3 equivalent -@requires_boto_gte("2.39") -@mock_iam_deprecated() -def test_managed_policy(): - conn = boto.connect_iam() - - conn.create_policy( - policy_name="UserManagedPolicy", - policy_document=MOCK_POLICY, - path="/mypolicy/", - description="my user managed policy", - ) - - marker = 0 - aws_policies = [] - while marker is not None: - response = conn.list_policies(scope="AWS", marker=marker)[ - "list_policies_response" - ]["list_policies_result"] - for policy in response["policies"]: - aws_policies.append(policy) - marker = response.get("marker") - set(p.name for p in aws_managed_policies).should.equal( - set(p["policy_name"] for p in aws_policies) - ) - - user_policies = conn.list_policies(scope="Local")["list_policies_response"][ - "list_policies_result" - ]["policies"] - set(["UserManagedPolicy"]).should.equal( - set(p["policy_name"] for p in user_policies) - ) - - marker = 0 - all_policies = [] - while marker is not None: - response = conn.list_policies(marker=marker)["list_policies_response"][ - "list_policies_result" - ] - for policy in response["policies"]: - all_policies.append(policy) - marker = response.get("marker") - set(p["policy_name"] for p in aws_policies + user_policies).should.equal( - set(p["policy_name"] for p in all_policies) - ) - - role_name = "my-role" - conn.create_role( - role_name, assume_role_policy_document={"policy": "test"}, path="my-path", - ) - for policy_name in [ - "AmazonElasticMapReduceRole", - "AmazonElasticMapReduceforEC2Role", - ]: - policy_arn = "arn:aws:iam::aws:policy/service-role/" + policy_name - conn.attach_role_policy(policy_arn, role_name) - - rows = conn.list_policies(only_attached=True)["list_policies_response"][ - "list_policies_result" - ]["policies"] - rows.should.have.length_of(2) - for x in rows: - int(x["attachment_count"]).should.be.greater_than(0) - - # boto has not implemented this end point but accessible this way - resp = conn.get_response( - "ListAttachedRolePolicies", - {"RoleName": role_name}, - list_marker="AttachedPolicies", - ) - resp["list_attached_role_policies_response"]["list_attached_role_policies_result"][ - "attached_policies" - ].should.have.length_of(2) - - conn.detach_role_policy( - "arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceRole", role_name, - ) - rows = conn.list_policies(only_attached=True)["list_policies_response"][ - "list_policies_result" - ]["policies"] - rows.should.have.length_of(1) - for x in rows: - int(x["attachment_count"]).should.be.greater_than(0) - - # boto has not implemented this end point but accessible this way - resp = conn.get_response( - "ListAttachedRolePolicies", - {"RoleName": role_name}, - list_marker="AttachedPolicies", - ) - resp["list_attached_role_policies_response"]["list_attached_role_policies_result"][ - "attached_policies" - ].should.have.length_of(1) - - with pytest.raises(BotoServerError): - conn.detach_role_policy( - "arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceRole", - role_name, - ) - - with pytest.raises(BotoServerError): - conn.detach_role_policy("arn:aws:iam::aws:policy/Nonexistent", role_name) - - @mock_iam def test_managed_policy_boto3(): conn = boto3.client("iam", region_name="us-west-1") diff --git a/tests/test_iam/test_iam_groups.py b/tests/test_iam/test_iam_groups.py index f13af8980..60bce11ff 100644 --- a/tests/test_iam/test_iam_groups.py +++ b/tests/test_iam/test_iam_groups.py @@ -1,14 +1,12 @@ from datetime import datetime -import boto import boto3 import sure # noqa # pylint: disable=unused-import import json import pytest -from boto.exception import BotoServerError from botocore.exceptions import ClientError -from moto import mock_iam, mock_iam_deprecated +from moto import mock_iam from moto.core import ACCOUNT_ID MOCK_POLICY = """ @@ -24,15 +22,6 @@ MOCK_POLICY = """ """ -# Has boto3 equivalent -@mock_iam_deprecated() -def test_create_group(): - conn = boto.connect_iam() - conn.create_group("my-group") - with pytest.raises(BotoServerError): - conn.create_group("my-group") - - @mock_iam def test_create_group_boto3(): conn = boto3.client("iam", region_name="us-east-1") @@ -44,16 +33,6 @@ def test_create_group_boto3(): err["Message"].should.equal(None) -# Has boto3 equivalent -@mock_iam_deprecated() -def test_get_group(): - conn = boto.connect_iam() - conn.create_group("my-group") - conn.get_group("my-group") - with pytest.raises(BotoServerError): - conn.get_group("not-group") - - @mock_iam def test_get_group_boto3(): conn = boto3.client("iam", region_name="us-east-1") @@ -95,18 +74,6 @@ def test_get_group_current(): ] == "arn:aws:iam::{}:group/some/location/my-other-group".format(ACCOUNT_ID) -# Has boto3 equivalent -@mock_iam_deprecated() -def test_get_all_groups(): - conn = boto.connect_iam() - conn.create_group("my-group1") - conn.create_group("my-group2") - groups = conn.get_all_groups()["list_groups_response"]["list_groups_result"][ - "groups" - ] - groups.should.have.length_of(2) - - @mock_iam def test_get_all_groups_boto3(): conn = boto3.client("iam", region_name="us-east-1") @@ -116,19 +83,6 @@ def test_get_all_groups_boto3(): groups.should.have.length_of(2) -# Has boto3 equivalent -@mock_iam_deprecated() -def test_add_user_to_group(): - conn = boto.connect_iam() - with pytest.raises(BotoServerError): - conn.add_user_to_group("my-group", "my-user") - conn.create_group("my-group") - with pytest.raises(BotoServerError): - conn.add_user_to_group("my-group", "my-user") - conn.create_user("my-user") - conn.add_user_to_group("my-group", "my-user") - - @mock_iam def test_add_unknown_user_to_group(): conn = boto3.client("iam", region_name="us-east-1") @@ -158,20 +112,6 @@ def test_add_user_to_group_boto3(): conn.add_user_to_group(GroupName="my-group", UserName="my-user") -# Has boto3 equivalent -@mock_iam_deprecated() -def test_remove_user_from_group(): - conn = boto.connect_iam() - with pytest.raises(BotoServerError): - conn.remove_user_from_group("my-group", "my-user") - conn.create_group("my-group") - conn.create_user("my-user") - with pytest.raises(BotoServerError): - conn.remove_user_from_group("my-group", "my-user") - conn.add_user_to_group("my-group", "my-user") - conn.remove_user_from_group("my-group", "my-user") - - @mock_iam def test_remove_user_from_unknown_group(): conn = boto3.client("iam", region_name="us-east-1") @@ -203,23 +143,6 @@ def test_remove_user_from_group_boto3(): conn.remove_user_from_group(GroupName="my-group", UserName="my-user") -# Has boto3 equivalent -@mock_iam_deprecated() -def test_get_groups_for_user(): - conn = boto.connect_iam() - conn.create_group("my-group1") - conn.create_group("my-group2") - conn.create_group("other-group") - conn.create_user("my-user") - conn.add_user_to_group("my-group1", "my-user") - conn.add_user_to_group("my-group2", "my-user") - - groups = conn.get_groups_for_user("my-user")["list_groups_for_user_response"][ - "list_groups_for_user_result" - ]["groups"] - groups.should.have.length_of(2) - - @mock_iam def test_get_groups_for_user_boto3(): conn = boto3.client("iam", region_name="us-east-1") @@ -234,14 +157,6 @@ def test_get_groups_for_user_boto3(): groups.should.have.length_of(2) -# Has boto3 equivalent -@mock_iam_deprecated() -def test_put_group_policy(): - conn = boto.connect_iam() - conn.create_group("my-group") - conn.put_group_policy("my-group", "my-policy", MOCK_POLICY) - - @mock_iam def test_put_group_policy_boto3(): conn = boto3.client("iam", region_name="us-east-1") @@ -275,18 +190,6 @@ def test_attach_group_policies(): ].should.be.empty -# Has boto3 equivalent -@mock_iam_deprecated() -def test_get_group_policy(): - conn = boto.connect_iam() - conn.create_group("my-group") - with pytest.raises(BotoServerError): - conn.get_group_policy("my-group", "my-policy") - - conn.put_group_policy("my-group", "my-policy", MOCK_POLICY) - conn.get_group_policy("my-group", "my-policy") - - @mock_iam def test_get_group_policy_boto3(): conn = boto3.client("iam", region_name="us-east-1") @@ -306,22 +209,6 @@ def test_get_group_policy_boto3(): policy["PolicyDocument"].should.equal(json.loads(MOCK_POLICY)) -# Has boto3 equivalent -@mock_iam_deprecated() -def test_get_all_group_policies(): - conn = boto.connect_iam() - conn.create_group("my-group") - policies = conn.get_all_group_policies("my-group")["list_group_policies_response"][ - "list_group_policies_result" - ]["policy_names"] - assert policies == [] - conn.put_group_policy("my-group", "my-policy", MOCK_POLICY) - policies = conn.get_all_group_policies("my-group")["list_group_policies_response"][ - "list_group_policies_result" - ]["policy_names"] - assert policies == ["my-policy"] - - @mock_iam() def test_list_group_policies(): conn = boto3.client("iam", region_name="us-east-1") diff --git a/tests/test_kinesis/test_kinesis.py b/tests/test_kinesis/test_kinesis.py index 00b4020bc..f361611f7 100644 --- a/tests/test_kinesis/test_kinesis.py +++ b/tests/test_kinesis/test_kinesis.py @@ -2,49 +2,17 @@ import datetime import time import pytest -import boto.kinesis import boto3 -from boto.kinesis.exceptions import ResourceNotFoundException, InvalidArgumentException from botocore.exceptions import ClientError from dateutil.tz import tzlocal -from moto import mock_kinesis, mock_kinesis_deprecated +from moto import mock_kinesis from moto.core import ACCOUNT_ID import sure # noqa # pylint: disable=unused-import -# Has boto3 equivalent -@mock_kinesis_deprecated -def test_create_cluster(): - conn = boto.kinesis.connect_to_region("us-west-2") - - conn.create_stream("my_stream", 3) - - stream_response = conn.describe_stream("my_stream") - - stream = stream_response["StreamDescription"] - stream["StreamName"].should.equal("my_stream") - stream["HasMoreShards"].should.equal(False) - stream["StreamARN"].should.equal( - "arn:aws:kinesis:us-west-2:{}:stream/my_stream".format(ACCOUNT_ID) - ) - stream["StreamStatus"].should.equal("ACTIVE") - - shards = stream["Shards"] - shards.should.have.length_of(3) - - -# Has boto3 equivalent -@mock_kinesis_deprecated -def test_describe_non_existent_stream(): - conn = boto.kinesis.connect_to_region("us-east-1") - conn.describe_stream.when.called_with("not-a-stream").should.throw( - ResourceNotFoundException - ) - - @mock_kinesis def test_describe_non_existent_stream_boto3(): client = boto3.client("kinesis", region_name="us-west-2") @@ -57,26 +25,6 @@ def test_describe_non_existent_stream_boto3(): ) -# Has boto3 equivalent -@mock_kinesis_deprecated -def test_list_and_delete_stream(): - conn = boto.kinesis.connect_to_region("us-west-2") - - conn.create_stream("stream1", 1) - conn.create_stream("stream2", 1) - - conn.list_streams()["StreamNames"].should.have.length_of(2) - - conn.delete_stream("stream2") - - conn.list_streams()["StreamNames"].should.have.length_of(1) - - # Delete invalid id - conn.delete_stream.when.called_with("not-a-stream").should.throw( - ResourceNotFoundException - ) - - @mock_kinesis def test_list_and_delete_stream_boto3(): client = boto3.client("kinesis", region_name="us-west-2") @@ -139,26 +87,6 @@ def test_describe_stream_summary(): stream["StreamStatus"].should.equal("ACTIVE") -# Has boto3 equivalent -@mock_kinesis_deprecated -def test_basic_shard_iterator(): - conn = boto.kinesis.connect_to_region("us-west-2") - - stream_name = "my_stream" - conn.create_stream(stream_name, 1) - - response = conn.describe_stream(stream_name) - shard_id = response["StreamDescription"]["Shards"][0]["ShardId"] - - response = conn.get_shard_iterator(stream_name, shard_id, "TRIM_HORIZON") - shard_iterator = response["ShardIterator"] - - response = conn.get_records(shard_iterator) - shard_iterator = response["NextShardIterator"] - response["Records"].should.equal([]) - response["MillisBehindLatest"].should.equal(0) - - @mock_kinesis def test_basic_shard_iterator_boto3(): client = boto3.client("kinesis", region_name="us-west-1") @@ -178,19 +106,6 @@ def test_basic_shard_iterator_boto3(): resp.should.have.key("MillisBehindLatest").equal(0) -# Has boto3 equivalent -@mock_kinesis_deprecated -def test_get_invalid_shard_iterator(): - conn = boto.kinesis.connect_to_region("us-west-2") - - stream_name = "my_stream" - conn.create_stream(stream_name, 1) - - conn.get_shard_iterator.when.called_with( - stream_name, "123", "TRIM_HORIZON" - ).should.throw(ResourceNotFoundException) - - @mock_kinesis def test_get_invalid_shard_iterator_boto3(): client = boto3.client("kinesis", region_name="us-west-1") @@ -211,40 +126,6 @@ def test_get_invalid_shard_iterator_boto3(): ) -# Has boto3 equivalent -@mock_kinesis_deprecated -def test_put_records(): - conn = boto.kinesis.connect_to_region("us-west-2") - - stream_name = "my_stream" - conn.create_stream(stream_name, 1) - - data = "hello world" - partition_key = "1234" - - conn.put_record.when.called_with(stream_name, data, 1234).should.throw( - InvalidArgumentException - ) - - response = conn.put_record(stream_name, data, partition_key) - response["SequenceNumber"].should.equal("1") - - response = conn.describe_stream(stream_name) - shard_id = response["StreamDescription"]["Shards"][0]["ShardId"] - - response = conn.get_shard_iterator(stream_name, shard_id, "TRIM_HORIZON") - shard_iterator = response["ShardIterator"] - - response = conn.get_records(shard_iterator) - shard_iterator = response["NextShardIterator"] - response["Records"].should.have.length_of(1) - record = response["Records"][0] - - record["Data"].should.equal("hello world") - record["PartitionKey"].should.equal("1234") - record["SequenceNumber"].should.equal("1") - - @mock_kinesis def test_put_records_boto3(): client = boto3.client("kinesis", region_name="eu-west-2") @@ -276,36 +157,6 @@ def test_put_records_boto3(): record["SequenceNumber"].should.equal("1") -# Has boto3 equivalent -@mock_kinesis_deprecated -def test_get_records_limit(): - conn = boto.kinesis.connect_to_region("us-west-2") - - stream_name = "my_stream" - conn.create_stream(stream_name, 1) - - # Create some data - data = "hello world" - - for index in range(5): - conn.put_record(stream_name, data, str(index)) - - # Get a shard iterator - response = conn.describe_stream(stream_name) - shard_id = response["StreamDescription"]["Shards"][0]["ShardId"] - response = conn.get_shard_iterator(stream_name, shard_id, "TRIM_HORIZON") - shard_iterator = response["ShardIterator"] - - # Retrieve only 3 records - response = conn.get_records(shard_iterator, limit=3) - response["Records"].should.have.length_of(3) - - # Then get the rest of the results - next_shard_iterator = response["NextShardIterator"] - response = conn.get_records(next_shard_iterator) - response["Records"].should.have.length_of(2) - - @mock_kinesis def test_get_records_limit_boto3(): client = boto3.client("kinesis", region_name="eu-west-2") @@ -335,41 +186,6 @@ def test_get_records_limit_boto3(): response["Records"].should.have.length_of(2) -# Has boto3 equivalent -@mock_kinesis_deprecated -def test_get_records_at_sequence_number(): - # AT_SEQUENCE_NUMBER - Start reading exactly from the position denoted by - # a specific sequence number. - conn = boto.kinesis.connect_to_region("us-west-2") - stream_name = "my_stream" - conn.create_stream(stream_name, 1) - - # Create some data - for index in range(1, 5): - conn.put_record(stream_name, str(index), str(index)) - - # Get a shard iterator - response = conn.describe_stream(stream_name) - shard_id = response["StreamDescription"]["Shards"][0]["ShardId"] - response = conn.get_shard_iterator(stream_name, shard_id, "TRIM_HORIZON") - shard_iterator = response["ShardIterator"] - - # Get the second record - response = conn.get_records(shard_iterator, limit=2) - second_sequence_id = response["Records"][1]["SequenceNumber"] - - # Then get a new iterator starting at that id - response = conn.get_shard_iterator( - stream_name, shard_id, "AT_SEQUENCE_NUMBER", second_sequence_id - ) - shard_iterator = response["ShardIterator"] - - response = conn.get_records(shard_iterator) - # And the first result returned should be the second item - response["Records"][0]["SequenceNumber"].should.equal(second_sequence_id) - response["Records"][0]["Data"].should.equal("2") - - @mock_kinesis def test_get_records_at_sequence_number_boto3(): client = boto3.client("kinesis", region_name="eu-west-2") @@ -409,41 +225,6 @@ def test_get_records_at_sequence_number_boto3(): resp["Records"][0]["Data"].should.equal(b"data_2") -# Has boto3 equivalent -@mock_kinesis_deprecated -def test_get_records_after_sequence_number(): - # AFTER_SEQUENCE_NUMBER - Start reading right after the position denoted - # by a specific sequence number. - conn = boto.kinesis.connect_to_region("us-west-2") - stream_name = "my_stream" - conn.create_stream(stream_name, 1) - - # Create some data - for index in range(1, 5): - conn.put_record(stream_name, str(index), str(index)) - - # Get a shard iterator - response = conn.describe_stream(stream_name) - shard_id = response["StreamDescription"]["Shards"][0]["ShardId"] - response = conn.get_shard_iterator(stream_name, shard_id, "TRIM_HORIZON") - shard_iterator = response["ShardIterator"] - - # Get the second record - response = conn.get_records(shard_iterator, limit=2) - second_sequence_id = response["Records"][1]["SequenceNumber"] - - # Then get a new iterator starting after that id - response = conn.get_shard_iterator( - stream_name, shard_id, "AFTER_SEQUENCE_NUMBER", second_sequence_id - ) - shard_iterator = response["ShardIterator"] - - response = conn.get_records(shard_iterator) - # And the first result returned should be the third item - response["Records"][0]["Data"].should.equal("3") - response["MillisBehindLatest"].should.equal(0) - - @mock_kinesis def test_get_records_after_sequence_number_boto3(): client = boto3.client("kinesis", region_name="eu-west-2") @@ -484,46 +265,6 @@ def test_get_records_after_sequence_number_boto3(): resp["MillisBehindLatest"].should.equal(0) -# Has boto3 equivalent -@mock_kinesis_deprecated -def test_get_records_latest(): - # LATEST - Start reading just after the most recent record in the shard, - # so that you always read the most recent data in the shard. - conn = boto.kinesis.connect_to_region("us-west-2") - stream_name = "my_stream" - conn.create_stream(stream_name, 1) - - # Create some data - for index in range(1, 5): - conn.put_record(stream_name, str(index), str(index)) - - # Get a shard iterator - response = conn.describe_stream(stream_name) - shard_id = response["StreamDescription"]["Shards"][0]["ShardId"] - response = conn.get_shard_iterator(stream_name, shard_id, "TRIM_HORIZON") - shard_iterator = response["ShardIterator"] - - # Get the second record - response = conn.get_records(shard_iterator, limit=2) - second_sequence_id = response["Records"][1]["SequenceNumber"] - - # Then get a new iterator starting after that id - response = conn.get_shard_iterator( - stream_name, shard_id, "LATEST", second_sequence_id - ) - shard_iterator = response["ShardIterator"] - - # Write some more data - conn.put_record(stream_name, "last_record", "last_record") - - response = conn.get_records(shard_iterator) - # And the only result returned should be the new item - response["Records"].should.have.length_of(1) - response["Records"][0]["PartitionKey"].should.equal("last_record") - response["Records"][0]["Data"].should.equal("last_record") - response["MillisBehindLatest"].should.equal(0) - - @mock_kinesis def test_get_records_latest_boto3(): client = boto3.client("kinesis", region_name="eu-west-2") @@ -817,20 +558,6 @@ def test_invalid_decrease_stream_retention_period(): ex.value.response["Error"]["Message"].should.equal(20) -# Has boto3 equivalent -@mock_kinesis_deprecated -def test_invalid_shard_iterator_type(): - conn = boto.kinesis.connect_to_region("us-west-2") - stream_name = "my_stream" - conn.create_stream(stream_name, 1) - - response = conn.describe_stream(stream_name) - shard_id = response["StreamDescription"]["Shards"][0]["ShardId"] - response = conn.get_shard_iterator.when.called_with( - stream_name, shard_id, "invalid-type" - ).should.throw(InvalidArgumentException) - - @mock_kinesis def test_invalid_shard_iterator_type_boto3(): client = boto3.client("kinesis", region_name="eu-west-2") @@ -848,20 +575,6 @@ def test_invalid_shard_iterator_type_boto3(): err["Message"].should.equal("Invalid ShardIteratorType: invalid-type") -# Has boto3 equivalent -@mock_kinesis_deprecated -def test_add_tags(): - conn = boto.kinesis.connect_to_region("us-west-2") - stream_name = "my_stream" - conn.create_stream(stream_name, 1) - - conn.describe_stream(stream_name) - conn.add_tags_to_stream(stream_name, {"tag1": "val1"}) - conn.add_tags_to_stream(stream_name, {"tag2": "val2"}) - conn.add_tags_to_stream(stream_name, {"tag1": "val3"}) - conn.add_tags_to_stream(stream_name, {"tag2": "val4"}) - - @mock_kinesis def test_add_list_remove_tags_boto3(): client = boto3.client("kinesis", region_name="eu-west-2") @@ -894,147 +607,6 @@ def test_add_list_remove_tags_boto3(): tags.should.contain({"Key": "tag5", "Value": "val5"}) -# Has boto3 equivalent -@mock_kinesis_deprecated -def test_list_tags(): - conn = boto.kinesis.connect_to_region("us-west-2") - stream_name = "my_stream" - conn.create_stream(stream_name, 1) - - conn.describe_stream(stream_name) - conn.add_tags_to_stream(stream_name, {"tag1": "val1"}) - tags = dict( - [ - (tag["Key"], tag["Value"]) - for tag in conn.list_tags_for_stream(stream_name)["Tags"] - ] - ) - tags.get("tag1").should.equal("val1") - conn.add_tags_to_stream(stream_name, {"tag2": "val2"}) - tags = dict( - [ - (tag["Key"], tag["Value"]) - for tag in conn.list_tags_for_stream(stream_name)["Tags"] - ] - ) - tags.get("tag2").should.equal("val2") - conn.add_tags_to_stream(stream_name, {"tag1": "val3"}) - tags = dict( - [ - (tag["Key"], tag["Value"]) - for tag in conn.list_tags_for_stream(stream_name)["Tags"] - ] - ) - tags.get("tag1").should.equal("val3") - conn.add_tags_to_stream(stream_name, {"tag2": "val4"}) - tags = dict( - [ - (tag["Key"], tag["Value"]) - for tag in conn.list_tags_for_stream(stream_name)["Tags"] - ] - ) - tags.get("tag2").should.equal("val4") - - -# Has boto3 equivalent -@mock_kinesis_deprecated -def test_remove_tags(): - conn = boto.kinesis.connect_to_region("us-west-2") - stream_name = "my_stream" - conn.create_stream(stream_name, 1) - - conn.describe_stream(stream_name) - conn.add_tags_to_stream(stream_name, {"tag1": "val1"}) - tags = dict( - [ - (tag["Key"], tag["Value"]) - for tag in conn.list_tags_for_stream(stream_name)["Tags"] - ] - ) - tags.get("tag1").should.equal("val1") - conn.remove_tags_from_stream(stream_name, ["tag1"]) - tags = dict( - [ - (tag["Key"], tag["Value"]) - for tag in conn.list_tags_for_stream(stream_name)["Tags"] - ] - ) - tags.get("tag1").should.equal(None) - - conn.add_tags_to_stream(stream_name, {"tag2": "val2"}) - tags = dict( - [ - (tag["Key"], tag["Value"]) - for tag in conn.list_tags_for_stream(stream_name)["Tags"] - ] - ) - tags.get("tag2").should.equal("val2") - conn.remove_tags_from_stream(stream_name, ["tag2"]) - tags = dict( - [ - (tag["Key"], tag["Value"]) - for tag in conn.list_tags_for_stream(stream_name)["Tags"] - ] - ) - tags.get("tag2").should.equal(None) - - -# Has boto3 equivalent -@mock_kinesis_deprecated -def test_merge_shards(): - conn = boto.kinesis.connect_to_region("us-west-2") - stream_name = "my_stream" - - conn.create_stream(stream_name, 4) - - # Create some data - for index in range(1, 100): - conn.put_record(stream_name, str(index), str(index)) - - stream_response = conn.describe_stream(stream_name) - - stream = stream_response["StreamDescription"] - shards = stream["Shards"] - shards.should.have.length_of(4) - - conn.merge_shards.when.called_with( - stream_name, "shardId-000000000000", "shardId-000000000002" - ).should.throw(InvalidArgumentException) - - stream_response = conn.describe_stream(stream_name) - - stream = stream_response["StreamDescription"] - shards = stream["Shards"] - shards.should.have.length_of(4) - - conn.merge_shards(stream_name, "shardId-000000000000", "shardId-000000000001") - - stream_response = conn.describe_stream(stream_name) - - stream = stream_response["StreamDescription"] - shards = stream["Shards"] - active_shards = [ - shard - for shard in shards - if "EndingSequenceNumber" not in shard["SequenceNumberRange"] - ] - active_shards.should.have.length_of(3) - - conn.merge_shards(stream_name, "shardId-000000000002", "shardId-000000000000") - - stream_response = conn.describe_stream(stream_name) - - stream = stream_response["StreamDescription"] - shards = stream["Shards"] - active_shards = [ - shard - for shard in shards - if "EndingSequenceNumber" not in shard["SequenceNumberRange"] - ] - - active_shards.should.have.length_of(2) - - @mock_kinesis def test_merge_shards_boto3(): client = boto3.client("kinesis", region_name="eu-west-2") diff --git a/tests/test_kms/test_kms.py b/tests/test_kms/test_kms.py deleted file mode 100644 index 42fc86e19..000000000 --- a/tests/test_kms/test_kms.py +++ /dev/null @@ -1,793 +0,0 @@ -# -*- coding: utf-8 -*- -import base64 -import re - -import boto.kms -import boto3 -import sure # noqa # pylint: disable=unused-import -from boto.exception import JSONResponseError -from boto.kms.exceptions import AlreadyExistsException, NotFoundException -import pytest -from moto.core.exceptions import JsonRESTError -from moto.kms.models import KmsBackend -from moto.kms.exceptions import NotFoundException as MotoNotFoundException -from moto import mock_kms_deprecated, mock_kms - -PLAINTEXT_VECTORS = [ - b"some encodeable plaintext", - b"some unencodeable plaintext \xec\x8a\xcf\xb6r\xe9\xb5\xeb\xff\xa23\x16", - "some unicode characters ø˚∆øˆˆ∆ßçøˆˆçßøˆ¨¥", -] - - -def _get_encoded_value(plaintext): - if isinstance(plaintext, bytes): - return plaintext - - return plaintext.encode("utf-8") - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_describe_key(): - conn = boto.kms.connect_to_region("us-west-2") - key = conn.create_key( - policy="my policy", description="my key", key_usage="ENCRYPT_DECRYPT" - ) - key_id = key["KeyMetadata"]["KeyId"] - - key = conn.describe_key(key_id) - key["KeyMetadata"]["Description"].should.equal("my key") - key["KeyMetadata"]["KeyUsage"].should.equal("ENCRYPT_DECRYPT") - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_describe_key_via_alias(): - conn = boto.kms.connect_to_region("us-west-2") - key = conn.create_key( - policy="my policy", description="my key", key_usage="ENCRYPT_DECRYPT" - ) - conn.create_alias( - alias_name="alias/my-key-alias", target_key_id=key["KeyMetadata"]["KeyId"] - ) - - alias_key = conn.describe_key("alias/my-key-alias") - alias_key["KeyMetadata"]["Description"].should.equal("my key") - alias_key["KeyMetadata"]["KeyUsage"].should.equal("ENCRYPT_DECRYPT") - alias_key["KeyMetadata"]["Arn"].should.equal(key["KeyMetadata"]["Arn"]) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_describe_key_via_alias_not_found(): - conn = boto.kms.connect_to_region("us-west-2") - key = conn.create_key( - policy="my policy", description="my key", key_usage="ENCRYPT_DECRYPT" - ) - conn.create_alias( - alias_name="alias/my-key-alias", target_key_id=key["KeyMetadata"]["KeyId"] - ) - - conn.describe_key.when.called_with("alias/not-found-alias").should.throw( - NotFoundException - ) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_describe_key_via_arn(): - conn = boto.kms.connect_to_region("us-west-2") - key = conn.create_key( - policy="my policy", description="my key", key_usage="ENCRYPT_DECRYPT" - ) - arn = key["KeyMetadata"]["Arn"] - - the_key = conn.describe_key(arn) - the_key["KeyMetadata"]["Description"].should.equal("my key") - the_key["KeyMetadata"]["KeyUsage"].should.equal("ENCRYPT_DECRYPT") - the_key["KeyMetadata"]["KeyId"].should.equal(key["KeyMetadata"]["KeyId"]) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_describe_missing_key(): - conn = boto.kms.connect_to_region("us-west-2") - conn.describe_key.when.called_with("not-a-key").should.throw(NotFoundException) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_list_keys(): - conn = boto.kms.connect_to_region("us-west-2") - - conn.create_key( - policy="my policy", description="my key1", key_usage="ENCRYPT_DECRYPT" - ) - conn.create_key( - policy="my policy", description="my key2", key_usage="ENCRYPT_DECRYPT" - ) - - keys = conn.list_keys() - keys["Keys"].should.have.length_of(2) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_enable_key_rotation(): - conn = boto.kms.connect_to_region("us-west-2") - - key = conn.create_key( - policy="my policy", description="my key", key_usage="ENCRYPT_DECRYPT" - ) - key_id = key["KeyMetadata"]["KeyId"] - - conn.enable_key_rotation(key_id) - - conn.get_key_rotation_status(key_id)["KeyRotationEnabled"].should.equal(True) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_enable_key_rotation_via_arn(): - conn = boto.kms.connect_to_region("us-west-2") - - key = conn.create_key( - policy="my policy", description="my key", key_usage="ENCRYPT_DECRYPT" - ) - key_id = key["KeyMetadata"]["Arn"] - - conn.enable_key_rotation(key_id) - - conn.get_key_rotation_status(key_id)["KeyRotationEnabled"].should.equal(True) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_enable_key_rotation_with_missing_key(): - conn = boto.kms.connect_to_region("us-west-2") - conn.enable_key_rotation.when.called_with("not-a-key").should.throw( - NotFoundException - ) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_enable_key_rotation_with_alias_name_should_fail(): - conn = boto.kms.connect_to_region("us-west-2") - key = conn.create_key( - policy="my policy", description="my key", key_usage="ENCRYPT_DECRYPT" - ) - conn.create_alias( - alias_name="alias/my-key-alias", target_key_id=key["KeyMetadata"]["KeyId"] - ) - - alias_key = conn.describe_key("alias/my-key-alias") - alias_key["KeyMetadata"]["Arn"].should.equal(key["KeyMetadata"]["Arn"]) - - conn.enable_key_rotation.when.called_with("alias/my-alias").should.throw( - NotFoundException - ) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_disable_key_rotation(): - conn = boto.kms.connect_to_region("us-west-2") - - key = conn.create_key( - policy="my policy", description="my key", key_usage="ENCRYPT_DECRYPT" - ) - key_id = key["KeyMetadata"]["KeyId"] - - conn.enable_key_rotation(key_id) - conn.get_key_rotation_status(key_id)["KeyRotationEnabled"].should.equal(True) - - conn.disable_key_rotation(key_id) - conn.get_key_rotation_status(key_id)["KeyRotationEnabled"].should.equal(False) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_generate_data_key(): - conn = boto.kms.connect_to_region("us-west-2") - - key = conn.create_key( - policy="my policy", description="my key", key_usage="ENCRYPT_DECRYPT" - ) - key_id = key["KeyMetadata"]["KeyId"] - key_arn = key["KeyMetadata"]["Arn"] - - response = conn.generate_data_key(key_id=key_id, number_of_bytes=32) - - # CiphertextBlob must NOT be base64-encoded - with pytest.raises(Exception): - base64.b64decode(response["CiphertextBlob"], validate=True) - # Plaintext must NOT be base64-encoded - with pytest.raises(Exception): - base64.b64decode(response["Plaintext"], validate=True) - - response["KeyId"].should.equal(key_arn) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_disable_key_rotation_with_missing_key(): - conn = boto.kms.connect_to_region("us-west-2") - conn.disable_key_rotation.when.called_with("not-a-key").should.throw( - NotFoundException - ) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_get_key_rotation_status_with_missing_key(): - conn = boto.kms.connect_to_region("us-west-2") - conn.get_key_rotation_status.when.called_with("not-a-key").should.throw( - NotFoundException - ) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_get_key_rotation_status(): - conn = boto.kms.connect_to_region("us-west-2") - - key = conn.create_key( - policy="my policy", description="my key", key_usage="ENCRYPT_DECRYPT" - ) - key_id = key["KeyMetadata"]["KeyId"] - - conn.get_key_rotation_status(key_id)["KeyRotationEnabled"].should.equal(False) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_create_key_defaults_key_rotation(): - conn = boto.kms.connect_to_region("us-west-2") - - key = conn.create_key( - policy="my policy", description="my key", key_usage="ENCRYPT_DECRYPT" - ) - key_id = key["KeyMetadata"]["KeyId"] - - conn.get_key_rotation_status(key_id)["KeyRotationEnabled"].should.equal(False) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_get_key_policy(): - conn = boto.kms.connect_to_region("us-west-2") - - key = conn.create_key( - policy="my policy", description="my key1", key_usage="ENCRYPT_DECRYPT" - ) - key_id = key["KeyMetadata"]["KeyId"] - - policy = conn.get_key_policy(key_id, "default") - policy["Policy"].should.equal("my policy") - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_get_key_policy_via_arn(): - conn = boto.kms.connect_to_region("us-west-2") - - key = conn.create_key( - policy="my policy", description="my key1", key_usage="ENCRYPT_DECRYPT" - ) - policy = conn.get_key_policy(key["KeyMetadata"]["Arn"], "default") - - policy["Policy"].should.equal("my policy") - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_put_key_policy(): - conn = boto.kms.connect_to_region("us-west-2") - - key = conn.create_key( - policy="my policy", description="my key1", key_usage="ENCRYPT_DECRYPT" - ) - key_id = key["KeyMetadata"]["KeyId"] - - conn.put_key_policy(key_id, "default", "new policy") - policy = conn.get_key_policy(key_id, "default") - policy["Policy"].should.equal("new policy") - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_put_key_policy_via_arn(): - conn = boto.kms.connect_to_region("us-west-2") - - key = conn.create_key( - policy="my policy", description="my key1", key_usage="ENCRYPT_DECRYPT" - ) - key_id = key["KeyMetadata"]["Arn"] - - conn.put_key_policy(key_id, "default", "new policy") - policy = conn.get_key_policy(key_id, "default") - policy["Policy"].should.equal("new policy") - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_put_key_policy_via_alias_should_not_update(): - conn = boto.kms.connect_to_region("us-west-2") - - key = conn.create_key( - policy="my policy", description="my key1", key_usage="ENCRYPT_DECRYPT" - ) - conn.create_alias( - alias_name="alias/my-key-alias", target_key_id=key["KeyMetadata"]["KeyId"] - ) - - conn.put_key_policy.when.called_with( - "alias/my-key-alias", "default", "new policy" - ).should.throw(NotFoundException) - - policy = conn.get_key_policy(key["KeyMetadata"]["KeyId"], "default") - policy["Policy"].should.equal("my policy") - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_list_key_policies(): - conn = boto.kms.connect_to_region("us-west-2") - - key = conn.create_key( - policy="my policy", description="my key1", key_usage="ENCRYPT_DECRYPT" - ) - key_id = key["KeyMetadata"]["KeyId"] - - policies = conn.list_key_policies(key_id) - policies["PolicyNames"].should.equal(["default"]) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test__create_alias__returns_none_if_correct(): - kms = boto.connect_kms() - create_resp = kms.create_key() - key_id = create_resp["KeyMetadata"]["KeyId"] - - resp = kms.create_alias("alias/my-alias", key_id) - - resp.should.be.none - - -# Has boto3 equivalent -@mock_kms_deprecated -def test__create_alias__raises_if_reserved_alias(): - kms = boto.connect_kms() - create_resp = kms.create_key() - key_id = create_resp["KeyMetadata"]["KeyId"] - - reserved_aliases = [ - "alias/aws/ebs", - "alias/aws/s3", - "alias/aws/redshift", - "alias/aws/rds", - ] - - for alias_name in reserved_aliases: - with pytest.raises(JSONResponseError) as err: - kms.create_alias(alias_name, key_id) - - ex = err.value - ex.error_message.should.be.none - ex.error_code.should.equal("NotAuthorizedException") - ex.body.should.equal({"__type": "NotAuthorizedException"}) - ex.reason.should.equal("Bad Request") - ex.status.should.equal(400) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test__create_alias__can_create_multiple_aliases_for_same_key_id(): - kms = boto.connect_kms() - create_resp = kms.create_key() - key_id = create_resp["KeyMetadata"]["KeyId"] - - kms.create_alias("alias/my-alias3", key_id).should.be.none - kms.create_alias("alias/my-alias4", key_id).should.be.none - kms.create_alias("alias/my-alias5", key_id).should.be.none - - -# Has boto3 equivalent -@mock_kms_deprecated -def test__create_alias__raises_if_wrong_prefix(): - kms = boto.connect_kms() - create_resp = kms.create_key() - key_id = create_resp["KeyMetadata"]["KeyId"] - - with pytest.raises(JSONResponseError) as err: - kms.create_alias("wrongprefix/my-alias", key_id) - - ex = err.value - ex.error_message.should.equal("Invalid identifier") - ex.error_code.should.equal("ValidationException") - ex.body.should.equal( - {"message": "Invalid identifier", "__type": "ValidationException"} - ) - ex.reason.should.equal("Bad Request") - ex.status.should.equal(400) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test__create_alias__raises_if_duplicate(): - region = "us-west-2" - kms = boto.kms.connect_to_region(region) - create_resp = kms.create_key() - key_id = create_resp["KeyMetadata"]["KeyId"] - alias = "alias/my-alias" - - kms.create_alias(alias, key_id) - - with pytest.raises(AlreadyExistsException) as err: - kms.create_alias(alias, key_id) - - ex = err.value - ex.error_message.should.match( - r"An alias with the name arn:aws:kms:{region}:\d{{12}}:{alias} already exists".format( - **locals() - ) - ) - ex.error_code.should.be.none - ex.box_usage.should.be.none - ex.request_id.should.be.none - ex.body["message"].should.match( - r"An alias with the name arn:aws:kms:{region}:\d{{12}}:{alias} already exists".format( - **locals() - ) - ) - ex.body["__type"].should.equal("AlreadyExistsException") - ex.reason.should.equal("Bad Request") - ex.status.should.equal(400) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test__create_alias__raises_if_alias_has_restricted_characters(): - kms = boto.connect_kms() - create_resp = kms.create_key() - key_id = create_resp["KeyMetadata"]["KeyId"] - - alias_names_with_restricted_characters = [ - "alias/my-alias!", - "alias/my-alias$", - "alias/my-alias@", - ] - - for alias_name in alias_names_with_restricted_characters: - with pytest.raises(JSONResponseError) as err: - kms.create_alias(alias_name, key_id) - ex = err.value - ex.body["__type"].should.equal("ValidationException") - ex.body["message"].should.equal( - "1 validation error detected: Value '{alias_name}' at 'aliasName' failed to satisfy constraint: Member must satisfy regular expression pattern: ^[a-zA-Z0-9:/_-]+$".format( - **locals() - ) - ) - ex.error_code.should.equal("ValidationException") - ex.message.should.equal( - "1 validation error detected: Value '{alias_name}' at 'aliasName' failed to satisfy constraint: Member must satisfy regular expression pattern: ^[a-zA-Z0-9:/_-]+$".format( - **locals() - ) - ) - ex.reason.should.equal("Bad Request") - ex.status.should.equal(400) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test__create_alias__raises_if_alias_has_colon_character(): - # For some reason, colons are not accepted for an alias, even though they - # are accepted by regex ^[a-zA-Z0-9:/_-]+$ - kms = boto.connect_kms() - create_resp = kms.create_key() - key_id = create_resp["KeyMetadata"]["KeyId"] - - alias_names_with_restricted_characters = ["alias/my:alias"] - - for alias_name in alias_names_with_restricted_characters: - with pytest.raises(JSONResponseError) as err: - kms.create_alias(alias_name, key_id) - ex = err.value - ex.body["__type"].should.equal("ValidationException") - ex.body["message"].should.equal( - "{alias_name} contains invalid characters for an alias".format(**locals()) - ) - ex.error_code.should.equal("ValidationException") - ex.message.should.equal( - "{alias_name} contains invalid characters for an alias".format(**locals()) - ) - ex.reason.should.equal("Bad Request") - ex.status.should.equal(400) - - -# Has boto3 equivalent -@pytest.mark.parametrize("alias_name", ["alias/my-alias_/", "alias/my_alias-/"]) -@mock_kms_deprecated -def test__create_alias__accepted_characters(alias_name): - kms = boto.connect_kms() - create_resp = kms.create_key() - key_id = create_resp["KeyMetadata"]["KeyId"] - - kms.create_alias(alias_name, key_id) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test__create_alias__raises_if_target_key_id_is_existing_alias(): - kms = boto.connect_kms() - create_resp = kms.create_key() - key_id = create_resp["KeyMetadata"]["KeyId"] - alias = "alias/my-alias" - - kms.create_alias(alias, key_id) - - with pytest.raises(JSONResponseError) as err: - kms.create_alias(alias, alias) - - ex = err.value - ex.body["__type"].should.equal("ValidationException") - ex.body["message"].should.equal("Aliases must refer to keys. Not aliases") - ex.error_code.should.equal("ValidationException") - ex.message.should.equal("Aliases must refer to keys. Not aliases") - ex.reason.should.equal("Bad Request") - ex.status.should.equal(400) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test__delete_alias(): - kms = boto.connect_kms() - create_resp = kms.create_key() - key_id = create_resp["KeyMetadata"]["KeyId"] - alias = "alias/my-alias" - - # added another alias here to make sure that the deletion of the alias can - # be done when there are multiple existing aliases. - kms.create_key() - another_key_id = create_resp["KeyMetadata"]["KeyId"] - another_alias = "alias/another-alias" - - kms.create_alias(alias, key_id) - kms.create_alias(another_alias, another_key_id) - - resp = kms.delete_alias(alias) - - resp.should.be.none - - # we can create the alias again, since it has been deleted - kms.create_alias(alias, key_id) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test__delete_alias__raises_if_wrong_prefix(): - kms = boto.connect_kms() - - with pytest.raises(JSONResponseError) as err: - kms.delete_alias("wrongprefix/my-alias") - - ex = err.value - ex.body["__type"].should.equal("ValidationException") - ex.body["message"].should.equal("Invalid identifier") - ex.error_code.should.equal("ValidationException") - ex.message.should.equal("Invalid identifier") - ex.reason.should.equal("Bad Request") - ex.status.should.equal(400) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test__delete_alias__raises_if_alias_is_not_found(): - region = "us-west-2" - kms = boto.kms.connect_to_region(region) - alias_name = "alias/unexisting-alias" - - with pytest.raises(NotFoundException) as err: - kms.delete_alias(alias_name) - - expected_message_match = r"Alias arn:aws:kms:{region}:[0-9]{{12}}:{alias_name} is not found.".format( - region=region, alias_name=alias_name - ) - ex = err.value - ex.body["__type"].should.equal("NotFoundException") - ex.body["message"].should.match(expected_message_match) - ex.box_usage.should.be.none - ex.error_code.should.be.none - ex.message.should.match(expected_message_match) - ex.reason.should.equal("Bad Request") - ex.request_id.should.be.none - ex.status.should.equal(400) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test__list_aliases(): - region = "eu-west-1" - kms = boto.kms.connect_to_region(region) - - create_resp = kms.create_key() - key_id = create_resp["KeyMetadata"]["KeyId"] - kms.create_alias("alias/my-alias1", key_id) - kms.create_alias("alias/my-alias2", key_id) - kms.create_alias("alias/my-alias3", key_id) - - resp = kms.list_aliases() - - resp["Truncated"].should.be.false - - aliases = resp["Aliases"] - - def has_correct_arn(alias_obj): - alias_name = alias_obj["AliasName"] - alias_arn = alias_obj["AliasArn"] - return re.match( - r"arn:aws:kms:{region}:\d{{12}}:{alias_name}".format( - region=region, alias_name=alias_name - ), - alias_arn, - ) - - len( - [ - alias - for alias in aliases - if has_correct_arn(alias) and "alias/aws/ebs" == alias["AliasName"] - ] - ).should.equal(1) - len( - [ - alias - for alias in aliases - if has_correct_arn(alias) and "alias/aws/rds" == alias["AliasName"] - ] - ).should.equal(1) - len( - [ - alias - for alias in aliases - if has_correct_arn(alias) and "alias/aws/redshift" == alias["AliasName"] - ] - ).should.equal(1) - len( - [ - alias - for alias in aliases - if has_correct_arn(alias) and "alias/aws/s3" == alias["AliasName"] - ] - ).should.equal(1) - - len( - [ - alias - for alias in aliases - if has_correct_arn(alias) and "alias/my-alias1" == alias["AliasName"] - ] - ).should.equal(1) - len( - [ - alias - for alias in aliases - if has_correct_arn(alias) and "alias/my-alias2" == alias["AliasName"] - ] - ).should.equal(1) - - len( - [ - alias - for alias in aliases - if "TargetKeyId" in alias and key_id == alias["TargetKeyId"] - ] - ).should.equal(3) - - len(aliases).should.equal(17) - - -@mock_kms_deprecated -def test__assert_default_policy(): - from moto.kms.responses import _assert_default_policy - - _assert_default_policy.when.called_with("not-default").should.throw( - MotoNotFoundException - ) - _assert_default_policy.when.called_with("default").should_not.throw( - MotoNotFoundException - ) - - -sort = lambda l: sorted(l, key=lambda d: d.keys()) - - -def _check_tags(key_id, created_tags, client): - result = client.list_resource_tags(KeyId=key_id) - actual = result.get("Tags", []) - assert sort(created_tags) == sort(actual) - - client.untag_resource(KeyId=key_id, TagKeys=["key1"]) - - actual = client.list_resource_tags(KeyId=key_id).get("Tags", []) - expected = [{"TagKey": "key2", "TagValue": "value2"}] - assert sort(expected) == sort(actual) - - -@mock_kms -def test_key_tag_on_create_key_happy(): - client = boto3.client("kms", region_name="us-east-1") - - tags = [ - {"TagKey": "key1", "TagValue": "value1"}, - {"TagKey": "key2", "TagValue": "value2"}, - ] - key = client.create_key(Description="test-key-tagging", Tags=tags) - _check_tags(key["KeyMetadata"]["KeyId"], tags, client) - - -@mock_kms -def test_key_tag_on_create_key_on_arn_happy(): - client = boto3.client("kms", region_name="us-east-1") - - tags = [ - {"TagKey": "key1", "TagValue": "value1"}, - {"TagKey": "key2", "TagValue": "value2"}, - ] - key = client.create_key(Description="test-key-tagging", Tags=tags) - _check_tags(key["KeyMetadata"]["Arn"], tags, client) - - -@mock_kms -def test_key_tag_added_happy(): - client = boto3.client("kms", region_name="us-east-1") - - key = client.create_key(Description="test-key-tagging") - key_id = key["KeyMetadata"]["KeyId"] - tags = [ - {"TagKey": "key1", "TagValue": "value1"}, - {"TagKey": "key2", "TagValue": "value2"}, - ] - client.tag_resource(KeyId=key_id, Tags=tags) - _check_tags(key_id, tags, client) - - -@mock_kms -def test_key_tag_added_arn_based_happy(): - client = boto3.client("kms", region_name="us-east-1") - - key = client.create_key(Description="test-key-tagging") - key_id = key["KeyMetadata"]["Arn"] - tags = [ - {"TagKey": "key1", "TagValue": "value1"}, - {"TagKey": "key2", "TagValue": "value2"}, - ] - client.tag_resource(KeyId=key_id, Tags=tags) - _check_tags(key_id, tags, client) - - -# Has boto3 equivalent -@mock_kms_deprecated -def test_key_tagging_sad(): - b = KmsBackend(region="eu-north-1") - - try: - b.tag_resource("unknown", []) - raise "tag_resource should fail if KeyId is not known" - except JsonRESTError: - pass - - try: - b.untag_resource("unknown", []) - raise "untag_resource should fail if KeyId is not known" - except JsonRESTError: - pass - - try: - b.list_resource_tags("unknown") - raise "list_resource_tags should fail if KeyId is not known" - except JsonRESTError: - pass diff --git a/tests/test_kms/test_kms_boto3.py b/tests/test_kms/test_kms_boto3.py index 357b9b8d7..abfd99f95 100644 --- a/tests/test_kms/test_kms_boto3.py +++ b/tests/test_kms/test_kms_boto3.py @@ -1043,3 +1043,70 @@ def test__delete_alias__raises_if_alias_is_not_found(): err["Message"].should.equal( f"Alias arn:aws:kms:us-east-1:{ACCOUNT_ID}:alias/unknown-alias is not found." ) + + +sort = lambda l: sorted(l, key=lambda d: d.keys()) + + +def _check_tags(key_id, created_tags, client): + result = client.list_resource_tags(KeyId=key_id) + actual = result.get("Tags", []) + assert sort(created_tags) == sort(actual) + + client.untag_resource(KeyId=key_id, TagKeys=["key1"]) + + actual = client.list_resource_tags(KeyId=key_id).get("Tags", []) + expected = [{"TagKey": "key2", "TagValue": "value2"}] + assert sort(expected) == sort(actual) + + +@mock_kms +def test_key_tag_on_create_key_happy(): + client = boto3.client("kms", region_name="us-east-1") + + tags = [ + {"TagKey": "key1", "TagValue": "value1"}, + {"TagKey": "key2", "TagValue": "value2"}, + ] + key = client.create_key(Description="test-key-tagging", Tags=tags) + _check_tags(key["KeyMetadata"]["KeyId"], tags, client) + + +@mock_kms +def test_key_tag_on_create_key_on_arn_happy(): + client = boto3.client("kms", region_name="us-east-1") + + tags = [ + {"TagKey": "key1", "TagValue": "value1"}, + {"TagKey": "key2", "TagValue": "value2"}, + ] + key = client.create_key(Description="test-key-tagging", Tags=tags) + _check_tags(key["KeyMetadata"]["Arn"], tags, client) + + +@mock_kms +def test_key_tag_added_happy(): + client = boto3.client("kms", region_name="us-east-1") + + key = client.create_key(Description="test-key-tagging") + key_id = key["KeyMetadata"]["KeyId"] + tags = [ + {"TagKey": "key1", "TagValue": "value1"}, + {"TagKey": "key2", "TagValue": "value2"}, + ] + client.tag_resource(KeyId=key_id, Tags=tags) + _check_tags(key_id, tags, client) + + +@mock_kms +def test_key_tag_added_arn_based_happy(): + client = boto3.client("kms", region_name="us-east-1") + + key = client.create_key(Description="test-key-tagging") + key_id = key["KeyMetadata"]["Arn"] + tags = [ + {"TagKey": "key1", "TagValue": "value1"}, + {"TagKey": "key2", "TagValue": "value2"}, + ] + client.tag_resource(KeyId=key_id, Tags=tags) + _check_tags(key_id, tags, client) diff --git a/tests/test_packages/__init__.py b/tests/test_packages/__init__.py deleted file mode 100644 index a7d8ed43f..000000000 --- a/tests/test_packages/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -import logging - -# Disable extra logging for tests -logging.getLogger("boto").setLevel(logging.CRITICAL) -logging.getLogger("boto3").setLevel(logging.CRITICAL) -logging.getLogger("botocore").setLevel(logging.CRITICAL) diff --git a/tests/test_packages/test_httpretty.py b/tests/test_packages/test_httpretty.py deleted file mode 100644 index e6bed31fe..000000000 --- a/tests/test_packages/test_httpretty.py +++ /dev/null @@ -1,41 +0,0 @@ -# #!/usr/bin/env python -# -*- coding: utf-8 -*- -from moto.packages.httpretty.core import ( - HTTPrettyRequest, - fake_gethostname, - fake_gethostbyname, -) - - -def test_parse_querystring(): - - core = HTTPrettyRequest(headers="test test HTTP/1.1") - - qs = "test test" - response = core.parse_querystring(qs) - - assert response == {} - - -def test_parse_request_body(): - core = HTTPrettyRequest(headers="test test HTTP/1.1") - - qs = "test" - response = core.parse_request_body(qs) - - assert response == "test" - - -def test_fake_gethostname(): - - response = fake_gethostname() - - assert response == "localhost" - - -def test_fake_gethostbyname(): - - host = "test" - response = fake_gethostbyname(host=host) - - assert response == "127.0.0.1" diff --git a/tests/test_rds/test_rds.py b/tests/test_rds/test_rds.py index aa688af89..96ed6b6b6 100644 --- a/tests/test_rds/test_rds.py +++ b/tests/test_rds/test_rds.py @@ -1,11 +1,8 @@ import boto3 -import boto.rds -import boto.vpc -from boto.exception import BotoServerError import sure # noqa # pylint: disable=unused-import import pytest -from moto import mock_ec2_deprecated, mock_rds_deprecated, mock_rds +from moto import mock_rds def test_deprecation_warning(): @@ -16,42 +13,6 @@ def test_deprecation_warning(): ) -@mock_rds_deprecated -def test_create_database(): - conn = boto.rds.connect_to_region("us-west-2") - - database = conn.create_dbinstance( - "db-master-1", 10, "db.m1.small", "root", "hunter2", security_groups=["my_sg"] - ) - - database.status.should.equal("available") - database.id.should.equal("db-master-1") - database.allocated_storage.should.equal(10) - database.instance_class.should.equal("db.m1.small") - database.master_username.should.equal("root") - database.endpoint.should.equal( - ("db-master-1.aaaaaaaaaa.us-west-2.rds.amazonaws.com", 3306) - ) - database.security_groups[0].name.should.equal("my_sg") - - -@mock_rds_deprecated -def test_get_databases(): - conn = boto.rds.connect_to_region("us-west-2") - - list(conn.get_all_dbinstances()).should.have.length_of(0) - - conn.create_dbinstance("db-master-1", 10, "db.m1.small", "root", "hunter2") - conn.create_dbinstance("db-master-2", 10, "db.m1.small", "root", "hunter2") - - list(conn.get_all_dbinstances()).should.have.length_of(2) - - databases = conn.get_all_dbinstances("db-master-1") - list(databases).should.have.length_of(1) - - databases[0].id.should.equal("db-master-1") - - @mock_rds def test_get_databases_paginated(): conn = boto3.client("rds", region_name="us-west-2") @@ -71,263 +32,3 @@ def test_get_databases_paginated(): resp2 = conn.describe_db_instances(Marker=resp["Marker"]) resp2["DBInstances"].should.have.length_of(1) - - -@mock_rds_deprecated -def test_describe_non_existent_database(): - conn = boto.rds.connect_to_region("us-west-2") - conn.get_all_dbinstances.when.called_with("not-a-db").should.throw(BotoServerError) - - -@mock_rds_deprecated -def test_delete_database(): - conn = boto.rds.connect_to_region("us-west-2") - list(conn.get_all_dbinstances()).should.have.length_of(0) - - conn.create_dbinstance("db-master-1", 10, "db.m1.small", "root", "hunter2") - list(conn.get_all_dbinstances()).should.have.length_of(1) - - conn.delete_dbinstance("db-master-1") - list(conn.get_all_dbinstances()).should.have.length_of(0) - - -@mock_rds_deprecated -def test_delete_non_existent_database(): - conn = boto.rds.connect_to_region("us-west-2") - conn.delete_dbinstance.when.called_with("not-a-db").should.throw(BotoServerError) - - -@mock_rds_deprecated -def test_create_database_security_group(): - conn = boto.rds.connect_to_region("us-west-2") - - security_group = conn.create_dbsecurity_group("db_sg", "DB Security Group") - security_group.name.should.equal("db_sg") - security_group.description.should.equal("DB Security Group") - list(security_group.ip_ranges).should.equal([]) - - -@mock_rds_deprecated -def test_get_security_groups(): - conn = boto.rds.connect_to_region("us-west-2") - - list(conn.get_all_dbsecurity_groups()).should.have.length_of(0) - - conn.create_dbsecurity_group("db_sg1", "DB Security Group") - conn.create_dbsecurity_group("db_sg2", "DB Security Group") - - list(conn.get_all_dbsecurity_groups()).should.have.length_of(2) - - databases = conn.get_all_dbsecurity_groups("db_sg1") - list(databases).should.have.length_of(1) - - databases[0].name.should.equal("db_sg1") - - -@mock_rds_deprecated -def test_get_non_existent_security_group(): - conn = boto.rds.connect_to_region("us-west-2") - conn.get_all_dbsecurity_groups.when.called_with("not-a-sg").should.throw( - BotoServerError - ) - - -@mock_rds_deprecated -def test_delete_database_security_group(): - conn = boto.rds.connect_to_region("us-west-2") - conn.create_dbsecurity_group("db_sg", "DB Security Group") - - list(conn.get_all_dbsecurity_groups()).should.have.length_of(1) - - conn.delete_dbsecurity_group("db_sg") - list(conn.get_all_dbsecurity_groups()).should.have.length_of(0) - - -@mock_rds_deprecated -def test_delete_non_existent_security_group(): - conn = boto.rds.connect_to_region("us-west-2") - conn.delete_dbsecurity_group.when.called_with("not-a-db").should.throw( - BotoServerError - ) - - -@mock_rds_deprecated -def test_add_security_group_to_database(): - conn = boto.rds.connect_to_region("us-west-2") - - database = conn.create_dbinstance( - "db-master-1", 10, "db.m1.small", "root", "hunter2" - ) - security_group = conn.create_dbsecurity_group("db_sg", "DB Security Group") - database.modify(security_groups=[security_group]) - - database = conn.get_all_dbinstances()[0] - list(database.security_groups).should.have.length_of(1) - - database.security_groups[0].name.should.equal("db_sg") - - -@mock_ec2_deprecated -@mock_rds_deprecated -def test_add_database_subnet_group(): - vpc_conn = boto.vpc.connect_to_region("us-west-2") - vpc = vpc_conn.create_vpc("10.0.0.0/16") - subnet1 = vpc_conn.create_subnet(vpc.id, "10.0.1.0/24") - subnet2 = vpc_conn.create_subnet(vpc.id, "10.0.2.0/24") - - subnet_ids = [subnet1.id, subnet2.id] - conn = boto.rds.connect_to_region("us-west-2") - subnet_group = conn.create_db_subnet_group("db_subnet", "my db subnet", subnet_ids) - subnet_group.name.should.equal("db_subnet") - subnet_group.description.should.equal("my db subnet") - list(subnet_group.subnet_ids).should.equal(subnet_ids) - - -@mock_ec2_deprecated -@mock_rds_deprecated -def test_describe_database_subnet_group(): - vpc_conn = boto.vpc.connect_to_region("us-west-2") - vpc = vpc_conn.create_vpc("10.0.0.0/16") - subnet = vpc_conn.create_subnet(vpc.id, "10.0.1.0/24") - - conn = boto.rds.connect_to_region("us-west-2") - conn.create_db_subnet_group("db_subnet1", "my db subnet", [subnet.id]) - conn.create_db_subnet_group("db_subnet2", "my db subnet", [subnet.id]) - - list(conn.get_all_db_subnet_groups()).should.have.length_of(2) - list(conn.get_all_db_subnet_groups("db_subnet1")).should.have.length_of(1) - - conn.get_all_db_subnet_groups.when.called_with("not-a-subnet").should.throw( - BotoServerError - ) - - -@mock_ec2_deprecated -@mock_rds_deprecated -def test_delete_database_subnet_group(): - vpc_conn = boto.vpc.connect_to_region("us-west-2") - vpc = vpc_conn.create_vpc("10.0.0.0/16") - subnet = vpc_conn.create_subnet(vpc.id, "10.0.1.0/24") - - conn = boto.rds.connect_to_region("us-west-2") - conn.create_db_subnet_group("db_subnet1", "my db subnet", [subnet.id]) - list(conn.get_all_db_subnet_groups()).should.have.length_of(1) - - conn.delete_db_subnet_group("db_subnet1") - list(conn.get_all_db_subnet_groups()).should.have.length_of(0) - - conn.delete_db_subnet_group.when.called_with("db_subnet1").should.throw( - BotoServerError - ) - - -@mock_ec2_deprecated -@mock_rds_deprecated -def test_create_database_in_subnet_group(): - vpc_conn = boto.vpc.connect_to_region("us-west-2") - vpc = vpc_conn.create_vpc("10.0.0.0/16") - subnet = vpc_conn.create_subnet(vpc.id, "10.0.1.0/24") - - conn = boto.rds.connect_to_region("us-west-2") - conn.create_db_subnet_group("db_subnet1", "my db subnet", [subnet.id]) - - database = conn.create_dbinstance( - "db-master-1", - 10, - "db.m1.small", - "root", - "hunter2", - db_subnet_group_name="db_subnet1", - ) - - database = conn.get_all_dbinstances("db-master-1")[0] - database.subnet_group.name.should.equal("db_subnet1") - - -@mock_rds_deprecated -def test_create_database_replica(): - conn = boto.rds.connect_to_region("us-west-2") - - primary = conn.create_dbinstance( - "db-master-1", 10, "db.m1.small", "root", "hunter2" - ) - - replica = conn.create_dbinstance_read_replica( - "replica", "db-master-1", "db.m1.small" - ) - replica.id.should.equal("replica") - replica.instance_class.should.equal("db.m1.small") - status_info = replica.status_infos[0] - status_info.normal.should.equal(True) - status_info.status_type.should.equal("read replication") - status_info.status.should.equal("replicating") - - primary = conn.get_all_dbinstances("db-master-1")[0] - primary.read_replica_dbinstance_identifiers[0].should.equal("replica") - - conn.delete_dbinstance("replica") - - primary = conn.get_all_dbinstances("db-master-1")[0] - list(primary.read_replica_dbinstance_identifiers).should.have.length_of(0) - - -@mock_rds_deprecated -def test_create_cross_region_database_replica(): - west_1_conn = boto.rds.connect_to_region("us-west-1") - west_2_conn = boto.rds.connect_to_region("us-west-2") - - primary = west_1_conn.create_dbinstance( - "db-master-1", 10, "db.m1.small", "root", "hunter2" - ) - - primary_arn = "arn:aws:rds:us-west-1:1234567890:db:db-master-1" - replica = west_2_conn.create_dbinstance_read_replica( - "replica", primary_arn, "db.m1.small" - ) - - primary = west_1_conn.get_all_dbinstances("db-master-1")[0] - primary.read_replica_dbinstance_identifiers[0].should.equal("replica") - - replica = west_2_conn.get_all_dbinstances("replica")[0] - replica.instance_class.should.equal("db.m1.small") - - west_2_conn.delete_dbinstance("replica") - - primary = west_1_conn.get_all_dbinstances("db-master-1")[0] - list(primary.read_replica_dbinstance_identifiers).should.have.length_of(0) - - -@mock_rds_deprecated -def test_connecting_to_us_east_1(): - # boto does not use us-east-1 in the URL for RDS, - # and that broke moto in the past: - # https://github.com/boto/boto/blob/e271ff09364ea18d9d8b6f4d63d6b0ac6cbc9b75/boto/endpoints.json#L285 - conn = boto.rds.connect_to_region("us-east-1") - - database = conn.create_dbinstance( - "db-master-1", 10, "db.m1.small", "root", "hunter2", security_groups=["my_sg"] - ) - - database.status.should.equal("available") - database.id.should.equal("db-master-1") - database.allocated_storage.should.equal(10) - database.instance_class.should.equal("db.m1.small") - database.master_username.should.equal("root") - database.endpoint.should.equal( - ("db-master-1.aaaaaaaaaa.us-east-1.rds.amazonaws.com", 3306) - ) - database.security_groups[0].name.should.equal("my_sg") - - -@mock_rds_deprecated -def test_create_database_with_iops(): - conn = boto.rds.connect_to_region("us-west-2") - - database = conn.create_dbinstance( - "db-master-1", 10, "db.m1.small", "root", "hunter2", iops=6000 - ) - - database.status.should.equal("available") - database.iops.should.equal(6000) - # boto>2.36.0 may change the following property name to `storage_type` - database.StorageType.should.equal("io1") diff --git a/tests/test_redshift/test_redshift.py b/tests/test_redshift/test_redshift.py index 8c6d41bce..b66ed011c 100644 --- a/tests/test_redshift/test_redshift.py +++ b/tests/test_redshift/test_redshift.py @@ -1,23 +1,13 @@ import time import datetime -import boto import boto3 -from boto.redshift.exceptions import ( - ClusterNotFound, - ClusterParameterGroupNotFound, - ClusterSecurityGroupNotFound, - ClusterSubnetGroupNotFound, - InvalidSubnet, -) from botocore.exceptions import ClientError import pytest import sure # noqa # pylint: disable=unused-import from moto import mock_ec2 -from moto import mock_ec2_deprecated from moto import mock_redshift -from moto import mock_redshift_deprecated from moto.core import ACCOUNT_ID @@ -148,57 +138,6 @@ def test_no_snapshot_copy_grants(): len(response["SnapshotCopyGrants"]).should.equal(0) -# Has boto3 equivalent -@mock_redshift_deprecated -def test_create_cluster(): - conn = boto.redshift.connect_to_region("us-east-1") - cluster_identifier = "my_cluster" - - cluster_response = conn.create_cluster( - cluster_identifier, - node_type="dw.hs1.xlarge", - master_username="username", - master_user_password="password", - db_name="my_db", - cluster_type="multi-node", - availability_zone="us-east-1d", - preferred_maintenance_window="Mon:03:00-Mon:11:00", - automated_snapshot_retention_period=10, - port=1234, - cluster_version="1.0", - allow_version_upgrade=True, - number_of_nodes=3, - ) - cluster_response["CreateClusterResponse"]["CreateClusterResult"]["Cluster"][ - "ClusterStatus" - ].should.equal("creating") - - cluster_response = conn.describe_clusters(cluster_identifier) - cluster = cluster_response["DescribeClustersResponse"]["DescribeClustersResult"][ - "Clusters" - ][0] - - cluster["ClusterIdentifier"].should.equal(cluster_identifier) - cluster["NodeType"].should.equal("dw.hs1.xlarge") - cluster["MasterUsername"].should.equal("username") - cluster["DBName"].should.equal("my_db") - cluster["ClusterSecurityGroups"][0]["ClusterSecurityGroupName"].should.equal( - "Default" - ) - cluster["VpcSecurityGroups"].should.equal([]) - cluster["ClusterSubnetGroupName"].should.equal(None) - cluster["AvailabilityZone"].should.equal("us-east-1d") - cluster["PreferredMaintenanceWindow"].should.equal("Mon:03:00-Mon:11:00") - cluster["ClusterParameterGroups"][0]["ParameterGroupName"].should.equal( - "default.redshift-1.0" - ) - cluster["AutomatedSnapshotRetentionPeriod"].should.equal(10) - cluster["Port"].should.equal(1234) - cluster["ClusterVersion"].should.equal("1.0") - cluster["AllowVersionUpgrade"].should.equal(True) - cluster["NumberOfNodes"].should.equal(3) - - @mock_redshift def test_create_cluster_all_attributes(): """ @@ -285,33 +224,6 @@ def test_create_cluster_all_attributes(): cluster["NumberOfNodes"].should.equal(3) -# Has boto3 equivalent -@mock_redshift_deprecated -def test_create_single_node_cluster(): - conn = boto.redshift.connect_to_region("us-east-1") - cluster_identifier = "my_cluster" - - conn.create_cluster( - cluster_identifier, - node_type="dw.hs1.xlarge", - master_username="username", - master_user_password="password", - db_name="my_db", - cluster_type="single-node", - ) - - cluster_response = conn.describe_clusters(cluster_identifier) - cluster = cluster_response["DescribeClustersResponse"]["DescribeClustersResult"][ - "Clusters" - ][0] - - cluster["ClusterIdentifier"].should.equal(cluster_identifier) - cluster["NodeType"].should.equal("dw.hs1.xlarge") - cluster["MasterUsername"].should.equal("username") - cluster["DBName"].should.equal("my_db") - cluster["NumberOfNodes"].should.equal(1) - - @mock_redshift def test_create_single_node_cluster_boto3(): client = boto3.client("redshift", region_name="us-east-1") @@ -338,38 +250,6 @@ def test_create_single_node_cluster_boto3(): cluster["NumberOfNodes"].should.equal(1) -# Has boto3 equivalent -@mock_redshift_deprecated -def test_default_cluster_attributes(): - conn = boto.redshift.connect_to_region("us-east-1") - cluster_identifier = "my_cluster" - - conn.create_cluster( - cluster_identifier, - node_type="dw.hs1.xlarge", - master_username="username", - master_user_password="password", - ) - - cluster_response = conn.describe_clusters(cluster_identifier) - cluster = cluster_response["DescribeClustersResponse"]["DescribeClustersResult"][ - "Clusters" - ][0] - - cluster["DBName"].should.equal("dev") - cluster["ClusterSubnetGroupName"].should.equal(None) - assert "us-east-" in cluster["AvailabilityZone"] - cluster["PreferredMaintenanceWindow"].should.equal("Mon:03:00-Mon:03:30") - cluster["ClusterParameterGroups"][0]["ParameterGroupName"].should.equal( - "default.redshift-1.0" - ) - cluster["AutomatedSnapshotRetentionPeriod"].should.equal(1) - cluster["Port"].should.equal(5439) - cluster["ClusterVersion"].should.equal("1.0") - cluster["AllowVersionUpgrade"].should.equal(True) - cluster["NumberOfNodes"].should.equal(1) - - @mock_redshift @mock_ec2 def test_create_cluster_in_subnet_group(): @@ -422,32 +302,6 @@ def test_create_cluster_in_subnet_group_boto3(): cluster["ClusterSubnetGroupName"].should.equal("my_subnet_group") -# Has boto3 equivalent -@mock_redshift_deprecated -def test_create_cluster_with_security_group(): - conn = boto.redshift.connect_to_region("us-east-1") - conn.create_cluster_security_group("security_group1", "This is my security group") - conn.create_cluster_security_group("security_group2", "This is my security group") - - cluster_identifier = "my_cluster" - conn.create_cluster( - cluster_identifier, - node_type="dw.hs1.xlarge", - master_username="username", - master_user_password="password", - cluster_security_groups=["security_group1", "security_group2"], - ) - - cluster_response = conn.describe_clusters(cluster_identifier) - cluster = cluster_response["DescribeClustersResponse"]["DescribeClustersResult"][ - "Clusters" - ][0] - group_names = [ - group["ClusterSecurityGroupName"] for group in cluster["ClusterSecurityGroups"] - ] - set(group_names).should.equal(set(["security_group1", "security_group2"])) - - @mock_redshift def test_create_cluster_with_security_group_boto3(): client = boto3.client("redshift", region_name="us-east-1") @@ -476,34 +330,6 @@ def test_create_cluster_with_security_group_boto3(): set(group_names).should.equal({"security_group1", "security_group2"}) -# Has boto3 equivalent -@mock_redshift_deprecated -@mock_ec2_deprecated -def test_create_cluster_with_vpc_security_groups(): - vpc_conn = boto.connect_vpc() - ec2_conn = boto.connect_ec2() - redshift_conn = boto.connect_redshift() - vpc = vpc_conn.create_vpc("10.0.0.0/16") - security_group = ec2_conn.create_security_group( - "vpc_security_group", "a group", vpc_id=vpc.id - ) - - redshift_conn.create_cluster( - "my_cluster", - node_type="dw.hs1.xlarge", - master_username="username", - master_user_password="password", - vpc_security_group_ids=[security_group.id], - ) - - cluster_response = redshift_conn.describe_clusters("my_cluster") - cluster = cluster_response["DescribeClustersResponse"]["DescribeClustersResult"][ - "Clusters" - ][0] - group_ids = [group["VpcSecurityGroupId"] for group in cluster["VpcSecurityGroups"]] - list(group_ids).should.equal([security_group.id]) - - @mock_redshift @mock_ec2 def test_create_cluster_with_vpc_security_groups_boto3(): @@ -545,31 +371,6 @@ def test_create_cluster_with_iam_roles(): iam_roles_arn.should.equal(iam_roles) -# Has boto3 equivalent -@mock_redshift_deprecated -def test_create_cluster_with_parameter_group(): - conn = boto.connect_redshift() - conn.create_cluster_parameter_group( - "my_parameter_group", "redshift-1.0", "This is my parameter group" - ) - - conn.create_cluster( - "my_cluster", - node_type="dw.hs1.xlarge", - master_username="username", - master_user_password="password", - cluster_parameter_group_name="my_parameter_group", - ) - - cluster_response = conn.describe_clusters("my_cluster") - cluster = cluster_response["DescribeClustersResponse"]["DescribeClustersResult"][ - "Clusters" - ][0] - cluster["ClusterParameterGroups"][0]["ParameterGroupName"].should.equal( - "my_parameter_group" - ) - - @mock_redshift def test_create_cluster_with_parameter_group_boto3(): client = boto3.client("redshift", region_name="us-east-1") @@ -607,15 +408,6 @@ def test_create_cluster_with_parameter_group_boto3(): cluster["ClusterParameterGroups"][0]["ParameterApplyStatus"].should.equal("in-sync") -# Has boto3 equivalent -@mock_redshift_deprecated -def test_describe_non_existent_cluster(): - conn = boto.redshift.connect_to_region("us-east-1") - conn.describe_clusters.when.called_with("not-a-cluster").should.throw( - ClusterNotFound - ) - - @mock_redshift def test_describe_non_existent_cluster_boto3(): client = boto3.client("redshift", region_name="us-east-1") @@ -626,51 +418,6 @@ def test_describe_non_existent_cluster_boto3(): err["Message"].should.equal("Cluster not-a-cluster not found.") -# Has boto3 equivalent -@mock_redshift_deprecated -def test_delete_cluster(): - conn = boto.connect_redshift() - cluster_identifier = "my_cluster" - snapshot_identifier = "my_snapshot" - - conn.create_cluster( - cluster_identifier, - node_type="single-node", - master_username="username", - master_user_password="password", - ) - - conn.delete_cluster.when.called_with(cluster_identifier, False).should.throw( - boto.exception.JSONResponseError - ) - - clusters = conn.describe_clusters()["DescribeClustersResponse"][ - "DescribeClustersResult" - ]["Clusters"] - list(clusters).should.have.length_of(1) - - conn.delete_cluster( - cluster_identifier=cluster_identifier, - skip_final_cluster_snapshot=False, - final_cluster_snapshot_identifier=snapshot_identifier, - ) - - clusters = conn.describe_clusters()["DescribeClustersResponse"][ - "DescribeClustersResult" - ]["Clusters"] - list(clusters).should.have.length_of(0) - - snapshots = conn.describe_cluster_snapshots()["DescribeClusterSnapshotsResponse"][ - "DescribeClusterSnapshotsResult" - ]["Snapshots"] - list(snapshots).should.have.length_of(1) - - assert snapshot_identifier in snapshots[0]["SnapshotIdentifier"] - - # Delete invalid id - conn.delete_cluster.when.called_with("not-a-cluster").should.throw(ClusterNotFound) - - @mock_redshift def test_modify_cluster_vpc_routing(): iam_roles_arn = ["arn:aws:iam:::role/my-iam-role"] @@ -781,61 +528,6 @@ def test_modify_cluster_boto3(): cluster["NumberOfNodes"].should.equal(4) -# Has boto3 equivalent -@mock_redshift_deprecated -def test_modify_cluster(): - conn = boto.connect_redshift() - cluster_identifier = "my_cluster" - conn.create_cluster_security_group("security_group", "This is my security group") - conn.create_cluster_parameter_group( - "my_parameter_group", "redshift-1.0", "This is my parameter group" - ) - - conn.create_cluster( - cluster_identifier, - node_type="single-node", - master_username="username", - master_user_password="password", - ) - - cluster_response = conn.describe_clusters(cluster_identifier) - cluster = cluster_response["DescribeClustersResponse"]["DescribeClustersResult"][ - "Clusters" - ][0] - cluster["EnhancedVpcRouting"].should.equal(False) - - conn.modify_cluster( - cluster_identifier, - cluster_type="multi-node", - number_of_nodes=4, - node_type="dw.hs1.xlarge", - cluster_security_groups="security_group", - master_user_password="new_password", - cluster_parameter_group_name="my_parameter_group", - automated_snapshot_retention_period=7, - preferred_maintenance_window="Tue:03:00-Tue:11:00", - allow_version_upgrade=False, - new_cluster_identifier=cluster_identifier, - ) - - cluster_response = conn.describe_clusters(cluster_identifier) - cluster = cluster_response["DescribeClustersResponse"]["DescribeClustersResult"][ - "Clusters" - ][0] - cluster["ClusterIdentifier"].should.equal(cluster_identifier) - cluster["NodeType"].should.equal("dw.hs1.xlarge") - cluster["ClusterSecurityGroups"][0]["ClusterSecurityGroupName"].should.equal( - "security_group" - ) - cluster["PreferredMaintenanceWindow"].should.equal("Tue:03:00-Tue:11:00") - cluster["ClusterParameterGroups"][0]["ParameterGroupName"].should.equal( - "my_parameter_group" - ) - cluster["AutomatedSnapshotRetentionPeriod"].should.equal(7) - cluster["AllowVersionUpgrade"].should.equal(False) - cluster["NumberOfNodes"].should.equal(4) - - @mock_redshift @mock_ec2 def test_create_cluster_subnet_group(): @@ -914,16 +606,6 @@ def test_authorize_security_group_ingress(): ) -# Has boto3 equivalent -@mock_redshift_deprecated -@mock_ec2_deprecated -def test_create_invalid_cluster_subnet_group(): - redshift_conn = boto.connect_redshift() - redshift_conn.create_cluster_subnet_group.when.called_with( - "my_subnet", "This is my subnet group", subnet_ids=["subnet-1234"] - ).should.throw(InvalidSubnet) - - @mock_redshift @mock_ec2 def test_create_invalid_cluster_subnet_group_boto3(): @@ -939,15 +621,6 @@ def test_create_invalid_cluster_subnet_group_boto3(): err["Message"].should.match(r"Subnet \[[a-z0-9-']+\] not found.") -# Has boto3 equivalent -@mock_redshift_deprecated -def test_describe_non_existent_subnet_group(): - conn = boto.redshift.connect_to_region("us-east-1") - conn.describe_cluster_subnet_groups.when.called_with( - "not-a-subnet-group" - ).should.throw(ClusterSubnetGroupNotFound) - - @mock_redshift @mock_ec2 def test_describe_non_existent_subnet_group_boto3(): @@ -989,22 +662,6 @@ def test_delete_cluster_subnet_group(): ).should.throw(ClientError) -# Has boto3 equivalent -@mock_redshift_deprecated -def test_create_cluster_security_group(): - conn = boto.connect_redshift() - conn.create_cluster_security_group("my_security_group", "This is my security group") - - groups_response = conn.describe_cluster_security_groups("my_security_group") - my_group = groups_response["DescribeClusterSecurityGroupsResponse"][ - "DescribeClusterSecurityGroupsResult" - ]["ClusterSecurityGroups"][0] - - my_group["ClusterSecurityGroupName"].should.equal("my_security_group") - my_group["Description"].should.equal("This is my security group") - list(my_group["IPRanges"]).should.equal([]) - - @mock_redshift def test_create_cluster_security_group_boto3(): client = boto3.client("redshift", region_name="us-east-1") @@ -1031,15 +688,6 @@ def test_create_cluster_security_group_boto3(): my_group["Tags"].should.equal([{"Key": "tag_key", "Value": "tag_value"}]) -# Has boto3 equivalent -@mock_redshift_deprecated -def test_describe_non_existent_security_group(): - conn = boto.redshift.connect_to_region("us-east-1") - conn.describe_cluster_security_groups.when.called_with( - "not-a-security-group" - ).should.throw(ClusterSecurityGroupNotFound) - - @mock_redshift def test_describe_non_existent_security_group_boto3(): client = boto3.client("redshift", region_name="us-east-1") @@ -1051,32 +699,6 @@ def test_describe_non_existent_security_group_boto3(): err["Message"].should.equal("Security group non-existent not found.") -# Has boto3 equivalent -@mock_redshift_deprecated -def test_delete_cluster_security_group(): - conn = boto.connect_redshift() - conn.create_cluster_security_group("my_security_group", "This is my security group") - - groups_response = conn.describe_cluster_security_groups() - groups = groups_response["DescribeClusterSecurityGroupsResponse"][ - "DescribeClusterSecurityGroupsResult" - ]["ClusterSecurityGroups"] - groups.should.have.length_of(2) # The default group already exists - - conn.delete_cluster_security_group("my_security_group") - - groups_response = conn.describe_cluster_security_groups() - groups = groups_response["DescribeClusterSecurityGroupsResponse"][ - "DescribeClusterSecurityGroupsResult" - ]["ClusterSecurityGroups"] - groups.should.have.length_of(1) - - # Delete invalid id - conn.delete_cluster_security_group.when.called_with( - "not-a-security-group" - ).should.throw(ClusterSecurityGroupNotFound) - - @mock_redshift def test_delete_cluster_security_group_boto3(): client = boto3.client("redshift", region_name="us-east-1") @@ -1103,24 +725,6 @@ def test_delete_cluster_security_group_boto3(): err["Message"].should.equal("Security group not-a-security-group not found.") -# Has boto3 equivalent -@mock_redshift_deprecated -def test_create_cluster_parameter_group(): - conn = boto.connect_redshift() - conn.create_cluster_parameter_group( - "my_parameter_group", "redshift-1.0", "This is my parameter group" - ) - - groups_response = conn.describe_cluster_parameter_groups("my_parameter_group") - my_group = groups_response["DescribeClusterParameterGroupsResponse"][ - "DescribeClusterParameterGroupsResult" - ]["ParameterGroups"][0] - - my_group["ParameterGroupName"].should.equal("my_parameter_group") - my_group["ParameterGroupFamily"].should.equal("redshift-1.0") - my_group["Description"].should.equal("This is my parameter group") - - @mock_redshift def test_create_cluster_parameter_group_boto3(): client = boto3.client("redshift", region_name="us-east-1") @@ -1144,15 +748,6 @@ def test_create_cluster_parameter_group_boto3(): my_group["Description"].should.equal("This is my group") -# Has boto3 equivalent -@mock_redshift_deprecated -def test_describe_non_existent_parameter_group(): - conn = boto.redshift.connect_to_region("us-east-1") - conn.describe_cluster_parameter_groups.when.called_with( - "not-a-parameter-group" - ).should.throw(ClusterParameterGroupNotFound) - - @mock_redshift def test_describe_non_existent_parameter_group_boto3(): client = boto3.client("redshift", region_name="us-east-1") @@ -1165,34 +760,6 @@ def test_describe_non_existent_parameter_group_boto3(): err["Message"].should.equal("Parameter group not-a-parameter-group not found.") -# Has boto3 equivalent -@mock_redshift_deprecated -def test_delete_cluster_parameter_group(): - conn = boto.connect_redshift() - conn.create_cluster_parameter_group( - "my_parameter_group", "redshift-1.0", "This is my parameter group" - ) - - groups_response = conn.describe_cluster_parameter_groups() - groups = groups_response["DescribeClusterParameterGroupsResponse"][ - "DescribeClusterParameterGroupsResult" - ]["ParameterGroups"] - groups.should.have.length_of(2) # The default group already exists - - conn.delete_cluster_parameter_group("my_parameter_group") - - groups_response = conn.describe_cluster_parameter_groups() - groups = groups_response["DescribeClusterParameterGroupsResponse"][ - "DescribeClusterParameterGroupsResult" - ]["ParameterGroups"] - groups.should.have.length_of(1) - - # Delete invalid id - conn.delete_cluster_parameter_group.when.called_with( - "not-a-parameter-group" - ).should.throw(ClusterParameterGroupNotFound) - - @mock_redshift def test_delete_parameter_group_boto3(): client = boto3.client("redshift", region_name="us-east-1") diff --git a/tests/test_route53/test_route53.py b/tests/test_route53/test_route53.py index 9ac7144b8..3516b64fd 100644 --- a/tests/test_route53/test_route53.py +++ b/tests/test_route53/test_route53.py @@ -1,7 +1,4 @@ -import boto import boto3 -from boto.route53.healthcheck import HealthCheck -from boto.route53.record import ResourceRecordSets from botocore.exceptions import ClientError import sure # noqa # pylint: disable=unused-import @@ -9,32 +6,7 @@ import sure # noqa # pylint: disable=unused-import import botocore import pytest -from moto import mock_route53, mock_route53_deprecated - - -# Has boto3 equivalent -@mock_route53_deprecated -def test_hosted_zone(): - conn = boto.connect_route53("the_key", "the_secret") - firstzone = conn.create_hosted_zone("testdns.aws.com") - zones = conn.get_all_hosted_zones() - len(zones["ListHostedZonesResponse"]["HostedZones"]).should.equal(1) - - conn.create_hosted_zone("testdns1.aws.com") - zones = conn.get_all_hosted_zones() - len(zones["ListHostedZonesResponse"]["HostedZones"]).should.equal(2) - - id1 = firstzone["CreateHostedZoneResponse"]["HostedZone"]["Id"].split("/")[-1] - zone = conn.get_hosted_zone(id1) - zone["GetHostedZoneResponse"]["HostedZone"]["Name"].should.equal("testdns.aws.com.") - - conn.delete_hosted_zone(id1) - zones = conn.get_all_hosted_zones() - len(zones["ListHostedZonesResponse"]["HostedZones"]).should.equal(1) - - conn.get_hosted_zone.when.called_with("abcd").should.throw( - boto.route53.exception.DNSServerError, "404 Not Found" - ) +from moto import mock_route53 @mock_route53 @@ -101,120 +73,6 @@ def test_get_unknown_hosted_zone(): err["Message"].should.equal("No hosted zone found with ID: unknown") -# Has boto3 equivalent -@mock_route53_deprecated -def test_rrset(): - conn = boto.connect_route53("the_key", "the_secret") - - conn.get_all_rrsets.when.called_with("abcd").should.throw( - boto.route53.exception.DNSServerError, "404 Not Found" - ) - - zone = conn.create_hosted_zone("testdns.aws.com") - zoneid = zone["CreateHostedZoneResponse"]["HostedZone"]["Id"].split("/")[-1] - - conn.get_all_rrsets.when.called_with(zoneid, type="A").should.throw( - boto.route53.exception.DNSServerError, "400 Bad Request" - ) - - changes = ResourceRecordSets(conn, zoneid) - change = changes.add_change("CREATE", "foo.bar.testdns.aws.com", "A") - change.add_value("1.2.3.4") - changes.commit() - - rrsets = conn.get_all_rrsets(zoneid) - rrsets.should.have.length_of(1) - rrsets[0].resource_records[0].should.equal("1.2.3.4") - - rrsets = conn.get_all_rrsets(zoneid, name="foo.bar.testdns.aws.com.", type="CNAME") - rrsets.should.have.length_of(0) - - changes = ResourceRecordSets(conn, zoneid) - changes.add_change("DELETE", "foo.bar.testdns.aws.com", "A") - change = changes.add_change("CREATE", "foo.bar.testdns.aws.com", "A") - change.add_value("5.6.7.8") - changes.commit() - - rrsets = conn.get_all_rrsets(zoneid) - rrsets.should.have.length_of(1) - rrsets[0].resource_records[0].should.equal("5.6.7.8") - - changes = ResourceRecordSets(conn, zoneid) - changes.add_change("DELETE", "foo.bar.testdns.aws.com", "A") - changes.commit() - - rrsets = conn.get_all_rrsets(zoneid) - rrsets.should.have.length_of(0) - - changes = ResourceRecordSets(conn, zoneid) - change = changes.add_change("UPSERT", "foo.bar.testdns.aws.com", "A") - change.add_value("1.2.3.4") - changes.commit() - - rrsets = conn.get_all_rrsets(zoneid) - rrsets.should.have.length_of(1) - rrsets[0].resource_records[0].should.equal("1.2.3.4") - - changes = ResourceRecordSets(conn, zoneid) - change = changes.add_change("UPSERT", "foo.bar.testdns.aws.com", "A") - change.add_value("5.6.7.8") - changes.commit() - - rrsets = conn.get_all_rrsets(zoneid) - rrsets.should.have.length_of(1) - rrsets[0].resource_records[0].should.equal("5.6.7.8") - - changes = ResourceRecordSets(conn, zoneid) - change = changes.add_change("UPSERT", "foo.bar.testdns.aws.com", "TXT") - change.add_value("foo") - changes.commit() - - rrsets = conn.get_all_rrsets(zoneid) - rrsets.should.have.length_of(2) - rrsets[0].resource_records[0].should.equal("5.6.7.8") - rrsets[1].resource_records[0].should.equal("foo") - - changes = ResourceRecordSets(conn, zoneid) - changes.add_change("DELETE", "foo.bar.testdns.aws.com", "A") - changes.add_change("DELETE", "foo.bar.testdns.aws.com", "TXT") - changes.commit() - - changes = ResourceRecordSets(conn, zoneid) - change = changes.add_change("CREATE", "bar.foo.testdns.aws.com", "TXT") - change.add_value("bar") - change = changes.add_change("CREATE", "foo.bar.testdns.aws.com", "A") - change.add_value("1.2.3.4") - change = changes.add_change("CREATE", "bar.foo.testdns.aws.com", "A") - change.add_value("5.6.7.8") - changes.commit() - - rrsets = conn.get_all_rrsets(zoneid) - rrsets.should.have.length_of(3) - rrsets[0].resource_records[0].should.equal("1.2.3.4") - rrsets[1].resource_records[0].should.equal("5.6.7.8") - rrsets[2].resource_records[0].should.equal("bar") - - rrsets = conn.get_all_rrsets(zoneid, name="foo.bar.testdns.aws.com", type="A") - rrsets.should.have.length_of(3) - resource_records = [rr for rr_set in rrsets for rr in rr_set.resource_records] - resource_records.should.contain("1.2.3.4") - resource_records.should.contain("5.6.7.8") - resource_records.should.contain("bar") - - rrsets = conn.get_all_rrsets(zoneid, name="foo.testdns.aws.com", type="A") - rrsets.should.have.length_of(2) - rrsets[0].resource_records[0].should.equal("5.6.7.8") - rrsets[1].resource_records[0].should.equal("bar") - - rrsets = conn.get_all_rrsets(zoneid, name="foo.bar.testdns.aws.com", type="TXT") - rrsets.should.have.length_of(2) - rrsets[0].resource_records[0].should.equal("5.6.7.8") - rrsets[1].resource_records[0].should.equal("bar") - - rrsets = conn.get_all_rrsets(zoneid, name="foo.foo.testdns.aws.com", type="A") - rrsets.should.have.length_of(0) - - @mock_route53 def test_list_resource_record_set_unknown_zone(): conn = boto3.client("route53", region_name="us-east-1") @@ -242,92 +100,6 @@ def test_list_resource_record_set_unknown_type(): err["Message"].should.equal("Bad Request") -# Has boto3 equivalent -@mock_route53_deprecated -def test_rrset_with_multiple_values(): - conn = boto.connect_route53("the_key", "the_secret") - zone = conn.create_hosted_zone("testdns.aws.com") - zoneid = zone["CreateHostedZoneResponse"]["HostedZone"]["Id"].split("/")[-1] - - changes = ResourceRecordSets(conn, zoneid) - change = changes.add_change("CREATE", "foo.bar.testdns.aws.com", "A") - change.add_value("1.2.3.4") - change.add_value("5.6.7.8") - changes.commit() - - rrsets = conn.get_all_rrsets(zoneid) - rrsets.should.have.length_of(1) - set(rrsets[0].resource_records).should.equal(set(["1.2.3.4", "5.6.7.8"])) - - -# Has boto3 equivalent -@mock_route53_deprecated -def test_alias_rrset(): - conn = boto.connect_route53("the_key", "the_secret") - zone = conn.create_hosted_zone("testdns.aws.com") - zoneid = zone["CreateHostedZoneResponse"]["HostedZone"]["Id"].split("/")[-1] - - changes = ResourceRecordSets(conn, zoneid) - changes.add_change( - "CREATE", - "foo.alias.testdns.aws.com", - "A", - alias_hosted_zone_id="Z3DG6IL3SJCGPX", - alias_dns_name="foo.testdns.aws.com", - ) - changes.add_change( - "CREATE", - "bar.alias.testdns.aws.com", - "CNAME", - alias_hosted_zone_id="Z3DG6IL3SJCGPX", - alias_dns_name="bar.testdns.aws.com", - ) - changes.commit() - - rrsets = conn.get_all_rrsets(zoneid, name="alias.testdns.aws.com") - alias_targets = [rr_set.alias_dns_name for rr_set in rrsets] - alias_targets.should.have.length_of(2) - alias_targets.should.contain("foo.testdns.aws.com") - alias_targets.should.contain("bar.testdns.aws.com") - rrsets[0].alias_dns_name.should.equal("bar.testdns.aws.com") - rrsets[0].resource_records.should.have.length_of(0) - rrsets = conn.get_all_rrsets(zoneid, name="foo.alias.testdns.aws.com", type="A") - rrsets.should.have.length_of(1) - rrsets[0].alias_dns_name.should.equal("foo.testdns.aws.com") - rrsets[0].resource_records.should.have.length_of(0) - - -# Has boto3 equivalent -@mock_route53_deprecated -def test_create_health_check(): - conn = boto.connect_route53("the_key", "the_secret") - - check = HealthCheck( - ip_addr="10.0.0.25", - port=80, - hc_type="HTTP", - resource_path="/", - fqdn="example.com", - string_match="a good response", - request_interval=10, - failure_threshold=2, - ) - conn.create_health_check(check) - - checks = conn.get_list_health_checks()["ListHealthChecksResponse"]["HealthChecks"] - list(checks).should.have.length_of(1) - check = checks[0] - config = check["HealthCheckConfig"] - config["IPAddress"].should.equal("10.0.0.25") - config["Port"].should.equal("80") - config["Type"].should.equal("HTTP") - config["ResourcePath"].should.equal("/") - config["FullyQualifiedDomainName"].should.equal("example.com") - config["SearchString"].should.equal("a good response") - config["RequestInterval"].should.equal("10") - config["FailureThreshold"].should.equal("2") - - @mock_route53 def test_create_health_check_boto3(): conn = boto3.client("route53", region_name="us-east-1") @@ -386,23 +158,6 @@ def test_list_health_checks_boto3(): checks.should.contain(check) -# Has boto3 equivalent -@mock_route53_deprecated -def test_delete_health_check(): - conn = boto.connect_route53("the_key", "the_secret") - - check = HealthCheck(ip_addr="10.0.0.25", port=80, hc_type="HTTP", resource_path="/") - conn.create_health_check(check) - - checks = conn.get_list_health_checks()["ListHealthChecksResponse"]["HealthChecks"] - list(checks).should.have.length_of(1) - health_check_id = checks[0]["Id"] - - conn.delete_health_check(health_check_id) - checks = conn.get_list_health_checks()["ListHealthChecksResponse"]["HealthChecks"] - list(checks).should.have.length_of(0) - - @mock_route53 def test_delete_health_checks_boto3(): conn = boto3.client("route53", region_name="us-east-1") @@ -429,29 +184,6 @@ def test_delete_health_checks_boto3(): checks.should.have.length_of(0) -# Has boto3 equivalent -@mock_route53_deprecated -def test_use_health_check_in_resource_record_set(): - conn = boto.connect_route53("the_key", "the_secret") - - check = HealthCheck(ip_addr="10.0.0.25", port=80, hc_type="HTTP", resource_path="/") - check = conn.create_health_check(check)["CreateHealthCheckResponse"]["HealthCheck"] - check_id = check["Id"] - - zone = conn.create_hosted_zone("testdns.aws.com") - zone_id = zone["CreateHostedZoneResponse"]["HostedZone"]["Id"].split("/")[-1] - - changes = ResourceRecordSets(conn, zone_id) - change = changes.add_change( - "CREATE", "foo.bar.testdns.aws.com", "A", health_check=check_id - ) - change.add_value("1.2.3.4") - changes.commit() - - record_sets = conn.get_all_rrsets(zone_id) - record_sets[0].health_check.should.equal(check_id) - - @mock_route53 def test_use_health_check_in_resource_record_set_boto3(): conn = boto3.client("route53", region_name="us-east-1") @@ -498,28 +230,6 @@ def test_use_health_check_in_resource_record_set_boto3(): record_sets[0]["HealthCheckId"].should.equal(check_id) -# Has boto3 equivalent -@mock_route53_deprecated -def test_hosted_zone_comment_preserved(): - conn = boto.connect_route53("the_key", "the_secret") - - firstzone = conn.create_hosted_zone("testdns.aws.com.", comment="test comment") - zone_id = firstzone["CreateHostedZoneResponse"]["HostedZone"]["Id"].split("/")[-1] - - hosted_zone = conn.get_hosted_zone(zone_id) - hosted_zone["GetHostedZoneResponse"]["HostedZone"]["Config"][ - "Comment" - ].should.equal("test comment") - - hosted_zones = conn.get_all_hosted_zones() - hosted_zones["ListHostedZonesResponse"]["HostedZones"][0]["Config"][ - "Comment" - ].should.equal("test comment") - - zone = conn.get_zone("testdns.aws.com.") - zone.config["Comment"].should.equal("test comment") - - @mock_route53 def test_hosted_zone_comment_preserved_boto3(): conn = boto3.client("route53", region_name="us-east-1") @@ -538,32 +248,6 @@ def test_hosted_zone_comment_preserved_boto3(): hosted_zones["HostedZones"][0]["Config"]["Comment"].should.equal("test comment") -# Has boto3 equivalent -@mock_route53_deprecated -def test_deleting_weighted_route(): - conn = boto.connect_route53() - - conn.create_hosted_zone("testdns.aws.com.") - zone = conn.get_zone("testdns.aws.com.") - - zone.add_cname( - "cname.testdns.aws.com", "example.com", identifier=("success-test-foo", "50") - ) - zone.add_cname( - "cname.testdns.aws.com", "example.com", identifier=("success-test-bar", "50") - ) - - cnames = zone.get_cname("cname.testdns.aws.com.", all=True) - cnames.should.have.length_of(2) - foo_cname = [cname for cname in cnames if cname.identifier == "success-test-foo"][0] - - zone.delete_record(foo_cname) - cname = zone.get_cname("cname.testdns.aws.com.", all=True) - # When get_cname only had one result, it returns just that result instead - # of a list. - cname.identifier.should.equal("success-test-bar") - - @mock_route53 def test_deleting_weighted_route_boto3(): conn = boto3.client("route53", region_name="us-east-1") @@ -620,38 +304,6 @@ def test_deleting_weighted_route_boto3(): cnames[0]["SetIdentifier"].should.equal("success-test-bar") -# Has boto3 equivalent -@mock_route53_deprecated -def test_deleting_latency_route(): - conn = boto.connect_route53() - - conn.create_hosted_zone("testdns.aws.com.") - zone = conn.get_zone("testdns.aws.com.") - - zone.add_cname( - "cname.testdns.aws.com", - "example.com", - identifier=("success-test-foo", "us-west-2"), - ) - zone.add_cname( - "cname.testdns.aws.com", - "example.com", - identifier=("success-test-bar", "us-west-1"), - ) - - cnames = zone.get_cname("cname.testdns.aws.com.", all=True) - cnames.should.have.length_of(2) - foo_cname = [cname for cname in cnames if cname.identifier == "success-test-foo"][0] - foo_cname.region.should.equal("us-west-2") - - zone.delete_record(foo_cname) - cname = zone.get_cname("cname.testdns.aws.com.", all=True) - # When get_cname only had one result, it returns just that result instead - # of a list. - cname.identifier.should.equal("success-test-bar") - cname.region.should.equal("us-west-1") - - @mock_route53 def test_deleting_latency_route_boto3(): conn = boto3.client("route53", region_name="us-east-1") @@ -715,31 +367,6 @@ def test_deleting_latency_route_boto3(): cnames[0]["Region"].should.equal("us-west-1") -# Has boto3 equivalent -@mock_route53_deprecated -def test_hosted_zone_private_zone_preserved(): - conn = boto.connect_route53("the_key", "the_secret") - - firstzone = conn.create_hosted_zone( - "testdns.aws.com.", private_zone=True, vpc_id="vpc-fake", vpc_region="us-east-1" - ) - zone_id = firstzone["CreateHostedZoneResponse"]["HostedZone"]["Id"].split("/")[-1] - - hosted_zone = conn.get_hosted_zone(zone_id) - # in (original) boto, these bools returned as strings. - hosted_zone["GetHostedZoneResponse"]["HostedZone"]["Config"][ - "PrivateZone" - ].should.equal("True") - - hosted_zones = conn.get_all_hosted_zones() - hosted_zones["ListHostedZonesResponse"]["HostedZones"][0]["Config"][ - "PrivateZone" - ].should.equal("True") - - zone = conn.get_zone("testdns.aws.com.") - zone.config["PrivateZone"].should.equal("True") - - @mock_route53 def test_hosted_zone_private_zone_preserved_boto3(): conn = boto3.client("route53", region_name="us-east-1") diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index 6b6f50eed..f00f4232e 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -1,8 +1,6 @@ import datetime import os from boto3 import Session -from urllib.request import urlopen -from urllib.error import HTTPError from urllib.parse import urlparse, parse_qs from functools import wraps from gzip import GzipFile @@ -12,13 +10,10 @@ import pickle import uuid import json -import boto import boto3 from botocore.client import ClientError import botocore.exceptions -from boto.exception import S3CreateError, S3ResponseError -from boto.s3.connection import S3Connection -from boto.s3.key import Key +from botocore.handlers import disable_signing from freezegun import freeze_time import requests @@ -28,7 +23,7 @@ import pytest import sure # noqa # pylint: disable=unused-import -from moto import settings, mock_s3, mock_s3_deprecated, mock_config, mock_kms +from moto import settings, mock_s3, mock_config, mock_kms import moto.s3.models as s3model from moto.core.exceptions import InvalidNextTokenException from moto.settings import get_s3_default_key_buffer_size, S3_UPLOAD_PART_MIN_SIZE @@ -83,39 +78,6 @@ def test_keys_are_pickleable(): assert loaded.value == key.value -@mock_s3 -def test_append_to_value__basic(): - key = s3model.FakeKey("name", b"data!") - assert key.value == b"data!" - assert key.size == 5 - - key.append_to_value(b" And even more data") - assert key.value == b"data! And even more data" - assert key.size == 24 - - -@mock_s3 -def test_append_to_value__nothing_added(): - key = s3model.FakeKey("name", b"data!") - assert key.value == b"data!" - assert key.size == 5 - - key.append_to_value(b"") - assert key.value == b"data!" - assert key.size == 5 - - -@mock_s3 -def test_append_to_value__empty_key(): - key = s3model.FakeKey("name", b"") - assert key.value == b"" - assert key.size == 0 - - key.append_to_value(b"stuff") - assert key.value == b"stuff" - assert key.size == 5 - - @mock_s3 def test_my_model_save(): # Create Bucket so that test can run @@ -162,19 +124,6 @@ def test_key_etag(): ) -# Has boto3 equivalent -@mock_s3_deprecated -def test_multipart_upload_too_small(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - - multipart = bucket.initiate_multipart_upload("the-key") - multipart.upload_part_from_file(BytesIO(b"hello"), 1) - multipart.upload_part_from_file(BytesIO(b"world"), 2) - # Multipart with total size under 5MB is refused - multipart.complete_upload.should.throw(S3ResponseError) - - @mock_s3 def test_multipart_upload_too_small_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -215,24 +164,6 @@ def test_multipart_upload_too_small_boto3(): ) -# Has boto3 equivalent -@mock_s3_deprecated -@reduced_min_part_size -def test_multipart_upload(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - - multipart = bucket.initiate_multipart_upload("the-key") - part1 = b"0" * REDUCED_PART_SIZE - multipart.upload_part_from_file(BytesIO(part1), 1) - # last part, can be less than 5 MB - part2 = b"1" - multipart.upload_part_from_file(BytesIO(part2), 2) - multipart.complete_upload() - # we should get both parts as the key contents - bucket.get_key("the-key").get_contents_as_string().should.equal(part1 + part2) - - @mock_s3 @reduced_min_part_size def test_multipart_upload_boto3(): @@ -274,24 +205,6 @@ def test_multipart_upload_boto3(): response["Body"].read().should.equal(part1 + part2) -# Has boto3 equivalent -@mock_s3_deprecated -@reduced_min_part_size -def test_multipart_upload_out_of_order(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - - multipart = bucket.initiate_multipart_upload("the-key") - # last part, can be less than 5 MB - part2 = b"1" - multipart.upload_part_from_file(BytesIO(part2), 4) - part1 = b"0" * REDUCED_PART_SIZE - multipart.upload_part_from_file(BytesIO(part1), 2) - multipart.complete_upload() - # we should get both parts as the key contents - bucket.get_key("the-key").get_contents_as_string().should.equal(part1 + part2) - - @mock_s3 @reduced_min_part_size def test_multipart_upload_out_of_order_boto3(): @@ -333,22 +246,6 @@ def test_multipart_upload_out_of_order_boto3(): response["Body"].read().should.equal(part1 + part2) -# Has boto3 equivalent -@mock_s3_deprecated -@reduced_min_part_size -def test_multipart_upload_with_headers(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - - multipart = bucket.initiate_multipart_upload("the-key", metadata={"foo": "bar"}) - part1 = b"0" * 10 - multipart.upload_part_from_file(BytesIO(part1), 1) - multipart.complete_upload() - - key = bucket.get_key("the-key") - key.metadata.should.equal({"foo": "bar"}) - - @mock_s3 @reduced_min_part_size def test_multipart_upload_with_headers_boto3(): @@ -379,33 +276,6 @@ def test_multipart_upload_with_headers_boto3(): response["Metadata"].should.equal({"meta": "data"}) -# Has boto3 equivalent -@pytest.mark.parametrize( - "original_key_name", - [ - "original-key", - "the-unicode-💩-key", - "key-with?question-mark", - "key-with%2Fembedded%2Furl%2Fencoding", - ], -) -@mock_s3_deprecated -@reduced_min_part_size -def test_multipart_upload_with_copy_key(original_key_name): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = original_key_name - key.set_contents_from_string("key_value") - - multipart = bucket.initiate_multipart_upload("the-key") - part1 = b"0" * REDUCED_PART_SIZE - multipart.upload_part_from_file(BytesIO(part1), 1) - multipart.copy_part_from_key("foobar", original_key_name, 2, 0, 3) - multipart.complete_upload() - bucket.get_key("the-key").get_contents_as_string().should.equal(part1 + b"key_") - - @pytest.mark.parametrize( "original_key_name", [ @@ -454,21 +324,6 @@ def test_multipart_upload_with_copy_key_boto3(original_key_name): response["Body"].read().should.equal(part1 + b"key_") -# Has boto3 equivalent -@mock_s3_deprecated -@reduced_min_part_size -def test_multipart_upload_cancel(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - - multipart = bucket.initiate_multipart_upload("the-key") - part1 = b"0" * REDUCED_PART_SIZE - multipart.upload_part_from_file(BytesIO(part1), 1) - multipart.cancel_upload() - # TODO we really need some sort of assertion here, but we don't currently - # have the ability to list mulipart uploads for a bucket. - - @mock_s3 @reduced_min_part_size def test_multipart_upload_cancel_boto3(): @@ -494,93 +349,6 @@ def test_multipart_upload_cancel_boto3(): s3.list_multipart_uploads(Bucket="foobar").shouldnt.have.key("Uploads") -# Has boto3 equivalent -@mock_s3_deprecated -@reduced_min_part_size -def test_multipart_etag(): - # Create Bucket so that test can run - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("mybucket") - - multipart = bucket.initiate_multipart_upload("the-key") - part1 = b"0" * REDUCED_PART_SIZE - multipart.upload_part_from_file(BytesIO(part1), 1) - # last part, can be less than 5 MB - part2 = b"1" - multipart.upload_part_from_file(BytesIO(part2), 2) - multipart.complete_upload() - # we should get both parts as the key contents - bucket.get_key("the-key").etag.should.equal(EXPECTED_ETAG) - - -# Has boto3 equivalent -@mock_s3_deprecated -@reduced_min_part_size -def test_multipart_version(): - # Create Bucket so that test can run - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("mybucket") - bucket.configure_versioning(versioning=True) - multipart = bucket.initiate_multipart_upload("the-key") - part1 = b"0" * REDUCED_PART_SIZE - multipart.upload_part_from_file(BytesIO(part1), 1) - # last part, can be less than 5 MB - part2 = b"1" - multipart.upload_part_from_file(BytesIO(part2), 2) - resp = multipart.complete_upload() - resp.version_id.should_not.be.none - - -# Not sure what's being tested here, as it throws an EntityTooSmall error -# Different part order is allowed and tested for in other tests -@mock_s3_deprecated -@reduced_min_part_size -def test_multipart_invalid_order(): - # Create Bucket so that test can run - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("mybucket") - - multipart = bucket.initiate_multipart_upload("the-key") - part1 = b"0" * 5242880 - etag1 = multipart.upload_part_from_file(BytesIO(part1), 1).etag - # last part, can be less than 5 MB - part2 = b"1" - etag2 = multipart.upload_part_from_file(BytesIO(part2), 2).etag - xml = "{0}{1}" - xml = xml.format(2, etag2) + xml.format(1, etag1) - xml = "{0}".format(xml) - bucket.complete_multipart_upload.when.called_with( - multipart.key_name, multipart.id, xml - ).should.throw(S3ResponseError) - - -# Has boto3 equivalent -@mock_s3_deprecated -@reduced_min_part_size -def test_multipart_etag_quotes_stripped(): - # Create Bucket so that test can run - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("mybucket") - - multipart = bucket.initiate_multipart_upload("the-key") - part1 = b"0" * REDUCED_PART_SIZE - etag1 = multipart.upload_part_from_file(BytesIO(part1), 1).etag - # last part, can be less than 5 MB - part2 = b"1" - etag2 = multipart.upload_part_from_file(BytesIO(part2), 2).etag - # Strip quotes from etags - etag1 = etag1.replace('"', "") - etag2 = etag2.replace('"', "") - xml = "{0}{1}" - xml = xml.format(1, etag1) + xml.format(2, etag2) - xml = "{0}".format(xml) - bucket.complete_multipart_upload.when.called_with( - multipart.key_name, multipart.id, xml - ).should_not.throw(S3ResponseError) - # we should get both parts as the key contents - bucket.get_key("the-key").etag.should.equal(EXPECTED_ETAG) - - @mock_s3 @reduced_min_part_size def test_multipart_etag_quotes_stripped_boto3(): @@ -622,25 +390,6 @@ def test_multipart_etag_quotes_stripped_boto3(): response["Body"].read().should.equal(part1 + b"key_") -# Has boto3 equivalent -@mock_s3_deprecated -@reduced_min_part_size -def test_multipart_duplicate_upload(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - - multipart = bucket.initiate_multipart_upload("the-key") - part1 = b"0" * REDUCED_PART_SIZE - multipart.upload_part_from_file(BytesIO(part1), 1) - # same part again - multipart.upload_part_from_file(BytesIO(part1), 1) - part2 = b"1" * 1024 - multipart.upload_part_from_file(BytesIO(part2), 2) - multipart.complete_upload() - # We should get only one copy of part 1. - bucket.get_key("the-key").get_contents_as_string().should.equal(part1 + part2) - - @mock_s3 @reduced_min_part_size def test_multipart_duplicate_upload_boto3(): @@ -690,29 +439,6 @@ def test_multipart_duplicate_upload_boto3(): response["Body"].read().should.equal(part1 + part2) -# Has boto3 equivalent -@mock_s3_deprecated -def test_list_multiparts(): - # Create Bucket so that test can run - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("mybucket") - - multipart1 = bucket.initiate_multipart_upload("one-key") - multipart2 = bucket.initiate_multipart_upload("two-key") - uploads = bucket.get_all_multipart_uploads() - uploads.should.have.length_of(2) - dict([(u.key_name, u.id) for u in uploads]).should.equal( - {"one-key": multipart1.id, "two-key": multipart2.id} - ) - multipart2.cancel_upload() - uploads = bucket.get_all_multipart_uploads() - uploads.should.have.length_of(1) - uploads[0].key_name.should.equal("one-key") - multipart1.cancel_upload() - uploads = bucket.get_all_multipart_uploads() - uploads.should.be.empty - - @mock_s3 def test_list_multiparts_boto3(): s3 = boto3.client("s3", region_name=DEFAULT_REGION_NAME) @@ -739,19 +465,6 @@ def test_list_multiparts_boto3(): res.shouldnt.have.key("Uploads") -# Has boto3 equivalent -@mock_s3_deprecated -def test_key_save_to_missing_bucket(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.get_bucket("mybucket", validate=False) - - key = Key(bucket) - key.key = "the-key" - key.set_contents_from_string.when.called_with("foobar").should.throw( - S3ResponseError - ) - - @mock_s3 def test_key_save_to_missing_bucket_boto3(): s3 = boto3.resource("s3") @@ -765,25 +478,6 @@ def test_key_save_to_missing_bucket_boto3(): ) -# Can't really be converted for boto3, as the approach is very different -@mock_s3_deprecated -def test_missing_key(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - bucket.get_key("the-key").should.equal(None) - - -# Has boto3 equivalent -@mock_s3_deprecated -def test_missing_key_urllib2(): - conn = boto.connect_s3("the_key", "the_secret") - conn.create_bucket("foobar") - - urlopen.when.called_with("http://foobar.s3.amazonaws.com/the-key").should.throw( - HTTPError - ) - - @mock_s3 def test_missing_key_request_boto3(): s3 = boto3.client("s3", region_name=DEFAULT_REGION_NAME) @@ -796,20 +490,6 @@ def test_missing_key_request_boto3(): response.status_code.should.equal(404) -# Has boto3 equivalent -@mock_s3_deprecated -def test_empty_key(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = "the-key" - key.set_contents_from_string("") - - key = bucket.get_key("the-key") - key.size.should.equal(0) - key.get_contents_as_string().should.equal(b"") - - @mock_s3 def test_empty_key_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -824,23 +504,6 @@ def test_empty_key_boto3(): resp["Body"].read().should.equal(b"") -# Has boto3 equivalent -@mock_s3_deprecated -def test_empty_key_set_on_existing_key(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = "the-key" - key.set_contents_from_string("foobar") - - key = bucket.get_key("the-key") - key.size.should.equal(6) - key.get_contents_as_string().should.equal(b"foobar") - - key.set_contents_from_string("") - bucket.get_key("the-key").get_contents_as_string().should.equal(b"") - - @mock_s3 def test_empty_key_set_on_existing_key_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -861,18 +524,6 @@ def test_empty_key_set_on_existing_key_boto3(): resp["Body"].read().should.equal(b"") -# Has boto3 equivalent -@mock_s3_deprecated -def test_large_key_save(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = "the-key" - key.set_contents_from_string("foobar" * 100000) - - bucket.get_key("the-key").get_contents_as_string().should.equal(b"foobar" * 100000) - - @mock_s3 def test_large_key_save_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -886,44 +537,6 @@ def test_large_key_save_boto3(): resp["Body"].read().should.equal(b"foobar" * 100000) -# Has boto3 equivalent -@mock_s3_deprecated -def test_copy_key(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = "the-key" - key.set_contents_from_string("some value") - - bucket.copy_key("new-key", "foobar", "the-key") - - bucket.get_key("the-key").get_contents_as_string().should.equal(b"some value") - bucket.get_key("new-key").get_contents_as_string().should.equal(b"some value") - - -# Has boto3 equivalent -@pytest.mark.parametrize( - "key_name", - [ - "the-unicode-💩-key", - "key-with?question-mark", - "key-with%2Fembedded%2Furl%2Fencoding", - ], -) -@mock_s3_deprecated -def test_copy_key_with_special_chars(key_name): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = key_name - key.set_contents_from_string("some value") - - bucket.copy_key("new-key", "foobar", key_name) - - bucket.get_key(key_name).get_contents_as_string().should.equal(b"some value") - bucket.get_key("new-key").get_contents_as_string().should.equal(b"some value") - - @pytest.mark.parametrize( "key_name", [ @@ -951,24 +564,6 @@ def test_copy_key_boto3(key_name): resp["Body"].read().should.equal(b"some value") -# Has boto3 equivalent -@mock_s3_deprecated -def test_copy_key_with_version(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - bucket.configure_versioning(versioning=True) - key = Key(bucket) - key.key = "the-key" - key.set_contents_from_string("some value") - key.set_contents_from_string("another value") - - key = [key.version_id for key in bucket.get_all_versions() if not key.is_latest][0] - bucket.copy_key("new-key", "foobar", "the-key", src_version_id=key) - - bucket.get_key("the-key").get_contents_as_string().should.equal(b"another value") - bucket.get_key("new-key").get_contents_as_string().should.equal(b"some value") - - @mock_s3 def test_copy_key_with_version_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -998,19 +593,6 @@ def test_copy_key_with_version_boto3(): resp["Body"].read().should.equal(b"some value") -# Has boto3 equivalent -@mock_s3_deprecated -def test_set_metadata(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = "the-key" - key.set_metadata("md", "Metadatastring") - key.set_contents_from_string("Testval") - - bucket.get_key("the-key").get_metadata("md").should.equal("Metadatastring") - - @mock_s3 def test_set_metadata_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -1024,24 +606,6 @@ def test_set_metadata_boto3(): resp["Metadata"].should.equal({"md": "Metadatastring"}) -# Has boto3 equivalent -@mock_s3_deprecated -def test_copy_key_replace_metadata(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = "the-key" - key.set_metadata("md", "Metadatastring") - key.set_contents_from_string("some value") - - bucket.copy_key( - "new-key", "foobar", "the-key", metadata={"momd": "Mometadatastring"} - ) - - bucket.get_key("new-key").get_metadata("md").should.be.none - bucket.get_key("new-key").get_metadata("momd").should.equal("Mometadatastring") - - @mock_s3 def test_copy_key_with_metadata_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -1085,25 +649,6 @@ def test_copy_key_replace_metadata_boto3(): resp["ETag"].should.equal(initial["ETag"]) -# Has boto3 equivalent -@freeze_time("2012-01-01 12:00:00") -@mock_s3_deprecated -def test_last_modified(): - # See https://github.com/boto/boto/issues/466 - conn = boto.connect_s3() - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = "the-key" - key.set_contents_from_string("some value") - - rs = bucket.get_all_keys() - rs[0].last_modified.should.equal("2012-01-01T12:00:00.000Z") - - bucket.get_key("the-key").last_modified.should.equal( - "Sun, 01 Jan 2012 12:00:00 GMT" - ) - - @freeze_time("2012-01-01 12:00:00") @mock_s3 def test_last_modified_boto3(): @@ -1126,20 +671,6 @@ def test_last_modified_boto3(): as_header.should.equal("Sun, 01 Jan 2012 12:00:00 GMT") -# Has boto3 equivalent -@mock_s3_deprecated -def test_missing_bucket(): - conn = boto.connect_s3("the_key", "the_secret") - conn.get_bucket.when.called_with("mybucket").should.throw(S3ResponseError) - - -# Has boto3 equivalent -@mock_s3_deprecated -def test_bucket_with_dash(): - conn = boto.connect_s3("the_key", "the_secret") - conn.get_bucket.when.called_with("mybucket-test").should.throw(S3ResponseError) - - @mock_s3 def test_missing_bucket_boto3(): client = boto3.client("s3", region_name=DEFAULT_REGION_NAME) @@ -1154,16 +685,6 @@ def test_missing_bucket_boto3(): ex.value.response["Error"]["Message"].should.equal("Not Found") -# Has boto3 equivalent -@mock_s3_deprecated -def test_create_existing_bucket(): - "Trying to create a bucket that already exists should raise an Error" - conn = boto.s3.connect_to_region("us-west-2") - conn.create_bucket("foobar", location="us-west-2") - with pytest.raises(S3CreateError): - conn.create_bucket("foobar", location="us-west-2") - - @mock_s3 def test_create_existing_bucket_boto3(): "Trying to create a bucket that already exists should raise an Error" @@ -1181,24 +702,6 @@ def test_create_existing_bucket_boto3(): ) -# Has boto3 equivalent -@mock_s3_deprecated -def test_create_existing_bucket_in_us_east_1(): - "Trying to create a bucket that already exists in us-east-1 returns the bucket" - - """" - http://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html - Your previous request to create the named bucket succeeded and you already - own it. You get this error in all AWS regions except US Standard, - us-east-1. In us-east-1 region, you will get 200 OK, but it is no-op (if - bucket exists it Amazon S3 will not do anything). - """ - conn = boto.s3.connect_to_region(DEFAULT_REGION_NAME) - conn.create_bucket("foobar") - bucket = conn.create_bucket("foobar") - bucket.name.should.equal("foobar") - - @mock_s3 def test_create_existing_bucket_in_us_east_1_boto3(): "Trying to create a bucket that already exists in us-east-1 returns the bucket" @@ -1215,36 +718,6 @@ def test_create_existing_bucket_in_us_east_1_boto3(): client.create_bucket(Bucket="foobar") -@mock_s3_deprecated -def test_other_region(): - conn = S3Connection("key", "secret", host="s3-website-ap-southeast-2.amazonaws.com") - conn.create_bucket("foobar", location="ap-southeast-2") - list(conn.get_bucket("foobar").get_all_keys()).should.equal([]) - - -# Has boto3 equivalent -@mock_s3_deprecated -def test_bucket_deletion(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - - key = Key(bucket) - key.key = "the-key" - key.set_contents_from_string("some value") - - # Try to delete a bucket that still has keys - conn.delete_bucket.when.called_with("foobar").should.throw(S3ResponseError) - - bucket.delete_key("the-key") - conn.delete_bucket("foobar") - - # Get non-existing bucket - conn.get_bucket.when.called_with("foobar").should.throw(S3ResponseError) - - # Delete non-existent bucket - conn.delete_bucket.when.called_with("foobar").should.throw(S3ResponseError) - - @mock_s3 def test_bucket_deletion_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -1282,17 +755,6 @@ def test_bucket_deletion_boto3(): ) -# Has boto3 equivalent -@mock_s3_deprecated -def test_get_all_buckets(): - conn = boto.connect_s3("the_key", "the_secret") - conn.create_bucket("foobar") - conn.create_bucket("foobar2") - buckets = conn.get_all_buckets() - - buckets.should.have.length_of(2) - - @mock_s3 def test_get_all_buckets_boto3(): client = boto3.client("s3", region_name=DEFAULT_REGION_NAME) @@ -1302,20 +764,6 @@ def test_get_all_buckets_boto3(): client.list_buckets()["Buckets"].should.have.length_of(2) -# Has boto3 equivalent -@mock_s3 -@mock_s3_deprecated -def test_post_to_bucket(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - - requests.post( - "https://foobar.s3.amazonaws.com/", {"key": "the-key", "file": "nothing"} - ) - - bucket.get_key("the-key").get_contents_as_string().should.equal(b"nothing") - - @mock_s3 def test_post_to_bucket_boto3(): if settings.TEST_SERVER_MODE: @@ -1333,21 +781,6 @@ def test_post_to_bucket_boto3(): resp["Body"].read().should.equal(b"nothing") -# Has boto3 equivalent -@mock_s3 -@mock_s3_deprecated -def test_post_with_metadata_to_bucket(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - - requests.post( - "https://foobar.s3.amazonaws.com/", - {"key": "the-key", "file": "nothing", "x-amz-meta-test": "metadata"}, - ) - - bucket.get_key("the-key").get_metadata("test").should.equal("metadata") - - @mock_s3 def test_post_with_metadata_to_bucket_boto3(): if settings.TEST_SERVER_MODE: @@ -1365,34 +798,6 @@ def test_post_with_metadata_to_bucket_boto3(): resp["Metadata"].should.equal({"test": "metadata"}) -@mock_s3_deprecated -def test_delete_missing_key(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - - deleted_key = bucket.delete_key("foobar") - deleted_key.key.should.equal("foobar") - - -# Has boto3 equivalent -@mock_s3_deprecated -def test_delete_keys(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - - Key(bucket=bucket, name="file1").set_contents_from_string("abc") - Key(bucket=bucket, name="file2").set_contents_from_string("abc") - Key(bucket=bucket, name="file3").set_contents_from_string("abc") - Key(bucket=bucket, name="file4").set_contents_from_string("abc") - - result = bucket.delete_keys(["file2", "file3"]) - result.deleted.should.have.length_of(2) - result.errors.should.have.length_of(0) - keys = bucket.get_all_keys() - keys.should.have.length_of(2) - keys[0].name.should.equal("file1") - - @mock_s3 def test_delete_versioned_objects(): s3 = boto3.client("s3", region_name=DEFAULT_REGION_NAME) @@ -1450,32 +855,6 @@ def test_delete_versioned_objects(): delete_markers.should.be.none -# Has boto3 equivalent -@mock_s3_deprecated -def test_delete_keys_invalid(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - - Key(bucket=bucket, name="file1").set_contents_from_string("abc") - Key(bucket=bucket, name="file2").set_contents_from_string("abc") - Key(bucket=bucket, name="file3").set_contents_from_string("abc") - Key(bucket=bucket, name="file4").set_contents_from_string("abc") - - # non-existing key case - result = bucket.delete_keys(["abc", "file3"]) - - result.deleted.should.have.length_of(2) - keys = bucket.get_all_keys() - keys.should.have.length_of(3) - keys[0].name.should.equal("file1") - - # empty keys - result = bucket.delete_keys([]) - - result.deleted.should.have.length_of(0) - result.errors.should.have.length_of(0) - - @mock_s3 def test_delete_missing_key_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -1513,56 +892,20 @@ def test_boto3_delete_empty_keys_list(): assert err.value.response["Error"]["Code"] == "MalformedXML" -# Has boto3 equivalent -@mock_s3_deprecated -def test_bucket_name_with_dot(): - conn = boto.connect_s3() - bucket = conn.create_bucket("firstname.lastname") - - k = Key(bucket, "somekey") - k.set_contents_from_string("somedata") - - +@pytest.mark.parametrize("name", ["firstname.lastname", "with-dash"]) @mock_s3 -def test_bucket_name_with_dot_boto3(): +def test_bucket_name_with_special_chars_boto3(name): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) client = boto3.client("s3", region_name=DEFAULT_REGION_NAME) - bucket = s3.Bucket("firstname.lastname") + bucket = s3.Bucket(name) bucket.create() - s3.Object("firstname.lastname", "the-key").put(Body=b"some value") + s3.Object(name, "the-key").put(Body=b"some value") - resp = client.get_object(Bucket="firstname.lastname", Key="the-key") + resp = client.get_object(Bucket=name, Key="the-key") resp["Body"].read().should.equal(b"some value") -# Has boto3 equivalent -@mock_s3_deprecated -def test_key_with_special_characters(): - conn = boto.connect_s3() - bucket = conn.create_bucket("test_bucket_name") - - key = Key(bucket, "test_list_keys_2/x?y") - key.set_contents_from_string("value1") - - key_list = bucket.list("test_list_keys_2/", "/") - keys = [x for x in key_list] - keys[0].name.should.equal("test_list_keys_2/x?y") - - -# Has boto3 equivalent -@mock_s3_deprecated -def test_unicode_key_with_slash(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = "/the-key-unîcode/test" - key.set_contents_from_string("value") - - key = bucket.get_key("/the-key-unîcode/test") - key.get_contents_as_string().should.equal(b"value") - - @pytest.mark.parametrize( "key", ["normal", "test_list_keys_2/x?y", "/the-key-unîcode/test"] ) @@ -1582,55 +925,6 @@ def test_key_with_special_characters_boto3(key): resp["Body"].read().should.equal(b"value") -# Has boto3 equivalent -@mock_s3_deprecated -def test_bucket_key_listing_order(): - conn = boto.connect_s3() - bucket = conn.create_bucket("test_bucket") - prefix = "toplevel/" - - def store(name): - k = Key(bucket, prefix + name) - k.set_contents_from_string("somedata") - - names = ["x/key", "y.key1", "y.key2", "y.key3", "x/y/key", "x/y/z/key"] - - for name in names: - store(name) - - delimiter = None - keys = [x.name for x in bucket.list(prefix, delimiter)] - keys.should.equal( - [ - "toplevel/x/key", - "toplevel/x/y/key", - "toplevel/x/y/z/key", - "toplevel/y.key1", - "toplevel/y.key2", - "toplevel/y.key3", - ] - ) - - delimiter = "/" - keys = [x.name for x in bucket.list(prefix, delimiter)] - keys.should.equal( - ["toplevel/y.key1", "toplevel/y.key2", "toplevel/y.key3", "toplevel/x/"] - ) - - # Test delimiter with no prefix - delimiter = "/" - keys = [x.name for x in bucket.list(prefix=None, delimiter=delimiter)] - keys.should.equal(["toplevel/"]) - - delimiter = None - keys = [x.name for x in bucket.list(prefix + "x", delimiter)] - keys.should.equal(["toplevel/x/key", "toplevel/x/y/key", "toplevel/x/y/z/key"]) - - delimiter = "/" - keys = [x.name for x in bucket.list(prefix + "x", delimiter)] - keys.should.equal(["toplevel/x/"]) - - @mock_s3 def test_bucket_key_listing_order_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -1673,19 +967,6 @@ def test_bucket_key_listing_order_boto3(): keys.should.equal([]) -# Has boto3 equivalent -@mock_s3_deprecated -def test_key_with_reduced_redundancy(): - conn = boto.connect_s3() - bucket = conn.create_bucket("test_bucket_name") - - key = Key(bucket, "test_rr_key") - key.set_contents_from_string("value1", reduced_redundancy=True) - # we use the bucket iterator because of: - # https:/github.com/boto/boto/issues/1173 - list(bucket)[0].storage_class.should.equal("REDUCED_REDUNDANCY") - - @mock_s3 def test_key_with_reduced_redundancy_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -1702,24 +983,6 @@ def test_key_with_reduced_redundancy_boto3(): [x.storage_class for x in bucket.objects.all()].should.equal(["REDUCED_REDUNDANCY"]) -# Has boto3 equivalent -@mock_s3_deprecated -def test_copy_key_reduced_redundancy(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = "the-key" - key.set_contents_from_string("some value") - - bucket.copy_key("new-key", "foobar", "the-key", storage_class="REDUCED_REDUNDANCY") - - # we use the bucket iterator because of: - # https:/github.com/boto/boto/issues/1173 - keys = dict([(k.name, k) for k in bucket]) - keys["new-key"].storage_class.should.equal("REDUCED_REDUNDANCY") - keys["the-key"].storage_class.should.equal("STANDARD") - - @mock_s3 def test_copy_key_reduced_redundancy_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -1741,29 +1004,6 @@ def test_copy_key_reduced_redundancy_boto3(): keys["the-key"].storage_class.should.equal("STANDARD") -# Has boto3 equivalent -@freeze_time("2012-01-01 12:00:00") -@mock_s3_deprecated -def test_restore_key(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = "the-key" - key.storage_class = "GLACIER" - key.set_contents_from_string("some value") - list(bucket)[0].ongoing_restore.should.be.none - key.restore(1) - key = bucket.get_key("the-key") - key.ongoing_restore.should_not.be.none - key.ongoing_restore.should.be.false - key.expiry_date.should.equal("Mon, 02 Jan 2012 12:00:00 GMT") - key.restore(2) - key = bucket.get_key("the-key") - key.ongoing_restore.should_not.be.none - key.ongoing_restore.should.be.false - key.expiry_date.should.equal("Tue, 03 Jan 2012 12:00:00 GMT") - - @freeze_time("2012-01-01 12:00:00") @mock_s3 def test_restore_key_boto3(): @@ -1809,40 +1049,6 @@ def test_cannot_restore_standard_class_object_boto3(): ) -@freeze_time("2012-01-01 12:00:00") -@mock_s3_deprecated -def test_restore_key_headers(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = "the-key" - key.storage_class = "GLACIER" - key.set_contents_from_string("some value") - key.restore(1, headers={"foo": "bar"}) - key = bucket.get_key("the-key") - key.ongoing_restore.should_not.be.none - key.ongoing_restore.should.be.false - key.expiry_date.should.equal("Mon, 02 Jan 2012 12:00:00 GMT") - - -# Has boto3 equivalent -@mock_s3_deprecated -def test_get_versioning_status(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - d = bucket.get_versioning_status() - d.should.be.empty - - bucket.configure_versioning(versioning=True) - d = bucket.get_versioning_status() - d.shouldnt.be.empty - d.should.have.key("Versioning").being.equal("Enabled") - - bucket.configure_versioning(versioning=False) - d = bucket.get_versioning_status() - d.should.have.key("Versioning").being.equal("Suspended") - - @mock_s3 def test_get_versioning_status_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -1859,28 +1065,6 @@ def test_get_versioning_status_boto3(): v.status.should.equal("Suspended") -# Has boto3 equivalent -@mock_s3_deprecated -def test_key_version(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - bucket.configure_versioning(versioning=True) - - versions = [] - - key = Key(bucket) - key.key = "the-key" - key.version_id.should.be.none - key.set_contents_from_string("some string") - versions.append(key.version_id) - key.set_contents_from_string("some string") - versions.append(key.version_id) - set(versions).should.have.length_of(2) - - key = bucket.get_key("the-key") - key.version_id.should.equal(versions[-1]) - - @mock_s3 def test_key_version_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -1901,43 +1085,6 @@ def test_key_version_boto3(): key["VersionId"].should.equal(versions[-1]) -# Has boto3 equivalent -@mock_s3_deprecated -def test_list_versions(): - conn = boto.connect_s3("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - bucket.configure_versioning(versioning=True) - - key_versions = [] - - key = Key(bucket, "the-key") - key.version_id.should.be.none - key.set_contents_from_string("Version 1") - key_versions.append(key.version_id) - key.set_contents_from_string("Version 2") - key_versions.append(key.version_id) - key_versions.should.have.length_of(2) - - versions = list(bucket.list_versions()) - versions.should.have.length_of(2) - - versions[0].name.should.equal("the-key") - versions[0].version_id.should.equal(key_versions[1]) - versions[0].get_contents_as_string().should.equal(b"Version 2") - - versions[1].name.should.equal("the-key") - versions[1].version_id.should.equal(key_versions[0]) - versions[1].get_contents_as_string().should.equal(b"Version 1") - - key = Key(bucket, "the2-key") - key.set_contents_from_string("Version 1") - - keys = list(bucket.list()) - keys.should.have.length_of(2) - versions = list(bucket.list_versions(prefix="the2-")) - versions.should.have.length_of(1) - - @mock_s3 def test_list_versions_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -1980,110 +1127,87 @@ def test_list_versions_boto3(): versions.should.have.length_of(1) -# Has boto3 equivalent -@mock_s3_deprecated -def test_acl_setting(): - conn = boto.connect_s3() - bucket = conn.create_bucket("foobar") +@mock_s3 +def test_acl_setting_boto3(): + s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) + client = boto3.client("s3", region_name=DEFAULT_REGION_NAME) + bucket = s3.Bucket("foobar") + bucket.create() + content = b"imafile" keyname = "test.txt" - - key = Key(bucket, name=keyname) - key.content_type = "text/plain" - key.set_contents_from_string(content) - key.make_public() - - key = bucket.get_key(keyname) - - assert key.get_contents_as_string() == content - - grants = key.get_acl().acl.grants - assert any( - g.uri == "http://acs.amazonaws.com/groups/global/AllUsers" - and g.permission == "READ" - for g in grants - ), grants - - -# Has boto3 equivalent -@mock_s3_deprecated -def test_acl_setting_via_headers(): - conn = boto.connect_s3() - bucket = conn.create_bucket("foobar") - content = b"imafile" - keyname = "test.txt" - - key = Key(bucket, name=keyname) - key.content_type = "text/plain" - key.set_contents_from_string( - content, - headers={ - "x-amz-grant-full-control": 'uri="http://acs.amazonaws.com/groups/global/AllUsers"' - }, + bucket.put_object( + Key=keyname, Body=content, ContentType="text/plain", ACL="public-read" ) - key = bucket.get_key(keyname) - - assert key.get_contents_as_string() == content - - grants = key.get_acl().acl.grants - assert any( - g.uri == "http://acs.amazonaws.com/groups/global/AllUsers" - and g.permission == "FULL_CONTROL" - for g in grants - ), grants + grants = client.get_object_acl(Bucket="foobar", Key=keyname)["Grants"] + grants.should.contain( + { + "Grantee": { + "Type": "Group", + "URI": "http://acs.amazonaws.com/groups/global/AllUsers", + }, + "Permission": "READ", + } + ) -# Has boto3 equivalent -@mock_s3_deprecated -def test_acl_switching(): - conn = boto.connect_s3() - bucket = conn.create_bucket("foobar") - content = b"imafile" +@mock_s3 +def test_acl_setting_via_headers_boto3(): + s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) + client = boto3.client("s3", region_name=DEFAULT_REGION_NAME) + bucket = s3.Bucket("foobar") + bucket.create() + keyname = "test.txt" - key = Key(bucket, name=keyname) - key.content_type = "text/plain" - key.set_contents_from_string(content, policy="public-read") - key.set_acl("private") + bucket.put_object(Key=keyname, Body=b"imafile") + client.put_object_acl(ACL="public-read", Bucket="foobar", Key=keyname) - grants = key.get_acl().acl.grants - assert not any( - g.uri == "http://acs.amazonaws.com/groups/global/AllUsers" - and g.permission == "READ" - for g in grants - ), grants + grants = client.get_object_acl(Bucket="foobar", Key=keyname)["Grants"] + grants.should.contain( + { + "Grantee": { + "Type": "Group", + "URI": "http://acs.amazonaws.com/groups/global/AllUsers", + }, + "Permission": "READ", + } + ) -@mock_s3_deprecated -def test_bucket_acl_setting(): - conn = boto.connect_s3() - bucket = conn.create_bucket("foobar") +@mock_s3 +def test_acl_switching_boto3(): + s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) + client = boto3.client("s3", region_name=DEFAULT_REGION_NAME) + bucket = s3.Bucket("foobar") + bucket.create() + keyname = "test.txt" - bucket.make_public() + bucket.put_object(Key=keyname, Body=b"asdf", ACL="public-read") + client.put_object_acl(ACL="private", Bucket="foobar", Key=keyname) - grants = bucket.get_acl().acl.grants - assert any( - g.uri == "http://acs.amazonaws.com/groups/global/AllUsers" - and g.permission == "READ" - for g in grants - ), grants + grants = client.get_object_acl(Bucket="foobar", Key=keyname)["Grants"] + grants.shouldnt.contain( + { + "Grantee": { + "Type": "Group", + "URI": "http://acs.amazonaws.com/groups/global/AllUsers", + }, + "Permission": "READ", + } + ) -@mock_s3_deprecated -def test_bucket_acl_switching(): - conn = boto.connect_s3() - bucket = conn.create_bucket("foobar") - bucket.make_public() +@mock_s3 +def test_acl_switching_nonexistent_key(): + s3 = boto3.client("s3", region_name=DEFAULT_REGION_NAME) + s3.create_bucket(Bucket="mybucket") - bucket.set_acl("private") + with pytest.raises(ClientError) as e: + s3.put_object_acl(Bucket="mybucket", Key="nonexistent", ACL="private") - grants = bucket.get_acl().acl.grants - assert not any( - g.uri == "http://acs.amazonaws.com/groups/global/AllUsers" - and g.permission == "READ" - for g in grants - ), grants + e.value.response["Error"]["Code"].should.equal("NoSuchKey") @mock_s3 @@ -2191,18 +1315,29 @@ def test_default_key_buffer_size(): os.environ["MOTO_S3_DEFAULT_KEY_BUFFER_SIZE"] = original_default_key_buffer_size -# Has boto3 equivalent -@mock_s3_deprecated -def test_unicode_key(): - conn = boto.connect_s3() - bucket = conn.create_bucket("mybucket") - key = Key(bucket) - key.key = "こんにちは.jpg" - key.set_contents_from_string("Hello world!") - assert [listed_key.key for listed_key in bucket.list()] == [key.key] - fetched_key = bucket.get_key(key.key) - assert fetched_key.key == key.key - assert fetched_key.get_contents_as_string().decode("utf-8") == "Hello world!" +@mock_s3 +def test_s3_object_in_private_bucket(): + s3 = boto3.resource("s3") + bucket = s3.Bucket("test-bucket") + bucket.create( + ACL="private", CreateBucketConfiguration={"LocationConstraint": "us-west-1"} + ) + bucket.put_object(ACL="private", Body=b"ABCD", Key="file.txt") + + s3_anonymous = boto3.resource("s3") + s3_anonymous.meta.client.meta.events.register("choose-signer.s3.*", disable_signing) + + with pytest.raises(ClientError) as exc: + s3_anonymous.Object(key="file.txt", bucket_name="test-bucket").get() + exc.value.response["Error"]["Code"].should.equal("403") + + bucket.put_object(ACL="public-read", Body=b"ABCD", Key="file.txt") + contents = ( + s3_anonymous.Object(key="file.txt", bucket_name="test-bucket") + .get()["Body"] + .read() + ) + contents.should.equal(b"ABCD") @mock_s3 @@ -2219,19 +1354,6 @@ def test_unicode_key_boto3(): fetched_key.get()["Body"].read().decode("utf-8").should.equal("Hello world!") -# Has boto3 equivalent -@mock_s3_deprecated -def test_unicode_value(): - conn = boto.connect_s3() - bucket = conn.create_bucket("mybucket") - key = Key(bucket) - key.key = "some_key" - key.set_contents_from_string("こんにちは.jpg") - list(bucket.list()) - key = bucket.get_key(key.key) - assert key.get_contents_as_string().decode("utf-8") == "こんにちは.jpg" - - @mock_s3 def test_unicode_value_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -2244,20 +1366,6 @@ def test_unicode_value_boto3(): key.get()["Body"].read().decode("utf-8").should.equal("こんにちは.jpg") -# Has boto3 equivalent -@mock_s3_deprecated -def test_setting_content_encoding(): - conn = boto.connect_s3() - bucket = conn.create_bucket("mybucket") - key = bucket.new_key("keyname") - key.set_metadata("Content-Encoding", "gzip") - compressed_data = "abcdef" - key.set_contents_from_string(compressed_data) - - key = bucket.get_key("keyname") - key.content_encoding.should.equal("gzip") - - @mock_s3 def test_setting_content_encoding_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -2270,13 +1378,6 @@ def test_setting_content_encoding_boto3(): key.content_encoding.should.equal("gzip") -@mock_s3_deprecated -def test_bucket_location(): - conn = boto.s3.connect_to_region("us-west-2") - bucket = conn.create_bucket("mybucket", location="us-west-2") - bucket.get_location().should.equal("us-west-2") - - @mock_s3 def test_bucket_location_default(): cli = boto3.client("s3", region_name=DEFAULT_REGION_NAME) @@ -2688,49 +1789,6 @@ if not settings.TEST_SERVER_MODE: ) -# Has boto3 equivalent -@mock_s3_deprecated -def test_ranged_get(): - conn = boto.connect_s3() - bucket = conn.create_bucket("mybucket") - key = Key(bucket) - key.key = "bigkey" - rep = b"0123456789" - key.set_contents_from_string(rep * 10) - - # Implicitly bounded range requests. - key.get_contents_as_string(headers={"Range": "bytes=0-"}).should.equal(rep * 10) - key.get_contents_as_string(headers={"Range": "bytes=50-"}).should.equal(rep * 5) - key.get_contents_as_string(headers={"Range": "bytes=99-"}).should.equal(b"9") - - # Explicitly bounded range requests starting from the first byte. - key.get_contents_as_string(headers={"Range": "bytes=0-0"}).should.equal(b"0") - key.get_contents_as_string(headers={"Range": "bytes=0-49"}).should.equal(rep * 5) - key.get_contents_as_string(headers={"Range": "bytes=0-99"}).should.equal(rep * 10) - key.get_contents_as_string(headers={"Range": "bytes=0-100"}).should.equal(rep * 10) - key.get_contents_as_string(headers={"Range": "bytes=0-700"}).should.equal(rep * 10) - - # Explicitly bounded range requests starting from the / a middle byte. - key.get_contents_as_string(headers={"Range": "bytes=50-54"}).should.equal(rep[:5]) - key.get_contents_as_string(headers={"Range": "bytes=50-99"}).should.equal(rep * 5) - key.get_contents_as_string(headers={"Range": "bytes=50-100"}).should.equal(rep * 5) - key.get_contents_as_string(headers={"Range": "bytes=50-700"}).should.equal(rep * 5) - - # Explicitly bounded range requests starting from the last byte. - key.get_contents_as_string(headers={"Range": "bytes=99-99"}).should.equal(b"9") - key.get_contents_as_string(headers={"Range": "bytes=99-100"}).should.equal(b"9") - key.get_contents_as_string(headers={"Range": "bytes=99-700"}).should.equal(b"9") - - # Suffix range requests. - key.get_contents_as_string(headers={"Range": "bytes=-1"}).should.equal(b"9") - key.get_contents_as_string(headers={"Range": "bytes=-60"}).should.equal(rep * 6) - key.get_contents_as_string(headers={"Range": "bytes=-100"}).should.equal(rep * 10) - key.get_contents_as_string(headers={"Range": "bytes=-101"}).should.equal(rep * 10) - key.get_contents_as_string(headers={"Range": "bytes=-700"}).should.equal(rep * 10) - - key.size.should.equal(100) - - @mock_s3 def test_ranged_get_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -2772,61 +1830,6 @@ def test_ranged_get_boto3(): key.content_length.should.equal(100) -# Has boto3 equivalent -@mock_s3_deprecated -def test_policy(): - conn = boto.connect_s3() - bucket_name = "mybucket" - bucket = conn.create_bucket(bucket_name) - - policy = json.dumps( - { - "Version": "2012-10-17", - "Id": "PutObjPolicy", - "Statement": [ - { - "Sid": "DenyUnEncryptedObjectUploads", - "Effect": "Deny", - "Principal": "*", - "Action": "s3:PutObject", - "Resource": "arn:aws:s3:::{bucket_name}/*".format( - bucket_name=bucket_name - ), - "Condition": { - "StringNotEquals": { - "s3:x-amz-server-side-encryption": "aws:kms" - } - }, - } - ], - } - ) - - with pytest.raises(S3ResponseError) as err: - bucket.get_policy() - - ex = err.value - ex.box_usage.should.be.none - ex.error_code.should.equal("NoSuchBucketPolicy") - ex.message.should.equal("The bucket policy does not exist") - ex.reason.should.equal("Not Found") - ex.resource.should.be.none - ex.status.should.equal(404) - ex.body.should.contain(bucket_name) - ex.request_id.should_not.be.none - - bucket.set_policy(policy).should.be.true - - bucket = conn.get_bucket(bucket_name) - - bucket.get_policy().decode("utf-8").should.equal(policy) - - bucket.delete_policy() - - with pytest.raises(S3ResponseError) as err: - bucket.get_policy() - - @mock_s3 def test_policy_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -2876,15 +1879,6 @@ def test_policy_boto3(): ex.value.response["Error"]["Code"].should.equal("NoSuchBucketPolicy") -# Has boto3 equivalent -@mock_s3_deprecated -def test_website_configuration_xml(): - conn = boto.connect_s3() - bucket = conn.create_bucket("test-bucket") - bucket.set_website_configuration_xml(TEST_XML) - bucket.get_website_configuration_xml().should.equal(TEST_XML) - - @mock_s3 def test_website_configuration_xml_boto3(): s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME) @@ -2917,23 +1911,6 @@ def test_website_configuration_xml_boto3(): c.shouldnt.have.key("ErrorDocument") -@mock_s3_deprecated -def test_key_with_trailing_slash_in_ordinary_calling_format(): - conn = boto.connect_s3( - "access_key", - "secret_key", - calling_format=boto.s3.connection.OrdinaryCallingFormat(), - ) - bucket = conn.create_bucket("test_bucket_name") - - key_name = "key_with_slash/" - - key = Key(bucket, key_name) - key.set_contents_from_string("some value") - - [k.name for k in bucket.get_all_keys()].should.contain(key_name) - - @mock_s3 def test_boto3_key_etag(): s3 = boto3.client("s3", region_name=DEFAULT_REGION_NAME) diff --git a/tests/test_s3/test_s3_lifecycle.py b/tests/test_s3/test_s3_lifecycle.py index 84d12801f..76df2b405 100644 --- a/tests/test_s3/test_s3_lifecycle.py +++ b/tests/test_s3/test_s3_lifecycle.py @@ -1,31 +1,11 @@ -import boto import boto3 -from boto.exception import S3ResponseError -from boto.s3.lifecycle import Lifecycle, Transition, Expiration, Rule -import sure # pylint: disable=unused-import +import sure # noqa # pylint: disable=unused-import from botocore.exceptions import ClientError from datetime import datetime import pytest -from moto import mock_s3_deprecated, mock_s3 - - -@mock_s3_deprecated -def test_lifecycle_create(): - conn = boto.s3.connect_to_region("us-west-1") - bucket = conn.create_bucket("foobar", location="us-west-1") - - lifecycle = Lifecycle() - lifecycle.add_rule("myid", "", "Enabled", 30) - bucket.configure_lifecycle(lifecycle) - response = bucket.get_lifecycle_config() - len(response).should.equal(1) - lifecycle = response[0] - lifecycle.id.should.equal("myid") - lifecycle.prefix.should.equal("") - lifecycle.status.should.equal("Enabled") - list(lifecycle.transition).should.equal([]) +from moto import mock_s3 @mock_s3 @@ -437,26 +417,6 @@ def test_lifecycle_with_aimu(): # TODO: Add test for failures due to missing children -# Has boto3 equivalent -@mock_s3_deprecated -def test_lifecycle_with_glacier_transition(): - conn = boto.s3.connect_to_region("us-west-1") - bucket = conn.create_bucket("foobar", location="us-west-1") - - lifecycle = Lifecycle() - transition = Transition(days=30, storage_class="GLACIER") - rule = Rule( - "myid", prefix="", status="Enabled", expiration=None, transition=transition - ) - lifecycle.append(rule) - bucket.configure_lifecycle(lifecycle) - response = bucket.get_lifecycle_config() - transition = response[0].transition - transition.days.should.equal(30) - transition.storage_class.should.equal("GLACIER") - transition.date.should.equal(None) - - @mock_s3 def test_lifecycle_with_glacier_transition_boto3(): s3 = boto3.resource("s3", region_name="us-east-1") @@ -488,49 +448,6 @@ def test_lifecycle_with_glacier_transition_boto3(): transition.shouldnt.have.key("Date") -# Has boto3 equivalent -@mock_s3_deprecated -def test_lifecycle_multi(): - conn = boto.s3.connect_to_region("us-west-1") - bucket = conn.create_bucket("foobar", location="us-west-1") - - date = "2022-10-12T00:00:00.000Z" - sc = "GLACIER" - lifecycle = Lifecycle() - lifecycle.add_rule("1", "1/", "Enabled", 1) - lifecycle.add_rule("2", "2/", "Enabled", Expiration(days=2)) - lifecycle.add_rule("3", "3/", "Enabled", Expiration(date=date)) - lifecycle.add_rule("4", "4/", "Enabled", None, Transition(days=4, storage_class=sc)) - lifecycle.add_rule( - "5", "5/", "Enabled", None, Transition(date=date, storage_class=sc) - ) - - bucket.configure_lifecycle(lifecycle) - # read the lifecycle back - rules = bucket.get_lifecycle_config() - - for rule in rules: - if rule.id == "1": - rule.prefix.should.equal("1/") - rule.expiration.days.should.equal(1) - elif rule.id == "2": - rule.prefix.should.equal("2/") - rule.expiration.days.should.equal(2) - elif rule.id == "3": - rule.prefix.should.equal("3/") - rule.expiration.date.should.equal(date) - elif rule.id == "4": - rule.prefix.should.equal("4/") - rule.transition.days.should.equal(4) - rule.transition.storage_class.should.equal(sc) - elif rule.id == "5": - rule.prefix.should.equal("5/") - rule.transition.date.should.equal(date) - rule.transition.storage_class.should.equal(sc) - else: - assert False, "Invalid rule id" - - @mock_s3 def test_lifecycle_multi_boto3(): s3 = boto3.resource("s3", region_name="us-east-1") @@ -604,22 +521,6 @@ def test_lifecycle_multi_boto3(): assert False, "Invalid rule id" -# Has boto3 equivalent -@mock_s3_deprecated -def test_lifecycle_delete(): - conn = boto.s3.connect_to_region("us-west-1") - bucket = conn.create_bucket("foobar", location="us-west-1") - - lifecycle = Lifecycle() - lifecycle.add_rule(expiration=30) - bucket.configure_lifecycle(lifecycle) - response = bucket.get_lifecycle_config() - response.should.have.length_of(1) - - bucket.delete_lifecycle_configuration() - bucket.get_lifecycle_config.when.called_with().should.throw(S3ResponseError) - - @mock_s3 def test_lifecycle_delete_boto3(): s3 = boto3.resource("s3", region_name="us-east-1") diff --git a/tests/test_s3/test_s3_lock.py b/tests/test_s3/test_s3_lock.py index a73bef866..935cfa1e0 100644 --- a/tests/test_s3/test_s3_lock.py +++ b/tests/test_s3/test_s3_lock.py @@ -5,7 +5,7 @@ import botocore from moto import mock_s3 from botocore.config import Config from moto.s3.responses import DEFAULT_REGION_NAME -import sure # pylint: disable=unused-import +import sure # noqa # pylint: disable=unused-import @mock_s3 diff --git a/tests/test_s3bucket_path/test_s3bucket_path.py b/tests/test_s3bucket_path/test_s3bucket_path.py deleted file mode 100644 index 836c4f0c8..000000000 --- a/tests/test_s3bucket_path/test_s3bucket_path.py +++ /dev/null @@ -1,321 +0,0 @@ -from urllib.request import urlopen -from urllib.error import HTTPError - -import boto -from boto.exception import S3ResponseError -from boto.s3.key import Key -from boto.s3.connection import OrdinaryCallingFormat - -from freezegun import freeze_time -import requests - -import sure # noqa # pylint: disable=unused-import - -from moto import mock_s3, mock_s3_deprecated - - -def create_connection(key=None, secret=None): - return boto.connect_s3(key, secret, calling_format=OrdinaryCallingFormat()) - - -class MyModel(object): - def __init__(self, name, value): - self.name = name - self.value = value - - def save(self): - conn = create_connection("the_key", "the_secret") - bucket = conn.get_bucket("mybucket") - k = Key(bucket) - k.key = self.name - k.set_contents_from_string(self.value) - - -@mock_s3_deprecated -def test_my_model_save(): - # Create Bucket so that test can run - conn = create_connection("the_key", "the_secret") - conn.create_bucket("mybucket") - #################################### - - model_instance = MyModel("steve", "is awesome") - model_instance.save() - - conn.get_bucket("mybucket").get_key("steve").get_contents_as_string().should.equal( - b"is awesome" - ) - - -@mock_s3_deprecated -def test_missing_key(): - conn = create_connection("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - bucket.get_key("the-key").should.equal(None) - - -@mock_s3_deprecated -def test_missing_key_urllib2(): - conn = create_connection("the_key", "the_secret") - conn.create_bucket("foobar") - - urlopen.when.called_with("http://s3.amazonaws.com/foobar/the-key").should.throw( - HTTPError - ) - - -@mock_s3_deprecated -def test_empty_key(): - conn = create_connection("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = "the-key" - key.set_contents_from_string("") - - bucket.get_key("the-key").get_contents_as_string().should.equal(b"") - - -@mock_s3_deprecated -def test_empty_key_set_on_existing_key(): - conn = create_connection("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = "the-key" - key.set_contents_from_string("foobar") - - bucket.get_key("the-key").get_contents_as_string().should.equal(b"foobar") - - key.set_contents_from_string("") - bucket.get_key("the-key").get_contents_as_string().should.equal(b"") - - -@mock_s3_deprecated -def test_large_key_save(): - conn = create_connection("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = "the-key" - key.set_contents_from_string("foobar" * 100000) - - bucket.get_key("the-key").get_contents_as_string().should.equal(b"foobar" * 100000) - - -@mock_s3_deprecated -def test_copy_key(): - conn = create_connection("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = "the-key" - key.set_contents_from_string("some value") - - bucket.copy_key("new-key", "foobar", "the-key") - - bucket.get_key("the-key").get_contents_as_string().should.equal(b"some value") - bucket.get_key("new-key").get_contents_as_string().should.equal(b"some value") - - -@mock_s3_deprecated -def test_set_metadata(): - conn = create_connection("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = "the-key" - key.set_metadata("md", "Metadatastring") - key.set_contents_from_string("Testval") - - bucket.get_key("the-key").get_metadata("md").should.equal("Metadatastring") - - -@freeze_time("2012-01-01 12:00:00") -@mock_s3_deprecated -def test_last_modified(): - # See https://github.com/boto/boto/issues/466 - conn = create_connection() - bucket = conn.create_bucket("foobar") - key = Key(bucket) - key.key = "the-key" - key.set_contents_from_string("some value") - - rs = bucket.get_all_keys() - rs[0].last_modified.should.equal("2012-01-01T12:00:00.000Z") - - bucket.get_key("the-key").last_modified.should.equal( - "Sun, 01 Jan 2012 12:00:00 GMT" - ) - - -@mock_s3_deprecated -def test_missing_bucket(): - conn = create_connection("the_key", "the_secret") - conn.get_bucket.when.called_with("mybucket").should.throw(S3ResponseError) - - -@mock_s3_deprecated -def test_bucket_with_dash(): - conn = create_connection("the_key", "the_secret") - conn.get_bucket.when.called_with("mybucket-test").should.throw(S3ResponseError) - - -@mock_s3_deprecated -def test_bucket_deletion(): - conn = create_connection("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - - key = Key(bucket) - key.key = "the-key" - key.set_contents_from_string("some value") - - # Try to delete a bucket that still has keys - conn.delete_bucket.when.called_with("foobar").should.throw(S3ResponseError) - - bucket.delete_key("the-key") - conn.delete_bucket("foobar") - - # Get non-existing bucket - conn.get_bucket.when.called_with("foobar").should.throw(S3ResponseError) - - # Delete non-existent bucket - conn.delete_bucket.when.called_with("foobar").should.throw(S3ResponseError) - - -@mock_s3_deprecated -def test_get_all_buckets(): - conn = create_connection("the_key", "the_secret") - conn.create_bucket("foobar") - conn.create_bucket("foobar2") - buckets = conn.get_all_buckets() - - buckets.should.have.length_of(2) - - -@mock_s3 -@mock_s3_deprecated -def test_post_to_bucket(): - conn = create_connection("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - - requests.post( - "https://s3.amazonaws.com/foobar", {"key": "the-key", "file": "nothing"} - ) - - bucket.get_key("the-key").get_contents_as_string().should.equal(b"nothing") - - -@mock_s3 -@mock_s3_deprecated -def test_post_with_metadata_to_bucket(): - conn = create_connection("the_key", "the_secret") - bucket = conn.create_bucket("foobar") - - requests.post( - "https://s3.amazonaws.com/foobar", - {"key": "the-key", "file": "nothing", "x-amz-meta-test": "metadata"}, - ) - - bucket.get_key("the-key").get_metadata("test").should.equal("metadata") - - -@mock_s3_deprecated -def test_bucket_name_with_dot(): - conn = create_connection() - bucket = conn.create_bucket("firstname.lastname") - - k = Key(bucket, "somekey") - k.set_contents_from_string("somedata") - - -@mock_s3_deprecated -def test_key_with_special_characters(): - conn = create_connection() - bucket = conn.create_bucket("test_bucket_name") - - key = Key(bucket, "test_list_keys_2/*x+?^@~!y") - key.set_contents_from_string("value1") - - key_list = bucket.list("test_list_keys_2/", "/") - keys = [x for x in key_list] - keys[0].name.should.equal("test_list_keys_2/*x+?^@~!y") - - -@mock_s3_deprecated -def test_bucket_key_listing_order(): - conn = create_connection() - bucket = conn.create_bucket("test_bucket") - prefix = "toplevel/" - - def store(name): - k = Key(bucket, prefix + name) - k.set_contents_from_string("somedata") - - names = ["x/key", "y.key1", "y.key2", "y.key3", "x/y/key", "x/y/z/key"] - - for name in names: - store(name) - - delimiter = None - keys = [x.name for x in bucket.list(prefix, delimiter)] - keys.should.equal( - [ - "toplevel/x/key", - "toplevel/x/y/key", - "toplevel/x/y/z/key", - "toplevel/y.key1", - "toplevel/y.key2", - "toplevel/y.key3", - ] - ) - - delimiter = "/" - keys = [x.name for x in bucket.list(prefix, delimiter)] - keys.should.equal( - ["toplevel/y.key1", "toplevel/y.key2", "toplevel/y.key3", "toplevel/x/"] - ) - - # Test delimiter with no prefix - delimiter = "/" - keys = [x.name for x in bucket.list(prefix=None, delimiter=delimiter)] - keys.should.equal(["toplevel/"]) - - delimiter = None - keys = [x.name for x in bucket.list(prefix + "x", delimiter)] - keys.should.equal(["toplevel/x/key", "toplevel/x/y/key", "toplevel/x/y/z/key"]) - - delimiter = "/" - keys = [x.name for x in bucket.list(prefix + "x", delimiter)] - keys.should.equal(["toplevel/x/"]) - - -@mock_s3_deprecated -def test_delete_keys(): - conn = create_connection() - bucket = conn.create_bucket("foobar") - - Key(bucket=bucket, name="file1").set_contents_from_string("abc") - Key(bucket=bucket, name="file2").set_contents_from_string("abc") - Key(bucket=bucket, name="file3").set_contents_from_string("abc") - Key(bucket=bucket, name="file4").set_contents_from_string("abc") - - result = bucket.delete_keys(["file2", "file3"]) - result.deleted.should.have.length_of(2) - result.errors.should.have.length_of(0) - keys = bucket.get_all_keys() - keys.should.have.length_of(2) - keys[0].name.should.equal("file1") - - -@mock_s3_deprecated -def test_delete_keys_with_invalid(): - conn = create_connection() - bucket = conn.create_bucket("foobar") - - Key(bucket=bucket, name="file1").set_contents_from_string("abc") - Key(bucket=bucket, name="file2").set_contents_from_string("abc") - Key(bucket=bucket, name="file3").set_contents_from_string("abc") - Key(bucket=bucket, name="file4").set_contents_from_string("abc") - - result = bucket.delete_keys(["abc", "file3"]) - - result.deleted.should.have.length_of(2) - result.errors.should.have.length_of(0) - keys = bucket.get_all_keys() - keys.should.have.length_of(3) - keys[0].name.should.equal("file1") diff --git a/tests/test_s3bucket_path/test_s3bucket_path_combo.py b/tests/test_s3bucket_path/test_s3bucket_path_combo.py deleted file mode 100644 index 522cea5d8..000000000 --- a/tests/test_s3bucket_path/test_s3bucket_path_combo.py +++ /dev/null @@ -1,23 +0,0 @@ -import boto -from boto.s3.connection import OrdinaryCallingFormat - -from moto import mock_s3_deprecated - - -def create_connection(key=None, secret=None): - return boto.connect_s3(key, secret, calling_format=OrdinaryCallingFormat()) - - -def test_bucketpath_combo_serial(): - @mock_s3_deprecated - def make_bucket_path(): - conn = create_connection() - conn.create_bucket("mybucketpath") - - @mock_s3_deprecated - def make_bucket(): - conn = boto.connect_s3("the_key", "the_secret") - conn.create_bucket("mybucket") - - make_bucket() - make_bucket_path() diff --git a/tests/test_ses/test_ses.py b/tests/test_ses/test_ses.py deleted file mode 100644 index 3ab581c7f..000000000 --- a/tests/test_ses/test_ses.py +++ /dev/null @@ -1,185 +0,0 @@ -import email - -import boto -from boto.exception import BotoServerError - -import sure # noqa # pylint: disable=unused-import - -from moto import mock_ses_deprecated - - -# Has boto3 equivalent -@mock_ses_deprecated -def test_verify_email_identity(): - conn = boto.connect_ses("the_key", "the_secret") - conn.verify_email_identity("test@example.com") - - identities = conn.list_identities() - address = identities["ListIdentitiesResponse"]["ListIdentitiesResult"][ - "Identities" - ][0] - address.should.equal("test@example.com") - - -# Has boto3 equivalent -@mock_ses_deprecated -def test_domain_verify(): - conn = boto.connect_ses("the_key", "the_secret") - - conn.verify_domain_dkim("domain1.com") - conn.verify_domain_identity("domain2.com") - - identities = conn.list_identities() - domains = list( - identities["ListIdentitiesResponse"]["ListIdentitiesResult"]["Identities"] - ) - domains.should.equal(["domain1.com", "domain2.com"]) - - -# Has boto3 equivalent -@mock_ses_deprecated -def test_delete_identity(): - conn = boto.connect_ses("the_key", "the_secret") - conn.verify_email_identity("test@example.com") - - conn.list_identities()["ListIdentitiesResponse"]["ListIdentitiesResult"][ - "Identities" - ].should.have.length_of(1) - conn.delete_identity("test@example.com") - conn.list_identities()["ListIdentitiesResponse"]["ListIdentitiesResult"][ - "Identities" - ].should.have.length_of(0) - - -# Has boto3 equivalent -@mock_ses_deprecated -def test_send_email(): - conn = boto.connect_ses("the_key", "the_secret") - - conn.send_email.when.called_with( - "test@example.com", "test subject", "test body", "test_to@example.com" - ).should.throw(BotoServerError) - - conn.verify_email_identity("test@example.com") - conn.send_email( - "test@example.com", "test subject", "test body", "test_to@example.com" - ) - - send_quota = conn.get_send_quota() - sent_count = int( - send_quota["GetSendQuotaResponse"]["GetSendQuotaResult"]["SentLast24Hours"] - ) - sent_count.should.equal(1) - - -# Has boto3 equivalent -@mock_ses_deprecated -def test_send_html_email(): - conn = boto.connect_ses("the_key", "the_secret") - - conn.send_email.when.called_with( - "test@example.com", - "test subject", - "test body", - "test_to@example.com", - format="html", - ).should.throw(BotoServerError) - - conn.verify_email_identity("test@example.com") - conn.send_email( - "test@example.com", - "test subject", - "test body", - "test_to@example.com", - format="html", - ) - - send_quota = conn.get_send_quota() - sent_count = int( - send_quota["GetSendQuotaResponse"]["GetSendQuotaResult"]["SentLast24Hours"] - ) - sent_count.should.equal(1) - - -# Has boto3 equivalent -@mock_ses_deprecated -def test_send_raw_email(): - conn = boto.connect_ses("the_key", "the_secret") - - message = email.mime.multipart.MIMEMultipart() - message["Subject"] = "Test" - message["From"] = "test@example.com" - message["To"] = "to@example.com" - - # Message body - part = email.mime.text.MIMEText("test file attached") - message.attach(part) - - # Attachment - part = email.mime.text.MIMEText("contents of test file here") - part.add_header("Content-Disposition", "attachment; filename=test.txt") - message.attach(part) - - conn.send_raw_email.when.called_with( - source=message["From"], raw_message=message.as_string() - ).should.throw(BotoServerError) - - conn.verify_email_identity("test@example.com") - conn.send_raw_email(source=message["From"], raw_message=message.as_string()) - - send_quota = conn.get_send_quota() - sent_count = int( - send_quota["GetSendQuotaResponse"]["GetSendQuotaResult"]["SentLast24Hours"] - ) - sent_count.should.equal(1) - - -# Has boto3 equivalent -@mock_ses_deprecated -def test_get_send_statistics(): - conn = boto.connect_ses("the_key", "the_secret") - - conn.send_email.when.called_with( - "test@example.com", - "test subject", - "test body", - "test_to@example.com", - format="html", - ).should.throw(BotoServerError) - - # tests to verify rejects in get_send_statistics - result = conn.get_send_statistics() - - reject_count = int( - result["GetSendStatisticsResponse"]["GetSendStatisticsResult"][ - "SendDataPoints" - ][0]["Rejects"] - ) - delivery_count = int( - result["GetSendStatisticsResponse"]["GetSendStatisticsResult"][ - "SendDataPoints" - ][0]["DeliveryAttempts"] - ) - reject_count.should.equal(1) - delivery_count.should.equal(0) - - conn.verify_email_identity("test@example.com") - conn.send_email( - "test@example.com", "test subject", "test body", "test_to@example.com" - ) - - # tests to delivery attempts in get_send_statistics - result = conn.get_send_statistics() - - reject_count = int( - result["GetSendStatisticsResponse"]["GetSendStatisticsResult"][ - "SendDataPoints" - ][0]["Rejects"] - ) - delivery_count = int( - result["GetSendStatisticsResponse"]["GetSendStatisticsResult"][ - "SendDataPoints" - ][0]["DeliveryAttempts"] - ) - reject_count.should.equal(1) - delivery_count.should.equal(1) diff --git a/tests/test_sns/test_application.py b/tests/test_sns/test_application.py deleted file mode 100644 index 9c9a9251b..000000000 --- a/tests/test_sns/test_application.py +++ /dev/null @@ -1,319 +0,0 @@ -import boto -from boto.exception import BotoServerError -from moto import mock_sns_deprecated -from moto.core import ACCOUNT_ID -import sure # noqa # pylint: disable=unused-import - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_create_platform_application(): - conn = boto.connect_sns() - platform_application = conn.create_platform_application( - name="my-application", - platform="APNS", - attributes={ - "PlatformCredential": "platform_credential", - "PlatformPrincipal": "platform_principal", - }, - ) - application_arn = platform_application["CreatePlatformApplicationResponse"][ - "CreatePlatformApplicationResult" - ]["PlatformApplicationArn"] - application_arn.should.equal( - "arn:aws:sns:us-east-1:{}:app/APNS/my-application".format(ACCOUNT_ID) - ) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_get_platform_application_attributes(): - conn = boto.connect_sns() - platform_application = conn.create_platform_application( - name="my-application", - platform="APNS", - attributes={ - "PlatformCredential": "platform_credential", - "PlatformPrincipal": "platform_principal", - }, - ) - arn = platform_application["CreatePlatformApplicationResponse"][ - "CreatePlatformApplicationResult" - ]["PlatformApplicationArn"] - attributes = conn.get_platform_application_attributes(arn)[ - "GetPlatformApplicationAttributesResponse" - ]["GetPlatformApplicationAttributesResult"]["Attributes"] - attributes.should.equal( - { - "PlatformCredential": "platform_credential", - "PlatformPrincipal": "platform_principal", - } - ) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_get_missing_platform_application_attributes(): - conn = boto.connect_sns() - conn.get_platform_application_attributes.when.called_with( - "a-fake-arn" - ).should.throw(BotoServerError) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_set_platform_application_attributes(): - conn = boto.connect_sns() - platform_application = conn.create_platform_application( - name="my-application", - platform="APNS", - attributes={ - "PlatformCredential": "platform_credential", - "PlatformPrincipal": "platform_principal", - }, - ) - arn = platform_application["CreatePlatformApplicationResponse"][ - "CreatePlatformApplicationResult" - ]["PlatformApplicationArn"] - conn.set_platform_application_attributes(arn, {"PlatformPrincipal": "other"}) - attributes = conn.get_platform_application_attributes(arn)[ - "GetPlatformApplicationAttributesResponse" - ]["GetPlatformApplicationAttributesResult"]["Attributes"] - attributes.should.equal( - {"PlatformCredential": "platform_credential", "PlatformPrincipal": "other"} - ) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_list_platform_applications(): - conn = boto.connect_sns() - conn.create_platform_application(name="application1", platform="APNS") - conn.create_platform_application(name="application2", platform="APNS") - - applications_response = conn.list_platform_applications() - applications = applications_response["ListPlatformApplicationsResponse"][ - "ListPlatformApplicationsResult" - ]["PlatformApplications"] - applications.should.have.length_of(2) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_delete_platform_application(): - conn = boto.connect_sns() - conn.create_platform_application(name="application1", platform="APNS") - conn.create_platform_application(name="application2", platform="APNS") - - applications_response = conn.list_platform_applications() - applications = applications_response["ListPlatformApplicationsResponse"][ - "ListPlatformApplicationsResult" - ]["PlatformApplications"] - applications.should.have.length_of(2) - - application_arn = applications[0]["PlatformApplicationArn"] - conn.delete_platform_application(application_arn) - - applications_response = conn.list_platform_applications() - applications = applications_response["ListPlatformApplicationsResponse"][ - "ListPlatformApplicationsResult" - ]["PlatformApplications"] - applications.should.have.length_of(1) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_create_platform_endpoint(): - conn = boto.connect_sns() - platform_application = conn.create_platform_application( - name="my-application", platform="APNS" - ) - application_arn = platform_application["CreatePlatformApplicationResponse"][ - "CreatePlatformApplicationResult" - ]["PlatformApplicationArn"] - - endpoint = conn.create_platform_endpoint( - platform_application_arn=application_arn, - token="some_unique_id", - custom_user_data="some user data", - attributes={"Enabled": False}, - ) - - endpoint_arn = endpoint["CreatePlatformEndpointResponse"][ - "CreatePlatformEndpointResult" - ]["EndpointArn"] - endpoint_arn.should.contain( - "arn:aws:sns:us-east-1:{}:endpoint/APNS/my-application/".format(ACCOUNT_ID) - ) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_get_list_endpoints_by_platform_application(): - conn = boto.connect_sns() - platform_application = conn.create_platform_application( - name="my-application", platform="APNS" - ) - application_arn = platform_application["CreatePlatformApplicationResponse"][ - "CreatePlatformApplicationResult" - ]["PlatformApplicationArn"] - - endpoint = conn.create_platform_endpoint( - platform_application_arn=application_arn, - token="some_unique_id", - custom_user_data="some user data", - attributes={"CustomUserData": "some data"}, - ) - endpoint_arn = endpoint["CreatePlatformEndpointResponse"][ - "CreatePlatformEndpointResult" - ]["EndpointArn"] - - endpoint_list = conn.list_endpoints_by_platform_application( - platform_application_arn=application_arn - )["ListEndpointsByPlatformApplicationResponse"][ - "ListEndpointsByPlatformApplicationResult" - ][ - "Endpoints" - ] - - endpoint_list.should.have.length_of(1) - endpoint_list[0]["Attributes"]["CustomUserData"].should.equal("some data") - endpoint_list[0]["EndpointArn"].should.equal(endpoint_arn) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_get_endpoint_attributes(): - conn = boto.connect_sns() - platform_application = conn.create_platform_application( - name="my-application", platform="APNS" - ) - application_arn = platform_application["CreatePlatformApplicationResponse"][ - "CreatePlatformApplicationResult" - ]["PlatformApplicationArn"] - - endpoint = conn.create_platform_endpoint( - platform_application_arn=application_arn, - token="some_unique_id", - custom_user_data="some user data", - attributes={"CustomUserData": "some data"}, - ) - endpoint_arn = endpoint["CreatePlatformEndpointResponse"][ - "CreatePlatformEndpointResult" - ]["EndpointArn"] - - attributes = conn.get_endpoint_attributes(endpoint_arn)[ - "GetEndpointAttributesResponse" - ]["GetEndpointAttributesResult"]["Attributes"] - attributes.should.equal( - {"Token": "some_unique_id", "Enabled": "true", "CustomUserData": "some data"} - ) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_get_missing_endpoint_attributes(): - conn = boto.connect_sns() - conn.get_endpoint_attributes.when.called_with("a-fake-arn").should.throw( - BotoServerError - ) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_set_endpoint_attributes(): - conn = boto.connect_sns() - platform_application = conn.create_platform_application( - name="my-application", platform="APNS" - ) - application_arn = platform_application["CreatePlatformApplicationResponse"][ - "CreatePlatformApplicationResult" - ]["PlatformApplicationArn"] - - endpoint = conn.create_platform_endpoint( - platform_application_arn=application_arn, - token="some_unique_id", - custom_user_data="some user data", - attributes={"Enabled": False, "CustomUserData": "some data"}, - ) - endpoint_arn = endpoint["CreatePlatformEndpointResponse"][ - "CreatePlatformEndpointResult" - ]["EndpointArn"] - - conn.set_endpoint_attributes(endpoint_arn, {"CustomUserData": "other data"}) - attributes = conn.get_endpoint_attributes(endpoint_arn)[ - "GetEndpointAttributesResponse" - ]["GetEndpointAttributesResult"]["Attributes"] - attributes.should.equal( - {"Token": "some_unique_id", "Enabled": "false", "CustomUserData": "other data"} - ) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_delete_endpoint(): - conn = boto.connect_sns() - platform_application = conn.create_platform_application( - name="my-application", platform="APNS" - ) - application_arn = platform_application["CreatePlatformApplicationResponse"][ - "CreatePlatformApplicationResult" - ]["PlatformApplicationArn"] - - endpoint = conn.create_platform_endpoint( - platform_application_arn=application_arn, - token="some_unique_id", - custom_user_data="some user data", - attributes={"Enabled": False, "CustomUserData": "some data"}, - ) - endpoint_arn = endpoint["CreatePlatformEndpointResponse"][ - "CreatePlatformEndpointResult" - ]["EndpointArn"] - - endpoint_list = conn.list_endpoints_by_platform_application( - platform_application_arn=application_arn - )["ListEndpointsByPlatformApplicationResponse"][ - "ListEndpointsByPlatformApplicationResult" - ][ - "Endpoints" - ] - - endpoint_list.should.have.length_of(1) - - conn.delete_endpoint(endpoint_arn) - - endpoint_list = conn.list_endpoints_by_platform_application( - platform_application_arn=application_arn - )["ListEndpointsByPlatformApplicationResponse"][ - "ListEndpointsByPlatformApplicationResult" - ][ - "Endpoints" - ] - endpoint_list.should.have.length_of(0) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_publish_to_platform_endpoint(): - conn = boto.connect_sns() - platform_application = conn.create_platform_application( - name="my-application", platform="APNS" - ) - application_arn = platform_application["CreatePlatformApplicationResponse"][ - "CreatePlatformApplicationResult" - ]["PlatformApplicationArn"] - - endpoint = conn.create_platform_endpoint( - platform_application_arn=application_arn, - token="some_unique_id", - custom_user_data="some user data", - attributes={"Enabled": True}, - ) - - endpoint_arn = endpoint["CreatePlatformEndpointResponse"][ - "CreatePlatformEndpointResult" - ]["EndpointArn"] - - conn.publish( - message="some message", message_structure="json", target_arn=endpoint_arn - ) diff --git a/tests/test_sns/test_publishing.py b/tests/test_sns/test_publishing.py deleted file mode 100644 index 98aea11ac..000000000 --- a/tests/test_sns/test_publishing.py +++ /dev/null @@ -1,106 +0,0 @@ -import boto -import re -from freezegun import freeze_time -import sure # noqa # pylint: disable=unused-import - -from moto import mock_sns_deprecated, mock_sqs_deprecated -from moto.core import ACCOUNT_ID - -MESSAGE_FROM_SQS_TEMPLATE = ( - '{\n "Message": "%s",\n "MessageId": "%s",\n "Signature": "EXAMPLElDMXvB8r9R83tGoNn0ecwd5UjllzsvSvbItzfaMpN2nk5HVSw7XnOn/49IkxDKz8YrlH2qJXj2iZB0Zo2O71c4qQk1fMUDi3LGpij7RCW7AW9vYYsSqIKRnFS94ilu7NFhUzLiieYr4BKHpdTmdD6c0esKEYBpabxDSc=",\n "SignatureVersion": "1",\n "SigningCertURL": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem",\n "Subject": "%s",\n "Timestamp": "2015-01-01T12:00:00.000Z",\n "TopicArn": "arn:aws:sns:%s:' - + ACCOUNT_ID - + ':some-topic",\n "Type": "Notification",\n "UnsubscribeURL": "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:' - + ACCOUNT_ID - + ':some-topic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55"\n}' -) - - -# Has boto3 equivalent -@mock_sqs_deprecated -@mock_sns_deprecated -def test_publish_to_sqs(): - conn = boto.connect_sns() - conn.create_topic("some-topic") - topics_json = conn.get_all_topics() - topic_arn = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"][0][ - "TopicArn" - ] - - sqs_conn = boto.connect_sqs() - sqs_conn.create_queue("test-queue") - - conn.subscribe( - topic_arn, "sqs", "arn:aws:sqs:us-east-1:{}:test-queue".format(ACCOUNT_ID) - ) - - message_to_publish = "my message" - subject_to_publish = "test subject" - with freeze_time("2015-01-01 12:00:00"): - published_message = conn.publish( - topic=topic_arn, message=message_to_publish, subject=subject_to_publish - ) - published_message_id = published_message["PublishResponse"]["PublishResult"][ - "MessageId" - ] - - queue = sqs_conn.get_queue("test-queue") - with freeze_time("2015-01-01 12:00:01"): - message = queue.read(1) - expected = MESSAGE_FROM_SQS_TEMPLATE % ( - message_to_publish, - published_message_id, - subject_to_publish, - "us-east-1", - ) - acquired_message = re.sub( - r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z", - "2015-01-01T12:00:00.000Z", - message.get_body(), - ) - acquired_message.should.equal(expected) - - -# Has boto3 equivalent -@mock_sqs_deprecated -@mock_sns_deprecated -def test_publish_to_sqs_in_different_region(): - conn = boto.sns.connect_to_region("us-west-1") - conn.create_topic("some-topic") - topics_json = conn.get_all_topics() - topic_arn = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"][0][ - "TopicArn" - ] - - sqs_conn = boto.sqs.connect_to_region("us-west-2") - sqs_conn.create_queue("test-queue") - - conn.subscribe( - topic_arn, "sqs", "arn:aws:sqs:us-west-2:{}:test-queue".format(ACCOUNT_ID) - ) - - message_to_publish = "my message" - subject_to_publish = "test subject" - with freeze_time("2015-01-01 12:00:00"): - published_message = conn.publish( - topic=topic_arn, message=message_to_publish, subject=subject_to_publish - ) - published_message_id = published_message["PublishResponse"]["PublishResult"][ - "MessageId" - ] - - queue = sqs_conn.get_queue("test-queue") - with freeze_time("2015-01-01 12:00:01"): - message = queue.read(1) - expected = MESSAGE_FROM_SQS_TEMPLATE % ( - message_to_publish, - published_message_id, - subject_to_publish, - "us-west-1", - ) - - acquired_message = re.sub( - r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z", - "2015-01-01T12:00:00.000Z", - message.get_body(), - ) - acquired_message.should.equal(expected) diff --git a/tests/test_sns/test_subscriptions.py b/tests/test_sns/test_subscriptions.py deleted file mode 100644 index e6631df40..000000000 --- a/tests/test_sns/test_subscriptions.py +++ /dev/null @@ -1,164 +0,0 @@ -import boto - -import sure # noqa # pylint: disable=unused-import - -from moto import mock_sns_deprecated -from moto.sns.models import DEFAULT_PAGE_SIZE - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_creating_subscription(): - conn = boto.connect_sns() - conn.create_topic("some-topic") - topics_json = conn.get_all_topics() - topic_arn = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"][0][ - "TopicArn" - ] - - conn.subscribe(topic_arn, "http", "http://example.com/") - - subscriptions = conn.get_all_subscriptions()["ListSubscriptionsResponse"][ - "ListSubscriptionsResult" - ]["Subscriptions"] - subscriptions.should.have.length_of(1) - subscription = subscriptions[0] - subscription["TopicArn"].should.equal(topic_arn) - subscription["Protocol"].should.equal("http") - subscription["SubscriptionArn"].should.contain(topic_arn) - subscription["Endpoint"].should.equal("http://example.com/") - - # Now unsubscribe the subscription - conn.unsubscribe(subscription["SubscriptionArn"]) - - # And there should be zero subscriptions left - subscriptions = conn.get_all_subscriptions()["ListSubscriptionsResponse"][ - "ListSubscriptionsResult" - ]["Subscriptions"] - subscriptions.should.have.length_of(0) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_deleting_subscriptions_by_deleting_topic(): - conn = boto.connect_sns() - conn.create_topic("some-topic") - topics_json = conn.get_all_topics() - topic_arn = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"][0][ - "TopicArn" - ] - - conn.subscribe(topic_arn, "http", "http://example.com/") - - subscriptions = conn.get_all_subscriptions()["ListSubscriptionsResponse"][ - "ListSubscriptionsResult" - ]["Subscriptions"] - subscriptions.should.have.length_of(1) - subscription = subscriptions[0] - subscription_arn = subscription["SubscriptionArn"] - subscription["TopicArn"].should.equal(topic_arn) - subscription["Protocol"].should.equal("http") - subscription_arn.should.contain(topic_arn) - subscription["Endpoint"].should.equal("http://example.com/") - - # Now delete the topic - conn.delete_topic(topic_arn) - - # And there should now be 0 topics - topics_json = conn.get_all_topics() - topics = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"] - topics.should.have.length_of(0) - - # And the subscription should still be left - subscriptions = conn.get_all_subscriptions()["ListSubscriptionsResponse"][ - "ListSubscriptionsResult" - ]["Subscriptions"] - subscriptions.should.have.length_of(0) - - # Now delete hanging subscription - conn.unsubscribe(subscription_arn) - - subscriptions = conn.get_all_subscriptions()["ListSubscriptionsResponse"][ - "ListSubscriptionsResult" - ]["Subscriptions"] - subscriptions.should.have.length_of(0) - - # Deleting it again should not result in any error - conn.unsubscribe(subscription_arn) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_getting_subscriptions_by_topic(): - conn = boto.connect_sns() - conn.create_topic("topic1") - conn.create_topic("topic2") - - topics_json = conn.get_all_topics() - topics = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"] - topic1_arn = topics[0]["TopicArn"] - topic2_arn = topics[1]["TopicArn"] - - conn.subscribe(topic1_arn, "http", "http://example1.com/") - conn.subscribe(topic2_arn, "http", "http://example2.com/") - - topic1_subscriptions = conn.get_all_subscriptions_by_topic(topic1_arn)[ - "ListSubscriptionsByTopicResponse" - ]["ListSubscriptionsByTopicResult"]["Subscriptions"] - topic1_subscriptions.should.have.length_of(1) - topic1_subscriptions[0]["Endpoint"].should.equal("http://example1.com/") - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_subscription_paging(): - conn = boto.connect_sns() - conn.create_topic("topic1") - conn.create_topic("topic2") - - topics_json = conn.get_all_topics() - topics = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"] - topic1_arn = topics[0]["TopicArn"] - topic2_arn = topics[1]["TopicArn"] - - for index in range(DEFAULT_PAGE_SIZE + int(DEFAULT_PAGE_SIZE / 3)): - conn.subscribe(topic1_arn, "email", "email_" + str(index) + "@test.com") - conn.subscribe(topic2_arn, "email", "email_" + str(index) + "@test.com") - - all_subscriptions = conn.get_all_subscriptions() - all_subscriptions["ListSubscriptionsResponse"]["ListSubscriptionsResult"][ - "Subscriptions" - ].should.have.length_of(DEFAULT_PAGE_SIZE) - next_token = all_subscriptions["ListSubscriptionsResponse"][ - "ListSubscriptionsResult" - ]["NextToken"] - next_token.should.equal(DEFAULT_PAGE_SIZE) - - all_subscriptions = conn.get_all_subscriptions(next_token=next_token * 2) - all_subscriptions["ListSubscriptionsResponse"]["ListSubscriptionsResult"][ - "Subscriptions" - ].should.have.length_of(int(DEFAULT_PAGE_SIZE * 2 / 3)) - next_token = all_subscriptions["ListSubscriptionsResponse"][ - "ListSubscriptionsResult" - ]["NextToken"] - next_token.should.equal(None) - - topic1_subscriptions = conn.get_all_subscriptions_by_topic(topic1_arn) - topic1_subscriptions["ListSubscriptionsByTopicResponse"][ - "ListSubscriptionsByTopicResult" - ]["Subscriptions"].should.have.length_of(DEFAULT_PAGE_SIZE) - next_token = topic1_subscriptions["ListSubscriptionsByTopicResponse"][ - "ListSubscriptionsByTopicResult" - ]["NextToken"] - next_token.should.equal(DEFAULT_PAGE_SIZE) - - topic1_subscriptions = conn.get_all_subscriptions_by_topic( - topic1_arn, next_token=next_token - ) - topic1_subscriptions["ListSubscriptionsByTopicResponse"][ - "ListSubscriptionsByTopicResult" - ]["Subscriptions"].should.have.length_of(int(DEFAULT_PAGE_SIZE / 3)) - next_token = topic1_subscriptions["ListSubscriptionsByTopicResponse"][ - "ListSubscriptionsByTopicResult" - ]["NextToken"] - next_token.should.equal(None) diff --git a/tests/test_sns/test_topics.py b/tests/test_sns/test_topics.py deleted file mode 100644 index 67181739f..000000000 --- a/tests/test_sns/test_topics.py +++ /dev/null @@ -1,190 +0,0 @@ -import boto -import json - -import sure # noqa # pylint: disable=unused-import - -from boto.exception import BotoServerError -from moto import mock_sns_deprecated -from moto.sns.models import DEFAULT_EFFECTIVE_DELIVERY_POLICY, DEFAULT_PAGE_SIZE -from moto.core import ACCOUNT_ID - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_create_and_delete_topic(): - conn = boto.connect_sns() - conn.create_topic("some-topic") - - topics_json = conn.get_all_topics() - topics = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"] - topics.should.have.length_of(1) - topics[0]["TopicArn"].should.equal( - "arn:aws:sns:{0}:{1}:some-topic".format(conn.region.name, ACCOUNT_ID) - ) - - # Delete the topic - conn.delete_topic(topics[0]["TopicArn"]) - - # And there should now be 0 topics - topics_json = conn.get_all_topics() - topics = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"] - topics.should.have.length_of(0) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_delete_non_existent_topic(): - conn = boto.connect_sns() - conn.delete_topic.when.called_with("a-fake-arn").should.throw(BotoServerError) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_get_missing_topic(): - conn = boto.connect_sns() - conn.get_topic_attributes.when.called_with("a-fake-arn").should.throw( - BotoServerError - ) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_create_topic_in_multiple_regions(): - for region in ["us-west-1", "us-west-2"]: - conn = boto.sns.connect_to_region(region) - conn.create_topic("some-topic") - list( - conn.get_all_topics()["ListTopicsResponse"]["ListTopicsResult"]["Topics"] - ).should.have.length_of(1) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_topic_corresponds_to_region(): - for region in ["us-east-1", "us-west-2"]: - conn = boto.sns.connect_to_region(region) - conn.create_topic("some-topic") - topics_json = conn.get_all_topics() - topic_arn = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"][0][ - "TopicArn" - ] - topic_arn.should.equal( - "arn:aws:sns:{0}:{1}:some-topic".format(region, ACCOUNT_ID) - ) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_topic_attributes(): - conn = boto.connect_sns() - conn.create_topic("some-topic") - - topics_json = conn.get_all_topics() - topic_arn = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"][0][ - "TopicArn" - ] - - attributes = conn.get_topic_attributes(topic_arn)["GetTopicAttributesResponse"][ - "GetTopicAttributesResult" - ]["Attributes"] - attributes["TopicArn"].should.equal( - "arn:aws:sns:{0}:{1}:some-topic".format(conn.region.name, ACCOUNT_ID) - ) - attributes["Owner"].should.equal(ACCOUNT_ID) - json.loads(attributes["Policy"]).should.equal( - { - "Version": "2008-10-17", - "Id": "__default_policy_ID", - "Statement": [ - { - "Effect": "Allow", - "Sid": "__default_statement_ID", - "Principal": {"AWS": "*"}, - "Action": [ - "SNS:GetTopicAttributes", - "SNS:SetTopicAttributes", - "SNS:AddPermission", - "SNS:RemovePermission", - "SNS:DeleteTopic", - "SNS:Subscribe", - "SNS:ListSubscriptionsByTopic", - "SNS:Publish", - "SNS:Receive", - ], - "Resource": "arn:aws:sns:us-east-1:{}:some-topic".format( - ACCOUNT_ID - ), - "Condition": {"StringEquals": {"AWS:SourceOwner": ACCOUNT_ID}}, - } - ], - } - ) - attributes["DisplayName"].should.equal("") - attributes["SubscriptionsPending"].should.equal(0) - attributes["SubscriptionsConfirmed"].should.equal(0) - attributes["SubscriptionsDeleted"].should.equal(0) - attributes["DeliveryPolicy"].should.equal("") - json.loads(attributes["EffectiveDeliveryPolicy"]).should.equal( - DEFAULT_EFFECTIVE_DELIVERY_POLICY - ) - - policy = json.dumps({"foo": "bar"}) - displayname = "My display name" - delivery = {"http": {"defaultHealthyRetryPolicy": {"numRetries": 5}}} - conn.set_topic_attributes(topic_arn, "Policy", policy) - conn.set_topic_attributes(topic_arn, "DisplayName", displayname) - conn.set_topic_attributes(topic_arn, "DeliveryPolicy", delivery) - - attributes = conn.get_topic_attributes(topic_arn)["GetTopicAttributesResponse"][ - "GetTopicAttributesResult" - ]["Attributes"] - attributes["Policy"].should.equal('{"foo": "bar"}') - attributes["DisplayName"].should.equal("My display name") - attributes["DeliveryPolicy"].should.equal( - "{'http': {'defaultHealthyRetryPolicy': {'numRetries': 5}}}" - ) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_topic_paging(): - conn = boto.connect_sns() - for index in range(DEFAULT_PAGE_SIZE + int(DEFAULT_PAGE_SIZE / 2)): - conn.create_topic("some-topic_" + str(index)) - - topics_json = conn.get_all_topics() - topics_list = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"] - next_token = topics_json["ListTopicsResponse"]["ListTopicsResult"]["NextToken"] - - len(topics_list).should.equal(DEFAULT_PAGE_SIZE) - next_token.should.equal(DEFAULT_PAGE_SIZE) - - topics_json = conn.get_all_topics(next_token=next_token) - topics_list = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"] - next_token = topics_json["ListTopicsResponse"]["ListTopicsResult"]["NextToken"] - - topics_list.should.have.length_of(int(DEFAULT_PAGE_SIZE / 2)) - next_token.should.equal(None) - - -# Has boto3 equivalent -@mock_sns_deprecated -def test_topic_kms_master_key_id_attribute(): - conn = boto.connect_sns() - - conn.create_topic("test-sns-no-key-attr") - topics_json = conn.get_all_topics() - topic_arn = topics_json["ListTopicsResponse"]["ListTopicsResult"]["Topics"][0][ - "TopicArn" - ] - attributes = conn.get_topic_attributes(topic_arn)["GetTopicAttributesResponse"][ - "GetTopicAttributesResult" - ]["Attributes"] - attributes.should_not.have.key("KmsMasterKeyId") - - conn.set_topic_attributes(topic_arn, "KmsMasterKeyId", "test-key") - attributes = conn.get_topic_attributes(topic_arn)["GetTopicAttributesResponse"][ - "GetTopicAttributesResult" - ]["Attributes"] - attributes.should.have.key("KmsMasterKeyId") - attributes["KmsMasterKeyId"].should.equal("test-key") diff --git a/tests/test_sqs/test_integration.py b/tests/test_sqs/test_integration.py index d93570052..f9e6da573 100644 --- a/tests/test_sqs/test_integration.py +++ b/tests/test_sqs/test_integration.py @@ -1,7 +1,6 @@ import json import time import uuid - import boto3 import sure # noqa # pylint: disable=unused-import from moto import mock_sqs, mock_lambda, mock_logs diff --git a/tests/test_sqs/test_sqs.py b/tests/test_sqs/test_sqs.py index 7cb8d0f60..9ab134062 100644 --- a/tests/test_sqs/test_sqs.py +++ b/tests/test_sqs/test_sqs.py @@ -1,23 +1,18 @@ -import base64 import json import time import uuid import hashlib -import boto import boto3 import botocore.exceptions import sure # noqa # pylint: disable=unused-import -from boto.exception import SQSError -from boto.sqs.message import Message, RawMessage from botocore.exceptions import ClientError from freezegun import freeze_time -from moto import mock_sqs, mock_sqs_deprecated, settings +from moto import mock_sqs, settings from unittest import SkipTest, mock import pytest -from tests.helpers import requires_boto_gte from moto.core import ACCOUNT_ID from moto.sqs.models import ( Queue, @@ -1410,22 +1405,6 @@ def test_receive_messages_with_wait_seconds_timeout_of_zero(): messages.should.equal([]) -# Has boto3 equivalent -@mock_sqs_deprecated -def test_send_message_with_xml_characters(): - conn = boto.connect_sqs("the_key", "the_secret") - queue = conn.create_queue("test-queue", visibility_timeout=3) - queue.set_message_class(RawMessage) - - body_one = "< & >" - - queue.write(queue.new_message(body_one)) - - messages = conn.receive_message(queue, number_messages=1) - - messages[0].get_body().should.equal(body_one) - - @mock_sqs def test_send_message_with_xml_characters_boto3(): sqs = boto3.resource("sqs", region_name="us-east-1") @@ -1441,69 +1420,6 @@ def test_send_message_with_xml_characters_boto3(): messages[0]["Body"].should.equal(body_one) -# Has boto3 equivalent -@requires_boto_gte("2.28") -@mock_sqs_deprecated -def test_send_message_with_attributes(): - conn = boto.connect_sqs("the_key", "the_secret") - queue = conn.create_queue("test-queue", visibility_timeout=3) - queue.set_message_class(RawMessage) - - body = "this is a test message" - message = queue.new_message(body) - BASE64_BINARY = base64.b64encode(b"binary value").decode("utf-8") - message_attributes = { - "test.attribute_name": { - "data_type": "String", - "string_value": "attribute value", - }, - "test.binary_attribute": {"data_type": "Binary", "binary_value": BASE64_BINARY}, - "test.number_attribute": { - "data_type": "Number", - "string_value": "string value", - }, - } - message.message_attributes = message_attributes - - queue.write(message) - - messages = conn.receive_message( - queue, - message_attributes=[ - "test.attribute_name", - "test.binary_attribute", - "test.number_attribute", - ], - ) - - messages[0].get_body().should.equal(body) - - for name, value in message_attributes.items(): - dict(messages[0].message_attributes[name]).should.equal(value) - - -# Has boto3 equivalent -@mock_sqs_deprecated -def test_send_message_with_delay(): - conn = boto.connect_sqs("the_key", "the_secret") - queue = conn.create_queue("test-queue", visibility_timeout=3) - queue.set_message_class(RawMessage) - - body_one = "this is a test message" - body_two = "this is another test message" - - queue.write(queue.new_message(body_one), delay_seconds=3) - queue.write(queue.new_message(body_two)) - - queue.count().should.equal(1) - - messages = conn.receive_message(queue, number_messages=2) - assert len(messages) == 1 - message = messages[0] - assert message.get_body().should.equal(body_two) - queue.count().should.equal(0) - - @mock_sqs def test_send_message_with_delay_boto3(): sqs = boto3.resource("sqs", region_name="us-east-1") @@ -1524,19 +1440,6 @@ def test_send_message_with_delay_boto3(): messages.should.have.length_of(0) -# Has boto3 equivalent -@mock_sqs_deprecated -def test_send_large_message_fails(): - conn = boto.connect_sqs("the_key", "the_secret") - queue = conn.create_queue("test-queue", visibility_timeout=3) - queue.set_message_class(RawMessage) - - body_one = "test message" * 200000 - huge_message = queue.new_message(body_one) - - queue.write.when.called_with(huge_message).should.throw(SQSError) - - @mock_sqs def test_send_large_message_fails_boto3(): sqs = boto3.resource("sqs", region_name="us-east-1") @@ -1552,28 +1455,6 @@ def test_send_large_message_fails_boto3(): ) -# Has boto3 equivalent -@mock_sqs_deprecated -def test_message_becomes_inflight_when_received(): - conn = boto.connect_sqs("the_key", "the_secret") - queue = conn.create_queue("test-queue", visibility_timeout=2) - queue.set_message_class(RawMessage) - - body_one = "this is a test message" - queue.write(queue.new_message(body_one)) - queue.count().should.equal(1) - - messages = conn.receive_message(queue, number_messages=1) - queue.count().should.equal(0) - - assert len(messages) == 1 - - # Wait - time.sleep(3) - - queue.count().should.equal(1) - - @mock_sqs def test_message_becomes_inflight_when_received_boto3(): sqs = boto3.resource("sqs", region_name="eu-west-1") @@ -1602,25 +1483,6 @@ def test_message_becomes_inflight_when_received_boto3(): queue.attributes["ApproximateNumberOfMessages"].should.equal("1") -# Has boto3 equivalent -@mock_sqs_deprecated -def test_receive_message_with_explicit_visibility_timeout(): - conn = boto.connect_sqs("the_key", "the_secret") - queue = conn.create_queue("test-queue", visibility_timeout=3) - queue.set_message_class(RawMessage) - - body_one = "this is another test message" - queue.write(queue.new_message(body_one)) - - queue.count().should.equal(1) - messages = conn.receive_message(queue, number_messages=1, visibility_timeout=0) - - assert len(messages) == 1 - - # Message should remain visible - queue.count().should.equal(1) - - @mock_sqs def test_receive_message_with_explicit_visibility_timeout_boto3(): sqs = boto3.resource("sqs", region_name="us-east-1") @@ -1643,41 +1505,6 @@ def test_receive_message_with_explicit_visibility_timeout_boto3(): queue.attributes["ApproximateNumberOfMessages"].should.equal("1") -# Has boto3 equivalent -@mock_sqs_deprecated -def test_change_message_visibility(): - conn = boto.connect_sqs("the_key", "the_secret") - queue = conn.create_queue("test-queue", visibility_timeout=2) - queue.set_message_class(RawMessage) - - body_one = "this is another test message" - queue.write(queue.new_message(body_one)) - - queue.count().should.equal(1) - messages = conn.receive_message(queue, number_messages=1) - - assert len(messages) == 1 - - queue.count().should.equal(0) - - messages[0].change_visibility(2) - - # Wait - time.sleep(1) - - # Message is not visible - queue.count().should.equal(0) - - time.sleep(2) - - # Message now becomes visible - queue.count().should.equal(1) - - messages = conn.receive_message(queue, number_messages=1) - messages[0].delete() - queue.count().should.equal(0) - - @mock_sqs def test_change_message_visibility_boto3(): sqs = boto3.resource("sqs", region_name="us-east-1") @@ -1735,56 +1562,6 @@ def test_change_message_visibility_on_unknown_receipt_handle(): err["Message"].should.equal("The input receipt handle is invalid.") -# Has boto3 equivalent -@mock_sqs_deprecated -def test_message_attributes(): - conn = boto.connect_sqs("the_key", "the_secret") - queue = conn.create_queue("test-queue", visibility_timeout=2) - queue.set_message_class(RawMessage) - - body_one = "this is another test message" - queue.write(queue.new_message(body_one)) - - queue.count().should.equal(1) - - messages = conn.receive_message(queue, number_messages=1, attributes=["All"]) - queue.count().should.equal(0) - - assert len(messages) == 1 - - message_attributes = messages[0].attributes - - assert message_attributes.get("ApproximateFirstReceiveTimestamp") - assert int(message_attributes.get("ApproximateReceiveCount")) == 1 - assert message_attributes.get("SentTimestamp") - assert message_attributes.get("SenderId") - - -# Has boto3 equivalent -@mock_sqs_deprecated -def test_read_message_from_queue(): - conn = boto.connect_sqs() - queue = conn.create_queue("testqueue") - queue.set_message_class(RawMessage) - - body = "foo bar baz" - queue.write(queue.new_message(body)) - message = queue.read(1) - message.get_body().should.equal(body) - - -# Has boto3 equivalent -@mock_sqs_deprecated -def test_queue_length(): - conn = boto.connect_sqs("the_key", "the_secret") - queue = conn.create_queue("test-queue", visibility_timeout=3) - queue.set_message_class(RawMessage) - - queue.write(queue.new_message("this is a test message")) - queue.write(queue.new_message("this is another test message")) - queue.count().should.equal(2) - - @mock_sqs def test_queue_length_boto3(): sqs = boto3.resource("sqs", region_name="us-east-1") @@ -1799,96 +1576,6 @@ def test_queue_length_boto3(): queue.attributes["ApproximateNumberOfMessages"].should.equal("2") -# Has boto3 equivalent -@mock_sqs_deprecated -def test_delete_message(): - conn = boto.connect_sqs("the_key", "the_secret") - queue = conn.create_queue("test-queue", visibility_timeout=3) - queue.set_message_class(RawMessage) - - queue.write(queue.new_message("this is a test message")) - queue.write(queue.new_message("this is another test message")) - queue.count().should.equal(2) - - messages = conn.receive_message(queue, number_messages=1) - assert len(messages) == 1 - messages[0].delete() - queue.count().should.equal(1) - - messages = conn.receive_message(queue, number_messages=1) - assert len(messages) == 1 - messages[0].delete() - queue.count().should.equal(0) - - -# Has boto3 equivalent -@mock_sqs_deprecated -def test_send_batch_operation(): - conn = boto.connect_sqs("the_key", "the_secret") - queue = conn.create_queue("test-queue", visibility_timeout=3) - - # See https://github.com/boto/boto/issues/831 - queue.set_message_class(RawMessage) - - queue.write_batch( - [ - ("my_first_message", "test message 1", 0), - ("my_second_message", "test message 2", 0), - ("my_third_message", "test message 3", 0), - ] - ) - - messages = queue.get_messages(3) - messages[0].get_body().should.equal("test message 1") - - # Test that pulling more messages doesn't break anything - messages = queue.get_messages(2) - - -# Has boto3 equivalent -@requires_boto_gte("2.28") -@mock_sqs_deprecated -def test_send_batch_operation_with_message_attributes(): - conn = boto.connect_sqs("the_key", "the_secret") - queue = conn.create_queue("test-queue", visibility_timeout=3) - queue.set_message_class(RawMessage) - - message_tuple = ( - "my_first_message", - "test message 1", - 0, - {"name1": {"data_type": "String", "string_value": "foo"}}, - ) - queue.write_batch([message_tuple]) - - messages = queue.get_messages(message_attributes=["name1"]) - messages[0].get_body().should.equal("test message 1") - - for name, value in message_tuple[3].items(): - dict(messages[0].message_attributes[name]).should.equal(value) - - -# Has boto3 equivalent -@mock_sqs_deprecated -def test_delete_batch_operation(): - conn = boto.connect_sqs("the_key", "the_secret") - queue = conn.create_queue("test-queue", visibility_timeout=3) - - conn.send_message_batch( - queue, - [ - ("my_first_message", "test message 1", 0), - ("my_second_message", "test message 2", 0), - ("my_third_message", "test message 3", 0), - ], - ) - - messages = queue.get_messages(2) - queue.delete_message_batch(messages) - - queue.count().should.equal(1) - - @mock_sqs def test_delete_batch_operation_boto3(): sqs = boto3.resource("sqs", region_name="us-east-1") @@ -1911,40 +1598,6 @@ def test_delete_batch_operation_boto3(): queue.attributes["ApproximateNumberOfMessages"].should.equal("1") -# Has boto3 equivalent -@mock_sqs_deprecated -def test_queue_attributes(): - conn = boto.connect_sqs("the_key", "the_secret") - - queue_name = "test-queue" - visibility_timeout = 3 - - queue = conn.create_queue(queue_name, visibility_timeout=visibility_timeout) - - attributes = queue.get_attributes() - - attributes["QueueArn"].should.look_like( - "arn:aws:sqs:us-east-1:{AccountId}:{name}".format( - AccountId=ACCOUNT_ID, name=queue_name - ) - ) - - attributes["VisibilityTimeout"].should.look_like(str(visibility_timeout)) - - attribute_names = queue.get_attributes().keys() - attribute_names.should.contain("ApproximateNumberOfMessagesNotVisible") - attribute_names.should.contain("MessageRetentionPeriod") - attribute_names.should.contain("ApproximateNumberOfMessagesDelayed") - attribute_names.should.contain("MaximumMessageSize") - attribute_names.should.contain("CreatedTimestamp") - attribute_names.should.contain("ApproximateNumberOfMessages") - attribute_names.should.contain("ReceiveMessageWaitTimeSeconds") - attribute_names.should.contain("DelaySeconds") - attribute_names.should.contain("VisibilityTimeout") - attribute_names.should.contain("LastModifiedTimestamp") - attribute_names.should.contain("QueueArn") - - @mock_sqs def test_change_message_visibility_on_old_message_boto3(): sqs = boto3.resource("sqs", region_name="us-east-1") @@ -2011,20 +1664,6 @@ def test_change_message_visibility_on_visible_message_boto3(): queue.attributes["ApproximateNumberOfMessages"].should.equal("0") -# Has boto3 equivalent -@mock_sqs_deprecated -def test_purge_action(): - conn = boto.sqs.connect_to_region("us-east-1") - - queue = conn.create_queue("new-queue") - queue.write(queue.new_message("this is another test message")) - queue.count().should.equal(1) - - queue.purge() - - queue.count().should.equal(0) - - @mock_sqs def test_purge_queue_before_delete_message(): client = boto3.client("sqs", region_name="us-east-1") @@ -2057,28 +1696,6 @@ def test_purge_queue_before_delete_message(): receive_resp2["Messages"][0]["Body"].should.equal("second_message") -# Has boto3 equivalent -@mock_sqs_deprecated -def test_delete_message_after_visibility_timeout(): - VISIBILITY_TIMEOUT = 1 - conn = boto.sqs.connect_to_region("us-east-1") - new_queue = conn.create_queue("new-queue", visibility_timeout=VISIBILITY_TIMEOUT) - - m1 = Message() - m1.set_body("Message 1!") - new_queue.write(m1) - - assert new_queue.count() == 1 - - m1_retrieved = new_queue.read() - - time.sleep(VISIBILITY_TIMEOUT + 1) - - m1_retrieved.delete() - - assert new_queue.count() == 0 - - @mock_sqs def test_delete_message_after_visibility_timeout_boto3(): VISIBILITY_TIMEOUT = 1 diff --git a/tests/test_sts/test_sts.py b/tests/test_sts/test_sts.py index b5d5286be..b516121de 100644 --- a/tests/test_sts/test_sts.py +++ b/tests/test_sts/test_sts.py @@ -1,7 +1,6 @@ from base64 import b64encode import json -import boto import boto3 from botocore.client import ClientError from datetime import datetime @@ -9,26 +8,11 @@ from freezegun import freeze_time import pytest import sure # noqa # pylint: disable=unused-import -from moto import mock_sts, mock_sts_deprecated, mock_iam, settings +from moto import mock_sts, mock_iam, settings from moto.core import ACCOUNT_ID from moto.sts.responses import MAX_FEDERATION_TOKEN_POLICY_LENGTH -# Has boto3 equivalent -@freeze_time("2012-01-01 12:00:00") -@mock_sts_deprecated -def test_get_session_token(): - conn = boto.connect_sts() - token = conn.get_session_token(duration=123) - - token.expiration.should.equal("2012-01-01T12:02:03.000Z") - token.session_token.should.equal( - "AQoEXAMPLEH4aoAH0gNCAPyJxz4BlCFFxWNE1OPTgk5TthT+FvwqnKwRcOIfrRh3c/LTo6UDdyJwOOvEVPvLXCrrrUtdnniCEXAMPLE/IvU1dYUg2RVAJBanLiHb4IgRmpRV3zrkuWJOgQs8IZZaIv2BXIa2R4OlgkBN9bkUDNCJiBeb/AXlzBBko7b15fjrBs2+cTQtpZ3CYWFXG8C5zqx37wnOE49mRl/+OtkIKGO7fAE" - ) - token.access_key.should.equal("AKIAIOSFODNN7EXAMPLE") - token.secret_key.should.equal("wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY") - - @freeze_time("2012-01-01 12:00:00") @mock_sts def test_get_session_token_boto3(): @@ -47,30 +31,6 @@ def test_get_session_token_boto3(): creds["SecretAccessKey"].should.equal("wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY") -# Has boto3 equivalent -@freeze_time("2012-01-01 12:00:00") -@mock_sts_deprecated -def test_get_federation_token(): - conn = boto.connect_sts() - token_name = "Bob" - token = conn.get_federation_token(duration=123, name=token_name) - - token.credentials.expiration.should.equal("2012-01-01T12:02:03.000Z") - token.credentials.session_token.should.equal( - "AQoDYXdzEPT//////////wEXAMPLEtc764bNrC9SAPBSM22wDOk4x4HIZ8j4FZTwdQWLWsKWHGBuFqwAeMicRXmxfpSPfIeoIYRqTflfKD8YUuwthAx7mSEI/qkPpKPi/kMcGdQrmGdeehM4IC1NtBmUpp2wUE8phUZampKsburEDy0KPkyQDYwT7WZ0wq5VSXDvp75YU9HFvlRd8Tx6q6fE8YQcHNVXAkiY9q6d+xo0rKwT38xVqr7ZD0u0iPPkUL64lIZbqBAz+scqKmlzm8FDrypNC9Yjc8fPOLn9FX9KSYvKTr4rvx3iSIlTJabIQwj2ICCR/oLxBA==" - ) - token.credentials.access_key.should.equal("AKIAIOSFODNN7EXAMPLE") - token.credentials.secret_key.should.equal( - "wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY" - ) - token.federated_user_arn.should.equal( - "arn:aws:sts::{account_id}:federated-user/{token_name}".format( - account_id=ACCOUNT_ID, token_name=token_name - ) - ) - token.federated_user_id.should.equal(str(ACCOUNT_ID) + ":" + token_name) - - @freeze_time("2012-01-01 12:00:00") @mock_sts def test_get_federation_token_boto3(): @@ -651,49 +611,6 @@ def test_assume_role_with_saml_should_default_session_duration_to_3600_seconds_w credentials["Expiration"].isoformat().should.equal("2012-01-01T13:00:00+00:00") -# Has boto3 equivalent -@freeze_time("2012-01-01 12:00:00") -@mock_sts_deprecated -def test_assume_role_with_web_identity(): - conn = boto.connect_sts() - - policy = json.dumps( - { - "Statement": [ - { - "Sid": "Stmt13690092345534", - "Action": ["S3:ListBucket"], - "Effect": "Allow", - "Resource": ["arn:aws:s3:::foobar-tester"], - } - ] - } - ) - role_name = "test-role" - s3_role = "arn:aws:iam::{account_id}:role/{role_name}".format( - account_id=ACCOUNT_ID, role_name=role_name - ) - session_name = "session-name" - role = conn.assume_role_with_web_identity( - s3_role, session_name, policy, duration_seconds=123 - ) - - credentials = role.credentials - credentials.expiration.should.equal("2012-01-01T12:02:03.000Z") - credentials.session_token.should.have.length_of(356) - assert credentials.session_token.startswith("FQoGZXIvYXdzE") - credentials.access_key.should.have.length_of(20) - assert credentials.access_key.startswith("ASIA") - credentials.secret_key.should.have.length_of(40) - - role.user.arn.should.equal( - "arn:aws:sts::{account_id}:assumed-role/{role_name}/{session_name}".format( - account_id=ACCOUNT_ID, role_name=role_name, session_name=session_name - ) - ) - role.user.assume_role_id.should.contain("session-name") - - @freeze_time("2012-01-01 12:00:00") @mock_sts def test_assume_role_with_web_identity_boto3(): diff --git a/tests/test_swf/responses/test_activity_tasks.py b/tests/test_swf/responses/test_activity_tasks.py index 8dc9ba81d..945e326f2 100644 --- a/tests/test_swf/responses/test_activity_tasks.py +++ b/tests/test_swf/responses/test_activity_tasks.py @@ -1,40 +1,17 @@ -from boto.swf.exceptions import SWFResponseError from botocore.exceptions import ClientError from freezegun import freeze_time import sure # noqa # pylint: disable=unused-import from unittest import SkipTest import pytest -from moto import mock_swf, mock_swf_deprecated +from moto import mock_swf from moto import settings -from moto.swf import swf_backend -from ..utils import setup_workflow, SCHEDULE_ACTIVITY_TASK_DECISION +from ..utils import SCHEDULE_ACTIVITY_TASK_DECISION from ..utils import setup_workflow_boto3 # PollForActivityTask endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_poll_for_activity_task_when_one(): - conn = setup_workflow() - decision_token = conn.poll_for_decision_task("test-domain", "queue")["taskToken"] - conn.respond_decision_task_completed( - decision_token, decisions=[SCHEDULE_ACTIVITY_TASK_DECISION] - ) - resp = conn.poll_for_activity_task( - "test-domain", "activity-task-list", identity="surprise" - ) - resp["activityId"].should.equal("my-activity-001") - resp["taskToken"].should_not.be.none - - resp = conn.get_workflow_execution_history( - "test-domain", conn.run_id, "uid-abcd1234" - ) - resp["events"][-1]["eventType"].should.equal("ActivityTaskStarted") - resp["events"][-1]["activityTaskStartedEventAttributes"].should.equal( - {"identity": "surprise", "scheduledEventId": 5} - ) @mock_swf @@ -64,22 +41,6 @@ def test_poll_for_activity_task_when_one_boto3(): ) -# Has boto3 equivalent -@mock_swf_deprecated -def test_poll_for_activity_task_when_none(): - conn = setup_workflow() - resp = conn.poll_for_activity_task("test-domain", "activity-task-list") - resp.should.equal({"startedEventId": 0}) - - -# Has boto3 equivalent -@mock_swf_deprecated -def test_poll_for_activity_task_on_non_existent_queue(): - conn = setup_workflow() - resp = conn.poll_for_activity_task("test-domain", "non-existent-queue") - resp.should.equal({"startedEventId": 0}) - - @pytest.mark.parametrize("task_name", ["activity-task-list", "non-existent-queue"]) @mock_swf def test_poll_for_activity_task_when_none_boto3(task_name): @@ -93,25 +54,6 @@ def test_poll_for_activity_task_when_none_boto3(task_name): # CountPendingActivityTasks endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_count_pending_activity_tasks(): - conn = setup_workflow() - decision_token = conn.poll_for_decision_task("test-domain", "queue")["taskToken"] - conn.respond_decision_task_completed( - decision_token, decisions=[SCHEDULE_ACTIVITY_TASK_DECISION] - ) - - resp = conn.count_pending_activity_tasks("test-domain", "activity-task-list") - resp.should.equal({"count": 1, "truncated": False}) - - -# Has boto3 equivalent -@mock_swf_deprecated -def test_count_pending_decision_tasks_on_non_existent_task_list(): - conn = setup_workflow() - resp = conn.count_pending_activity_tasks("test-domain", "non-existent") - resp.should.equal({"count": 0, "truncated": False}) @pytest.mark.parametrize( @@ -135,30 +77,6 @@ def test_count_pending_activity_tasks_boto3(task_name, cnt): # RespondActivityTaskCompleted endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_respond_activity_task_completed(): - conn = setup_workflow() - decision_token = conn.poll_for_decision_task("test-domain", "queue")["taskToken"] - conn.respond_decision_task_completed( - decision_token, decisions=[SCHEDULE_ACTIVITY_TASK_DECISION] - ) - activity_token = conn.poll_for_activity_task("test-domain", "activity-task-list")[ - "taskToken" - ] - - resp = conn.respond_activity_task_completed( - activity_token, result="result of the task" - ) - resp.should.be.none - - resp = conn.get_workflow_execution_history( - "test-domain", conn.run_id, "uid-abcd1234" - ) - resp["events"][-2]["eventType"].should.equal("ActivityTaskCompleted") - resp["events"][-2]["activityTaskCompletedEventAttributes"].should.equal( - {"result": "result of the task", "scheduledEventId": 5, "startedEventId": 6} - ) @mock_swf @@ -188,29 +106,6 @@ def test_respond_activity_task_completed_boto3(): ) -# Has boto3 equivalent -@mock_swf_deprecated -def test_respond_activity_task_completed_on_closed_workflow_execution(): - conn = setup_workflow() - decision_token = conn.poll_for_decision_task("test-domain", "queue")["taskToken"] - conn.respond_decision_task_completed( - decision_token, decisions=[SCHEDULE_ACTIVITY_TASK_DECISION] - ) - activity_token = conn.poll_for_activity_task("test-domain", "activity-task-list")[ - "taskToken" - ] - - # bad: we're closing workflow execution manually, but endpoints are not - # coded for now.. - wfe = swf_backend.domains[0].workflow_executions[-1] - wfe.execution_status = "CLOSED" - # /bad - - conn.respond_activity_task_completed.when.called_with(activity_token).should.throw( - SWFResponseError, "WorkflowExecution=" - ) - - @mock_swf def test_respond_activity_task_completed_on_closed_workflow_execution_boto3(): client = setup_workflow_boto3() @@ -237,25 +132,6 @@ def test_respond_activity_task_completed_on_closed_workflow_execution_boto3(): ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) -# Has boto3 equivalent -@mock_swf_deprecated -def test_respond_activity_task_completed_with_task_already_completed(): - conn = setup_workflow() - decision_token = conn.poll_for_decision_task("test-domain", "queue")["taskToken"] - conn.respond_decision_task_completed( - decision_token, decisions=[SCHEDULE_ACTIVITY_TASK_DECISION] - ) - activity_token = conn.poll_for_activity_task("test-domain", "activity-task-list")[ - "taskToken" - ] - - conn.respond_activity_task_completed(activity_token) - - conn.respond_activity_task_completed.when.called_with(activity_token).should.throw( - SWFResponseError, "Unknown activity, scheduledEventId = 5" - ) - - @mock_swf def test_respond_activity_task_completed_with_task_already_completed_boto3(): client = setup_workflow_boto3() @@ -281,35 +157,6 @@ def test_respond_activity_task_completed_with_task_already_completed_boto3(): # RespondActivityTaskFailed endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_respond_activity_task_failed(): - conn = setup_workflow() - decision_token = conn.poll_for_decision_task("test-domain", "queue")["taskToken"] - conn.respond_decision_task_completed( - decision_token, decisions=[SCHEDULE_ACTIVITY_TASK_DECISION] - ) - activity_token = conn.poll_for_activity_task("test-domain", "activity-task-list")[ - "taskToken" - ] - - resp = conn.respond_activity_task_failed( - activity_token, reason="short reason", details="long details" - ) - resp.should.be.none - - resp = conn.get_workflow_execution_history( - "test-domain", conn.run_id, "uid-abcd1234" - ) - resp["events"][-2]["eventType"].should.equal("ActivityTaskFailed") - resp["events"][-2]["activityTaskFailedEventAttributes"].should.equal( - { - "reason": "short reason", - "details": "long details", - "scheduledEventId": 5, - "startedEventId": 6, - } - ) @mock_swf @@ -344,23 +191,6 @@ def test_respond_activity_task_failed_boto3(): ) -# Has boto3 equivalent -@mock_swf_deprecated -def test_respond_activity_task_completed_with_wrong_token(): - # NB: we just test ONE failure case for RespondActivityTaskFailed - # because the safeguards are shared with RespondActivityTaskCompleted, so - # no need to retest everything end-to-end. - conn = setup_workflow() - decision_token = conn.poll_for_decision_task("test-domain", "queue")["taskToken"] - conn.respond_decision_task_completed( - decision_token, decisions=[SCHEDULE_ACTIVITY_TASK_DECISION] - ) - conn.poll_for_activity_task("test-domain", "activity-task-list") - conn.respond_activity_task_failed.when.called_with( - "not-a-correct-token" - ).should.throw(SWFResponseError, "Invalid token") - - @mock_swf def test_respond_activity_task_completed_with_wrong_token_boto3(): # NB: we just test ONE failure case for RespondActivityTaskFailed @@ -385,20 +215,6 @@ def test_respond_activity_task_completed_with_wrong_token_boto3(): # RecordActivityTaskHeartbeat endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_record_activity_task_heartbeat(): - conn = setup_workflow() - decision_token = conn.poll_for_decision_task("test-domain", "queue")["taskToken"] - conn.respond_decision_task_completed( - decision_token, decisions=[SCHEDULE_ACTIVITY_TASK_DECISION] - ) - activity_token = conn.poll_for_activity_task("test-domain", "activity-task-list")[ - "taskToken" - ] - - resp = conn.record_activity_task_heartbeat(activity_token) - resp.should.equal({"cancelRequested": False}) @mock_swf @@ -418,21 +234,6 @@ def test_record_activity_task_heartbeat_boto3(): resp.should.have.key("cancelRequested").equal(False) -# Has boto3 equivalent -@mock_swf_deprecated -def test_record_activity_task_heartbeat_with_wrong_token(): - conn = setup_workflow() - decision_token = conn.poll_for_decision_task("test-domain", "queue")["taskToken"] - conn.respond_decision_task_completed( - decision_token, decisions=[SCHEDULE_ACTIVITY_TASK_DECISION] - ) - conn.poll_for_activity_task("test-domain", "activity-task-list")["taskToken"] - - conn.record_activity_task_heartbeat.when.called_with( - "bad-token", details="some progress details" - ).should.throw(SWFResponseError) - - @mock_swf def test_record_activity_task_heartbeat_with_wrong_token_boto3(): client = setup_workflow_boto3() @@ -453,32 +254,6 @@ def test_record_activity_task_heartbeat_with_wrong_token_boto3(): ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) -# Has boto3 equivalent -@mock_swf_deprecated -def test_record_activity_task_heartbeat_sets_details_in_case_of_timeout(): - conn = setup_workflow() - decision_token = conn.poll_for_decision_task("test-domain", "queue")["taskToken"] - conn.respond_decision_task_completed( - decision_token, decisions=[SCHEDULE_ACTIVITY_TASK_DECISION] - ) - with freeze_time("2015-01-01 12:00:00"): - activity_token = conn.poll_for_activity_task( - "test-domain", "activity-task-list" - )["taskToken"] - conn.record_activity_task_heartbeat( - activity_token, details="some progress details" - ) - - with freeze_time("2015-01-01 12:05:30"): - # => Activity Task Heartbeat timeout reached!! - resp = conn.get_workflow_execution_history( - "test-domain", conn.run_id, "uid-abcd1234" - ) - resp["events"][-2]["eventType"].should.equal("ActivityTaskTimedOut") - attrs = resp["events"][-2]["activityTaskTimedOutEventAttributes"] - attrs["details"].should.equal("some progress details") - - @mock_swf def test_record_activity_task_heartbeat_sets_details_in_case_of_timeout_boto3(): if settings.TEST_SERVER_MODE: diff --git a/tests/test_swf/responses/test_activity_types.py b/tests/test_swf/responses/test_activity_types.py index a2ac5169f..55d7b85ef 100644 --- a/tests/test_swf/responses/test_activity_types.py +++ b/tests/test_swf/responses/test_activity_types.py @@ -1,28 +1,12 @@ -import boto -from boto.swf.exceptions import SWFResponseError import boto3 from botocore.exceptions import ClientError import sure # noqa # pylint: disable=unused-import import pytest -from moto import mock_swf_deprecated from moto import mock_swf # RegisterActivityType endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_register_activity_type(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - conn.register_activity_type("test-domain", "test-activity", "v1.0") - - types = conn.list_activity_types("test-domain", "REGISTERED") - actype = types["typeInfos"][0] - actype["activityType"]["name"].should.equal("test-activity") - actype["activityType"]["version"].should.equal("v1.0") - - @mock_swf def test_register_activity_type_boto3(): client = boto3.client("swf", region_name="us-west-2") @@ -42,18 +26,6 @@ def test_register_activity_type_boto3(): actype["activityType"]["version"].should.equal("v1.0") -# Has boto3 equivalent -@mock_swf_deprecated -def test_register_already_existing_activity_type(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - conn.register_activity_type("test-domain", "test-activity", "v1.0") - - conn.register_activity_type.when.called_with( - "test-domain", "test-activity", "v1.0" - ).should.throw(SWFResponseError) - - @mock_swf def test_register_already_existing_activity_type_boto3(): client = boto3.client("swf", region_name="us-west-2") @@ -75,33 +47,7 @@ def test_register_already_existing_activity_type_boto3(): ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) -# Has boto3 equivalent -@mock_swf_deprecated -def test_register_with_wrong_parameter_type(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - - conn.register_activity_type.when.called_with( - "test-domain", "test-activity", 12 - ).should.throw(SWFResponseError) - - # ListActivityTypes endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_list_activity_types(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - conn.register_activity_type("test-domain", "b-test-activity", "v1.0") - conn.register_activity_type("test-domain", "a-test-activity", "v1.0") - conn.register_activity_type("test-domain", "c-test-activity", "v1.0") - - all_activity_types = conn.list_activity_types("test-domain", "REGISTERED") - names = [ - activity_type["activityType"]["name"] - for activity_type in all_activity_types["typeInfos"] - ] - names.should.equal(["a-test-activity", "b-test-activity", "c-test-activity"]) # ListActivityTypes endpoint @@ -130,25 +76,6 @@ def test_list_activity_types_boto3(): names.should.equal(["a-test-activity", "b-test-activity", "c-test-activity"]) -# Has boto3 equivalent -@mock_swf_deprecated -def test_list_activity_types_reverse_order(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - conn.register_activity_type("test-domain", "b-test-activity", "v1.0") - conn.register_activity_type("test-domain", "a-test-activity", "v1.0") - conn.register_activity_type("test-domain", "c-test-activity", "v1.0") - - all_activity_types = conn.list_activity_types( - "test-domain", "REGISTERED", reverse_order=True - ) - names = [ - activity_type["activityType"]["name"] - for activity_type in all_activity_types["typeInfos"] - ] - names.should.equal(["c-test-activity", "b-test-activity", "a-test-activity"]) - - @mock_swf def test_list_activity_types_reverse_order_boto3(): client = boto3.client("swf", region_name="us-west-2") @@ -175,21 +102,6 @@ def test_list_activity_types_reverse_order_boto3(): names.should.equal(["c-test-activity", "b-test-activity", "a-test-activity"]) -# DeprecateActivityType endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_deprecate_activity_type(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - conn.register_activity_type("test-domain", "test-activity", "v1.0") - conn.deprecate_activity_type("test-domain", "test-activity", "v1.0") - - actypes = conn.list_activity_types("test-domain", "DEPRECATED") - actype = actypes["typeInfos"][0] - actype["activityType"]["name"].should.equal("test-activity") - actype["activityType"]["version"].should.equal("v1.0") - - # DeprecateActivityType endpoint @mock_swf def test_deprecate_activity_type_boto3(): @@ -213,19 +125,6 @@ def test_deprecate_activity_type_boto3(): actype["activityType"]["version"].should.equal("v1.0") -# Has boto3 equivalent -@mock_swf_deprecated -def test_deprecate_already_deprecated_activity_type(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - conn.register_activity_type("test-domain", "test-activity", "v1.0") - conn.deprecate_activity_type("test-domain", "test-activity", "v1.0") - - conn.deprecate_activity_type.when.called_with( - "test-domain", "test-activity", "v1.0" - ).should.throw(SWFResponseError) - - @mock_swf def test_deprecate_already_deprecated_activity_type_boto3(): client = boto3.client("swf", region_name="us-west-2") @@ -251,17 +150,6 @@ def test_deprecate_already_deprecated_activity_type_boto3(): ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) -# Has boto3 equivalent -@mock_swf_deprecated -def test_deprecate_non_existent_activity_type(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - - conn.deprecate_activity_type.when.called_with( - "test-domain", "non-existent", "v1.0" - ).should.throw(SWFResponseError) - - @mock_swf def test_deprecate_non_existent_activity_type_boto3(): client = boto3.client("swf", region_name="us-west-2") @@ -353,27 +241,6 @@ def test_undeprecate_non_existent_activity_type(): # DescribeActivityType endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_describe_activity_type(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - conn.register_activity_type( - "test-domain", - "test-activity", - "v1.0", - task_list="foo", - default_task_heartbeat_timeout="32", - ) - - actype = conn.describe_activity_type("test-domain", "test-activity", "v1.0") - actype["configuration"]["defaultTaskList"]["name"].should.equal("foo") - infos = actype["typeInfo"] - infos["activityType"]["name"].should.equal("test-activity") - infos["activityType"]["version"].should.equal("v1.0") - infos["status"].should.equal("REGISTERED") - - @mock_swf def test_describe_activity_type_boto3(): client = boto3.client("swf", region_name="us-east-1") @@ -398,17 +265,6 @@ def test_describe_activity_type_boto3(): infos["status"].should.equal("REGISTERED") -# Has boto3 equivalent -@mock_swf_deprecated -def test_describe_non_existent_activity_type(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - - conn.describe_activity_type.when.called_with( - "test-domain", "non-existent", "v1.0" - ).should.throw(SWFResponseError) - - @mock_swf def test_describe_non_existent_activity_type_boto3(): client = boto3.client("swf", region_name="us-east-1") diff --git a/tests/test_swf/responses/test_decision_tasks.py b/tests/test_swf/responses/test_decision_tasks.py index 62141690a..9bf5c68cd 100644 --- a/tests/test_swf/responses/test_decision_tasks.py +++ b/tests/test_swf/responses/test_decision_tasks.py @@ -1,4 +1,3 @@ -from boto.swf.exceptions import SWFResponseError from botocore.exceptions import ClientError from datetime import datetime from freezegun import freeze_time @@ -6,33 +5,12 @@ from time import sleep import sure # noqa # pylint: disable=unused-import import pytest -from moto import mock_swf_deprecated, mock_swf, settings -from moto.swf import swf_backend +from moto import mock_swf, settings -from ..utils import setup_workflow, setup_workflow_boto3 +from ..utils import setup_workflow_boto3 # PollForDecisionTask endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_poll_for_decision_task_when_one(): - conn = setup_workflow() - - resp = conn.get_workflow_execution_history( - "test-domain", conn.run_id, "uid-abcd1234" - ) - types = [evt["eventType"] for evt in resp["events"]] - types.should.equal(["WorkflowExecutionStarted", "DecisionTaskScheduled"]) - - resp = conn.poll_for_decision_task("test-domain", "queue", identity="srv01") - types = [evt["eventType"] for evt in resp["events"]] - types.should.equal( - ["WorkflowExecutionStarted", "DecisionTaskScheduled", "DecisionTaskStarted"] - ) - - resp["events"][-1]["decisionTaskStartedEventAttributes"]["identity"].should.equal( - "srv01" - ) @mock_swf @@ -59,31 +37,6 @@ def test_poll_for_decision_task_when_one_boto3(): ) -# Has boto3 equivalent -@mock_swf_deprecated -def test_poll_for_decision_task_previous_started_event_id(): - conn = setup_workflow() - - resp = conn.poll_for_decision_task("test-domain", "queue") - assert resp["workflowExecution"]["runId"] == conn.run_id - assert "previousStartedEventId" not in resp - - # Require a failing decision, in this case a non-existant activity type - attrs = { - "activityId": "spam", - "activityType": {"name": "test-activity", "version": "v1.42"}, - "taskList": "eggs", - } - decision = { - "decisionType": "ScheduleActivityTask", - "scheduleActivityTaskDecisionAttributes": attrs, - } - conn.respond_decision_task_completed(resp["taskToken"], decisions=[decision]) - resp = conn.poll_for_decision_task("test-domain", "queue") - assert resp["workflowExecution"]["runId"] == conn.run_id - assert resp["previousStartedEventId"] == 3 - - @mock_swf def test_poll_for_decision_task_previous_started_event_id_boto3(): client = setup_workflow_boto3() @@ -114,18 +67,6 @@ def test_poll_for_decision_task_previous_started_event_id_boto3(): assert resp["previousStartedEventId"] == 3 -# Has boto3 equivalent -@mock_swf_deprecated -def test_poll_for_decision_task_when_none(): - conn = setup_workflow() - conn.poll_for_decision_task("test-domain", "queue") - - resp = conn.poll_for_decision_task("test-domain", "queue") - # this is the DecisionTask representation you get from the real SWF - # after waiting 60s when there's no decision to be taken - resp.should.equal({"previousStartedEventId": 0, "startedEventId": 0}) - - @mock_swf def test_poll_for_decision_task_when_none_boto3(): client = setup_workflow_boto3() @@ -141,14 +82,6 @@ def test_poll_for_decision_task_when_none_boto3(): resp.should.have.key("startedEventId").equal(0) -# Has boto3 equivalent -@mock_swf_deprecated -def test_poll_for_decision_task_on_non_existent_queue(): - conn = setup_workflow() - resp = conn.poll_for_decision_task("test-domain", "non-existent-queue") - resp.should.equal({"previousStartedEventId": 0, "startedEventId": 0}) - - @mock_swf def test_poll_for_decision_task_on_non_existent_queue_boto3(): client = setup_workflow_boto3() @@ -159,17 +92,6 @@ def test_poll_for_decision_task_on_non_existent_queue_boto3(): resp.should.have.key("startedEventId").equal(0) -# Has boto3 equivalent -@mock_swf_deprecated -def test_poll_for_decision_task_with_reverse_order(): - conn = setup_workflow() - resp = conn.poll_for_decision_task("test-domain", "queue", reverse_order=True) - types = [evt["eventType"] for evt in resp["events"]] - types.should.equal( - ["DecisionTaskStarted", "DecisionTaskScheduled", "WorkflowExecutionStarted"] - ) - - @mock_swf def test_poll_for_decision_task_with_reverse_order_boto3(): client = setup_workflow_boto3() @@ -183,13 +105,6 @@ def test_poll_for_decision_task_with_reverse_order_boto3(): # CountPendingDecisionTasks endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_count_pending_decision_tasks(): - conn = setup_workflow() - conn.poll_for_decision_task("test-domain", "queue") - resp = conn.count_pending_decision_tasks("test-domain", "queue") - resp.should.equal({"count": 1, "truncated": False}) @mock_swf @@ -203,14 +118,6 @@ def test_count_pending_decision_tasks_boto3(): resp.should.have.key("truncated").equal(False) -# Has boto3 equivalent -@mock_swf_deprecated -def test_count_pending_decision_tasks_on_non_existent_task_list(): - conn = setup_workflow() - resp = conn.count_pending_decision_tasks("test-domain", "non-existent") - resp.should.equal({"count": 0, "truncated": False}) - - @mock_swf def test_count_pending_decision_tasks_on_non_existent_task_list_boto3(): client = setup_workflow_boto3() @@ -221,17 +128,6 @@ def test_count_pending_decision_tasks_on_non_existent_task_list_boto3(): resp.should.have.key("truncated").equal(False) -# Has boto3 equivalent -@mock_swf_deprecated -def test_count_pending_decision_tasks_after_decision_completes(): - conn = setup_workflow() - resp = conn.poll_for_decision_task("test-domain", "queue") - conn.respond_decision_task_completed(resp["taskToken"]) - - resp = conn.count_pending_decision_tasks("test-domain", "queue") - resp.should.equal({"count": 0, "truncated": False}) - - @mock_swf def test_count_pending_decision_tasks_after_decision_completes_boto3(): client = setup_workflow_boto3() @@ -248,42 +144,6 @@ def test_count_pending_decision_tasks_after_decision_completes_boto3(): # RespondDecisionTaskCompleted endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_respond_decision_task_completed_with_no_decision(): - conn = setup_workflow() - - resp = conn.poll_for_decision_task("test-domain", "queue") - task_token = resp["taskToken"] - - resp = conn.respond_decision_task_completed( - task_token, execution_context="free-form context" - ) - resp.should.be.none - - resp = conn.get_workflow_execution_history( - "test-domain", conn.run_id, "uid-abcd1234" - ) - types = [evt["eventType"] for evt in resp["events"]] - types.should.equal( - [ - "WorkflowExecutionStarted", - "DecisionTaskScheduled", - "DecisionTaskStarted", - "DecisionTaskCompleted", - ] - ) - evt = resp["events"][-1] - evt["decisionTaskCompletedEventAttributes"].should.equal( - { - "executionContext": "free-form context", - "scheduledEventId": 2, - "startedEventId": 3, - } - ) - - resp = conn.describe_workflow_execution("test-domain", conn.run_id, "uid-abcd1234") - resp["latestExecutionContext"].should.equal("free-form context") @mock_swf @@ -327,16 +187,6 @@ def test_respond_decision_task_completed_with_no_decision_boto3(): resp["latestExecutionContext"].should.equal("free-form context") -# Has boto3 equivalent -@mock_swf_deprecated -def test_respond_decision_task_completed_with_wrong_token(): - conn = setup_workflow() - conn.poll_for_decision_task("test-domain", "queue") - conn.respond_decision_task_completed.when.called_with( - "not-a-correct-token" - ).should.throw(SWFResponseError) - - @mock_swf def test_respond_decision_task_completed_with_wrong_token_boto3(): client = setup_workflow_boto3() @@ -348,24 +198,6 @@ def test_respond_decision_task_completed_with_wrong_token_boto3(): ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) -# Has boto3 equivalent -@mock_swf_deprecated -def test_respond_decision_task_completed_on_close_workflow_execution(): - conn = setup_workflow() - resp = conn.poll_for_decision_task("test-domain", "queue") - task_token = resp["taskToken"] - - # bad: we're closing workflow execution manually, but endpoints are not - # coded for now.. - wfe = swf_backend.domains[0].workflow_executions[-1] - wfe.execution_status = "CLOSED" - # /bad - - conn.respond_decision_task_completed.when.called_with(task_token).should.throw( - SWFResponseError - ) - - @mock_swf def test_respond_decision_task_completed_on_close_workflow_execution_boto3(): client = setup_workflow_boto3() @@ -387,19 +219,6 @@ def test_respond_decision_task_completed_on_close_workflow_execution_boto3(): ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) -# Has boto3 equivalent -@mock_swf_deprecated -def test_respond_decision_task_completed_with_task_already_completed(): - conn = setup_workflow() - resp = conn.poll_for_decision_task("test-domain", "queue") - task_token = resp["taskToken"] - conn.respond_decision_task_completed(task_token) - - conn.respond_decision_task_completed.when.called_with(task_token).should.throw( - SWFResponseError - ) - - @mock_swf def test_respond_decision_task_completed_with_task_already_completed_boto3(): client = setup_workflow_boto3() @@ -418,40 +237,6 @@ def test_respond_decision_task_completed_with_task_already_completed_boto3(): ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) -# Has boto3 equivalent -@mock_swf_deprecated -def test_respond_decision_task_completed_with_complete_workflow_execution(): - conn = setup_workflow() - resp = conn.poll_for_decision_task("test-domain", "queue") - task_token = resp["taskToken"] - - decisions = [ - { - "decisionType": "CompleteWorkflowExecution", - "completeWorkflowExecutionDecisionAttributes": {"result": "foo bar"}, - } - ] - resp = conn.respond_decision_task_completed(task_token, decisions=decisions) - resp.should.be.none - - resp = conn.get_workflow_execution_history( - "test-domain", conn.run_id, "uid-abcd1234" - ) - types = [evt["eventType"] for evt in resp["events"]] - types.should.equal( - [ - "WorkflowExecutionStarted", - "DecisionTaskScheduled", - "DecisionTaskStarted", - "DecisionTaskCompleted", - "WorkflowExecutionCompleted", - ] - ) - resp["events"][-1]["workflowExecutionCompletedEventAttributes"][ - "result" - ].should.equal("foo bar") - - @mock_swf def test_respond_decision_task_completed_with_complete_workflow_execution_boto3(): client = setup_workflow_boto3() @@ -487,23 +272,6 @@ def test_respond_decision_task_completed_with_complete_workflow_execution_boto3( ].should.equal("foo bar") -# Has boto3 equivalent -@mock_swf_deprecated -def test_respond_decision_task_completed_with_close_decision_not_last(): - conn = setup_workflow() - resp = conn.poll_for_decision_task("test-domain", "queue") - task_token = resp["taskToken"] - - decisions = [ - {"decisionType": "CompleteWorkflowExecution"}, - {"decisionType": "WeDontCare"}, - ] - - conn.respond_decision_task_completed.when.called_with( - task_token, decisions=decisions - ).should.throw(SWFResponseError, r"Close must be last decision in list") - - @mock_swf def test_respond_decision_task_completed_with_close_decision_not_last_boto3(): client = setup_workflow_boto3() @@ -528,26 +296,6 @@ def test_respond_decision_task_completed_with_close_decision_not_last_boto3(): ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) -# Has boto3 equivalent -@mock_swf_deprecated -def test_respond_decision_task_completed_with_invalid_decision_type(): - conn = setup_workflow() - resp = conn.poll_for_decision_task("test-domain", "queue") - task_token = resp["taskToken"] - - decisions = [ - {"decisionType": "BadDecisionType"}, - {"decisionType": "CompleteWorkflowExecution"}, - ] - - conn.respond_decision_task_completed.when.called_with( - task_token, decisions=decisions - ).should.throw( - SWFResponseError, - r"Value 'BadDecisionType' at 'decisions.1.member.decisionType'", - ) - - @mock_swf def test_respond_decision_task_completed_with_invalid_decision_type_boto3(): client = setup_workflow_boto3() @@ -572,47 +320,6 @@ def test_respond_decision_task_completed_with_invalid_decision_type_boto3(): ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) -# Has boto3 equivalent -@mock_swf_deprecated -def test_respond_decision_task_completed_with_missing_attributes(): - conn = setup_workflow() - resp = conn.poll_for_decision_task("test-domain", "queue") - task_token = resp["taskToken"] - - decisions = [ - { - "decisionType": "should trigger even with incorrect decision type", - "startTimerDecisionAttributes": {}, - } - ] - - conn.respond_decision_task_completed.when.called_with( - task_token, decisions=decisions - ).should.throw( - SWFResponseError, - r"Value null at 'decisions.1.member.startTimerDecisionAttributes.timerId' " - r"failed to satisfy constraint: Member must not be null", - ) - - -# Has boto3 equivalent -@mock_swf_deprecated -def test_respond_decision_task_completed_with_missing_attributes_totally(): - conn = setup_workflow() - resp = conn.poll_for_decision_task("test-domain", "queue") - task_token = resp["taskToken"] - - decisions = [{"decisionType": "StartTimer"}] - - conn.respond_decision_task_completed.when.called_with( - task_token, decisions=decisions - ).should.throw( - SWFResponseError, - r"Value null at 'decisions.1.member.startTimerDecisionAttributes.timerId' " - r"failed to satisfy constraint: Member must not be null", - ) - - @mock_swf def test_respond_decision_task_completed_with_missing_attributes_totally_boto3(): client = setup_workflow_boto3() @@ -634,43 +341,6 @@ def test_respond_decision_task_completed_with_missing_attributes_totally_boto3() ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) -# Has boto3 equivalent -@mock_swf_deprecated -def test_respond_decision_task_completed_with_fail_workflow_execution(): - conn = setup_workflow() - resp = conn.poll_for_decision_task("test-domain", "queue") - task_token = resp["taskToken"] - - decisions = [ - { - "decisionType": "FailWorkflowExecution", - "failWorkflowExecutionDecisionAttributes": { - "reason": "my rules", - "details": "foo", - }, - } - ] - resp = conn.respond_decision_task_completed(task_token, decisions=decisions) - resp.should.be.none - - resp = conn.get_workflow_execution_history( - "test-domain", conn.run_id, "uid-abcd1234" - ) - types = [evt["eventType"] for evt in resp["events"]] - types.should.equal( - [ - "WorkflowExecutionStarted", - "DecisionTaskScheduled", - "DecisionTaskStarted", - "DecisionTaskCompleted", - "WorkflowExecutionFailed", - ] - ) - attrs = resp["events"][-1]["workflowExecutionFailedEventAttributes"] - attrs["reason"].should.equal("my rules") - attrs["details"].should.equal("foo") - - @mock_swf def test_respond_decision_task_completed_with_fail_workflow_execution_boto3(): client = setup_workflow_boto3() @@ -709,57 +379,6 @@ def test_respond_decision_task_completed_with_fail_workflow_execution_boto3(): attrs["details"].should.equal("foo") -# Has boto3 equivalent -@mock_swf_deprecated -@freeze_time("2015-01-01 12:00:00") -def test_respond_decision_task_completed_with_schedule_activity_task(): - conn = setup_workflow() - resp = conn.poll_for_decision_task("test-domain", "queue") - task_token = resp["taskToken"] - - decisions = [ - { - "decisionType": "ScheduleActivityTask", - "scheduleActivityTaskDecisionAttributes": { - "activityId": "my-activity-001", - "activityType": {"name": "test-activity", "version": "v1.1"}, - "heartbeatTimeout": "60", - "input": "123", - "taskList": {"name": "my-task-list"}, - }, - } - ] - resp = conn.respond_decision_task_completed(task_token, decisions=decisions) - resp.should.be.none - - resp = conn.get_workflow_execution_history( - "test-domain", conn.run_id, "uid-abcd1234" - ) - types = [evt["eventType"] for evt in resp["events"]] - types.should.equal( - [ - "WorkflowExecutionStarted", - "DecisionTaskScheduled", - "DecisionTaskStarted", - "DecisionTaskCompleted", - "ActivityTaskScheduled", - ] - ) - resp["events"][-1]["activityTaskScheduledEventAttributes"].should.equal( - { - "decisionTaskCompletedEventId": 4, - "activityId": "my-activity-001", - "activityType": {"name": "test-activity", "version": "v1.1"}, - "heartbeatTimeout": "60", - "input": "123", - "taskList": {"name": "my-task-list"}, - } - ) - - resp = conn.describe_workflow_execution("test-domain", conn.run_id, "uid-abcd1234") - resp["latestActivityTaskTimestamp"].should.equal(1420113600.0) - - @mock_swf @freeze_time("2015-01-01 12:00:00") def test_respond_decision_task_completed_with_schedule_activity_task_boto3(): diff --git a/tests/test_swf/responses/test_domains.py b/tests/test_swf/responses/test_domains.py index 94c150230..9d00cbfca 100644 --- a/tests/test_swf/responses/test_domains.py +++ b/tests/test_swf/responses/test_domains.py @@ -1,33 +1,13 @@ -import boto -from boto.swf.exceptions import SWFResponseError import boto3 from botocore.exceptions import ClientError import sure # noqa # pylint: disable=unused-import import pytest -from moto import mock_swf_deprecated from moto import mock_swf from moto.core import ACCOUNT_ID # RegisterDomain endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_register_domain(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60", description="A test domain") - - all_domains = conn.list_domains("REGISTERED") - domain = all_domains["domainInfos"][0] - - domain["name"].should.equal("test-domain") - domain["status"].should.equal("REGISTERED") - domain["description"].should.equal("A test domain") - domain["arn"].should.equal( - "arn:aws:swf:us-east-1:{0}:/domain/test-domain".format(ACCOUNT_ID) - ) - - @mock_swf def test_register_domain_boto3(): client = boto3.client("swf", region_name="us-west-1") @@ -49,17 +29,6 @@ def test_register_domain_boto3(): ) -# Has boto3 equivalent -@mock_swf_deprecated -def test_register_already_existing_domain(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60", description="A test domain") - - conn.register_domain.when.called_with( - "test-domain", "60", description="A test domain" - ).should.throw(SWFResponseError) - - @mock_swf def test_register_already_existing_domain_boto3(): client = boto3.client("swf", region_name="us-west-1") @@ -80,30 +49,7 @@ def test_register_already_existing_domain_boto3(): ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) -# Has boto3 equivalent -@mock_swf_deprecated -def test_register_with_wrong_parameter_type(): - conn = boto.connect_swf("the_key", "the_secret") - - conn.register_domain.when.called_with( - "test-domain", 60, description="A test domain" - ).should.throw(SWFResponseError) - - # ListDomains endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_list_domains_order(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("b-test-domain", "60") - conn.register_domain("a-test-domain", "60") - conn.register_domain("c-test-domain", "60") - - all_domains = conn.list_domains("REGISTERED") - names = [domain["name"] for domain in all_domains["domainInfos"]] - names.should.equal(["a-test-domain", "b-test-domain", "c-test-domain"]) - - @mock_swf def test_list_domains_order_boto3(): client = boto3.client("swf", region_name="us-west-1") @@ -124,19 +70,6 @@ def test_list_domains_order_boto3(): names.should.equal(["a-test-domain", "b-test-domain", "c-test-domain"]) -# Has boto3 equivalent -@mock_swf_deprecated -def test_list_domains_reverse_order(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("b-test-domain", "60") - conn.register_domain("a-test-domain", "60") - conn.register_domain("c-test-domain", "60") - - all_domains = conn.list_domains("REGISTERED", reverse_order=True) - names = [domain["name"] for domain in all_domains["domainInfos"]] - names.should.equal(["c-test-domain", "b-test-domain", "a-test-domain"]) - - @mock_swf def test_list_domains_reverse_order_boto3(): client = boto3.client("swf", region_name="us-west-1") @@ -160,19 +93,6 @@ def test_list_domains_reverse_order_boto3(): # DeprecateDomain endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_deprecate_domain(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60", description="A test domain") - conn.deprecate_domain("test-domain") - - all_domains = conn.list_domains("DEPRECATED") - domain = all_domains["domainInfos"][0] - - domain["name"].should.equal("test-domain") - - @mock_swf def test_deprecate_domain_boto3(): client = boto3.client("swf", region_name="us-west-1") @@ -191,16 +111,6 @@ def test_deprecate_domain_boto3(): domain["name"].should.equal("test-domain") -# Has boto3 equivalent -@mock_swf_deprecated -def test_deprecate_already_deprecated_domain(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60", description="A test domain") - conn.deprecate_domain("test-domain") - - conn.deprecate_domain.when.called_with("test-domain").should.throw(SWFResponseError) - - @mock_swf def test_deprecate_already_deprecated_domain_boto3(): client = boto3.client("swf", region_name="us-west-1") @@ -216,16 +126,6 @@ def test_deprecate_already_deprecated_domain_boto3(): ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) -# Has boto3 equivalent -@mock_swf_deprecated -def test_deprecate_non_existent_domain(): - conn = boto.connect_swf("the_key", "the_secret") - - conn.deprecate_domain.when.called_with("non-existent").should.throw( - SWFResponseError - ) - - @mock_swf def test_deprecate_non_existent_domain_boto3(): client = boto3.client("swf", region_name="us-west-1") @@ -288,19 +188,6 @@ def test_undeprecate_non_existent_domain(): # DescribeDomain endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_describe_domain(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60", description="A test domain") - - domain = conn.describe_domain("test-domain") - domain["configuration"]["workflowExecutionRetentionPeriodInDays"].should.equal("60") - domain["domainInfo"]["description"].should.equal("A test domain") - domain["domainInfo"]["name"].should.equal("test-domain") - domain["domainInfo"]["status"].should.equal("REGISTERED") - - @mock_swf def test_describe_domain_boto3(): client = boto3.client("swf", region_name="us-east-1") @@ -317,14 +204,6 @@ def test_describe_domain_boto3(): domain["domainInfo"]["status"].should.equal("REGISTERED") -# Has boto3 equivalent -@mock_swf_deprecated -def test_describe_non_existent_domain(): - conn = boto.connect_swf("the_key", "the_secret") - - conn.describe_domain.when.called_with("non-existent").should.throw(SWFResponseError) - - @mock_swf def test_describe_non_existent_domain_boto3(): client = boto3.client("swf", region_name="us-west-1") diff --git a/tests/test_swf/responses/test_timeouts.py b/tests/test_swf/responses/test_timeouts.py index 1cca20dd8..f3172c1da 100644 --- a/tests/test_swf/responses/test_timeouts.py +++ b/tests/test_swf/responses/test_timeouts.py @@ -3,50 +3,12 @@ from freezegun import freeze_time import sure # noqa # pylint: disable=unused-import from unittest import SkipTest -from moto import mock_swf_deprecated, mock_swf, settings +from moto import mock_swf, settings -from ..utils import setup_workflow, SCHEDULE_ACTIVITY_TASK_DECISION +from ..utils import SCHEDULE_ACTIVITY_TASK_DECISION from ..utils import setup_workflow_boto3 -# Activity Task Heartbeat timeout -# Default value in workflow helpers: 5 mins -# Has boto3 equivalent -@mock_swf_deprecated -def test_activity_task_heartbeat_timeout(): - with freeze_time("2015-01-01 12:00:00"): - conn = setup_workflow() - decision_token = conn.poll_for_decision_task("test-domain", "queue")[ - "taskToken" - ] - conn.respond_decision_task_completed( - decision_token, decisions=[SCHEDULE_ACTIVITY_TASK_DECISION] - ) - conn.poll_for_activity_task( - "test-domain", "activity-task-list", identity="surprise" - ) - - with freeze_time("2015-01-01 12:04:30"): - resp = conn.get_workflow_execution_history( - "test-domain", conn.run_id, "uid-abcd1234" - ) - resp["events"][-1]["eventType"].should.equal("ActivityTaskStarted") - - with freeze_time("2015-01-01 12:05:30"): - # => Activity Task Heartbeat timeout reached!! - resp = conn.get_workflow_execution_history( - "test-domain", conn.run_id, "uid-abcd1234" - ) - - resp["events"][-2]["eventType"].should.equal("ActivityTaskTimedOut") - attrs = resp["events"][-2]["activityTaskTimedOutEventAttributes"] - attrs["timeoutType"].should.equal("HEARTBEAT") - # checks that event has been emitted at 12:05:00, not 12:05:30 - resp["events"][-2]["eventTimestamp"].should.equal(1420113900.0) - - resp["events"][-1]["eventType"].should.equal("DecisionTaskScheduled") - - # Activity Task Heartbeat timeout # Default value in workflow helpers: 5 mins @mock_swf @@ -90,54 +52,6 @@ def test_activity_task_heartbeat_timeout_boto3(): ts.should.equal("2015-01-01 12:05:00") -# Decision Task Start to Close timeout -# Default value in workflow helpers: 5 mins -# Has boto3 equivalent -@mock_swf_deprecated -def test_decision_task_start_to_close_timeout(): - pass - with freeze_time("2015-01-01 12:00:00"): - conn = setup_workflow() - conn.poll_for_decision_task("test-domain", "queue")["taskToken"] - - with freeze_time("2015-01-01 12:04:30"): - resp = conn.get_workflow_execution_history( - "test-domain", conn.run_id, "uid-abcd1234" - ) - - event_types = [evt["eventType"] for evt in resp["events"]] - event_types.should.equal( - ["WorkflowExecutionStarted", "DecisionTaskScheduled", "DecisionTaskStarted"] - ) - - with freeze_time("2015-01-01 12:05:30"): - # => Decision Task Start to Close timeout reached!! - resp = conn.get_workflow_execution_history( - "test-domain", conn.run_id, "uid-abcd1234" - ) - - event_types = [evt["eventType"] for evt in resp["events"]] - event_types.should.equal( - [ - "WorkflowExecutionStarted", - "DecisionTaskScheduled", - "DecisionTaskStarted", - "DecisionTaskTimedOut", - "DecisionTaskScheduled", - ] - ) - attrs = resp["events"][-2]["decisionTaskTimedOutEventAttributes"] - attrs.should.equal( - { - "scheduledEventId": 2, - "startedEventId": 3, - "timeoutType": "START_TO_CLOSE", - } - ) - # checks that event has been emitted at 12:05:00, not 12:05:30 - resp["events"][-2]["eventTimestamp"].should.equal(1420113900.0) - - # Decision Task Start to Close timeout # Default value in workflow helpers: 5 mins @mock_swf @@ -191,43 +105,6 @@ def test_decision_task_start_to_close_timeout_boto3(): ts.should.equal("2015-01-01 12:05:00") -# Workflow Execution Start to Close timeout -# Default value in workflow helpers: 2 hours -# Has boto3 equivalent -@mock_swf_deprecated -def test_workflow_execution_start_to_close_timeout(): - pass - with freeze_time("2015-01-01 12:00:00"): - conn = setup_workflow() - - with freeze_time("2015-01-01 13:59:30"): - resp = conn.get_workflow_execution_history( - "test-domain", conn.run_id, "uid-abcd1234" - ) - - event_types = [evt["eventType"] for evt in resp["events"]] - event_types.should.equal(["WorkflowExecutionStarted", "DecisionTaskScheduled"]) - - with freeze_time("2015-01-01 14:00:30"): - # => Workflow Execution Start to Close timeout reached!! - resp = conn.get_workflow_execution_history( - "test-domain", conn.run_id, "uid-abcd1234" - ) - - event_types = [evt["eventType"] for evt in resp["events"]] - event_types.should.equal( - [ - "WorkflowExecutionStarted", - "DecisionTaskScheduled", - "WorkflowExecutionTimedOut", - ] - ) - attrs = resp["events"][-1]["workflowExecutionTimedOutEventAttributes"] - attrs.should.equal({"childPolicy": "ABANDON", "timeoutType": "START_TO_CLOSE"}) - # checks that event has been emitted at 14:00:00, not 14:00:30 - resp["events"][-1]["eventTimestamp"].should.equal(1420120800.0) - - # Workflow Execution Start to Close timeout # Default value in workflow helpers: 2 hours @mock_swf diff --git a/tests/test_swf/responses/test_workflow_executions.py b/tests/test_swf/responses/test_workflow_executions.py index e5e6a7b28..e56a9154f 100644 --- a/tests/test_swf/responses/test_workflow_executions.py +++ b/tests/test_swf/responses/test_workflow_executions.py @@ -1,34 +1,14 @@ -import boto import boto3 -from boto.swf.exceptions import SWFResponseError from botocore.exceptions import ClientError from datetime import datetime, timedelta import sure # noqa # pylint: disable=unused-import import pytest -from moto import mock_swf_deprecated, mock_swf +from moto import mock_swf from moto.core.utils import unix_time -# Utils -@mock_swf_deprecated -def setup_swf_environment(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60", description="A test domain") - conn.register_workflow_type( - "test-domain", - "test-workflow", - "v1.0", - task_list="queue", - default_child_policy="TERMINATE", - default_execution_start_to_close_timeout="300", - default_task_start_to_close_timeout="300", - ) - conn.register_activity_type("test-domain", "test-activity", "v1.1") - return conn - - def setup_swf_environment_boto3(): client = boto3.client("swf", region_name="us-west-1") client.register_domain( @@ -51,18 +31,6 @@ def setup_swf_environment_boto3(): return client -# StartWorkflowExecution endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_start_workflow_execution(): - conn = setup_swf_environment() - - wf = conn.start_workflow_execution( - "test-domain", "uid-abcd1234", "test-workflow", "v1.0" - ) - wf.should.contain("runId") - - # StartWorkflowExecution endpoint @mock_swf def test_start_workflow_execution_boto3(): @@ -76,24 +44,6 @@ def test_start_workflow_execution_boto3(): wf.should.have.key("runId") -# Has boto3 equivalent -@mock_swf_deprecated -def test_signal_workflow_execution(): - conn = setup_swf_environment() - hsh = conn.start_workflow_execution( - "test-domain", "uid-abcd1234", "test-workflow", "v1.0" - ) - run_id = hsh["runId"] - - wfe = conn.signal_workflow_execution( - "test-domain", "my_signal", "uid-abcd1234", "my_input", run_id - ) - - wfe = conn.describe_workflow_execution("test-domain", run_id, "uid-abcd1234") - - wfe["openCounts"]["openDecisionTasks"].should.equal(2) - - @mock_swf def test_signal_workflow_execution_boto3(): client = setup_swf_environment_boto3() @@ -119,19 +69,6 @@ def test_signal_workflow_execution_boto3(): wfe["openCounts"]["openDecisionTasks"].should.equal(2) -# Has boto3 equivalent -@mock_swf_deprecated -def test_start_already_started_workflow_execution(): - conn = setup_swf_environment() - conn.start_workflow_execution( - "test-domain", "uid-abcd1234", "test-workflow", "v1.0" - ) - - conn.start_workflow_execution.when.called_with( - "test-domain", "uid-abcd1234", "test-workflow", "v1.0" - ).should.throw(SWFResponseError) - - @mock_swf def test_start_already_started_workflow_execution_boto3(): client = setup_swf_environment_boto3() @@ -154,17 +91,6 @@ def test_start_already_started_workflow_execution_boto3(): ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) -# Has boto3 equivalent -@mock_swf_deprecated -def test_start_workflow_execution_on_deprecated_type(): - conn = setup_swf_environment() - conn.deprecate_workflow_type("test-domain", "test-workflow", "v1.0") - - conn.start_workflow_execution.when.called_with( - "test-domain", "uid-abcd1234", "test-workflow", "v1.0" - ).should.throw(SWFResponseError) - - @mock_swf def test_start_workflow_execution_on_deprecated_type_boto3(): client = setup_swf_environment_boto3() @@ -185,21 +111,6 @@ def test_start_workflow_execution_on_deprecated_type_boto3(): ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) -# DescribeWorkflowExecution endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_describe_workflow_execution(): - conn = setup_swf_environment() - hsh = conn.start_workflow_execution( - "test-domain", "uid-abcd1234", "test-workflow", "v1.0" - ) - run_id = hsh["runId"] - - wfe = conn.describe_workflow_execution("test-domain", run_id, "uid-abcd1234") - wfe["executionInfo"]["execution"]["workflowId"].should.equal("uid-abcd1234") - wfe["executionInfo"]["executionStatus"].should.equal("OPEN") - - # DescribeWorkflowExecution endpoint @mock_swf def test_describe_workflow_execution_boto3(): @@ -218,16 +129,6 @@ def test_describe_workflow_execution_boto3(): wfe["executionInfo"]["executionStatus"].should.equal("OPEN") -# Has boto3 equivalent -@mock_swf_deprecated -def test_describe_non_existent_workflow_execution(): - conn = setup_swf_environment() - - conn.describe_workflow_execution.when.called_with( - "test-domain", "wrong-run-id", "wrong-workflow-id" - ).should.throw(SWFResponseError) - - @mock_swf def test_describe_non_existent_workflow_execution_boto3(): client = setup_swf_environment_boto3() @@ -244,21 +145,6 @@ def test_describe_non_existent_workflow_execution_boto3(): ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) -# GetWorkflowExecutionHistory endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_get_workflow_execution_history(): - conn = setup_swf_environment() - hsh = conn.start_workflow_execution( - "test-domain", "uid-abcd1234", "test-workflow", "v1.0" - ) - run_id = hsh["runId"] - - resp = conn.get_workflow_execution_history("test-domain", run_id, "uid-abcd1234") - types = [evt["eventType"] for evt in resp["events"]] - types.should.equal(["WorkflowExecutionStarted", "DecisionTaskScheduled"]) - - # GetWorkflowExecutionHistory endpoint @mock_swf def test_get_workflow_execution_history_boto3(): @@ -277,22 +163,6 @@ def test_get_workflow_execution_history_boto3(): types.should.equal(["WorkflowExecutionStarted", "DecisionTaskScheduled"]) -# Has boto3 equivalent -@mock_swf_deprecated -def test_get_workflow_execution_history_with_reverse_order(): - conn = setup_swf_environment() - hsh = conn.start_workflow_execution( - "test-domain", "uid-abcd1234", "test-workflow", "v1.0" - ) - run_id = hsh["runId"] - - resp = conn.get_workflow_execution_history( - "test-domain", run_id, "uid-abcd1234", reverse_order=True - ) - types = [evt["eventType"] for evt in resp["events"]] - types.should.equal(["DecisionTaskScheduled", "WorkflowExecutionStarted"]) - - @mock_swf def test_get_workflow_execution_history_with_reverse_order_boto3(): client = setup_swf_environment_boto3() @@ -312,16 +182,6 @@ def test_get_workflow_execution_history_with_reverse_order_boto3(): types.should.equal(["DecisionTaskScheduled", "WorkflowExecutionStarted"]) -# Has boto3 equivalent -@mock_swf_deprecated -def test_get_workflow_execution_history_on_non_existent_workflow_execution(): - conn = setup_swf_environment() - - conn.get_workflow_execution_history.when.called_with( - "test-domain", "wrong-run-id", "wrong-workflow-id" - ).should.throw(SWFResponseError) - - @mock_swf def test_get_workflow_execution_history_on_non_existent_workflow_execution_boto3(): client = setup_swf_environment_boto3() @@ -338,45 +198,6 @@ def test_get_workflow_execution_history_on_non_existent_workflow_execution_boto3 ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) -# ListOpenWorkflowExecutions endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_list_open_workflow_executions(): - conn = setup_swf_environment() - # One open workflow execution - conn.start_workflow_execution( - "test-domain", "uid-abcd1234", "test-workflow", "v1.0" - ) - # One closed workflow execution to make sure it isn't displayed - run_id = conn.start_workflow_execution( - "test-domain", "uid-abcd12345", "test-workflow", "v1.0" - )["runId"] - conn.terminate_workflow_execution( - "test-domain", - "uid-abcd12345", - details="some details", - reason="a more complete reason", - run_id=run_id, - ) - - yesterday = datetime.utcnow() - timedelta(days=1) - oldest_date = unix_time(yesterday) - response = conn.list_open_workflow_executions( - "test-domain", oldest_date, workflow_id="test-workflow" - ) - execution_infos = response["executionInfos"] - len(execution_infos).should.equal(1) - open_workflow = execution_infos[0] - open_workflow["workflowType"].should.equal( - {"version": "v1.0", "name": "test-workflow"} - ) - open_workflow.should.contain("startTimestamp") - open_workflow["execution"]["workflowId"].should.equal("uid-abcd1234") - open_workflow["execution"].should.contain("runId") - open_workflow["cancelRequested"].should.be(False) - open_workflow["executionStatus"].should.equal("OPEN") - - # ListOpenWorkflowExecutions endpoint @mock_swf def test_list_open_workflow_executions_boto3(): @@ -421,45 +242,6 @@ def test_list_open_workflow_executions_boto3(): open_workflow["executionStatus"].should.equal("OPEN") -# ListClosedWorkflowExecutions endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_list_closed_workflow_executions(): - conn = setup_swf_environment() - # Leave one workflow execution open to make sure it isn't displayed - conn.start_workflow_execution( - "test-domain", "uid-abcd1234", "test-workflow", "v1.0" - ) - # One closed workflow execution - run_id = conn.start_workflow_execution( - "test-domain", "uid-abcd12345", "test-workflow", "v1.0" - )["runId"] - conn.terminate_workflow_execution( - "test-domain", - "uid-abcd12345", - details="some details", - reason="a more complete reason", - run_id=run_id, - ) - - yesterday = datetime.utcnow() - timedelta(days=1) - oldest_date = unix_time(yesterday) - response = conn.list_closed_workflow_executions( - "test-domain", start_oldest_date=oldest_date, workflow_id="test-workflow" - ) - execution_infos = response["executionInfos"] - len(execution_infos).should.equal(1) - open_workflow = execution_infos[0] - open_workflow["workflowType"].should.equal( - {"version": "v1.0", "name": "test-workflow"} - ) - open_workflow.should.contain("startTimestamp") - open_workflow["execution"]["workflowId"].should.equal("uid-abcd12345") - open_workflow["execution"].should.contain("runId") - open_workflow["cancelRequested"].should.be(False) - open_workflow["executionStatus"].should.equal("CLOSED") - - # ListClosedWorkflowExecutions endpoint @mock_swf def test_list_closed_workflow_executions_boto3(): @@ -504,33 +286,6 @@ def test_list_closed_workflow_executions_boto3(): open_workflow["executionStatus"].should.equal("CLOSED") -# TerminateWorkflowExecution endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_terminate_workflow_execution(): - conn = setup_swf_environment() - run_id = conn.start_workflow_execution( - "test-domain", "uid-abcd1234", "test-workflow", "v1.0" - )["runId"] - - resp = conn.terminate_workflow_execution( - "test-domain", - "uid-abcd1234", - details="some details", - reason="a more complete reason", - run_id=run_id, - ) - resp.should.be.none - - resp = conn.get_workflow_execution_history("test-domain", run_id, "uid-abcd1234") - evt = resp["events"][-1] - evt["eventType"].should.equal("WorkflowExecutionTerminated") - attrs = evt["workflowExecutionTerminatedEventAttributes"] - attrs["details"].should.equal("some details") - attrs["reason"].should.equal("a more complete reason") - attrs["cause"].should.equal("OPERATOR_INITIATED") - - # TerminateWorkflowExecution endpoint @mock_swf def test_terminate_workflow_execution_boto3(): @@ -560,42 +315,6 @@ def test_terminate_workflow_execution_boto3(): attrs["cause"].should.equal("OPERATOR_INITIATED") -# Has boto3 equivalent -@mock_swf_deprecated -def test_terminate_workflow_execution_with_wrong_workflow_or_run_id(): - conn = setup_swf_environment() - run_id = conn.start_workflow_execution( - "test-domain", "uid-abcd1234", "test-workflow", "v1.0" - )["runId"] - - # terminate workflow execution - conn.terminate_workflow_execution("test-domain", "uid-abcd1234") - - # already closed, with run_id - conn.terminate_workflow_execution.when.called_with( - "test-domain", "uid-abcd1234", run_id=run_id - ).should.throw( - SWFResponseError, "WorkflowExecution=[workflowId=uid-abcd1234, runId=" - ) - - # already closed, without run_id - conn.terminate_workflow_execution.when.called_with( - "test-domain", "uid-abcd1234" - ).should.throw(SWFResponseError, "Unknown execution, workflowId = uid-abcd1234") - - # wrong workflow id - conn.terminate_workflow_execution.when.called_with( - "test-domain", "uid-non-existent" - ).should.throw(SWFResponseError, "Unknown execution, workflowId = uid-non-existent") - - # wrong run_id - conn.terminate_workflow_execution.when.called_with( - "test-domain", "uid-abcd1234", run_id="foo" - ).should.throw( - SWFResponseError, "WorkflowExecution=[workflowId=uid-abcd1234, runId=" - ) - - @mock_swf def test_terminate_workflow_execution_with_wrong_workflow_or_run_id_boto3(): client = setup_swf_environment_boto3() diff --git a/tests/test_swf/responses/test_workflow_types.py b/tests/test_swf/responses/test_workflow_types.py index 91ff9ba6e..8ad8a7d9a 100644 --- a/tests/test_swf/responses/test_workflow_types.py +++ b/tests/test_swf/responses/test_workflow_types.py @@ -1,28 +1,11 @@ import sure # noqa # pylint: disable=unused-import -import boto import boto3 import pytest -from moto import mock_swf_deprecated from moto import mock_swf -from boto.swf.exceptions import SWFResponseError from botocore.exceptions import ClientError -# RegisterWorkflowType endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_register_workflow_type(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - conn.register_workflow_type("test-domain", "test-workflow", "v1.0") - - types = conn.list_workflow_types("test-domain", "REGISTERED") - actype = types["typeInfos"][0] - actype["workflowType"]["name"].should.equal("test-workflow") - actype["workflowType"]["version"].should.equal("v1.0") - - # RegisterWorkflowType endpoint @mock_swf def test_register_workflow_type_boto3(): @@ -42,18 +25,6 @@ def test_register_workflow_type_boto3(): actype["workflowType"]["version"].should.equal("v1.0") -# Has boto3 equivalent -@mock_swf_deprecated -def test_register_already_existing_workflow_type(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - conn.register_workflow_type("test-domain", "test-workflow", "v1.0") - - conn.register_workflow_type.when.called_with( - "test-domain", "test-workflow", "v1.0" - ).should.throw(SWFResponseError) - - @mock_swf def test_register_already_existing_workflow_type_boto3(): client = boto3.client("swf", region_name="us-east-1") @@ -74,35 +45,6 @@ def test_register_already_existing_workflow_type_boto3(): ) -# Has boto3 equivalent -@mock_swf_deprecated -def test_register_with_wrong_parameter_type(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - - conn.register_workflow_type.when.called_with( - "test-domain", "test-workflow", 12 - ).should.throw(SWFResponseError) - - -# ListWorkflowTypes endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_list_workflow_types(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - conn.register_workflow_type("test-domain", "b-test-workflow", "v1.0") - conn.register_workflow_type("test-domain", "a-test-workflow", "v1.0") - conn.register_workflow_type("test-domain", "c-test-workflow", "v1.0") - - all_workflow_types = conn.list_workflow_types("test-domain", "REGISTERED") - names = [ - activity_type["workflowType"]["name"] - for activity_type in all_workflow_types["typeInfos"] - ] - names.should.equal(["a-test-workflow", "b-test-workflow", "c-test-workflow"]) - - # ListWorkflowTypes endpoint @mock_swf def test_list_workflow_types_boto3(): @@ -130,25 +72,6 @@ def test_list_workflow_types_boto3(): names.should.equal(["a-test-workflow", "b-test-workflow", "c-test-workflow"]) -# Has boto3 equivalent -@mock_swf_deprecated -def test_list_workflow_types_reverse_order(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - conn.register_workflow_type("test-domain", "b-test-workflow", "v1.0") - conn.register_workflow_type("test-domain", "a-test-workflow", "v1.0") - conn.register_workflow_type("test-domain", "c-test-workflow", "v1.0") - - all_workflow_types = conn.list_workflow_types( - "test-domain", "REGISTERED", reverse_order=True - ) - names = [ - activity_type["workflowType"]["name"] - for activity_type in all_workflow_types["typeInfos"] - ] - names.should.equal(["c-test-workflow", "b-test-workflow", "a-test-workflow"]) - - # ListWorkflowTypes endpoint @mock_swf def test_list_workflow_types_reverse_order_boto3(): @@ -176,21 +99,6 @@ def test_list_workflow_types_reverse_order_boto3(): names.should.equal(["c-test-workflow", "b-test-workflow", "a-test-workflow"]) -# DeprecateWorkflowType endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_deprecate_workflow_type(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - conn.register_workflow_type("test-domain", "test-workflow", "v1.0") - conn.deprecate_workflow_type("test-domain", "test-workflow", "v1.0") - - actypes = conn.list_workflow_types("test-domain", "DEPRECATED") - actype = actypes["typeInfos"][0] - actype["workflowType"]["name"].should.equal("test-workflow") - actype["workflowType"]["version"].should.equal("v1.0") - - # DeprecateWorkflowType endpoint @mock_swf def test_deprecate_workflow_type_boto3(): @@ -213,19 +121,6 @@ def test_deprecate_workflow_type_boto3(): actype["workflowType"]["version"].should.equal("v1.0") -# Has boto3 equivalent -@mock_swf_deprecated -def test_deprecate_already_deprecated_workflow_type(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - conn.register_workflow_type("test-domain", "test-workflow", "v1.0") - conn.deprecate_workflow_type("test-domain", "test-workflow", "v1.0") - - conn.deprecate_workflow_type.when.called_with( - "test-domain", "test-workflow", "v1.0" - ).should.throw(SWFResponseError) - - @mock_swf def test_deprecate_already_deprecated_workflow_type_boto3(): client = boto3.client("swf", region_name="us-east-1") @@ -250,17 +145,6 @@ def test_deprecate_already_deprecated_workflow_type_boto3(): ) -# Has boto3 equivalent -@mock_swf_deprecated -def test_deprecate_non_existent_workflow_type(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - - conn.deprecate_workflow_type.when.called_with( - "test-domain", "non-existent", "v1.0" - ).should.throw(SWFResponseError) - - @mock_swf def test_deprecate_non_existent_workflow_type_boto3(): client = boto3.client("swf", region_name="us-east-1") @@ -351,29 +235,6 @@ def test_undeprecate_non_existent_workflow_type(): # DescribeWorkflowType endpoint -# Has boto3 equivalent -@mock_swf_deprecated -def test_describe_workflow_type(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - conn.register_workflow_type( - "test-domain", - "test-workflow", - "v1.0", - task_list="foo", - default_child_policy="TERMINATE", - ) - - actype = conn.describe_workflow_type("test-domain", "test-workflow", "v1.0") - actype["configuration"]["defaultTaskList"]["name"].should.equal("foo") - actype["configuration"]["defaultChildPolicy"].should.equal("TERMINATE") - actype["configuration"].keys().should_not.contain("defaultTaskStartToCloseTimeout") - infos = actype["typeInfo"] - infos["workflowType"]["name"].should.equal("test-workflow") - infos["workflowType"]["version"].should.equal("v1.0") - infos["status"].should.equal("REGISTERED") - - @mock_swf def test_describe_workflow_type_full_boto3(): # boto3 required as boto doesn't support all of the arguments @@ -409,17 +270,6 @@ def test_describe_workflow_type_full_boto3(): resp["configuration"]["defaultLambdaRole"].should.equal("arn:bar") -# Has boto3 equivalent -@mock_swf_deprecated -def test_describe_non_existent_workflow_type(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60") - - conn.describe_workflow_type.when.called_with( - "test-domain", "non-existent", "v1.0" - ).should.throw(SWFResponseError) - - @mock_swf def test_describe_non_existent_workflow_type_boto3(): client = boto3.client("swf", region_name="us-east-1") diff --git a/tests/test_swf/utils.py b/tests/test_swf/utils.py index c1cc25b65..0ba7177ac 100644 --- a/tests/test_swf/utils.py +++ b/tests/test_swf/utils.py @@ -1,4 +1,3 @@ -import boto import boto3 from moto.swf.models import ActivityType, Domain, WorkflowType, WorkflowExecution @@ -90,27 +89,6 @@ def auto_start_decision_tasks(wfe): return wfe -# Setup a complete example workflow and return the connection object -def setup_workflow(): - conn = boto.connect_swf("the_key", "the_secret") - conn.register_domain("test-domain", "60", description="A test domain") - conn = mock_basic_workflow_type("test-domain", conn) - conn.register_activity_type( - "test-domain", - "test-activity", - "v1.1", - default_task_heartbeat_timeout="600", - default_task_schedule_to_close_timeout="600", - default_task_schedule_to_start_timeout="600", - default_task_start_to_close_timeout="600", - ) - wfe = conn.start_workflow_execution( - "test-domain", "uid-abcd1234", "test-workflow", "v1.0" - ) - conn.run_id = wfe["runId"] - return conn - - # Setup a complete example workflow and return the connection object def setup_workflow_boto3(): client = boto3.client("swf", region_name="us-west-1")