Skip to content

Commit

Permalink
Fix bindings and add CI tests
Browse files Browse the repository at this point in the history
Signed-off-by: Anand Krishnamoorthi <[email protected]>
  • Loading branch information
anakrish committed May 21, 2024
1 parent 495e91c commit 6806769
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 42 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/test-c-cpp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: test-c-cpp

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
setup:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup gcc, g++, cmake, ninja
run: sudo apt update && sudo apt install -y gcc g++ cmake ninja-build

- name: Test c binding
run: |
mkdir bindings/c/build
cd bindings/c/build
cmake -G Ninja ..
ninja
./regorus_test
- name: Test c-nostd binding
run: |
mkdir bindings/c-nostd/build
cd bindings/c-nostd/build
cmake -G Ninja ..
ninja
./regorus_test
- name: Test cpp binding
run: |
mkdir bindings/cpp/build
cd bindings/cpp/build
cmake -G Ninja ..
ninja
./regorus_test
49 changes: 49 additions & 0 deletions .github/workflows/test-python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: test-ruby

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
setup:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
name: test-ruby

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
setup:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Build wheels
uses: PyO3/maturin-action@v1
with:
args: --release --out dist --find-interpreter --manifest-path bindings/python/Cargo.toml
sccache: 'true'
manylinux: auto

- name: Test wheel
run: |
cd bindings/python
pip3 install ../../target/wheels/*.whl
python3 test.py
30 changes: 30 additions & 0 deletions .github/workflows/test-wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: test-wasm

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
setup:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 18

- name: Install wasmlpack
run: cargo install wasm-pack

- name: Test wasm binding
run: |
cd bindings/wasm
wasm-pack build --target nodejs --release
node test.js
6 changes: 3 additions & 3 deletions bindings/c-nostd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ corrosion_import_crate(
CRATE_TYPES staticlib FLAGS --crate-type=staticlib
)

add_executable(regorus_test_nostd main.c)
add_executable(regorus_test main.c)
# Add path to <regorus-source-folder>/bindings/ffi
target_include_directories(regorus_test_nostd PRIVATE "../ffi")
target_link_libraries(regorus_test_nostd regorus-ffi)
target_include_directories(regorus_test PRIVATE "../ffi")
target_link_libraries(regorus_test regorus-ffi)
3 changes: 3 additions & 0 deletions bindings/c-nostd/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,21 @@ int main() {
free(buffer);
if (r.status != RegorusStatusOk)
goto error;
printf("Loaded policy %s\n", r.output);
regorus_result_drop(r);

r = regorus_engine_add_policy(engine, "api.rego", (buffer = file_to_string("../../../tests/aci/api.rego")));
free(buffer);
if (r.status != RegorusStatusOk)
goto error;
printf("Loaded policy %s\n", r.output);
regorus_result_drop(r);

r = regorus_engine_add_policy(engine, "policy.rego", (buffer = file_to_string("../../../tests/aci/policy.rego")));
free(buffer);
if (r.status != RegorusStatusOk)
goto error;
printf("Loaded policy %s\n", r.output);
regorus_result_drop(r);

// Add data
Expand Down
3 changes: 3 additions & 0 deletions bindings/c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ int main() {
r = regorus_engine_add_policy_from_file(engine, "../../../tests/aci/framework.rego");
if (r.status != RegorusStatusOk)
goto error;
printf("Loaded policy %s\n", r.output);
regorus_result_drop(r);

r = regorus_engine_add_policy_from_file(engine, "../../../tests/aci/api.rego");
if (r.status != RegorusStatusOk)
goto error;
printf("Loaded policy %s\n", r.output);
regorus_result_drop(r);

r = regorus_engine_add_policy_from_file(engine, "../../../tests/aci/policy.rego");
if (r.status != RegorusStatusOk)
goto error;
printf("Loaded policy %s\n", r.output);
regorus_result_drop(r);

// Add data
Expand Down
10 changes: 8 additions & 2 deletions bindings/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ corrosion_import_crate(
MANIFEST_PATH "../ffi/Cargo.toml"
# Always build regorus in Release mode.
PROFILE "release"
# Only build the "regorusc" crate.
CRATES "regorus-ffi")
# Only build the "regorus-ffi" crate.
CRATES "regorus-ffi"

# Select specific features in regorus.
FEATURES "regorus/semver"

# Link statically
CRATE_TYPES "staticlib")

add_executable(regorus_test main.cpp)
# Add path to <regorus-source-folder>/bindings/ffi
Expand Down
1 change: 1 addition & 0 deletions bindings/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ int main() {
std::cerr<<result.error()<<std::endl;
return -1;
}
std::cout<<"Loaded policy "<<result.output()<< std::endl;
}
{
auto result = engine.add_data_from_json_file("../../../tests/aci/data.json");
Expand Down
26 changes: 22 additions & 4 deletions bindings/ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ fn to_regorus_result(r: Result<()>) -> RegorusResult {
}
}

fn to_regorus_string_result(r: Result<String>) -> RegorusResult {
match r {
Ok(s) => RegorusResult {
status: RegorusStatus::RegorusStatusOk,
output: to_c_str(s),
error_message: std::ptr::null_mut(),
},
Err(e) => RegorusResult {
status: RegorusStatus::RegorusStatusError,
output: std::ptr::null_mut(),
error_message: to_c_str(format!("{e}")),
},
}
}

/// Wrapper for `regorus::Engine`.
#[derive(Clone)]
pub struct RegorusEngine {
Expand All @@ -81,10 +96,13 @@ pub struct RegorusEngine {
/// `output` and `error_message` strings are not valid after drop.
#[no_mangle]
pub extern "C" fn regorus_result_drop(r: RegorusResult) {
if !r.error_message.is_null() {
unsafe {
unsafe {
if !r.error_message.is_null() {
let _ = CString::from_raw(r.error_message);
}
if !r.output.is_null() {
let _ = CString::from_raw(r.output);
}
}
}

Expand Down Expand Up @@ -133,7 +151,7 @@ pub extern "C" fn regorus_engine_add_policy(
path: *const c_char,
rego: *const c_char,
) -> RegorusResult {
to_regorus_result(|| -> Result<()> {
to_regorus_string_result(|| -> Result<String> {
to_ref(&engine)?
.engine
.add_policy(from_c_str(path)?, from_c_str(rego)?)
Expand All @@ -146,7 +164,7 @@ pub extern "C" fn regorus_engine_add_policy_from_file(
engine: *mut RegorusEngine,
path: *const c_char,
) -> RegorusResult {
to_regorus_result(|| -> Result<()> {
to_regorus_string_result(|| -> Result<String> {
to_ref(&engine)?
.engine
.add_policy_from_file(from_c_str(path)?)
Expand Down
Loading

0 comments on commit 6806769

Please sign in to comment.