ManagementClientTest.php 8.0 KB

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