From 671e91970d3cbc0149a03d5341d0fc7a5da7b642 Mon Sep 17 00:00:00 2001 From: vsivanandharao_expedia Date: Sun, 5 Jan 2025 14:02:14 -0600 Subject: [PATCH] Add functional tests and GH workflows for it --- .github/workflows/functional-tests.yaml | 45 +++++++++++++++++++++++ pyarchitecture/memory/windows.py | 6 ++++ tests/functional.py | 47 +++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 .github/workflows/functional-tests.yaml create mode 100644 tests/functional.py diff --git a/.github/workflows/functional-tests.yaml b/.github/workflows/functional-tests.yaml new file mode 100644 index 0000000..3901d61 --- /dev/null +++ b/.github/workflows/functional-tests.yaml @@ -0,0 +1,45 @@ +name: functional-tests + +on: + push: + branches: + - main + paths: + - '**/*.py' + workflow_dispatch: + +jobs: + functional-tests: + strategy: + matrix: + platform: + - release_for: linux-amd64 + - release_for: darwin-amd64 + - release_for: windows-amd64 + name: Test functionality on ${{ matrix.platform.release_for }} + runs-on: + - thevickypedia-default + - ${{ matrix.platform.release_for }} + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Python + if: matrix.platform.release_for != 'darwin-amd64' + uses: actions/setup-python@v5 + with: + python-version: '3.x' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + export PYTHONPATH="$(pwd):$PYTHONPATH" + python -m pip install . + shell: bash + + - name: Run functional tests + run: python functional.py + working-directory: tests + shell: bash diff --git a/pyarchitecture/memory/windows.py b/pyarchitecture/memory/windows.py index 1f0d9da..ff9a951 100644 --- a/pyarchitecture/memory/windows.py +++ b/pyarchitecture/memory/windows.py @@ -10,6 +10,8 @@ class MEMORYSTATUSEX(ctypes.Structure): >>> MEMORYSTATUSEX + References: + https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-memorystatusex """ _fields_ = [ @@ -50,6 +52,10 @@ def get_memory_info(_: str) -> Dict[str, int]: used = total - available # Virtual memory + # It is tied to the addressable memory space of your operating system and CPU architecture + # It is not tied to the physical memory installed on your system + # For example, a 64-bit processor can theoretically address 2^64 bytes of memory (16 exabytes) + # Practical limits are imposed by the OS. So, Windows typically uses a 48-bit address space, corresponding to 128 TB virtual_total = memory_status.ullTotalVirtual virtual_available = memory_status.ullAvailVirtual diff --git a/tests/functional.py b/tests/functional.py new file mode 100644 index 0000000..8c49ca3 --- /dev/null +++ b/tests/functional.py @@ -0,0 +1,47 @@ +import platform +from typing import Set, NoReturn + +import pyarchitecture + + +def assert_disks(valid_keys: Set[str]) -> None | NoReturn: + """Assert disks.""" + disk_info = pyarchitecture.disks.get_all_disks() + assert disk_info + for disk in disk_info: + assert set(disk.keys()) == valid_keys + # No assertion for values since they can be null in VMs + + +def assert_cpu() -> None | NoReturn: + """Assert CPU output.""" + assert pyarchitecture.cpu.get_cpu_info() + + +def assert_memory(valid_keys: Set[str]) -> None | NoReturn: + mem_info = pyarchitecture.memory.get_memory_info() + assert set(mem_info.keys()) == valid_keys, f"{set(mem_info.keys())} != {valid_keys}" + assert all(mem_info.values()) + + +def main() -> None | NoReturn: + """Main entrypoint.""" + system = platform.system().lower() + disk_keys = {"name", "size", "device_id", "mountpoints"} + memory_keys = {"total", "free", "used", "available"} + if system == "darwin": + disk_keys.add("node") + memory_keys.update(("swap_total", "swap_used", "swap_free")) + if system == "linux": + memory_keys.update(("swap_total", "swap_used", "swap_free")) + if system == "windows": + # Windows doesn't have a distinction between free and available memory + memory_keys.remove("free") + memory_keys.update(("virtual_total", "virtual_available")) + assert_disks(disk_keys) + assert_cpu() + assert_memory(memory_keys) + + +if __name__ == '__main__': + main()