Skip to content

Commit f28252c

Browse files
AnnaOparevaAnna OparevaEzzhevNikita
authored
Fixed and added unit tests for JavaToolInstallerV0 task (#15355)
* fix test for subfolder * add test * fix string template * fix path * fix conduction Co-authored-by: Anna Opareva <[email protected]> Co-authored-by: Nikita Ezzhev <[email protected]>
1 parent 55020aa commit f28252c

File tree

4 files changed

+192
-6
lines changed

4 files changed

+192
-6
lines changed

Tasks/JavaToolInstallerV0/Tests/L0.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,26 @@ describe('JavaToolInstaller L0 Suite', function () {
2121
it('should run successfully when fetching JDK files from azure storage', function () {
2222
this.timeout(20000);
2323

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

2727
testRunner.run();
2828

2929
assert(testRunner.succeeded, 'task should have succeeded.');
3030
});
3131

32+
it('should run successfully when fetching JDK files from azure storage from subfolder', function () {
33+
this.timeout(20000);
34+
35+
const testPath: string = path.join(__dirname, 'L0DownloadsJdkFromAzureStorageSubFolder.js');
36+
const testRunner: ttm.MockTestRunner = new ttm.MockTestRunner(testPath);
37+
38+
testRunner.run();
39+
40+
assert((testRunner.stdOutContained('jdkFileName: DestinationDirectory\\JDKname.tar.gz') || testRunner.stdOutContained('jdkFileName: DestinationDirectory/JDKname.tar.gz')) , 'JDK archive should unpack in the right destination directory');
41+
assert(testRunner.succeeded, 'task should have succeeded.');
42+
});
43+
3244
it('should fail when JavaToolInstaller is run with to destination folder specified', function () {
3345
this.timeout(20000);
3446

Tasks/JavaToolInstallerV0/Tests/L0DownloadsJdkFromAzureStorage.ts

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ma = require('azure-pipelines-task-lib/mock-answer');
22
import tmrm = require('azure-pipelines-task-lib/mock-run');
3+
import msRestAzure = require('azure-pipelines-tasks-azure-arm-rest-v2/azure-arm-common');
34
import path = require('path');
45
import mockTask = require('azure-pipelines-task-lib/mock-task');
56

@@ -12,10 +13,13 @@ tr.setInput("jdkArchitectureOption", "x64");
1213
tr.setInput("azureResourceManagerEndpoint", "ARM1");
1314
tr.setInput("azureStorageAccountName", "storage1");
1415
tr.setInput("azureContainerName", "container1");
15-
tr.setInput("azureCommonVirtualFile", "");
16-
tr.setInput("jdkDestinationDirectory", "javaJDK");
16+
tr.setInput("azureCommonVirtualFile", "JDKname.tar.gz");
17+
tr.setInput("jdkDestinationDirectory", "DestinationDirectory");
1718
tr.setInput("cleanDestinationDirectory", "false");
1819

20+
process.env['AGENT_TOOLSDIRECTORY'] = '/tool';
21+
process.env['AGENT_VERSION'] = '2.194.0';
22+
1923
process.env['ENDPOINT_URL_ID1'] = 'http://url';
2024
process.env['ENDPOINT_AUTH_PARAMETER_connection1_username'] = 'dummyusername';
2125
process.env['ENDPOINT_AUTH_PARAMETER_connection1_password'] = 'dummypassword';
@@ -29,7 +33,19 @@ process.env['ENDPOINT_DATA_ARM1_environmentAuthorityUrl'] = 'dummyurl';
2933
process.env['ENDPOINT_DATA_ARM1_activeDirectoryServiceEndpointResourceId'] = 'dummyResourceId';
3034
process.env['ENDPOINT_DATA_ARM1_subscriptionId'] = 'dummySubscriptionId';
3135

32-
tr.registerMock("azure-pipelines-tasks-azure-arm-rest/azure-arm-storage", {
36+
const a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
37+
"stats": {
38+
"DestinationDirectory\\JDKname.tar.gz": true,
39+
"DestinationDirectory/JDKname.tar.gz": true,
40+
},
41+
"find": {
42+
"DestinationDirectory": ["rootJDK/", "rootJDK/secondlevelJDK2"],
43+
},
44+
};
45+
46+
tr.setAnswers(a);
47+
48+
tr.registerMock("azure-pipelines-tasks-azure-arm-rest-v2/azure-arm-storage", {
3349
StorageManagementClient: function (A, B) {
3450
return {
3551
storageAccounts: {
@@ -56,10 +72,48 @@ tr.registerMock("azure-pipelines-tasks-azure-arm-rest/azure-arm-storage", {
5672
}
5773
});
5874

59-
tr.registerMock("azure-pipelines-tasks-azure-arm-rest/azure-arm-common", {
75+
tr.registerMock("azure-pipelines-tasks-azure-arm-rest-v2/azure-arm-common", {
6076
ApplicationTokenCredentials: function(A,B,C,D,E,F,G) {
6177
return {};
6278
}
6379
});
6480

81+
tr.registerMock('./AzureStorageArtifacts/AzureStorageArtifactDownloader',{
82+
AzureStorageArtifactDownloader: function(A,B,C) {
83+
return {
84+
downloadArtifacts: function(A,B) {
85+
return "pathFromDownloader";
86+
}
87+
}
88+
}
89+
})
90+
91+
const mtl = require("azure-pipelines-tool-lib/tool")
92+
const mtlClone = Object.assign({}, mtl);
93+
94+
mtlClone.prependPath = function(variable1: string, variable2: string) {
95+
return {};
96+
};
97+
98+
tr.registerMock("azure-pipelines-tool-lib/tool", mtlClone);
99+
100+
const mfs = require('fs')
101+
const mfsClone = Object.assign({}, mfs);
102+
103+
mfsClone.lstatSync = function(variable: string) {
104+
return {
105+
isDirectory: function() {
106+
return true;
107+
}
108+
};
109+
};
110+
111+
mfsClone.existsSync = function (variable: string) {
112+
if (variable === "DestinationDirectory\\econdlevelJDK2" || variable === "DestinationDirectory/econdlevelJDK2") {
113+
return false;
114+
} else return true;
115+
}
116+
117+
tr.registerMock('fs', mfsClone);
118+
65119
tr.run();
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import ma = require('azure-pipelines-task-lib/mock-answer');
2+
import tmrm = require('azure-pipelines-task-lib/mock-run');
3+
import msRestAzure = require('azure-pipelines-tasks-azure-arm-rest-v2/azure-arm-common');
4+
import path = require('path');
5+
import mockTask = require('azure-pipelines-task-lib/mock-task');
6+
7+
8+
const taskPath = path.join(__dirname, '..', 'javatoolinstaller.js');
9+
const tr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath);
10+
11+
tr.setInput("versionSpec", "8.1");
12+
tr.setInput("jdkSourceOption", "AzureStorage")
13+
tr.setInput("jdkArchitectureOption", "x64");
14+
tr.setInput("azureResourceManagerEndpoint", "ARM1");
15+
tr.setInput("azureStorageAccountName", "storage1");
16+
tr.setInput("azureContainerName", "container1");
17+
tr.setInput("azureCommonVirtualFile", "folder/JDKname.tar.gz");
18+
tr.setInput("jdkDestinationDirectory", "DestinationDirectory");
19+
tr.setInput("cleanDestinationDirectory", "true");
20+
21+
process.env['AGENT_TOOLSDIRECTORY'] = '/tool';
22+
process.env['AGENT_VERSION'] = '2.194.0';
23+
24+
process.env['ENDPOINT_URL_ID1'] = 'http://url';
25+
process.env['ENDPOINT_AUTH_PARAMETER_connection1_username'] = 'dummyusername';
26+
process.env['ENDPOINT_AUTH_PARAMETER_connection1_password'] = 'dummypassword';
27+
process.env['ENDPOINT_DATA_ID1_acceptUntrustedCerts'] = 'true';
28+
29+
process.env['ENDPOINT_URL_ARM1'] = 'http://url';
30+
process.env['ENDPOINT_AUTH_PARAMETER_connection1_serviceprincipalid'] = 'dummyid';
31+
process.env['ENDPOINT_AUTH_PARAMETER_connection1_serviceprincipalkey'] = 'dummykey';
32+
process.env['ENDPOINT_AUTH_PARAMETER_connection1_tenantid'] = 'dummyTenantid';
33+
process.env['ENDPOINT_DATA_ARM1_environmentAuthorityUrl'] = 'dummyurl';
34+
process.env['ENDPOINT_DATA_ARM1_activeDirectoryServiceEndpointResourceId'] = 'dummyResourceId';
35+
process.env['ENDPOINT_DATA_ARM1_subscriptionId'] = 'dummySubscriptionId';
36+
37+
const a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
38+
"stats": {
39+
"DestinationDirectory\\JDKname.tar.gz": true,
40+
"DestinationDirectory/JDKname.tar.gz": true,
41+
},
42+
"find": {
43+
"DestinationDirectory": ["rootJDK/", "rootJDK/secondlevelJDK2"],
44+
},
45+
};
46+
47+
tr.setAnswers(a);
48+
49+
tr.registerMock("azure-pipelines-tasks-azure-arm-rest-v2/azure-arm-storage", {
50+
StorageManagementClient: function (A, B) {
51+
return {
52+
storageAccounts: {
53+
get: function (A) {
54+
return {
55+
properties: {
56+
primaryEndpoints: {
57+
blob: "primaryBlobUrl"
58+
}
59+
},
60+
id: "StorageAccountUrl"
61+
}
62+
},
63+
listkeys: function (A, B, C) {
64+
return ["accesskey1", "accessKey2"];
65+
}
66+
}
67+
}
68+
},
69+
StorageAccounts: {
70+
getResourceGroupNameFromUri: function (A) {
71+
return "storageAccountResouceGroupName";
72+
}
73+
}
74+
});
75+
76+
tr.registerMock("azure-pipelines-tasks-azure-arm-rest-v2/azure-arm-common", {
77+
ApplicationTokenCredentials: function(A,B,C,D,E,F,G) {
78+
return {};
79+
}
80+
});
81+
82+
tr.registerMock('./AzureStorageArtifacts/AzureStorageArtifactDownloader',{
83+
AzureStorageArtifactDownloader: function(A,B,C) {
84+
return {
85+
downloadArtifacts: function(A,B) {
86+
return "pathFromDownloader";
87+
}
88+
}
89+
}
90+
})
91+
92+
const mtl = require("azure-pipelines-tool-lib/tool")
93+
const mtlClone = Object.assign({}, mtl);
94+
95+
mtlClone.prependPath = function(variable1: string, variable2: string) {
96+
return {};
97+
};
98+
99+
tr.registerMock("azure-pipelines-tool-lib/tool", mtlClone);
100+
101+
const mfs = require('fs')
102+
const mfsClone = Object.assign({}, mfs);
103+
104+
mfsClone.lstatSync = function(variable: string) {
105+
return {
106+
isDirectory: function() {
107+
return true;
108+
}
109+
};
110+
};
111+
112+
mfsClone.existsSync = function (variable: string) {
113+
if (variable === "DestinationDirectory\\econdlevelJDK2" || variable === "DestinationDirectory/econdlevelJDK2" ) {
114+
return false;
115+
} else return true;
116+
}
117+
118+
tr.registerMock('fs', mfsClone);
119+
120+
tr.run();

Tasks/JavaToolInstallerV0/javatoolinstaller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async function getJava(versionSpec: string, jdkArchitectureOption: string): Prom
6464
await taskutils.sleepFor(250); //Wait for the file to be released before extracting it.
6565
let jdkArchiveName = path.basename(fileNameAndPath);
6666
jdkFileName = path.join(extractLocation, jdkArchiveName);
67-
toolLib.debug('jdkFileName: ${jdkFileName}');
67+
toolLib.debug(`jdkFileName: ${jdkFileName}`);
6868
} else {
6969
// get from local directory
7070
console.log(taskLib.loc('RetrievingJdkFromLocalPath'));

0 commit comments

Comments
 (0)