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>
This commit is contained in:
Vidya Reddy 2022-06-27 15:31:07 -07:00 committed by GitHub
parent b474dc39ef
commit 2eb2a370ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 11418 additions and 11311 deletions

View file

@ -4,7 +4,4 @@ about: Create a report to help us improve
title: ''
labels: need-to-triage
assignees: '@aksatlanta'
---

View file

@ -1,30 +1,30 @@
name: setting-default-labels
on:
schedule:
- cron: "0 0/3 * * *"
schedule:
- cron: '0 0/3 * * *'
jobs:
label-issues:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v3
name: Setting issue as idle
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: 'This issue is idle because it has been open for 14 days with no activity.'
stale-issue-label: 'idle'
days-before-stale: 14
days-before-close: -1
operations-per-run: 100
exempt-issue-labels: 'backlog'
- uses: actions/stale@v3
name: Setting PR as idle
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-pr-message: 'This PR is idle because it has been open for 14 days with no activity.'
stale-pr-label: 'idle'
days-before-stale: 14
days-before-close: -1
operations-per-run: 100
label-issues:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v3
name: Setting issue as idle
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: 'This issue is idle because it has been open for 14 days with no activity.'
stale-issue-label: 'idle'
days-before-stale: 14
days-before-close: -1
operations-per-run: 100
exempt-issue-labels: 'backlog'
- uses: actions/stale@v3
name: Setting PR as idle
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-pr-message: 'This PR is idle because it has been open for 14 days with no activity.'
stale-pr-label: 'idle'
days-before-stale: 14
days-before-close: -1
operations-per-run: 100

View file

@ -1,49 +1,49 @@
name: Integration test for setup-kubectl
on: # rebuild any PRs and main branch changes
pull_request:
branches:
- main
- "releases/*"
push:
branches:
- main
- "releases/*"
pull_request:
branches:
- main
- 'releases/*'
push:
branches:
- main
- 'releases/*'
jobs:
run-integration-test:
name: Validate release and master branch
runs-on: ubuntu-latest
env:
KUBECONFIG: /home/runner/.kube/config
PR_BASE_REF: ${{ github.event.pull_request.base.ref }}
steps:
- uses: actions/checkout@v2
name: Checkout from PR branch
run-integration-test:
name: Validate release and master branch
runs-on: ubuntu-latest
env:
KUBECONFIG: /home/runner/.kube/config
PR_BASE_REF: ${{ github.event.pull_request.base.ref }}
steps:
- uses: actions/checkout@v2
name: Checkout from PR branch
- id: action-npm-build
name: npm install and build
run: |
echo $PR_BASE_REF
if [[ $PR_BASE_REF != releases/* ]]; then
npm install
npm run build
fi
- id: action-npm-build
name: npm install and build
run: |
echo $PR_BASE_REF
if [[ $PR_BASE_REF != releases/* ]]; then
npm install
npm run build
fi
- uses: actions/setup-python@v2
name: Install Python
with:
python-version: "3.x"
- uses: actions/setup-python@v2
name: Install Python
with:
python-version: '3.x'
- name: Install requests library
run: pip install requests
- name: Install requests library
run: pip install requests
- name: Validate kubectl setup
run: python test/validate-kubectl.py !v1.15.1
- name: Validate kubectl setup
run: python test/validate-kubectl.py latest
- name: Setup kubectl
uses: ./
with:
version: "v1.15.1"
- name: Setup kubectl
uses: ./
with:
version: 'v1.15.1'
- name: Validate kubectl setup
run: python test/validate-kubectl.py 'v1.15.1'
- name: Validate kubectl setup
run: python test/validate-kubectl.py 'v1.15.1'

18
.github/workflows/prettify-code.yml vendored Normal file
View file

@ -0,0 +1,18 @@
name: 'Run prettify'
on:
pull_request:
push:
branches: [main]
jobs:
prettier:
name: Prettier Check
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Enforce Prettier
uses: actionsx/prettier@v2
with:
args: --check .

View file

@ -1,14 +1,14 @@
name: Create release PR
on:
workflow_dispatch:
inputs:
release:
description: "Define release version (ex: v1, v2, v3)"
required: true
workflow_dispatch:
inputs:
release:
description: 'Define release version (ex: v1, v2, v3)'
required: true
jobs:
release-pr:
uses: OliverMKing/javascript-release-workflow/.github/workflows/release-pr.yml@main
with:
release: ${{ github.event.inputs.release }}
release-pr:
uses: OliverMKing/javascript-release-workflow/.github/workflows/release-pr.yml@main
with:
release: ${{ github.event.inputs.release }}

View file

@ -1,10 +1,10 @@
name: Tag and create release draft
on:
push:
branches:
- releases/*
push:
branches:
- releases/*
jobs:
tag-and-release:
uses: OliverMKing/javascript-release-workflow/.github/workflows/tag-and-release.yml@main
tag-and-release:
uses: OliverMKing/javascript-release-workflow/.github/workflows/tag-and-release.yml@main

View file

@ -1,21 +1,21 @@
name: "Run unit tests."
name: 'Run unit tests.'
on: # rebuild any PRs and main branch changes
pull_request:
branches:
- main
- 'releases/*'
push:
branches:
- main
- 'releases/*'
pull_request:
branches:
- main
- 'releases/*'
push:
branches:
- main
- 'releases/*'
jobs:
build: # make sure build/ci works properly
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Build and run L0 tests.
run: |
npm install
npm test
build: # make sure build/ci works properly
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Build and run L0 tests.
run: |
npm install
npm test

4
.prettierignore Normal file
View file

@ -0,0 +1,4 @@
# dependencies
/node_modules
coverage
/lib

8
.prettierrc.json Normal file
View file

@ -0,0 +1,8 @@
{
"trailingComma": "none",
"bracketSpacing": false,
"semi": false,
"tabWidth": 3,
"singleQuote": true,
"printWidth": 80
}

View file

@ -1,9 +1,9 @@
# Microsoft Open Source Code of Conduct
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
Resources:
- [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/)
- [Microsoft Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/)
- Contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with questions or concerns
# Microsoft Open Source Code of Conduct
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
Resources:
- [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/)
- [Microsoft Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/)
- Contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with questions or concerns

View file

@ -1,26 +1,28 @@
# Setup Kubectl
#### Sample workflow to install a specific version of kubectl binary on the runner.
Acceptable values are latest or any semantic version string like `v1.15.0`. Use this action in workflow to define which version of kubectl will be used.
```yaml
- uses: azure/setup-kubectl@v2.0
with:
version: '<version>' # default is latest stable
id: install
```
Refer to the action metadata file for details about all the inputs https://github.com/Azure/setup-kubectl/blob/main/action.yml
# Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
# Setup Kubectl
#### Sample workflow to install a specific version of kubectl binary on the runner.
Acceptable values are latest or any semantic version string like `v1.15.0`. Use this action in workflow to define which version of kubectl will be used.
```yaml
- uses: azure/setup-kubectl@v2.0
with:
version: '<version>' # default is latest stable
id: install
```
Refer to the action metadata file for details about all the inputs https://github.com/Azure/setup-kubectl/blob/main/action.yml
# Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.

View file

@ -1,35 +1,35 @@
<!-- BEGIN MICROSOFT SECURITY.MD V0.0.1 BLOCK -->
## Security
Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/Microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet), [Xamarin](https://github.com/xamarin), and [many more](https://opensource.microsoft.com/).
If you believe you have found a security vulnerability in any Microsoft-owned repository that meets Microsoft's [definition](https://docs.microsoft.com/en-us/previous-versions/tn-archive/cc751383(v=technet.10)) of a security vulnerability, please report it to us as described below.
## Reporting Security Issues
**Please do not report security vulnerabilities through public GitHub issues.** Instead, please report them to the Microsoft Security Response Center at [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://technet.microsoft.com/en-us/security/dn606155).
You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://www.microsoft.com/msrc).
Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue:
* Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.)
* Full paths of source file(s) related to the manifestation of the issue
* The location of the affected source code (tag/branch/commit or direct URL)
* Any special configuration required to reproduce the issue
* Step-by-step instructions to reproduce the issue
* Proof-of-concept or exploit code (if possible)
* Impact of the issue, including how an attacker might exploit the issue
This information will help us triage your report more quickly.
## Preferred Languages
We prefer all communications to be in English.
## Policy
Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://www.microsoft.com/en-us/msrc/cvd).
<!-- END MICROSOFT SECURITY.MD BLOCK -->
<!-- BEGIN MICROSOFT SECURITY.MD V0.0.1 BLOCK -->
## Security
Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/Microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet), [Xamarin](https://github.com/xamarin), and [many more](https://opensource.microsoft.com/).
If you believe you have found a security vulnerability in any Microsoft-owned repository that meets Microsoft's [definition](<https://docs.microsoft.com/en-us/previous-versions/tn-archive/cc751383(v=technet.10)>) of a security vulnerability, please report it to us as described below.
## Reporting Security Issues
**Please do not report security vulnerabilities through public GitHub issues.** Instead, please report them to the Microsoft Security Response Center at [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://technet.microsoft.com/en-us/security/dn606155).
You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://www.microsoft.com/msrc).
Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue:
- Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.)
- Full paths of source file(s) related to the manifestation of the issue
- The location of the affected source code (tag/branch/commit or direct URL)
- Any special configuration required to reproduce the issue
- Step-by-step instructions to reproduce the issue
- Proof-of-concept or exploit code (if possible)
- Impact of the issue, including how an attacker might exploit the issue
This information will help us triage your report more quickly.
## Preferred Languages
We prefer all communications to be in English.
## Policy
Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://www.microsoft.com/en-us/msrc/cvd).
<!-- END MICROSOFT SECURITY.MD BLOCK -->

View file

@ -1,15 +1,15 @@
name: "Kubectl tool installer"
description: "Install a specific version of kubectl binary. Acceptable values are latest or any semantic version string like 1.15.0"
inputs:
version:
description: "Version of kubectl"
required: true
default: "latest"
outputs:
kubectl-path:
description: "Path to the cached kubectl binary"
branding:
color: "blue"
runs:
using: "node16"
main: "lib/index.js"
name: 'Kubectl tool installer'
description: 'Install a specific version of kubectl binary. Acceptable values are latest or any semantic version string like 1.15.0'
inputs:
version:
description: 'Version of kubectl'
required: true
default: 'latest'
outputs:
kubectl-path:
description: 'Path to the cached kubectl binary'
branding:
color: 'blue'
runs:
using: 'node16'
main: 'lib/index.js'

View file

@ -1,18 +1,18 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true,
coverageThreshold: {
"global": {
"branches": 0,
"functions": 14,
"lines": 27,
"statements": 27
}
}
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true,
coverageThreshold: {
global: {
branches: 0,
functions: 14,
lines: 27,
statements: 27
}
}
}

21655
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,31 +1,34 @@
{
"name": "setup-kubectl-action",
"version": "0.0.0",
"private": true,
"main": "lib/index.js",
"scripts": {
"build": "ncc build src/run.ts -o lib",
"test": "jest",
"test-coverage": "jest --coverage"
},
"keywords": [
"actions",
"node",
"setup"
],
"author": "GitHub",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.2.6",
"@actions/exec": "^1.0.0",
"@actions/tool-cache": "^1.0.0"
},
"devDependencies": {
"@types/node": "^12.0.4",
"@vercel/ncc": "^0.33.1",
"jest": "^26.0.1",
"@types/jest": "^26.0.1",
"ts-jest": "^26.0.1",
"typescript": "3.9.2"
}
}
{
"name": "setup-kubectl-action",
"version": "0.0.0",
"private": true,
"main": "lib/index.js",
"scripts": {
"build": "ncc build src/run.ts -o lib",
"test": "jest",
"test-coverage": "jest --coverage",
"format": "prettier --write .",
"format-check": "prettier --check ."
},
"keywords": [
"actions",
"node",
"setup"
],
"author": "GitHub",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.2.6",
"@actions/exec": "^1.0.0",
"@actions/tool-cache": "^1.0.0"
},
"devDependencies": {
"@types/jest": "^26.0.0",
"@types/node": "^12.0.4",
"@vercel/ncc": "^0.33.1",
"jest": "^26.0.1",
"prettier": "2.7.1",
"ts-jest": "^26.0.0",
"typescript": "3.9.2"
}
}

View file

@ -1,31 +1,43 @@
import * as os from 'os';
import * as util from 'util';
import * as os from 'os'
import * as util from 'util'
export function getKubectlArch(): string {
const arch = os.arch();
if (arch === 'x64') {
return 'amd64';
}
return arch;
const 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/%s/kubectl', version, arch);
switch (os.type()) {
case 'Linux':
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/%s/kubectl', version, arch);
case 'Darwin':
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/%s/kubectl.exe', version, arch);
}
case 'Windows_NT':
default:
return util.format(
'https://storage.googleapis.com/kubernetes-release/release/%s/bin/windows/%s/kubectl.exe',
version,
arch
)
}
}
export function getExecutableExtension(): string {
if (os.type().match(/^Win/)) {
return '.exe';
}
return '';
}
if (os.type().match(/^Win/)) {
return '.exe'
}
return ''
}

View file

@ -1,183 +1,235 @@
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';
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');
test('getExecutableExtension() - return .exe when os is Windows', () => {
jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
expect(getExecutableExtension()).toBe('.exe');
expect(os.type).toBeCalled();
});
expect(getExecutableExtension()).toBe('.exe')
expect(os.type).toBeCalled()
})
test('getExecutableExtension() - return empty string for non-windows OS', () => {
jest.spyOn(os, 'type').mockReturnValue('Darwin');
test('getExecutableExtension() - return empty string for non-windows OS', () => {
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'],
['x64', 'amd64']
])("getKubectlArch() - return on %s os arch %s kubectl arch", (osArch, kubectlArch) => {
jest.spyOn(os, 'arch').mockReturnValue(osArch);
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();
});
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://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/linux/%s/kubectl', arch);
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://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/linux/%s/kubectl',
arch
)
expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlLinuxUrl);
expect(os.type).toBeCalled();
});
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://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/darwin/%s/kubectl', arch);
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://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/darwin/%s/kubectl',
arch
)
expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlDarwinUrl);
expect(os.type).toBeCalled();
});
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');
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://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/windows/%s/kubectl.exe', arch);
expect(getkubectlDownloadURL('v1.15.0', arch)).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(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');
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');
});
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('');
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');
});
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.');
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();
});
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(() => { });
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');
});
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.');
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();
});
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';
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);
});
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();
});
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');
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();
});
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();
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'));
});
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();
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'));
});
});
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')
)
})
})

View file

@ -1,64 +1,93 @@
import * as path from 'path';
import * as util from 'util';
import * as fs from 'fs';
import * as toolCache from '@actions/tool-cache';
import * as core from '@actions/core';
import { getkubectlDownloadURL, getKubectlArch, getExecutableExtension } from './helpers';
const kubectlToolName = 'kubectl';
const stableKubectlVersion = 'v1.15.0';
const stableVersionUrl = 'https://storage.googleapis.com/kubernetes-release/release/stable.txt';
export async function run() {
let version = core.getInput('version', { 'required': true });
if (version.toLocaleLowerCase() === 'latest') {
version = await getStableKubectlVersion();
}
const cachedPath = await downloadKubectl(version);
core.addPath(path.dirname(cachedPath));
core.debug(`Kubectl tool version: '${version}' has been cached at ${cachedPath}`);
core.setOutput('kubectl-path', cachedPath);
}
export async function getStableKubectlVersion(): Promise<string> {
return toolCache.downloadTool(stableVersionUrl).then((downloadPath) => {
let version = fs.readFileSync(downloadPath, 'utf8').toString().trim();
if (!version) {
version = stableKubectlVersion;
}
return version;
}, (error) => {
core.debug(error);
core.warning('GetStableVersionFailed');
return stableKubectlVersion;
});
}
export async function downloadKubectl(version: string): Promise<string> {
let cachedToolpath = toolCache.find(kubectlToolName, version);
let kubectlDownloadPath = '';
const arch = getKubectlArch();
if (!cachedToolpath) {
try {
kubectlDownloadPath = await toolCache.downloadTool(getkubectlDownloadURL(version, arch));
} catch (exception) {
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);
}
const kubectlPath = path.join(cachedToolpath, kubectlToolName + getExecutableExtension());
fs.chmodSync(kubectlPath, '775');
return kubectlPath;
}
run().catch(core.setFailed);
import * as path from 'path'
import * as util from 'util'
import * as fs from 'fs'
import * as toolCache from '@actions/tool-cache'
import * as core from '@actions/core'
import {
getkubectlDownloadURL,
getKubectlArch,
getExecutableExtension
} from './helpers'
const kubectlToolName = 'kubectl'
const stableKubectlVersion = 'v1.15.0'
const stableVersionUrl =
'https://storage.googleapis.com/kubernetes-release/release/stable.txt'
export async function run() {
let version = core.getInput('version', {required: true})
if (version.toLocaleLowerCase() === 'latest') {
version = await getStableKubectlVersion()
}
const cachedPath = await downloadKubectl(version)
core.addPath(path.dirname(cachedPath))
core.debug(
`Kubectl tool version: '${version}' has been cached at ${cachedPath}`
)
core.setOutput('kubectl-path', cachedPath)
}
export async function getStableKubectlVersion(): Promise<string> {
return toolCache.downloadTool(stableVersionUrl).then(
(downloadPath) => {
let version = fs.readFileSync(downloadPath, 'utf8').toString().trim()
if (!version) {
version = stableKubectlVersion
}
return version
},
(error) => {
core.debug(error)
core.warning('GetStableVersionFailed')
return stableKubectlVersion
}
)
}
export async function downloadKubectl(version: string): Promise<string> {
let cachedToolpath = toolCache.find(kubectlToolName, version)
let kubectlDownloadPath = ''
const arch = getKubectlArch()
if (!cachedToolpath) {
try {
kubectlDownloadPath = await toolCache.downloadTool(
getkubectlDownloadURL(version, arch)
)
} catch (exception) {
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
)
}
const kubectlPath = path.join(
cachedToolpath,
kubectlToolName + getExecutableExtension()
)
fs.chmodSync(kubectlPath, '775')
return kubectlPath
}
run().catch(core.setFailed)

View file

@ -1,10 +1,7 @@
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs"
},
"exclude": [
"node_modules",
"test"
]
}
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs"
},
"exclude": ["node_modules", "test"]
}