Skip to content

Commit

Permalink
Improve unittest (PyTest, add perf, add Selenium) #2757
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolargo committed Dec 28, 2024
2 parents ef602ac + 1624e66 commit 8fb49d9
Show file tree
Hide file tree
Showing 28 changed files with 336 additions and 128 deletions.
34 changes: 18 additions & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ jobs:
with:
args: 'check'

- name: Static type check
run: |
echo "Skipping static type check for the moment, too much error...";
# pip install pyright
# pyright glances
# - name: Static type check
# run: |
# echo "Skipping static type check for the moment, too much error...";
# # pip install pyright
# # pyright glances


test-linux:

needs: source-code-checks
# https://github.com/actions/runner-images?tab=readme-ov-file#available-images
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
Expand All @@ -52,11 +52,12 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
python -m pip install pytest
if [ -f requirements.txt ]; then python -m pip install -r requirements.txt; fi
- name: Unitary tests
run: |
python ./unittest-core.py
python -m pytest ./tests/test_core.py
# Error appear with h11, not related to Glances
# Should be tested if correction is done
Expand All @@ -69,9 +70,7 @@ jobs:
# runs-on: windows-2022
# strategy:
# matrix:
# # Python version "3.12" introduce this issue:
# # https://github.com/nicolargo/glances/actions/runs/6439648370/job/17487567454
# python-version: ["3.8", "3.9", "3.10", "3.11"]
# python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
# steps:

# - uses: actions/checkout@v4
Expand All @@ -85,12 +84,13 @@ jobs:
# - name: Install dependencies
# run: |
# python -m pip install --upgrade pip
# pip install pytest
# if (Test-Path -PathType Leaf "requirements.txt") { python -m pip install -r requirements.txt }
# python setup.py install
# pip install .

# - name: Unitary tests
# run: |
# python ./unittest-core.py
# python -m pytest ./tests/test_core.py

test-macos:

Expand All @@ -115,11 +115,12 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
python -m pip install pytest
if [ -f requirements.txt ]; then python -m pip install -r requirements.txt; fi
- name: Unitary tests
run: |
python ./unittest-core.py
python -m pytest ./tests/test_core.py
# Error when trying to implement #2749
# pkg: No packages available to install matching 'py-pip' have been found in the repositories
Expand All @@ -139,5 +140,6 @@ jobs:
# pkg install -y python3 py-pip
# run: |
# set -e -x
# python3 -m pip install pytest
# python3 -m pip install --user -r requirements.txt
# python ./unittest-core.py
# python3 -m pytest ./tests/test_core.py
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ bower_components/
# Virtual env
/venv*/

# Test
.coverage
67 changes: 34 additions & 33 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
PORT ?= 8008
venv_full:= venv/bin
venv_dev := venv-dev/bin
venv_min := venv-min/bin
CONF := conf/glances.conf
PIP := $(venv_full)/pip
PYTHON := $(venv_full)/python
PYTEST := $(venv_full)/python -m pytest
LASTTAG = $(shell git describe --tags --abbrev=0)

VENV_TYPES := full min dev
VENV_TYPES := full min
VENV_PYTHON := $(VENV_TYPES:%=venv-%-python)
VENV_UPG := $(VENV_TYPES:%=venv-%-upgrade)
VENV_DEPS := $(VENV_TYPES:%=venv-%)
Expand All @@ -30,7 +30,7 @@ DOCKER_OPTS := --rm -e TZ="${TZ}" -e GLANCES_OPT="" --pid host --network h
# if the command is only `make`, the default tasks will be the printing of the help.
.DEFAULT_GOAL := help

.PHONY: help test docs docs-server venv venv-min venv-dev
.PHONY: help test docs docs-server venv venv-min

help: ## List all make commands available
@grep -E '^[\.a-zA-Z_%-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
Expand All @@ -53,7 +53,7 @@ endef
$(foreach TYPE,$(VENV_TYPES),$(eval $(DEFINE_VARS_FOR_TYPE)))

$(VENV_PYTHON): venv-%-python:
virtualenv -p /usr/bin/python3 $(if $(filter full,$*),venv,venv-$*)
virtualenv -p python3 $(if $(filter full,$*),venv,venv-$*)

$(VENV_INST_UPG): venv-%:
$(if $(UPGRADE),$(VIRTUAL_ENV)/pip install --upgrade pip,)
Expand All @@ -66,65 +66,66 @@ venv-upgrade: $(VENV_UPG) ## Upgrade all Python 3 dependencies

# For full installation (with optional dependencies)

venv-full venv-full-upgrade: REQS = requirements.txt optional-requirements.txt
venv-full venv-full-upgrade: REQS = requirements.txt optional-requirements.txt dev-requirements.txt doc-requirements.txt

venv-full-python: ## Install Python 3 venv
venv-full: venv-python ## Install Python 3 run-time
venv-full-upgrade: ## Upgrade Python 3 run-time dependencies
venv-full: PRE_COMMIT = 1

# For minimal installation (without optional dependencies)

venv-min venv-min-upgrade: REQS = requirements.txt
venv-min venv-min-upgrade: REQS = requirements.txt dev-requirements.txt doc-requirements.txt

venv-min-python: ## Install Python 3 venv minimal
venv-min: venv-min-python ## Install Python 3 minimal run-time dependencies
venv-min-upgrade: ## Upgrade Python 3 minimal run-time dependencies

# For development

venv-dev venv-dev-upgrade: REQS = dev-requirements.txt doc-requirements.txt
venv-dev: PRE_COMMIT = 1

venv-dev-python: ## Install Python 3 venv
venv-dev: venv-python ## Install Python 3 dev dependencies
venv-dev-upgrade: ## Upgrade Python 3 dev dependencies

# ===================================================================
# Tests
# ===================================================================

$(UNIT_TESTS): test-%: unittest-%.py
$(PYTHON) $<
test: ## Run All unit tests
$(PYTEST)

test-core: ## Run Core unit tests
$(PYTEST) tests/test_core.py

test-perf: ## Run Perf unit tests
$(PYTEST) tests/test_perf.py

test-restful: ## Run Restful API unit tests
$(PYTEST) tests/test_restful.py

test-core: ## Run core unit tests
test-restful: ## Run Restful unit tests
test-xmlrpc: ## Run XMLRPC unit tests
test-webui: ## Run WebUI unit tests
$(PYTEST) tests/test_webui.py

test: $(UNIT_TESTS) ## Run unit tests
test-xmlrpc: ## Run XMLRPC API unit tests
$(PYTEST) tests/test_xmlrpc.py

test-with-upgrade: venv-upgrade venv-dev-upgrade test ## Upgrade deps and run unit tests
test-with-upgrade: venv-upgrade test ## Upgrade deps and run unit tests

test-min: ## Run core unit tests in minimal environment
$(venv_min)/python unittest-core.py
$(venv_min)/python -m pytest tests/test_core.py

test-min-with-upgrade: venv-min-upgrade ## Upgrade deps and run unit tests in minimal environment
$(venv_min)/python unittest-core.py
$(venv_min)/python -m pytest tests/test_core.py

# ===================================================================
# Linters, profilers and cyber security
# ===================================================================

format: ## Format the code
$(venv_dev)/python -m ruff format .
$(venv_full)/python -m ruff format .

lint: ## Lint the code.
$(venv_dev)/python -m ruff check . --fix
$(venv_full)/python -m ruff check . --fix

codespell: ## Run codespell to fix common misspellings in text files
$(venv_dev)/codespell -S .git,./docs/_build,./Glances.egg-info,./venv*,./glances/outputs,*.svg -L hart,bu,te,statics -w
$(venv_full)/codespell -S .git,./docs/_build,./Glances.egg-info,./venv*,./glances/outputs,*.svg -L hart,bu,te,statics -w

semgrep: ## Run semgrep to find bugs and enforce code standards
$(venv_dev)/semgrep scan --config=auto
$(venv_full)/semgrep scan --config=auto

profiling-%: SLEEP = 3
profiling-%: TIMES = 30
Expand Down Expand Up @@ -167,12 +168,12 @@ memory-profiling: ## Profile memory usage
@echo "It's a very long test (~4 hours)..."
rm -f $(PROFILE)
@echo "1/2 - Start memory profiling with the history option enable"
$(venv_dev)/mprof run -T 1 -C run-venv.py -C $(CONF) --stop-after $(TIMES) --quiet
$(venv_dev)/mprof plot --output $(OUT_DIR)/glances-memory-profiling-with-history.png
$(venv_full)/mprof run -T 1 -C run-venv.py -C $(CONF) --stop-after $(TIMES) --quiet
$(venv_full)/mprof plot --output $(OUT_DIR)/glances-memory-profiling-with-history.png
rm -f $(PROFILE)
@echo "2/2 - Start memory profiling with the history option disable"
$(venv_dev)/mprof run -T 1 -C run-venv.py -C $(CONF) --disable-history --stop-after $(TIMES) --quiet
$(venv_dev)/mprof plot --output $(OUT_DIR)/glances-memory-profiling-without-history.png
$(venv_full)/mprof run -T 1 -C run-venv.py -C $(CONF) --disable-history --stop-after $(TIMES) --quiet
$(venv_full)/mprof plot --output $(OUT_DIR)/glances-memory-profiling-without-history.png
rm -f $(PROFILE)

# Trivy installation: https://aquasecurity.github.io/trivy/latest/getting-started/installation/
Expand Down Expand Up @@ -220,7 +221,7 @@ webui-audit-fix: ## Fix audit the Web UI
# Packaging
# ===================================================================

flatpak: venv-dev-upgrade ## Generate FlatPack JSON file
flatpak: venv-upgrade ## Generate FlatPack JSON file
git clone https://github.com/flatpak/flatpak-builder-tools.git
$(PYTHON) ./flatpak-builder-tools/pip/flatpak-pip-generator glances
rm -rf ./flatpak-builder-tools
Expand Down
4 changes: 4 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
codespell
coverage
fonttools>=4.43.0 # not directly required, pinned by Snyk to avoid a vulnerability
gprof2dot
matplotlib
Expand All @@ -8,7 +9,10 @@ pillow>=10.0.1 # not directly required, pinned by Snyk to avoid a vulnerability
pre-commit
py-spy
pyright
pytest
requirements-parser
ruff
selenium
semgrep
setuptools>=65.5.1 # not directly required, pinned by Snyk to avoid a vulnerability
webdriver-manager
Loading

0 comments on commit 8fb49d9

Please sign in to comment.