TestCase.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Cloud
  17. * @subpackage Infrastructure
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Cloud_Infrastructure_Adapter
  23. */
  24. require_once 'Zend/Cloud/Infrastructure/Adapter.php';
  25. /**
  26. * @see Zend_Cloud_Infrastructure_Instance
  27. */
  28. require_once 'Zend/Cloud/Infrastructure/Instance.php';
  29. /**
  30. * This class forces the adapter tests to implement tests for all methods on
  31. * Zend_Cloud_Infrastructure.
  32. *
  33. * @category Zend
  34. * @package Zend_Cloud
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. abstract class Zend_Cloud_Infrastructure_TestCase extends PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * Reference to Document adapter to test
  43. *
  44. * @var Zend_Cloud_Infrastructure
  45. */
  46. protected $_commonInfrastructure;
  47. protected $_dummyCollectionNamePrefix = 'TestCollection';
  48. protected $_dummyDataPrefix = 'TestData';
  49. protected $_clientType = 'stdClass';
  50. const ID_FIELD = "__id";
  51. /**
  52. * Config object
  53. *
  54. * @var Zend_Config
  55. */
  56. protected $_config;
  57. /**
  58. * Period to wait for propagation in seconds
  59. * Should be set by adapter
  60. *
  61. * @var int
  62. */
  63. protected $_waitPeriod = 1;
  64. public function testInfrastructure()
  65. {
  66. $this->assertTrue($this->_commonInfrastructure instanceof Zend_Cloud_Infrastructure_Adapter);
  67. }
  68. public function testGetClient()
  69. {
  70. $this->assertTrue(is_a($this->_commonInfrastructure->getClient(), $this->_clientType));
  71. }
  72. /**
  73. * Test all the constants of the class
  74. */
  75. public function testConstants()
  76. {
  77. $this->assertEquals('running', Zend_Cloud_Infrastructure_Instance::STATUS_RUNNING);
  78. $this->assertEquals('stopped', Zend_Cloud_Infrastructure_Instance::STATUS_STOPPED);
  79. $this->assertEquals('shutting-down', Zend_Cloud_Infrastructure_Instance::STATUS_SHUTTING_DOWN);
  80. $this->assertEquals('rebooting', Zend_Cloud_Infrastructure_Instance::STATUS_REBOOTING);
  81. $this->assertEquals('terminated', Zend_Cloud_Infrastructure_Instance::STATUS_TERMINATED);
  82. $this->assertEquals('id', Zend_Cloud_Infrastructure_Instance::INSTANCE_ID);
  83. $this->assertEquals('imageId', Zend_Cloud_Infrastructure_Instance::INSTANCE_IMAGEID);
  84. $this->assertEquals('name', Zend_Cloud_Infrastructure_Instance::INSTANCE_NAME);
  85. $this->assertEquals('status', Zend_Cloud_Infrastructure_Instance::INSTANCE_STATUS);
  86. $this->assertEquals('publicDns', Zend_Cloud_Infrastructure_Instance::INSTANCE_PUBLICDNS);
  87. $this->assertEquals('cpu', Zend_Cloud_Infrastructure_Instance::INSTANCE_CPU);
  88. $this->assertEquals('ram', Zend_Cloud_Infrastructure_Instance::INSTANCE_RAM);
  89. $this->assertEquals('storageSize', Zend_Cloud_Infrastructure_Instance::INSTANCE_STORAGE);
  90. $this->assertEquals('zone', Zend_Cloud_Infrastructure_Instance::INSTANCE_ZONE);
  91. $this->assertEquals('launchTime', Zend_Cloud_Infrastructure_Instance::INSTANCE_LAUNCHTIME);
  92. $this->assertEquals('CpuUsage', Zend_Cloud_Infrastructure_Instance::MONITOR_CPU);
  93. $this->assertEquals('NetworkIn', Zend_Cloud_Infrastructure_Instance::MONITOR_NETWORK_IN);
  94. $this->assertEquals('NetworkOut', Zend_Cloud_Infrastructure_Instance::MONITOR_NETWORK_OUT);
  95. $this->assertEquals('DiskWrite', Zend_Cloud_Infrastructure_Instance::MONITOR_DISK_WRITE);
  96. $this->assertEquals('DiskRead', Zend_Cloud_Infrastructure_Instance::MONITOR_DISK_READ);
  97. $this->assertEquals('StartTime', Zend_Cloud_Infrastructure_Instance::MONITOR_START_TIME);
  98. $this->assertEquals('EndTime', Zend_Cloud_Infrastructure_Instance::MONITOR_END_TIME);
  99. $this->assertEquals('username', Zend_Cloud_Infrastructure_Instance::SSH_USERNAME);
  100. $this->assertEquals('password', Zend_Cloud_Infrastructure_Instance::SSH_PASSWORD);
  101. $this->assertEquals('privateKey', Zend_Cloud_Infrastructure_Instance::SSH_PRIVATE_KEY);
  102. $this->assertEquals('publicKey', Zend_Cloud_Infrastructure_Instance::SSH_PUBLIC_KEY);
  103. $this->assertEquals('passphrase', Zend_Cloud_Infrastructure_Instance::SSH_PASSPHRASE);
  104. }
  105. /**
  106. * Test construct with missing params
  107. */
  108. public function testConstructExceptionMissingParams()
  109. {
  110. $this->setExpectedException(
  111. 'Zend\Cloud\Infrastructure\Exception\InvalidArgumentException',
  112. 'You must pass an array of params'
  113. );
  114. $instance = new Zend_Cloud_Infrastructure_Instance(self::$adapter,array());
  115. }
  116. /**
  117. * Test construct with invalid keys in the params
  118. */
  119. public function testConstructExceptionInvalidKeys()
  120. {
  121. $this->setExpectedException(
  122. 'Zend\Cloud\Infrastructure\Exception\InvalidArgumentException',
  123. 'The param "'.Zend_Cloud_Infrastructure_Instance::INSTANCE_ID.'" is a required param for Zend\Cloud\Infrastructure\Instance'
  124. );
  125. $instance = new Zend_Cloud_Infrastructure_Instance(self::$adapter,array('foo'=>'bar'));
  126. }
  127. /**
  128. * Test get Id
  129. */
  130. public function testGetId()
  131. {
  132. $this->assertEquals('foo',$this->_commonInfrastructure->getId());
  133. $this->assertEquals('foo',$this->_commonInfrastructure->getAttribute(Instance::INSTANCE_ID));
  134. }
  135. /**
  136. * Test get Image Id
  137. */
  138. public function testGetImageId()
  139. {
  140. $this->assertEquals('foo',$this->_commonInfrastructure->getImageId());
  141. $this->assertEquals('foo',$this->_commonInfrastructure->getAttribute(Instance::INSTANCE_IMAGEID));
  142. }
  143. /**
  144. * Test get name
  145. */
  146. public function testGetName()
  147. {
  148. $this->assertEquals('foo',$this->_commonInfrastructure->getName());
  149. $this->assertEquals('foo',$this->_commonInfrastructure->getAttribute(Instance::INSTANCE_NAME));
  150. }
  151. /**
  152. * Test get status
  153. */
  154. public function testGetStatus()
  155. {
  156. $this->assertEquals('ZendTest\Cloud\Infrastructure\TestAsset\MockAdapter::statusInstance',$this->_commonInfrastructure->getStatus());
  157. $this->assertEquals('foo',$this->_commonInfrastructure->getAttribute(Instance::INSTANCE_STATUS));
  158. }
  159. /**
  160. * Test get public DNS
  161. */
  162. public function testGetPublicDns()
  163. {
  164. $this->assertEquals('foo',$this->_commonInfrastructure->getPublicDns());
  165. $this->assertEquals('foo',$this->_commonInfrastructure->getAttribute(Instance::INSTANCE_PUBLICDNS));
  166. }
  167. /**
  168. * Test get CPU
  169. */
  170. public function testGetCpu()
  171. {
  172. $this->assertEquals('foo',$this->_commonInfrastructure->getCpu());
  173. $this->assertEquals('foo',$this->_commonInfrastructure->getAttribute(Instance::INSTANCE_CPU));
  174. }
  175. /**
  176. * Test get RAM size
  177. */
  178. public function testGetRam()
  179. {
  180. $this->assertEquals('foo',$this->_commonInfrastructure->getRamSize());
  181. $this->assertEquals('foo',$this->_commonInfrastructure->getAttribute(Instance::INSTANCE_RAM));
  182. }
  183. /**
  184. * Test get storage size (disk)
  185. */
  186. public function testGetStorageSize()
  187. {
  188. $this->assertEquals('foo',$this->_commonInfrastructure->getStorageSize());
  189. $this->assertEquals('foo',$this->_commonInfrastructure->getAttribute(Instance::INSTANCE_STORAGE));
  190. }
  191. /**
  192. * Test get zone
  193. */
  194. public function testGetZone()
  195. {
  196. $this->assertEquals('foo',$this->_commonInfrastructure->getZone());
  197. $this->assertEquals('foo',$this->_commonInfrastructure->getAttribute(Instance::INSTANCE_ZONE));
  198. }
  199. /**
  200. * Test get the launch time of the instance
  201. */
  202. public function testGetLaunchTime()
  203. {
  204. $this->assertEquals('foo',$this->_commonInfrastructure->getLaunchTime());
  205. $this->assertEquals('foo',$this->_commonInfrastructure->getAttribute(Instance::INSTANCE_LAUNCHTIME));
  206. }
  207. /**
  208. * Test reboot
  209. */
  210. public function testReboot()
  211. {
  212. $this->assertEquals('ZendTest\Cloud\Infrastructure\TestAsset\MockAdapter::rebootInstance',$this->_commonInfrastructure->reboot());
  213. }
  214. /**
  215. * Test stop
  216. */
  217. public function testStop()
  218. {
  219. $this->assertEquals('ZendTest\Cloud\Infrastructure\TestAsset\MockAdapter::stopInstance',$this->_commonInfrastructure->stop());
  220. }
  221. /**
  222. * Test start
  223. */
  224. public function testStart()
  225. {
  226. $this->assertEquals('ZendTest\Cloud\Infrastructure\TestAsset\MockAdapter::startInstance',$this->_commonInfrastructure->start());
  227. }
  228. /**
  229. * Test destroy
  230. */
  231. public function testDestroy()
  232. {
  233. $this->assertEquals('ZendTest\Cloud\Infrastructure\TestAsset\MockAdapter::destroyInstance',$this->_commonInfrastructure->destroy());
  234. }
  235. /**
  236. * Test monitor
  237. */
  238. public function testMonitor()
  239. {
  240. $this->assertEquals('ZendTest\Cloud\Infrastructure\TestAsset\MockAdapter::monitorInstance',$this->_commonInfrastructure->monitor('foo'));
  241. }
  242. /**
  243. * Test deploy
  244. */
  245. public function testDeploy()
  246. {
  247. $this->assertEquals('ZendTest\Cloud\Infrastructure\TestAsset\MockAdapter::deployInstance',$this->_commonInfrastructure->deploy('foo','bar'));
  248. }
  249. public function setUp()
  250. {
  251. $this->_config = $this->_getConfig();
  252. $this->_commonInfrastructure = Zend_Cloud_Infrastructure_Factory::getAdapter($this->_config);
  253. parent::setUp();
  254. }
  255. abstract protected function _getConfig();
  256. protected function _wait() {
  257. sleep($this->_waitPeriod);
  258. }
  259. }