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

Added arch detection #21

Merged
merged 1 commit into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
91 changes: 66 additions & 25 deletions __tests__/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,74 @@ import * as toolCache from '@actions/tool-cache';
import * as fs from 'fs';
import * as path from 'path';
import * as core from '@actions/core';
import * as util from 'util';

describe('Testing all functions in run file.', () => {
test('getExecutableExtension() - return .exe when os is Windows', () => {
jest.spyOn(os, 'type').mockReturnValue('Windows_NT');

expect(run.getExecutableExtension()).toBe('.exe');
expect(os.type).toBeCalled();
expect(os.type).toBeCalled();
});

test('getExecutableExtension() - return empty string for non-windows OS', () => {
jest.spyOn(os, 'type').mockReturnValue('Darwin');

expect(run.getExecutableExtension()).toBe('');
expect(os.type).toBeCalled();

expect(run.getExecutableExtension()).toBe('');
expect(os.type).toBeCalled();
});

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('getkubectlDownloadURL() - return the URL to download kubectl for Linux', () => {
test.each([
['arm'],
['arm64'],
['amd64']
])('getkubectlDownloadURL() - return the URL to download %s kubectl for Linux', (arch) => {
jest.spyOn(os, 'type').mockReturnValue('Linux');
const kubectlLinuxUrl = 'https://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/linux/amd64/kubectl'
expect(run.getkubectlDownloadURL('v1.15.0')).toBe(kubectlLinuxUrl);
expect(os.type).toBeCalled();
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', arch)).toBe(kubectlLinuxUrl);
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');
const kubectlDarwinUrl = 'https://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/darwin/amd64/kubectl'
expect(run.getkubectlDownloadURL('v1.15.0')).toBe(kubectlDarwinUrl);
expect(os.type).toBeCalled();
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', arch)).toBe(kubectlDarwinUrl);
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');
const kubectlWindowsUrl = 'https://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/windows/amd64/kubectl.exe'
expect(run.getkubectlDownloadURL('v1.15.0')).toBe(kubectlWindowsUrl);
expect(os.type).toBeCalled();

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', arch)).toBe(kubectlWindowsUrl);
expect(os.type).toBeCalled();
});

test('getStableKubectlVersion() - download stable version file, read version and return it', async () => {
jest.spyOn(toolCache, 'downloadTool').mockReturnValue(Promise.resolve('pathToTool'));
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.20.4');

expect(await run.getStableKubectlVersion()).toBe('v1.20.4');
expect(toolCache.downloadTool).toBeCalled();
expect(fs.readFileSync).toBeCalledWith('pathToTool', 'utf8');
Expand All @@ -56,15 +80,15 @@ describe('Testing all functions in run file.', () => {
test('getStableKubectlVersion() - return default v1.15.0 if version read is empty', async () => {
jest.spyOn(toolCache, 'downloadTool').mockReturnValue(Promise.resolve('pathToTool'));
jest.spyOn(fs, 'readFileSync').mockReturnValue('');

expect(await run.getStableKubectlVersion()).toBe('v1.15.0');
expect(toolCache.downloadTool).toBeCalled();
expect(fs.readFileSync).toBeCalledWith('pathToTool', 'utf8');
});

test('getStableKubectlVersion() - return default v1.15.0 if unable to download file', async () => {
jest.spyOn(toolCache, 'downloadTool').mockRejectedValue('Unable to download.');

expect(await run.getStableKubectlVersion()).toBe('v1.15.0');
expect(toolCache.downloadTool).toBeCalled();
});
Expand All @@ -75,7 +99,7 @@ describe('Testing all functions in run file.', () => {
jest.spyOn(toolCache, 'cacheFile').mockReturnValue(Promise.resolve('pathToCachedTool'));
jest.spyOn(os, 'type').mockReturnValue('Windows_NT');
jest.spyOn(fs, 'chmodSync').mockImplementation(() => {});

expect(await run.downloadKubectl('v1.15.0')).toBe(path.join('pathToCachedTool', 'kubectl.exe'));
expect(toolCache.find).toBeCalledWith('kubectl', 'v1.15.0');
expect(toolCache.downloadTool).toBeCalled();
Expand All @@ -93,12 +117,29 @@ describe('Testing all functions in run file.', () => {
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 () => {
jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool');
jest.spyOn(os, 'type').mockReturnValue('Windows_NT');
jest.spyOn(fs, 'chmodSync').mockImplementation(() => {});
jest.spyOn(toolCache, 'downloadTool');

expect(await run.downloadKubectl('v1.15.0')).toBe(path.join('pathToCachedTool', 'kubectl.exe'));
expect(toolCache.find).toBeCalledWith('kubectl', 'v1.15.0');
expect(os.type).toBeCalled();
Expand Down
25 changes: 19 additions & 6 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,25 @@ export function getExecutableExtension(): string {
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()) {
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':
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':
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);

}
}
Expand All @@ -49,11 +57,16 @@ export async function getStableKubectlVersion(): Promise<string> {
export async function downloadKubectl(version: string): Promise<string> {
let cachedToolpath = toolCache.find(kubectlToolName, version);
let kubectlDownloadPath = '';
let arch = getKubectlArch();
if (!cachedToolpath) {
try {
kubectlDownloadPath = await toolCache.downloadTool(getkubectlDownloadURL(version));
kubectlDownloadPath = await toolCache.downloadTool(getkubectlDownloadURL(version, arch));
} 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);
Expand Down