Skip to content

Commit e6570a5

Browse files
Share test setup steps to manage divergence
1 parent 387597a commit e6570a5

File tree

5 files changed

+133
-80
lines changed

5 files changed

+133
-80
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: test-setup
2+
author: Matthias Valvekens
3+
description: Perform set-up for python-pkcs11 CI
4+
inputs:
5+
os:
6+
description: OS to target
7+
required: true
8+
python-version:
9+
description: Python version to target
10+
required: true
11+
dependency-group:
12+
description: UV dependency group to install
13+
required: true
14+
pkcs11-platform:
15+
description: PKCS#11 platform to target
16+
required: true
17+
token-label:
18+
description: Label assigned to the token
19+
required: true
20+
token-user-pin:
21+
description: User PIN to configure on the token
22+
required: true
23+
token-so-pin:
24+
description: Security officer PIN to configure on the token
25+
required: true
26+
outputs:
27+
module:
28+
description: Path to PKCS#11 module
29+
value: ${{ steps.install-result.outputs.module }}
30+
runs:
31+
using: "composite"
32+
steps:
33+
- name: Setup Python
34+
uses: actions/setup-python@v5
35+
with:
36+
python-version: ${{ inputs.python-version }}
37+
- uses: ./.github/actions/install-softhsm
38+
if: inputs.pkcs11-platform == 'softhsm'
39+
id: softhsm
40+
with:
41+
os: ${{ inputs.os }}
42+
token-label: ${{ inputs.token-label }}
43+
token-so-pin: ${{ inputs.token-so-pin }}
44+
token-user-pin: ${{ inputs.token-user-pin }}
45+
- uses: ./.github/actions/install-opencryptoki
46+
# only run opencryptoki tests on ubuntu
47+
# (macos and windows don't seem to be supported)
48+
if: inputs.pkcs11-platform == 'opencryptoki'
49+
id: opencryptoki
50+
with:
51+
os: ${{ inputs.os }}
52+
token-label: ${{ inputs.token-label }}
53+
token-so-pin: ${{ inputs.token-so-pin }}
54+
token-user-pin: ${{ inputs.token-user-pin }}
55+
- name: Set module path
56+
id: install-result
57+
shell: bash
58+
run: |
59+
if [[ "$PLATFORM" == 'opencryptoki' ]]; then
60+
echo "module=${{ steps.opencryptoki.outputs.module }}" >> "$GITHUB_OUTPUT"
61+
elif [[ "$PLATFORM" == 'softhsm' ]]; then
62+
echo "module=${{ steps.softhsm.outputs.module }}" >> "$GITHUB_OUTPUT"
63+
else
64+
echo "$PLATFORM is not a valid PKCS#11 platform choice"
65+
exit 1
66+
fi
67+
env:
68+
PLATFORM: ${{ inputs.pkcs11-platform }}
69+
- name: Install uv
70+
uses: astral-sh/setup-uv@v4
71+
with:
72+
enable-cache: true
73+
python-version: ${{ inputs.python-version }}
74+
- name: Install testing dependencies
75+
shell: bash
76+
run: uv sync --no-dev --exact --group "${{ inputs.dependency-group }}"

.github/workflows/coverage.yml

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ jobs:
1414
# seems to lead to segfaults in Python 3.13 -> TODO: investigate
1515
pytest-coverage:
1616
runs-on: ubuntu-latest
17+
strategy:
18+
matrix:
19+
pkcs11-platform:
20+
- softhsm
21+
- opencryptoki
1722
steps:
1823
- name: Acquire sources
1924
uses: actions/checkout@v4
@@ -23,50 +28,27 @@ jobs:
2328
# Doing it here is still better than introducing a non-declarative setup.py into the
2429
# build again.
2530
run: sed -i 's/#coverage#cython/#cython/g' pkcs11/*.pyx
26-
- name: Setup Python
27-
uses: actions/setup-python@v5
28-
with:
29-
python-version: 3.12
30-
- uses: ./.github/actions/install-softhsm
31-
id: softhsm
32-
with:
33-
os: ubuntu-latest
34-
token-label: ${{ env.PKCS11_TOKEN_LABEL }}
35-
token-so-pin: ${{ env.PKCS11_TOKEN_SO_PIN }}
36-
token-user-pin: ${{ env.PKCS11_TOKEN_PIN }}
37-
- uses: ./.github/actions/install-opencryptoki
38-
# only run opencryptoki tests on ubuntu
39-
# (macos and windows don't seem to be supported)
40-
id: opencryptoki
31+
- uses: ./.github/actions/test-setup
32+
id: setup
4133
with:
4234
os: ubuntu-latest
35+
python-version: "3.12"
36+
dependency-group: coverage
4337
token-label: ${{ env.PKCS11_TOKEN_LABEL }}
4438
token-so-pin: ${{ env.PKCS11_TOKEN_SO_PIN }}
4539
token-user-pin: ${{ env.PKCS11_TOKEN_PIN }}
46-
- name: Install uv
47-
uses: astral-sh/setup-uv@v4
48-
with:
49-
enable-cache: true
50-
python-version: 3.12
51-
- name: Install testing dependencies
52-
run: uv sync --no-dev --exact --group coverage
40+
pkcs11-platform: ${{ matrix.pkcs11-platform }}
5341
env:
5442
CFLAGS: "-DCYTHON_TRACE_NOGIL=1"
5543
EXT_BUILD_DEBUG: "1"
56-
- name: Run tests with SoftHSM
57-
run: uv run pytest -v --cov=pkcs11 --cov-branch --cov-report=xml:python-softhsm-coverage.xml
58-
env:
59-
PKCS11_MODULE: ${{ steps.softhsm.outputs.module }}
60-
- name: Run tests with opencryptoki
61-
run: uv run pytest -v --cov=pkcs11 --cov-branch --cov-report=xml:python-opencryptoki-coverage.xml
44+
- name: Run tests
45+
run: uv run pytest -v --cov=pkcs11 --cov-branch --cov-report=xml:${{ matrix.pkcs11-platform }}-coverage.xml
6246
env:
63-
PKCS11_MODULE: ${{ steps.opencryptoki.outputs.module }}
64-
# For testing logic around swapping PKCS#11 libs
65-
PKCS11_MODULE2: ${{ steps.softhsm.outputs.module }}
47+
PKCS11_MODULE: ${{ steps.setup.outputs.module }}
6648
- name: Stash coverage report
6749
uses: actions/upload-artifact@v4
6850
with:
69-
name: coverage
51+
name: coverage-${{ strategy.job-index }}
7052
path: "*-coverage.xml"
7153
codecov-upload:
7254
permissions:
@@ -80,7 +62,7 @@ jobs:
8062
- name: Retrieve coverage reports
8163
uses: actions/download-artifact@v4
8264
with:
83-
name: coverage
65+
pattern: coverage-*
8466
path: ./reports/
8567
- name: Upload all coverage reports to Codecov
8668
uses: codecov/codecov-action@v5

.github/workflows/tests.yml

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,46 +27,30 @@ jobs:
2727
- "3.11"
2828
- "3.12"
2929
- "3.13"
30+
pkcs11-platform:
31+
- softhsm
32+
- opencryptoki
33+
exclude:
34+
# only run opencryptoki tests on ubuntu
35+
# (macos and windows don't seem to be supported)
36+
- pkcs11-platform: opencryptoki
37+
os: windows-latest
38+
- pkcs11-platform: opencryptoki
39+
os: macos-latest
3040
steps:
3141
- name: Acquire sources
3242
uses: actions/checkout@v4
33-
34-
- name: Setup Python
35-
uses: actions/setup-python@v5
36-
with:
37-
python-version: ${{ matrix.python-version }}
38-
- uses: ./.github/actions/install-softhsm
39-
id: softhsm
40-
with:
41-
os: ${{ matrix.os }}
42-
token-label: ${{ env.PKCS11_TOKEN_LABEL }}
43-
token-so-pin: ${{ env.PKCS11_TOKEN_SO_PIN }}
44-
token-user-pin: ${{ env.PKCS11_TOKEN_PIN }}
45-
- uses: ./.github/actions/install-opencryptoki
46-
# only run opencryptoki tests on ubuntu
47-
# (macos and windows don't seem to be supported)
48-
if: matrix.os == 'ubuntu-latest'
49-
id: opencryptoki
43+
- uses: ./.github/actions/test-setup
44+
id: setup
5045
with:
5146
os: ${{ matrix.os }}
5247
token-label: ${{ env.PKCS11_TOKEN_LABEL }}
5348
token-so-pin: ${{ env.PKCS11_TOKEN_SO_PIN }}
5449
token-user-pin: ${{ env.PKCS11_TOKEN_PIN }}
55-
- name: Install uv
56-
uses: astral-sh/setup-uv@v4
57-
with:
58-
enable-cache: true
5950
python-version: ${{ matrix.python-version }}
60-
- name: Install testing dependencies
61-
run: uv sync --no-dev --exact --group testing
62-
- name: Run tests with SoftHSM
63-
run: uv run pytest -v
64-
env:
65-
PKCS11_MODULE: ${{ steps.softhsm.outputs.module }}
66-
- name: Run tests with opencryptoki
67-
if: matrix.os == 'ubuntu-latest'
51+
pkcs11-platform: ${{ matrix.pkcs11-platform }}
52+
dependency-group: testing
53+
- name: Run tests
6854
run: uv run pytest -v
6955
env:
70-
PKCS11_MODULE: ${{ steps.opencryptoki.outputs.module }}
71-
# For testing logic around swapping PKCS#11 libs
72-
PKCS11_MODULE2: ${{ steps.softhsm.outputs.module }}
56+
PKCS11_MODULE: ${{ steps.setup.outputs.module }}

tests/test_multilib.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
PKCS#11 Slots and Tokens
3+
"""
4+
5+
import os
6+
import unittest
7+
8+
import pkcs11
9+
10+
from . import LIB
11+
12+
13+
@unittest.skipUnless("PKCS11_MODULE2" in os.environ, "Requires an additional PKCS#11 module")
14+
class MultilibTests(unittest.TestCase):
15+
def test_double_initialise_different_libs(self):
16+
lib1 = pkcs11.lib(LIB)
17+
lib2 = pkcs11.lib(os.environ["PKCS11_MODULE2"])
18+
self.assertIsNotNone(lib1)
19+
self.assertIsNotNone(lib2)
20+
self.assertIsNot(lib1, lib2)
21+
22+
slots1 = lib1.get_slots()
23+
slots2 = lib2.get_slots()
24+
25+
self.assertGreaterEqual(len(slots1), 1)
26+
self.assertGreaterEqual(len(slots2), 1)

tests/test_slots_and_tokens.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
PKCS#11 Slots and Tokens
33
"""
44

5-
import os
65
import unittest
76

87
import pkcs11
@@ -23,20 +22,6 @@ def test_nonexistent_lib(self):
2322
with self.assertRaises(RuntimeError):
2423
pkcs11.lib("thislibdoesntexist.so")
2524

26-
@unittest.skipUnless("PKCS11_MODULE2" in os.environ, "Requires an additional PKCS#11 module")
27-
def test_double_initialise_different_libs(self):
28-
lib1 = pkcs11.lib(LIB)
29-
lib2 = pkcs11.lib(os.environ["PKCS11_MODULE2"])
30-
self.assertIsNotNone(lib1)
31-
self.assertIsNotNone(lib2)
32-
self.assertIsNot(lib1, lib2)
33-
34-
slots1 = lib1.get_slots()
35-
slots2 = lib2.get_slots()
36-
37-
self.assertGreaterEqual(len(slots1), 1)
38-
self.assertGreaterEqual(len(slots2), 1)
39-
4025
def test_double_initialise_nonexistent_lib(self):
4126
self.assertIsNotNone(pkcs11.lib(LIB))
4227
with self.assertRaises(RuntimeError):

0 commit comments

Comments
 (0)