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

Fixed and added unit tests for JavaToolInstallerV0 task #15355

Merged
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions Tasks/JavaToolInstallerV0/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,26 @@ describe('JavaToolInstaller L0 Suite', function () {
it('should run successfully when fetching JDK files from azure storage', function () {
this.timeout(20000);

const testPath: string = path.join(__dirname, 'L0DownloadArtifactsFromAzureStorage.js');
const testPath: string = path.join(__dirname, 'L0DownloadsJdkFromAzureStorage.js');
const testRunner: ttm.MockTestRunner = new ttm.MockTestRunner(testPath);

testRunner.run();

AnnaOpareva marked this conversation as resolved.
Show resolved Hide resolved
assert(testRunner.succeeded, 'task should have succeeded.');
});

it('should run successfully when fetching JDK files from azure storage from subfolder', function () {
this.timeout(20000);

const testPath: string = path.join(__dirname, 'L0DownloadsJdkFromAzureStorageSubFolder.js');
const testRunner: ttm.MockTestRunner = new ttm.MockTestRunner(testPath);

testRunner.run();

assert(testRunner.stdOutContained('jdkArchiveName: DestinationDirectory\\JDKname.tar.gz'), 'JDK archive should unpack in the right destination directory');
assert(testRunner.succeeded, 'task should have succeeded.');
});

it('should fail when JavaToolInstaller is run with to destination folder specified', function () {
this.timeout(20000);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ma = require('azure-pipelines-task-lib/mock-answer');
import tmrm = require('azure-pipelines-task-lib/mock-run');
import msRestAzure = require('azure-pipelines-tasks-azure-arm-rest-v2/azure-arm-common');
import path = require('path');
import mockTask = require('azure-pipelines-task-lib/mock-task');

Expand All @@ -12,10 +13,13 @@ tr.setInput("jdkArchitectureOption", "x64");
tr.setInput("azureResourceManagerEndpoint", "ARM1");
tr.setInput("azureStorageAccountName", "storage1");
tr.setInput("azureContainerName", "container1");
tr.setInput("azureCommonVirtualFile", "");
tr.setInput("jdkDestinationDirectory", "javaJDK");
tr.setInput("azureCommonVirtualFile", "JDKname.tar.gz");
tr.setInput("jdkDestinationDirectory", "DestinationDirectory");
tr.setInput("cleanDestinationDirectory", "false");

process.env['AGENT_TOOLSDIRECTORY'] = '/tool';
process.env['AGENT_VERSION'] = '2.194.0';

process.env['ENDPOINT_URL_ID1'] = 'http://url';
process.env['ENDPOINT_AUTH_PARAMETER_connection1_username'] = 'dummyusername';
process.env['ENDPOINT_AUTH_PARAMETER_connection1_password'] = 'dummypassword';
Expand All @@ -29,7 +33,18 @@ process.env['ENDPOINT_DATA_ARM1_environmentAuthorityUrl'] = 'dummyurl';
process.env['ENDPOINT_DATA_ARM1_activeDirectoryServiceEndpointResourceId'] = 'dummyResourceId';
process.env['ENDPOINT_DATA_ARM1_subscriptionId'] = 'dummySubscriptionId';

tr.registerMock("azure-pipelines-tasks-azure-arm-rest/azure-arm-storage", {
const a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
"stats": {
"DestinationDirectory\\JDKname.tar.gz": true,
},
"find": {
"DestinationDirectory": ["rootJDK/", "rootJDK/secondlevelJDK2"],
},
};

tr.setAnswers(a);

tr.registerMock("azure-pipelines-tasks-azure-arm-rest-v2/azure-arm-storage", {
StorageManagementClient: function (A, B) {
return {
storageAccounts: {
Expand All @@ -56,10 +71,48 @@ tr.registerMock("azure-pipelines-tasks-azure-arm-rest/azure-arm-storage", {
}
});

tr.registerMock("azure-pipelines-tasks-azure-arm-rest/azure-arm-common", {
tr.registerMock("azure-pipelines-tasks-azure-arm-rest-v2/azure-arm-common", {
ApplicationTokenCredentials: function(A,B,C,D,E,F,G) {
return {};
}
});

tr.registerMock('./AzureStorageArtifacts/AzureStorageArtifactDownloader',{
AzureStorageArtifactDownloader: function(A,B,C) {
return {
downloadArtifacts: function(A,B) {
return "pathFromDownloader";
}
}
}
})

const mtl = require("azure-pipelines-tool-lib/tool")
const mtlClone = Object.assign({}, mtl);

mtlClone.prependPath = function(variable1: string, variable2: string) {
return {};
};

tr.registerMock("azure-pipelines-tool-lib/tool", mtlClone);

const mfs = require('fs')
const mfsClone = Object.assign({}, mfs);

mfsClone.lstatSync = function(variable: string) {
return {
isDirectory: function() {
return true;
}
};
};

mfsClone.existsSync = function (variable: string) {
if (variable === "DestinationDirectory\\econdlevelJDK2") {
return false;
} else return true;
}

tr.registerMock('fs', mfsClone);

tr.run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import ma = require('azure-pipelines-task-lib/mock-answer');
import tmrm = require('azure-pipelines-task-lib/mock-run');
import msRestAzure = require('azure-pipelines-tasks-azure-arm-rest-v2/azure-arm-common');
import path = require('path');
import mockTask = require('azure-pipelines-task-lib/mock-task');


const taskPath = path.join(__dirname, '..', 'javatoolinstaller.js');
const tr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath);

tr.setInput("versionSpec", "8.1");
tr.setInput("jdkSourceOption", "AzureStorage")
tr.setInput("jdkArchitectureOption", "x64");
tr.setInput("azureResourceManagerEndpoint", "ARM1");
tr.setInput("azureStorageAccountName", "storage1");
tr.setInput("azureContainerName", "container1");
tr.setInput("azureCommonVirtualFile", "folder/JDKname.tar.gz");
tr.setInput("jdkDestinationDirectory", "DestinationDirectory");
tr.setInput("cleanDestinationDirectory", "true");

process.env['AGENT_TOOLSDIRECTORY'] = '/tool';
process.env['AGENT_VERSION'] = '2.194.0';

process.env['ENDPOINT_URL_ID1'] = 'http://url';
process.env['ENDPOINT_AUTH_PARAMETER_connection1_username'] = 'dummyusername';
process.env['ENDPOINT_AUTH_PARAMETER_connection1_password'] = 'dummypassword';
process.env['ENDPOINT_DATA_ID1_acceptUntrustedCerts'] = 'true';

process.env['ENDPOINT_URL_ARM1'] = 'http://url';
process.env['ENDPOINT_AUTH_PARAMETER_connection1_serviceprincipalid'] = 'dummyid';
process.env['ENDPOINT_AUTH_PARAMETER_connection1_serviceprincipalkey'] = 'dummykey';
process.env['ENDPOINT_AUTH_PARAMETER_connection1_tenantid'] = 'dummyTenantid';
process.env['ENDPOINT_DATA_ARM1_environmentAuthorityUrl'] = 'dummyurl';
process.env['ENDPOINT_DATA_ARM1_activeDirectoryServiceEndpointResourceId'] = 'dummyResourceId';
process.env['ENDPOINT_DATA_ARM1_subscriptionId'] = 'dummySubscriptionId';

const a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
"stats": {
"DestinationDirectory\\JDKname.tar.gz": true,
},
"find": {
"DestinationDirectory": ["rootJDK/", "rootJDK/secondlevelJDK2"],
},
};

tr.setAnswers(a);

tr.registerMock("azure-pipelines-tasks-azure-arm-rest-v2/azure-arm-storage", {
StorageManagementClient: function (A, B) {
return {
storageAccounts: {
get: function (A) {
return {
properties: {
primaryEndpoints: {
blob: "primaryBlobUrl"
}
},
id: "StorageAccountUrl"
}
},
listkeys: function (A, B, C) {
return ["accesskey1", "accessKey2"];
}
}
}
},
StorageAccounts: {
getResourceGroupNameFromUri: function (A) {
return "storageAccountResouceGroupName";
}
}
});

tr.registerMock("azure-pipelines-tasks-azure-arm-rest-v2/azure-arm-common", {
ApplicationTokenCredentials: function(A,B,C,D,E,F,G) {
return {};
}
});

tr.registerMock('./AzureStorageArtifacts/AzureStorageArtifactDownloader',{
AzureStorageArtifactDownloader: function(A,B,C) {
return {
downloadArtifacts: function(A,B) {
return "pathFromDownloader";
}
}
}
})

const mtl = require("azure-pipelines-tool-lib/tool")
const mtlClone = Object.assign({}, mtl);

mtlClone.prependPath = function(variable1: string, variable2: string) {
return {};
};

tr.registerMock("azure-pipelines-tool-lib/tool", mtlClone);

const mfs = require('fs')
const mfsClone = Object.assign({}, mfs);

mfsClone.lstatSync = function(variable: string) {
return {
isDirectory: function() {
return true;
}
};
};

mfsClone.existsSync = function (variable: string) {
if (variable === "DestinationDirectory\\econdlevelJDK2") {
return false;
} else return true;
}

tr.registerMock('fs', mfsClone);

tr.run();
6 changes: 4 additions & 2 deletions Tasks/JavaToolInstallerV0/javatoolinstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ async function getJava(versionSpec: string, jdkArchitectureOption: string): Prom
taskLib.getInput('azureStorageAccountName', true), taskLib.getInput('azureContainerName', true), "");
await azureDownloader.downloadArtifacts(extractLocation, '*' + fileNameAndPath);
await taskutils.sleepFor(250); //Wait for the file to be released before extracting it.
jdkFileName = path.join(extractLocation, fileNameAndPath);
let jdkArchiveName = path.basename(fileNameAndPath);
jdkFileName = path.join(extractLocation, jdkArchiveName);
toolLib.debug(`jdkArchiveName: ${jdkFileName}`);
} else {
// get from local directory
console.log(taskLib.loc('RetrievingJdkFromLocalPath'));
Expand Down Expand Up @@ -169,7 +171,7 @@ async function unpackArchive(unpackDir: string, jdkFileName: string, fileExt: st
}

/**
* Get the path to a folder inside the VOLUMES_FOLDER.
* Get the path to a folder inside the VOLUMES_FOLDER.
AnnaOpareva marked this conversation as resolved.
Show resolved Hide resolved
* Only for macOS.
* @param volumes VOLUMES_FOLDER contents before attaching a disk image.
* @returns string
Expand Down
2 changes: 1 addition & 1 deletion Tasks/JavaToolInstallerV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 193,
"Minor": 194,
AnnaOpareva marked this conversation as resolved.
Show resolved Hide resolved
"Patch": 0
},
"satisfies": [
Expand Down
2 changes: 1 addition & 1 deletion Tasks/JavaToolInstallerV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 193,
"Minor": 194,
"Patch": 0
},
"satisfies": [
Expand Down