Skip to content

Commit bef4dfb

Browse files
authored
Merge pull request #371 from EESSI/develop
[gitlab] Sync `gitlab` branch with `develop`
2 parents 167da4a + 544d34c commit bef4dfb

12 files changed

Lines changed: 527 additions & 70 deletions

.coveragerc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[run]
2+
omit = tests/*

tests/conftest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
# EESSI software layer, see https://github.com/EESSI/software-layer
66
#
77
# author: Thomas Roeblitz (@trz42)
8+
# author: Sondre Bergsvaag Risanger (@sondrebr)
89
#
910
# license: GPLv2
1011
#
1112

13+
import os
14+
import shutil
15+
1216

1317
def pytest_configure(config):
1418
# register custom markers
@@ -24,3 +28,18 @@ def pytest_configure(config):
2428
config.addinivalue_line(
2529
"markers", "create_fails(bool): let function create_issue_comment return None"
2630
)
31+
32+
33+
def pytest_sessionstart():
34+
# Back up app.cfg if it exists
35+
if os.path.exists("app.cfg"):
36+
shutil.copyfile("app.cfg", "appbackup.cfg")
37+
38+
# Copy needed app.cfg from tests directory
39+
shutil.copyfile("tests/test_app.cfg", "app.cfg")
40+
41+
42+
def pytest_sessionfinish():
43+
# Restore backup if it exists
44+
if os.path.exists("appbackup.cfg"):
45+
shutil.copyfile("appbackup.cfg", "app.cfg")

tests/test_app.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ running_job = job `{job_id}` is running
3636
[finished_job_comments]
3737

3838
[bot_control]
39+
command_permission = user01 second_user

tests/test_eessi_bot_job_manager.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,10 @@
1212
# license: GPLv2
1313
#
1414

15-
import shutil
16-
1715
from eessi_bot_job_manager import EESSIBotSoftwareLayerJobManager
1816

1917

2018
def test_determine_running_jobs():
21-
# copy needed app.cfg from tests directory
22-
shutil.copyfile("tests/test_app.cfg", "app.cfg")
23-
2419
job_manager = EESSIBotSoftwareLayerJobManager()
2520

2621
assert job_manager.determine_running_jobs({}) == []

tests/test_task_build.py

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import filecmp
1919
import os
2020
import re
21-
import shutil
2221
from unittest.mock import patch
2322

2423
# Third party imports (anything installed into the local Python environment)
@@ -37,43 +36,47 @@
3736
from tests.test_tools_pr_comments import MockIssueComment
3837

3938

40-
def test_run_cmd(tmpdir):
39+
def test_run_cmd(tmp_path):
4140
"""Tests for run_cmd function."""
42-
log_file = os.path.join(tmpdir, "log.txt")
43-
output, err, exit_code = run_cmd("echo hello", 'test', tmpdir, log_file=log_file)
41+
log_file = os.path.join(tmp_path, "log.txt")
42+
output, err, exit_code = run_cmd("echo hello", 'test', tmp_path, log_file=log_file)
4443

4544
assert exit_code == 0
4645
assert output == "hello\n"
4746
assert err == ""
4847

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

5252
assert exit_code != 0
5353
assert output == ""
5454
assert "No such file or directory" in err
5555

56+
# Command fails and raise_on_error=False
5657
output, err, exit_code = run_cmd("ls -l /does_not_exists.txt",
5758
'fail test',
58-
tmpdir,
59+
tmp_path,
5960
log_file=log_file,
6061
raise_on_error=False)
6162

6263
assert exit_code != 0
6364
assert output == ""
6465
assert "No such file or directory" in err
6566

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

6971
assert exit_code != 0
7072
assert output == ""
7173
assert ("this_command_does_not_exist: command not found" in err or
7274
"this_command_does_not_exist: not found" in err)
7375

76+
# Command does not exists and raise_on_error=False
7477
output, err, exit_code = run_cmd("this_command_does_not_exist",
7578
'fail test',
76-
tmpdir,
79+
tmp_path,
7780
log_file=log_file,
7881
raise_on_error=False)
7982

@@ -82,33 +85,69 @@ def test_run_cmd(tmpdir):
8285
assert ("this_command_does_not_exist: command not found" in err or
8386
"this_command_does_not_exist: not found" in err)
8487

85-
output, err, exit_code = run_cmd("echo hello", "test in file", tmpdir, log_file=log_file)
88+
# Check that log_msg is written to log_file
89+
output, err, exit_code = run_cmd("echo hello", "test in file", tmp_path, log_file=log_file)
8690
with open(log_file, "r") as fp:
8791
assert "test in file" in fp.read()
8892

8993

90-
def test_run_subprocess(tmpdir):
94+
def test_run_subprocess(tmp_path):
9195
"""Tests for run_subprocess function."""
92-
log_file = os.path.join(tmpdir, "log.txt")
93-
output, err, exit_code = run_subprocess("echo hello", 'test', tmpdir, log_file=log_file)
96+
log_file = os.path.join(tmp_path, "log.txt")
97+
output, err, exit_code = run_subprocess("echo hello", 'test', tmp_path, log_file=log_file)
9498

9599
assert exit_code == 0
96100
assert output == "hello\n"
97101
assert err == ""
98102

99-
output, err, exit_code = run_subprocess("ls -l /does_not_exists.txt", 'fail test', tmpdir, log_file=log_file)
103+
# log_msg=""
104+
output, err, exit_code = run_subprocess("echo hello", "", tmp_path, log_file=log_file)
105+
106+
assert exit_code == 0
107+
assert output == "hello\n"
108+
assert err == ""
109+
with open(log_file, "r") as fp:
110+
# TODO: Better way to do this?
111+
assert "run_subprocess(): Running" in fp.read()
112+
113+
# working_dir=tmp_path
114+
output, err, exit_code = run_subprocess("pwd", "test", tmp_path, log_file=log_file)
115+
116+
assert exit_code == 0
117+
assert f"{tmp_path}\n" == output
118+
assert err == ""
119+
120+
# working_dir=None
121+
wd = os.getcwd()
122+
output, err, exit_code = run_subprocess("pwd", "test", None, log_file=log_file)
123+
124+
assert exit_code == 0
125+
assert wd in output
126+
assert err == ""
127+
128+
# env is not None
129+
output, err, exit_code = run_subprocess("env", "test", tmp_path, log_file=log_file, env={"DUMMY": "123"})
130+
131+
assert exit_code == 0
132+
assert "DUMMY=123" in output
133+
assert err == ""
134+
135+
# Command fails
136+
output, err, exit_code = run_subprocess("ls -l /does_not_exists.txt", 'fail test', tmp_path, log_file=log_file)
100137

101138
assert exit_code != 0
102139
assert output == ""
103140
assert "No such file or directory" in err
104141

105-
output, err, exit_code = run_subprocess("this_command_does_not_exist", 'fail test', tmpdir, log_file=log_file)
142+
# Command does not exist
143+
output, err, exit_code = run_subprocess("this_command_does_not_exist", 'fail test', tmp_path, log_file=log_file)
106144

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

111-
output, err, exit_code = run_subprocess("echo hello", "test in file", tmpdir, log_file=log_file)
149+
# Check that log_msg is written to log_file
150+
output, err, exit_code = run_subprocess("echo hello", "test in file", tmp_path, log_file=log_file)
112151
with open(log_file, "r") as fp:
113152
assert "test in file" in fp.read()
114153

@@ -279,15 +318,14 @@ def no_sleep_after_create(delay):
279318
# returns !None --> create_pr_comment returns comment (with id == 1)
280319
@pytest.mark.repo_name("EESSI/software-layer")
281320
@pytest.mark.pr_number(1)
282-
def test_create_pr_comment_succeeds(monkeypatch, mocked_github, tmpdir):
321+
def test_create_pr_comment_succeeds(monkeypatch, mocked_github, tmp_path):
283322
"""Tests for function create_pr_comment."""
284323
monkeypatch.setattr('tools.pr_comments.github', mocked_github)
285-
shutil.copyfile("tests/test_app.cfg", "app.cfg")
286324
# creating a PR comment
287325
print("CREATING PR COMMENT")
288326
ym = datetime.today().strftime('%Y.%m')
289327
pr_number = 1
290-
job = Job(tmpdir, "test/architecture", "EESSI", "--speed-up", ym, pr_number, "fpga/magic", "user01")
328+
job = Job(tmp_path, "test/architecture", "EESSI", "--speed-up", ym, pr_number, "fpga/magic", "user01")
291329
build_params = EESSIBotBuildParams("arch=amd/zen4,accel=nvidia/cc90")
292330

293331
job_id = "123"
@@ -310,15 +348,14 @@ def test_create_pr_comment_succeeds(monkeypatch, mocked_github, tmpdir):
310348
@pytest.mark.repo_name("EESSI/software-layer")
311349
@pytest.mark.pr_number(1)
312350
@pytest.mark.create_fails(True)
313-
def test_create_pr_comment_succeeds_none(monkeypatch, mocked_github, tmpdir):
351+
def test_create_pr_comment_succeeds_none(monkeypatch, mocked_github, tmp_path):
314352
"""Tests for function create_pr_comment."""
315353
monkeypatch.setattr('tools.pr_comments.github', mocked_github)
316-
shutil.copyfile("tests/test_app.cfg", "app.cfg")
317354
# creating a PR comment
318355
print("CREATING PR COMMENT")
319356
ym = datetime.today().strftime('%Y.%m')
320357
pr_number = 1
321-
job = Job(tmpdir, "test/architecture", "EESSI", "--speed-up", ym, pr_number, "fpga/magic", "user01")
358+
job = Job(tmp_path, "test/architecture", "EESSI", "--speed-up", ym, pr_number, "fpga/magic", "user01")
322359
build_params = EESSIBotBuildParams("arch=amd/zen4,accel=nvidia/cc90")
323360

324361
job_id = "123"
@@ -337,15 +374,14 @@ def test_create_pr_comment_succeeds_none(monkeypatch, mocked_github, tmpdir):
337374
@pytest.mark.repo_name("EESSI/software-layer")
338375
@pytest.mark.pr_number(1)
339376
@pytest.mark.create_raises("1")
340-
def test_create_pr_comment_raises_once_then_succeeds(monkeypatch, mocked_github, tmpdir):
377+
def test_create_pr_comment_raises_once_then_succeeds(monkeypatch, mocked_github, tmp_path):
341378
"""Tests for function create_pr_comment."""
342379
monkeypatch.setattr('tools.pr_comments.github', mocked_github)
343-
shutil.copyfile("tests/test_app.cfg", "app.cfg")
344380
# creating a PR comment
345381
print("CREATING PR COMMENT")
346382
ym = datetime.today().strftime('%Y.%m')
347383
pr_number = 1
348-
job = Job(tmpdir, "test/architecture", "EESSI", "--speed-up", ym, pr_number, "fpga/magic", "user01")
384+
job = Job(tmp_path, "test/architecture", "EESSI", "--speed-up", ym, pr_number, "fpga/magic", "user01")
349385
build_params = EESSIBotBuildParams("arch=amd/zen4,accel=nvidia/cc90")
350386

351387
job_id = "123"
@@ -364,15 +400,14 @@ def test_create_pr_comment_raises_once_then_succeeds(monkeypatch, mocked_github,
364400
@pytest.mark.repo_name("EESSI/software-layer")
365401
@pytest.mark.pr_number(1)
366402
@pytest.mark.create_raises("always_raise")
367-
def test_create_pr_comment_always_raises(monkeypatch, mocked_github, tmpdir):
403+
def test_create_pr_comment_always_raises(monkeypatch, mocked_github, tmp_path):
368404
"""Tests for function create_pr_comment."""
369405
monkeypatch.setattr('tools.pr_comments.github', mocked_github)
370-
shutil.copyfile("tests/test_app.cfg", "app.cfg")
371406
# creating a PR comment
372407
print("CREATING PR COMMENT")
373408
ym = datetime.today().strftime('%Y.%m')
374409
pr_number = 1
375-
job = Job(tmpdir, "test/architecture", "EESSI", "--speed-up", ym, pr_number, "fpga/magic", "user01")
410+
job = Job(tmp_path, "test/architecture", "EESSI", "--speed-up", ym, pr_number, "fpga/magic", "user01")
376411
build_params = EESSIBotBuildParams("arch=amd/zen4,accel=nvidia/cc90")
377412

378413
job_id = "123"
@@ -392,15 +427,14 @@ def test_create_pr_comment_always_raises(monkeypatch, mocked_github, tmpdir):
392427
@pytest.mark.repo_name("EESSI/software-layer")
393428
@pytest.mark.pr_number(1)
394429
@pytest.mark.create_raises("3")
395-
def test_create_pr_comment_three_raises(monkeypatch, mocked_github, tmpdir):
430+
def test_create_pr_comment_three_raises(monkeypatch, mocked_github, tmp_path):
396431
"""Tests for function create_pr_comment."""
397432
monkeypatch.setattr('tools.pr_comments.github', mocked_github)
398-
shutil.copyfile("tests/test_app.cfg", "app.cfg")
399433
# creating a PR comment
400434
print("CREATING PR COMMENT")
401435
ym = datetime.today().strftime('%Y.%m')
402436
pr_number = 1
403-
job = Job(tmpdir, "test/architecture", "EESSI", "--speed-up", ym, pr_number, "fpga/magic", "user01")
437+
job = Job(tmp_path, "test/architecture", "EESSI", "--speed-up", ym, pr_number, "fpga/magic", "user01")
404438
build_params = EESSIBotBuildParams("arch=amd/zen4,accel=nvidia/cc90")
405439

406440
job_id = "123"
@@ -418,12 +452,12 @@ def test_create_pr_comment_three_raises(monkeypatch, mocked_github, tmpdir):
418452

419453
@pytest.mark.repo_name("test_repo")
420454
@pytest.mark.pr_number(999)
421-
def test_create_read_metadata_file(mocked_github, tmpdir):
455+
def test_create_read_metadata_file(mocked_github, tmp_path):
422456
"""Tests for function create_metadata_file."""
423457
# create some test data
424458
ym = datetime.today().strftime('%Y.%m')
425459
pr_number = 999
426-
job = Job(tmpdir, "test/architecture", "EESSI", "--speed_up_job", ym, pr_number, "fpga/magic", "user01")
460+
job = Job(tmp_path, "test/architecture", "EESSI", "--speed_up_job", ym, pr_number, "fpga/magic", "user01")
427461

428462
job_id = "123"
429463

@@ -432,7 +466,7 @@ def test_create_read_metadata_file(mocked_github, tmpdir):
432466
create_metadata_file(job, job_id, pr_comment)
433467

434468
expected_file = f"_bot_job{job_id}.metadata"
435-
expected_file_path = os.path.join(tmpdir, expected_file)
469+
expected_file_path = os.path.join(tmp_path, expected_file)
436470
# assert expected_file exists
437471
assert os.path.exists(expected_file_path)
438472

@@ -455,7 +489,7 @@ def test_create_read_metadata_file(mocked_github, tmpdir):
455489
assert sorted(metadata["PR"].keys()) == ["job_owner", "pr_comment_id", "pr_number", "repo"]
456490

457491
# use directory that does not exist
458-
dir_does_not_exist = os.path.join(tmpdir, "dir_does_not_exist")
492+
dir_does_not_exist = os.path.join(tmp_path, "dir_does_not_exist")
459493
job2 = Job(dir_does_not_exist, "test/architecture", "EESSI", "--speed_up_job", ym, pr_number, "fpga/magic",
460494
"user01")
461495
job_id2 = "222"
@@ -475,12 +509,12 @@ def test_create_read_metadata_file(mocked_github, tmpdir):
475509

476510
# use undefined values for parameters
477511
# job_id = None
478-
job4 = Job(tmpdir, "test/architecture", "EESSI", "--speed_up_job", ym, pr_number, "fpga/magic", "user01")
512+
job4 = Job(tmp_path, "test/architecture", "EESSI", "--speed_up_job", ym, pr_number, "fpga/magic", "user01")
479513
job_id4 = None
480514
create_metadata_file(job4, job_id4, pr_comment)
481515

482516
expected_file4 = f"_bot_job{job_id}.metadata"
483-
expected_file_path4 = os.path.join(tmpdir, expected_file4)
517+
expected_file_path4 = os.path.join(tmp_path, expected_file4)
484518
# assert expected_file exists
485519
assert os.path.exists(expected_file_path4)
486520

0 commit comments

Comments
 (0)