2
0

ManagementClientTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_WindowsAzure
  17. * @subpackage UnitTests
  18. * @version $Id$
  19. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. date_default_timezone_set('UTC');
  23. if (!defined('PHPUnit_MAIN_METHOD')) {
  24. define('PHPUnit_MAIN_METHOD', 'Zend_Service_WindowsAzure_Management_ManagementClientTest::main');
  25. }
  26. /**
  27. * Test helpers
  28. */
  29. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  30. require_once dirname(__FILE__) . '/../../../../TestConfiguration.php.dist';
  31. require_once 'PHPUnit/Framework/TestCase.php';
  32. /** Zend_Service_WindowsAzure_Management_Client */
  33. require_once 'Zend/Service/WindowsAzure/Management/Client.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Service_WindowsAzure
  37. * @subpackage UnitTests
  38. * @version $Id$
  39. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Zend_Service_WindowsAzure_Management_ManagementClientTest extends PHPUnit_Framework_TestCase
  43. {
  44. static $path;
  45. static $debug = true;
  46. protected $packageUrl;
  47. public function __construct()
  48. {
  49. self::$path = dirname(__FILE__).'/_files/';
  50. }
  51. public static function main()
  52. {
  53. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_RUNTESTS) {
  54. $suite = new PHPUnit_Framework_TestSuite("Zend_Service_WindowsAzure_Management_ManagementClientTest");
  55. $result = PHPUnit_TextUI_TestRunner::run($suite);
  56. }
  57. }
  58. /**
  59. * Test setup
  60. */
  61. protected function setUp()
  62. {
  63. // Upload sample package to Windows Azure
  64. $storageClient = $this->createStorageInstance();
  65. $storageClient->createContainerIfNotExists(TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_CONTAINER);
  66. $storageClient->putBlob(TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_CONTAINER, 'PhpOnAzure.cspkg', self::$path . 'PhpOnAzure.cspkg');
  67. $this->packageUrl = $storageClient->listBlobs(TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_CONTAINER);
  68. $this->packageUrl = $this->packageUrl[0]->Url;
  69. }
  70. /**
  71. * Test teardown
  72. */
  73. protected function tearDown()
  74. {
  75. // Clean up storage
  76. $storageClient = $this->createStorageInstance();
  77. $storageClient->deleteContainer(TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_CONTAINER);
  78. // Clean up subscription
  79. $managementClient = $this->createManagementClient();
  80. // Remove deployment
  81. try { $managementClient->updateDeploymentStatusBySlot(TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_SERVICENAME, 'production', 'suspended'); $managementClient->waitForOperation(); } catch (Exception $ex) { }
  82. try { $managementClient->deleteDeploymentBySlot(TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_SERVICENAME, 'production'); $managementClient->waitForOperation(); } catch (Exception $ex) { }
  83. // Remove hosted service
  84. try { $managementClient->deleteHostedService(TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_SERVICENAME); $managementClient->waitForOperation(); } catch (Exception $ex) { }
  85. // Remove affinity group
  86. try { $managementClient->deleteAffinityGroup('test'); } catch (Exception $ex) { }
  87. }
  88. protected function createStorageInstance()
  89. {
  90. return new Zend_Service_WindowsAzure_Storage_Blob(TESTS_ZEND_SERVICE_WINDOWSAZURE_BLOB_HOST_PROD, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_ACCOUNT_PROD, TESTS_ZEND_SERVICE_WINDOWSAZURE_STORAGE_KEY_PROD, false, Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::retryN(10, 250));
  91. }
  92. protected function createManagementClient()
  93. {
  94. return new Zend_Service_WindowsAzure_Management_Client(
  95. TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_SUBSCRIPTIONID, self::$path . '/management.pem', TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_CERTIFICATEPASSWORD);
  96. }
  97. protected function log($message)
  98. {
  99. if (self::$debug) {
  100. echo date('Y-m-d H:i:s') . ' - ' . $message . "\r\n";
  101. }
  102. }
  103. /**
  104. * Test hosted service
  105. */
  106. public function testHostedService()
  107. {
  108. if (TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_RUNTESTS) {
  109. // Create a deployment name
  110. $deploymentName = 'deployment' . time();
  111. // Create a management client
  112. $managementClient = $this->createManagementClient();
  113. // ** Step 1: create an affinity group
  114. $this->log('Creating affinity group...');
  115. $managementClient->createAffinityGroup('test', 'test', 'A test affinity group.', 'North Central US');
  116. $this->log('Created affinity group.');
  117. // ** Step 2: create a hosted service
  118. $this->log('Creating hosted service...');
  119. $managementClient->createHostedService(TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_SERVICENAME, TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_SERVICENAME, TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_SERVICENAME, null, 'test');
  120. $managementClient->waitForOperation();
  121. $this->log('Created hosted service.');
  122. // ** Step 3: create a new deployment
  123. $this->log('Creating staging deployment...');
  124. $managementClient->createDeployment(TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_SERVICENAME, 'staging', $deploymentName, $deploymentName, $this->packageUrl, self::$path . 'ServiceConfiguration.cscfg', false, false);
  125. $managementClient->waitForOperation();
  126. $this->log('Created staging deployment.');
  127. // ** Step 4: Run the deployment
  128. $this->log('Changing status of staging deployment to running...');
  129. $managementClient->updateDeploymentStatusBySlot(TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_SERVICENAME, 'staging', 'running');
  130. $managementClient->waitForOperation();
  131. $this->log('Changed status of staging deployment to running.');
  132. // ** Step 5: Swap production <-> staging
  133. $this->log('Performing VIP swap...');
  134. $result = $managementClient->getHostedServiceProperties(TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_SERVICENAME);
  135. $managementClient->swapDeployment(TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_SERVICENAME, $deploymentName, $result->Deployments[0]->Name);
  136. $managementClient->waitForOperation();
  137. $this->log('Performed VIP swap.');
  138. // ** Step 6: Scale to two instances
  139. $this->log('Scaling out...');
  140. $managementClient->setInstanceCountBySlot(TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_SERVICENAME, 'production', 'PhpOnAzure.Web', 2);
  141. $managementClient->waitForOperation();
  142. $this->log('Scaled out.');
  143. // ** Step 7: Scale back
  144. $this->log('Scaling in...');
  145. $managementClient->setInstanceCountBySlot(TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_SERVICENAME, 'production', 'PhpOnAzure.Web', 1);
  146. $managementClient->waitForOperation();
  147. $this->log('Scaled in.');
  148. // ** Step 8: Reboot
  149. $this->log('Rebooting...');
  150. $managementClient->rebootRoleInstanceBySlot(TESTS_ZEND_SERVICE_WINDOWSAZURE_MANAGEMENT_SERVICENAME, 'production', 'PhpOnAzure.Web_IN_0');
  151. $managementClient->waitForOperation();
  152. $this->log('Rebooted.');
  153. // Dumb assertion...
  154. $this->assertTrue(true);
  155. }
  156. }
  157. }
  158. // Call Zend_Service_WindowsAzure_Management_ManagementClientTest::main() if this source file is executed directly.
  159. if (PHPUnit_MAIN_METHOD == "Zend_Service_WindowsAzure_Management_ManagementClientTest::main") {
  160. Zend_Service_WindowsAzure_Management_ManagementClientTest::main();
  161. }