public function testFunctionalRds($clientType)
{
$this->skipIfEc2PlatformDisabled();
$aws = $this->getEnvironment()->aws(AwsTestCase::REGION);
$aws->rds->setApiClientType($clientType);
$aws->rds->enableEntityManager();
$this->removesPreviouslyCreatedData();
//Describes DB Events
$req = new DescribeEventRequestData();
$req->startTime = new \DateTime('-2 hour', new \DateTimeZone('UTC'));
$req->eventCategories = array('deletion', 'availability');
$eventList = $aws->rds->event->describe($req);
$this->assertInstanceOf($this->getRdsClassName('DataType\\EventList'), $eventList);
unset($eventList);
//test DBParameterGroup
//Describes DB Parameters
$parList = $aws->rds->dbParameterGroup->describeParameters('default.mysql5.6');
$this->assertInstanceOf($this->getRdsClassName('DataType\\ParameterList'), $parList);
unset($parList);
//Creates a new DBParameterGroup
/* @var $pg DBParameterGroupData */
$pg = $aws->rds->dbParameterGroup->create(new DBParameterGroupData(self::getTestName(self::NAME_DB_PARAMETER_GROUP), 'mysql5.6', 'phpunit temporary group'));
$this->assertInstanceOf($this->getRdsClassName('DataType\\DBParameterGroupData'), $pg);
$this->assertEquals(self::getTestName(self::NAME_DB_PARAMETER_GROUP), $pg->dBParameterGroupName);
$this->assertEquals('mysql5.6', $pg->dBParameterGroupFamily);
$this->assertEquals('phpunit temporary group', $pg->description);
$this->assertSame($aws->rds->dbParameterGroup->get(self::getTestName(self::NAME_DB_PARAMETER_GROUP)), $pg);
//Modifies parameters
$ret = $pg->modify([new ParameterData('autocommit', ParameterData::APPLY_METHOD_PENDING_REBOOT, '0'), new ParameterData('automatic_sp_privileges', ParameterData::APPLY_METHOD_PENDING_REBOOT, '0')]);
$this->assertEquals($pg->dBParameterGroupName, $ret);
//test modify with fake parameter
$this->assertClientException(function () use($pg) {
$pg->modify(new ParameterData('fake', ParameterData::APPLY_METHOD_PENDING_REBOOT, '0'));
});
//Resets parameters
$ret = $pg->reset([new ParameterData('auto_increment_offset', ParameterData::APPLY_METHOD_PENDING_REBOOT)]);
$this->assertEquals($pg->dBParameterGroupName, $ret);
//Test reset pending changes pd
$this->assertClientException(function () use($pg) {
$pg->reset([new ParameterData('automatic_sp_privileges', ParameterData::APPLY_METHOD_PENDING_REBOOT)]);
});
//test DBSecurityGroup
//Creates DB Security Group
/* @var $sg DBSecurityGroupData */
$sg = $aws->rds->dbSecurityGroup->create(self::getTestName(self::NAME_SG), 'phpunit temporary security group');
$this->assertInstanceOf($this->getRdsClassName('DataType\\DBSecurityGroupData'), $sg);
$this->assertEquals(self::getTestName(self::NAME_SG), $sg->dBSecurityGroupName);
$this->assertEquals('phpunit temporary security group', $sg->dBSecurityGroupDescription);
$this->assertSame($aws->rds->dbSecurityGroup->get(self::getTestName(self::NAME_SG)), $sg);
$req = $sg->getIngressRequest();
$req->cIDRIP = '0.0.0.1/0';
$sg2 = $sg->authorizeIngress($req);
$this->assertSame($sg2, $sg);
unset($sg2);
$this->assertInstanceOf($this->getRdsClassName('DataType\\DBSecurityGroupData'), $sg);
$this->assertEquals('0.0.0.1/0', $sg->iPRanges->get(0)->cIDRIP);
//Avoids an error - cannot revoke an authorization which is in the authorizing state
$timeout = 0;
while (count($sg->iPRanges) && ($timeout += static::SLEEP_TIMEOUT) < static::SLEEP_TIME) {
foreach ($sg->iPRanges as $r) {
if ($r->status == IPRangeData::STATUS_AUTHORIZED) {
break 2;
}
}
sleep(static::SLEEP_TIMEOUT);
$sg = $sg->refresh();
}
//test exist authorization
$this->assertClientException(function () use($sg, $req) {
$sg->authorizeIngress($req);
});
$sg2 = $sg->revokeIngress($req);
$this->assertSame($sg2, $sg);
$this->assertInstanceOf($this->getRdsClassName('DataType\\DBSecurityGroupData'), $sg);
unset($sg2);
$timeout = 0;
while (count($sg->iPRanges) && ($timeout += static::SLEEP_TIMEOUT) < static::SLEEP_TIME) {
sleep(static::SLEEP_TIMEOUT);
$sg = $sg->refresh();
}
$this->assertEquals(0, count($sg->iPRanges));
//test failed authorization
$this->assertClientException(function () use($sg) {
$req = $sg->getIngressRequest();
$req->eC2SecurityGroupName = 'default';
$sg->authorizeIngress($req);
});
//test authorization with sg name and ownerId
$req = $sg->getIngressRequest();
$req->eC2SecurityGroupName = 'default';
$req->eC2SecurityGroupOwnerId = $aws->getAccountNumber();
$sg2 = $sg->authorizeIngress($req);
$this->assertSame($sg2, $sg);
unset($sg2);
//test DBInstance
//Creates DB Instance
/* @var $req CreateDBInstanceRequestData */
$req = new CreateDBInstanceRequestData(self::getTestName(self::NAME_INSTANCE), 'db.m1.small', 'mysql');
$req->allocatedStorage = 5;
$req->masterUsername = 'masterusername';
$req->masterUserPassword = substr(uniqid(), 0, 10);
$req->dBName = 'testname';
$req->port = 3306;
$req->backupRetentionPeriod = 0;
/* @var $dbi DBInstanceData */
$dbi = $aws->rds->dbInstance->create($req);
unset($req);
$this->assertInstanceOf($this->getRdsClassName('DataType\\DBInstanceData'), $dbi);
$this->assertEquals(self::getTestName(self::NAME_INSTANCE), $dbi->dBInstanceIdentifier);
$this->assertEquals('db.m1.small', $dbi->dBInstanceClass);
$this->assertEquals('mysql', $dbi->engine);
$this->assertSame($aws->rds->dbInstance->get(self::getTestName(self::NAME_INSTANCE)), $dbi);
$timeout = 0;
while ($dbi->dBInstanceStatus != DBInstanceData::STATUS_AVAILABLE && ($timeout += static::SLEEP_TIMEOUT) < static::SLEEP_TIME) {
sleep(static::SLEEP_TIMEOUT);
$dbi = $dbi->refresh();
}
$this->assertEquals(DBInstanceData::STATUS_AVAILABLE, $dbi->dBInstanceStatus);
//Modifies DBInstance
/* @var $req ModifyDBInstanceRequestData */
$req = new ModifyDBInstanceRequestData(self::getTestName(self::NAME_INSTANCE));
$req->masterUserPassword = substr(uniqid(), 0, 10);
$req->dBSecurityGroups = $sg->dBSecurityGroupName;
$req->dBParameterGroupName = $pg->dBParameterGroupName;
$dbi = $dbi->modify($req);
$this->assertInstanceOf($this->getRdsClassName('DataType\\DBInstanceData'), $dbi);
unset($req);
//test delete using sg and pg
$this->assertClientException(function () use($pg) {
$pg->delete();
});
$this->assertClientException(function () use($sg) {
$sg->delete();
});
//Reboots DB Instance
$dbi = $dbi->reboot();
$this->assertInstanceOf($this->getRdsClassName('DataType\\DBInstanceData'), $dbi);
$timeout = 0;
while ($dbi->dBInstanceStatus != DBInstanceData::STATUS_AVAILABLE && ($timeout += static::SLEEP_TIMEOUT) < static::SLEEP_TIME) {
sleep(static::SLEEP_TIMEOUT);
$dbi = $dbi->refresh();
}
$this->assertEquals(DBInstanceData::STATUS_AVAILABLE, $dbi->dBInstanceStatus);
// Adds tags to DB Instance
$aws->rds->tag->add($dbi->dBInstanceIdentifier, Rds::DB_INSTANCE_RESOURCE_TYPE, [['key' => self::getTestName(self::KEY_TAG), 'value' => self::VALUE_TAG]]);
$tagsList = $aws->rds->tag->describe($dbi->dBInstanceIdentifier, Rds::DB_INSTANCE_RESOURCE_TYPE);
$this->assertInstanceOf($this->getRdsClassName('DataType\\TagsList'), $tagsList);
$this->assertEquals(1, count($tagsList));
$tagData = $tagsList->get(0);
$this->assertInstanceOf($this->getRdsClassName('DataType\\TagsData'), $tagData);
$this->assertTrue($dbi->removeTags([$tagData->key]));
//Created DB Snapshot
/* @var $sn DBSnapshotData */
$sn = $dbi->createSnapshot(self::getTestName(self::NAME_DB_SNAPSHOT));
$this->assertInstanceOf($this->getRdsClassName('DataType\\DBSnapshotData'), $sn);
$timeout = 0;
while ($sn->status !== DBSnapshotData::STATUS_AVAILABLE && ($timeout += static::SLEEP_TIMEOUT) < static::SLEEP_TIME) {
sleep(static::SLEEP_TIMEOUT);
$sn = $sn->refresh();
}
$this->assertEquals(DBSnapshotData::STATUS_AVAILABLE, $sn->status);
$this->removeDBInstance($dbi);
//Restores DB Instance From DB Snapshot
$dbi = $sn->restoreFromSnapshot(self::getTestName(self::NAME_INSTANCE));
$this->assertInstanceOf($this->getRdsClassName('DataType\\DBInstanceData'), $dbi);
$this->assertSame($aws->rds->dbInstance->get(self::getTestName(self::NAME_INSTANCE)), $dbi);
$timeout = 0;
while ($dbi->dBInstanceStatus !== DBInstanceData::STATUS_AVAILABLE && ($timeout += static::SLEEP_TIMEOUT) < static::SLEEP_TIME) {
sleep(static::SLEEP_TIMEOUT);
$dbi = $dbi->refresh();
}
$this->assertEquals(DBInstanceData::STATUS_AVAILABLE, $dbi->dBInstanceStatus);
$this->removesPreviouslyCreatedData();
$aws->rds->getEntityManager()->detachAll();
}