Skip to content

Commit ee425f6

Browse files
Merge pull request #16 from stackql/feat/windows-support
update the exec and added os matrix in assert test
2 parents cbd7f27 + b0c3661 commit ee425f6

File tree

4 files changed

+68
-57
lines changed

4 files changed

+68
-57
lines changed

.github/workflows/stackql-assert.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,29 @@ on:
77
pull_request:
88
jobs:
99
stackql-exec-google-example:
10-
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
os: [ubuntu-latest, windows-latest, macos-latest]
13+
runs-on: ${{matrix.os}}
1114
name: 'Assert test '
1215

1316
steps:
1417
- name: Checkout
1518
uses: actions/checkout@v3
1619

1720

21+
- name: Prep Google Creds (Windows)
22+
if: ${{ matrix.os == 'windows-latest'}}
23+
run: | ## use the secret to create json file
24+
$GoogleCreds = [System.Environment]::GetEnvironmentVariable("GOOGLE_CREDS_ENV")
25+
$GoogleCredsDecoded = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($GoogleCreds))
26+
Write-Output $GoogleCredsDecoded | Set-Content sa-key.json
27+
shell: pwsh
28+
env:
29+
GOOGLE_CREDS_ENV: ${{ secrets.GOOGLE_CREDS }}
30+
1831
- name: Prep Google Creds (bash)
32+
if: ${{ matrix.os != 'windows-latest' }}
1933
shell: bash
2034
run: | ## use the base64 encoded secret to create json file
2135
sudo echo ${{ secrets.GOOGLE_CREDS }} | base64 -d > sa-key.json

action.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: 'StackQL Studios - stackql-assert'
1+
name: 'StackQL Studios - StackQL Assert'
22
description: 'run StackQL query to test and audit your infrastructure.'
33
author: 'Yuncheng Yang, StackQL Studios'
44
inputs:
@@ -55,18 +55,23 @@ runs:
5555
AUTH_FILE_PATH: ${{ inputs.auth_obj_path }}
5656
AUTH_STR: ${{inputs.auth_str}}
5757

58-
- name: Execute query
59-
id: exec-query
58+
- name: get stackql command
6059
uses: actions/github-script@v6
6160
with:
6261
script: |
63-
const {executeStackql} = require('./lib/utils.js')
64-
await executeStackql(core, exec)
62+
const {getStackqlCommand} = require('./lib/utils.js')
63+
getStackqlCommand(core)
6564
env:
6665
QUERY_FILE_PATH: ${{ inputs.test_query_file_path }}
6766
QUERY: ${{inputs.test_query}}
6867
OUTPUT: 'json'
6968

69+
- name: execute stackql command
70+
id: exec-query
71+
shell: bash
72+
run: |
73+
${{ env.STACKQL_COMMAND }}
74+
7075
- name: Check results
7176
uses: actions/github-script@v6
7277
with:

lib/tests/utils.test.js

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const { setupAuth, executeStackql } = require("../utils");
1+
const { setupAuth, getStackqlCommand, setOutput } = require("../utils");
22

33
describe("util", () => {
44
let core;
5-
let exec;
6-
const expectedAuth = '{ "google": { "type": "service_account", "credentialsfilepath": "sa-key.json" }}'
5+
const expectedAuth =
6+
'{ "google": { "type": "service_account", "credentialsfilepath": "sa-key.json" }}';
77

88
beforeEach(() => {
99
core = {
@@ -18,10 +18,6 @@ describe("util", () => {
1818
console.error(message);
1919
}),
2020
};
21-
22-
exec = {
23-
exec: jest.fn().mockResolvedValue(true),
24-
};
2521
});
2622

2723
describe("setupAuth", () => {
@@ -30,7 +26,6 @@ describe("util", () => {
3026
AUTH_FILE_PATH: "./lib/tests/test-auth.json",
3127
};
3228

33-
3429
beforeEach(() => {
3530
jest.resetModules();
3631
process.env = { ...AUTH_ENV };
@@ -43,41 +38,41 @@ describe("util", () => {
4338
it("should throw error when neither AUTH_STR or AUTH_FILE_PATH is set", () => {
4439
process.env.AUTH_STR = undefined;
4540
process.env.AUTH_FILE_PATH = undefined;
46-
47-
setupAuth(core)
41+
42+
setupAuth(core);
4843
expect(core.setFailed).toBeCalledWith(
4944
"Either AUTH_FILE_PATH or AUTH_STR must be set."
5045
);
5146
});
5247

5348
it("should set AUTH environment variable when AUTH_STR is set", () => {
54-
process.env.AUTH_FILE_PATH = undefined;
49+
process.env.AUTH_FILE_PATH = undefined;
50+
51+
setupAuth(core);
5552

56-
setupAuth(core);
57-
58-
expect(core.exportVariable).toBeCalledWith("AUTH", expectedAuth);
53+
expect(core.exportVariable).toBeCalledWith("AUTH", expectedAuth);
5954
});
6055

6156
it("should set AUTH environment variable when AUTH_FILE_PATH is set", () => {
62-
process.env.AUTH_STR = undefined;
63-
64-
setupAuth(core);
65-
66-
expect(core.exportVariable).toBeCalledWith("AUTH", expectedAuth);
57+
process.env.AUTH_STR = undefined;
58+
59+
setupAuth(core);
60+
61+
expect(core.exportVariable).toBeCalledWith("AUTH", expectedAuth);
6762
});
6863

6964
it("should throw error when AUTH_FILE_PATH is set but file does not exist", () => {
7065
process.env.AUTH_STR = undefined;
7166
process.env.AUTH_FILE_PATH = "./failed-test-auth.json";
72-
73-
setupAuth(core)
67+
68+
setupAuth(core);
7469
expect(core.setFailed).toBeCalledWith(
7570
`Cannot find auth file ${process.env.AUTH_FILE_PATH}`
7671
);
7772
});
7873
});
7974

80-
describe("executeStackql", () => {
75+
describe("getStackqlCommand", () => {
8176
const EXECUTE_ENV = {
8277
QUERY: "test",
8378
QUERY_FILE_PATH: "test-query.json",
@@ -91,56 +86,52 @@ describe("util", () => {
9186

9287
afterEach(() => {
9388
process.env = EXECUTE_ENV;
89+
jest.clearAllMocks();
9490
});
9591

9692
it("should return error when there is neither query or query file path", async () => {
9793
process.env = { ...EXECUTE_ENV };
9894
process.env.QUERY = undefined;
9995
process.env.QUERY_FILE_PATH = undefined;
100-
101-
await executeStackql(core, exec);
96+
97+
getStackqlCommand(core);
10298

10399
expect(core.setFailed).toBeCalledWith(
104-
"Either test_query or test_query_file_path need to be set"
100+
"Either query or query_file_path need to be set"
105101
);
106102
});
107103

108-
it("should return error when there is no AUTH", async() => {
104+
it("should return error when there is no AUTH", async () => {
109105
process.env = { ...EXECUTE_ENV };
110106
process.env.AUTH = undefined;
111107

112-
await executeStackql(core, exec);
108+
getStackqlCommand(core);
113109

114-
expect(core.setFailed).toHaveBeenCalledWith("Cannot find AUTH environment variable when executing stackql");
110+
expect(core.setFailed).toHaveBeenCalledWith(
111+
"Cannot find AUTH environment variable when executing stackql"
112+
);
115113
});
116114

117115
it("should execute stackql with query file path", async () => {
118116
process.env = { ...EXECUTE_ENV };
119117
process.env.QUERY = undefined;
120118

121-
await executeStackql(core, exec);
119+
getStackqlCommand(core);
122120

123-
expect(exec.exec).toHaveBeenCalledWith("stackql", [
124-
"exec",
125-
"-i",
126-
"test-query.json",
127-
"--auth=test-auth",
128-
"--output=json",
129-
]);
121+
expect(core.exportVariable).toBeCalledWith(
122+
"STACKQL_COMMAND", "stackql exec -i test-query.json --auth='test-auth' --output='json'"
123+
);
130124
});
131125

132126
it("should execute stackql with query", async () => {
133127
process.env = { ...EXECUTE_ENV };
134128
process.env.QUERY_FILE_PATH = undefined;
135129

136-
await executeStackql(core, exec);
130+
getStackqlCommand(core);
137131

138-
expect(exec.exec).toHaveBeenCalledWith("stackql", [
139-
"exec",
140-
"test",
141-
"--auth=test-auth",
142-
"--output=json",
143-
]);
132+
expect(core.exportVariable).toBeCalledWith(
133+
"STACKQL_COMMAND", "stackql exec \"test\" --auth='test-auth' --output='json'"
134+
);
144135
});
145136
});
146137
});

lib/utils.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
function setupAuth(core) {
23
const fs = require("fs");
34
let auth;
@@ -27,7 +28,8 @@ function setupAuth(core) {
2728
core.exportVariable("AUTH", auth);
2829
}
2930

30-
async function executeStackql(core, exec) {
31+
async function getStackqlCommand(core) {
32+
3133
if (!checkEnvVarValid(process.env.AUTH)) {
3234
core.setFailed("Cannot find AUTH environment variable when executing stackql");
3335
return;
@@ -42,7 +44,7 @@ async function executeStackql(core, exec) {
4244

4345

4446
if (!checkEnvVarValid(query) && !checkEnvVarValid(queryFilePath)) {
45-
core.setFailed("Either test_query or test_query_file_path need to be set");
47+
core.setFailed("Either query or query_file_path need to be set");
4648
return;
4749
}
4850

@@ -52,16 +54,15 @@ async function executeStackql(core, exec) {
5254
"exec",
5355
"-i",
5456
queryFilePath,
55-
"--auth=" + auth,
56-
"--output=" + output,
57+
`--auth='${auth}'`,
58+
`--output='${output}'`
5759
];
5860
}
5961
if (query) {
60-
args = ["exec", query, "--auth=" + auth, "--output=" + output];
62+
args = ["exec", `"${query}"`, `--auth='${auth}'`, `--output='${output}'`];
6163
}
62-
6364
try {
64-
await exec.exec("stackql", args);
65+
core.exportVariable('STACKQL_COMMAND', `stackql ${args.join(" ")}`)
6566
} catch (error) {
6667
core.error(error);
6768
core.setFailed("Error when executing stackql");
@@ -81,5 +82,5 @@ const checkEnvVarValid = (variable) => {
8182

8283
module.exports = {
8384
setupAuth,
84-
executeStackql,
85+
getStackqlCommand,
8586
};

0 commit comments

Comments
 (0)