1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347 |
- # -*- coding: utf-8 -*-
- # Import Python libs
- from __future__ import absolute_import, print_function, unicode_literals
- import datetime
- import logging
- import os
- import random
- import string
- import pytest
- # Import Salt libs
- import salt.config
- import salt.loader
- # Import Salt Libs
- import salt.states.boto_apigateway as boto_apigateway
- import salt.utils.files
- import salt.utils.yaml
- # Import 3rd-party libs
- from salt.ext.six.moves import range
- from salt.utils.versions import LooseVersion
- # Import Salt Testing libs
- from tests.support.mixins import LoaderModuleMockMixin
- from tests.support.mock import MagicMock, patch
- from tests.support.unit import TestCase, skipIf
- # pylint: disable=import-error,no-name-in-module
- from tests.unit.modules.test_boto_apigateway import BotoApiGatewayTestCaseMixin
- try:
- import boto3
- import botocore
- from botocore.exceptions import ClientError
- HAS_BOTO = True
- except ImportError:
- HAS_BOTO = False
- # pylint: enable=import-error,no-name-in-module
- # the boto_apigateway module relies on the connect_to_region() method
- # which was added in boto 2.8.0
- # https://github.com/boto/boto/commit/33ac26b416fbb48a60602542b4ce15dcc7029f12
- required_boto3_version = "1.2.1"
- required_botocore_version = "1.4.49"
- region = "us-east-1"
- access_key = "GKTADJGHEIQSXMKKRBJ08H"
- secret_key = "askdjghsdfjkghWupUjasdflkdfklgjsdfjajkghs"
- conn_parameters = {
- "region": region,
- "key": access_key,
- "keyid": secret_key,
- "profile": {},
- }
- error_message = (
- "An error occurred (101) when calling the {0} operation: Test-defined error"
- )
- error_content = {"Error": {"Code": 101, "Message": "Test-defined error"}}
- api_ret = dict(
- description='{\n "context": "See deployment or stage description",\n "provisioned_by": "Salt boto_apigateway.present State"\n}',
- createdDate=datetime.datetime(2015, 11, 17, 16, 33, 50),
- id="vni0vq8wzi",
- name="unit test api",
- )
- no_apis_ret = {"items": []}
- apis_ret = {"items": [api_ret]}
- mock_model_ret = dict(
- contentType="application/json",
- description="mock model",
- id="123abc",
- name="mock model",
- schema=(
- "{\n"
- ' "$schema": "http://json-schema.org/draft-04/schema#",\n'
- ' "properties": {\n'
- ' "field": {\n'
- ' "type": "string"\n'
- " }\n"
- " }\n"
- "}"
- ),
- )
- models_ret = {
- "items": [
- dict(
- contentType="application/json",
- description="Error",
- id="50nw8r",
- name="Error",
- schema=(
- "{\n"
- ' "$schema": "http://json-schema.org/draft-04/schema#",\n'
- ' "properties": {\n'
- ' "code": {\n'
- ' "format": "int32",\n'
- ' "type": "integer"\n'
- " },\n"
- ' "fields": {\n'
- ' "type": "string"\n'
- " },\n"
- ' "message": {\n'
- ' "type": "string"\n'
- " }\n"
- " },\n"
- ' "title": "Error Schema",\n'
- ' "type": "object"\n'
- "}"
- ),
- ),
- dict(
- contentType="application/json",
- description="User",
- id="terlnw",
- name="User",
- schema=(
- "{\n"
- ' "$schema": "http://json-schema.org/draft-04/schema#",\n'
- ' "properties": {\n'
- ' "password": {\n'
- ' "description": "A password for the new user",\n'
- ' "type": "string"\n'
- " },\n"
- ' "username": {\n'
- ' "description": "A unique username for the user",\n'
- ' "type": "string"\n'
- " }\n"
- " },\n"
- ' "title": "User Schema",\n'
- ' "type": "object"\n'
- "}"
- ),
- ),
- ]
- }
- root_resources_ret = {"items": [dict(id="bgk0rk8rqb", path="/")]}
- resources_ret = {
- "items": [
- dict(id="bgk0rk8rqb", path="/"),
- dict(
- id="9waiaz",
- parentId="bgk0rk8rqb",
- path="/users",
- pathPart="users",
- resourceMethods={"POST": {}},
- ),
- ]
- }
- no_resources_ret = {"items": []}
- stage1_deployment1_ret = dict(
- cacheClusterEnabled=False,
- cacheClusterSize=0.5,
- cacheClusterStatus="NOT_AVAILABLE",
- createdDate=datetime.datetime(2015, 11, 17, 16, 33, 50),
- deploymentId="kobnrb",
- description=(
- "{\n"
- ' "current_deployment_label": {\n'
- ' "api_name": "unit test api",\n'
- ' "swagger_file": "temp-swagger-sample.yaml",\n'
- ' "swagger_file_md5sum": "4fb17e43bab3a96e7f2410a1597cd0a5",\n'
- ' "swagger_info_object": {\n'
- ' "description": "salt boto apigateway unit test service",\n'
- ' "title": "salt boto apigateway unit test service",\n'
- ' "version": "0.0.0"\n'
- " }\n"
- " }\n"
- "}"
- ),
- lastUpdatedDate=datetime.datetime(2015, 11, 17, 16, 33, 50),
- methodSettings=dict(),
- stageName="test",
- variables=dict(),
- )
- stage1_deployment1_vars_ret = dict(
- cacheClusterEnabled=False,
- cacheClusterSize=0.5,
- cacheClusterStatus="NOT_AVAILABLE",
- createdDate=datetime.datetime(2015, 11, 17, 16, 33, 50),
- deploymentId="kobnrb",
- description=(
- "{\n"
- ' "current_deployment_label": {\n'
- ' "api_name": "unit test api",\n'
- ' "swagger_file": "temp-swagger-sample.yaml",\n'
- ' "swagger_file_md5sum": "4fb17e43bab3a96e7f2410a1597cd0a5",\n'
- ' "swagger_info_object": {\n'
- ' "description": "salt boto apigateway unit test service",\n'
- ' "title": "salt boto apigateway unit test service",\n'
- ' "version": "0.0.0"\n'
- " }\n"
- " }\n"
- "}"
- ),
- lastUpdatedDate=datetime.datetime(2015, 11, 17, 16, 33, 50),
- methodSettings=dict(),
- stageName="test",
- variables={"var1": "val1"},
- )
- stage1_deployment2_ret = dict(
- cacheClusterEnabled=False,
- cacheClusterSize=0.5,
- cacheClusterStatus="NOT_AVAILABLE",
- createdDate=datetime.datetime(2015, 11, 17, 16, 33, 50),
- deploymentId="kobnrc",
- description=(
- "{\n"
- ' "current_deployment_label": {\n'
- ' "api_name": "unit test api",\n'
- ' "swagger_file": "temp-swagger-sample.yaml",\n'
- ' "swagger_file_md5sum": "5fd538c4336ed5c54b4bf39ddf97c661",\n'
- ' "swagger_info_object": {\n'
- ' "description": "salt boto apigateway unit test service",\n'
- ' "title": "salt boto apigateway unit test service",\n'
- ' "version": "0.0.2"\n'
- " }\n"
- " }\n"
- "}"
- ),
- lastUpdatedDate=datetime.datetime(2015, 11, 17, 16, 33, 50),
- methodSettings=dict(),
- stageName="test",
- variables=dict(),
- )
- stage2_ret = dict(
- cacheClusterEnabled=False,
- cacheClusterSize=0.5,
- cacheClusterStatus="NOT_AVAILABLE",
- createdDate=datetime.datetime(2015, 11, 17, 16, 33, 50),
- deploymentId="kobnrb",
- description=(
- "{\n"
- ' "current_deployment_label": {\n'
- ' "api_name": "unit test api",\n'
- ' "swagger_file": "temp-swagger-sample.yaml",\n'
- ' "swagger_file_md5sum": "4fb17e43bab3a96e7f2410a1597cd0a5",\n'
- ' "swagger_info_object": {\n'
- ' "description": "salt boto apigateway unit test service",\n'
- ' "title": "salt boto apigateway unit test service",\n'
- ' "version": "0.0.0"\n'
- " }\n"
- " }\n"
- "}"
- ),
- lastUpdatedDate=datetime.datetime(2015, 11, 17, 16, 33, 50),
- methodSettings=dict(),
- stageName="dev",
- variables=dict(),
- )
- stages_stage2_ret = {"item": [stage2_ret]}
- no_stages_ret = {"item": []}
- deployment1_ret = dict(
- createdDate=datetime.datetime(2015, 11, 17, 16, 33, 50),
- description=(
- "{\n"
- ' "api_name": "unit test api",\n'
- ' "swagger_file": "temp-swagger-sample.yaml",\n'
- ' "swagger_file_md5sum": "55a948ff90ad80ff747ec91657c7a299",\n'
- ' "swagger_info_object": {\n'
- ' "description": "salt boto apigateway unit test service",\n'
- ' "title": "salt boto apigateway unit test service",\n'
- ' "version": "0.0.0"\n'
- " }\n"
- "}"
- ),
- id="kobnrb",
- )
- deployment2_ret = dict(
- createdDate=datetime.datetime(2015, 11, 17, 16, 33, 50),
- description=(
- "{\n"
- ' "api_name": "unit test api",\n'
- ' "swagger_file": "temp-swagger-sample.yaml",\n'
- ' "swagger_file_md5sum": "5fd538c4336ed5c54b4bf39ddf97c661",\n'
- ' "swagger_info_object": {\n'
- ' "description": "salt boto apigateway unit test service",\n'
- ' "title": "salt boto apigateway unit test service",\n'
- ' "version": "0.0.2"\n'
- " }\n"
- "}"
- ),
- id="kobnrc",
- )
- deployments_ret = {"items": [deployment1_ret, deployment2_ret]}
- function_ret = dict(
- FunctionName="unit_test_api_users_post",
- Runtime="python2.7",
- Role=None,
- Handler="handler",
- Description="abcdefg",
- Timeout=5,
- MemorySize=128,
- CodeSha256="abcdef",
- CodeSize=199,
- FunctionArn="arn:lambda:us-east-1:1234:Something",
- LastModified="yes",
- )
- method_integration_response_200_ret = dict(
- responseParameters={"method.response.header.Access-Control-Allow-Origin": "*"},
- responseTemplates={},
- selectionPattern=".*",
- statusCode="200",
- )
- method_integration_ret = dict(
- cacheKeyParameters={},
- cacheNamespace="9waiaz",
- credentials="arn:aws:iam::1234:role/apigatewayrole",
- httpMethod="POST",
- integrationResponses={"200": method_integration_response_200_ret},
- requestParameters={},
- requestTemplates={
- "application/json": (
- "#set($inputRoot = $input.path('$'))"
- "{"
- '"header-params" : {#set ($map = $input.params().header)#foreach( $param in $map.entrySet() )"$param.key" : "$param.value" #if( $foreach.hasNext ), #end#end},'
- '"query-params" : {#set ($map = $input.params().querystring)#foreach( $param in $map.entrySet() )"$param.key" : "$param.value" #if( $foreach.hasNext ), #end#end},'
- '"path-params" : {#set ($map = $input.params().path)#foreach( $param in $map.entrySet() )"$param.key" : "$param.value" #if( $foreach.hasNext ), #end#end},'
- "\"body-params\" : $input.json('$')"
- "}"
- )
- },
- type="AWS",
- uri=(
- "arn:aws:apigateway:us-west-2:"
- "lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:1234567:"
- "function:unit_test_api_api_users_post/invocations"
- ),
- )
- method_response_200_ret = dict(
- responseModels={"application/json": "User"},
- responseParameters={"method.response.header.Access-Control-Allow-Origin": False},
- statusCode="200",
- )
- method_ret = dict(
- apiKeyRequired=False,
- authorizationType="None",
- httpMethod="POST",
- methodIntegration=method_integration_ret,
- methodResponses={"200": method_response_200_ret},
- requestModels={"application/json": "User"},
- requestParameters={},
- )
- throttle_rateLimit = 10.0
- association_stage_1 = {"apiId": "apiId1", "stage": "stage1"}
- association_stage_2 = {"apiId": "apiId1", "stage": "stage2"}
- log = logging.getLogger(__name__)
- def _has_required_boto():
- """
- Returns True/False boolean depending on if Boto is installed and correct
- version.
- """
- if not HAS_BOTO:
- return False
- elif LooseVersion(boto3.__version__) < LooseVersion(required_boto3_version):
- return False
- else:
- return True
- def _has_required_botocore():
- """
- Returns True/False boolean depending on if botocore supports usage plan
- """
- if not HAS_BOTO:
- return False
- elif LooseVersion(botocore.__version__) < LooseVersion(required_botocore_version):
- return False
- else:
- return True
- class TempSwaggerFile(object):
- _tmp_swagger_dict = {
- "info": {
- "version": "0.0.0",
- "description": "salt boto apigateway unit test service",
- "title": "salt boto apigateway unit test service",
- },
- "paths": {
- "/users": {
- "post": {
- "responses": {
- "200": {
- "headers": {
- "Access-Control-Allow-Origin": {"type": "string"}
- },
- "description": "The username of the new user",
- "schema": {"$ref": "#/definitions/User"},
- }
- },
- "parameters": [
- {
- "in": "body",
- "description": "New user details.",
- "name": "NewUser",
- "schema": {"$ref": "#/definitions/User"},
- }
- ],
- "produces": ["application/json"],
- "description": "Creates a new user.",
- "tags": ["Auth"],
- "consumes": ["application/json"],
- "summary": "Registers a new user",
- }
- }
- },
- "schemes": ["https"],
- "produces": ["application/json"],
- "basePath": "/api",
- "host": "rm06h9oac4.execute-api.us-west-2.amazonaws.com",
- "definitions": {
- "User": {
- "properties": {
- "username": {
- "type": "string",
- "description": "A unique username for the user",
- },
- "password": {
- "type": "string",
- "description": "A password for the new user",
- },
- }
- },
- "Error": {
- "properties": {
- "fields": {"type": "string"},
- "message": {"type": "string"},
- "code": {"type": "integer", "format": "int32"},
- }
- },
- },
- "swagger": "2.0",
- }
- def __enter__(self):
- self.swaggerfile = "temp-swagger-sample.yaml"
- with salt.utils.files.fopen(self.swaggerfile, "w") as fp_:
- salt.utils.yaml.safe_dump(self.swaggerdict, fp_, default_flow_style=False)
- return self.swaggerfile
- def __exit__(self, objtype, value, traceback):
- os.remove(self.swaggerfile)
- def __init__(self, create_invalid_file=False):
- if create_invalid_file:
- self.swaggerdict = TempSwaggerFile._tmp_swagger_dict.copy()
- # add an invalid top level key
- self.swaggerdict["invalid_key"] = "invalid"
- # remove one of the required keys 'schemes'
- self.swaggerdict.pop("schemes", None)
- # set swagger version to an unsupported version 3.0
- self.swaggerdict["swagger"] = "3.0"
- # missing info object
- self.swaggerdict.pop("info", None)
- else:
- self.swaggerdict = TempSwaggerFile._tmp_swagger_dict
- class BotoApiGatewayStateTestCaseBase(TestCase, LoaderModuleMockMixin):
- conn = None
- @classmethod
- def setUpClass(cls):
- cls.opts = salt.config.DEFAULT_MINION_OPTS.copy()
- cls.opts["grains"] = salt.loader.grains(cls.opts)
- @classmethod
- def tearDownClass(cls):
- del cls.opts
- def setup_loader_modules(self):
- context = {}
- utils = salt.loader.utils(
- self.opts,
- whitelist=["boto", "boto3", "args", "systemd", "path", "platform", "reg"],
- context=context,
- )
- serializers = salt.loader.serializers(self.opts)
- self.funcs = salt.loader.minion_mods(
- self.opts, context=context, utils=utils, whitelist=["boto_apigateway"]
- )
- self.salt_states = salt.loader.states(
- opts=self.opts,
- functions=self.funcs,
- utils=utils,
- whitelist=["boto_apigateway"],
- serializers=serializers,
- )
- return {
- boto_apigateway: {
- "__opts__": self.opts,
- "__utils__": utils,
- "__salt__": self.funcs,
- "__states__": self.salt_states,
- "__serializers__": serializers,
- }
- }
- # Set up MagicMock to replace the boto3 session
- def setUp(self):
- self.addCleanup(delattr, self, "funcs")
- self.addCleanup(delattr, self, "salt_states")
- # connections keep getting cached from prior tests, can't find the
- # correct context object to clear it. So randomize the cache key, to prevent any
- # cache hits
- conn_parameters["key"] = "".join(
- random.choice(string.ascii_lowercase + string.digits) for _ in range(50)
- )
- patcher = patch("boto3.session.Session")
- self.addCleanup(patcher.stop)
- mock_session = patcher.start()
- session_instance = mock_session.return_value
- self.conn = MagicMock()
- self.addCleanup(delattr, self, "conn")
- session_instance.client.return_value = self.conn
- @skipIf(HAS_BOTO is False, "The boto module must be installed.")
- @skipIf(
- _has_required_boto() is False,
- "The boto3 module must be greater than"
- " or equal to version {0}".format(required_boto3_version),
- )
- class BotoApiGatewayTestCase(
- BotoApiGatewayStateTestCaseBase, BotoApiGatewayTestCaseMixin
- ):
- """
- TestCase for salt.modules.boto_apigateway state.module
- """
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_present_when_swagger_file_is_invalid(self):
- """
- Tests present when the swagger file is invalid.
- """
- result = {}
- with TempSwaggerFile(create_invalid_file=True) as swagger_file:
- result = self.salt_states["boto_apigateway.present"](
- "api present",
- "unit test api",
- swagger_file,
- "test",
- False,
- "arn:aws:iam::1234:role/apigatewayrole",
- **conn_parameters
- )
- self.assertFalse(result.get("result", True))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_present_when_stage_is_already_at_desired_deployment(self):
- """
- Tests scenario where no action will be taken since we're already
- at desired state
- """
- self.conn.get_rest_apis.return_value = apis_ret
- self.conn.get_deployment.return_value = deployment1_ret
- self.conn.get_stage.return_value = stage1_deployment1_ret
- self.conn.update_stage.side_effect = ClientError(
- error_content, "update_stage should not be called"
- )
- result = {}
- with TempSwaggerFile() as swagger_file:
- result = self.salt_states["boto_apigateway.present"](
- "api present",
- "unit test api",
- swagger_file,
- "test",
- False,
- "arn:aws:iam::1234:role/apigatewayrole",
- **conn_parameters
- )
- self.assertFalse(result.get("abort"))
- self.assertTrue(result.get("current"))
- self.assertIs(result.get("result"), True)
- self.assertNotIn("update_stage should not be called", result.get("comment", ""))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_present_when_stage_is_already_at_desired_deployment_and_needs_stage_variables_update(
- self,
- ):
- """
- Tests scenario where the deployment is current except for the need to update stage variables
- from {} to {'var1':'val1'}
- """
- self.conn.get_rest_apis.return_value = apis_ret
- self.conn.get_deployment.return_value = deployment1_ret
- self.conn.get_stage.return_value = stage1_deployment1_ret
- self.conn.update_stage.return_value = stage1_deployment1_vars_ret
- result = {}
- with TempSwaggerFile() as swagger_file:
- result = self.salt_states["boto_apigateway.present"](
- "api present",
- "unit test api",
- swagger_file,
- "test",
- False,
- "arn:aws:iam::1234:role/apigatewayrole",
- stage_variables={"var1": "val1"},
- **conn_parameters
- )
- self.assertFalse(result.get("abort"))
- self.assertTrue(result.get("current"))
- self.assertIs(result.get("result"), True)
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_present_when_stage_exists_and_is_to_associate_to_existing_deployment(self):
- """
- Tests scenario where we merely reassociate a stage to a pre-existing
- deployments
- """
- self.conn.get_rest_apis.return_value = apis_ret
- self.conn.get_deployment.return_value = deployment2_ret
- self.conn.get_deployments.return_value = deployments_ret
- self.conn.get_stage.return_value = stage1_deployment2_ret
- self.conn.update_stage.return_value = stage1_deployment1_ret
- # should never get to the following calls
- self.conn.create_stage.side_effect = ClientError(error_content, "create_stage")
- self.conn.create_deployment.side_effect = ClientError(
- error_content, "create_deployment"
- )
- result = {}
- with TempSwaggerFile() as swagger_file:
- result = self.salt_states["boto_apigateway.present"](
- "api present",
- "unit test api",
- swagger_file,
- "test",
- False,
- "arn:aws:iam::1234:role/apigatewayrole",
- **conn_parameters
- )
- self.assertTrue(result.get("publish"))
- self.assertIs(result.get("result"), True)
- self.assertFalse(result.get("abort"))
- self.assertTrue(result.get("changes", {}).get("new", [{}])[0])
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_present_when_stage_is_to_associate_to_new_deployment(self):
- """
- Tests creation of a new api/model/resource given nothing has been created previously
- """
- # no api existed
- self.conn.get_rest_apis.return_value = no_apis_ret
- # create top level api
- self.conn.create_rest_api.return_value = api_ret
- # no models existed in the newly created api
- self.conn.get_model.side_effect = ClientError(error_content, "get_model")
- # create model return values
- self.conn.create_model.return_value = mock_model_ret
- # various paths/resources already created
- self.conn.get_resources.return_value = resources_ret
- # the following should never be called
- self.conn.create_resource.side_effect = ClientError(
- error_content, "create_resource"
- )
- # create api method for POST
- self.conn.put_method.return_value = method_ret
- # create api method integration for POST
- self.conn.put_integration.return_value = method_integration_ret
- # create api method response for POST/200
- self.conn.put_method_response.return_value = method_response_200_ret
- # create api method integration response for POST
- self.conn.put_intgration_response.return_value = (
- method_integration_response_200_ret
- )
- result = {}
- with patch.dict(
- self.funcs,
- {
- "boto_lambda.describe_function": MagicMock(
- return_value={"function": function_ret}
- )
- },
- ):
- with TempSwaggerFile() as swagger_file:
- result = self.salt_states["boto_apigateway.present"](
- "api present",
- "unit test api",
- swagger_file,
- "test",
- False,
- "arn:aws:iam::1234:role/apigatewayrole",
- **conn_parameters
- )
- self.assertIs(result.get("result"), True)
- self.assertIs(result.get("abort"), None)
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_present_when_stage_associating_to_new_deployment_errored_on_api_creation(
- self,
- ):
- """
- Tests creation of a new api/model/resource given nothing has been created previously,
- and we failed on creating the top level api object.
- """
- # no api existed
- self.conn.get_rest_apis.return_value = no_apis_ret
- # create top level api
- self.conn.create_rest_api.side_effect = ClientError(
- error_content, "create_rest_api"
- )
- result = {}
- with TempSwaggerFile() as swagger_file:
- result = self.salt_states["boto_apigateway.present"](
- "api present",
- "unit test api",
- swagger_file,
- "test",
- False,
- "arn:aws:iam::1234:role/apigatewayrole",
- **conn_parameters
- )
- self.assertIs(result.get("abort"), True)
- self.assertIs(result.get("result"), False)
- self.assertIn("create_rest_api", result.get("comment", ""))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_present_when_stage_associating_to_new_deployment_errored_on_model_creation(
- self,
- ):
- """
- Tests creation of a new api/model/resource given nothing has been created previously,
- and we failed on creating the models after successful creation of top level api object.
- """
- # no api existed
- self.conn.get_rest_apis.return_value = no_apis_ret
- # create top level api
- self.conn.create_rest_api.return_value = api_ret
- # no models existed in the newly created api
- self.conn.get_model.side_effect = ClientError(error_content, "get_model")
- # create model return error
- self.conn.create_model.side_effect = ClientError(error_content, "create_model")
- result = {}
- with TempSwaggerFile() as swagger_file:
- result = self.salt_states["boto_apigateway.present"](
- "api present",
- "unit test api",
- swagger_file,
- "test",
- False,
- "arn:aws:iam::1234:role/apigatewayrole",
- **conn_parameters
- )
- self.assertIs(result.get("abort"), True)
- self.assertIs(result.get("result"), False)
- self.assertIn("create_model", result.get("comment", ""))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_present_when_stage_associating_to_new_deployment_errored_on_resource_creation(
- self,
- ):
- """
- Tests creation of a new api/model/resource given nothing has been created previously,
- and we failed on creating the resource (paths) after successful creation of top level api/model
- objects.
- """
- # no api existed
- self.conn.get_rest_apis.return_value = no_apis_ret
- # create top level api
- self.conn.create_rest_api.return_value = api_ret
- # no models existed in the newly created api
- self.conn.get_model.side_effect = ClientError(error_content, "get_model")
- # create model return values
- self.conn.create_model.return_value = mock_model_ret
- # get resources has nothing intiially except to the root path '/'
- self.conn.get_resources.return_value = root_resources_ret
- # create resources return error
- self.conn.create_resource.side_effect = ClientError(
- error_content, "create_resource"
- )
- result = {}
- with TempSwaggerFile() as swagger_file:
- result = self.salt_states["boto_apigateway.present"](
- "api present",
- "unit test api",
- swagger_file,
- "test",
- False,
- "arn:aws:iam::1234:role/apigatewayrole",
- **conn_parameters
- )
- self.assertIs(result.get("abort"), True)
- self.assertIs(result.get("result"), False)
- self.assertIn("create_resource", result.get("comment", ""))
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_present_when_stage_associating_to_new_deployment_errored_on_put_method(
- self,
- ):
- """
- Tests creation of a new api/model/resource given nothing has been created previously,
- and we failed on adding a post method to the resource after successful creation of top level
- api, model, resource objects.
- """
- # no api existed
- self.conn.get_rest_apis.return_value = no_apis_ret
- # create top level api
- self.conn.create_rest_api.return_value = api_ret
- # no models existed in the newly created api
- self.conn.get_model.side_effect = ClientError(error_content, "get_model")
- # create model return values
- self.conn.create_model.return_value = mock_model_ret
- # various paths/resources already created
- self.conn.get_resources.return_value = resources_ret
- # the following should never be called
- self.conn.create_resource.side_effect = ClientError(
- error_content, "create_resource"
- )
- # create api method for POST
- self.conn.put_method.side_effect = ClientError(error_content, "put_method")
- result = {}
- with patch.dict(
- self.funcs,
- {
- "boto_lambda.describe_function": MagicMock(
- return_value={"function": function_ret}
- )
- },
- ):
- with TempSwaggerFile() as swagger_file:
- result = self.salt_states["boto_apigateway.present"](
- "api present",
- "unit test api",
- swagger_file,
- "test",
- False,
- "arn:aws:iam::1234:role/apigatewayrole",
- **conn_parameters
- )
- self.assertIs(result.get("abort"), True)
- self.assertIs(result.get("result"), False)
- self.assertIn("put_method", result.get("comment", ""))
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_present_when_stage_associating_to_new_deployment_errored_on_lambda_function_lookup(
- self,
- ):
- """
- Tests creation of a new api/model/resource given nothing has been created previously,
- and we failed on adding a post method due to a lamda look up failure after successful
- creation of top level api, model, resource objects.
- """
- # no api existed
- self.conn.get_rest_apis.return_value = no_apis_ret
- # create top level api
- self.conn.create_rest_api.return_value = api_ret
- # no models existed in the newly created api
- self.conn.get_model.side_effect = ClientError(error_content, "get_model")
- # create model return values
- self.conn.create_model.return_value = mock_model_ret
- # various paths/resources already created
- self.conn.get_resources.return_value = resources_ret
- # the following should never be called
- self.conn.create_resource.side_effect = ClientError(
- error_content, "create_resource"
- )
- # create api method for POST
- self.conn.put_method.return_value = method_ret
- # create api method integration for POST
- self.conn.put_integration.side_effect = ClientError(
- error_content, "put_integration should not be invoked"
- )
- result = {}
- with patch.dict(
- self.funcs,
- {
- "boto_lambda.describe_function": MagicMock(
- return_value={"error": "no such lambda"}
- )
- },
- ):
- with TempSwaggerFile() as swagger_file:
- result = self.salt_states["boto_apigateway.present"](
- "api present",
- "unit test api",
- swagger_file,
- "test",
- False,
- "arn:aws:iam::1234:role/apigatewayrole",
- **conn_parameters
- )
- self.assertIs(result.get("result"), False)
- self.assertNotIn(
- "put_integration should not be invoked", result.get("comment", "")
- )
- self.assertIn("not find lambda function", result.get("comment", ""))
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_present_when_stage_associating_to_new_deployment_errored_on_put_integration(
- self,
- ):
- """
- Tests creation of a new api/model/resource given nothing has been created previously,
- and we failed on adding an integration for the post method to the resource after
- successful creation of top level api, model, resource objects.
- """
- # no api existed
- self.conn.get_rest_apis.return_value = no_apis_ret
- # create top level api
- self.conn.create_rest_api.return_value = api_ret
- # no models existed in the newly created api
- self.conn.get_model.side_effect = ClientError(error_content, "get_model")
- # create model return values
- self.conn.create_model.return_value = mock_model_ret
- # various paths/resources already created
- self.conn.get_resources.return_value = resources_ret
- # the following should never be called
- self.conn.create_resource.side_effect = ClientError(
- error_content, "create_resource"
- )
- # create api method for POST
- self.conn.put_method.return_value = method_ret
- # create api method integration for POST
- self.conn.put_integration.side_effect = ClientError(
- error_content, "put_integration"
- )
- result = {}
- with patch.dict(
- self.funcs,
- {
- "boto_lambda.describe_function": MagicMock(
- return_value={"function": function_ret}
- )
- },
- ):
- with TempSwaggerFile() as swagger_file:
- result = self.salt_states["boto_apigateway.present"](
- "api present",
- "unit test api",
- swagger_file,
- "test",
- False,
- "arn:aws:iam::1234:role/apigatewayrole",
- **conn_parameters
- )
- self.assertIs(result.get("abort"), True)
- self.assertIs(result.get("result"), False)
- self.assertIn("put_integration", result.get("comment", ""))
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_present_when_stage_associating_to_new_deployment_errored_on_put_method_response(
- self,
- ):
- """
- Tests creation of a new api/model/resource given nothing has been created previously,
- and we failed on adding a method response for the post method to the resource after
- successful creation of top level api, model, resource objects.
- """
- # no api existed
- self.conn.get_rest_apis.return_value = no_apis_ret
- # create top level api
- self.conn.create_rest_api.return_value = api_ret
- # no models existed in the newly created api
- self.conn.get_model.side_effect = ClientError(error_content, "get_model")
- # create model return values
- self.conn.create_model.return_value = mock_model_ret
- # various paths/resources already created
- self.conn.get_resources.return_value = resources_ret
- # the following should never be called
- self.conn.create_resource.side_effect = ClientError(
- error_content, "create_resource"
- )
- # create api method for POST
- self.conn.put_method.return_value = method_ret
- # create api method integration for POST
- self.conn.put_integration.return_value = method_integration_ret
- # create api method response for POST/200
- self.conn.put_method_response.side_effect = ClientError(
- error_content, "put_method_response"
- )
- result = {}
- with patch.dict(
- self.funcs,
- {
- "boto_lambda.describe_function": MagicMock(
- return_value={"function": function_ret}
- )
- },
- ):
- with TempSwaggerFile() as swagger_file:
- result = self.salt_states["boto_apigateway.present"](
- "api present",
- "unit test api",
- swagger_file,
- "test",
- False,
- "arn:aws:iam::1234:role/apigatewayrole",
- **conn_parameters
- )
- self.assertIs(result.get("abort"), True)
- self.assertIs(result.get("result"), False)
- self.assertIn("put_method_response", result.get("comment", ""))
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_present_when_stage_associating_to_new_deployment_errored_on_put_integration_response(
- self,
- ):
- """
- Tests creation of a new api/model/resource given nothing has been created previously,
- and we failed on adding an integration response for the post method to the resource after
- successful creation of top level api, model, resource objects.
- """
- # no api existed
- self.conn.get_rest_apis.return_value = no_apis_ret
- # create top level api
- self.conn.create_rest_api.return_value = api_ret
- # no models existed in the newly created api
- self.conn.get_model.side_effect = ClientError(error_content, "get_model")
- # create model return values
- self.conn.create_model.return_value = mock_model_ret
- # various paths/resources already created
- self.conn.get_resources.return_value = resources_ret
- # the following should never be called
- self.conn.create_resource.side_effect = ClientError(
- error_content, "create_resource"
- )
- # create api method for POST
- self.conn.put_method.return_value = method_ret
- # create api method integration for POST
- self.conn.put_integration.return_value = method_integration_ret
- # create api method response for POST/200
- self.conn.put_method_response.return_value = method_response_200_ret
- # create api method integration response for POST
- self.conn.put_integration_response.side_effect = ClientError(
- error_content, "put_integration_response"
- )
- result = {}
- with patch.dict(
- self.funcs,
- {
- "boto_lambda.describe_function": MagicMock(
- return_value={"function": function_ret}
- )
- },
- ):
- with TempSwaggerFile() as swagger_file:
- result = self.salt_states["boto_apigateway.present"](
- "api present",
- "unit test api",
- swagger_file,
- "test",
- False,
- "arn:aws:iam::1234:role/apigatewayrole",
- **conn_parameters
- )
- self.assertIs(result.get("abort"), True)
- self.assertIs(result.get("result"), False)
- self.assertIn("put_integration_response", result.get("comment", ""))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_absent_when_rest_api_does_not_exist(self):
- """
- Tests scenario where the given api_name does not exist, absent state should return True
- with no changes.
- """
- self.conn.get_rest_apis.return_value = apis_ret
- self.conn.get_stage.side_effect = ClientError(
- error_content, "get_stage should not be called"
- )
- result = self.salt_states["boto_apigateway.absent"](
- "api present",
- "no_such_rest_api",
- "no_such_stage",
- nuke_api=False,
- **conn_parameters
- )
- self.assertIs(result.get("result"), True)
- self.assertNotIn("get_stage should not be called", result.get("comment", ""))
- self.assertEqual(result.get("changes"), {})
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_absent_when_stage_is_invalid(self):
- """
- Tests scenario where the stagename doesn't exist
- """
- self.conn.get_rest_apis.return_value = apis_ret
- self.conn.get_stage.return_value = stage1_deployment1_ret
- self.conn.delete_stage.side_effect = ClientError(error_content, "delete_stage")
- result = self.salt_states["boto_apigateway.absent"](
- "api present",
- "unit test api",
- "no_such_stage",
- nuke_api=False,
- **conn_parameters
- )
- self.assertTrue(result.get("abort", False))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_absent_when_stage_is_valid_and_only_one_stage_is_associated_to_deployment(
- self,
- ):
- """
- Tests scenario where the stagename exists
- """
- self.conn.get_rest_apis.return_value = apis_ret
- self.conn.get_stage.return_value = stage1_deployment1_ret
- self.conn.delete_stage.return_value = {
- "ResponseMetadata": {
- "HTTPStatusCode": 200,
- "RequestId": "2d31072c-9d15-11e5-9977-6d9fcfda9c0a",
- }
- }
- self.conn.get_stages.return_value = no_stages_ret
- self.conn.delete_deployment.return_value = {
- "ResponseMetadata": {
- "HTTPStatusCode": 200,
- "RequestId": "2d31072c-9d15-11e5-9977-6d9fcfda9c0a",
- }
- }
- result = self.salt_states["boto_apigateway.absent"](
- "api present", "unit test api", "test", nuke_api=False, **conn_parameters
- )
- self.assertTrue(result.get("result", False))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_absent_when_stage_is_valid_and_two_stages_are_associated_to_deployment(
- self,
- ):
- """
- Tests scenario where the stagename exists and there are two stages associated with same deployment
- """
- self.conn.get_rest_apis.return_value = apis_ret
- self.conn.get_stage.return_value = stage1_deployment1_ret
- self.conn.delete_stage.return_value = {
- "ResponseMetadata": {
- "HTTPStatusCode": 200,
- "RequestId": "2d31072c-9d15-11e5-9977-6d9fcfda9c0a",
- }
- }
- self.conn.get_stages.return_value = stages_stage2_ret
- result = self.salt_states["boto_apigateway.absent"](
- "api present", "unit test api", "test", nuke_api=False, **conn_parameters
- )
- self.assertTrue(result.get("result", False))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_absent_when_failing_to_delete_a_deployment_no_longer_associated_with_any_stages(
- self,
- ):
- """
- Tests scenario where stagename exists and is deleted, but a failure occurs when trying to delete
- the deployment which is no longer associated to any other stages
- """
- self.conn.get_rest_apis.return_value = apis_ret
- self.conn.get_stage.return_value = stage1_deployment1_ret
- self.conn.delete_stage.return_value = {
- "ResponseMetadata": {
- "HTTPStatusCode": 200,
- "RequestId": "2d31072c-9d15-11e5-9977-6d9fcfda9c0a",
- }
- }
- self.conn.get_stages.return_value = no_stages_ret
- self.conn.delete_deployment.side_effect = ClientError(
- error_content, "delete_deployment"
- )
- result = self.salt_states["boto_apigateway.absent"](
- "api present", "unit test api", "test", nuke_api=False, **conn_parameters
- )
- self.assertTrue(result.get("abort", False))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_absent_when_nuke_api_and_no_more_stages_deployments_remain(self):
- """
- Tests scenario where the stagename exists and there are no stages associated with same deployment,
- the api would be deleted.
- """
- self.conn.get_rest_apis.return_value = apis_ret
- self.conn.get_stage.return_value = stage1_deployment1_ret
- self.conn.delete_stage.return_value = {
- "ResponseMetadata": {
- "HTTPStatusCode": 200,
- "RequestId": "2d31072c-9d15-11e5-9977-6d9fcfda9c0a",
- }
- }
- self.conn.get_stages.return_value = no_stages_ret
- self.conn.get_deployments.return_value = deployments_ret
- self.conn.delete_rest_api.return_value = {
- "ResponseMetadata": {
- "HTTPStatusCode": 200,
- "RequestId": "2d31072c-9d15-11e5-9977-6d9fcfda9c0a",
- }
- }
- result = self.salt_states["boto_apigateway.absent"](
- "api present", "unit test api", "test", nuke_api=True, **conn_parameters
- )
- self.assertIs(result.get("result"), True)
- self.assertIsNot(result.get("abort"), True)
- self.assertIs(
- result.get("changes", {})
- .get("new", [{}])[0]
- .get("delete_api", {})
- .get("deleted"),
- True,
- )
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_absent_when_nuke_api_and_other_stages_deployments_exist(self):
- """
- Tests scenario where the stagename exists and there are two stages associated with same deployment,
- though nuke_api is requested, due to remaining deployments, we will not call the delete_rest_api call.
- """
- self.conn.get_rest_apis.return_value = apis_ret
- self.conn.get_stage.return_value = stage1_deployment1_ret
- self.conn.delete_stage.return_value = {
- "ResponseMetadata": {
- "HTTPStatusCode": 200,
- "RequestId": "2d31072c-9d15-11e5-9977-6d9fcfda9c0a",
- }
- }
- self.conn.get_stages.return_value = stages_stage2_ret
- self.conn.get_deployments.return_value = deployments_ret
- self.conn.delete_rest_api.side_effect = ClientError(
- error_content, "unexpected_api_delete"
- )
- result = self.salt_states["boto_apigateway.absent"](
- "api present", "unit test api", "test", nuke_api=True, **conn_parameters
- )
- self.assertIs(result.get("result"), True)
- self.assertIsNot(result.get("abort"), True)
- @skipIf(HAS_BOTO is False, "The boto module must be installed.")
- @skipIf(
- _has_required_boto() is False,
- "The boto3 module must be greater than"
- " or equal to version {0}".format(required_boto3_version),
- )
- @skipIf(
- _has_required_botocore() is False,
- "The botocore module must be greater than"
- " or equal to version {0}".format(required_botocore_version),
- )
- class BotoApiGatewayUsagePlanTestCase(
- BotoApiGatewayStateTestCaseBase, BotoApiGatewayTestCaseMixin
- ):
- """
- TestCase for salt.modules.boto_apigateway state.module, usage_plans portion
- """
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_present_if_describe_fails(self, *args):
- """
- Tests correct error processing for describe_usage_plan failure
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={"error": "error"}
- )
- },
- ):
- result = boto_apigateway.usage_plan_present(
- "name", "plan_name", **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"], "Failed to describe existing usage plans"
- )
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_present_if_there_is_no_such_plan_and_test_option_is_set(
- self, *args
- ):
- """
- TestCse for salt.modules.boto_apigateway state.module, checking that if __opts__['test'] is set
- and usage plan does not exist, correct diagnostic will be returned
- """
- with patch.dict(boto_apigateway.__opts__, {"test": True}):
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={"plans": []}
- )
- },
- ):
- result = boto_apigateway.usage_plan_present(
- "name", "plan_name", **conn_parameters
- )
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"], "a new usage plan plan_name would be created"
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], None)
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_present_if_create_usage_plan_fails(self, *args):
- """
- Tests behavior for the case when creating a new usage plan fails
- """
- with patch.dict(boto_apigateway.__opts__, {"test": False}):
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={"plans": []}
- ),
- "boto_apigateway.create_usage_plan": MagicMock(
- return_value={"error": "error"}
- ),
- },
- ):
- result = boto_apigateway.usage_plan_present(
- "name", "plan_name", **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"], "Failed to create a usage plan plan_name, error"
- )
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_present_if_plan_is_there_and_needs_no_updates(self, *args):
- """
- Tests behavior for the case when plan is present and needs no updates
- """
- with patch.dict(boto_apigateway.__opts__, {"test": False}):
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={"plans": [{"id": "planid", "name": "planname"}]}
- ),
- "boto_apigateway.update_usage_plan": MagicMock(),
- },
- ):
- result = boto_apigateway.usage_plan_present(
- "name", "plan_name", **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], True)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"],
- "usage plan plan_name is already in a correct state",
- )
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- self.assertTrue(
- boto_apigateway.__salt__[
- "boto_apigateway.update_usage_plan"
- ].call_count
- == 0
- )
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_present_if_plan_is_there_and_needs_updates_but_test_is_set(
- self, *args
- ):
- """
- Tests behavior when usage plan needs to be updated by tests option is set
- """
- with patch.dict(boto_apigateway.__opts__, {"test": True}):
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={
- "plans": [
- {
- "id": "planid",
- "name": "planname",
- "throttle": {"rateLimit": 10.0},
- }
- ]
- }
- ),
- "boto_apigateway.update_usage_plan": MagicMock(),
- },
- ):
- result = boto_apigateway.usage_plan_present(
- "name", "plan_name", **conn_parameters
- )
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"], "a new usage plan plan_name would be updated"
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], None)
- self.assertTrue(
- boto_apigateway.__salt__[
- "boto_apigateway.update_usage_plan"
- ].call_count
- == 0
- )
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_present_if_plan_is_there_and_needs_updates_but_update_fails(
- self, *args
- ):
- """
- Tests error processing for the case when updating an existing usage plan fails
- """
- with patch.dict(boto_apigateway.__opts__, {"test": False}):
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={
- "plans": [
- {
- "id": "planid",
- "name": "planname",
- "throttle": {"rateLimit": 10.0},
- }
- ]
- }
- ),
- "boto_apigateway.update_usage_plan": MagicMock(
- return_value={"error": "error"}
- ),
- },
- ):
- result = boto_apigateway.usage_plan_present(
- "name", "plan_name", **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"], "Failed to update a usage plan plan_name, error"
- )
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_present_if_plan_has_been_created(self, *args):
- """
- Tests successful case for creating a new usage plan
- """
- with patch.dict(boto_apigateway.__opts__, {"test": False}):
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- side_effect=[{"plans": []}, {"plans": [{"id": "id"}]}]
- ),
- "boto_apigateway.create_usage_plan": MagicMock(
- return_value={"created": True}
- ),
- },
- ):
- result = boto_apigateway.usage_plan_present(
- "name", "plan_name", **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], True)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"], "A new usage plan plan_name has been created"
- )
- self.assertEqual(result["changes"]["old"], {"plan": None})
- self.assertEqual(result["changes"]["new"], {"plan": {"id": "id"}})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_present_if_plan_has_been_updated(self, *args):
- """
- Tests successful case for updating a usage plan
- """
- with patch.dict(boto_apigateway.__opts__, {"test": False}):
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- side_effect=[
- {"plans": [{"id": "id"}]},
- {
- "plans": [
- {
- "id": "id",
- "throttle": {"rateLimit": throttle_rateLimit},
- }
- ]
- },
- ]
- ),
- "boto_apigateway.update_usage_plan": MagicMock(
- return_value={"updated": True}
- ),
- },
- ):
- result = boto_apigateway.usage_plan_present(
- "name",
- "plan_name",
- throttle={"rateLimit": throttle_rateLimit},
- **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], True)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"], "usage plan plan_name has been updated"
- )
- self.assertEqual(result["changes"]["old"], {"plan": {"id": "id"}})
- self.assertEqual(
- result["changes"]["new"],
- {
- "plan": {
- "id": "id",
- "throttle": {"rateLimit": throttle_rateLimit},
- }
- },
- )
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_present_if_ValueError_is_raised(self, *args):
- """
- Tests error processing for the case when ValueError is raised when creating a usage plan
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- side_effect=ValueError("error")
- )
- },
- ):
- result = boto_apigateway.usage_plan_present(
- "name",
- "plan_name",
- throttle={"rateLimit": throttle_rateLimit},
- **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(result["comment"], repr(("error",)))
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_present_if_IOError_is_raised(self, *args):
- """
- Tests error processing for the case when IOError is raised when creating a usage plan
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- side_effect=IOError("error")
- )
- },
- ):
- result = boto_apigateway.usage_plan_present(
- "name",
- "plan_name",
- throttle={"rateLimit": throttle_rateLimit},
- **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(result["comment"], repr(("error",)))
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_absent_if_describe_fails(self, *args):
- """
- Tests correct error processing for describe_usage_plan failure
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={"error": "error"}
- )
- },
- ):
- result = {}
- result = boto_apigateway.usage_plan_absent(
- "name", "plan_name", **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"], "Failed to describe existing usage plans"
- )
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_absent_if_plan_is_not_present(self, *args):
- """
- Tests behavior for the case when the plan that needs to be absent does not exist
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={"plans": []}
- )
- },
- ):
- result = {}
- result = boto_apigateway.usage_plan_absent(
- "name", "plan_name", **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], True)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"], "Usage plan plan_name does not exist already"
- )
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_absent_if_plan_is_present_but_test_option_is_set(self, *args):
- """
- Tests behavior for the case when usage plan needs to be deleted by tests option is set
- """
- with patch.dict(boto_apigateway.__opts__, {"test": True}):
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={"plans": [{"id": "id"}]}
- )
- },
- ):
- result = {}
- result = boto_apigateway.usage_plan_absent(
- "name", "plan_name", **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], None)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"],
- "Usage plan plan_name exists and would be deleted",
- )
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_absent_if_plan_is_present_but_delete_fails(self, *args):
- """
- Tests correct error processing when deleting a usage plan fails
- """
- with patch.dict(boto_apigateway.__opts__, {"test": False}):
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={"plans": [{"id": "id"}]}
- ),
- "boto_apigateway.delete_usage_plan": MagicMock(
- return_value={"error": "error"}
- ),
- },
- ):
- result = boto_apigateway.usage_plan_absent(
- "name", "plan_name", **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"],
- "Failed to delete usage plan plan_name, "
- + repr({"error": "error"}),
- )
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_absent_if_plan_has_been_deleted(self, *args):
- """
- Tests successful case for deleting a usage plan
- """
- with patch.dict(boto_apigateway.__opts__, {"test": False}):
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={"plans": [{"id": "id"}]}
- ),
- "boto_apigateway.delete_usage_plan": MagicMock(
- return_value={"deleted": True}
- ),
- },
- ):
- result = boto_apigateway.usage_plan_absent(
- "name", "plan_name", **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], True)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"], "Usage plan plan_name has been deleted"
- )
- self.assertIn("changes", result)
- self.assertEqual(
- result["changes"],
- {"new": {"plan": None}, "old": {"plan": {"id": "id"}}},
- )
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_absent_if_ValueError_is_raised(self, *args):
- """
- Tests correct error processing for the case when ValueError is raised when deleting a usage plan
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- side_effect=ValueError("error")
- )
- },
- ):
- result = boto_apigateway.usage_plan_absent(
- "name", "plan_name", **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(result["comment"], repr(("error",)))
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_absent_if_IOError_is_raised(self, *args):
- """
- Tests correct error processing for the case when IOError is raised when deleting a usage plan
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- side_effect=IOError("error")
- )
- },
- ):
- result = boto_apigateway.usage_plan_absent(
- "name", "plan_name", **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(result["comment"], repr(("error",)))
- @skipIf(HAS_BOTO is False, "The boto module must be installed.")
- @skipIf(
- _has_required_boto() is False,
- "The boto3 module must be greater than"
- " or equal to version {0}".format(required_boto3_version),
- )
- @skipIf(
- _has_required_botocore() is False,
- "The botocore module must be greater than"
- " or equal to version {0}".format(required_botocore_version),
- )
- class BotoApiGatewayUsagePlanAssociationTestCase(
- BotoApiGatewayStateTestCaseBase, BotoApiGatewayTestCaseMixin
- ):
- """
- TestCase for salt.modules.boto_apigateway state.module, usage_plans_association portion
- """
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_association_present_if_describe_fails(self, *args):
- """
- Tests correct error processing for describe_usage_plan failure
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={"error": "error"}
- )
- },
- ):
- result = boto_apigateway.usage_plan_association_present(
- "name", "plan_name", [association_stage_1], **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"], "Failed to describe existing usage plans"
- )
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_association_present_if_plan_is_not_present(self, *args):
- """
- Tests correct error processing if a plan for which association has been requested is not present
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={"plans": []}
- )
- },
- ):
- result = boto_apigateway.usage_plan_association_present(
- "name", "plan_name", [association_stage_1], **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(result["comment"], "Usage plan plan_name does not exist")
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_association_present_if_multiple_plans_with_the_same_name_exist(
- self, *args
- ):
- """
- Tests correct error processing for the case when multiple plans with the same name exist
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={"plans": [{"id": "id1"}, {"id": "id2"}]}
- )
- },
- ):
- result = boto_apigateway.usage_plan_association_present(
- "name", "plan_name", [association_stage_1], **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"],
- "There are multiple usage plans with the same name - it is not supported",
- )
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_association_present_if_association_already_exists(self, *args):
- """
- Tests the behavior for the case when requested association is already present
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={
- "plans": [{"id": "id1", "apiStages": [association_stage_1]}]
- }
- )
- },
- ):
- result = boto_apigateway.usage_plan_association_present(
- "name", "plan_name", [association_stage_1], **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], True)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"], "Usage plan is already asssociated to all api stages"
- )
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_association_present_if_update_fails(self, *args):
- """
- Tests correct error processing for the case when adding associations fails
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={
- "plans": [{"id": "id1", "apiStages": [association_stage_1]}]
- }
- ),
- "boto_apigateway.attach_usage_plan_to_apis": MagicMock(
- return_value={"error": "error"}
- ),
- },
- ):
- result = boto_apigateway.usage_plan_association_present(
- "name", "plan_name", [association_stage_2], **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertTrue(
- result["comment"].startswith("Failed to associate a usage plan")
- )
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_association_present_success(self, *args):
- """
- Tests successful case for adding usage plan associations to a given api stage
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={
- "plans": [{"id": "id1", "apiStages": [association_stage_1]}]
- }
- ),
- "boto_apigateway.attach_usage_plan_to_apis": MagicMock(
- return_value={
- "result": {
- "apiStages": [association_stage_1, association_stage_2]
- }
- }
- ),
- },
- ):
- result = boto_apigateway.usage_plan_association_present(
- "name", "plan_name", [association_stage_2], **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], True)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"], "successfully associated usage plan to apis"
- )
- self.assertIn("changes", result)
- self.assertEqual(
- result["changes"],
- {
- "new": [association_stage_1, association_stage_2],
- "old": [association_stage_1],
- },
- )
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_association_present_if_value_error_is_thrown(self, *args):
- """
- Tests correct error processing for the case when IOError is raised while trying to set usage plan associations
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- side_effect=ValueError("error")
- )
- },
- ):
- result = boto_apigateway.usage_plan_association_present(
- "name", "plan_name", [], **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(result["comment"], repr(("error",)))
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_association_present_if_io_error_is_thrown(self, *args):
- """
- Tests correct error processing for the case when IOError is raised while trying to set usage plan associations
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- side_effect=IOError("error")
- )
- },
- ):
- result = boto_apigateway.usage_plan_association_present(
- "name", "plan_name", [], **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(result["comment"], repr(("error",)))
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_association_absent_if_describe_fails(self, *args):
- """
- Tests correct error processing for describe_usage_plan failure
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={"error": "error"}
- )
- },
- ):
- result = boto_apigateway.usage_plan_association_absent(
- "name", "plan_name", [association_stage_1], **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"], "Failed to describe existing usage plans"
- )
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_association_absent_if_plan_is_not_present(self, *args):
- """
- Tests error processing for the case when plan for which associations need to be modified is not present
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={"plans": []}
- )
- },
- ):
- result = boto_apigateway.usage_plan_association_absent(
- "name", "plan_name", [association_stage_1], **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(result["comment"], "Usage plan plan_name does not exist")
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_association_absent_if_multiple_plans_with_the_same_name_exist(
- self, *args
- ):
- """
- Tests the case when there are multiple plans with the same name but different Ids
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={"plans": [{"id": "id1"}, {"id": "id2"}]}
- )
- },
- ):
- result = boto_apigateway.usage_plan_association_absent(
- "name", "plan_name", [association_stage_1], **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"],
- "There are multiple usage plans with the same name - it is not supported",
- )
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_association_absent_if_plan_has_no_associations(self, *args):
- """
- Tests the case when the plan has no associations at all
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={"plans": [{"id": "id1", "apiStages": []}]}
- )
- },
- ):
- result = boto_apigateway.usage_plan_association_absent(
- "name", "plan_name", [association_stage_1], **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], True)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"],
- "Usage plan plan_name has no associated stages already",
- )
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_association_absent_if_plan_has_no_specific_association(
- self, *args
- ):
- """
- Tests the case when requested association is not present already
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={
- "plans": [{"id": "id1", "apiStages": [association_stage_1]}]
- }
- )
- },
- ):
- result = boto_apigateway.usage_plan_association_absent(
- "name", "plan_name", [association_stage_2], **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], True)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"],
- "Usage plan is already not asssociated to any api stages",
- )
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_association_absent_if_detaching_association_fails(self, *args):
- """
- Tests correct error processing when detaching the usage plan from the api function is called
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={
- "plans": [
- {
- "id": "id1",
- "apiStages": [association_stage_1, association_stage_2],
- }
- ]
- }
- ),
- "boto_apigateway.detach_usage_plan_from_apis": MagicMock(
- return_value={"error": "error"}
- ),
- },
- ):
- result = boto_apigateway.usage_plan_association_absent(
- "name", "plan_name", [association_stage_2], **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertTrue(
- result["comment"].startswith(
- "Failed to disassociate a usage plan plan_name from the apis"
- )
- )
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_association_absent_success(self, *args):
- """
- Tests successful case of disaccosiation the usage plan from api stages
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- return_value={
- "plans": [
- {
- "id": "id1",
- "apiStages": [association_stage_1, association_stage_2],
- }
- ]
- }
- ),
- "boto_apigateway.detach_usage_plan_from_apis": MagicMock(
- return_value={"result": {"apiStages": [association_stage_1]}}
- ),
- },
- ):
- result = boto_apigateway.usage_plan_association_absent(
- "name", "plan_name", [association_stage_2], **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], True)
- self.assertIn("comment", result)
- self.assertEqual(
- result["comment"], "successfully disassociated usage plan from apis"
- )
- self.assertIn("changes", result)
- self.assertEqual(
- result["changes"],
- {
- "new": [association_stage_1],
- "old": [association_stage_1, association_stage_2],
- },
- )
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_association_absent_if_ValueError_is_raised(self, *args):
- """
- Tests correct error processing for the case where ValueError is raised while trying to remove plan associations
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- side_effect=ValueError("error")
- )
- },
- ):
- result = boto_apigateway.usage_plan_association_absent(
- "name", "plan_name", [association_stage_1], **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(result["comment"], repr(("error",)))
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
- @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
- def test_usage_plan_association_absent_if_IOError_is_raised(self, *args):
- """
- Tests correct error processing for the case where IOError exception is raised while trying to remove plan associations
- """
- with patch.dict(
- boto_apigateway.__salt__,
- {
- "boto_apigateway.describe_usage_plans": MagicMock(
- side_effect=IOError("error")
- )
- },
- ):
- result = boto_apigateway.usage_plan_association_absent(
- "name", "plan_name", [association_stage_1], **conn_parameters
- )
- self.assertIn("result", result)
- self.assertEqual(result["result"], False)
- self.assertIn("comment", result)
- self.assertEqual(result["comment"], repr(("error",)))
- self.assertIn("changes", result)
- self.assertEqual(result["changes"], {})
|