Zend_Service_Amazon_Ec2_Instance = new Zend_Service_Amazon_Ec2_Instance('access_key', 'secret_access_key');
$adapter = new Zend_Http_Client_Adapter_Test();
$client = new Zend_Http_Client(null, array(
'adapter' => $adapter
));
$this->adapter = $adapter;
Zend_Service_Amazon_Ec2_Instance::setHttpClient($client);
}
/**
* Cleans up the environment after running a test.
*/
protected function tearDown()
{
unset($this->adapter);
$this->Zend_Service_Amazon_Ec2_Instance = null;
parent::tearDown();
}
public function testConstants()
{
$this->assertEquals('m1.small', Zend_Service_Amazon_Ec2_Instance::SMALL);
$this->assertEquals('m1.large', Zend_Service_Amazon_Ec2_Instance::LARGE);
$this->assertEquals('m1.xlarge', Zend_Service_Amazon_Ec2_Instance::XLARGE);
$this->assertEquals('c1.medium', Zend_Service_Amazon_Ec2_Instance::HCPU_MEDIUM);
$this->assertEquals('c1.xlarge', Zend_Service_Amazon_Ec2_Instance::HCPU_XLARGE);
}
/**
* Tests Zend_Service_Amazon_Ec2_Instance->confirmProduct()
*/
public function testConfirmProductReturnsOwnerId()
{
$rawHttpResponse = "HTTP/1.1 200 OK\r\n"
. "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Server: hi\r\n"
. "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Status: 200 OK\r\n"
. "Content-type: application/xml; charset=utf-8\r\n"
. "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
. "Connection: close\r\n"
. "\r\n"
. "\r\n"
. " true\r\n"
. " 254933287430\r\n"
. "\r\n";
$this->adapter->setResponse($rawHttpResponse);
$return = $this->Zend_Service_Amazon_Ec2_Instance->confirmProduct('254933287430', 'i-1bda7172');
$this->assertEquals('254933287430', $return['ownerId']);
}
public function testConfirmProductReturnsFalse()
{
$rawHttpResponse = "HTTP/1.1 200 OK\r\n"
. "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Server: hi\r\n"
. "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Status: 200 OK\r\n"
. "Content-type: application/xml; charset=utf-8\r\n"
. "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
. "Connection: close\r\n"
. "\r\n"
. "\r\n"
. " false\r\n"
. "\r\n";
$this->adapter->setResponse($rawHttpResponse);
$return = $this->Zend_Service_Amazon_Ec2_Instance->confirmProduct('254933287430', 'i-1bda7172');
$this->assertFalse($return);
}
/**
* Tests Zend_Service_Amazon_Ec2_Instance->describe()
*/
public function testDescribeSingleInstance()
{
$rawHttpResponse = "HTTP/1.1 200 OK\r\n"
. "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Server: hi\r\n"
. "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Status: 200 OK\r\n"
. "Content-type: application/xml; charset=utf-8\r\n"
. "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
. "Connection: close\r\n"
. "\r\n"
. "\r\n"
. " \r\n"
. " - \r\n"
. " r-44a5402d\r\n"
. " UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM\r\n"
. " \r\n"
. "
- \r\n"
. " default\r\n"
. "
\r\n"
. " \r\n"
. " \r\n"
. " - \r\n"
. " i-28a64341\r\n"
. " ami-6ea54007\r\n"
. " \r\n"
. "
0
\r\n"
. " running\r\n"
. " \r\n"
. " 10-251-50-75.ec2.internal\r\n"
. " ec2-72-44-33-4.compute-1.amazonaws.com\r\n"
. " example-key-name\r\n"
. " \r\n"
. " - 774F4FF8
\r\n"
. " \r\n"
. " m1.small\r\n"
. " 2007-08-07T11:54:42.000Z\r\n"
. " \r\n"
. " us-east-1b\r\n"
. " \r\n"
. " aki-ba3adfd3\r\n"
. " ari-badbad00\r\n"
. " \r\n"
. " \r\n"
. " \r\n"
. " \r\n"
. "\r\n";
$this->adapter->setResponse($rawHttpResponse);
$return = $this->Zend_Service_Amazon_Ec2_Instance->describe('i-28a64341');
$this->assertEquals('r-44a5402d', $return['instances'][0]['reservationId']);
$this->assertEquals('default', $return['instances'][0]['groupSet'][0]);
$this->assertEquals('i-28a64341', $return['instances'][0]['instanceId']);
$this->assertEquals('ami-6ea54007', $return['instances'][0]['imageId']);
$this->assertEquals('m1.small', $return['instances'][0]['instanceType']);
$this->assertEquals('us-east-1b', $return['instances'][0]['availabilityZone']);
}
public function testDescribeIgnoreTerminatedInstance()
{
$rawHttpResponse = "HTTP/1.1 200 OK\r\n"
. "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Server: hi\r\n"
. "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Status: 200 OK\r\n"
. "Content-type: application/xml; charset=utf-8\r\n"
. "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
. "Connection: close\r\n"
. "\r\n"
. "\r\n"
. " \r\n"
. " - \r\n"
. " r-44a5402d\r\n"
. " UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM\r\n"
. " \r\n"
. "
- \r\n"
. " default\r\n"
. "
\r\n"
. " \r\n"
. " \r\n"
. " - \r\n"
. " i-28a64341\r\n"
. " ami-6ea54007\r\n"
. " \r\n"
. "
48
\r\n"
. " terminated\r\n"
. " \r\n"
. " 10-251-50-75.ec2.internal\r\n"
. " ec2-72-44-33-4.compute-1.amazonaws.com\r\n"
. " example-key-name\r\n"
. " \r\n"
. " - 774F4FF8
\r\n"
. " \r\n"
. " m1.small\r\n"
. " 2007-08-07T11:54:42.000Z\r\n"
. " \r\n"
. " us-east-1b\r\n"
. " \r\n"
. " aki-ba3adfd3\r\n"
. " ari-badbad00\r\n"
. " \r\n"
. " \r\n"
. " \r\n"
. " \r\n"
. "\r\n";
$this->adapter->setResponse($rawHttpResponse, true);
$return = $this->Zend_Service_Amazon_Ec2_Instance->describe('i-28a64341', true);
$this->assertEquals(0, count($return['instances']));
}
public function testDescribeByImageId()
{
$rawHttpResponse = "HTTP/1.1 200 OK\r\n"
. "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Server: hi\r\n"
. "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Status: 200 OK\r\n"
. "Content-type: application/xml; charset=utf-8\r\n"
. "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
. "Connection: close\r\n"
. "\r\n"
. "\r\n"
. " \r\n"
. " - \r\n"
. " r-44a5402d\r\n"
. " UYY3TLBUXIEON5NQVUUX6OMPWBZIQNFM\r\n"
. " \r\n"
. "
- \r\n"
. " default\r\n"
. "
\r\n"
. " \r\n"
. " \r\n"
. " - \r\n"
. " i-28a64341\r\n"
. " ami-6ea54007\r\n"
. " \r\n"
. "
0
\r\n"
. " running\r\n"
. " \r\n"
. " 10-251-50-75.ec2.internal\r\n"
. " ec2-72-44-33-4.compute-1.amazonaws.com\r\n"
. " example-key-name\r\n"
. " \r\n"
. " - 774F4FF8
\r\n"
. " \r\n"
. " m1.small\r\n"
. " 2007-08-07T11:54:42.000Z\r\n"
. " \r\n"
. " us-east-1b\r\n"
. " \r\n"
. " aki-ba3adfd3\r\n"
. " ari-badbad00\r\n"
. " \r\n"
. " \r\n"
. " \r\n"
. " \r\n"
. "\r\n";
$this->adapter->setResponse($rawHttpResponse);
$return = $this->Zend_Service_Amazon_Ec2_Instance->describeByImageId('ami-6ea54007');
$this->assertEquals('i-28a64341', $return[0]['instanceId']);
$this->assertEquals('ami-6ea54007', $return[0]['imageId']);
$this->assertEquals('m1.small', $return[0]['instanceType']);
$this->assertEquals('us-east-1b', $return[0]['availabilityZone']);
}
public function testRunThrowsExceptionWhenNoImageIdPassedIn()
{
$arrStart = array(
'maxStart' => 3,
'keyName' => 'example-key-name',
'securityGroup' => 'default',
'userData' => 'instance_id=www3',
'placement' => 'us-east-1b',
'kernelId' => 'aki-4438dd2d',
'ramdiskId' => 'ari-4538dd2c',
'blockDeviceVirtualName' => 'vertdevice',
'blockDeviceName' => '/dev/sdv'
);
try {
$return = $this->Zend_Service_Amazon_Ec2_Instance->run($arrStart);
$this->fail('Exception should be thrown when no image id is passed into the run commmand');
} catch (Zend_Service_Amazon_Ec2_Exception $zsaee) {
$this->assertEquals('No Image Id Provided', $zsaee->getMessage());
}
}
public function testRunOneSecurityGroup()
{
$rawHttpResponse = "HTTP/1.1 200 OK\r\n"
. "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Server: hi\r\n"
. "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Status: 200 OK\r\n"
. "Content-type: application/xml; charset=utf-8\r\n"
. "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
. "Connection: close\r\n"
. "\r\n"
. "\r\n"
. " r-47a5402e\r\n"
. " 495219933132\r\n"
. " \r\n"
. " - \r\n"
. " default\r\n"
. "
\r\n"
. " \r\n"
. " \r\n"
. " - \r\n"
. " i-2ba64342\r\n"
. " ami-60a54009\r\n"
. " \r\n"
. "
0
\r\n"
. " pending\r\n"
. " \r\n"
. " \r\n"
. " \r\n"
. " example-key-name\r\n"
. " 0\r\n"
. " m1.small\r\n"
. " 2007-08-07T11:51:50.000Z\r\n"
. " \r\n"
. " us-east-1b\r\n"
. " \r\n"
. " \r\n"
. " - \r\n"
. " i-2bc64242\r\n"
. " ami-60a54009\r\n"
. " \r\n"
. "
0
\r\n"
. " pending\r\n"
. " \r\n"
. " \r\n"
. " \r\n"
. " example-key-name\r\n"
. " 1\r\n"
. " m1.small\r\n"
. " 2007-08-07T11:51:50.000Z\r\n"
. " \r\n"
. " us-east-1b\r\n"
. " \r\n"
. " \r\n"
. " - \r\n"
. " i-2be64332\r\n"
. " ami-60a54009\r\n"
. " \r\n"
. "
0
\r\n"
. " pending\r\n"
. " \r\n"
. " \r\n"
. " \r\n"
. " example-key-name\r\n"
. " 2\r\n"
. " m1.small\r\n"
. " 2007-08-07T11:51:50.000Z\r\n"
. " \r\n"
. " us-east-1b\r\n"
. " \r\n"
. " \r\n"
. " \r\n"
. "\r\n";
$this->adapter->setResponse($rawHttpResponse);
$arrStart = array(
'imageId' => 'ami-60a54009',
'maxStart' => 3,
'keyName' => 'example-key-name',
'securityGroup' => 'default',
'userData' => 'instance_id=www3',
'placement' => 'us-east-1b',
'kernelId' => 'aki-4438dd2d',
'ramdiskId' => 'ari-4538dd2c',
'blockDeviceVirtualName' => 'vertdevice',
'blockDeviceName' => '/dev/sdv'
);
$return = $this->Zend_Service_Amazon_Ec2_Instance->run($arrStart);
$this->assertEquals(3, count($return['instances']));
$this->assertEquals('495219933132', $return['ownerId']);
$arrInstanceIds = array('i-2ba64342', 'i-2bc64242', 'i-2be64332');
foreach($return['instances'] as $k => $r) {
$this->assertEquals($arrInstanceIds[$k], $r['instanceId']);
$this->assertEquals($k, $r['amiLaunchIndex']);
}
}
public function testRunMultipleSecurityGroups()
{
$rawHttpResponse = "HTTP/1.1 200 OK\r\n"
. "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Server: hi\r\n"
. "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Status: 200 OK\r\n"
. "Content-type: application/xml; charset=utf-8\r\nn"
. "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
. "Connection: close\r\n"
. "\r\n"
. "\r\n"
. " r-47a5402e\r\n"
. " 495219933132\r\n"
. " \r\n"
. " - \r\n"
. " default\r\n"
. "
\r\n"
. " - \r\n"
. " web\r\n"
. "
\r\n"
. " \r\n"
. " \r\n"
. " - \r\n"
. " i-2ba64342\r\n"
. " ami-60a54009\r\n"
. " \r\n"
. "
0
\r\n"
. " pending\r\n"
. " \r\n"
. " \r\n"
. " \r\n"
. " example-key-name\r\n"
. " 0\r\n"
. " m1.small\r\n"
. " 2007-08-07T11:51:50.000Z\r\n"
. " \r\n"
. " us-east-1b\r\n"
. " \r\n"
. " \r\n"
. " \r\n"
. "\r\n";
$this->adapter->setResponse($rawHttpResponse);
$arrStart = array(
'imageId' => 'ami-60a54009',
'keyName' => 'example-key-name',
'securityGroup' => array('default','web'),
'userData' => 'instance_id=www3',
'placement' => 'us-east-1b',
'kernelId' => 'aki-4438dd2d',
'ramdiskId' => 'ari-4538dd2c',
'blockDeviceVirtualName' => 'vertdevice',
'blockDeviceName' => '/dev/sdv'
);
$return = $this->Zend_Service_Amazon_Ec2_Instance->run($arrStart);
$arrGroups = array('default', 'web');
$this->assertSame($arrGroups, $return['groupSet']);
}
public function testTerminateSingleInstances()
{
$rawHttpResponse = "HTTP/1.1 200 OK\r\n"
. "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Server: hi\r\n"
. "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Status: 200 OK\r\n"
. "Content-type: application/xml; charset=utf-8\r\n"
. "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
. "Connection: close\r\n"
. "\r\n"
. "\r\n"
. " \r\n"
. " - \r\n"
. " i-28a64341\r\n"
. " \r\n"
. "
32
\r\n"
. " shutting-down\r\n"
. " \r\n"
. " \r\n"
. " 16
\r\n"
. " running\r\n"
. " \r\n"
. " \r\n"
. " \r\n"
. "\r\n";
$this->adapter->setResponse($rawHttpResponse);
$return = $this->Zend_Service_Amazon_Ec2_Instance->terminate('i-28a64341');
$this->assertEquals(1, count($return));
foreach($return as $r) {
$this->assertEquals('i-28a64341', $r['instanceId']);
}
}
public function testTerminateMultipleInstances()
{
$rawHttpResponse = "HTTP/1.1 200 OK\r\n"
. "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Server: hi\r\n"
. "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Status: 200 OK\r\n"
. "Content-type: application/xml; charset=utf-8\r\n"
. "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
. "Connection: close\r\n"
. "\r\n"
. "\r\n"
. " \r\n"
. " - \r\n"
. " i-28a64341\r\n"
. " \r\n"
. "
32
\r\n"
. " shutting-down\r\n"
. " \r\n"
. " \r\n"
. " 16
\r\n"
. " running\r\n"
. " \r\n"
. " \r\n"
. " - \r\n"
. " i-21a64348\r\n"
. " \r\n"
. "
32
\r\n"
. " shutting-down\r\n"
. " \r\n"
. " \r\n"
. " 16
\r\n"
. " running\r\n"
. " \r\n"
. " \r\n"
. " \r\n"
. "\r\n";
$this->adapter->setResponse($rawHttpResponse);
$arrInstanceIds = array('i-28a64341', 'i-21a64348');
$return = $this->Zend_Service_Amazon_Ec2_Instance->terminate($arrInstanceIds);
$this->assertEquals(2, count($return));
foreach($return as $k=>$r) {
$this->assertEquals($arrInstanceIds[$k], $r['instanceId']);
}
}
public function testRebootMultipleInstances()
{
$rawHttpResponse = "HTTP/1.1 200 OK\r\n"
. "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Server: hi\r\n"
. "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Status: 200 OK\r\n"
. "Content-type: application/xml; charset=utf-8\r\n"
. "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
. "Connection: close\r\n"
. "\r\n"
. "\r\n"
. " true\r\n"
. "\r\n";
$this->adapter->setResponse($rawHttpResponse);
$arrInstanceIds = array('i-28a64341', 'i-21a64348');
$return = $this->Zend_Service_Amazon_Ec2_Instance->reboot($arrInstanceIds);
$this->assertTrue($return);
}
public function testRebootSingleInstance()
{
$rawHttpResponse = "HTTP/1.1 200 OK\r\n"
. "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Server: hi\r\n"
. "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Status: 200 OK\r\n"
. "Content-type: application/xml; charset=utf-8\r\n"
. "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
. "Connection: close\r\n"
. "\r\n"
. "\r\n"
. " true\r\n"
. "\r\n";
$this->adapter->setResponse($rawHttpResponse);
$return = $this->Zend_Service_Amazon_Ec2_Instance->reboot('i-28a64341');
$this->assertTrue($return);
}
public function testGetConsoleOutput()
{
$rawHttpResponse = "HTTP/1.1 200 OK\r\n"
. "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Server: hi\r\n"
. "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Status: 200 OK\r\n"
. "Content-type: application/xml; charset=utf-8\r\n"
. "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
. "Connection: close\r\n"
. "\r\n"
. "\r\n"
. " i-28a64341\r\n"
. " 2007-01-03 15:00:00\r\n"
. " \r\n"
. "\r\n";
$this->adapter->setResponse($rawHttpResponse);
$return = $this->Zend_Service_Amazon_Ec2_Instance->consoleOutput('i-28a64341');
$arrOutput = array(
'instanceId' => 'i-28a64341',
'timestamp' => '2007-01-03 15:00:00',
'output' => "Linux version 2.6.16-xenU (builder@patchbat.amazonsa) (gcc version 4.0.1 20050727 (Red Hat 4.0.1-5)) #1 SMP Thu Oct 26 08:41:26 SAST 2006\n"
. "BIOS-provided physical RAM map:\n"
. "Xen: 0000000000000000 - 000000006a400000 (usable)\n"
. "980MB HIGHMEM available.\n"
. "727MB LOWMEM available.\n"
. "NX (Execute Disable) protection: active\n"
. "IRQ lockup detection disabled\n"
. "Built 1 zonelists\n"
. "Kernel command line: root=/dev/sda1 ro 4\n"
. "Enabling fast FPU save and restore... done.\n");
$this->assertSame($arrOutput, $return);
}
public function testMonitorInstance()
{
$rawHttpResponse = "HTTP/1.1 200 OK\r\n"
. "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Server: hi\r\n"
. "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Status: 200 OK\r\n"
. "Content-type: application/xml; charset=utf-8\r\n"
. "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
. "Connection: close\r\n"
. "\r\n"
. "\r\n"
. " "
. " - "
. " i-43a4412a"
. " "
. " monitoring"
. " "
. "
"
. " "
. "\r\n";
$this->adapter->setResponse($rawHttpResponse);
$return = $this->Zend_Service_Amazon_Ec2_Instance->monitor('i-43a4412a');
$arrReturn = array(array('instanceid' => 'i-43a4412a', 'monitorstate' => 'monitoring'));
$this->assertSame($arrReturn, $return);
}
public function testUnmonitorInstance()
{
$rawHttpResponse = "HTTP/1.1 200 OK\r\n"
. "Date: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Server: hi\r\n"
. "Last-modified: Fri, 24 Oct 2008 17:24:52 GMT\r\n"
. "Status: 200 OK\r\n"
. "Content-type: application/xml; charset=utf-8\r\n"
. "Expires: Tue, 31 Mar 1981 05:00:00 GMT\r\n"
. "Connection: close\r\n"
. "\r\n"
. "\r\n"
. " "
. " - "
. " i-43a4412a"
. " "
. " pending"
. " "
. "
"
. " "
. "\r\n";
$this->adapter->setResponse($rawHttpResponse);
$return = $this->Zend_Service_Amazon_Ec2_Instance->unmonitor('i-43a4412a');
$arrReturn = array(array('instanceid' => 'i-43a4412a', 'monitorstate' => 'pending'));
$this->assertSame($arrReturn, $return);
}
}