OnlineTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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_Service_Rackspace_Servers
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. require_once 'Zend/Service/Rackspace/Servers.php';
  22. require_once 'Zend/Http/Client/Adapter/Socket.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Service_Rackspace_Servers
  26. * @subpackage UnitTests
  27. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. * @group Zend_Service_Rackspace_Servers
  30. */
  31. class Zend_Service_Rackspace_Servers_OnlineTest extends PHPUnit_Framework_TestCase
  32. {
  33. /**
  34. * Reference to Rackspace Servers object
  35. *
  36. * @var Zend_Service_Rackspace_Servers
  37. */
  38. protected $rackspace;
  39. /**
  40. * Check if the resize was successfully done
  41. *
  42. * @var boolean
  43. */
  44. protected $resize;
  45. /**
  46. * List of flavors available
  47. *
  48. * @var array
  49. */
  50. protected static $flavors;
  51. /**
  52. * List of images available
  53. *
  54. * @var Zend_Service_Rackspace_Servers_ImageList
  55. */
  56. protected static $images;
  57. /**
  58. * Id of the image created
  59. *
  60. * @var string
  61. */
  62. protected static $imageId;
  63. /**
  64. * Server id of testing
  65. *
  66. * @var integer
  67. */
  68. protected static $serverId;
  69. /**
  70. * Admin password of the server
  71. *
  72. * @var string
  73. */
  74. protected static $adminPass;
  75. /**
  76. * Shared Ip group
  77. *
  78. * @var Zend_Service_Rackspace_Servers_SharedIpGroup
  79. */
  80. protected static $sharedIpGroup;
  81. /**
  82. * Socket based HTTP client adapter
  83. *
  84. * @var Zend_Http_Client_Adapter_Socket
  85. */
  86. protected $httpClientAdapterSocket;
  87. /**
  88. * Sets up this test case
  89. *
  90. * @return void
  91. */
  92. public function setUp()
  93. {
  94. if (!constant('TESTS_ZEND_SERVICE_RACKSPACE_ONLINE_ENABLED')) {
  95. $this->markTestSkipped('Zend_Service_Rackspace_Servers_OnlineTest tests are not enabled');
  96. }
  97. if(!defined('TESTS_ZEND_SERVICE_RACKSPACE_ONLINE_USER') || !defined('TESTS_ZEND_SERVICE_RACKSPACE_ONLINE_KEY')) {
  98. $this->markTestSkipped('Constants User and Key have to be set.');
  99. }
  100. $this->rackspace = new Zend_Service_Rackspace_Servers(TESTS_ZEND_SERVICE_RACKSPACE_ONLINE_USER,
  101. TESTS_ZEND_SERVICE_RACKSPACE_ONLINE_KEY);
  102. $this->httpClientAdapterSocket = new Zend_Http_Client_Adapter_Socket();
  103. $this->rackspace->getHttpClient()
  104. ->setAdapter($this->httpClientAdapterSocket);
  105. // terms of use compliance: safe delay between each test
  106. sleep(2);
  107. }
  108. /**
  109. * Wait n seconds for status change
  110. *
  111. * @param string $status
  112. * @param integer $timeout
  113. * @return boolean
  114. */
  115. protected function waitForStatus($status,$timeout=TESTS_ZEND_SERVICE_RACKSPACE_TIMEOUT)
  116. {
  117. $info['status']= null;
  118. $i=0;
  119. while ((strtoupper($info['status'])!==strtoupper($status)) && ($i<$timeout)) {
  120. $info= $this->rackspace->getServer(self::$serverId)->toArray();
  121. $i+=5;
  122. sleep(5);
  123. }
  124. return ($i<$timeout);
  125. }
  126. /**
  127. * Test constants
  128. */
  129. public function testConstants()
  130. {
  131. $this->assertEquals(10240, Zend_Service_Rackspace_Servers::LIMIT_FILE_SIZE);
  132. $this->assertEquals(5, Zend_Service_Rackspace_Servers::LIMIT_NUM_FILE);
  133. $this->assertEquals('json', Zend_Service_Rackspace_Servers::API_FORMAT);
  134. }
  135. /**
  136. * Test authentication
  137. */
  138. public function testAuthentication()
  139. {
  140. $this->filename= __METHOD__;
  141. $this->assertTrue($this->rackspace->authenticate());
  142. }
  143. /**
  144. * Test create server
  145. */
  146. public function testCreateServer()
  147. {
  148. $data = array (
  149. 'name' => TESTS_ZEND_SERVICE_RACKSPACE_SERVER_NAME,
  150. 'imageId' => TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGEID,
  151. 'flavorId' => TESTS_ZEND_SERVICE_RACKSPACE_SERVER_FLAVORID
  152. );
  153. $server= $this->rackspace->createServer($data);
  154. $this->assertTrue($server!==false);
  155. self::$serverId= $server->getId();
  156. self::$adminPass= $server->getAdminPass();
  157. $this->assertEquals(TESTS_ZEND_SERVICE_RACKSPACE_SERVER_NAME,$server->getName());
  158. $this->assertTrue($this->waitForStatus('active'));
  159. }
  160. /**
  161. * Test Get Server
  162. */
  163. public function testGetServer()
  164. {
  165. $server= $this->rackspace->getServer(self::$serverId);
  166. $this->assertTrue($server!==false);
  167. $this->assertEquals(TESTS_ZEND_SERVICE_RACKSPACE_SERVER_NAME,$server->getName());
  168. }
  169. /**
  170. * Test list servers
  171. */
  172. public function testListServers()
  173. {
  174. $servers= $this->rackspace->listServers();
  175. $this->assertTrue($servers!==false);
  176. }
  177. /**
  178. * Test change server name
  179. */
  180. public function testChangeServerName()
  181. {
  182. $this->assertTrue($this->rackspace->changeServerName(self::$serverId,TESTS_ZEND_SERVICE_RACKSPACE_SERVER_NAME.'_renamed'));
  183. }
  184. /**
  185. * Test rechange server name
  186. */
  187. public function testRechangeServerName()
  188. {
  189. $this->assertTrue($this->rackspace->changeServerName(self::$serverId,TESTS_ZEND_SERVICE_RACKSPACE_SERVER_NAME));
  190. }
  191. /**
  192. * Test change admin password
  193. */
  194. public function testChangeServerPassword()
  195. {
  196. self::$adminPass= md5(time().rand());
  197. $this->assertTrue($this->rackspace->changeServerPassword(self::$serverId,self::$adminPass));
  198. }
  199. /**
  200. * Test get server IP
  201. */
  202. public function testGetServerIp()
  203. {
  204. $addresses= $this->rackspace->getServerIp(self::$serverId);
  205. $this->assertTrue(!empty($addresses['public']) && is_array($addresses['public']));
  206. $this->assertTrue(!empty($addresses['private']) && is_array($addresses['private']));
  207. }
  208. /**
  209. * Test get server public IP
  210. */
  211. public function testGetServerPublicIp()
  212. {
  213. $public= $this->rackspace->getServerPublicIp(self::$serverId);
  214. $this->assertTrue(!empty($public) && is_array($public));
  215. }
  216. /**
  217. * Test get server private IP
  218. */
  219. public function testGetServerPrivateIp()
  220. {
  221. $private= $this->rackspace->getServerPrivateIp(self::$serverId);
  222. $this->assertTrue(!empty($private) && is_array($private));
  223. }
  224. /**
  225. * Test reboot the server
  226. */
  227. public function testSoftRebootServer()
  228. {
  229. $this->assertTrue($this->rackspace->rebootServer(self::$serverId));
  230. $this->assertTrue($this->waitForStatus('active'));
  231. }
  232. /**
  233. * Test hard reboot the server
  234. */
  235. public function testHardRebootServer()
  236. {
  237. $this->assertTrue($this->rackspace->rebootServer(self::$serverId,true));
  238. $this->assertTrue($this->waitForStatus('active'));
  239. }
  240. /**
  241. * Test rebuild the server image
  242. */
  243. public function testRebuildServer()
  244. {
  245. $this->assertTrue($this->rackspace->rebuildServer(self::$serverId,TESTS_ZEND_SERVICE_RACKSPACE_SERVER_NEW_IMAGEID));
  246. }
  247. /**
  248. * Test resize server
  249. */
  250. public function testResizeServer()
  251. {
  252. $this->markTestSkipped('Resize server skipped');
  253. }
  254. /**
  255. * Test confirm resize server
  256. */
  257. public function testConfirmResizeServer()
  258. {
  259. $this->markTestSkipped('Confirm resize server skipped');
  260. }
  261. /**
  262. * Test revert resize server
  263. */
  264. public function testRevertResizeServer()
  265. {
  266. $this->markTestSkipped('Revert resize server skipped');
  267. }
  268. /**
  269. * Test list flavors
  270. */
  271. public function testListFlavors()
  272. {
  273. self::$flavors= $this->rackspace->listFlavors(true);
  274. $this->assertTrue(is_array(self::$flavors) && !empty(self::$flavors));
  275. $this->assertTrue(isset(self::$flavors[0]['id']));
  276. }
  277. /**
  278. * Test get flavor
  279. */
  280. public function testGetFlavor()
  281. {
  282. $flavor= $this->rackspace->getFlavor(self::$flavors[0]['id']);
  283. $this->assertTrue(is_array($flavor) && !empty($flavor));
  284. $this->assertEquals($flavor['id'],self::$flavors[0]['id']);
  285. }
  286. /**
  287. * Test list images
  288. */
  289. public function testListImages()
  290. {
  291. self::$images= $this->rackspace->listImages(true);
  292. $this->assertTrue(count(self::$images)>0);
  293. $image= self::$images[0];
  294. $imageId= $image->getId();
  295. $this->assertTrue(!empty($imageId));
  296. }
  297. /**
  298. * Test get image
  299. */
  300. public function testGetImage()
  301. {
  302. $image= self::$images[0];
  303. $getImage= $this->rackspace->getImage($image->getId());
  304. $this->assertEquals($getImage->getId(),$image->getId());
  305. }
  306. /**
  307. * Test get image info
  308. */
  309. public function testGetImageInfo()
  310. {
  311. $image= $this->rackspace->getImage(self::$images[0]->getId())->toArray();
  312. $this->assertTrue(is_array($image) && !empty($image));
  313. $this->assertEquals($image['id'],self::$images[0]->getId());
  314. }
  315. /**
  316. * Test create image
  317. */
  318. public function testCreateImage()
  319. {
  320. $image= $this->rackspace->createImage(self::$serverId, TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGE_NAME);
  321. if ($image!==false) {
  322. self::$imageId= $image->getId();
  323. }
  324. $this->assertTrue($image!==false);
  325. $this->assertEquals($image->getName(),TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGE_NAME);
  326. }
  327. /**
  328. * Test delete image
  329. */
  330. public function testDeleteImage()
  331. {
  332. if (isset(self::$imageId)) {
  333. $this->assertTrue($this->rackspace->deleteImage(self::$imageId));
  334. } else {
  335. $this->markTestSkipped('Delete image skipped because the new image has not been created');
  336. }
  337. }
  338. /**
  339. * Test get backup schedule
  340. */
  341. public function testGetBackupSchedule()
  342. {
  343. $this->markTestSkipped('Get backup schedule skipped');
  344. }
  345. /**
  346. * Test change backup schedule
  347. */
  348. public function testChangeBackupSchedule()
  349. {
  350. $this->markTestSkipped('Change backup schedule skipped');
  351. }
  352. /**
  353. * Test disable backup schedule
  354. */
  355. public function testDisableBackupSchedule()
  356. {
  357. $this->markTestSkipped('Disable backup schedule skipped');
  358. }
  359. /**
  360. * Test create shared Ip group
  361. */
  362. public function testCreateSharedIpGroup()
  363. {
  364. self::$sharedIpGroup= $this->rackspace->createSharedIpGroup(TESTS_ZEND_SERVICE_RACKSPACE_SERVER_SHARED_IP_GROUP_NAME, self::$serverId);
  365. $this->assertTrue(self::$sharedIpGroup!==false);
  366. $this->assertEquals(self::$sharedIpGroup->getName(),TESTS_ZEND_SERVICE_RACKSPACE_SERVER_SHARED_IP_GROUP_NAME);
  367. }
  368. /**
  369. * Test list shared ip groups
  370. */
  371. public function testListSharedIpGroups()
  372. {
  373. $groups= $this->rackspace->listSharedIpGroups(true);
  374. $this->assertTrue($groups!==false);
  375. }
  376. /**
  377. * Test get shared IP group
  378. */
  379. public function testGetSharedIpGroup()
  380. {
  381. $groupId= self::$sharedIpGroup->getId();
  382. $group= $this->rackspace->getSharedIpGroup($groupId);
  383. $this->assertTrue($group!==false);
  384. $this->assertEquals($group->getId(), $groupId);
  385. }
  386. /**
  387. * Test delete shared ip group
  388. */
  389. public function testDeleteSharedIpGroup()
  390. {
  391. $this->assertTrue($this->rackspace->deleteSharedIpGroup(self::$sharedIpGroup->getId()));
  392. }
  393. /**
  394. * Test delete server
  395. */
  396. public function testDeleteServer()
  397. {
  398. $this->assertTrue($this->rackspace->deleteServer(self::$serverId));
  399. }
  400. }