Skip to content

remove unused #24

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

Merged
merged 4 commits into from
Oct 28, 2024
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
17 changes: 17 additions & 0 deletions .github/workflows/npm-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: 'npm test'
on:
pull_request:

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/[email protected]
- name: Use Node.js 16
uses: actions/[email protected]
with:
node-version: 16.x
- run: npm ci
- run: npm test

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The `stackql/stackql-assert` action is an composite action that runs a `stackql`

# Usage

> This action uses the [setup-stackql](https://github.com/marketplace/actions/stackql-studios-setup-stackql) and [stackql-exec](https://github.com/marketplace/actions/stackql-studios-stackql-exec) actions
> This action uses the [setup-stackql](https://github.com/marketplace/actions/setup-stackql) and [stackql-exec](https://github.com/marketplace/actions/stackql-exec) actions

## Provider Authentication
Authentication to StackQL providers is done via environment variables source from GitHub Actions Secrets. To learn more about authentication, see the setup instructions for your provider or providers at the [StackQL Provider Registry Docs](https://stackql.io/registry).
Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 'StackQL Studios - StackQL Assert'
name: 'stackql-assert'
description: 'Run StackQL query to test and audit your infrastructure.'
author: 'Yuncheng Yang, StackQL Studios'
inputs:
Expand Down Expand Up @@ -66,4 +66,4 @@ runs:

branding:
icon: 'terminal'
color: 'green'
color: 'blue'
10 changes: 9 additions & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ function assertResult(core) {

if (!RESULT) throw new Error("Result from StackQL execution is missing.");

// if no EXPECTED_RESULTS_STR, EXPECTED_RESULTS_FILE_PATH or EXPECTED_ROWS, fail the action
if (!EXPECTED_RESULTS_STR && !EXPECTED_RESULTS_FILE_PATH && !EXPECTED_ROWS) throw new Error("❌ Cannot find expected result, file path or expected rows");

const actualResult = parseResult(RESULT);

core.info("🔍 Checking results...");
Expand All @@ -64,4 +67,9 @@ function assertResult(core) {
}
}

module.exports = { assertResult };
module.exports = {
parseResult,
getExpectedResult,
checkResult,
assertResult
};
279 changes: 131 additions & 148 deletions lib/tests/assert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,14 @@ const {checkResult, parseResult, assertResult, getExpectedResult} = require('../

describe('parseResult', ()=>{

it('should parsedResult correctly when it starts with register', ()=>{
const resultString = `google provider, version 'v23.01.00116' successfully installed \n
[{"name":"stackql-demo-001","status":"TERMINATED"}]`

const expected = [{"name":"stackql-demo-001","status":"TERMINATED"}]

const actual = parseResult(resultString);
expect(actual).toEqual(expected);
})

it('should parsedResult correctly when it is only the object', ()=>{
it('should parsedResult correctly', ()=>{
const resultString = `[{"name":"stackql-demo-001","status":"TERMINATED"}]`

const expected = [{"name":"stackql-demo-001","status":"TERMINATED"}]

const actual = parseResult(resultString);
expect(actual).toEqual(expected);
})
})

describe('checkResult', ()=>{
let expectedResult;
let actualResult;
beforeEach(()=>{
expectedResult= [{"name":"stackql-demo-001","status":"TERMINATED"}]
actualResult= [{"name":"stackql-demo-001","status":"TERMINATED"}]

})
it('should return equality false when the result object does not match', ()=>{
actualResult = [{"name":"stackql-demo-001","status":"RUNNING"}]

const {equality} = checkResult(expectedResult, undefined, actualResult);

expect(equality).toEqual(false);
})

it('should return equality true when the result object does match', ()=>{

const {equality} = checkResult(expectedResult, undefined, actualResult);

expect(equality).toEqual(true);
})

it('should return equality false when expected row is not matching', ()=>{

const expectedRows = 2;

const {equality} = checkResult(undefined, expectedRows, actualResult);

expect(equality).toEqual(false);
})

it('should return equality true when expected row is matching', ()=>{
const expectedRows = 1;

const {equality} = checkResult(undefined, expectedRows, actualResult);

expect(equality).toEqual(true);
})

it('should return equality false when expected row is matching, but result object does not match', ()=>{
const expectedRows = 1;
actualResult = [{"name":"stackql-demo-001","status":"RUNNING"}]

const {equality} = checkResult(expectedResult, expectedRows, actualResult);

expect(equality).toEqual(false);
})

it('should return equality true when expected row is matching, but result object matches', ()=>{
const expectedRows = 1;
actualResult = [{"name":"stackql-demo-001","status":"TERMINATED"}]

const {equality} = checkResult(expectedResult, expectedRows, actualResult);

expect(equality).toEqual(true);
})
})

describe('getExpectedResult', ()=>{
it('should return expectedResult when expectedResultStr is passed', ()=>{
const expectedResultStr = `[{"name":"stackql-demo-001","status":"TERMINATED"}]`
Expand All @@ -102,106 +31,160 @@ describe('getExpectedResult', ()=>{

})

describe('assertResult integration test', ()=>{
let coreObj;

const ACTION_ENV = {
RESULT: `google provider, version 'v23.01.00116' successfully installed \n
[{"name":"stackql-demo-001","status":"TERMINATED"}]`,
EXPECTED_RESULTS_STR: `[{"name":"stackql-demo-001","status":"TERMINATED"}]`,
EXPECTED_RESULTS_FILE_PATH: 'test.json',
EXPECTED_ROWS: 1
describe('checkResult', () => {
const core = {
info: jest.fn(),
error: jest.fn(),
setFailed: jest.fn()
};

beforeEach(() => {
core.info.mockClear();
core.error.mockClear();
core.setFailed.mockClear();
});

it('should return false and log an error when the actual length does not match expected rows', () => {
const expectedRows = "3";
const actualResult = [{}, {}, {}, {}]; // 4 items, should not match expectedRows

const result = checkResult(core, undefined, actualResult, expectedRows);

expect(result).toBe(false);
expect(core.error).toHaveBeenCalledWith(`Expected rows: ${expectedRows}, got: ${actualResult.length}`);
});

it('should return true when the actual length matches expected rows', () => {
const expectedRows = "2";
const actualResult = [{}, {}]; // 2 items, matches expectedRows

const result = checkResult(core, undefined, actualResult, expectedRows);

expect(result).toBe(true);
expect(core.error).not.toHaveBeenCalled();
});

it('should return false and log an error when expected does not match actual and expectedRows is undefined', () => {
const expected = [{ name: "test1" }, { name: "test2" }];
const actual = [{ name: "test1" }, { name: "test3" }];

const result = checkResult(core, expected, actual, undefined);

expect(result).toBe(false);
expect(core.error).toHaveBeenCalledWith(`Expected results do not match actual results.\nExpected: ${JSON.stringify(expected)}\nActual: ${JSON.stringify(actual)}`);
});

it('should return true when expected matches actual and expectedRows is undefined', () => {
const expected = [{ name: "test1" }, { name: "test2" }];
const actual = [{ name: "test1" }, { name: "test2" }];

const result = checkResult(core, expected, actual, undefined);

expect(result).toBe(true);
expect(core.error).not.toHaveBeenCalled();
});
});

describe('assertResult', ()=>{
let coreObj;

const ACTION_ENV = {
RESULT: `[{"name":"stackql-demo-001","status":"TERMINATED"}]`,
EXPECTED_RESULTS_STR: `[{"name":"stackql-demo-001","status":"TERMINATED"}]`,
EXPECTED_RESULTS_FILE_PATH: 'test.json',
EXPECTED_ROWS: 1
}

beforeEach(() => {
jest.resetModules()
process.env = {...ACTION_ENV}
coreObj = {
setFailed: jest.fn(),
info: jest.fn(),
error: jest.fn(),
}

beforeEach(() => {
jest.resetModules()
process.env = {...ACTION_ENV}
coreObj = {
setFailed: jest.fn(),
info: jest.fn().mockImplementation((message)=>{console.log(message)})
}
})
afterEach(() => {
process.env = ACTION_ENV
})
})
afterEach(() => {
process.env = ACTION_ENV
})


it('it should setFailed when there is expected results are undefined', () => {
process.env.EXPECTED_RESULTS_FILE_PATH = undefined
process.env.EXPECTED_RESULTS_STR = undefined
process.env.EXPECTED_ROWS = undefined
it('it should setFailed when there is expected results are undefined', () => {
process.env.EXPECTED_RESULTS_FILE_PATH = undefined
process.env.EXPECTED_RESULTS_STR = undefined
process.env.EXPECTED_ROWS = undefined

assertResult(coreObj)
assertResult(coreObj)

expect(coreObj.setFailed).toHaveBeenCalledWith('❌ Cannot find expected result, file path or expected rows')
});
expect(coreObj.setFailed).toHaveBeenCalledWith('Assertion error: ❌ Cannot find expected result, file path or expected rows')
});

it('it should setFailed when actual result is not equal to expected result', () => {
process.env.RESULT= "[{\"name\":\"stackql-demo-001\",\"status\":\"RUNNING\"}]"
it('it should setFailed when actual result is not equal to expected result', () => {
process.env.RESULT= "[{\"name\":\"stackql-demo-001\",\"status\":\"RUNNING\"}]"

assertResult(coreObj)
assertResult(coreObj)

expect(coreObj.setFailed).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Failed'))
});
expect(coreObj.setFailed).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Failed'))
});

it('it should not setFailed when actual result equal to expected result', () => {
assertResult(coreObj)
it('it should not setFailed when actual result equal to expected result', () => {
assertResult(coreObj)

expect(coreObj.setFailed).not.toHaveBeenCalled()
expect(coreObj.info).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Successful'))
});
expect(coreObj.setFailed).not.toHaveBeenCalled()
expect(coreObj.info).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Successful'))
});

it('it should setFailed when actual result is not equal to expected result', () => {
process.env.EXPECTED_RESULTS_STR= undefined;
process.env.EXPECTED_RESULTS_FILE_PATH = 'lib/tests/failed-result.json'
it('it should setFailed when actual result is not equal to expected result', () => {
process.env.EXPECTED_RESULTS_STR= undefined;
process.env.EXPECTED_RESULTS_FILE_PATH = 'lib/tests/failed-result.json'

assertResult(coreObj)
assertResult(coreObj)

expect(coreObj.setFailed).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Failed'))
});
expect(coreObj.setFailed).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Failed'))
});

it('it should not setFailed when actual result equal to expected result in file', () => {
process.env.EXPECTED_RESULTS_STR= undefined;
process.env.EXPECTED_RESULTS_FILE_PATH = 'lib/tests/success-result.json'
it('it should not setFailed when actual result equal to expected result in file', () => {
process.env.EXPECTED_RESULTS_STR= undefined;
process.env.EXPECTED_RESULTS_FILE_PATH = 'lib/tests/success-result.json'

assertResult(coreObj)
assertResult(coreObj)

expect(coreObj.setFailed).not.toHaveBeenCalled()
expect(coreObj.info).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Successful'))
})
expect(coreObj.setFailed).not.toHaveBeenCalled()
expect(coreObj.info).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Successful'))
})

it('it should setFailed when actual result does not match expected rows', () => {
process.env.EXPECTED_RESULTS_STR= undefined;
process.env.EXPECTED_RESULTS_FILE_PATH = undefined;
process.env.EXPECTED_ROWS = 2
it('it should setFailed when actual result does not match expected rows', () => {
process.env.EXPECTED_RESULTS_STR= undefined;
process.env.EXPECTED_RESULTS_FILE_PATH = undefined;
process.env.EXPECTED_ROWS = 2

assertResult(coreObj)
assertResult(coreObj)

expect(coreObj.setFailed).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Failed'))
});
expect(coreObj.setFailed).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Failed'))
});

it('it should not setFailed when actual result match expected rows', ()=>{
process.env.EXPECTED_RESULTS_STR= undefined;
process.env.EXPECTED_RESULTS_FILE_PATH = undefined;
process.env.EXPECTED_ROWS = 1
it('it should not setFailed when actual result match expected rows', ()=>{
process.env.EXPECTED_RESULTS_STR= undefined;
process.env.EXPECTED_RESULTS_FILE_PATH = undefined;
process.env.EXPECTED_ROWS = 1

assertResult(coreObj)
assertResult(coreObj)

expect(coreObj.setFailed).not.toHaveBeenCalled()
expect(coreObj.info).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Successful'))
})
expect(coreObj.setFailed).not.toHaveBeenCalled()
expect(coreObj.info).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Successful'))
})

it('it should not setFailed when actual result match expected rows in string number', ()=>{
process.env.EXPECTED_RESULTS_STR= undefined;
process.env.EXPECTED_RESULTS_FILE_PATH = undefined;
process.env.EXPECTED_ROWS = '1'
it('it should not setFailed when actual result match expected rows in string number', ()=>{
process.env.EXPECTED_RESULTS_STR= undefined;
process.env.EXPECTED_RESULTS_FILE_PATH = undefined;
process.env.EXPECTED_ROWS = '1'

assertResult(coreObj)
assertResult(coreObj)

expect(coreObj.setFailed).not.toHaveBeenCalled()
expect(coreObj.info).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Successful'))
})
expect(coreObj.setFailed).not.toHaveBeenCalled()
expect(coreObj.info).toHaveBeenCalledWith(expect.stringContaining('StackQL Assert Successful'))
})



})
})
1 change: 0 additions & 1 deletion lib/tests/test-auth.json

This file was deleted.

Loading
Loading