OfflineTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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-2014 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/Test.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Service_Rackspace_Servers
  26. * @subpackage UnitTests
  27. * @copyright Copyright (c) 2005-2014 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_OfflineTest 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 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_Test
  85. */
  86. protected $httpClientAdapterTest;
  87. /**
  88. * SetUpBerofeClass
  89. */
  90. public function setUp()
  91. {
  92. $this->rackspace= new Zend_Service_Rackspace_Servers('foo','bar');
  93. $this->httpClientAdapterTest = new Zend_Http_Client_Adapter_Test();
  94. $this->rackspace->getHttpClient()
  95. ->setAdapter($this->httpClientAdapterTest);
  96. // authentication (from a file)
  97. $this->httpClientAdapterTest->setResponse(self::loadResponse('../../_files/testAuthenticate'));
  98. $this->assertTrue($this->rackspace->authenticate(),'Authentication failed');
  99. // load the HTTP response (from a file)
  100. $this->httpClientAdapterTest->setResponse($this->loadResponse($this->getName()));
  101. }
  102. /**
  103. * Utility method for returning a string HTTP response, which is loaded from a file
  104. *
  105. * @param string $name
  106. * @return string
  107. */
  108. protected function loadResponse($name)
  109. {
  110. return @file_get_contents(__DIR__ . '/_files/' . $name . '.response');
  111. }
  112. /**
  113. * Test constants
  114. */
  115. public function testConstants()
  116. {
  117. $this->assertEquals(10240, Zend_Service_Rackspace_Servers::LIMIT_FILE_SIZE);
  118. $this->assertEquals(5, Zend_Service_Rackspace_Servers::LIMIT_NUM_FILE);
  119. $this->assertEquals('json', Zend_Service_Rackspace_Servers::API_FORMAT);
  120. }
  121. /**
  122. * Test create server
  123. */
  124. public function testCreateServer()
  125. {
  126. $data = array (
  127. 'name' => TESTS_ZEND_SERVICE_RACKSPACE_SERVER_NAME,
  128. 'imageId' => TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGEID,
  129. 'flavorId' => TESTS_ZEND_SERVICE_RACKSPACE_SERVER_FLAVORID
  130. );
  131. $server= $this->rackspace->createServer($data);
  132. $this->assertTrue($server!==false);
  133. self::$serverId= $server->getId();
  134. $this->assertEquals(TESTS_ZEND_SERVICE_RACKSPACE_SERVER_NAME,$server->getName());
  135. $this->assertEquals(self::$serverId,20247478);
  136. }
  137. /**
  138. * Test Get Server
  139. */
  140. public function testGetServer()
  141. {
  142. $server= $this->rackspace->getServer(self::$serverId);
  143. $this->assertTrue($server!==false);
  144. $this->assertEquals(TESTS_ZEND_SERVICE_RACKSPACE_SERVER_NAME,$server->getName());
  145. }
  146. /**
  147. * Test list servers
  148. */
  149. public function testListServers()
  150. {
  151. $servers= $this->rackspace->listServers();
  152. $this->assertTrue($servers!==false);
  153. }
  154. /**
  155. * Test change server name
  156. */
  157. public function testChangeServerName()
  158. {
  159. $this->assertTrue($this->rackspace->changeServerName(self::$serverId,TESTS_ZEND_SERVICE_RACKSPACE_SERVER_NAME.'_renamed'));
  160. }
  161. /**
  162. * Test rechange server name
  163. */
  164. public function testRechangeServerName()
  165. {
  166. $this->assertTrue($this->rackspace->changeServerName(self::$serverId,TESTS_ZEND_SERVICE_RACKSPACE_SERVER_NAME));
  167. }
  168. /**
  169. * Test change admin password
  170. */
  171. public function testChangeServerPassword()
  172. {
  173. self::$adminPass= md5(time().rand());
  174. $this->assertTrue($this->rackspace->changeServerPassword(self::$serverId,self::$adminPass));
  175. }
  176. /**
  177. * Test get server IP
  178. */
  179. public function testGetServerIp()
  180. {
  181. $addresses= $this->rackspace->getServerIp(self::$serverId);
  182. $this->assertTrue(!empty($addresses['public']) && is_array($addresses['public']));
  183. $this->assertTrue(!empty($addresses['private']) && is_array($addresses['private']));
  184. }
  185. /**
  186. * Test get server public IP
  187. */
  188. public function testGetServerPublicIp()
  189. {
  190. $public= $this->rackspace->getServerPublicIp(self::$serverId);
  191. $this->assertTrue(!empty($public) && is_array($public));
  192. }
  193. /**
  194. * Test get server private IP
  195. */
  196. public function testGetServerPrivateIp()
  197. {
  198. $private= $this->rackspace->getServerPrivateIp(self::$serverId);
  199. $this->assertTrue(!empty($private) && is_array($private));
  200. }
  201. /**
  202. * Test reboot the server
  203. */
  204. public function testSoftRebootServer()
  205. {
  206. $this->assertTrue($this->rackspace->rebootServer(self::$serverId));
  207. }
  208. /**
  209. * Test hard reboot the server
  210. */
  211. public function testHardRebootServer()
  212. {
  213. $this->assertTrue($this->rackspace->rebootServer(self::$serverId,true));
  214. }
  215. /**
  216. * Test rebuild the server image
  217. */
  218. public function testRebuildServer()
  219. {
  220. $this->assertTrue($this->rackspace->rebuildServer(self::$serverId,TESTS_ZEND_SERVICE_RACKSPACE_SERVER_NEW_IMAGEID));
  221. }
  222. /**
  223. * Test resize server
  224. */
  225. public function testResizeServer()
  226. {
  227. $this->markTestSkipped('Resize server skipped');
  228. }
  229. /**
  230. * Test confirm resize server
  231. */
  232. public function testConfirmResizeServer()
  233. {
  234. $this->markTestSkipped('Confirm resize server skipped');
  235. }
  236. /**
  237. * Test revert resize server
  238. */
  239. public function testRevertResizeServer()
  240. {
  241. $this->markTestSkipped('Revert resize server skipped');
  242. }
  243. /**
  244. * Test list flavors
  245. */
  246. public function testListFlavors()
  247. {
  248. self::$flavors= $this->rackspace->listFlavors(true);
  249. $this->assertTrue(is_array(self::$flavors) && !empty(self::$flavors));
  250. $this->assertTrue(isset(self::$flavors[0]['id']));
  251. }
  252. /**
  253. * Test get flavor
  254. */
  255. public function testGetFlavor()
  256. {
  257. $flavor= $this->rackspace->getFlavor(self::$flavors[0]['id']);
  258. $this->assertTrue(is_array($flavor) && !empty($flavor));
  259. $this->assertEquals($flavor['id'],self::$flavors[0]['id']);
  260. }
  261. /**
  262. * Test list images
  263. */
  264. public function testListImages()
  265. {
  266. self::$images= $this->rackspace->listImages(true);
  267. $this->assertTrue(count(self::$images)>0);
  268. $image= self::$images[0];
  269. $imageId= $image->getId();
  270. $this->assertTrue(!empty($imageId));
  271. }
  272. /**
  273. * Test get image
  274. */
  275. public function testGetImage()
  276. {
  277. $image= self::$images[0];
  278. $getImage= $this->rackspace->getImage($image->getId());
  279. $this->assertEquals($getImage->getId(),$image->getId());
  280. }
  281. /**
  282. * Test get image info
  283. */
  284. public function testGetImageInfo()
  285. {
  286. $image= $this->rackspace->getImage(self::$images[0]->getId())->toArray();
  287. $this->assertTrue(is_array($image) && !empty($image));
  288. $this->assertEquals($image['id'],self::$images[0]->getId());
  289. }
  290. /**
  291. * Test create image
  292. */
  293. public function testCreateImage()
  294. {
  295. $image= $this->rackspace->createImage(self::$serverId, TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGE_NAME);
  296. if ($image!==false) {
  297. self::$imageId= $image->getId();
  298. }
  299. $this->assertTrue($image!==false);
  300. $this->assertEquals($image->getName(),TESTS_ZEND_SERVICE_RACKSPACE_SERVER_IMAGE_NAME);
  301. }
  302. /**
  303. * Test delete image
  304. */
  305. public function testDeleteImage()
  306. {
  307. if (isset(self::$imageId)) {
  308. $this->assertTrue($this->rackspace->deleteImage(self::$imageId));
  309. } else {
  310. $this->markTestSkipped('Delete image skipped because the new image has not been created');
  311. }
  312. }
  313. /**
  314. * Test get backup schedule
  315. */
  316. public function testGetBackupSchedule()
  317. {
  318. $this->markTestSkipped('Get backup schedule skipped');
  319. }
  320. /**
  321. * Test change backup schedule
  322. */
  323. public function testChangeBackupSchedule()
  324. {
  325. $this->markTestSkipped('Change backup schedule skipped');
  326. }
  327. /**
  328. * Test disable backup schedule
  329. */
  330. public function testDisableBackupSchedule()
  331. {
  332. $this->markTestSkipped('Disable backup schedule skipped');
  333. }
  334. /**
  335. * Test create shared Ip group
  336. */
  337. public function testCreateSharedIpGroup()
  338. {
  339. self::$sharedIpGroup= $this->rackspace->createSharedIpGroup(TESTS_ZEND_SERVICE_RACKSPACE_SERVER_SHARED_IP_GROUP_NAME, self::$serverId);
  340. $this->assertTrue(self::$sharedIpGroup!==false);
  341. $this->assertEquals(self::$sharedIpGroup->getName(),TESTS_ZEND_SERVICE_RACKSPACE_SERVER_SHARED_IP_GROUP_NAME);
  342. }
  343. /**
  344. * Test list shared ip groups
  345. */
  346. public function testListSharedIpGroups()
  347. {
  348. $groups= $this->rackspace->listSharedIpGroups(true);
  349. $this->assertTrue($groups!==false);
  350. }
  351. /**
  352. * Test get shared IP group
  353. */
  354. public function testGetSharedIpGroup()
  355. {
  356. $groupId= self::$sharedIpGroup->getId();
  357. $group= $this->rackspace->getSharedIpGroup($groupId);
  358. $this->assertTrue($group!==false);
  359. $this->assertEquals($group->getId(), $groupId);
  360. }
  361. /**
  362. * Test delete shared ip group
  363. */
  364. public function testDeleteSharedIpGroup()
  365. {
  366. $this->assertTrue($this->rackspace->deleteSharedIpGroup(self::$sharedIpGroup->getId()));
  367. }
  368. /**
  369. * Test delete server
  370. */
  371. public function testDeleteServer()
  372. {
  373. $this->assertTrue($this->rackspace->deleteServer(self::$serverId));
  374. }
  375. }