def start(): """ Entrypoint for mocking EC2. :return: nothing """ # start rds mocking with moto mock = mock_rds() mock.start()
def rds(): """RDS mock service""" mock = mock_rds() mock.start() rds = boto3.client('rds') instances = rds.create_db_instance( DBName='db-xxxx', DBInstanceIdentifier='id1ewbkjqprhw13', AllocatedStorage=64, DBInstanceClass='db.t2.medium', MasterUsername='******', MasterUserPassword='******', AvailabilityZone='us-east-1a', Port=3306, MultiAZ=True, Engine='mysql', EngineVersion='5.7.11', AutoMinorVersionUpgrade=False, LicenseModel='general-public-license', Iops=256, PubliclyAccessible=True, Tags=[ { 'Key': 'Name', 'Value': 'id1ewbkjqprhw13' }, ], DBClusterIdentifier='id1ewbkjqprhw13', StorageType='standard', StorageEncrypted=False, CopyTagsToSnapshot=True, ) yield {'instances': instances} mock.stop()
class TestDBInstanceFilters(object): mock = mock_rds() @classmethod def setup_class(cls): cls.mock.start() client = boto3.client("rds", region_name="us-west-2") for i in range(10): identifier = "db-instance-{}".format(i) engine = "postgres" if (i % 3) else "mysql" client.create_db_instance( DBInstanceIdentifier=identifier, Engine=engine, DBInstanceClass="db.m1.small", ) cls.client = client @classmethod def teardown_class(cls): try: cls.mock.stop() except RuntimeError: pass def test_invalid_filter_name_raises_error(self): with pytest.raises(ClientError) as ex: self.client.describe_db_instances( Filters=[{"Name": "invalid-filter-name", "Values": []}] ) ex.value.response["Error"]["Code"].should.equal("InvalidParameterValue") ex.value.response["Error"]["Message"].should.equal( "Unrecognized filter name: invalid-filter-name" ) def test_empty_filter_values_raises_error(self): with pytest.raises(ClientError) as ex: self.client.describe_db_instances( Filters=[{"Name": "db-instance-id", "Values": []}] ) ex.value.response["Error"]["Code"].should.equal("InvalidParameterCombination") ex.value.response["Error"]["Message"].should.contain("must not be empty") def test_db_instance_id_filter(self): resp = self.client.describe_db_instances() db_instance_identifier = resp["DBInstances"][0]["DBInstanceIdentifier"] db_instances = self.client.describe_db_instances( Filters=[{"Name": "db-instance-id", "Values": [db_instance_identifier]}] ).get("DBInstances") db_instances.should.have.length_of(1) db_instances[0]["DBInstanceIdentifier"].should.equal(db_instance_identifier) def test_db_instance_id_filter_works_with_arns(self): resp = self.client.describe_db_instances() db_instance_arn = resp["DBInstances"][0]["DBInstanceArn"] db_instances = self.client.describe_db_instances( Filters=[{"Name": "db-instance-id", "Values": [db_instance_arn]}] ).get("DBInstances") db_instances.should.have.length_of(1) db_instances[0]["DBInstanceArn"].should.equal(db_instance_arn) def test_dbi_resource_id_filter(self): resp = self.client.describe_db_instances() dbi_resource_identifier = resp["DBInstances"][0]["DbiResourceId"] db_instances = self.client.describe_db_instances( Filters=[{"Name": "dbi-resource-id", "Values": [dbi_resource_identifier]}] ).get("DBInstances") for db_instance in db_instances: db_instance["DbiResourceId"].should.equal(dbi_resource_identifier) def test_engine_filter(self): db_instances = self.client.describe_db_instances( Filters=[{"Name": "engine", "Values": ["postgres"]}] ).get("DBInstances") for db_instance in db_instances: db_instance["Engine"].should.equal("postgres") db_instances = self.client.describe_db_instances( Filters=[{"Name": "engine", "Values": ["oracle"]}] ).get("DBInstances") db_instances.should.have.length_of(0) def test_multiple_filters(self): resp = self.client.describe_db_instances( Filters=[ { "Name": "db-instance-id", "Values": ["db-instance-0", "db-instance-1", "db-instance-3"], }, {"Name": "engine", "Values": ["mysql", "oracle"]}, ] ) returned_identifiers = [ db["DBInstanceIdentifier"] for db in resp["DBInstances"] ] returned_identifiers.should.have.length_of(2) "db-instance-0".should.be.within(returned_identifiers) "db-instance-3".should.be.within(returned_identifiers) def test_invalid_db_instance_identifier_with_exclusive_filter(self): # Passing a non-existent DBInstanceIdentifier will not raise an error # if the resulting filter matches other resources. resp = self.client.describe_db_instances( DBInstanceIdentifier="non-existent", Filters=[{"Name": "db-instance-id", "Values": ["db-instance-1"]}], ) resp["DBInstances"].should.have.length_of(1) resp["DBInstances"][0]["DBInstanceIdentifier"].should.equal("db-instance-1") def test_invalid_db_instance_identifier_with_non_matching_filter(self): # Passing a non-existent DBInstanceIdentifier will raise an error if # the resulting filter does not match any resources. with pytest.raises(ClientError) as ex: self.client.describe_db_instances( DBInstanceIdentifier="non-existent", Filters=[{"Name": "engine", "Values": ["mysql"]}], ) ex.value.response["Error"]["Code"].should.equal("DBInstanceNotFound") ex.value.response["Error"]["Message"].should.equal( "DBInstance non-existent not found." ) def test_valid_db_instance_identifier_with_exclusive_filter(self): # Passing a valid DBInstanceIdentifier with a filter it does not match # but does match other resources will return those other resources. resp = self.client.describe_db_instances( DBInstanceIdentifier="db-instance-0", Filters=[ {"Name": "db-instance-id", "Values": ["db-instance-1"]}, {"Name": "engine", "Values": ["postgres"]}, ], ) returned_identifiers = [ db["DBInstanceIdentifier"] for db in resp["DBInstances"] ] "db-instance-0".should_not.be.within(returned_identifiers) "db-instance-1".should.be.within(returned_identifiers) def test_valid_db_instance_identifier_with_inclusive_filter(self): # Passing a valid DBInstanceIdentifier with a filter it matches but also # matches other resources will return all matching resources. resp = self.client.describe_db_instances( DBInstanceIdentifier="db-instance-0", Filters=[ {"Name": "db-instance-id", "Values": ["db-instance-1"]}, {"Name": "engine", "Values": ["mysql", "postgres"]}, ], ) returned_identifiers = [ db["DBInstanceIdentifier"] for db in resp["DBInstances"] ] "db-instance-0".should.be.within(returned_identifiers) "db-instance-1".should.be.within(returned_identifiers) def test_valid_db_instance_identifier_with_non_matching_filter(self): # Passing a valid DBInstanceIdentifier will raise an error if the # resulting filter does not match any resources. with pytest.raises(ClientError) as ex: self.client.describe_db_instances( DBInstanceIdentifier="db-instance-0", Filters=[{"Name": "engine", "Values": ["postgres"]}], ) ex.value.response["Error"]["Code"].should.equal("DBInstanceNotFound") ex.value.response["Error"]["Message"].should.equal( "DBInstance db-instance-0 not found." )
def test_deprecation_warning(): with pytest.warns(None) as record: mock_rds() str(record[0].message).should.contain( "Module mock_rds has been deprecated, and will be repurposed in a later release" )
def rds(aws_credentials): with moto.mock_rds(): yield boto3.client("rds", region_name="us-east-1")
def cr(self): with moto.mock_rds(): cr = config_rules.ConfigRules(logging) yield cr