Skip to content

Commit

Permalink
Fixed and added unit tests for JavaToolInstallerV0 task (#15355)
Browse files Browse the repository at this point in the history
* fix test for subfolder

* add test

* fix string template

* fix path

* fix conduction

Co-authored-by: Anna Opareva <v-aopareva@microsoft.com>
Co-authored-by: Nikita Ezzhev <v-niezz@microsoft.com>
  • Loading branch information
3 people authored Oct 7, 2021
1 parent 55020aa commit f28252c
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 6 deletions.
14 changes: 13 additions & 1 deletion 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();

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('jdkFileName: DestinationDirectory\\JDKname.tar.gz') || testRunner.stdOutContained('jdkFileName: 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
62 changes: 58 additions & 4 deletions Tasks/JavaToolInstallerV0/Tests/L0DownloadsJdkFromAzureStorage.ts
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,19 @@ 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,
"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 +72,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" || 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,120 @@
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,
"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" || variable === "DestinationDirectory/econdlevelJDK2" ) {
return false;
} else return true;
}

tr.registerMock('fs', mfsClone);

tr.run();
2 changes: 1 addition & 1 deletion Tasks/JavaToolInstallerV0/javatoolinstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async function getJava(versionSpec: string, jdkArchitectureOption: string): Prom
await taskutils.sleepFor(250); //Wait for the file to be released before extracting it.
let jdkArchiveName = path.basename(fileNameAndPath);
jdkFileName = path.join(extractLocation, jdkArchiveName);
toolLib.debug('jdkFileName: ${jdkFileName}');
toolLib.debug(`jdkFileName: ${jdkFileName}`);
} else {
// get from local directory
console.log(taskLib.loc('RetrievingJdkFromLocalPath'));
Expand Down

0 comments on commit f28252c

Please sign in to comment.