Skip to content

Commit

Permalink
nix: move devShell test into pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
dougch committed Jun 16, 2023
1 parent 3af5d12 commit febc471
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 25 deletions.
11 changes: 11 additions & 0 deletions nix/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import platform
import pytest

ALL = set(["aarch64", "x86_64"])


def pytest_runtest_setup(item):
supported_platforms = ALL.intersection(mark.name for mark in item.iter_markers())
plat = platform.machine()
if supported_platforms and plat not in supported_platforms:
pytest.skip("platform specific test; not running on {}".format(plat))
3 changes: 2 additions & 1 deletion nix/pyenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ let
];
};
in pkgs.python310.withPackages (ps: [
ps.pep8
ps.autopep8
ps.pyfiglet
ps.pytest # ==5.3.5 TODO: check if this version is correct/if it matters
ps.pytest-xdist # ==1.34.0 TODO: check if this version is correct/if it matters
sslyze # ==5.0.2 TODO: check if this version is correct/if it matters
Expand Down
4 changes: 4 additions & 0 deletions nix/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
markers =
aarch64: tests that should pass on aarch64
x86_64: tests that should pass on x86_64
27 changes: 3 additions & 24 deletions nix/shell.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
echo nix/shell.sh: Entering a devShell
pyfiglet -f starwars "s2n-tls devShell"
echo nix/shell.sh: Entering a devShell...checking environment.
export SRC_ROOT=$(pwd)
export PATH=$SRC_ROOT/build/bin:$PATH
pytest -rfs -v ./nix/test_devshell.py

banner()
{
Expand All @@ -9,7 +11,6 @@ banner()
echo "+---------------------------------------------------------+"
}


function clean {
banner "Cleanup up ./build"
rm -rf ./build
Expand Down Expand Up @@ -115,28 +116,6 @@ function do-clang-format {
echo $src_files | xargs -n 1 -P $(nproc) clang-format -style=file -i)
}

function test_toolchain_counts {
# This is a starting point for a unit test of the devShell.
# The choosen S2N_LIBCRYPTO should be 2, and the others should be zero.
banner "Checking the CMAKE_INCLUDE_PATH for libcrypto counts"
echo $CMAKE_INCLUDE_PATH|gawk 'BEGIN{RS=":"; o10=0; o11=0; o3=0;awslc=0;libre=0}
/openssl-3.0/{o3++}
/openssl-1.1/{o11++}
/openssl-1.0/{o10++}
/aws-lc/{awslc++}
/libressl/{libre++}
END{print "\nOpenssl3:\t",o3,"\nOpenssl1.1:\t",o11,"\nOpenssl1.0.2:\t",o10,"\nAwlc:\t\t",awslc,"\nLibreSSL:\t", libre}'
banner "Checking tooling counts (these should all be 1)"
echo -e "\nOpenssl integ:\t $(openssl version|grep -c '1.1.1')"
echo -e "Corretto 17:\t $(java -version 2>&1|grep -ce 'Runtime.*Corretto-17')"
echo -e "gnutls-cli:\t $(gnutls-cli --version |grep -c 'gnutls-cli 3.7')"
echo -e "gnutls-serv:\t $(gnutls-serv --version |grep -c 'gnutls-serv 3.7')"
echo -e "Nix Python:\t $(which python|grep -c '/nix/store')"
echo -e "Nix pytest:\t $(which pytest|grep -c '/nix/store')"
echo -e "Nix sslyze:\t $(which sslyze|grep -c '/nix/store')"
echo -e "python nassl:\t $(pip freeze|grep -c 'nassl')"
}

function test_nonstandard_compilation {
# Any script that needs to compile s2n in a non-standard way can run here
./codebuild/bin/test_dynamic_load.sh $(mktemp -d)
Expand Down
84 changes: 84 additions & 0 deletions nix/test_devshell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import enum
import sys
import os
import shutil
import subprocess
import platform
import pytest


def translate_lc(lc: str):
# awslc for S2N_LIBCRYPTO is special.
if "awslc" in lc:
return "aws-lc"
else:
return lc


@pytest.mark.parametrize("lc", [os.getenv("S2N_LIBCRYPTO")])
def test_s2n_libcrypto(lc):
# Validate S2N_LIBCRYPTO is in the CMAKE_INCLUDE_PATH
libcrypto = translate_lc(lc)
include_path = os.getenv("CMAKE_INCLUDE_PATH")
assert libcrypto is not None
assert include_path is not None
assert libcrypto in include_path


@pytest.mark.parametrize("lc", [os.getenv("S2N_LIBCRYPTO")])
def test_s2n_libcrypto_uniq(lc):
# Make certain we only have the preferred libcrypto in CMAKE_INCLUDE_PATH.
libcrypto = translate_lc(lc)
include_path = os.getenv("CMAKE_INCLUDE_PATH")
assert libcrypto is not None
assert include_path is not None
all_lcs = ["openssl-3.0", "openssl-1.1.1",
"openssl-1.0.2", "libressl", "aws-lc"]
all_lcs.remove(libcrypto)
for library in all_lcs:
assert library not in include_path


@pytest.mark.parametrize("cmd,expected,version", [
("gnutls-serv", "gnutls-serv 3.7", "--version"),
("gnutls-cli", "gnutls-cli 3.7", "--version"),
("openssl", "OpenSSL 1.1.1", "version"),
("java", "Corretto-17", "--version")])
def test_utility_versions(cmd, expected, version):
# Valildate utility is in the path and the correct version.
abspath = shutil.which(cmd)
result = ""
with subprocess.Popen([abspath, version], shell=False, stdout=subprocess.PIPE) as p:
output = p.stdout.readlines()
for line in output:
result += line.decode().strip()
' '.join(result)
assert expected in result


def test_python():
# Validate python _from nix_ is in the PATH.
assert 'nix' in shutil.which('python')


def test_pytest():
# Validate pytest _from nix_ is in the PATH.
assert 'nix' in shutil.which('pytest')


@pytest.mark.x86_64
def test_sslyze():
# On x86, validate sslyze is available
assert 'nix' in shutil.which('sslyze')


@pytest.mark.x86_64
def test_nassl():
# On x86 validate nassl and version.
import nassl
assert nassl.__name__ == "nassl"
assert nassl.__version__ == "5.0.0"


if __name__ == "__main__":
print("Use pytest")

0 comments on commit febc471

Please sign in to comment.