Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the ToolLib to download and cache the tool #11825

Merged
merged 9 commits into from
Nov 26, 2019
2 changes: 1 addition & 1 deletion Tasks/ContainerStructureTestV0/containerstructuretest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TestResultPublisher, TestSummary } from "./testresultspublisher";
import { TestRunner } from "./testrunner";

const telemetryArea: string = 'TestExecution';
const telemetryFeature: string = 'PublishTestResultsTask';
const telemetryFeature: string = 'ContainerStructureTestTask';
const telemetryData: { [key: string]: any; } = <{ [key: string]: any; }>{};
const defaultRunTitlePrefix: string = 'ContainerStructureTest_TestResults_';
const buildString = "build";
Expand Down
4 changes: 2 additions & 2 deletions Tasks/ContainerStructureTestV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 1,
"Patch": 9
"Minor": 162,
"Patch": 0
},
"preview": true,
"demands": [],
Expand Down
4 changes: 2 additions & 2 deletions Tasks/ContainerStructureTestV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 1,
"Patch": 9
"Minor": 162,
"Patch": 0
},
"preview": true,
"demands": [],
Expand Down
59 changes: 34 additions & 25 deletions Tasks/ContainerStructureTestV0/testrunner.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { TestSummary } from "./testresultspublisher";
import * as tl from 'azure-pipelines-task-lib/task';
import tr = require('azure-pipelines-task-lib/toolrunner');
import { chmodSync, existsSync } from 'fs';
import * as path from "path";
import downloadutility = require("utility-common/downloadutility");
import * as toolLib from 'vsts-task-tool-lib/tool';

export class TestRunner {
constructor(testFilePath: string, imageName: string) {
Expand All @@ -15,12 +16,28 @@ export class TestRunner {
try {
const runnerDownloadUrl = this.getContainerStructureTestRunnerDownloadPath(this.osType);
if (!runnerDownloadUrl) {
throw new Error("Unable to get runner download path");
throw new Error(`Not supported OS: ${this.osType}`);
}

const runnerPath = await this.downloadTestRunner(runnerDownloadUrl);
tl.debug(`Successfully downloaded : ${runnerDownloadUrl}`);
let toolPath = toolLib.findLocalTool(this.toolName, "1.0.0");

if(!toolPath) {
const downloadPath = await toolLib.downloadTool(runnerDownloadUrl);
tl.debug(`Successfully downloaded : ${downloadPath}`);
toolPath = await toolLib.cacheFile(downloadPath, this.toolName, this.toolName, "1.0.0");
tl.debug(`Successfully Added to cache`);
} else {
tl.debug(`Tool is retrieved from cache.`);
}

const runnerPath = path.join(toolPath, this.toolName);

// Checking if tool exists.
if (!existsSync(runnerPath)) {
throw new Error(`Download or caching of tool(${runnerPath}) failed`);
}

chmodSync(runnerPath, "777");
var start = new Date().getTime();
const output: string = this.runContainerStructureTest(runnerPath, this.testFilePath, this.imageName);
var end = new Date().getTime();
Expand Down Expand Up @@ -50,30 +67,21 @@ export class TestRunner {
return null;
}
}

private async downloadTestRunner(downloadUrl: string): Promise<string> {
const gcst = path.join(__dirname, "container-structure-test");
return downloadutility.download(downloadUrl, gcst, false, true).then((res) => {
chmodSync(gcst, "777");
if (!existsSync(gcst)) {
tl.error(tl.loc('FileNotFoundException', path));
throw new Error(tl.loc('FileNotFoundException', path));
}
return gcst;
}).catch((reason) => {
tl.error(tl.loc('DownloadException', reason));
throw new Error(tl.loc('DownloadException', reason));
})
}

private runContainerStructureTest(runnerPath: string, testFilePath: string, image: string): string {
let command = tl.tool(runnerPath);
command.arg(["test", "--image", image, "--config", testFilePath, "--json"]);

const output = command.execSync();
var tool:tr.ToolRunner = tl.tool(runnerPath).arg(["test", "--image", image, "--config", testFilePath, "--json"]);
let output = undefined;

try {
output = tool.execSync();
} catch(error) {
tl.error("Error While executing the tool: " + error);
throw error;
}

let jsonOutput: string;
if (!output.error) {

if (output && !output.error) {
jsonOutput = output.stdout;
} else {
tl.error(tl.loc('ErrorInExecutingCommand', output.error));
Expand All @@ -91,4 +99,5 @@ export class TestRunner {
private readonly testFilePath: string;
private readonly imageName: string;
private readonly osType = tl.osType().toLowerCase();
private readonly toolName = "container-structure-test";
}