Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
use stackql-exec
  • Loading branch information
jeffreyaven committed Apr 28, 2024
commit 6a8344efd69d3feaac81d6f245524c7b4ea02235
3 changes: 1 addition & 2 deletions .github/workflows/stackql-assert.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
#
# Pull required providers
#
- name: Use test query string and expected rows
- name: pull required providers
uses: stackql/[email protected]
with:
is_command: true
Expand All @@ -33,7 +33,6 @@ jobs:
uses: ./
with:
test_query: |
REGISTRY PULL google;
SELECT name
FROM google.compute.instances
WHERE project = 'stackql-demo' AND zone = 'australia-southeast1-a' AND name = 'stackql-demo-001';
Expand Down
140 changes: 48 additions & 92 deletions lib/tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,123 +1,79 @@
const { setupAuth, getStackqlCommand } = require("../utils");
const { assertResult, parseResult, getExpectedResult } = require('./assert');
const fs = require('fs');

describe("util", () => {
jest.mock('fs');

describe('assert.js functions', () => {
let core;
const expectedAuth = '{ "google": { "type": "service_account", "credentialsfilepath": "sa-key.json" }}';

beforeEach(() => {
core = {
setFailed: jest.fn(),
info: jest.fn(),
exportVariable: jest.fn(),
error: jest.fn(),
setFailed: jest.fn()
};
process.env.RESULT = JSON.stringify([{ id: 1, value: 'test' }]);
process.env.EXPECTED_ROWS = '1';
});

describe("setupAuth", () => {
let AUTH_ENV = {
AUTH_STR: expectedAuth,
AUTH_FILE_PATH: "./lib/tests/test-auth.json",
};

beforeEach(() => {
jest.resetModules();
process.env = { ...AUTH_ENV };
});
afterEach(() => {
jest.resetAllMocks();
jest.restoreAllMocks();
});

afterEach(() => {
process.env = AUTH_ENV;
describe('parseResult', () => {
it('should correctly parse valid JSON', () => {
const input = JSON.stringify({ key: 'value' });
expect(parseResult(input, 'valid JSON')).toEqual({ key: 'value' });
});

it("should not throw an error when neither AUTH_STR or AUTH_FILE_PATH is set", () => {
process.env.AUTH_STR = undefined;
process.env.AUTH_FILE_PATH = undefined;

setupAuth(core);

expect(core.setFailed).not.toBeCalled();
it('should throw an error on invalid JSON', () => {
const input = "invalid JSON";
expect(() => parseResult(input, 'invalid JSON')).toThrow('Failed to parse invalid JSON JSON');
});
});

it("should set AUTH environment variable when AUTH_STR is set", () => {
process.env.AUTH_FILE_PATH = undefined;

setupAuth(core);

expect(core.exportVariable).toBeCalledWith("AUTH", expectedAuth);
describe('getExpectedResult', () => {
it('should return parsed result from string', () => {
const input = JSON.stringify({ key: 'value' });
expect(getExpectedResult(input, null)).toEqual({ key: 'value' });
});

it("should set AUTH environment variable when AUTH_FILE_PATH is set", () => {
process.env.AUTH_STR = undefined;

setupAuth(core);

expect(core.exportVariable).toBeCalledWith("AUTH", expectedAuth);
it('should return parsed result from file', () => {
const input = JSON.stringify({ key: 'value' });
fs.readFileSync.mockReturnValue(input);
expect(getExpectedResult(null, 'path/to/file')).toEqual({ key: 'value' });
expect(fs.readFileSync).toHaveBeenCalledWith('path/to/file', 'utf-8');
});

it("should throw error when AUTH_FILE_PATH is set but file does not exist", () => {
process.env.AUTH_STR = undefined;
process.env.AUTH_FILE_PATH = "./failed-test-auth.json";

setupAuth(core);

expect(core.setFailed).toBeCalledWith(`Cannot find auth file ${process.env.AUTH_FILE_PATH}`);
it('should throw an error if no input is provided', () => {
expect(() => getExpectedResult(null, null)).toThrow('No expected result provided.');
});
});

describe("getStackqlCommand", () => {
const EXECUTE_ENV = {
QUERY: "test",
QUERY_FILE_PATH: "test-query.iql",
AUTH: "test-auth",
};

beforeEach(() => {
jest.resetModules();
process.env = { ...EXECUTE_ENV };
describe('assertResult', () => {
it('should log success if the expected rows and results match', () => {
process.env.EXPECTED_RESULTS_STR = process.env.RESULT;
assertResult(core);
expect(core.info).toHaveBeenCalledWith("✅ StackQL Assert Successful");
});

afterEach(() => {
process.env = EXECUTE_ENV;
jest.clearAllMocks();
it('should fail if expected rows do not match', () => {
process.env.EXPECTED_ROWS = '2'; // Actual result will have only one item
assertResult(core);
expect(core.setFailed).toHaveBeenCalledWith(expect.stringContaining("Expected rows: 2, got: 1"));
});

it("should return error when there is neither query or query file path", () => {
process.env.QUERY = undefined;
process.env.QUERY_FILE_PATH = undefined;

getStackqlCommand(core);

expect(core.setFailed).toBeCalledWith("Either test_query or test_query_file_path need to be set");
it('should fail if expected results do not match', () => {
process.env.EXPECTED_RESULTS_STR = JSON.stringify([{ id: 1, value: 'wrong' }]);
assertResult(core);
expect(core.setFailed).toHaveBeenCalledWith(expect.stringContaining("Expected results do not match actual results."));
});

// it("should not return error when there is no AUTH", () => {
// process.env.AUTH = undefined;

// getStackqlCommand(core);

// expect(core.setFailed).not.toBeCalled();
// expect(core.exportVariable).toBeCalledWith(
// "STACKQL_COMMAND", "stackql exec \"test\" --output='json'"
// );
// });

it("should execute stackql with query file path", () => {
process.env.QUERY = undefined;

getStackqlCommand(core);

expect(core.exportVariable).toBeCalledWith(
"STACKQL_COMMAND", "stackql exec -i test-query.iql --auth='test-auth' --output='json'"
);
});

it("should execute stackql with query", () => {
process.env.QUERY_FILE_PATH = undefined;

getStackqlCommand(core);

expect(core.exportVariable).toBeCalledWith(
"STACKQL_COMMAND", "stackql exec \"test\" --auth='test-auth' --output='json'"
);
it('should handle errors during processing', () => {
process.env.RESULT = 'invalid json';
assertResult(core);
expect(core.setFailed).toHaveBeenCalledWith(expect.stringContaining("Assertion error"));
});
});
});