From 45cbb91d84e1fb9f16007ef2afb468c32daf9b51 Mon Sep 17 00:00:00 2001 From: Paul Arthur Date: Tue, 25 Jun 2024 18:38:15 +0000 Subject: [PATCH] CI: add basic framework --- .github/workflows/build.yml | 67 ++++++++++++++++++++++++++++++++ Makefile.am | 2 +- configure.ac | 26 +------------ test/.gitignore | 1 + test/Makefile.am | 4 ++ test/conftest.py | 62 +++++++++++++++++++++++++++++ test/files/milter.conf | 3 ++ test/files/test_config_fail.conf | 2 + test/pytest.ini | 3 ++ test/test_config.py | 13 +++++++ 10 files changed, 157 insertions(+), 26 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 test/.gitignore create mode 100644 test/Makefile.am create mode 100644 test/conftest.py create mode 100644 test/files/milter.conf create mode 100644 test/files/test_config_fail.conf create mode 100644 test/pytest.ini create mode 100644 test/test_config.py diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..4afbd6d --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,67 @@ +name: build +on: + push: + pull_request: + +jobs: + flake8: + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v4 + + - name: Install flake8 + run: sudo pip install flake8 + + - name: Lint Python code + run: flake8 --max-line-length=160 + + check: + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v4 + + - name: apt update + run: sudo apt update + + - name: Install dependencies + run: sudo apt install libbsd-dev liblua5.4-dev libmilter-dev libssl-dev + + - name: Install Python dependencies + run: sudo pip install pytest + + - name: Build OpenARC + run: | + autoreconf -fiv + CFLAGS='-Wall' ./configure + make -j4 + + - name: Check out miltertest + uses: actions/checkout@v4 + with: + repository: flowerysong/miltertest + path: miltertest + + - name: Build miltertest + run: | + autoreconf -fiv + ./configure + make + working-directory: ${{ github.workspace }}/miltertest + + - name: Test OpenARC + run: | + make check + + - name: Build OpenARC with clang + run: | + make distclean + CC=clang ./configure + make -j4 + + - name: Build OpenARC without milter + run: | + make distclean + CC=clang ./configure --disable-filter + make -j4 diff --git a/Makefile.am b/Makefile.am index a65d3d1..0c02b22 100644 --- a/Makefile.am +++ b/Makefile.am @@ -4,7 +4,7 @@ AUTOMAKE_OPTIONS = foreign ACLOCAL_AMFLAGS = -I m4 -SUBDIRS = libopenarc contrib docs openarc +SUBDIRS = libopenarc contrib docs openarc test dist_doc_DATA = LICENSE LICENSE.Sendmail RELEASE_NOTES dist_noinst_SCRIPTS = libtool diff --git a/configure.ac b/configure.ac index 8f9523b..1133599 100644 --- a/configure.ac +++ b/configure.ac @@ -888,31 +888,6 @@ AC_SUBST(LIBOPENARC_INC) AC_DEFINE_UNQUOTED([LIBOPENARC_FEATURE_STRING], "$LIBOPENARC_FEATURE_STRING", [Feature string for printing]) -# -# setup for testing -# - -AC_ARG_ENABLE([live-testing], - AS_HELP_STRING([--disable-live-testing], - [disable tests that require Internet access]), - [live_tests="$enable_live_testing"], [live_tests="yes"]) -AM_CONDITIONAL(LIVE_TESTS, test x"$live_tests" = x"yes") - -# -# specify test socket -# - -AC_ARG_WITH([test-socket], - AS_HELP_STRING([--with-test-socket], - [specify socket to use for all tests]), - [testsocket="$withval"], [testsocket=""]) -AM_CONDITIONAL(TEST_SOCKET, test x"$testsocket" != x"") -if test x"$testsocket" != x"" -then - TESTSOCKET=$testsocket - AC_SUBST(TESTSOCKET) -fi - # # Platform Specific Configuration # @@ -983,5 +958,6 @@ AC_CONFIG_FILES([ openarc/openarc.8 openarc/openarc.conf.5 openarc/openarc.conf.simple + test/Makefile ]) AC_OUTPUT diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/test/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/test/Makefile.am b/test/Makefile.am new file mode 100644 index 0000000..6c3baa6 --- /dev/null +++ b/test/Makefile.am @@ -0,0 +1,4 @@ +EXTRA_DIST = files pytest.ini *.py + +check: + pytest -vv diff --git a/test/conftest.py b/test/conftest.py new file mode 100644 index 0000000..d167dd9 --- /dev/null +++ b/test/conftest.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 + +import os +import subprocess + +import pytest + + +@pytest.fixture() +def private_key(scope='session'): + filepath = os.path.dirname(os.path.realpath(__file__)) + filepath = os.path.join(filepath, 'files', 'private.key') + binargs = [ + 'openssl', + 'genrsa', + '-out', filepath, + '2048', + ] + subprocess.run(binargs) + + +@pytest.fixture() +def tool_path(scope='session'): + def _tool_path(tool): + binpath = os.path.dirname(os.path.realpath(__file__)) + binpath = os.path.join(binpath, '..', tool) + return os.path.realpath(binpath) + return _tool_path + + +@pytest.fixture() +def milter_config(request, private_key): + base_path = os.path.join(request.fspath.dirname, 'files') + for candidate in [ + request.fspath.basename, # test file + request.function.__name__, # test function + ]: + fname = os.path.join(base_path, '.'.join([candidate, 'conf'])) + if os.path.isfile(fname): + return fname + + return os.path.join(base_path, 'milter.conf') + + +@pytest.fixture() +def milter_cmdline(tmp_path, tool_path, milter_config): + return [ + tool_path('openarc/openarc'), + '-f', + '-v', + '-c', milter_config, + '-p', tmp_path.joinpath('milter.sock'), + ] + + +@pytest.fixture() +def milter(milter_cmdline): + milter_proc = subprocess.Popen(milter_cmdline) + + yield milter_proc + + milter_proc.terminate() diff --git a/test/files/milter.conf b/test/files/milter.conf new file mode 100644 index 0000000..3a0d2dd --- /dev/null +++ b/test/files/milter.conf @@ -0,0 +1,3 @@ +Domain example.com +KeyFile private.key +Selector elpmaxe diff --git a/test/files/test_config_fail.conf b/test/files/test_config_fail.conf new file mode 100644 index 0000000..12d9cf5 --- /dev/null +++ b/test/files/test_config_fail.conf @@ -0,0 +1,2 @@ +Domain example.com +Selector elpmaxe diff --git a/test/pytest.ini b/test/pytest.ini new file mode 100644 index 0000000..f75b4c1 --- /dev/null +++ b/test/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +console_output_style = count +log_file = pytest.log diff --git a/test/test_config.py b/test/test_config.py new file mode 100644 index 0000000..3eac8af --- /dev/null +++ b/test/test_config.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +import subprocess + + +def test_config(milter): + pass + + +def test_config_fail(milter_cmdline): + res = subprocess.run(milter_cmdline, capture_output=True, text=True, timeout=4) + assert res.returncode != 0 + assert 'parameter "KeyFile" required when signing' in res.stderr