Skip to content

Commit

Permalink
Fixing bad failure logic + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NorseGaud committed Jun 18, 2020
1 parent 9225b4e commit 58218c1
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 110 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/failure-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Failure Tests
on:
push:
branches: [ master, release/* ]

jobs:

one:
runs-on: [self-hosted, macOS]
steps:
- name: command failure
id: command-failure
uses: ./
with:
anka-template: "10.15.4"
anka-tag: "base"
anka-run-options: "-n"
commands: "java -version"
two:
runs-on: [self-hosted, macOS]
steps:
- name: bad host command option
id: bad-host-command-option
uses: ./
with:
anka-template: "10.15.4"
anka-tag: "base"
anka-run-options: "-n"
host-command-options: "1231!!"
commands: "echo 123"
three:
runs-on: [self-hosted, macOS]
steps:
- name: multi-line commands failure
id: multi-line-commands-failure
uses: ./
with:
anka-template: "10.15.4"
anka-tag: "base"
anka-run-options: "-n"
commands: |
echo 123
java -version
2 changes: 1 addition & 1 deletion .github/workflows/functional-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
anka-template: "10.15.4"
anka-tag: "base"
anka-run-options: "-n"
commands: "echo 123"
commands: "echo \"123\""

functional-tests:
runs-on: [self-hosted, macOS]
Expand Down
117 changes: 60 additions & 57 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,11 @@ async function nodeCommands(commands,hostCommandOptions,existingSTD) {
}
}
// Execute
await exec.exec('bash', ['-c', commands], options);
try {
await exec.exec('bash', ['-c', commands], options);
} catch(error) {
throw new Error(`nodeCommands exec.exec\n${error.stack}`)
}
// Return STD outputs
exports.STD = STD;
exports.finalHostCommandOptions = options; // used in tests
Expand Down Expand Up @@ -2048,64 +2052,63 @@ const ankaTemplate = core.getInput('anka-template');
const lockFileLocation = core.getInput('lock-file-location');

async function run() {

const ankaTag = core.getInput('anka-tag');
const ankaVMLabel = await helpers.getVMLabel(ankaCustomVMLabel)

const ankaCommands = core.getInput('commands');
const hostPreCommands = core.getInput('host-pre-commands');
const hostPostCommands = core.getInput('host-post-commands');

const ankaStartOptions = core.getInput('anka-start-options');
const ankaRunOptions = core.getInput('anka-run-options');
const ankaRegistryPullOptions = core.getInput('anka-registry-pull-options');

const filesToArtifact = core.getInput('artifact-files');
const artifactArchiveFileName = core.getInput('artifact-archive-file-name');
const artifactRootDirectory = core.getInput('artifacts-root-directory');

const skipRegistryPull = core.getInput('skip-registry-pull');

const ankaVirtCLIPath = await io.which('anka', true)
console.log(`Anka Virtualization CLI found at: ${ankaVirtCLIPath}`);
if (ankaTemplate.length === 0) {
throw new Error('anka-template is required in your workflow definition!');
}
// Execution =========
if (hostPreCommands) {
await execute.nodeCommands(hostPreCommands,hostCommandOptions,execute.STD)
}
/// Prepare VM
//// Pull from Registry
await prepare.ankaRegistryPull(ankaTemplate,ankaTag,ankaRegistryPullOptions,hostCommandOptions,lockFileLocation,skipRegistryPull);
//// Clone from template
await prepare.ankaClone(ankaTemplate,ankaVMLabel,hostCommandOptions,lockFileLocation);
//// Start the VM
await prepare.ankaStart(ankaVMLabel,ankaStartOptions,hostCommandOptions);
/// Run commands inside VM
console.log(`Running commands inside of Anka VM ==============\nAnka run options: ${ankaRunOptions}\n${ankaCommands}\n==============\n`)
await execute.ankaRun(ankaVMLabel,ankaRunOptions,ankaCommands,hostCommandOptions);
if (hostPostCommands) {
await execute.nodeCommands(hostPostCommands,hostCommandOptions,execute.STD);
}
if (filesToArtifact) { // No artifacts, no problem!
await prepare.uploadArtifacts(artifactArchiveFileName,filesToArtifact,artifactRootDirectory,hostCommandOptions)
}
core.setOutput('std', execute.STD);
/// Cleanup
helpers.cleanup(ankaCustomVMLabel,hostCommandOptions,ankaTemplate,lockFileLocation)
}

try {
// We use GITHUB_ACTION in the ENV to prevent other steps from using isPost when they didn't really fail.
if (process.env[`${process.env['GITHUB_ACTION']}_isPost`] === 'true') {
try {
const ankaTag = core.getInput('anka-tag');
const ankaVMLabel = await helpers.getVMLabel(ankaCustomVMLabel)

const ankaCommands = core.getInput('commands');
const hostPreCommands = core.getInput('host-pre-commands');
const hostPostCommands = core.getInput('host-post-commands');

const ankaStartOptions = core.getInput('anka-start-options');
const ankaRunOptions = core.getInput('anka-run-options');
const ankaRegistryPullOptions = core.getInput('anka-registry-pull-options');

const filesToArtifact = core.getInput('artifact-files');
const artifactArchiveFileName = core.getInput('artifact-archive-file-name');
const artifactRootDirectory = core.getInput('artifacts-root-directory');

const skipRegistryPull = core.getInput('skip-registry-pull');

const ankaVirtCLIPath = await io.which('anka', true)
console.log(`Anka Virtualization CLI found at: ${ankaVirtCLIPath}`);
if (ankaTemplate.length === 0) {
throw new Error('anka-template is required in your workflow definition!');
}
// Execution =========
if (hostPreCommands) {
await execute.nodeCommands(hostPreCommands,hostCommandOptions,execute.STD)
}
/// Prepare VM
//// Pull from Registry
await prepare.ankaRegistryPull(ankaTemplate,ankaTag,ankaRegistryPullOptions,hostCommandOptions,lockFileLocation,skipRegistryPull);
//// Clone from template
await prepare.ankaClone(ankaTemplate,ankaVMLabel,hostCommandOptions,lockFileLocation);
//// Start the VM
await prepare.ankaStart(ankaVMLabel,ankaStartOptions,hostCommandOptions);
/// Run commands inside VM
console.log(`Running commands inside of Anka VM ==============\nAnka run options: ${ankaRunOptions}\n${ankaCommands}\n==============\n`)
await execute.ankaRun(ankaVMLabel,ankaRunOptions,ankaCommands,hostCommandOptions);
if (hostPostCommands) {
await execute.nodeCommands(hostPostCommands,hostCommandOptions,execute.STD);
}
if (filesToArtifact) { // No artifacts, no problem!
await prepare.uploadArtifacts(artifactArchiveFileName,filesToArtifact,artifactRootDirectory,hostCommandOptions)
}
core.setOutput('std', execute.STD);
/// Cleanup
helpers.cleanup(ankaCustomVMLabel,hostCommandOptions,ankaTemplate,lockFileLocation)
} else {
core.exportVariable(`${process.env['GITHUB_ACTION']}_isPost`, true);
run()
} catch (error) {
core.setFailed(`${error.stack}`);
}
} catch (error) {
core.setFailed(error.stack);
}

// We use GITHUB_ACTION in the ENV to prevent other steps from using isPost when they didn't really fail.
if (process.env[`${process.env['GITHUB_ACTION']}_isPost`] === 'true') {
helpers.cleanup(ankaCustomVMLabel,hostCommandOptions,ankaTemplate,lockFileLocation)
} else {
core.exportVariable(`${process.env['GITHUB_ACTION']}_isPost`, true);
run()
}


Expand Down
6 changes: 5 additions & 1 deletion execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ async function nodeCommands(commands,hostCommandOptions,existingSTD) {
}
}
// Execute
await exec.exec('bash', ['-c', commands], options);
try {
await exec.exec('bash', ['-c', commands], options);
} catch(error) {
throw new Error(`nodeCommands exec.exec\n${error.stack}`)
}
// Return STD outputs
exports.STD = STD;
exports.finalHostCommandOptions = options; // used in tests
Expand Down
7 changes: 6 additions & 1 deletion execute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ let execute = require('./execute');

describe('execute functions', () => {

let options = "{ silent: true }"
let options = "{ silent: false }"
describe('nodeCommands', () => {
test('basic', async() => {
await execute.nodeCommands("echo 123",options)
await expect(`${execute.STD.trim()}`).toBe("123");
});
test('fail with bad command option', async() => {
await expect(
execute.nodeCommands("java --version",options)
).rejects.toThrowError(/failed with exit code 1/)
});
test('fail with single quotes', async() => {
await expect(
execute.nodeCommands("echo 123","{ cwd: './' }")
Expand Down
99 changes: 49 additions & 50 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,61 @@ const ankaTemplate = core.getInput('anka-template');
const lockFileLocation = core.getInput('lock-file-location');

async function run() {
try {
const ankaTag = core.getInput('anka-tag');
const ankaVMLabel = await helpers.getVMLabel(ankaCustomVMLabel)

const ankaTag = core.getInput('anka-tag');
const ankaVMLabel = await helpers.getVMLabel(ankaCustomVMLabel)
const ankaCommands = core.getInput('commands');
const hostPreCommands = core.getInput('host-pre-commands');
const hostPostCommands = core.getInput('host-post-commands');

const ankaCommands = core.getInput('commands');
const hostPreCommands = core.getInput('host-pre-commands');
const hostPostCommands = core.getInput('host-post-commands');
const ankaStartOptions = core.getInput('anka-start-options');
const ankaRunOptions = core.getInput('anka-run-options');
const ankaRegistryPullOptions = core.getInput('anka-registry-pull-options');

const ankaStartOptions = core.getInput('anka-start-options');
const ankaRunOptions = core.getInput('anka-run-options');
const ankaRegistryPullOptions = core.getInput('anka-registry-pull-options');
const filesToArtifact = core.getInput('artifact-files');
const artifactArchiveFileName = core.getInput('artifact-archive-file-name');
const artifactRootDirectory = core.getInput('artifacts-root-directory');

const filesToArtifact = core.getInput('artifact-files');
const artifactArchiveFileName = core.getInput('artifact-archive-file-name');
const artifactRootDirectory = core.getInput('artifacts-root-directory');
const skipRegistryPull = core.getInput('skip-registry-pull');

const skipRegistryPull = core.getInput('skip-registry-pull');

const ankaVirtCLIPath = await io.which('anka', true)
console.log(`Anka Virtualization CLI found at: ${ankaVirtCLIPath}`);
if (ankaTemplate.length === 0) {
throw new Error('anka-template is required in your workflow definition!');
}
// Execution =========
if (hostPreCommands) {
await execute.nodeCommands(hostPreCommands,hostCommandOptions,execute.STD)
}
/// Prepare VM
//// Pull from Registry
await prepare.ankaRegistryPull(ankaTemplate,ankaTag,ankaRegistryPullOptions,hostCommandOptions,lockFileLocation,skipRegistryPull);
//// Clone from template
await prepare.ankaClone(ankaTemplate,ankaVMLabel,hostCommandOptions,lockFileLocation);
//// Start the VM
await prepare.ankaStart(ankaVMLabel,ankaStartOptions,hostCommandOptions);
/// Run commands inside VM
console.log(`Running commands inside of Anka VM ==============\nAnka run options: ${ankaRunOptions}\n${ankaCommands}\n==============\n`)
await execute.ankaRun(ankaVMLabel,ankaRunOptions,ankaCommands,hostCommandOptions);
if (hostPostCommands) {
await execute.nodeCommands(hostPostCommands,hostCommandOptions,execute.STD);
}
if (filesToArtifact) { // No artifacts, no problem!
await prepare.uploadArtifacts(artifactArchiveFileName,filesToArtifact,artifactRootDirectory,hostCommandOptions)
const ankaVirtCLIPath = await io.which('anka', true)
console.log(`Anka Virtualization CLI found at: ${ankaVirtCLIPath}`);
if (ankaTemplate.length === 0) {
throw new Error('anka-template is required in your workflow definition!');
}
// Execution =========
if (hostPreCommands) {
await execute.nodeCommands(hostPreCommands,hostCommandOptions,execute.STD)
}
/// Prepare VM
//// Pull from Registry
await prepare.ankaRegistryPull(ankaTemplate,ankaTag,ankaRegistryPullOptions,hostCommandOptions,lockFileLocation,skipRegistryPull);
//// Clone from template
await prepare.ankaClone(ankaTemplate,ankaVMLabel,hostCommandOptions,lockFileLocation);
//// Start the VM
await prepare.ankaStart(ankaVMLabel,ankaStartOptions,hostCommandOptions);
/// Run commands inside VM
console.log(`Running commands inside of Anka VM ==============\nAnka run options: ${ankaRunOptions}\n${ankaCommands}\n==============\n`)
await execute.ankaRun(ankaVMLabel,ankaRunOptions,ankaCommands,hostCommandOptions);
if (hostPostCommands) {
await execute.nodeCommands(hostPostCommands,hostCommandOptions,execute.STD);
}
if (filesToArtifact) { // No artifacts, no problem!
await prepare.uploadArtifacts(artifactArchiveFileName,filesToArtifact,artifactRootDirectory,hostCommandOptions)
}
core.setOutput('std', execute.STD);
/// Cleanup
helpers.cleanup(ankaCustomVMLabel,hostCommandOptions,ankaTemplate,lockFileLocation)
} catch (error) {
core.setFailed(`${error.stack}`);
}
core.setOutput('std', execute.STD);
/// Cleanup
helpers.cleanup(ankaCustomVMLabel,hostCommandOptions,ankaTemplate,lockFileLocation)
}

try {
// We use GITHUB_ACTION in the ENV to prevent other steps from using isPost when they didn't really fail.
if (process.env[`${process.env['GITHUB_ACTION']}_isPost`] === 'true') {
helpers.cleanup(ankaCustomVMLabel,hostCommandOptions,ankaTemplate,lockFileLocation)
} else {
core.exportVariable(`${process.env['GITHUB_ACTION']}_isPost`, true);
run()
}
} catch (error) {
core.setFailed(error.stack);
// We use GITHUB_ACTION in the ENV to prevent other steps from using isPost when they didn't really fail.
if (process.env[`${process.env['GITHUB_ACTION']}_isPost`] === 'true') {
helpers.cleanup(ankaCustomVMLabel,hostCommandOptions,ankaTemplate,lockFileLocation)
} else {
core.exportVariable(`${process.env['GITHUB_ACTION']}_isPost`, true);
run()
}

0 comments on commit 58218c1

Please sign in to comment.