Skip to content

Commit

Permalink
Try fixing abort
Browse files Browse the repository at this point in the history
  • Loading branch information
Anna Kocheshkova committed Jun 25, 2020
1 parent 86f3ca4 commit f570b5e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 16 deletions.
@@ -1,7 +1,9 @@
import ma = require('vsts-task-lib/mock-answer');
import tmrm = require('vsts-task-lib/mock-run');
import path = require('path');
import fs = require('fs');
import azureBlobUploadHelper = require('../azure-blob-upload-helper');
const Stats = require('fs').Stats;

var nock = require('nock');

Expand All @@ -11,28 +13,46 @@ let tmr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath);

tmr.setInput('serverEndpoint', 'MyTestEndpoint');
tmr.setInput('appSlug', 'testuser/testapp');
tmr.setInput('app', '/test/path/to/my.zip');
tmr.setInput('app', './test.zip');
tmr.setInput('releaseNotesSelection', 'releaseNotesInput');
tmr.setInput('releaseNotesInput', 'my release notes');
tmr.setInput('isMandatory', 'True');
tmr.setInput('destinationType', 'stores');
tmr.setInput('destinationStoreId', '11111111-1111-1111-1111-111111111111');

//prepare upload
nock('https://example.test')
.post('/v0.1/apps/testuser/testapp/release_uploads', body => !body.build_version)
.post('/v0.1/apps/testuser/testapp/uploads/releases', body => !body.build_version)
.reply(201, {
upload_id: 1,
upload_url: 'https://example.upload.test/release_upload'
id: 1,
upload_url: "https://upload.example.test/upload/upload_chunk/00000000-0000-0000-0000-000000000000",
package_asset_id: 1,
upload_domain: 'https://example.upload.test/release_upload',
url_encoded_token: "fdsf"
});

//upload
nock('https://example.upload.test')
.post('/release_upload') // we expect that uploading zip without build version leads to error
.post('/release_upload/upload/set_metadata/1')
.query(true)
.reply(200, {
resume_restart: false,
chunk_list: [1],
chunk_size: 100,
blob_partitions: 1
});

nock('https://example.upload.test')
.post('/release_upload/upload/upload_chunk/1')
.query(true)
.times(21)
.reply(422, {
status: 'failed'

});

nock('https://example.test')
.patch('/v0.1/apps/testuser/testapp/uploads/releases/1')
.reply(200, {
}).log(console.log);

fs.createReadStream = (s: string) => {
let stream = new Readable;
stream.push(s);
Expand All @@ -45,6 +65,43 @@ azureBlobUploadHelper.AzureBlobUploadHelper.prototype.upload = async () => {
return Promise.resolve();
}

// provide answers for task mock
let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
"checkPath" : {
"./test.zip": true,
},
"findMatch" : {
"./test.zip": [
"./test.zip"
]
}
};
tmr.setAnswers(a);
let fsos = fs.openSync;
fs.openSync = (path: string, flags: string) => {
if (path.endsWith("test.zip")){
console.log("Using mocked fs.openSync");
return 1234567.89;
}
return fsos(path, flags);
};
let fsrs = fs.readSync;
fs.readSync = (fd: number, buffer: Buffer, offset: number, length: number, position: number)=> {
if (fd==1234567.89) {
buffer = new Buffer(100);
console.log("Using mocked fs.readSync");
return;
}
return fsrs(fd, buffer, offset, length, position);
};
fs.statSync = (s: string) => {
const stat = new Stats;
stat.isFile = () => s.endsWith('.txt') || s.endsWith('.zip');
stat.isDirectory = () => !s.endsWith('.txt') && !s.endsWith('.zip');
stat.size = 100;
return stat;
}
fs.lstatSync = fs.statSync;
tmr.registerMock('azure-blob-upload-helper', azureBlobUploadHelper);
tmr.registerMock('fs', fs);
tmr.run();
Expand Down
28 changes: 20 additions & 8 deletions Tasks/AppCenterDistributeV3/appcenterdistribute.ts
Expand Up @@ -184,12 +184,19 @@ function uploadRelease(releaseUploadParams: any, file: string): Q.Promise<void>
};
mcFusUploader = new McFusNodeUploader(uploadSettings);
const fullFile = path.resolve(file);
console.log(fullFile);
const appFile = new McFile(fullFile);
mcFusUploader.start(appFile);
return defer.promise;
}

function tryAbort(apiServer: string, apiVersion: string, appSlug: string, upload_id: string, token: string, userAgent: string, error: Error): Q.Promise<void> {
try {
return abortReleaseUpload(apiServer, apiVersion, appSlug, upload_id, token, userAgent);
} catch (abortError) {
tl.debug("---- Failed to abort release upload");
}
}

function abortReleaseUpload(apiServer: string, apiVersion: string, appSlug: string, upload_id: string, token: string, userAgent: string): Q.Promise<void> {
tl.debug("-- Aborting release...");
let defer = Q.defer<void>();
Expand Down Expand Up @@ -627,16 +634,21 @@ async function run() {
try {

// Perform the upload
await uploadRelease(uploadInfo, app);
await uploadRelease(uploadInfo, app).catch(async error => {
console.log("ff");
await tryAbort(effectiveApiServer, effectiveApiVersion, appSlug, uploadId, apiToken, userAgent, error);
console.log("ff1");
tl.setResult(tl.TaskResult.Failed, `${error}`);
console.log("ff2");
return;
});
await patchRelease(effectiveApiServer, effectiveApiVersion, appSlug, uploadId, apiToken, userAgent);
releaseId = await loadReleaseIdUntilSuccess(effectiveApiServer, effectiveApiVersion, appSlug, uploadId, apiToken, userAgent);
} catch (error) {
try {
return abortReleaseUpload(effectiveApiServer, effectiveApiVersion, appSlug, uploadId, apiToken, userAgent);
} catch (abortError) {
tl.debug("---- Failed to abort release upload");
}
throw error;
console.log("ff4");
await tryAbort(effectiveApiServer, effectiveApiVersion, appSlug, uploadId, apiToken, userAgent, error);
tl.setResult(tl.TaskResult.Failed, `${error}`);
return;
}
await updateRelease(effectiveApiServer, effectiveApiVersion, appSlug, releaseId, releaseNotes, apiToken, userAgent);

Expand Down

0 comments on commit f570b5e

Please sign in to comment.