Merge branch 'main' into dependabot/npm_and_yarn/y18n-4.0.1

This commit is contained in:
tauhid621 2021-06-14 15:27:27 +05:30
commit aa5e415a91
4 changed files with 95 additions and 41 deletions

View file

@ -4,50 +4,74 @@ import * as toolCache from '@actions/tool-cache';
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as util from 'util';
describe('Testing all functions in run file.', () => { describe('Testing all functions in run file.', () => {
test('getExecutableExtension() - return .exe when os is Windows', () => { test('getExecutableExtension() - return .exe when os is Windows', () => {
jest.spyOn(os, 'type').mockReturnValue('Windows_NT'); jest.spyOn(os, 'type').mockReturnValue('Windows_NT');
expect(run.getExecutableExtension()).toBe('.exe'); expect(run.getExecutableExtension()).toBe('.exe');
expect(os.type).toBeCalled(); expect(os.type).toBeCalled();
}); });
test('getExecutableExtension() - return empty string for non-windows OS', () => { test('getExecutableExtension() - return empty string for non-windows OS', () => {
jest.spyOn(os, 'type').mockReturnValue('Darwin'); jest.spyOn(os, 'type').mockReturnValue('Darwin');
expect(run.getExecutableExtension()).toBe(''); expect(run.getExecutableExtension()).toBe('');
expect(os.type).toBeCalled(); expect(os.type).toBeCalled();
}); });
test('getkubectlDownloadURL() - return the URL to download kubectl for Linux', () => { test.each([
['arm', 'arm'],
['arm64', 'arm64'],
['x64', 'amd64']
])("getKubectlArch() - return on %s os arch %s kubectl arch", (osArch, kubectlArch) => {
jest.spyOn(os, 'arch').mockReturnValue(osArch);
expect(run.getKubectlArch()).toBe(kubectlArch);
expect(os.arch).toBeCalled();
});
test.each([
['arm'],
['arm64'],
['amd64']
])('getkubectlDownloadURL() - return the URL to download %s kubectl for Linux', (arch) => {
jest.spyOn(os, 'type').mockReturnValue('Linux'); jest.spyOn(os, 'type').mockReturnValue('Linux');
const kubectlLinuxUrl = 'https://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/linux/amd64/kubectl' const kubectlLinuxUrl = util.format('https://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/linux/%s/kubectl', arch);
expect(run.getkubectlDownloadURL('v1.15.0')).toBe(kubectlLinuxUrl); expect(run.getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlLinuxUrl);
expect(os.type).toBeCalled(); expect(os.type).toBeCalled();
}); });
test('getkubectlDownloadURL() - return the URL to download kubectl for Darwin', () => { test.each([
['arm'],
['arm64'],
['amd64']
])('getkubectlDownloadURL() - return the URL to download %s kubectl for Darwin', (arch) => {
jest.spyOn(os, 'type').mockReturnValue('Darwin'); jest.spyOn(os, 'type').mockReturnValue('Darwin');
const kubectlDarwinUrl = 'https://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/darwin/amd64/kubectl' const kubectlDarwinUrl = util.format('https://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/darwin/%s/kubectl', arch);
expect(run.getkubectlDownloadURL('v1.15.0')).toBe(kubectlDarwinUrl); expect(run.getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlDarwinUrl);
expect(os.type).toBeCalled(); expect(os.type).toBeCalled();
}); });
test('getkubectlDownloadURL() - return the URL to download kubectl for Windows', () => { test.each([
['arm'],
['arm64'],
['amd64']
])('getkubectlDownloadURL() - return the URL to download %s kubectl for Windows', (arch) => {
jest.spyOn(os, 'type').mockReturnValue('Windows_NT'); jest.spyOn(os, 'type').mockReturnValue('Windows_NT');
const kubectlWindowsUrl = 'https://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/windows/amd64/kubectl.exe' const kubectlWindowsUrl = util.format('https://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/windows/%s/kubectl.exe', arch);
expect(run.getkubectlDownloadURL('v1.15.0')).toBe(kubectlWindowsUrl); expect(run.getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlWindowsUrl);
expect(os.type).toBeCalled(); expect(os.type).toBeCalled();
}); });
test('getStableKubectlVersion() - download stable version file, read version and return it', async () => { test('getStableKubectlVersion() - download stable version file, read version and return it', async () => {
jest.spyOn(toolCache, 'downloadTool').mockReturnValue(Promise.resolve('pathToTool')); jest.spyOn(toolCache, 'downloadTool').mockReturnValue(Promise.resolve('pathToTool'));
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.20.4'); jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.20.4');
expect(await run.getStableKubectlVersion()).toBe('v1.20.4'); expect(await run.getStableKubectlVersion()).toBe('v1.20.4');
expect(toolCache.downloadTool).toBeCalled(); expect(toolCache.downloadTool).toBeCalled();
expect(fs.readFileSync).toBeCalledWith('pathToTool', 'utf8'); expect(fs.readFileSync).toBeCalledWith('pathToTool', 'utf8');
@ -56,7 +80,7 @@ describe('Testing all functions in run file.', () => {
test('getStableKubectlVersion() - return default v1.15.0 if version read is empty', async () => { test('getStableKubectlVersion() - return default v1.15.0 if version read is empty', async () => {
jest.spyOn(toolCache, 'downloadTool').mockReturnValue(Promise.resolve('pathToTool')); jest.spyOn(toolCache, 'downloadTool').mockReturnValue(Promise.resolve('pathToTool'));
jest.spyOn(fs, 'readFileSync').mockReturnValue(''); jest.spyOn(fs, 'readFileSync').mockReturnValue('');
expect(await run.getStableKubectlVersion()).toBe('v1.15.0'); expect(await run.getStableKubectlVersion()).toBe('v1.15.0');
expect(toolCache.downloadTool).toBeCalled(); expect(toolCache.downloadTool).toBeCalled();
expect(fs.readFileSync).toBeCalledWith('pathToTool', 'utf8'); expect(fs.readFileSync).toBeCalledWith('pathToTool', 'utf8');
@ -64,7 +88,7 @@ describe('Testing all functions in run file.', () => {
test('getStableKubectlVersion() - return default v1.15.0 if unable to download file', async () => { test('getStableKubectlVersion() - return default v1.15.0 if unable to download file', async () => {
jest.spyOn(toolCache, 'downloadTool').mockRejectedValue('Unable to download.'); jest.spyOn(toolCache, 'downloadTool').mockRejectedValue('Unable to download.');
expect(await run.getStableKubectlVersion()).toBe('v1.15.0'); expect(await run.getStableKubectlVersion()).toBe('v1.15.0');
expect(toolCache.downloadTool).toBeCalled(); expect(toolCache.downloadTool).toBeCalled();
}); });
@ -75,7 +99,7 @@ describe('Testing all functions in run file.', () => {
jest.spyOn(toolCache, 'cacheFile').mockReturnValue(Promise.resolve('pathToCachedTool')); jest.spyOn(toolCache, 'cacheFile').mockReturnValue(Promise.resolve('pathToCachedTool'));
jest.spyOn(os, 'type').mockReturnValue('Windows_NT'); jest.spyOn(os, 'type').mockReturnValue('Windows_NT');
jest.spyOn(fs, 'chmodSync').mockImplementation(() => {}); jest.spyOn(fs, 'chmodSync').mockImplementation(() => {});
expect(await run.downloadKubectl('v1.15.0')).toBe(path.join('pathToCachedTool', 'kubectl.exe')); expect(await run.downloadKubectl('v1.15.0')).toBe(path.join('pathToCachedTool', 'kubectl.exe'));
expect(toolCache.find).toBeCalledWith('kubectl', 'v1.15.0'); expect(toolCache.find).toBeCalledWith('kubectl', 'v1.15.0');
expect(toolCache.downloadTool).toBeCalled(); expect(toolCache.downloadTool).toBeCalled();
@ -93,12 +117,29 @@ describe('Testing all functions in run file.', () => {
expect(toolCache.downloadTool).toBeCalled(); expect(toolCache.downloadTool).toBeCalled();
}); });
test('downloadKubectl() - throw kubectl not found error when receive 404 response', async () => {
const kubectlVersion = 'v1.15.0'
const arch = 'arm128';
jest.spyOn(os, 'arch').mockReturnValue(arch);
jest.spyOn(toolCache, 'find').mockReturnValue('');
jest.spyOn(toolCache, 'downloadTool').mockImplementation(_ => {
throw new toolCache.HTTPError(404);
});
await expect(run.downloadKubectl(kubectlVersion)).rejects
.toThrow(util.format("Kubectl '%s' for '%s' arch not found.", kubectlVersion, arch));
expect(os.arch).toBeCalled();
expect(toolCache.find).toBeCalledWith('kubectl', kubectlVersion);
expect(toolCache.downloadTool).toBeCalled();
});
test('downloadKubectl() - return path to existing cache of kubectl', async () => { test('downloadKubectl() - return path to existing cache of kubectl', async () => {
jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool'); jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool');
jest.spyOn(os, 'type').mockReturnValue('Windows_NT'); jest.spyOn(os, 'type').mockReturnValue('Windows_NT');
jest.spyOn(fs, 'chmodSync').mockImplementation(() => {}); jest.spyOn(fs, 'chmodSync').mockImplementation(() => {});
jest.spyOn(toolCache, 'downloadTool'); jest.spyOn(toolCache, 'downloadTool');
expect(await run.downloadKubectl('v1.15.0')).toBe(path.join('pathToCachedTool', 'kubectl.exe')); expect(await run.downloadKubectl('v1.15.0')).toBe(path.join('pathToCachedTool', 'kubectl.exe'));
expect(toolCache.find).toBeCalledWith('kubectl', 'v1.15.0'); expect(toolCache.find).toBeCalledWith('kubectl', 'v1.15.0');
expect(os.type).toBeCalled(); expect(os.type).toBeCalled();

18
package-lock.json generated
View file

@ -2355,9 +2355,9 @@
} }
}, },
"hosted-git-info": { "hosted-git-info": {
"version": "2.8.8", "version": "2.8.9",
"resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.8.tgz", "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.9.tgz",
"integrity": "sha512-f/wzC2QaWBs7t9IYqB4T3sR1xviIViXJRJTWBlx2Gf3g0Xi5vI7Yy4koXQ1c9OYDGHN9sBy1DQ2AB8fqZBWhUg==", "integrity": "sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==",
"dev": true "dev": true
}, },
"html-encoding-sniffer": { "html-encoding-sniffer": {
@ -4169,9 +4169,9 @@
} }
}, },
"lodash": { "lodash": {
"version": "4.17.20", "version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
"integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==", "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
"dev": true "dev": true
}, },
"lodash.memoize": { "lodash.memoize": {
@ -5913,9 +5913,9 @@
} }
}, },
"ws": { "ws": {
"version": "7.3.1", "version": "7.4.6",
"resolved": "https://registry.npmjs.org/ws/-/ws-7.3.1.tgz", "resolved": "https://registry.npmjs.org/ws/-/ws-7.4.6.tgz",
"integrity": "sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA==", "integrity": "sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A==",
"dev": true "dev": true
}, },
"xml-name-validator": { "xml-name-validator": {

View file

@ -4,7 +4,7 @@
"private": true, "private": true,
"main": "lib/run.js", "main": "lib/run.js",
"scripts": { "scripts": {
"build": "tsc --outDir .\\lib\\ --rootDir .\\src\\", "build": "tsc --outDir ./lib --rootDir ./src",
"test": "jest", "test": "jest",
"test-coverage": "jest --coverage" "test-coverage": "jest --coverage"
}, },

View file

@ -17,17 +17,25 @@ export function getExecutableExtension(): string {
return ''; return '';
} }
export function getkubectlDownloadURL(version: string): string { export function getKubectlArch(): string {
let arch = os.arch();
if (arch === 'x64') {
return 'amd64';
}
return arch;
}
export function getkubectlDownloadURL(version: string, arch: string): string {
switch (os.type()) { switch (os.type()) {
case 'Linux': case 'Linux':
return util.format('https://storage.googleapis.com/kubernetes-release/release/%s/bin/linux/amd64/kubectl', version); return util.format('https://storage.googleapis.com/kubernetes-release/release/%s/bin/linux/%s/kubectl', version, arch);
case 'Darwin': case 'Darwin':
return util.format('https://storage.googleapis.com/kubernetes-release/release/%s/bin/darwin/amd64/kubectl', version); return util.format('https://storage.googleapis.com/kubernetes-release/release/%s/bin/darwin/%s/kubectl', version, arch);
case 'Windows_NT': case 'Windows_NT':
default: default:
return util.format('https://storage.googleapis.com/kubernetes-release/release/%s/bin/windows/amd64/kubectl.exe', version); return util.format('https://storage.googleapis.com/kubernetes-release/release/%s/bin/windows/%s/kubectl.exe', version, arch);
} }
} }
@ -49,11 +57,16 @@ export async function getStableKubectlVersion(): Promise<string> {
export async function downloadKubectl(version: string): Promise<string> { export async function downloadKubectl(version: string): Promise<string> {
let cachedToolpath = toolCache.find(kubectlToolName, version); let cachedToolpath = toolCache.find(kubectlToolName, version);
let kubectlDownloadPath = ''; let kubectlDownloadPath = '';
let arch = getKubectlArch();
if (!cachedToolpath) { if (!cachedToolpath) {
try { try {
kubectlDownloadPath = await toolCache.downloadTool(getkubectlDownloadURL(version)); kubectlDownloadPath = await toolCache.downloadTool(getkubectlDownloadURL(version, arch));
} catch (exception) { } catch (exception) {
throw new Error('DownloadKubectlFailed'); if (exception instanceof toolCache.HTTPError && exception.httpStatusCode === 404) {
throw new Error(util.format("Kubectl '%s' for '%s' arch not found.", version, arch));
} else {
throw new Error('DownloadKubectlFailed');
}
} }
cachedToolpath = await toolCache.cacheFile(kubectlDownloadPath, kubectlToolName + getExecutableExtension(), kubectlToolName, version); cachedToolpath = await toolCache.cacheFile(kubectlDownloadPath, kubectlToolName + getExecutableExtension(), kubectlToolName, version);