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 L0 tests. #15

Merged
merged 2 commits into from
Mar 25, 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
68 changes: 11 additions & 57 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,67 +1,21 @@
on:
name: "build-test"
on: # rebuild any PRs and main branch changes
pull_request:
branches:
- main
- 'releases/*'
push:
branches:
- main
- 'releases/*'

jobs:
build_test_job:
name: 'Build and test job'
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
build: # make sure build/ci works properly
runs-on: ubuntu-latest
steps:

- name: 'Checking out repo code'
uses: actions/checkout@v1

- name: 'Run L0 tests'
- uses: actions/checkout@v1
- name: build and run tests
run: |
npm install
npm run test

- name : 'Run test coverage'
if: runner.os == 'Windows' && (github.event.pull_request.base.ref == 'releases/v1' || github.event.pull_request.base.ref == 'main')
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
$coverage_result = npm run test-coverage
$start = $false;
$middle = $false;
$end = $false;
$count = 0;

foreach ($j in $coverage_result)
{
if ($j.tostring().startswith("----------"))
{
if (!$start)
{
$start = $true;
$start_index = $count
}
elseif (!$middle)
{
$middle = $true;
}
elseif (!$end)
{
$end = $true;
$end_index = $count
}
}
$count++
}

$tbl_md = $coverage_result[($start_index+1)..($end_index-1)] -join "\n"
$summary = $coverage_result[($end_index + 1)..$count] -join "\n"
$comment = $tbl_md + "\n" + $summary
$url = "https://api.github.com/repos/${env:GITHUB_REPOSITORY}/issues/${env:PR_NUMBER}/comments"
$headers = @{
"Authorization" = "token ${env:GITHUB_TOKEN}"
}
$body = "{ `"body`": `"${comment}`" }"
Invoke-RestMethod -Method POST -Uri $url -Headers $headers -Body $body
npm build
npm test
146 changes: 140 additions & 6 deletions __tests__/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,141 @@
import { run } from '../src/run'
import * as run from '../src/run'
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';

describe('This is a placeholder for intial test cases, to be removed', () => {
test('Dummy test case', async () => {
await expect(run()).rejects.toThrow();
})
})
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();
});

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

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

test('getkubectlDownloadURL() - return the URL to download kubectl for Linux', () => {
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();
});

test('getkubectlDownloadURL() - return the URL to download kubectl for Darwin', () => {
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();
});

test('getkubectlDownloadURL() - return the URL to download kubectl for Windows', () => {
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();
});

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'), '777');
});

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() - 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'), '777');
expect(toolCache.downloadTool).not.toBeCalled();
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test for function run() ?


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'));
});
});
10 changes: 5 additions & 5 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const kubectlToolName = 'kubectl';
const stableKubectlVersion = 'v1.15.0';
const stableVersionUrl = 'https://storage.googleapis.com/kubernetes-release/release/stable.txt';

function getExecutableExtension(): string {
export function getExecutableExtension(): string {
if (os.type().match(/^Win/)) {
return '.exe';
}
return '';
}

function getkubectlDownloadURL(version: string): string {
export function getkubectlDownloadURL(version: string): string {
switch (os.type()) {
case 'Linux':
return util.format('https://storage.googleapis.com/kubernetes-release/release/%s/bin/linux/amd64/kubectl', version);
Expand All @@ -32,7 +32,7 @@ function getkubectlDownloadURL(version: string): string {
}
}

async function getStableKubectlVersion(): Promise<string> {
export async function getStableKubectlVersion(): Promise<string> {
return toolCache.downloadTool(stableVersionUrl).then((downloadPath) => {
let version = fs.readFileSync(downloadPath, 'utf8').toString().trim();
if (!version) {
Expand All @@ -46,7 +46,7 @@ async function getStableKubectlVersion(): Promise<string> {
});
}

async function downloadKubectl(version: string): Promise<string> {
export async function downloadKubectl(version: string): Promise<string> {
let cachedToolpath = toolCache.find(kubectlToolName, version);
let kubectlDownloadPath = '';
if (!cachedToolpath) {
Expand Down Expand Up @@ -77,4 +77,4 @@ export async function run() {
core.setOutput('kubectl-path', cachedPath);
}

run().catch(core.setFailed);
run().catch(core.setFailed);