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
2 changes: 1 addition & 1 deletion Tasks/ContainerStructureTestV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 0,
"Minor": 1,
navin22 marked this conversation as resolved.
Show resolved Hide resolved
"Patch": 9
"Patch": 10
},
"preview": true,
"demands": [],
Expand Down
2 changes: 1 addition & 1 deletion Tasks/ContainerStructureTestV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 0,
"Minor": 1,
"Patch": 9
"Patch": 10
},
"preview": true,
"demands": [],
Expand Down
62 changes: 36 additions & 26 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,14 +16,22 @@ 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.`);
}

var start = new Date().getTime();
const output: string = this.runContainerStructureTest(runnerPath, this.testFilePath, this.imageName);
const output: string = this.runContainerStructureTest(path.join(toolPath, this.toolName), this.testFilePath, this.imageName);
var end = new Date().getTime();

if (!output || output.length <= 0) {
Expand Down Expand Up @@ -50,30 +59,30 @@ 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();
existsSync(runnerPath);
navin22 marked this conversation as resolved.
Show resolved Hide resolved
chmodSync(runnerPath, "777");
navin22 marked this conversation as resolved.
Show resolved Hide resolved

var toolPath = tl.which(runnerPath);

if(!toolPath) {
navin22 marked this conversation as resolved.
Show resolved Hide resolved
throw "Unable to find the Tool";
}

var tool:tr.ToolRunner = tl.tool(toolPath).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 +100,5 @@ export class TestRunner {
private readonly testFilePath: string;
private readonly imageName: string;
private readonly osType = tl.osType().toLowerCase();
private readonly toolName = "container-structure-test";
}