|
1 | 1 | # stackql-assert
|
2 | 2 |
|
3 |
| -The `stackql/stackql-assert` action is an composite action that runs stackql query and check if result matches expected result |
4 |
| - |
| 3 | +The `stackql/stackql-assert` action is an composite action that runs a `stackql` query and checks if the result matches an expected result |
5 | 4 |
|
6 | 5 | # Usage
|
7 | 6 |
|
8 |
| -## AUTH |
9 |
| -> **Note** |
10 |
| -> stackql-assert action uses same auth setup as [stackql-exec](https://github.com/stackql/stackql-exec/blob/main/README.md). |
11 |
| -> [Learn more](https://stackql.io/docs/getting-started/authenticating) about authentication setup when running stackql |
| 7 | +## Provider Authentication |
| 8 | +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). |
12 | 9 |
|
13 |
| -### Example auth string |
14 |
| -``` |
15 |
| -{ "google": { "type": "service_account", "credentialsfilepath": "sa-key.json" }, |
16 |
| - "github": { "type": "basic", "credentialsenvvar": "STACKQL_GITHUB_CREDS" }} |
17 |
| -``` |
18 |
| -It can be passed with `auth_str` as a string, or stored in a file and pass filename to `auth-obj-path` |
19 |
| -- For "basic" auth, you need to set a environment variable with same name as the value of `credentialsenvvar` in the auth string for the Github Action step. You can use [Github Secrets](https://docs.github.com/en/actions/reference/encrypted-secrets) to store the value of the environment variable, and use env to pass it to the action. For example: |
20 |
| -``` |
21 |
| -env: |
22 |
| - STACKQL_GITHUB_CREDS: ${{ secrets.STACKQL_GITHUB_CREDS }} |
23 |
| -``` |
24 |
| -- For "service_account" auth, you need to store the credentials into a file; You can follow the example of `Prep Google Creds (bash)` step in the [example workflow](./.github/workflows/stackql-assert.yml) |
| 10 | +## Inputs |
| 11 | +- `test_query` - stackql query to execute **(need to supply either `test_query` or `test_query_file_path`)** |
| 12 | +- `test_query_file_path` - stackql query file to execute **(need to supply either `test_query` or `test_query_file_path`)** |
| 13 | +- `data_file_path` - (optional) path to data file to pass to the stackql query preprocessor (`json` or `jsonnet`) |
| 14 | +- `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 |
| 15 | +- `expected_rows` - (optional) Expected number of rows in the result. |
| 16 | +- `expected_results_str` - (optional) Expected result (`json`) from executing test query, support object string (overrides `expected_results_file_path`) |
| 17 | +- `expected_results_file_path` - (optional) Results file (`json`) that stores expected result, json is support |
| 18 | +- `auth_obj_path` - (optional) the path of json file that stores stackql AUTH string **(only required when using non-standard environment variable names)** |
| 19 | +- `auth_str` - (optional) stackql AUTH string **(only required when using non-standard environment variable names)** |
| 20 | + |
| 21 | +**__NOTE:__ one of `expected_rows`, `expected_results_str` or `expected_results_file_path` is required** |
25 | 22 |
|
26 | 23 | ## Test Query
|
27 |
| -- Use `test_query` or `test_query_path` to pass the test query to the action. The test query should be a valid StackQL query. The action will run the query and check if the result is empty or not. If the result is empty, the action will fail the step. |
28 |
| -- Either `test_query` or `test_query_path` is required. If both are provided, `test_query` will be used. |
| 24 | +- Use `test_query` or `test_query_file_path` to pass the test query to the action. The test query should be a valid StackQL query. The action will run the query and check if the result is empty or not. If the result is empty, the action will fail the step. |
| 25 | +- Either `test_query` or `test_query_file_path` are required. If both are provided, `test_query` will be used. |
29 | 26 | - query example can be found in [example workflow](./.github/workflows/stackql-assert.yml) and [example .iql file](./.github/workflows/workflow_scripts)
|
30 | 27 |
|
31 | 28 | ## Expected Result
|
32 |
| -- Use `expected_results_str` or `expected_results_file_path` to pass the expected result to the action. The expected result 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. |
33 |
| -- Either `expected_results_str` or `expected_results_file_path` is required. If both are provided, `expected_results_str` will be used. |
| 29 | +- 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. |
| 30 | +- 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. |
34 | 31 | - Expected result example can be found in [example workflow](./.github/workflows/stackql-assert.yml) and [example .json file](./.github/workflows/workflow_scripts)
|
35 | 32 |
|
| 33 | +## Examples |
| 34 | +The following excerpts from a GitHub Actions workflow demonstrate how to use the `stackql/stackql-assert` action. |
| 35 | + |
| 36 | +## Example `test_query` with `expected_rows` |
| 37 | + |
| 38 | +```yaml |
| 39 | + - name: Use test query string and expected rows |
| 40 | + uses: ./ |
| 41 | + with: |
| 42 | + test_query: | |
| 43 | + REGISTRY PULL google; |
| 44 | + SELECT name |
| 45 | + FROM google.compute.instances |
| 46 | + WHERE project = 'stackql-demo' AND zone = 'australia-southeast1-a' AND name = 'stackql-demo-001'; |
| 47 | + expected_rows: 1 |
| 48 | + env: |
| 49 | + GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }} |
| 50 | +``` |
| 51 | +
|
| 52 | +## Example `test_query_file_path` with `expected_results_str` |
| 53 | + |
| 54 | +```yaml |
| 55 | + - name: Use test query file and expected result string |
| 56 | + uses: ./ |
| 57 | + with: |
| 58 | + test_query_file_path: './.github/workflows/workflow_scripts/google-example.iql' |
| 59 | + expected_results_str: '[{"name":"stackql-demo-001"}]' |
| 60 | + env: |
| 61 | + GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }} |
| 62 | +``` |
| 63 | + |
| 64 | +## Example `test_query_file_path` with `expected_results_file_path` supplying `vars` using an inline `jsonnet` config block |
| 65 | + |
| 66 | +```yaml |
| 67 | + - name: Use test query file and expected results file path using inline jsonnet config block and external vars |
| 68 | + uses: ./ |
| 69 | + with: |
| 70 | + test_query_file_path: './.github/workflows/workflow_scripts/google-example-inline-jsonnet.iql' |
| 71 | + expected_results_file_path: './.github/workflows/workflow_scripts/google-example-inline-jsonnet-results.json' |
| 72 | + vars: GOOGLE_PROJECT=${{ env.GOOGLE_PROJECT }},GOOGLE_ZONE=${{ env.GOOGLE_ZONE }} |
| 73 | + env: |
| 74 | + GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }} |
| 75 | + GOOGLE_PROJECT: ${{ vars.GOOGLE_PROJECT }} |
| 76 | + GOOGLE_ZONE: ${{ vars.GOOGLE_ZONE }} |
| 77 | +``` |
| 78 | + |
| 79 | +## Example `test_query_file_path` with `expected_rows` supplying `vars` using `jsonnet` config provided using `data_file_path` |
| 80 | + |
| 81 | +```yaml |
| 82 | + - name: Use test query string and expected results file, with auth object |
| 83 | + uses: ./ |
| 84 | + with: |
| 85 | + test_query_file_path: './.github/workflows/workflow_scripts/google-example.iql' |
| 86 | + data_file_path: './.github/workflows/workflow_scripts/google-example-data.jsonnet' |
| 87 | + vars: TEST_ORG=${{ env.TEST_ORG }},TEST_REPO=${{ env.TEST_REPO }} |
| 88 | + expected_rows: 1 |
| 89 | + env: |
| 90 | + STACKQL_GITHUB_USERNAME: ${{ secrets.STACKQL_GITHUB_USERNAME }} |
| 91 | + STACKQL_GITHUB_PASSWORD: ${{ secrets.STACKQL_GITHUB_PASSWORD }} |
| 92 | + TEST_ORG: ${{ vars.TEST_ORG }} |
| 93 | + TEST_REPO: ${{ vars.TEST_REPO }} |
| 94 | +``` |
| 95 | + |
| 96 | + |
36 | 97 |
|
37 |
| -## Input |
38 |
| -- `auth_obj_path` - Path to the auth object file. |
39 |
| -- `auth_str` - Auth string. |
40 |
| -- `test_query` - Test query to run (overrides test_query_path). |
41 |
| -- `test_query_file_path` - Path to the test query file. |
42 |
| -- `expected_rows` - Expected number of rows in the result. |
43 |
| -- `expected_results_str` - expected result from executing test query, support object string (overrides expected_results_file_path) |
44 |
| -- `expected_results_file_path` - file that stores expected result, json is support |
|
0 commit comments