901a10e89e
* Bump ansi-regex from 5.0.0 to 5.0.1 (#56) Bumps [ansi-regex](https://github.com/chalk/ansi-regex) from 5.0.0 to 5.0.1. - [Release notes](https://github.com/chalk/ansi-regex/releases) - [Commits](https://github.com/chalk/ansi-regex/compare/v5.0.0...v5.0.1) --- updated-dependencies: - dependency-name: ansi-regex dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Vidya reddy prettier (#58) * upgraded to Node16 * Enforce Prettier * code fix * jest version change and prettify code Co-authored-by: Vidya Reddy <vidyareddy@microsoft.com> * Upgraded the ncc version (#61) Co-authored-by: Vidya Reddy <vidyareddy@microsoft.com> * Update README example to v3 (#60) * Bump @actions/core from 1.9.0 to 1.9.1 (#63) Bumps [@actions/core](https://github.com/actions/toolkit/tree/HEAD/packages/core) from 1.9.0 to 1.9.1. - [Release notes](https://github.com/actions/toolkit/releases) - [Changelog](https://github.com/actions/toolkit/blob/main/packages/core/RELEASES.md) - [Commits](https://github.com/actions/toolkit/commits/HEAD/packages/core) --- updated-dependencies: - dependency-name: "@actions/core" dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Add the issue report and feature request form (#64) * syntax error fixes (#66) * added support message (#67) * Bump @actions/core (#68) to address https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/ * Bump decode-uri-component from 0.2.0 to 0.2.2 (#73) Bumps [decode-uri-component](https://github.com/SamVerschueren/decode-uri-component) from 0.2.0 to 0.2.2. - [Release notes](https://github.com/SamVerschueren/decode-uri-component/releases) - [Commits](https://github.com/SamVerschueren/decode-uri-component/compare/v0.2.0...v0.2.2) --- updated-dependencies: - dependency-name: decode-uri-component dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * change uri (#77) * Add node modules and compiled JavaScript from main Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Vidya Reddy <59590642+Vidya2606@users.noreply.github.com> Co-authored-by: Vidya Reddy <vidyareddy@microsoft.com> Co-authored-by: Oliver King <olivermerkleyking@gmail.com> Co-authored-by: Asa Gayle <azmatch.gayle@gmail.com> Co-authored-by: Sumner Warren <sumner.warren@gmail.com> Co-authored-by: Oliver King <oking3@uncc.edu>
235 lines
8.6 KiB
TypeScript
235 lines
8.6 KiB
TypeScript
import * as run from './run'
|
|
import {
|
|
getkubectlDownloadURL,
|
|
getKubectlArch,
|
|
getExecutableExtension
|
|
} from './helpers'
|
|
import * as os from 'os'
|
|
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(getExecutableExtension()).toBe('.exe')
|
|
expect(os.type).toBeCalled()
|
|
})
|
|
|
|
test('getExecutableExtension() - return empty string for non-windows OS', () => {
|
|
jest.spyOn(os, 'type').mockReturnValue('Darwin')
|
|
|
|
expect(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(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')
|
|
const kubectlLinuxUrl = util.format(
|
|
'https://dl.k8s.io/release/v1.15.0/bin/linux/%s/kubectl',
|
|
arch
|
|
)
|
|
|
|
expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlLinuxUrl)
|
|
expect(os.type).toBeCalled()
|
|
}
|
|
)
|
|
|
|
test.each([['arm'], ['arm64'], ['amd64']])(
|
|
'getkubectlDownloadURL() - return the URL to download %s kubectl for Darwin',
|
|
(arch) => {
|
|
jest.spyOn(os, 'type').mockReturnValue('Darwin')
|
|
const kubectlDarwinUrl = util.format(
|
|
'https://dl.k8s.io/release/v1.15.0/bin/darwin/%s/kubectl',
|
|
arch
|
|
)
|
|
|
|
expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlDarwinUrl)
|
|
expect(os.type).toBeCalled()
|
|
}
|
|
)
|
|
|
|
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 = util.format(
|
|
'https://dl.k8s.io/release/v1.15.0/bin/windows/%s/kubectl.exe',
|
|
arch
|
|
)
|
|
expect(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')
|
|
})
|
|
|
|
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()
|
|
})
|
|
|
|
test('downloadKubectl() - download kubectl, add it to toolCache and return path to it', async () => {
|
|
jest.spyOn(toolCache, 'find').mockReturnValue('')
|
|
jest
|
|
.spyOn(toolCache, 'downloadTool')
|
|
.mockReturnValue(Promise.resolve('pathToTool'))
|
|
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()
|
|
expect(toolCache.cacheFile).toBeCalled()
|
|
expect(os.type).toBeCalled()
|
|
expect(fs.chmodSync).toBeCalledWith(
|
|
path.join('pathToCachedTool', 'kubectl.exe'),
|
|
'775'
|
|
)
|
|
})
|
|
|
|
test('downloadKubectl() - throw DownloadKubectlFailed error when unable to download kubectl', async () => {
|
|
jest.spyOn(toolCache, 'find').mockReturnValue('')
|
|
jest
|
|
.spyOn(toolCache, 'downloadTool')
|
|
.mockRejectedValue('Unable to download kubectl.')
|
|
|
|
await expect(run.downloadKubectl('v1.15.0')).rejects.toThrow(
|
|
'DownloadKubectlFailed'
|
|
)
|
|
expect(toolCache.find).toBeCalledWith('kubectl', 'v1.15.0')
|
|
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()
|
|
expect(fs.chmodSync).toBeCalledWith(
|
|
path.join('pathToCachedTool', 'kubectl.exe'),
|
|
'775'
|
|
)
|
|
expect(toolCache.downloadTool).not.toBeCalled()
|
|
})
|
|
|
|
test('run() - download specified version and set output', async () => {
|
|
jest.spyOn(core, 'getInput').mockReturnValue('v1.15.5')
|
|
jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool')
|
|
jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
|
|
jest.spyOn(fs, 'chmodSync').mockImplementation()
|
|
jest.spyOn(core, 'addPath').mockImplementation()
|
|
jest.spyOn(console, 'log').mockImplementation()
|
|
jest.spyOn(core, 'setOutput').mockImplementation()
|
|
|
|
expect(await run.run()).toBeUndefined()
|
|
expect(core.getInput).toBeCalledWith('version', {required: true})
|
|
expect(core.addPath).toBeCalledWith('pathToCachedTool')
|
|
expect(core.setOutput).toBeCalledWith(
|
|
'kubectl-path',
|
|
path.join('pathToCachedTool', 'kubectl.exe')
|
|
)
|
|
})
|
|
|
|
test('run() - get latest version, download it and set output', async () => {
|
|
jest.spyOn(core, 'getInput').mockReturnValue('latest')
|
|
jest
|
|
.spyOn(toolCache, 'downloadTool')
|
|
.mockReturnValue(Promise.resolve('pathToTool'))
|
|
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.20.4')
|
|
jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool')
|
|
jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
|
|
jest.spyOn(fs, 'chmodSync').mockImplementation()
|
|
jest.spyOn(core, 'addPath').mockImplementation()
|
|
jest.spyOn(console, 'log').mockImplementation()
|
|
jest.spyOn(core, 'setOutput').mockImplementation()
|
|
|
|
expect(await run.run()).toBeUndefined()
|
|
expect(toolCache.downloadTool).toBeCalledWith(
|
|
'https://storage.googleapis.com/kubernetes-release/release/stable.txt'
|
|
)
|
|
expect(core.getInput).toBeCalledWith('version', {required: true})
|
|
expect(core.addPath).toBeCalledWith('pathToCachedTool')
|
|
expect(core.setOutput).toBeCalledWith(
|
|
'kubectl-path',
|
|
path.join('pathToCachedTool', 'kubectl.exe')
|
|
)
|
|
})
|
|
})
|