Skip to content

Commit

Permalink
Enable jenkins CI (#118)
Browse files Browse the repository at this point in the history
- Enable CI
- Committed test suite that was forgotten on dev machine.
  • Loading branch information
pylessard authored Feb 13, 2024
1 parent 3a278e8 commit fdfabf3
Show file tree
Hide file tree
Showing 13 changed files with 435 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[run]
source = isotp
concurrency = thread
90 changes: 90 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
FROM ubuntu:20.04 as build-tests

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y \
git \
wget \
libssl-dev \
build-essential \
libffi-dev \
libreadline-dev \
zlib1g-dev \
libsqlite3-dev \
libssl-dev \
iproute2 \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /tmp/

# ============================================
ARG PYTHON_VERSION="3.11.1"
ARG PYTHON_SRC="https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz"

RUN wget $PYTHON_SRC \
&& tar -xvzf "Python-${PYTHON_VERSION}.tgz" \
&& cd "Python-${PYTHON_VERSION}" \
&& ./configure \
&& make -j 4 \
&& make install \
&& cd .. \
&& rm "Python-${PYTHON_VERSION}.tgz" \
&& rm -rf "Python-${PYTHON_VERSION}"


# ============================================
ARG PYTHON_VERSION="3.10.9"
ARG PYTHON_SRC="https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz"

RUN wget $PYTHON_SRC \
&& tar -xvzf "Python-${PYTHON_VERSION}.tgz" \
&& cd "Python-${PYTHON_VERSION}" \
&& ./configure \
&& make -j 4 \
&& make install \
&& cd .. \
&& rm "Python-${PYTHON_VERSION}.tgz" \
&& rm -rf "Python-${PYTHON_VERSION}"

# ============================================
ARG PYTHON_VERSION="3.9.16"
ARG PYTHON_SRC="https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz"

RUN wget $PYTHON_SRC \
&& tar -xvzf "Python-${PYTHON_VERSION}.tgz" \
&& cd "Python-${PYTHON_VERSION}" \
&& ./configure \
&& make -j 4 \
&& make install \
&& cd .. \
&& rm "Python-${PYTHON_VERSION}.tgz" \
&& rm -rf "Python-${PYTHON_VERSION}"

# ============================================
ARG PYTHON_VERSION="3.8.16"
ARG PYTHON_SRC="https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz"

RUN wget $PYTHON_SRC \
&& tar -xvzf "Python-${PYTHON_VERSION}.tgz" \
&& cd "Python-${PYTHON_VERSION}" \
&& ./configure \
&& make -j 4 \
&& make install \
&& cd .. \
&& rm "Python-${PYTHON_VERSION}.tgz" \
&& rm -rf "Python-${PYTHON_VERSION}"

# ============================================

ARG PYTHON_VERSION="3.7.17"
ARG PYTHON_SRC="https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz"

RUN wget $PYTHON_SRC \
&& tar -xvzf "Python-${PYTHON_VERSION}.tgz" \
&& cd "Python-${PYTHON_VERSION}" \
&& ./configure \
&& make -j 4 \
&& make install \
&& cd .. \
&& rm "Python-${PYTHON_VERSION}.tgz" \
&& rm -rf "Python-${PYTHON_VERSION}"
85 changes: 85 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
pipeline {
agent {
label 'docker'
}
stages {
stage ('Docker') {
agent {
dockerfile {
// Required to be root to create vcan interfaces
args '-e HOME=/tmp -e BUILD_CONTEXT=ci --cap-add=NET_ADMIN -u 0:0'
additionalBuildArgs '--target build-tests'
reuseNode true
}
}
stages {
stage('Setup vcan'){
steps {
sh '''
ip link add dev vcan0 type vcan || true
ip link set up vcan0
ip link add dev vcan1 type vcan || true
ip link set up vcan1
ip link add dev vcan2 type vcan || true
ip link set up vcan2
ip link add dev vcan3 type vcan || true
ip link set up vcan3
ip link add dev vcan4 type vcan || true
ip link set up vcan4
'''
}
}
stage('Testing'){
parallel{

stage ('Python 3.11') {
steps {
sh '''
python3.11 -m venv venv-3.11
VENV_DIR=venv-3.11 scripts/with-venv.sh scripts/check-python-version.sh 3.11
VENV_DIR=venv-3.11 COVERAGE_SUFFIX=3.11 UNITTEST_VCAN=vcan0 scripts/with-venv.sh scripts/runtests.sh
'''
}
}
stage ('Python 3.10') {
steps {
sh '''
python3.10 -m venv venv-3.10
VENV_DIR=venv-3.10 scripts/with-venv.sh scripts/check-python-version.sh 3.10
VENV_DIR=venv-3.10 COVERAGE_SUFFIX=3.10 UNITTEST_VCAN=vcan1 scripts/with-venv.sh scripts/runtests.sh
'''
}
}
stage ('Python 3.9') {
steps {
sh '''
python3.9 -m venv venv-3.9
VENV_DIR=venv-3.9 scripts/with-venv.sh scripts/check-python-version.sh 3.9
VENV_DIR=venv-3.9 COVERAGE_SUFFIX=3.9 UNITTEST_VCAN=vcan2 scripts/with-venv.sh scripts/runtests.sh
'''
}
}
stage ('Python 3.8') {
steps {
sh '''
python3.8 -m venv venv-3.8
VENV_DIR=venv-3.8 scripts/with-venv.sh scripts/check-python-version.sh 3.8
VENV_DIR=venv-3.8 COVERAGE_SUFFIX=3.8 UNITTEST_VCAN=vcan3 scripts/with-venv.sh scripts/runtests.sh
'''
}
}
stage ('Python 3.7') {
steps {
sh '''
python3.7 -m venv venv-3.7
VENV_DIR=venv-3.7 scripts/with-venv.sh scripts/check-python-version.sh 3.7
VENV_DIR=venv-3.7 COVERAGE_SUFFIX=3.7 UNITTEST_VCAN=vcan4 scripts/with-venv.sh scripts/runtests.sh
'''
}
}
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion doc/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# udsoncan documentation build configuration file, created by
# isotp documentation build configuration file, created by
# sphinx-quickstart on Thu Apr 26 19:43:51 2018.
#
# This file is execfile()d with the current directory set to its
Expand Down
7 changes: 4 additions & 3 deletions isotp/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from collections.abc import Iterable


from typing import Optional, Any, List, Callable, Dict, Tuple, Union, Generator
from typing import Optional, Any, List, Callable, Dict, Tuple, Union, Generator, cast

try:
import can
Expand Down Expand Up @@ -528,6 +528,7 @@ def __init__(self,
gen, size = data
self.generator = FiniteByteGenerator(gen, size)
elif isinstance(data, Iterable):
data = cast(Union[bytes, bytearray], data) # type:ignore
self.generator = FiniteByteGenerator((x for x in data), len(data))
else:
raise ValueError("data must be an iterable element (bytes or bytearray) or a tuple of generator,size")
Expand Down Expand Up @@ -1684,11 +1685,11 @@ class BusOwner:

def python_can_tx_canbus_3plus(owner: BusOwner, msg: CanMessage) -> None:
owner.bus.send(can.Message(arbitration_id=msg.arbitration_id, data=msg.data,
is_extended_id=msg.is_extended_id, is_fd=msg.is_fd, bitrate_switch=msg.bitrate_switch)) # type:ignore
is_extended_id=msg.is_extended_id, is_fd=msg.is_fd, bitrate_switch=msg.bitrate_switch))


def python_can_tx_canbus_3minus(owner: BusOwner, msg: CanMessage) -> None:
owner.bus.send(can.Message(arbitration_id=msg.arbitration_id, data=msg.data,
owner.bus.send(can.Message(arbitration_id=msg.arbitration_id, data=msg.data, # type:ignore
extended_id=msg.is_extended_id, is_fd=msg.is_fd, bitrate_switch=msg.bitrate_switch)) # type:ignore


Expand Down
41 changes: 41 additions & 0 deletions scripts/activate-venv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
set -euo pipefail

PROJECT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/.. >/dev/null 2>&1 && pwd -P )"
PY_MODULE_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." >/dev/null 2>&1 && pwd -P )"

VENV_DIR="${VENV_DIR:-venv}"
VENV_ROOT="${VENV_DIR:-$PROJECT_ROOT/$VENV_DIR}"

log() { echo -e "\x1B[92m[OK]\x1B[39m $@"; }

[ ! -d "$VENV_ROOT" ] \
&& log "Missing venv. Creating..." \
&& python3 -m venv "$VENV_ROOT"

source "$VENV_ROOT/bin/activate"

if ! pip3 show wheel 2>&1 >/dev/null; then
log "Installing wheel..."
pip3 install wheel
log "Upgrading pip..."
pip3 install --upgrade pip
log "Upgrading setuptools..."
pip3 install --upgrade setuptools
fi

MODULE_FEATURE="[dev]"
if ! [[ -z "${BUILD_CONTEXT+x}" ]]; then
if [[ "$BUILD_CONTEXT" == "ci" ]]; then
MODULE_FEATURE="[test]" # Will cause testing tools to be installed.
fi
fi

if ! diff "$PY_MODULE_ROOT/setup.py" "$VENV_ROOT/cache/setup.py" 2>&1 >/dev/null; then
log "Install inside venv"
pip3 install -e "${PY_MODULE_ROOT}${MODULE_FEATURE}"
mkdir -p "$VENV_ROOT/cache/"
cp "$PY_MODULE_ROOT/setup.py" "$VENV_ROOT/cache/setup.py"
fi

set +e
28 changes: 28 additions & 0 deletions scripts/check-python-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash
set -euo pipefail

shopt -s nocasematch

if [ $# -eq 0 ]; then
echo "Python version must be specified"
exit 1
fi

REQUIRED_VERSION=$1
ACTUAL_PYTHON_VERSION=$(python3 --version)
ACTUAL_PIP_VERSION=$(pip3 --version)

if [[ $ACTUAL_PYTHON_VERSION != *"python ${REQUIRED_VERSION}"* ]]; then
echo "ERROR - Reported python3 version is ${ACTUAL_PYTHON_VERSION}. Expecting Python ${REQUIRED_VERSION}"
exit 1
fi

if [[ $ACTUAL_PIP_VERSION != *"python ${REQUIRED_VERSION}"* ]]; then
echo "ERROR - Reported pip3 version is ${ACTUAL_PIP_VERSION}. Expecting pip for Python ${REQUIRED_VERSION}"
exit 1
fi

echo "Python version OK."
echo " - ${ACTUAL_PYTHON_VERSION}"
echo " - ${ACTUAL_PIP_VERSION}"
exit 0
22 changes: 22 additions & 0 deletions scripts/runtests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
set -euo pipefail

COVERAGE_SUFFIX="${COVERAGE_SUFFIX:-dev}"
HTML_COVDIR="htmlcov_${COVERAGE_SUFFIX}"
COV_DATAFILE=".coverage_${COVERAGE_SUFFIX}"

if ! [[ -z "${BUILD_CONTEXT+x}" ]]; then
if [[ "$BUILD_CONTEXT" == "ci" ]]; then
if ! [[ -z "${NODE_NAME+x}" ]]; then
echo "Running tests on agent: ${NODE_NAME}"
fi
fi
fi

set -x

python3 -m coverage run --data-file ${COV_DATAFILE} -m unittest -v
python3 -m mypy isotp --strict --no-warn-unused-ignore
python3 -m coverage report --data-file ${COV_DATAFILE}
python3 -m coverage html --data-file ${COV_DATAFILE} -d $HTML_COVDIR

8 changes: 8 additions & 0 deletions scripts/with-venv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

set -euo pipefail
PROJECT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/.. >/dev/null 2>&1 && pwd -P )"
source "$PROJECT_ROOT/scripts/activate-venv.sh"

set -e # activate-venv sets +e
exec "$@"
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
name='can-isotp',
packages=find_packages(where='.', exclude=['test', 'test.*'], include=['isotp', "isotp.*"]),
version='2.0.4',
extras_require={
'test': ['mypy', 'coverage', 'python-can'],
'dev': ['mypy', 'ipdb', 'autopep8', 'coverage', 'python-can']
},
description='Module enabling the IsoTP protocol defined by ISO-15765',
long_description=long_description,
author='Pier-Yves Lessard',
Expand Down
16 changes: 9 additions & 7 deletions test/ThreadableTest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import unittest
import unittest
import queue
import _thread as thread
import threading

#Class borrowed from Python Socket test suite.
# Class borrowed from Python Socket test suite.


class ThreadableTest(unittest.TestCase):
def __init__(self, *args, **kwargs):
unittest.TestCase.__init__(self, *args, **kwargs)
Expand All @@ -26,9 +27,10 @@ def _setUp(self):
# Do some munging to start the client test.
methodname = self.id()
i = methodname.rfind('.')
methodname = methodname[i+1:]
methodname = methodname[i + 1:]
test_method = getattr(self, '_' + methodname)
self.client_thread = thread.start_new_thread(self.clientRun, (test_method,))
self.client_thread = threading.Thread(target=self.clientRun, args=(test_method,))
self.client_thread.start()

try:
self.__setUp()
Expand Down Expand Up @@ -81,5 +83,5 @@ def _clientTearDown(self):
self.clientTearDown()
except BaseException as e:
self.queue.put(e)
finally:
thread.exit()
finally:
raise SystemExit()
Loading

0 comments on commit fdfabf3

Please sign in to comment.