mirror of
https://github.com/azure/setup-kubectl.git
synced 2026-12-03 19:51:53 +00:00
7500adf963
* feat: Implement resolveKubectlVersion function with comprehensive test coverage Introduce resolveKubectlVersion function that enables automatic selection of the latest patch version when provided with major.minor version input (e.g., '1.27' resolves to 'v1.27.15') Test Coverage: - Major.minor version expansion to latest available patch - Full version passthrough behavior (returns unchanged) - Single matching version selection logic - Comprehensive unit tests for kubectl version resolution scenarios * chore: fix Prettier formatting * refactor(resolveKubectlVersion): switch to k8s CDN for security patch retrieval Replaced GitHub API Octo client with k8s CDN to fetch the latest security patch for improved reliability. Separated the API call logic from resolveKubectlVersion to enhance testability and readability. * feat: validate semantic version and refactor patch logic - Added validation to `resolveKubectlVersion` to ensure input follows "major.minor" or "major.minor.patch" format. - Moved `getLatestPatchVersion` from `run.ts` to `helpers.ts` to improve code organization and ensure a more robust testing process. * Bump github/codeql-action in /.github/workflows in the actions group (#173) Bumps the actions group in /.github/workflows with 1 update: [github/codeql-action](https://github.com/github/codeql-action). Updates `github/codeql-action` from 3.29.0 to 3.29.1 - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/github/codeql-action/compare/ce28f5bb42b7a9f2c824e633a3f6ee835bab6858...39edc492dbe16b1465b0cafca41432d857bdb31a) --- updated-dependencies: - dependency-name: github/codeql-action dependency-version: 3.29.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: actions ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore: fix code style issues with Prettier * revised parsing logic * Improved readability and maintainability * regenerated package-lock.json * Regenerated Package-lock.json * removed unnecessary files * regenerated package-lock.json * Regenerate package-lock.json to match package.json version ranges * Restore package-lock.json to previous version * uninstall ncc and regenerate package-lock.json using npm ci --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
264 lines
11 KiB
TypeScript
264 lines
11 KiB
TypeScript
import * as run from './run'
|
|
import {
|
|
getkubectlDownloadURL,
|
|
getKubectlArch,
|
|
getExecutableExtension,
|
|
getLatestPatchVersion
|
|
} 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.', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
})
|
|
test('getExecutableExtension() - return .exe when os is Windows', () => {
|
|
jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
|
|
expect(getExecutableExtension()).toBe('.exe')
|
|
expect(os.type).toHaveBeenCalled()
|
|
})
|
|
test('getExecutableExtension() - return empty string for non-windows OS', () => {
|
|
jest.spyOn(os, 'type').mockReturnValue('Darwin')
|
|
expect(getExecutableExtension()).toBe('')
|
|
expect(os.type).toHaveBeenCalled()
|
|
})
|
|
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 as NodeJS.Architecture)
|
|
expect(getKubectlArch()).toBe(kubectlArch)
|
|
expect(os.arch).toHaveBeenCalled()
|
|
}
|
|
)
|
|
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).toHaveBeenCalled()
|
|
}
|
|
)
|
|
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).toHaveBeenCalled()
|
|
}
|
|
)
|
|
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).toHaveBeenCalled()
|
|
}
|
|
)
|
|
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).toHaveBeenCalled()
|
|
expect(fs.readFileSync).toHaveBeenCalledWith('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).toHaveBeenCalled()
|
|
expect(fs.readFileSync).toHaveBeenCalledWith('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).toHaveBeenCalled()
|
|
})
|
|
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).toHaveBeenCalledWith('kubectl', 'v1.15.0')
|
|
expect(toolCache.downloadTool).toHaveBeenCalled()
|
|
expect(toolCache.cacheFile).toHaveBeenCalled()
|
|
expect(os.type).toHaveBeenCalled()
|
|
expect(fs.chmodSync).toHaveBeenCalledWith(
|
|
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).toHaveBeenCalledWith('kubectl', 'v1.15.0')
|
|
expect(toolCache.downloadTool).toHaveBeenCalled()
|
|
})
|
|
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 as any)
|
|
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).toHaveBeenCalled()
|
|
expect(toolCache.find).toHaveBeenCalledWith('kubectl', kubectlVersion)
|
|
expect(toolCache.downloadTool).toHaveBeenCalled()
|
|
})
|
|
test('downloadKubectl() - return path to existing cache of kubectl', async () => {
|
|
jest.spyOn(core, 'getInput').mockImplementation(() => 'v1.15.5')
|
|
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).toHaveBeenCalledWith('kubectl', 'v1.15.0')
|
|
expect(os.type).toHaveBeenCalled()
|
|
expect(fs.chmodSync).toHaveBeenCalledWith(
|
|
path.join('pathToCachedTool', 'kubectl.exe'),
|
|
'775'
|
|
)
|
|
expect(toolCache.downloadTool).not.toHaveBeenCalled()
|
|
})
|
|
test('getLatestPatchVersion() - download and return latest patch version', async () => {
|
|
jest.spyOn(toolCache, 'downloadTool').mockResolvedValue('pathToTool')
|
|
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.27.15')
|
|
|
|
const result = await getLatestPatchVersion('1', '27')
|
|
|
|
expect(result).toBe('v1.27.15')
|
|
expect(toolCache.downloadTool).toHaveBeenCalledWith(
|
|
'https://cdn.dl.k8s.io/release/stable-1.27.txt'
|
|
)
|
|
})
|
|
|
|
test('getLatestPatchVersion() - throw error when patch version is empty', async () => {
|
|
jest.spyOn(toolCache, 'downloadTool').mockResolvedValue('pathToTool')
|
|
jest.spyOn(fs, 'readFileSync').mockReturnValue('')
|
|
|
|
await expect(getLatestPatchVersion('1', '27')).rejects.toThrow(
|
|
'Failed to get latest patch version for 1.27'
|
|
)
|
|
})
|
|
|
|
test('getLatestPatchVersion() - throw error when download fails', async () => {
|
|
jest
|
|
.spyOn(toolCache, 'downloadTool')
|
|
.mockRejectedValue(new Error('Network error'))
|
|
|
|
await expect(getLatestPatchVersion('1', '27')).rejects.toThrow(
|
|
'Failed to get latest patch version for 1.27'
|
|
)
|
|
})
|
|
test('resolveKubectlVersion() - expands major.minor to latest patch', async () => {
|
|
jest.spyOn(toolCache, 'downloadTool').mockResolvedValue('pathToTool')
|
|
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.27.15')
|
|
|
|
const result = await run.resolveKubectlVersion('1.27')
|
|
expect(result).toBe('v1.27.15')
|
|
})
|
|
|
|
test('resolveKubectlVersion() - returns full version unchanged', async () => {
|
|
const result = await run.resolveKubectlVersion('v1.27.15')
|
|
expect(result).toBe('v1.27.15')
|
|
})
|
|
test('resolveKubectlVersion() - adds v prefix to full version', async () => {
|
|
const result = await run.resolveKubectlVersion('1.27.15')
|
|
expect(result).toBe('v1.27.15')
|
|
})
|
|
test('resolveKubectlVersion() - expands v-prefixed major.minor to latest patch', async () => {
|
|
jest.spyOn(toolCache, 'downloadTool').mockResolvedValue('pathToTool')
|
|
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.27.15')
|
|
|
|
const result = await run.resolveKubectlVersion('v1.27')
|
|
expect(result).toBe('v1.27.15')
|
|
})
|
|
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).toHaveBeenCalledWith('version', {required: true})
|
|
expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool')
|
|
expect(core.setOutput).toHaveBeenCalledWith(
|
|
'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).toHaveBeenCalledWith(
|
|
'https://storage.googleapis.com/kubernetes-release/release/stable.txt'
|
|
)
|
|
expect(core.getInput).toHaveBeenCalledWith('version', {required: true})
|
|
expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool')
|
|
expect(core.setOutput).toHaveBeenCalledWith(
|
|
'kubectl-path',
|
|
path.join('pathToCachedTool', 'kubectl.exe')
|
|
)
|
|
})
|
|
})
|