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

Write Test Case For Azure cli Action #48

Merged
merged 7 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
47 changes: 47 additions & 0 deletions .github/workflows/ci-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: ci-workflow
on:
Copy link
Contributor

Choose a reason for hiding this comment

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

Replace on with this -

on:
  push:
  schedule:
    - cron: '0 */3 * * *'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

workflow_dispatch:
push:
jobs:
test_action_job:
runs-on: ubuntu-latest
steps:
- name: Check out source code
uses: actions/checkout@v1
- uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: "12.x"
- run: npm install --production

- run: sudo npm i -g ts-node
- run: npm install typescript

- name: Azure CLI Version test
env:
INPUT_AZCLIVERSION: 2.0.72
INPUT_INLINESCRIPT: |
az account show
az storage -h
EXPECTED_TO: pass
run: ts-node test/main.test.ts

- name: Azure CLI Version Test - Negative
env:
INPUT_AZCLIVERSION: 0
INPUT_INLINESCRIPT: |
az account show
az storage -h
EXPECTED_TO: fail
run: ts-node test/main.test.ts

- name: Inline Script Test - Negative
env:
INPUT_AZCLIVERSION: 2.0.72
INPUT_INLINESCRIPT: " "
EXPECTED_TO: fail
run: ts-node test/main.test.ts

Copy link
Contributor

Choose a reason for hiding this comment

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

Add post status action at the end

 - name: Post to slack on failure
        if: failure()
        uses: 8398a7/[email protected]
        with:
            status: ${{ job.status }}
            fields: repo,message,author,action,ref,workflow
        env:
            SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

11 changes: 8 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const BASH_ARG: string = `bash --noprofile --norc -e `;
const CONTAINER_WORKSPACE: string = '/github/workspace';
const CONTAINER_TEMP_DIRECTORY: string = '/_temp';

const run = async () => {

export const run = async () => {
var scriptFileName: string = '';
const CONTAINER_NAME = `MICROSOFT_AZURE_CLI_${getCurrentTime()}_CONTAINER`;
try {
Expand All @@ -21,15 +22,18 @@ const run = async () => {
}

let inlineScript: string = core.getInput('inlineScript', { required: true });
let azcliversion: string = core.getInput('azcliversion', { required: true }).trim().toLowerCase();

let azcliversion: string = core.getInput('azcliversion', { required: true });
Copy link
Contributor

Choose a reason for hiding this comment

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

Add .trim().toLowerCase() back

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added .trim().toLowerCase()

if (!(await checkIfValidCLIVersion(azcliversion))) {
console.log("INVALID VERSION")
Copy link
Contributor

Choose a reason for hiding this comment

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

remove this console.log

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

core.setFailed('Please enter a valid azure cli version. \nSee available versions: https://github.com/Azure/azure-cli/releases.');
throw new Error('Please enter a valid azure cli version. \nSee available versions: https://github.com/Azure/azure-cli/releases.')
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

remove return

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}

if (!inlineScript.trim()) {
core.setFailed('Please enter a valid script.');
throw new Error('Please enter a valid script.')
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

remove return

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}
inlineScript = ` set -e >&2; echo '${START_SCRIPT_EXECUTION_MARKER}' >&2; ${inlineScript}`;
Expand Down Expand Up @@ -66,6 +70,7 @@ const run = async () => {
} catch (error) {
core.error(error);
core.setFailed(error.stderr);
throw error;
}
finally {
// clean up
Expand Down
20 changes: 20 additions & 0 deletions test/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { run } from "../src/main";
import * as core from '@actions/core';

run()
.then(() => {
checkOutcome('pass')
})
.catch((e) => {
core.error(e)
checkOutcome('fail')
});

function checkOutcome(outcome){
if(outcome != process.env.EXPECTED_TO){
core.error(`Expected outcome did not meet the real outcome. Expected value: ${process.env.EXPECTED_TO}, actual value: ${outcome}`)
process.exit(1)
} else{
process.exit(0)
}
}