Skip to content

use stackql-exec #23

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 13 commits into from
Apr 28, 2024
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 'StackQL Assert'
name: 'stackql-assert'

on:
push:
Expand All @@ -15,7 +15,16 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/[email protected]

#
# Pull required providers
#
- name: pull required providers
uses: stackql/[email protected]
with:
is_command: true
query: "REGISTRY PULL google; REGISTRY PULL github"

#
# Example `test_query` with `expected_rows`
Expand All @@ -24,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
18 changes: 0 additions & 18 deletions .github/workflows/test.yml

This file was deleted.

1 change: 0 additions & 1 deletion .github/workflows/workflow_scripts/github-example.iql
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
REGISTRY PULL github;
SELECT name FROM github.repos.repos WHERE org = '{{ .org }}' AND name = '{{ .repo }}';
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
[![StackQL Assert](https://github.com/stackql/stackql-assert/actions/workflows/stackql-assert-test.yml/badge.svg)](https://github.com/stackql/stackql-assert/actions/workflows/stackql-assert-test.yml)

# stackql-assert

The `stackql/stackql-assert` action is an composite action that runs a `stackql` query and checks if the result matches an expected result

# 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

## 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).

## Inputs
- `test_query` - stackql query to execute **(need to supply either `test_query` or `test_query_file_path`)**
- `test_query_file_path` - stackql query file to execute **(need to supply either `test_query` or `test_query_file_path`)**
- `data_file_path` - (optional) path to data file to pass to the stackql query preprocessor (`json` or `jsonnet`)
- `vars` - (optional) comma delimited list of variables to pass to the stackql query preprocessor (supported with `jsonnet` config blocks or `jsonnet` data files only), accepts `var1=val1,var2=val2`, can be used to source environment variables into stackql queries
- `expected_rows` - (optional) Expected number of rows in the result.
- `expected_results_str` - (optional) Expected result (`json`) from executing test query, support object string (overrides `expected_results_file_path`)
- `expected_results_file_path` - (optional) Results file (`json`) that stores expected result, json is support
- `auth_obj_path` - (optional) the path of json file that stores stackql AUTH string **(only required when using non-standard environment variable names)**
- `auth_str` - (optional) stackql AUTH string **(only required when using non-standard environment variable names)**
- **`test_query`** - stackql query to execute *(need to supply either `test_query` or `test_query_file_path`)*
- **`test_query_file_path`** - stackql query file to execute *(need to supply either `test_query` or `test_query_file_path`)*
- **`data_file_path`** - (optional) path to data file to pass to the stackql query preprocessor (`json` or `jsonnet`)
- **`vars`** - (optional) comma delimited list of variables to pass to the stackql query preprocessor (supported with `jsonnet` config blocks or `jsonnet` data files only), accepts `var1=val1,var2=val2`, can be used to source environment variables into stackql queries
- **`expected_rows`** - (optional) Expected number of rows in the result.
- **`expected_results_str`** - (optional) Expected result (`json`) from executing test query, support object string (overrides `expected_results_file_path`)
- **`expected_results_file_path`** - (optional) Results file (`json`) that stores expected result, json is support
- **`auth_obj_path`** - (optional) the path of json file that stores stackql AUTH string *(only required when using non-standard environment variable names)*
- **`auth_str`** - (optional) stackql AUTH string *(only required when using non-standard environment variable names)*

**__NOTE:__ one of `expected_rows`, `expected_results_str` or `expected_results_file_path` is required**

Expand All @@ -28,7 +32,7 @@ Authentication to StackQL providers is done via environment variables source fro
## Expected Result
- Use `expected_results_str` or `expected_results_file_path` or `expected_rows` to pass the expected result to the action. The expected result (`expected_results_str` or `expected_results_file_path`) should be a valid `json` object. The action will compare the result with the expected result. If the result is not the same as the expected result, the action will fail the step.
- Either `expected_results_str` or `expected_results_file_path` or `expected_rows` are required. If `expected_results_str` and `expected_results_file_path` are provided, `expected_results_str` will be used.
- Expected result example can be found in [example workflow](./.github/workflows/stackql-assert.yml) and [example .json file](./.github/workflows/workflow_scripts)
- Expected result example can be found in [example workflow](./.github/workflows/stackql-assert-test.yml) and [example .json file](./.github/workflows/workflow_scripts)

## Examples
The following excerpts from a GitHub Actions workflow demonstrate how to use the `stackql/stackql-assert` action.
Expand All @@ -40,7 +44,6 @@ The following excerpts from a GitHub Actions workflow demonstrate how to use the
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
113 changes: 31 additions & 82 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,120 +1,69 @@
name: 'StackQL Studios - StackQL Assert'
description: 'run StackQL query to test and audit your infrastructure.'
description: 'Run StackQL query to test and audit your infrastructure.'
author: 'Yuncheng Yang, StackQL Studios'
inputs:
test_query:
description: stackql query to execute (need to supply either test_query or test_query_file_path)
description: 'StackQL query to execute (supply either test_query or test_query_file_path)'
required: false
test_query_file_path:
description: stackql query file to execute (need to supply either test_query or test_query_file_path)
description: 'StackQL query file to execute (supply either test_query or test_query_file_path)'
required: false
data_file_path:
description: path to data file to pass to the stackql query preprocessor (json or jsonnet file)
description: 'Path to data file to pass to the StackQL query preprocessor (JSON or Jsonnet file)'
required: false
vars:
description: comma delimited list of variables to pass to the stackql query preprocessor (supported with jsonnet config blocks or jsonnet data files only), accepts 'var1=val1,var2=val2', can be used to source environment variables into stackql queries
description: 'Comma delimited list of variables to pass to the StackQL query preprocessor (supported with Jsonnet config blocks or Jsonnet data files only)'
required: false
expected_rows:
description: expected number of rows from executing test query
expected_rows:
description: 'Expected number of rows from executing test query'
required: false
expected_results_str:
description: expected result (as a json string) from executing test query, overrides expected_results_file_path
description: 'Expected result (as a JSON string) from executing test query, overrides expected_results_file_path'
required: false
expected_results_file_path:
description: json file with the expected result
description: 'JSON file with the expected result'
required: false
auth_obj_path:
description: the path of json file that stores stackql AUTH string (only required when using non-standard environment variable names)
description: 'Path of JSON file that stores StackQL AUTH string (only required when using non-standard environment variable names)'
required: false
auth_str:
description: stackql AUTH string (only required when using non-standard environment variable names)
description: 'StackQL AUTH string (only required when using non-standard environment variable names)'
required: false

runs:
using: "composite"
steps:
- name: check if stackql is installed and set output
id: check-stackql
shell: bash
run: |
if command -v stackql &> /dev/null; then
echo "stackql_installed=true" >> $GITHUB_OUTPUT
else
echo "stackql_installed=false" >> $GITHUB_OUTPUT
fi

- name: setup stackql
uses: stackql/[email protected]
if: ${{steps.check-stackql.outputs.stackql_installed == 'false'}}
- name: Setup StackQL
uses: stackql/[email protected]
with:
use_wrapper: true

- name: setup auth
if: (inputs.auth_obj_path != '') || (inputs.auth_str != '')
id: setup-auth
uses: actions/github-script@v6
with:
script: |
const path = require('path');
const utilsPath = path.join(process.env.GITHUB_ACTION_PATH, 'lib', 'utils.js')
const {setupAuth} = require(utilsPath)
setupAuth(core)
env:
AUTH_FILE_PATH: ${{ inputs.auth_obj_path }}
AUTH_STR: ${{inputs.auth_str}}

- name: get stackql command
uses: actions/github-script@v6
with:
script: |
const path = require('path');
const utilsPath = path.join(process.env.GITHUB_ACTION_PATH, 'lib', 'utils.js')
const {getStackqlCommand} = require(utilsPath)
getStackqlCommand(core)
env:
QUERY_FILE_PATH: ${{ inputs.test_query_file_path }}
QUERY: ${{inputs.test_query}}
DATA_FILE_PATH: ${{inputs.data_file_path}}
VARS: ${{inputs.vars}}
OUTPUT: 'json'

- name: dryrun stackql command
id: dryrun-query
shell: bash
run: |
${{ env.STACKQL_DRYRUN_COMMAND }}

- name: show rendered stackql query
uses: actions/github-script@v6
- name: Execute StackQL Command
id: exec-query
uses: stackql/[email protected]
with:
script: |
const path = require('path');
const utilsPath = path.join(process.env.GITHUB_ACTION_PATH, 'lib', 'utils.js')
const {showStackQLQuery} = require(utilsPath)
showStackQLQuery(core)
env:
DRYRUN_RESULT: ${{steps.dryrun-query.outputs.stdout}}
query: ${{ inputs.test_query }}
query_file_path: ${{ inputs.test_query_file_path }}
data_file_path: ${{ inputs.data_file_path }}
vars: ${{ inputs.vars }}
auth_obj_path: ${{ inputs.auth_obj_path }}
auth_str: ${{ inputs.auth_str }}
dry_run: false

- name: execute stackql command
id: exec-query
shell: bash
run: |
${{ env.STACKQL_COMMAND }}

- name: Check results
uses: actions/github-script@v6
- name: Check Results
uses: actions/[email protected]
with:
script: |
const path = require('path');
const assertPath = path.join(process.env.GITHUB_ACTION_PATH, 'stackql-assert.js')
const assertPath = path.join(process.env.GITHUB_ACTION_PATH, 'lib', 'assert.js')
const {assertResult} = require(assertPath)
assertResult(core)
env:
RESULT: ${{steps.exec-query.outputs.stdout}}
RESULT: ${{ steps.exec-query.outputs.stackql-query-results }}
EXPECTED_RESULTS_STR: ${{ inputs.expected_results_str }}
EXPECTED_RESULTS_FILE_PATH: ${{inputs.expected_results_file_path}}
EXPECTED_ROWS: ${{inputs.expected_rows}}
EXPECTED_RESULTS_FILE_PATH: ${{ inputs.expected_results_file_path }}
EXPECTED_ROWS: ${{ inputs.expected_rows }}

branding:
icon: 'terminal'
color: 'green'
color: 'green'
Loading