diff --git a/.github/workflows/test.yml b/.github/workflows/test-eessi.yml similarity index 100% rename from .github/workflows/test.yml rename to .github/workflows/test-eessi.yml diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 0000000..5fa7f2d --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,30 @@ +# documentation: https://help.github.com/en/articles/workflow-syntax-for-github-actions +name: Unit tests +on: [push, pull_request, workflow_dispatch] +permissions: + contents: read # to fetch code (actions/checkout) +jobs: + ubuntu-minimal: + runs-on: ubuntu-22.04 + strategy: + matrix: + python: ['3.9', '3.10', '3.11', '3.12', '3.13', '3.14'] + steps: + - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 + - name: set up Python + uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0 + with: + python-version: ${{matrix.python}} + + - name: Install eessi command line tool + id: install + run: | + pip install . + + - name: Install pytest + run: | + pip install pytest + + - name: Run tests + run: | + pytest diff --git a/src/eessi/cli/check.py b/src/eessi/cli/check.py index b9cebb7..7c296e4 100644 --- a/src/eessi/cli/check.py +++ b/src/eessi/cli/check.py @@ -102,14 +102,19 @@ def get_repo_attribute(repo: str, key: str): Get repository attribute """ repo_path = os.path.join(CVMFS_ROOT, repo) + + value = UNKNOWN + (stdout, stderr, exit_code) = run_cmd(f"attr -g {key} {repo_path}") if exit_code == 0: - # expected output is something like: - # Attribute "revision" had a 5 byte value for /cvmfs/software.eessi.io: - # 13972 - value = stdout.splitlines()[-1] - else: - value = UNKNOWN + # Expected output is something like: + # Attribute "revision" had a 5 byte value for /cvmfs/software.eessi.io: + # 13972 + # + # If there's no value found ('0 byte value'), then last line is *empty* + regex = re.compile("had a [1-9][0-9]* byte value", re.M) + if regex.search(stdout): + value = stdout.splitlines()[-1].strip() return value diff --git a/tests/test_check.py b/tests/test_check.py new file mode 100644 index 0000000..4e53184 --- /dev/null +++ b/tests/test_check.py @@ -0,0 +1,25 @@ +import eessi.cli.check +from eessi.cli.check import UNKNOWN, get_repo_attribute + + +def test_get_repo_attribute(monkeypatch): + """ + Tests for get_repo_attribute + """ + + out = '\n'.join([ + 'Attribute "revision" had a 5 byte value for /cvmfs/software.eessi.io:', + '13972', + ]) + monkeypatch.setattr(eessi.cli.check, 'run_cmd', lambda x: (out, '', 0)) + res = get_repo_attribute('software.eessi.io', 'revision') + assert res == '13972' + + # check that UNKNOWN is returned when attr returns '0 byte value' with empty last line + out = '\n'.join([ + 'Attribute "ncleanup24" had a 0 byte value for /cvmfs/software.eessi.io:', + '', + ]) + monkeypatch.setattr(eessi.cli.check, 'run_cmd', lambda x: (out, '', 0)) + res = get_repo_attribute('software.eessi.io', 'ncleanup24') + assert res == UNKNOWN