Skip to content
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

ci: test examples #48

Merged
merged 7 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
99 changes: 99 additions & 0 deletions .github/workflows/bindings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# libpathrs: safe path resolution on Linux
# Copyright (C) 2019-2024 Aleksa Sarai <[email protected]>
# Copyright (C) 2019-2024 SUSE LLC
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
release:
types: [ published ]
schedule:
- cron: '0 0 * * *'

name: bindings-ci

jobs:
c:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# Build and install libpathrs.so.
- uses: dtolnay/rust-toolchain@stable
- name: build libpathrs
run: make release
- name: install libpathrs
run: sudo ./install.sh --libdir=/usr/lib
# Run smoke-tests.
- run: make -C examples/c smoke-test

go:
strategy:
fail-fast: false
matrix:
go-version: ["1.18.x", "1.21.x", "1.22.x"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# Build and install libpathrs.so.
- uses: dtolnay/rust-toolchain@stable
- name: build libpathrs
run: make release
- name: install libpathrs
run: sudo ./install.sh --libdir=/usr/lib
# Setup go.
- name: install go ${{ matrix.go-version }}
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
check-latest: true
# Run smoke-tests.
- run: make -C examples/go smoke-test

python:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.10", "3.12"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# Build and install libpathrs.so.
- uses: dtolnay/rust-toolchain@stable
- name: build libpathrs
run: make release
- name: install libpathrs
run: sudo ./install.sh --libdir=/usr/lib
# Set up python venv.
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: install pypa/build
run: >-
python3 -m pip install --user build
# Build and install our bindings.
- name: build python-libpathrs bindings
run: make -C contrib/bindings/python dist
- name: install python-libpathrs bindings
run: make -C contrib/bindings/python install
# Include the dist/ in our artefacts.
- name: upload python-libpathrs bindings dist/
uses: actions/upload-artifact@v4
with:
name: python-${{ matrix.python-version }}-libpathrs-dist
path: contrib/bindings/python/dist/
# Run smoke-tests.
- run: make -C examples/python smoke-test
30 changes: 17 additions & 13 deletions .github/workflows/ci.yml → .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ on:
schedule:
- cron: '0 0 * * *'

name: ci
name: rust-ci

jobs:
check:
Expand Down Expand Up @@ -58,18 +58,13 @@ jobs:
- uses: taiki-e/install-action@cargo-llvm-cov
- uses: taiki-e/install-action@nextest

- name: unit tests
run: cargo llvm-cov --no-report nextest
- name: doctests
run: cargo llvm-cov --no-report --doc

# Run the unit tests as root.
# NOTE: Ideally this would be configured in .cargo/config.toml so it
# would also work locally, but unfortunately it seems cargo doesn't
# support cfg(feature=...) for target runner configs.
# See <https://github.com/rust-lang/cargo/issues/14306>.
- name: unit tests (root)
run: CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER='sudo -E' cargo llvm-cov --no-report --features _test_as_root nextest
# Rust tests.
- name: rust doc tests
run: make CARGO_NIGHTLY=cargo test-rust-doctest
- name: rust unit tests
run: make CARGO_NIGHTLY=cargo test-rust-unpriv
- name: rust unit tests (root)
run: make CARGO_NIGHTLY=cargo test-rust-root

- name: calculate coverage
run: cargo llvm-cov report
Expand All @@ -81,6 +76,15 @@ jobs:
name: coverage-report
path: target/llvm-cov/html

examples:
name: smoke-test examples
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo build --examples
- run: make -C examples smoke-test-rust

fmt:
name: rustfmt
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ snafu = "^0.8"

[dev-dependencies]
anyhow = "^1"
clap = { version = "^4", features = ["cargo"] }
errno = "^0.3"
tempfile = "^3"
paste = "^1"
Expand Down
84 changes: 84 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# libpathrs: safe path resolution on Linux
# Copyright (C) 2019-2024 Aleksa Sarai <[email protected]>
# Copyright (C) 2019-2024 SUSE LLC
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.

CARGO ?= cargo
CARGO_NIGHTLY ?= cargo +nightly

SRC_FILES = $(shell find . -name '*.rs')

.PHONY: debug
debug: target/debug

target/debug: $(SRC_FILES)
$(CARGO) build

.PHONY: release
release: target/release

target/release: $(SRC_FILES)
$(CARGO) build --release

.PHONY: smoke-test
smoke-test:
make -C examples smoke-test

.PHONY: clean
clean:
-rm -rf target/

.PHONY: lint
lint: lint-rust

.PHONY: lint-rust
lint-rust:
$(CARGO_NIGHTLY) fmt --all -- --check
$(CARGO) clippy --all-features --all-targets
$(CARGO) check --all-features --all-targets

.PHONY: test-rust-doctest
test-rust-doctest:
$(CARGO_NIGHTLY) llvm-cov --no-report --branch --doc

.PHONY: test-rust-unpriv
test-rust-unpriv:
$(CARGO_NIGHTLY) llvm-cov --no-report --branch nextest

.PHONY: test-rust-root
test-rust-root:
# Run Rust tests as root.
# NOTE: Ideally this would be configured in .cargo/config.toml so it
# would also work locally, but unfortunately it seems cargo doesn't
# support cfg(feature=...) for target runner configs.
# See <https://github.com/rust-lang/cargo/issues/14306>.
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER='sudo -E' \
$(CARGO_NIGHTLY) llvm-cov --no-report --branch --features _test_as_root nextest

.PHONY: test-rust
test-rust:
-rm -rf target/llvm-cov*
make test-rust-{doctest,unpriv,root}

.PHONY: test
test: test-rust
$(CARGO_NIGHTLY) llvm-cov report
$(CARGO_NIGHTLY) llvm-cov report --open

.PHONY: install
install: release
@echo "If you want to configure the install paths, use ./install.sh directly."
@echo "[Sleeping for 3 seconds.]"
@sleep 3s
./install.sh
26 changes: 26 additions & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# libpathrs: safe path resolution on Linux
# Copyright (C) 2019-2024 Aleksa Sarai <[email protected]>
# Copyright (C) 2019-2024 SUSE LLC
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.

.PHONY: smoke-test
smoke-test:
make -C c $@
make -C go $@
make -C python $@
make smoke-test-rust

.PHONY: smoke-test-rust
smoke-test-rust:
make -C rust-cat smoke-test
1 change: 0 additions & 1 deletion examples/.gitignore → examples/c/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/cat
/cat_multithread
/__pycache__/
31 changes: 31 additions & 0 deletions examples/c/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# libpathrs: safe path resolution on Linux
# Copyright (C) 2019-2024 Aleksa Sarai <[email protected]>
# Copyright (C) 2019-2024 SUSE LLC
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.

CC ?= gcc

CFLAGS := $(shell pkg-config --cflags pathrs)
LDFLAGS := $(shell pkg-config --libs-only-L --libs-only-other pathrs)
LDLIBS := $(shell pkg-config --libs-only-l pathrs)

.PHONY: all
all: $(patsubst %.c,%,$(wildcard *.c))

cat_multithread: LDLIBS += -lpthread

.PHONY: smoke-test
smoke-test: cat cat_multithread
./cat . ../../cat.c >/dev/null
./cat_multithread . ../../cat_multithread.c >/dev/null
2 changes: 1 addition & 1 deletion examples/cat.c → examples/c/cat.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

/*
* File: examples/cat.c
* File: examples/c/cat.c
*
* An example program which opens a file inside a root and outputs its contents
* using libpathrs.
Expand Down
5 changes: 3 additions & 2 deletions examples/cat_multithread.c → examples/c/cat_multithread.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
*/

/*
* File: examples/cat_multithread.c
* File: examples/c/cat_multithread.c
*
* An example program which opens a file inside a root and outputs its contents
* using libpathrs.
* using libpathrs, but multithreaded to show that there are no obvious race
* conditions when using pathrs.
*/

#include <stdio.h>
Expand Down
26 changes: 26 additions & 0 deletions examples/go/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# libpathrs: safe path resolution on Linux
# Copyright (C) 2019-2024 Aleksa Sarai <[email protected]>
# Copyright (C) 2019-2024 SUSE LLC
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.

GO ?= go

GOMOD_FILES := go.mod go.sum

cat: cat.go $(GOMOD_FILES)
$(GO) build -o $@ $<

.PHONY: smoke-test
smoke-test: cat
./cat . ../../cat.go &>/dev/null
Loading
Loading