This commit is contained in:
Jaiveer Katariya 2025-01-30 16:18:41 -05:00
parent 0d0e4c7f33
commit b8db437677
No known key found for this signature in database
GPG key ID: AB670420E156D4FB

View file

@ -12,172 +12,147 @@ import * as core from '@actions/core'
import * as util from 'util' 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', () => { jest.spyOn(core, 'getInput').mockReturnValue('v1.15.5')
jest.spyOn(os, 'type').mockReturnValue('Windows_NT') // test('getExecutableExtension() - return .exe when os is Windows', () => {
// jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
expect(getExecutableExtension()).toBe('.exe') // expect(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(getExecutableExtension()).toBe('')
// expect(os.type).toBeCalled()
expect(getExecutableExtension()).toBe('') // })
expect(os.type).toBeCalled() // test.each([
}) // ['arm', 'arm'],
// ['arm64', 'arm64'],
test.each([ // ['x64', 'amd64']
['arm', 'arm'], // ])(
['arm64', 'arm64'], // 'getKubectlArch() - return on %s os arch %s kubectl arch',
['x64', 'amd64'] // (osArch, kubectlArch) => {
])( // jest.spyOn(os, 'arch').mockReturnValue(osArch)
'getKubectlArch() - return on %s os arch %s kubectl arch', // expect(getKubectlArch()).toBe(kubectlArch)
(osArch, kubectlArch) => { // expect(os.arch).toBeCalled()
jest.spyOn(os, 'arch').mockReturnValue(osArch) // }
// )
expect(getKubectlArch()).toBe(kubectlArch) // test.each([['arm'], ['arm64'], ['amd64']])(
expect(os.arch).toBeCalled() // 'getkubectlDownloadURL() - return the URL to download %s kubectl for Linux',
} // (arch) => {
) // jest.spyOn(os, 'type').mockReturnValue('Linux')
// const kubectlLinuxUrl = util.format(
test.each([['arm'], ['arm64'], ['amd64']])( // 'https://dl.k8s.io/release/v1.15.0/bin/linux/%s/kubectl',
'getkubectlDownloadURL() - return the URL to download %s kubectl for Linux', // arch
(arch) => { // )
jest.spyOn(os, 'type').mockReturnValue('Linux') // expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlLinuxUrl)
const kubectlLinuxUrl = util.format( // expect(os.type).toBeCalled()
'https://dl.k8s.io/release/v1.15.0/bin/linux/%s/kubectl', // }
arch // )
) // test.each([['arm'], ['arm64'], ['amd64']])(
// 'getkubectlDownloadURL() - return the URL to download %s kubectl for Darwin',
expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlLinuxUrl) // (arch) => {
expect(os.type).toBeCalled() // jest.spyOn(os, 'type').mockReturnValue('Darwin')
} // const kubectlDarwinUrl = util.format(
) // 'https://dl.k8s.io/release/v1.15.0/bin/darwin/%s/kubectl',
// arch
test.each([['arm'], ['arm64'], ['amd64']])( // )
'getkubectlDownloadURL() - return the URL to download %s kubectl for Darwin', // expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlDarwinUrl)
(arch) => { // expect(os.type).toBeCalled()
jest.spyOn(os, 'type').mockReturnValue('Darwin') // }
const kubectlDarwinUrl = util.format( // )
'https://dl.k8s.io/release/v1.15.0/bin/darwin/%s/kubectl', // test.each([['arm'], ['arm64'], ['amd64']])(
arch // 'getkubectlDownloadURL() - return the URL to download %s kubectl for Windows',
) // (arch) => {
// jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlDarwinUrl) // const kubectlWindowsUrl = util.format(
expect(os.type).toBeCalled() // 'https://dl.k8s.io/release/v1.15.0/bin/windows/%s/kubectl.exe',
} // arch
) // )
// expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlWindowsUrl)
test.each([['arm'], ['arm64'], ['amd64']])( // expect(os.type).toBeCalled()
'getkubectlDownloadURL() - return the URL to download %s kubectl for Windows', // }
(arch) => { // )
jest.spyOn(os, 'type').mockReturnValue('Windows_NT') // test('getStableKubectlVersion() - download stable version file, read version and return it', async () => {
// jest
const kubectlWindowsUrl = util.format( // .spyOn(toolCache, 'downloadTool')
'https://dl.k8s.io/release/v1.15.0/bin/windows/%s/kubectl.exe', // .mockReturnValue(Promise.resolve('pathToTool'))
arch // jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.20.4')
) // expect(await run.getStableKubectlVersion()).toBe('v1.20.4')
expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlWindowsUrl) // expect(toolCache.downloadTool).toBeCalled()
expect(os.type).toBeCalled() // expect(fs.readFileSync).toHaveBeenCalledWith('pathToTool', 'utf8')
} // })
) // test('getStableKubectlVersion() - return default v1.15.0 if version read is empty', async () => {
// jest
test('getStableKubectlVersion() - download stable version file, read version and return it', async () => { // .spyOn(toolCache, 'downloadTool')
jest // .mockReturnValue(Promise.resolve('pathToTool'))
.spyOn(toolCache, 'downloadTool') // jest.spyOn(fs, 'readFileSync').mockReturnValue('')
.mockReturnValue(Promise.resolve('pathToTool')) // expect(await run.getStableKubectlVersion()).toBe('v1.15.0')
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.20.4') // expect(toolCache.downloadTool).toBeCalled()
// expect(fs.readFileSync).toHaveBeenCalledWith('pathToTool', 'utf8')
expect(await run.getStableKubectlVersion()).toBe('v1.20.4') // })
expect(toolCache.downloadTool).toBeCalled() // test('getStableKubectlVersion() - return default v1.15.0 if unable to download file', async () => {
expect(fs.readFileSync).toHaveBeenCalledWith('pathToTool', 'utf8') // jest
}) // .spyOn(toolCache, 'downloadTool')
// .mockRejectedValue('Unable to download.')
test('getStableKubectlVersion() - return default v1.15.0 if version read is empty', async () => { // expect(await run.getStableKubectlVersion()).toBe('v1.15.0')
jest // expect(toolCache.downloadTool).toBeCalled()
.spyOn(toolCache, 'downloadTool') // })
.mockReturnValue(Promise.resolve('pathToTool')) // test('downloadKubectl() - download kubectl, add it to toolCache and return path to it', async () => {
jest.spyOn(fs, 'readFileSync').mockReturnValue('') // jest.spyOn(toolCache, 'find').mockReturnValue('')
// jest
expect(await run.getStableKubectlVersion()).toBe('v1.15.0') // .spyOn(toolCache, 'downloadTool')
expect(toolCache.downloadTool).toBeCalled() // .mockReturnValue(Promise.resolve('pathToTool'))
expect(fs.readFileSync).toHaveBeenCalledWith('pathToTool', 'utf8') // jest
}) // .spyOn(toolCache, 'cacheFile')
// .mockReturnValue(Promise.resolve('pathToCachedTool'))
test('getStableKubectlVersion() - return default v1.15.0 if unable to download file', async () => { // jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
jest // jest.spyOn(fs, 'chmodSync').mockImplementation(() => {})
.spyOn(toolCache, 'downloadTool') // expect(await run.downloadKubectl('v1.15.0')).toBe(
.mockRejectedValue('Unable to download.') // path.join('pathToCachedTool', 'kubectl.exe')
// )
expect(await run.getStableKubectlVersion()).toBe('v1.15.0') // expect(toolCache.find).toHaveBeenCalledWith('kubectl', 'v1.15.0')
expect(toolCache.downloadTool).toBeCalled() // expect(toolCache.downloadTool).toBeCalled()
}) // expect(toolCache.cacheFile).toBeCalled()
// expect(os.type).toBeCalled()
test('downloadKubectl() - download kubectl, add it to toolCache and return path to it', async () => { // expect(fs.chmodSync).toHaveBeenCalledWith(
jest.spyOn(toolCache, 'find').mockReturnValue('') // path.join('pathToCachedTool', 'kubectl.exe'),
jest // '775'
.spyOn(toolCache, 'downloadTool') // )
.mockReturnValue(Promise.resolve('pathToTool')) // })
jest // test('downloadKubectl() - throw DownloadKubectlFailed error when unable to download kubectl', async () => {
.spyOn(toolCache, 'cacheFile') // jest.spyOn(toolCache, 'find').mockReturnValue('')
.mockReturnValue(Promise.resolve('pathToCachedTool')) // jest
jest.spyOn(os, 'type').mockReturnValue('Windows_NT') // .spyOn(toolCache, 'downloadTool')
jest.spyOn(fs, 'chmodSync').mockImplementation(() => {}) // .mockRejectedValue('Unable to download kubectl.')
// await expect(run.downloadKubectl('v1.15.0')).rejects.toThrow(
expect(await run.downloadKubectl('v1.15.0')).toBe( // 'DownloadKubectlFailed'
path.join('pathToCachedTool', 'kubectl.exe') // )
) // expect(toolCache.find).toHaveBeenCalledWith('kubectl', 'v1.15.0')
expect(toolCache.find).toHaveBeenCalledWith('kubectl', 'v1.15.0') // expect(toolCache.downloadTool).toBeCalled()
expect(toolCache.downloadTool).toBeCalled() // })
expect(toolCache.cacheFile).toBeCalled() // test('downloadKubectl() - throw kubectl not found error when receive 404 response', async () => {
expect(os.type).toBeCalled() // const kubectlVersion = 'v1.15.0'
expect(fs.chmodSync).toHaveBeenCalledWith( // const arch = 'arm128'
path.join('pathToCachedTool', 'kubectl.exe'), // jest.spyOn(os, 'arch').mockReturnValue(arch)
'775' // jest.spyOn(toolCache, 'find').mockReturnValue('')
) // jest.spyOn(toolCache, 'downloadTool').mockImplementation((_) => {
}) // throw new toolCache.HTTPError(404)
// })
test('downloadKubectl() - throw DownloadKubectlFailed error when unable to download kubectl', async () => { // await expect(run.downloadKubectl(kubectlVersion)).rejects.toThrow(
jest.spyOn(toolCache, 'find').mockReturnValue('') // util.format(
jest // "Kubectl '%s' for '%s' arch not found.",
.spyOn(toolCache, 'downloadTool') // kubectlVersion,
.mockRejectedValue('Unable to download kubectl.') // arch
// )
await expect(run.downloadKubectl('v1.15.0')).rejects.toThrow( // )
'DownloadKubectlFailed' // expect(os.arch).toBeCalled()
) // expect(toolCache.find).toHaveBeenCalledWith('kubectl', kubectlVersion)
expect(toolCache.find).toHaveBeenCalledWith('kubectl', 'v1.15.0') // 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).toHaveBeenCalledWith('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( expect(await run.downloadKubectl('v1.15.0')).toBe(
path.join('pathToCachedTool', 'kubectl.exe') path.join('pathToCachedTool', 'kubectl.exe')
) )
@ -189,47 +164,43 @@ describe('Testing all functions in run file.', () => {
) )
expect(toolCache.downloadTool).not.toBeCalled() expect(toolCache.downloadTool).not.toBeCalled()
}) })
// test('run() - download specified version and set output', async () => {
test('run() - download specified version and set output', async () => { // jest.spyOn(core, 'getInput').mockReturnValue('v1.15.5')
jest.spyOn(core, 'getInput').mockReturnValue('v1.15.5') // 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(core, 'addPath').mockImplementation()
jest.spyOn(core, 'addPath').mockImplementation() // jest.spyOn(console, 'log').mockImplementation()
jest.spyOn(console, 'log').mockImplementation() // jest.spyOn(core, 'setOutput').mockImplementation()
jest.spyOn(core, 'setOutput').mockImplementation() // expect(await run.run()).toBeUndefined()
// expect(core.getInput).toHaveBeenCalledWith('version', {required: true})
expect(await run.run()).toBeUndefined() // expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool')
expect(core.getInput).toHaveBeenCalledWith('version', {required: true}) // expect(core.setOutput).toHaveBeenCalledWith(
expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool') // 'kubectl-path',
expect(core.setOutput).toHaveBeenCalledWith( // path.join('pathToCachedTool', 'kubectl.exe')
'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
test('run() - get latest version, download it and set output', async () => { // .spyOn(toolCache, 'downloadTool')
jest.spyOn(core, 'getInput').mockReturnValue('latest') // .mockReturnValue(Promise.resolve('pathToTool'))
jest // jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.20.4')
.spyOn(toolCache, 'downloadTool') // jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool')
.mockReturnValue(Promise.resolve('pathToTool')) // jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.20.4') // jest.spyOn(fs, 'chmodSync').mockImplementation()
jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool') // jest.spyOn(core, 'addPath').mockImplementation()
jest.spyOn(os, 'type').mockReturnValue('Windows_NT') // jest.spyOn(console, 'log').mockImplementation()
jest.spyOn(fs, 'chmodSync').mockImplementation() // jest.spyOn(core, 'setOutput').mockImplementation()
jest.spyOn(core, 'addPath').mockImplementation() // expect(await run.run()).toBeUndefined()
jest.spyOn(console, 'log').mockImplementation() // expect(toolCache.downloadTool).toHaveBeenCalledWith(
jest.spyOn(core, 'setOutput').mockImplementation() // 'https://storage.googleapis.com/kubernetes-release/release/stable.txt'
// )
expect(await run.run()).toBeUndefined() // expect(core.getInput).toHaveBeenCalledWith('version', {required: true})
expect(toolCache.downloadTool).toHaveBeenCalledWith( // expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool')
'https://storage.googleapis.com/kubernetes-release/release/stable.txt' // expect(core.setOutput).toHaveBeenCalledWith(
) // 'kubectl-path',
expect(core.getInput).toHaveBeenCalledWith('version', {required: true}) // path.join('pathToCachedTool', 'kubectl.exe')
expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool') // )
expect(core.setOutput).toHaveBeenCalledWith( // })
'kubectl-path',
path.join('pathToCachedTool', 'kubectl.exe')
)
})
}) })