From eca9868ab35c95377598d08ed1179a542c4b3dc8 Mon Sep 17 00:00:00 2001 From: "Rojas Chaves, Jose" Date: Thu, 12 Jun 2025 22:59:23 +0000 Subject: [PATCH 1/8] Adding test framework --- HEADER | 2 +- .../hec-assembler-tools/.coveragerc | 9 ++ .../hec-assembler-tools/pytest.ini | 4 + .../hec-assembler-tools/tests/conftest.py | 36 +++++ .../tests/unit_tests/test_he_prep.py | 146 ++++++++++++++++++ 5 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 assembler_tools/hec-assembler-tools/.coveragerc create mode 100644 assembler_tools/hec-assembler-tools/pytest.ini create mode 100644 assembler_tools/hec-assembler-tools/tests/conftest.py create mode 100644 assembler_tools/hec-assembler-tools/tests/unit_tests/test_he_prep.py diff --git a/HEADER b/HEADER index a0410374..bd8b8ebc 100644 --- a/HEADER +++ b/HEADER @@ -1,2 +1,2 @@ -Copyright (C) 2024 Intel Corporation +Copyright (C) 2025 Intel Corporation SPDX-License-Identifier: Apache-2.0 diff --git a/assembler_tools/hec-assembler-tools/.coveragerc b/assembler_tools/hec-assembler-tools/.coveragerc new file mode 100644 index 00000000..80e80098 --- /dev/null +++ b/assembler_tools/hec-assembler-tools/.coveragerc @@ -0,0 +1,9 @@ +[run] +branch = True +source = . +omit = + tests/* + +[report] +show_missing = True +skip_covered = True diff --git a/assembler_tools/hec-assembler-tools/pytest.ini b/assembler_tools/hec-assembler-tools/pytest.ini new file mode 100644 index 00000000..be1763fa --- /dev/null +++ b/assembler_tools/hec-assembler-tools/pytest.ini @@ -0,0 +1,4 @@ +[pytest] +pythonpath = . +testpaths = tests +addopts = --cov=. diff --git a/assembler_tools/hec-assembler-tools/tests/conftest.py b/assembler_tools/hec-assembler-tools/tests/conftest.py new file mode 100644 index 00000000..5f7b42e1 --- /dev/null +++ b/assembler_tools/hec-assembler-tools/tests/conftest.py @@ -0,0 +1,36 @@ +# Copyright (C) 2025 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +""" +Pytest configuration and fixtures for assembler_tools tests. +""" + +import os +import pytest +from assembler.spec_config.isa_spec import ISASpecConfig +from assembler.spec_config.mem_spec import MemSpecConfig + + +@pytest.fixture(scope="session", autouse=True) +def initialize_specs(): + """ + Fixture to initialize ISA and memory specifications for test session. + + This fixture is automatically used for the entire test session and ensures that + the ISA and memory specifications are initialized before any tests are run. + It determines the module directory relative to the current file and calls the + initialization methods for both ISASpecConfig and MemSpecConfig. + + Note: + This fixture is intended to be run from the assembler root directory. + + Yields: + None + + Raises: + Any exceptions raised by ISASpecConfig.initialize_isa_spec or + MemSpecConfig.initialize_mem_spec will propagate. + """ + module_dir = os.path.dirname(os.path.dirname(__file__)) + ISASpecConfig.initialize_isa_spec(module_dir, "") + MemSpecConfig.initialize_mem_spec(module_dir, "") diff --git a/assembler_tools/hec-assembler-tools/tests/unit_tests/test_he_prep.py b/assembler_tools/hec-assembler-tools/tests/unit_tests/test_he_prep.py new file mode 100644 index 00000000..e67823e8 --- /dev/null +++ b/assembler_tools/hec-assembler-tools/tests/unit_tests/test_he_prep.py @@ -0,0 +1,146 @@ +# Copyright (C) 2025 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +""" +Unit tests for he_prep module. +""" + +from unittest import mock +import os +import sys +import pathlib +import pytest +import he_prep + + +def test_main_assigns_and_saves(monkeypatch, tmp_path): + """ + Test that the main function assigns register banks, processes instructions, and saves the output. + + This test uses monkeypatching to mock dependencies and verifies that the output file + contains the expected instruction after processing a dummy input file. + """ + # Prepare dummy input file + input_file = tmp_path / "input.csv" + input_file.write_text("dummy") + output_file = tmp_path / "output.csv" + + dummy_model = object() + dummy_insts = [mock.Mock(toPISAFormat=mock.Mock(return_value="inst1"))] + + monkeypatch.setattr(he_prep, "MemoryModel", mock.Mock(return_value=dummy_model)) + monkeypatch.setattr( + he_prep.preprocessor, + "preprocessPISAKernelListing", + mock.Mock(return_value=dummy_insts), + ) + monkeypatch.setattr(he_prep.preprocessor, "assignRegisterBanksToVars", mock.Mock()) + + he_prep.main(str(output_file), str(input_file), b_verbose=False) + # Output file should contain the instruction + assert output_file.read_text().strip() == "inst1" + + +def test_main_no_input_file(): + """ + Test that main raises an error when no input file is provided. + """ + with pytest.raises(FileNotFoundError): + he_prep.main( + "", "", b_verbose=False + ) # Should raise an error due to missing input file + + +def test_main_no_output_file(): + """ + Test that main raises an error when no output file is provided. + """ + with pytest.raises(FileNotFoundError): + he_prep.main( + "", "input.csv", b_verbose=False + ) # Should raise an error due to missing output file + + +def test_main_no_instructions(monkeypatch): + """ + Test that main handles the case where no instructions are processed. + + This test checks that the function can handle an empty instruction list without errors. + """ + input_file = "empty_input.csv" + output_file = "empty_output.csv" + + with open(input_file, "w", encoding="utf-8") as f: + f.write("") # Create an empty input file + + dummy_model = object() + monkeypatch.setattr(he_prep, "MemoryModel", mock.Mock(return_value=dummy_model)) + monkeypatch.setattr( + he_prep.preprocessor, "preprocessPISAKernelListing", mock.Mock(return_value=[]) + ) + monkeypatch.setattr(he_prep.preprocessor, "assignRegisterBanksToVars", mock.Mock()) + + he_prep.main(output_file, input_file, b_verbose=False) + + # Output file should be empty + output_file_path = pathlib.Path(output_file) + assert ( + not output_file_path.exists() + or output_file_path.read_text(encoding="utf-8").strip() == "" + ) + + +def test_main_invalid_input_file(tmp_path): + """ + Test that main raises an error when the input file does not exist. + """ + input_file = tmp_path / "non_existent.csv" + output_file = tmp_path / "output.csv" + + with pytest.raises(FileNotFoundError): + he_prep.main( + str(output_file), str(input_file), b_verbose=False + ) # Should raise an error due to missing input file + + +def test_main_invalid_output_file(tmp_path): + """ + Test that main raises an error when the output file cannot be created. + This test checks that the function handles file permission errors gracefully. + """ + input_file = tmp_path / "input.csv" + input_file.write_text("") # Write empty string to avoid SyntaxError + output_file = tmp_path / "output.csv" + + # Make the output file read-only + output_file.touch() + os.chmod(output_file, 0o444) # Read-only permissions + + with pytest.raises(PermissionError): + he_prep.main( + str(output_file), str(input_file), b_verbose=False + ) # Should raise an error due to permission issues + + +def test_parse_args(): + """ + Test that parse_args returns the expected arguments. + """ + test_args = [ + "prog", + "input.csv", + "output.csv", + "--isa_spec", + "isa.json", + "--mem_spec", + "mem.json", + "--verbose", + ] + with mock.patch.object(sys, "argv", test_args): + args = he_prep.parse_args() + + assert args.output_file_name == "output.csv" + assert args.input_file_name == "input.csv" + assert args.isa_spec_file == "isa.json" + assert args.mem_spec_file == "mem.json" + assert args.verbose == 1 From 1abea58dbdbe18c7e58f6232a4b66c1ba56096e1 Mon Sep 17 00:00:00 2001 From: "Rojas Chaves, Jose" Date: Thu, 12 Jun 2025 23:23:51 +0000 Subject: [PATCH 2/8] Fixing github actions --- .github/workflows/format.yml | 3 ++ .github/workflows/hec-asm-test.yml | 44 ++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 .github/workflows/hec-asm-test.yml diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index b354139b..72af3c30 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -23,6 +23,9 @@ jobs: - name: Install pre-commit run: pip install pre-commit + - name: Install pylint + run: pip install pylint + - name: Fetch main branch for diff run: git fetch origin main diff --git a/.github/workflows/hec-asm-test.yml b/.github/workflows/hec-asm-test.yml new file mode 100644 index 00000000..82103956 --- /dev/null +++ b/.github/workflows/hec-asm-test.yml @@ -0,0 +1,44 @@ +name: HEC Assembler Tools Tests +on: + push: + branches: [main] + paths: + - 'encrypted-computing-sdk/assembler_tools/hec-assembler-tools/**' + pull_request: + branches: [main] + paths: + - 'encrypted-computing-sdk/assembler_tools/hec-assembler-tools/**' + +permissions: + contents: read + +jobs: + test: + runs-on: ubuntu-latest + defaults: + run: + working-directory: encrypted-computing-sdk/assembler_tools/hec-assembler-tools + steps: + - uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.10' + + - name: Install dependencies + run: | + pip install -r requirements.txt + pip install pytest + + - name: Check for changes in hec-assembler-tools + id: changes + uses: dorny/paths-filter@v3 + with: + filters: | + module: + - '**' + + - name: Run unit tests + if: steps.changes.outputs.module == 'true' + run: pytest tests From 139ca04f131b21fbcc86492a539b3df0da840b6b Mon Sep 17 00:00:00 2001 From: "Rojas Chaves, Jose" Date: Thu, 12 Jun 2025 23:33:09 +0000 Subject: [PATCH 3/8] Adding pytest dependency --- assembler_tools/hec-assembler-tools/requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/assembler_tools/hec-assembler-tools/requirements.txt b/assembler_tools/hec-assembler-tools/requirements.txt index 573a57b6..7f7ad59d 100644 --- a/assembler_tools/hec-assembler-tools/requirements.txt +++ b/assembler_tools/hec-assembler-tools/requirements.txt @@ -8,6 +8,7 @@ networkx==3.0 numpy==1.24.2 packaging==23.0 Pillow==10.3.0 +pytest==8.4.0 python-dateutil==2.8.2 PyYAML==6.0 six==1.16.0 From 44222e9fe071b39ef1cdf8219c8a8cd670742ea7 Mon Sep 17 00:00:00 2001 From: "Rojas Chaves, Jose" Date: Thu, 12 Jun 2025 23:43:06 +0000 Subject: [PATCH 4/8] Ficing actions --- .github/workflows/format.yml | 3 +++ .github/workflows/hec-asm-test.yml | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 72af3c30..83e4a1bd 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -20,6 +20,9 @@ jobs: with: python-version: '3.10' + - name: Install dependencies + run: pip install -r assembler_tools/hec-assembler-tools/requirements.txt + - name: Install pre-commit run: pip install pre-commit diff --git a/.github/workflows/hec-asm-test.yml b/.github/workflows/hec-asm-test.yml index 82103956..95a5ffaa 100644 --- a/.github/workflows/hec-asm-test.yml +++ b/.github/workflows/hec-asm-test.yml @@ -3,11 +3,11 @@ on: push: branches: [main] paths: - - 'encrypted-computing-sdk/assembler_tools/hec-assembler-tools/**' + - 'encrypted-computing-sdk/assembler_tools/hec-assembler-tools/**' pull_request: branches: [main] paths: - - 'encrypted-computing-sdk/assembler_tools/hec-assembler-tools/**' + - 'encrypted-computing-sdk/assembler_tools/hec-assembler-tools/**' permissions: contents: read From 348cd04e6f10b5b8d2af109667bf1b436e442f85 Mon Sep 17 00:00:00 2001 From: "Rojas Chaves, Jose" Date: Thu, 12 Jun 2025 23:45:30 +0000 Subject: [PATCH 5/8] Ficing actions --- .github/workflows/hec-asm-test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/hec-asm-test.yml b/.github/workflows/hec-asm-test.yml index 95a5ffaa..a24c17f3 100644 --- a/.github/workflows/hec-asm-test.yml +++ b/.github/workflows/hec-asm-test.yml @@ -3,11 +3,11 @@ on: push: branches: [main] paths: - - 'encrypted-computing-sdk/assembler_tools/hec-assembler-tools/**' + - 'assembler_tools/hec-assembler-tools/**' pull_request: branches: [main] paths: - - 'encrypted-computing-sdk/assembler_tools/hec-assembler-tools/**' + - 'assembler_tools/hec-assembler-tools/**' permissions: contents: read From 6ca04d2fa05913f8171d639eb8f957f9ed822930 Mon Sep 17 00:00:00 2001 From: "Rojas Chaves, Jose" Date: Thu, 12 Jun 2025 23:48:02 +0000 Subject: [PATCH 6/8] Fixing path on test action --- .github/workflows/hec-asm-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/hec-asm-test.yml b/.github/workflows/hec-asm-test.yml index a24c17f3..de2c1f3f 100644 --- a/.github/workflows/hec-asm-test.yml +++ b/.github/workflows/hec-asm-test.yml @@ -17,7 +17,7 @@ jobs: runs-on: ubuntu-latest defaults: run: - working-directory: encrypted-computing-sdk/assembler_tools/hec-assembler-tools + working-directory: assembler_tools/hec-assembler-tools steps: - uses: actions/checkout@v4 From 6e2f8a8c32c4ec5c92b107a300102050ff499b6d Mon Sep 17 00:00:00 2001 From: "Rojas Chaves, Jose" Date: Thu, 12 Jun 2025 23:52:11 +0000 Subject: [PATCH 7/8] Remove coverage reporting from action --- assembler_tools/hec-assembler-tools/pytest.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assembler_tools/hec-assembler-tools/pytest.ini b/assembler_tools/hec-assembler-tools/pytest.ini index be1763fa..06e92c68 100644 --- a/assembler_tools/hec-assembler-tools/pytest.ini +++ b/assembler_tools/hec-assembler-tools/pytest.ini @@ -1,4 +1,4 @@ [pytest] pythonpath = . testpaths = tests -addopts = --cov=. +#addopts = --cov=. From 87d6a6027074f6a5d14c7e4b76a8e46990b6ff30 Mon Sep 17 00:00:00 2001 From: christopherngutierrez Date: Thu, 12 Jun 2025 17:35:47 -0700 Subject: [PATCH 8/8] added path to pytest.ini --- pytest.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest.ini b/pytest.ini index 078e36d7..e369b13e 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,2 +1,2 @@ [pytest] -pythonpath = p-isa_tools/kerngen +pythonpath = p-isa_tools/kerngen assembler_tools/hec-assembler-tools