Skip to content

Commit

Permalink
Autoupdate oiejq (#222)
Browse files Browse the repository at this point in the history
* Autoupdate oiejq

* Add tests

* Fix tests

* Another fix

* Update sio2jail version

* Remove unnecessary try-catch

* Check if correct oiejq is installed

* Change oiejq install message

* Fix message

* Add test for oiejq reinstall
  • Loading branch information
MasloMaslane authored Apr 21, 2024
1 parent 9614701 commit 6cafe41
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/sinol_make/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def configure_parsers():

def check_oiejq():
if util.is_linux() and not oiejq.check_oiejq():
print(util.warning('`oiejq` in `~/.local/bin/` not found, installing now...'))
print(util.warning('Up to date `oiejq` in `~/.local/bin/` not found, installing new version...'))
try:
if oiejq.install_oiejq():
print(util.info('`oiejq` was successfully installed.'))
Expand Down
28 changes: 20 additions & 8 deletions src/sinol_make/oiejq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@ def _check_if_oiejq_executable(path):
if not os.access(path, os.X_OK):
return False

try:
p = subprocess.Popen([path], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.wait()
return p.returncode == 0
except FileNotFoundError:
return False
oiejq = subprocess.Popen([path], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
oiejq.wait()
return oiejq.returncode == 0


def _check_sio2jail(path):
sio2jail = subprocess.Popen(path + " --version", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, _ = sio2jail.communicate()
return out == (b"SIO2jail v1.5.0 compiled on Apr 15 2024 12:34:31 Linux 6.1.0-20-amd64 with gcc 10.2.1 20210110\n"
b"libseccomp 2.5.4\n")


def _check_oiejq(path):
return util.get_file_md5(path) == '7225efe59cb3052fa533b9fbc9ebf099'


def check_oiejq(path = None):
Expand All @@ -29,9 +38,12 @@ def check_oiejq(path = None):
return False

if path is not None:
return _check_if_oiejq_executable(path)
return _check_if_oiejq_executable(path) and _check_sio2jail(os.path.join(os.path.dirname(path), 'sio2jail')) \
and _check_oiejq(path)

if _check_if_oiejq_executable(os.path.expanduser('~/.local/bin/oiejq')):
if _check_if_oiejq_executable(os.path.expanduser('~/.local/bin/oiejq')) and \
_check_sio2jail(os.path.expanduser('~/.local/bin/sio2jail')) and \
_check_oiejq(os.path.expanduser('~/.local/bin/oiejq')):
return True
else:
return False
Expand Down
45 changes: 41 additions & 4 deletions tests/test_oiejq.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import shutil
import sys
from urllib.request import urlretrieve
import pytest

from sinol_make import oiejq, util
Expand All @@ -15,7 +16,7 @@ def test_install_oiejq():
os.remove(os.path.expanduser('~/.local/bin/oiejq'))
os.remove(os.path.expanduser('~/.local/bin/sio2jail'))
except IsADirectoryError:
shutil.rmtree(os.path.expanduser('~/.local/bin/oiejq'), ignore_errors=True)
shutil.rmtree(os.path.expanduser('~/.local/bin/oiejq'), ignore_errors=True)
except FileNotFoundError:
pass
assert not oiejq.check_oiejq()
Expand All @@ -27,12 +28,20 @@ def test_install_oiejq():
os.remove(os.path.expanduser('~/.local/bin/sio2jail'))
except FileNotFoundError:
pass

assert not oiejq.check_oiejq()
os.makedirs(os.path.expanduser('~/.local/bin/oiejq'))
with pytest.raises(SystemExit):
oiejq.install_oiejq()

# Test if oiejq is reinstalled when oiejq.sh is changed
os.rmdir(os.path.expanduser('~/.local/bin/oiejq'))
oiejq.install_oiejq()
with open(os.path.expanduser('~/.local/bin/oiejq'), 'a') as f:
f.write('\n')
assert not oiejq.check_oiejq()
oiejq.install_oiejq()


@pytest.mark.github_runner
def test_check_oiejq():
Expand All @@ -43,7 +52,7 @@ def test_check_oiejq():
os.remove(os.path.expanduser('~/.local/bin/oiejq'))
os.remove(os.path.expanduser('~/.local/bin/sio2jail'))
except IsADirectoryError:
shutil.rmtree(os.path.expanduser('~/.local/bin/oiejq'), ignore_errors=True)
shutil.rmtree(os.path.expanduser('~/.local/bin/oiejq'), ignore_errors=True)
except FileNotFoundError:
pass

Expand All @@ -58,7 +67,7 @@ def test_check_oiejq():
assert not oiejq.check_oiejq()
with open(os.path.expanduser('~/.local/bin/oiejq'), 'w') as f:
f.write('#!/bin/bash\necho "test"')
assert oiejq.check_oiejq()
assert oiejq._check_if_oiejq_executable(os.path.expanduser('~/.local/bin/oiejq'))


@pytest.mark.github_runner
Expand All @@ -69,6 +78,7 @@ def test_perf_counters_not_set():
if sys.platform != 'linux':
return

oiejq.install_oiejq()
with pytest.raises(SystemExit):
oiejq.check_perf_counters_enabled()

Expand All @@ -81,3 +91,30 @@ def test_perf_counters_set():
if not util.is_linux():
return
oiejq.check_perf_counters_enabled()


@pytest.mark.github_runner
def test_updating():
"""
Test updating oiejq
"""
if sys.platform != 'linux':
return
try:
os.remove(os.path.expanduser('~/.local/bin/oiejq'))
os.remove(os.path.expanduser('~/.local/bin/sio2jail'))
except IsADirectoryError:
shutil.rmtree(os.path.expanduser('~/.local/bin/oiejq'), ignore_errors=True)
except FileNotFoundError:
pass
assert not oiejq.check_oiejq()
assert oiejq.install_oiejq()
assert oiejq.get_oiejq_path() == os.path.expanduser('~/.local/bin/oiejq')

# Download older sio2jail
urlretrieve('https://github.com/sio2project/sio2jail/releases/download/v1.4.3/sio2jail',
os.path.expanduser('~/.local/bin/sio2jail'))
os.chmod(os.path.expanduser('~/.local/bin/sio2jail'), 0o777)
assert not oiejq.check_oiejq()
assert oiejq.install_oiejq()
assert oiejq.check_oiejq()

0 comments on commit 6cafe41

Please sign in to comment.