OnlineTest.php 13 KB

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