Skip to content

Commit

Permalink
Looked into return code for nuget download and imporved logging (#6943)
Browse files Browse the repository at this point in the history
* Looked into return code for nuget download and imporved logging

* refactored nested ifs and bumped task version

* added comments
  • Loading branch information
ShreyasRmsft authored Apr 12, 2018
1 parent 9b310fe commit 899457c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
"loc.messages.LookingForLatestStableVersion": "Looking for the latest stable version of the Microsoft.Testplatform.",
"loc.messages.LookingForLatestPreReleaseVersion": "Looking for the latest pre-release version of the Microsoft.Testplatform.",
"loc.messages.AgentWorkDirectoryPathTooLong": "Please ensure that the agent work directory path is less than 50 characters to guarantee successful download of the test platform package.",
"loc.messages.ListPackagesFailed": "Listing packages failed. Nuget.exe returned %s.\nStandard Error:\n%sStandard Out:\n%s"
"loc.messages.ListPackagesFailed": "Listing packages failed. Nuget.exe returned %s.\nStandard Error:\n%sStandard Out:\n%s",
"loc.messages.NugetErrorCode": "Nuget.exe returned error code: %s"
}
3 changes: 1 addition & 2 deletions Tasks/VsTestPlatformToolInstaller/cieventlogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export function publishEvent(subFeature: string, properties: { [key: string]: an
tl.assertAgent('2.125.0');
properties['subFeature'] = subFeature;
tl.publishTelemetry(area, feature, Object.assign(getDefaultProps(), properties));

} catch (err) {
tl.debug('Unable to publish telemetry due to lower agent version.');
tl.debug(`Unable to publish telemetry due to lower agent version.`);
}
}
5 changes: 3 additions & 2 deletions Tasks/VsTestPlatformToolInstaller/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 11
"Patch": 12
},
"preview": true,
"satisfies": ["VsTest"],
Expand Down Expand Up @@ -61,6 +61,7 @@
"LookingForLatestStableVersion": "Looking for the latest stable version of the Microsoft.Testplatform.",
"LookingForLatestPreReleaseVersion" : "Looking for the latest pre-release version of the Microsoft.Testplatform.",
"AgentWorkDirectoryPathTooLong": "Please ensure that the agent work directory path is less than 50 characters to guarantee successful download of the test platform package.",
"ListPackagesFailed": "Listing packages failed. Nuget.exe returned %s.\nStandard Error:\n%sStandard Out:\n%s"
"ListPackagesFailed": "Listing packages failed. Nuget.exe returned %s.\nStandard Error:\n%sStandard Out:\n%s",
"NugetErrorCode": "Nuget.exe returned error code: %s"
}
}
5 changes: 3 additions & 2 deletions Tasks/VsTestPlatformToolInstaller/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 11
"Patch": 12
},
"preview": true,
"satisfies": [
Expand Down Expand Up @@ -63,6 +63,7 @@
"LookingForLatestStableVersion": "ms-resource:loc.messages.LookingForLatestStableVersion",
"LookingForLatestPreReleaseVersion": "ms-resource:loc.messages.LookingForLatestPreReleaseVersion",
"AgentWorkDirectoryPathTooLong": "ms-resource:loc.messages.AgentWorkDirectoryPathTooLong",
"ListPackagesFailed": "ms-resource:loc.messages.ListPackagesFailed"
"ListPackagesFailed": "ms-resource:loc.messages.ListPackagesFailed",
"NugetErrorCode": "ms-resource:loc.messages.NugetErrorCode"
}
}
105 changes: 64 additions & 41 deletions Tasks/VsTestPlatformToolInstaller/vstestplatformtoolinstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@ import * as ci from './cieventlogger';
import { exec } from 'child_process';
const perf = require('performance-now');

const executionStartTime = perf();
const osPlat: string = os.platform();
const packageName = 'Microsoft.TestPlatform';
let packageSource = 'https://api.nuget.org/v3/index.json';

async function startInstaller() {
tl.setResourcePath(path.join(__dirname, 'task.json'));
ci.publishEvent('Start', { OS: osPlat, isSupportedOS: (osPlat === 'win32').toString(), startTime: executionStartTime } );

if (osPlat !== 'win32') {
// Fail the task if os is not windows
tl.setResult(tl.TaskResult.Failed, tl.loc('OnlyWindowsOsSupported'));
return;
}
let executionStartTime;

try {
const osPlat: string = os.platform();
executionStartTime = perf();

tl.setResourcePath(path.join(__dirname, 'task.json'));

console.log(tl.loc('StartingInstaller'));
console.log('==============================================================================');

ci.publishEvent('Start', { OS: osPlat, isSupportedOS: (osPlat === 'win32').toString(), startTime: executionStartTime } );

if (osPlat !== 'win32') {
// Fail the task if os is not windows
tl.setResult(tl.TaskResult.Failed, tl.loc('OnlyWindowsOsSupported'));
return;
}

// Read task inputs
const versionSelectorInput = tl.getInput('versionSelector', true);
const testPlatformVersion = tl.getInput('testPlatformVersion', false);
Expand All @@ -49,9 +53,9 @@ async function startInstaller() {
} catch (error) {
ci.publishEvent('Completed', { isSetupSuccessful: 'false', error: error.message } );
tl.setResult(tl.TaskResult.Failed, error.message);
} finally {
ci.publishEvent('Completed', { isSetupSuccessful: 'true', startTime: executionStartTime, endTime: perf() } );
}

ci.publishEvent('Completed', { isSetupSuccessful: 'true', startTime: executionStartTime, endTime: perf() } );
}

async function getVsTestPlatformTool(testPlatformVersion: string, versionSelectorInput: string) {
Expand Down Expand Up @@ -97,37 +101,49 @@ async function getVsTestPlatformTool(testPlatformVersion: string, versionSelecto
toolPath = toolLib.findLocalTool('VsTest', testPlatformVersion);
ci.publishEvent('CacheLookup', { CacheHit: (toolPath !== null && toolPath !== undefined && toolPath !== 'undefined').toString(), isFallback: 'false', version: testPlatformVersion, startTime: cacheLookupStartTime, endTime: perf() } );

if (!toolPath || toolPath === 'undefined') {
if (testPlatformVersion && testPlatformVersion !== 'x') {
tl.debug(`Could not find ${packageName}.${testPlatformVersion} in the tools cache. Fetching it from nuget.`);
if (toolLib.isExplicitVersion(testPlatformVersion)) {
// Download the required version and cache it
try {
toolPath = await acquireAndCacheVsTestPlatformNuget(testPlatformVersion);
} catch (error) {
// Download failed, look for the latest version available in the cache
tl.warning(tl.loc('TestPlatformDownloadFailed', testPlatformVersion));
ci.publishEvent('DownloadFailed', { action: 'getLatestAvailableInCache', error: error } );
testPlatformVersion = 'x';
cacheLookupStartTime = perf();
toolPath = toolLib.findLocalTool('VsTest', testPlatformVersion);
ci.publishEvent('CacheLookup', { CacheHit: (toolPath !== null && toolPath !== undefined && toolPath !== 'undefined').toString(), isFallback: 'true', version: testPlatformVersion, startTime: cacheLookupStartTime, endTime: perf() } );
if (!toolPath || toolPath === 'undefined') {
// No version found in cache, fail the task
tl.warning(tl.loc('NoPackageFoundInCache'));
throw new Error(tl.loc('FailedToAcquireTestPlatform'));
}
}
} else {
ci.publishEvent('InvalidVersionSpecified', { version: testPlatformVersion } );
throw new Error(tl.loc('ProvideExplicitVersion', testPlatformVersion));
}
} else {
// If found in the cache then set the tool location and return
if (toolPath && toolPath !== 'undefined') {
setVsTestToolLocation(toolPath);
return;
}

// If the testPlatformVersion is 'x' meaning listing failed and we were looking for a stable version in the cache
// and the cache lookup failed, then fail the task
if (!testPlatformVersion || testPlatformVersion === 'x') {
tl.warning(tl.loc('NoPackageFoundInCache'));
throw new Error(tl.loc('FailedToAcquireTestPlatform'));
}

// If the version provided is not an explicit version (ie contains containing wildcards) then throw
if (!toolLib.isExplicitVersion(testPlatformVersion)) {
ci.publishEvent('InvalidVersionSpecified', { version: testPlatformVersion } );
throw new Error(tl.loc('ProvideExplicitVersion', testPlatformVersion));
}

// Download the required version and cache it
try {
tl.debug(`Could not find ${packageName}.${testPlatformVersion} in the tools cache. Fetching it from nuget.`);
toolPath = await acquireAndCacheVsTestPlatformNuget(testPlatformVersion);
} catch (error) {
// Download failed, look for the latest version available in the cache
tl.warning(tl.loc('TestPlatformDownloadFailed', testPlatformVersion));
ci.publishEvent('DownloadFailed', { action: 'getLatestAvailableInCache', error: error } );
testPlatformVersion = 'x';
cacheLookupStartTime = perf();
toolPath = toolLib.findLocalTool('VsTest', testPlatformVersion);
ci.publishEvent('CacheLookup', { CacheHit: (toolPath !== null && toolPath !== undefined && toolPath !== 'undefined').toString(), isFallback: 'true', version: testPlatformVersion, startTime: cacheLookupStartTime, endTime: perf() } );
if (!toolPath || toolPath === 'undefined') {
// No version found in cache, fail the task
tl.warning(tl.loc('NoPackageFoundInCache'));
throw new Error(tl.loc('FailedToAcquireTestPlatform'));
}
}

// Set the vstest platform tool location for the vstest task to consume
setVsTestToolLocation(toolPath);
}

function setVsTestToolLocation(toolPath: string) {
// Set the task variable so that the VsTest task can consume this path
tl.setVariable('VsTestToolsInstallerInstalledToolLocation', toolPath);
console.log(tl.loc('InstallationSuccessful', toolPath));
Expand All @@ -154,14 +170,14 @@ function getLatestPackageVersionNumber(includePreRelease: boolean): string {
ci.publishEvent('ListLatestVersion', { includePreRelease: includePreRelease, startTime: startTime, endTime: perf() } );

if (result.code !== 0 || !(result.stderr === null || result.stderr === undefined || result.stderr === '')) {
tl.debug(`Nuget.exe returned error code: ${result.code}`);
tl.warning(tl.loc('NugetErrorCode', result.code));
throw new Error(tl.loc('ListPackagesFailed', result.code, result.stderr, result.stdout));
}

const listOfPackages = result.stdout.split('\r\n');
let version: string;

// nuget returns latest vesions of all packages that match the given name, we need to filter out the exact package we need from this list
// Nuget returns latest vesions of all packages that match the given name, we need to filter out the exact package we need from this list
listOfPackages.forEach(nugetPackage => {
if (nugetPackage.split(' ')[0] === packageName) {
version = nugetPackage.split(' ')[1];
Expand Down Expand Up @@ -194,7 +210,14 @@ async function acquireAndCacheVsTestPlatformNuget(testPlatformVersion: string):

tl.debug(`Downloading Test Platform version ${testPlatformVersion} from ${packageSource} to ${downloadPath}.`);
let startTime = perf();
await nugetTool.exec();
const resultCode = await nugetTool.exec();

tl.debug(`Nuget.exe returned with result code ${resultCode}`);

if (resultCode !== 0) {
tl.warning(tl.loc('NugetErrorCode', resultCode));
throw new Error(`Download failed with error code: ${resultCode}.`);
}

ci.publishEvent('DownloadPackage', { version: testPlatformVersion, startTime: startTime, endTime: perf() } );

Expand Down

0 comments on commit 899457c

Please sign in to comment.