|
| 1 | +import * as path from 'path'; |
| 2 | +import tmrm = require('azure-pipelines-task-lib/mock-run'); |
| 3 | +import { setEndpointData, setAgentsData, mockTaskArgument, nock, MOCK_SUBSCRIPTION_ID, mockAzureSpringCloudExists, mockCommonAzureAPIs, API_VERSION } from './mock_utils'; |
| 4 | +import { ASC_RESOURCE_TYPE, MOCK_RESOURCE_GROUP_NAME } from './mock_utils' |
| 5 | +import assert = require('assert'); |
| 6 | + |
| 7 | + |
| 8 | +const MOCK_DEPLOYMENT_STATUS_ENDPOINT = `/subscriptions/${MOCK_SUBSCRIPTION_ID}/resourceGroups/${MOCK_RESOURCE_GROUP_NAME}/providers/Microsoft.AppPlatform/locations/eastus2/operationStatus/default/operationId/mockoperationid?api-version=${API_VERSION}` |
| 9 | + |
| 10 | +export class DeploymentCustomImageToStagingSucceedsL0 { |
| 11 | + |
| 12 | + static readonly TEST_NAME = 'DeploymentCustomImageToStagingSucceedsL0'; |
| 13 | + static readonly MOCK_APP_NAME = 'testcontainerapp'; |
| 14 | + |
| 15 | + |
| 16 | + public static startTest() { |
| 17 | + console.log(`running ${this.TEST_NAME}`); |
| 18 | + let taskPath = path.join(__dirname, '..', 'azurespringclouddeployment.js'); |
| 19 | + let taskMockRunner: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath); |
| 20 | + setEndpointData(); |
| 21 | + setAgentsData(); |
| 22 | + mockCommonAzureAPIs(); |
| 23 | + mockAzureSpringCloudExists(this.TEST_NAME); |
| 24 | + this.mockTwoDeployments(); |
| 25 | + let nockScope = this.mockDeploymentApis(); |
| 26 | + |
| 27 | + taskMockRunner.setAnswers(mockTaskArgument()); |
| 28 | + taskMockRunner.run(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Simulate a deployment list API that returns a production deployment and a staging deployment. |
| 33 | + */ |
| 34 | + private static mockTwoDeployments() { |
| 35 | + nock('https://management.azure.com', { |
| 36 | + reqheaders: { |
| 37 | + "authorization": "Bearer DUMMY_ACCESS_TOKEN", |
| 38 | + "content-type": "application/json; charset=utf-8", |
| 39 | + "user-agent": "TFS_useragent" |
| 40 | + } |
| 41 | + }).get(`/subscriptions/${MOCK_SUBSCRIPTION_ID}/resourceGroups/${encodeURIComponent(MOCK_RESOURCE_GROUP_NAME)}/providers/${ASC_RESOURCE_TYPE}/${this.TEST_NAME}/apps/${this.MOCK_APP_NAME}/deployments?api-version=${API_VERSION}`) |
| 42 | + .reply(200, { |
| 43 | + "value": [ |
| 44 | + { |
| 45 | + "id": `/subscriptions/${MOCK_SUBSCRIPTION_ID}/resourceGroups/${encodeURIComponent(MOCK_RESOURCE_GROUP_NAME)}/providers/${ASC_RESOURCE_TYPE}/${this.TEST_NAME}/apps/${this.MOCK_APP_NAME}/deployments/default`, |
| 46 | + "name": "default", |
| 47 | + "properties": { |
| 48 | + "active": true, |
| 49 | + "appName": this.MOCK_APP_NAME, |
| 50 | + "deploymentSettings": { |
| 51 | + "cpu": 1, |
| 52 | + "environmentVariables": null, |
| 53 | + "memoryInGB": 1, |
| 54 | + "runtimeVersion": "Java_8" |
| 55 | + }, |
| 56 | + "instances": [ |
| 57 | + { |
| 58 | + "discoveryStatus": "UP", |
| 59 | + "name": `${this.MOCK_APP_NAME}-default-7-7b77f5b6f5-fff9t`, |
| 60 | + "startTime": "2021-03-13T01:39:20Z", |
| 61 | + "status": "Running" |
| 62 | + } |
| 63 | + ], |
| 64 | + "provisioningState": "Succeeded", |
| 65 | + "source": { |
| 66 | + "relativePath": "<default>", |
| 67 | + "type": "Jar" |
| 68 | + }, |
| 69 | + "status": "Running" |
| 70 | + }, |
| 71 | + "resourceGroup": MOCK_RESOURCE_GROUP_NAME, |
| 72 | + "sku": { |
| 73 | + "capacity": 1, |
| 74 | + "name": "S0", |
| 75 | + "tier": "Standard" |
| 76 | + }, |
| 77 | + "type": `${ASC_RESOURCE_TYPE}/apps/deployments` |
| 78 | + }, |
| 79 | + { |
| 80 | + "id": `/subscriptions/${MOCK_SUBSCRIPTION_ID}/resourceGroups/${encodeURIComponent(MOCK_RESOURCE_GROUP_NAME)}/providers/${ASC_RESOURCE_TYPE}/${this.TEST_NAME}/apps/${this.MOCK_APP_NAME}/deployments/theOtherOne`, |
| 81 | + "name": "theOtherOne", |
| 82 | + "properties": { |
| 83 | + "active": false, |
| 84 | + "appName": this.MOCK_APP_NAME, |
| 85 | + "deploymentSettings": { |
| 86 | + "cpu": 1, |
| 87 | + "environmentVariables": null, |
| 88 | + "memoryInGB": 1, |
| 89 | + "runtimeVersion": "Java_8" |
| 90 | + }, |
| 91 | + "instances": [ |
| 92 | + { |
| 93 | + "discoveryStatus": "UP", |
| 94 | + "name": `${this.MOCK_APP_NAME}-theOtherOne-7-7b77f5b6f5-90210`, |
| 95 | + "startTime": "2021-03-13T01:39:20Z", |
| 96 | + "status": "Running" |
| 97 | + } |
| 98 | + ], |
| 99 | + "provisioningState": "Succeeded", |
| 100 | + "source": { |
| 101 | + "relativePath": "<default>", |
| 102 | + "type": "Jar" |
| 103 | + }, |
| 104 | + "status": "Running" |
| 105 | + }, |
| 106 | + "resourceGroup": MOCK_RESOURCE_GROUP_NAME, |
| 107 | + "sku": { |
| 108 | + "capacity": 1, |
| 109 | + "name": "S0", |
| 110 | + "tier": "Standard" |
| 111 | + }, |
| 112 | + "type": `${ASC_RESOURCE_TYPE}/apps/deployments` |
| 113 | + }] |
| 114 | + |
| 115 | + }).persist(); |
| 116 | + } |
| 117 | + |
| 118 | + /** Simulate APIs invoked as part of deployment */ |
| 119 | + private static mockDeploymentApis() { |
| 120 | + //mock get resource upload URL |
| 121 | + nock('https://management.azure.com', { |
| 122 | + reqheaders: { |
| 123 | + "authorization": "Bearer DUMMY_ACCESS_TOKEN", |
| 124 | + "content-type": "application/json; charset=utf-8", |
| 125 | + "user-agent": "TFS_useragent" |
| 126 | + } |
| 127 | + }) |
| 128 | + // mock listTestKeys |
| 129 | + .post(`/subscriptions/${MOCK_SUBSCRIPTION_ID}/resourceGroups/${MOCK_RESOURCE_GROUP_NAME}/providers/${ASC_RESOURCE_TYPE}/${this.TEST_NAME}/listTestKeys?api-version=${API_VERSION}`) |
| 130 | + .once() |
| 131 | + .reply(200, |
| 132 | + { |
| 133 | + "primaryKey": "mockPrimaryKey", |
| 134 | + "secondaryKey": "mockSecondaryKey", |
| 135 | + "primaryTestEndpoint": `https://primary:mockPrimaryKey@${this.MOCK_APP_NAME}.test.azuremicroservices.io`, |
| 136 | + "secondaryTestEndpoint": `https://secondary:mockSecondaryKey@${this.MOCK_APP_NAME}.test.azuremicroservices.io`, |
| 137 | + "enabled": true |
| 138 | + } |
| 139 | + ) |
| 140 | + |
| 141 | + // Mock the deployment update API: |
| 142 | + |
| 143 | + .patch(`/subscriptions/${MOCK_SUBSCRIPTION_ID}/resourceGroups/${encodeURIComponent(MOCK_RESOURCE_GROUP_NAME)}/providers/${ASC_RESOURCE_TYPE}/${this.TEST_NAME}/apps/${this.MOCK_APP_NAME}/deployments/theOtherOne?api-version=${API_VERSION}`) |
| 144 | + .once() |
| 145 | + .reply((uri, serializedRequestBody) => { |
| 146 | + let requestBody = JSON.parse(serializedRequestBody); |
| 147 | + assert.strictEqual(requestBody.properties.source.type, 'Container'); |
| 148 | + assert.strictEqual(requestBody.properties.source.customerContainer.containerImage, 'azurespringcloudtesting/byoc-it-springboot:v1'); |
| 149 | + let responseBody = { |
| 150 | + "provisioningState": "Updating" |
| 151 | + } |
| 152 | + let returnHeaders = { |
| 153 | + 'azure-asyncoperation': 'https://management.azure.com' + MOCK_DEPLOYMENT_STATUS_ENDPOINT |
| 154 | + } |
| 155 | + return [202, responseBody, returnHeaders]; |
| 156 | + |
| 157 | + }) |
| 158 | + |
| 159 | + // Mock the operation status URL |
| 160 | + .get(MOCK_DEPLOYMENT_STATUS_ENDPOINT) |
| 161 | + .once() |
| 162 | + .reply(200, { |
| 163 | + status: "Completed" |
| 164 | + }) |
| 165 | + |
| 166 | + .persist(); |
| 167 | + |
| 168 | + } |
| 169 | +} |
0 commit comments