Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
omit = tests/*
18 changes: 18 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
# license: GPLv2
#

import os
import shutil


def pytest_configure(config):
# register custom markers
Expand All @@ -24,3 +27,18 @@ def pytest_configure(config):
config.addinivalue_line(
"markers", "create_fails(bool): let function create_issue_comment return None"
)


def pytest_sessionstart():
# Back up app.cfg if it exists
if os.path.exists("app.cfg"):
shutil.copyfile("app.cfg", "appbackup.cfg")
Comment thread
sondrebr marked this conversation as resolved.

# Copy needed app.cfg from tests directory
shutil.copyfile("tests/test_app.cfg", "app.cfg")


def pytest_sessionfinish():
# Restore backup if it exists
if os.path.exists("appbackup.cfg"):
shutil.copyfile("appbackup.cfg", "app.cfg")
5 changes: 0 additions & 5 deletions tests/test_eessi_bot_job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@
# license: GPLv2
#

import shutil

from eessi_bot_job_manager import EESSIBotSoftwareLayerJobManager


def test_determine_running_jobs():
# copy needed app.cfg from tests directory
shutil.copyfile("tests/test_app.cfg", "app.cfg")

job_manager = EESSIBotSoftwareLayerJobManager()

assert job_manager.determine_running_jobs({}) == []
Expand Down
46 changes: 40 additions & 6 deletions tests/test_task_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import filecmp
import os
import re
import shutil
from unittest.mock import patch

# Third party imports (anything installed into the local Python environment)
Expand Down Expand Up @@ -46,13 +45,15 @@ def test_run_cmd(tmpdir):
assert output == "hello\n"
assert err == ""

# Command fails and raise_on_error=True
with pytest.raises(Exception):
output, err, exit_code = run_cmd("ls -l /does_not_exists.txt", 'fail test', tmpdir, log_file=log_file)

assert exit_code != 0
assert output == ""
assert "No such file or directory" in err

# Command fails and raise_on_error=False
output, err, exit_code = run_cmd("ls -l /does_not_exists.txt",
'fail test',
tmpdir,
Expand All @@ -63,6 +64,7 @@ def test_run_cmd(tmpdir):
assert output == ""
assert "No such file or directory" in err

# Command does not exists and raise_on_error=True
with pytest.raises(Exception):
output, err, exit_code = run_cmd("this_command_does_not_exist", 'fail test', tmpdir, log_file=log_file)

Expand All @@ -71,6 +73,7 @@ def test_run_cmd(tmpdir):
assert ("this_command_does_not_exist: command not found" in err or
"this_command_does_not_exist: not found" in err)

# Command does not exists and raise_on_error=False
output, err, exit_code = run_cmd("this_command_does_not_exist",
'fail test',
tmpdir,
Expand All @@ -82,6 +85,7 @@ def test_run_cmd(tmpdir):
assert ("this_command_does_not_exist: command not found" in err or
"this_command_does_not_exist: not found" in err)

# Check that log_msg is written to log_file
output, err, exit_code = run_cmd("echo hello", "test in file", tmpdir, log_file=log_file)
with open(log_file, "r") as fp:
assert "test in file" in fp.read()
Expand All @@ -96,18 +100,53 @@ def test_run_subprocess(tmpdir):
assert output == "hello\n"
assert err == ""

# log_msg=""
output, err, exit_code = run_subprocess("echo hello", "", tmpdir, log_file=log_file)

assert exit_code == 0
assert output == "hello\n"
assert err == ""
with open(log_file, "r") as fp:
# TODO: Better way to do this?
assert "run_subprocess(): Running" in fp.read()

# working_dir=tmpdir
output, err, exit_code = run_subprocess("pwd", "test", tmpdir, log_file=log_file)

assert exit_code == 0
assert tmpdir.strpath+"\n" == output
Comment thread
sondrebr marked this conversation as resolved.
Outdated
assert err == ""

# working_dir=None
wd = os.getcwd()
output, err, exit_code = run_subprocess("pwd", "test", None, log_file=log_file)

assert exit_code == 0
assert wd in output
assert err == ""

# env is not None
output, err, exit_code = run_subprocess("env", "test", tmpdir, log_file=log_file, env={"DUMMY": "123"})

assert exit_code == 0
assert "DUMMY=123" in output
assert err == ""

# Command fails
output, err, exit_code = run_subprocess("ls -l /does_not_exists.txt", 'fail test', tmpdir, log_file=log_file)

assert exit_code != 0
assert output == ""
assert "No such file or directory" in err

# Command does not exist
output, err, exit_code = run_subprocess("this_command_does_not_exist", 'fail test', tmpdir, log_file=log_file)

assert exit_code != 0
assert output == ""
assert ("this_command_does_not_exist: command not found" in err or "this_command_does_not_exist: not found" in err)

# Check that log_msg is written to log_file
output, err, exit_code = run_subprocess("echo hello", "test in file", tmpdir, log_file=log_file)
with open(log_file, "r") as fp:
assert "test in file" in fp.read()
Expand Down Expand Up @@ -282,7 +321,6 @@ def no_sleep_after_create(delay):
def test_create_pr_comment_succeeds(monkeypatch, mocked_github, tmpdir):
"""Tests for function create_pr_comment."""
monkeypatch.setattr('tools.pr_comments.github', mocked_github)
shutil.copyfile("tests/test_app.cfg", "app.cfg")
# creating a PR comment
print("CREATING PR COMMENT")
ym = datetime.today().strftime('%Y.%m')
Expand Down Expand Up @@ -313,7 +351,6 @@ def test_create_pr_comment_succeeds(monkeypatch, mocked_github, tmpdir):
def test_create_pr_comment_succeeds_none(monkeypatch, mocked_github, tmpdir):
"""Tests for function create_pr_comment."""
monkeypatch.setattr('tools.pr_comments.github', mocked_github)
shutil.copyfile("tests/test_app.cfg", "app.cfg")
# creating a PR comment
print("CREATING PR COMMENT")
ym = datetime.today().strftime('%Y.%m')
Expand All @@ -340,7 +377,6 @@ def test_create_pr_comment_succeeds_none(monkeypatch, mocked_github, tmpdir):
def test_create_pr_comment_raises_once_then_succeeds(monkeypatch, mocked_github, tmpdir):
"""Tests for function create_pr_comment."""
monkeypatch.setattr('tools.pr_comments.github', mocked_github)
shutil.copyfile("tests/test_app.cfg", "app.cfg")
# creating a PR comment
print("CREATING PR COMMENT")
ym = datetime.today().strftime('%Y.%m')
Expand All @@ -367,7 +403,6 @@ def test_create_pr_comment_raises_once_then_succeeds(monkeypatch, mocked_github,
def test_create_pr_comment_always_raises(monkeypatch, mocked_github, tmpdir):
"""Tests for function create_pr_comment."""
monkeypatch.setattr('tools.pr_comments.github', mocked_github)
shutil.copyfile("tests/test_app.cfg", "app.cfg")
# creating a PR comment
print("CREATING PR COMMENT")
ym = datetime.today().strftime('%Y.%m')
Expand Down Expand Up @@ -395,7 +430,6 @@ def test_create_pr_comment_always_raises(monkeypatch, mocked_github, tmpdir):
def test_create_pr_comment_three_raises(monkeypatch, mocked_github, tmpdir):
"""Tests for function create_pr_comment."""
monkeypatch.setattr('tools.pr_comments.github', mocked_github)
shutil.copyfile("tests/test_app.cfg", "app.cfg")
# creating a PR comment
print("CREATING PR COMMENT")
ym = datetime.today().strftime('%Y.%m')
Expand Down
85 changes: 85 additions & 0 deletions tests/test_tools_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Tests for 'tools/args.py' of the EESSI build-and-deploy bot,
# see https://github.com/EESSI/eessi-bot-software-layer
#
# The bot helps with requests to add software installations to the
# EESSI software layer, see https://github.com/EESSI/software-layer
#
# author: Sondre Bergsvaag Risanger (@sondrebr)
#
# license: GPLv2
#

# Standard library imports
from argparse import Namespace
from contextlib import nullcontext

# Third party imports (anything installed into the local Python environment)
import pytest

# Local application imports (anything from EESSI/eessi-bot-software-layer)
from tools import args


# Test parse_common_args()
@pytest.mark.parametrize("test_args,expected_parsed,expected_unknown", [
# No args
([], Namespace(debug=False), []),

# Short-form args
(["-d"], Namespace(debug=True), []),

# Long-form args
(["--debug"], Namespace(debug=True), []),

# Unknown args
(["-u", "--unknown"], Namespace(debug=False), ["-u", "--unknown"]),
])
def test_parse_common_args(test_args, expected_parsed, expected_unknown):
parsed_args, unknown = args.parse_common_args(test_args)

assert parsed_args == expected_parsed
assert sorted(unknown) == sorted(expected_unknown)


# Test event_handler_parse()
@pytest.mark.parametrize("test_args,expectation", [
# No args
([], nullcontext(Namespace(debug=False, build=False, test=False, cron=False, file=None, port=3000))),

# Short-form args
(["-d", "-b", "-t", "-c", "-f", "file.json", "-p", "8000"],
nullcontext(Namespace(debug=True, build=True, test=True, cron=True, file="file.json", port="8000"))),

# Long-form args
(["--debug", "--build", "--test", "--cron", "--file", "file2.json", "--port", "9000"],
nullcontext(Namespace(debug=True, build=True, test=True, cron=True, file="file2.json", port="9000"))),

# Unknown args - should fail and exit
(["-u"], pytest.raises(SystemExit)),
(["--unknown"], pytest.raises(SystemExit)),
])
def test_event_handler_parse_known_args(test_args, expectation):
with expectation as expected:
assert args.event_handler_parse(test_args) == expected


# Test job_manager_parse()
@pytest.mark.parametrize("test_args,expectation", [
# No args
([], nullcontext(Namespace(debug=False, max_manager_iterations=-1, jobs=None))),

# Short-form args
(["-d", "-i", "0", "-j", "17"],
nullcontext(Namespace(debug=True, max_manager_iterations="0", jobs="17"))),

# Long-form args
(["--debug", "--max-manager-iterations", "10", "--jobs", "4,18,48"],
nullcontext(Namespace(debug=True, max_manager_iterations="10", jobs="4,18,48"))),

# Unknown args - should fail and exit
(["-u"], pytest.raises(SystemExit)),
(["--unknown"], pytest.raises(SystemExit)),
])
def test_job_manager_parse_known_args(test_args, expectation):
with expectation as expected:
assert args.job_manager_parse(test_args) == expected
43 changes: 43 additions & 0 deletions tests/test_tools_build_params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Tests for 'tools/build_params.py' of the EESSI build-and-deploy bot,
# see https://github.com/EESSI/eessi-bot-software-layer
#
# The bot helps with requests to add software installations to the
# EESSI software layer, see https://github.com/EESSI/software-layer
#
# author: Sondre Bergsvaag Risanger (@sondrebr)
#
# license: GPLv2
#

# Standard library imports
from contextlib import nullcontext

# Third party imports (anything installed into the local Python environment)
import pytest

# Local application imports (anything from EESSI/eessi-bot-software-layer)
from tools import build_params


# Test EESSIBotBuildParams class
@pytest.mark.parametrize("build_parameters,expectation", [
# Test value error
("notaparam", pytest.raises(build_params.EESSIBotBuildParamsValueError)),

# Test name error
("thisparam=doesnotexist", pytest.raises(build_params.EESSIBotBuildParamsNameError)),

# Test complete component names
("architecture=x86_64/amd/zen4,accelerator=nvidia/cc80",
nullcontext({"architecture": "x86_64/amd/zen4", "accelerator": "nvidia/cc80"})),

# Test shortened component names
("arch=aarch64/nvidia/grace,accel=nvidia/cc90",
nullcontext({"architecture": "aarch64/nvidia/grace", "accelerator": "nvidia/cc90"})),
])
def test_EESSIBotBuildParams(build_parameters, expectation):
with expectation as expected_params:
params = build_params.EESSIBotBuildParams(build_parameters)
# Verify that the resulting object contains the expected items
for name, expected_value in expected_params.items():
assert params[name] == expected_value
Loading