Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DCGM snap tests #4

Merged
merged 39 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
e347385
Update structure to comply with bootstack template
Deezzir Sep 6, 2024
89b10bd
Add install instructions to the README
Deezzir Sep 6, 2024
b531437
Add test structure and snap setup
Deezzir Sep 6, 2024
74f7c35
Update .gitignore
Deezzir Sep 6, 2024
ab9f502
Make dcgm-exporter into a daemon
Deezzir Sep 6, 2024
931c25d
Add simple smoke test for dcgm-exporter endpoint
Deezzir Sep 6, 2024
b4f5877
Update README.md
Deezzir Sep 6, 2024
36fd757
Fix format
Deezzir Sep 6, 2024
e257ad7
Remove unneccesary envar for dcgm-exporter daemon
Deezzir Sep 6, 2024
5d5047b
Un-omit the test directory for the additional coverage
Deezzir Sep 11, 2024
5be4161
Remove redundant unit test commands
Deezzir Sep 11, 2024
349ee7a
Make dcgm-exporter listen address configurable
Deezzir Sep 11, 2024
f53e647
Merge branch 'main' into main
Deezzir Sep 11, 2024
48a5059
Remove unittest from Makefile
Deezzir Sep 11, 2024
818bd7a
Improve dcgm-exporter endpoint test
Deezzir Sep 11, 2024
b0e1114
Merge branch 'main' into main
Deezzir Sep 11, 2024
c201332
Add missing trailing whitespace
Deezzir Sep 11, 2024
36a913f
Add simple tests for other components
Deezzir Sep 11, 2024
558007e
Improve dcgmi test
Deezzir Sep 12, 2024
9e24944
Revert README.md
Deezzir Sep 12, 2024
2577dfb
Switch to snap services subcommands
Deezzir Sep 12, 2024
4dfd250
Remane dcgm-exporter snap config
Deezzir Sep 12, 2024
bfb452a
Simplify dcgm-exporter test
Deezzir Sep 12, 2024
6c32bfc
Remove makefile and rename.sh
Deezzir Sep 12, 2024
19faa11
Imrpove & simplify tests
Deezzir Sep 12, 2024
9774fde
Add func test to the CI
Deezzir Sep 12, 2024
8738bc7
Fix check.yaml
Deezzir Sep 12, 2024
bea3f71
Fix check.yaml
Deezzir Sep 12, 2024
55874a9
Merge build and func CI jobs
Deezzir Sep 12, 2024
b740fa0
Remove redundant step for test job
Deezzir Sep 12, 2024
3138f0a
Revert CI to separate jobs for build and func
Deezzir Sep 12, 2024
c2ea073
Fix artifact path
Deezzir Sep 12, 2024
439376e
Refine
Deezzir Sep 13, 2024
60bdf85
Refinements
Deezzir Sep 13, 2024
76a8eee
Merge branch 'main' into main
Deezzir Sep 13, 2024
6987442
Refine tests and comments
Deezzir Sep 13, 2024
2ea70fd
Align check.yaml with check workflows
Deezzir Sep 13, 2024
4059a6d
Update comments for default configs
Deezzir Sep 13, 2024
bd6f22e
Revert back the bind configs after the test
Deezzir Sep 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
# This is a template `.gitignore` file for snaps
# This file is managed by bootstack-charms-spec and should not be modified
# within individual snap repos. https://launchpad.net/bootstack-charms-spec
aieri marked this conversation as resolved.
Show resolved Hide resolved

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Tests files and dir
.pytest_cache/
.coverage
.tox
.venv
reports/
**/report/
htmlcov/
.mypy_cache

# Log files
*.log
Expand All @@ -17,9 +30,14 @@ reports/
# version data
repo-info

# Python builds
deb_dist/
dist/
*.egg-info/

# Snaps
*.snap

# Builds
.build/
build/
build/
55 changes: 55 additions & 0 deletions Makefile
aieri marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# This is a template `Makefile` file for snaps
# This file is managed by bootstack-charms-spec and should not be modified
# within individual snap repos. https://launchpad.net/bootstack-charms-spec

PYTHON := /usr/bin/python3

PROJECTPATH=$(dir $(realpath ${MAKEFILE_LIST}))
SNAP_NAME=$(shell cat ${PROJECTPATH}/snap/snapcraft.yaml | grep -E '^name:' | awk '{print $$2}')
SNAP_FILE=${PROJECTPATH}/${SNAP_NAME}.snap

help:
@echo "This project supports the following targets"
@echo ""
@echo " make help - show this text"
@echo " make clean - remove unneeded files"
@echo " make build - build the snap"
@echo " make lint - run lint checkers"
@echo " make reformat - run lint tools to auto format code"
@echo " make unittests - run the tests defined in the unittest subdirectory"
Deezzir marked this conversation as resolved.
Show resolved Hide resolved
@echo " make functional - run the tests defined in the functional subdirectory"
@echo " make test - run lint, proof, unittests and functional targets"
@echo ""

lint:
@echo "Running lint checks"
@tox -e lint

unittests:
@echo "Running unit tests"
@tox -e unit -- ${UNIT_ARGS}

test: lint unittests functional
@echo "Tests completed for the snap."

reformat:
@echo "Reformat files with black and isort"
@tox -e reformat

build:
@echo "Building the snap"
@snapcraft --use-lxd
@bash -c ./rename.sh

clean:
@echo "Cleaning snap"
@snapcraft clean --use-lxd
@echo "Cleaning existing snap builds"
@rm -rf ${SNAP_FILE}

functional: build
@echo "Executing functional tests using built snap"
@TEST_SNAP=${SNAP_FILE} tox -e func -- ${FUNC_ARGS}

# The targets below don't depend on a file
.PHONY: help clean build lint reformat unittests functional test
Deezzir marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# dcgm-snap
# DCGM Snap

This is a snap delivering NVIDIA dcgm components.
The snap consists of [dcgm](https://developer.nvidia.com/dcgm) and [dcgm-exporter](https://github.com/NVIDIA/dcgm-exporter).
Expand All @@ -8,5 +8,12 @@ The snap consists of [dcgm](https://developer.nvidia.com/dcgm) and [dcgm-exporte
You can build the snap locally by using the command:

```shell
snapcraft --use-lxd
```
make build
```

## Install the snap

```shell
snap install --dangerous ./dcgm.snap
sudo systemctl enable snap.dcgm.dcgm-exporter.service --now # enable and start dcgm-exporter service
Deezzir marked this conversation as resolved.
Show resolved Hide resolved
```
66 changes: 66 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# This is a template `pyproject.toml` file for snaps
# This file is managed by bootstack-charms-spec and should not be modified
# within individual snap repos. https://launchpad.net/bootstack-charms-spec

[tool.flake8]
ignore = ["C901", "D100", "D101", "D102", "D103", "W503", "W504"]
exclude = ['.eggs', '.git', '.tox', '.venv', '.build', 'build', 'report']
max-line-length = 99
max-complexity = 10

[tool.black]
line-length = 99
exclude = '''
/(
| .eggs
| .git
| .tox
| .venv
| .build
| build
| report
)/
'''

[tool.isort]
profile = "black"
skip_glob = [
".eggs",
".git",
".tox",
".venv",
".build",
"build",
"report"
]

[tool.pylint]
max-line-length = 99
ignore = ['.eggs', '.git', '.tox', '.venv', '.build', 'report', 'tests']

[tool.mypy]
warn_unused_ignores = true
warn_unused_configs = true
warn_unreachable = true
disallow_untyped_defs = true
exclude = ['.eggs', '.git', '.tox', '.venv', '.build', 'report', 'tests']

## Ignore unsupported imports
[[tool.mypy.overrides]]
ignore_missing_imports = true
module = ["setuptools"]

[tool.coverage.run]
relative_files = true
source = ["."]
omit = ["tests/**", "docs/**", "lib/**", "snap/**", "build/**", "setup.py"]
Deezzir marked this conversation as resolved.
Show resolved Hide resolved

[tool.coverage.report]
fail_under = 100
show_missing = true

[tool.coverage.html]
directory = "tests/unit/report/html"

[tool.coverage.xml]
output = "tests/unit/report/coverage.xml"
17 changes: 17 additions & 0 deletions rename.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
# This is a template `rename.sh` file for snaps
# This file is managed by bootstack-charms-spec and should not be modified
# within individual snap repos. https://launchpad.net/bootstack-charms-spec

snap=$(grep -E "^name:" snap/snapcraft.yaml | awk '{print $2}')
echo "renaming ${snap}_*.snap to ${snap}.snap"
echo -n "pwd: "
pwd
ls -al
echo "Removing previous snap if it exists"
if [[ -e "${snap}.snap" ]];
then
rm "${snap}.snap"
fi
echo "Renaming snap here."
mv ${snap}_*.snap ${snap}.snap
6 changes: 6 additions & 0 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ apps:
plugs:
- network-bind
- opengl
daemon: simple
Deezzir marked this conversation as resolved.
Show resolved Hide resolved
install-mode: disable
aieri marked this conversation as resolved.
Show resolved Hide resolved
restart-condition: on-failure
restart-delay: 2s
aieri marked this conversation as resolved.
Show resolved Hide resolved
environment:
DCGM_HOME_DIR: "${SNAP_COMMON}"
Deezzir marked this conversation as resolved.
Show resolved Hide resolved
dcgmi:
command: usr/bin/dcgmi
plugs:
Expand Down
27 changes: 27 additions & 0 deletions tests/functional/conftest.py
Deezzir marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
import subprocess
from time import sleep

import pytest


@pytest.fixture(scope="session", autouse=True)
def install_dcgm_snap():
"""Install the snap and enable dcgm-exporter service for testing."""
snap = os.environ["TEST_SNAP"]
dcgm_exporter_service = "snap.dcgm.dcgm-exporter.service"
assert 0 == subprocess.run(["sudo", "snap", "install", "--dangerous", snap]).returncode
Deezzir marked this conversation as resolved.
Show resolved Hide resolved

subprocess.run(["sudo", "systemctl", "enable", "--now", dcgm_exporter_service])
chanchiwai-ray marked this conversation as resolved.
Show resolved Hide resolved
sleep(5) # Give some time for the service to start
Deezzir marked this conversation as resolved.
Show resolved Hide resolved

assert (
0
== subprocess.run(
Deezzir marked this conversation as resolved.
Show resolved Hide resolved
["sudo", "systemctl", "is-active", "--quiet", dcgm_exporter_service]
).returncode
)

yield

subprocess.run(["sudo", "snap", "remove", "--purge", "dcgm"])
17 changes: 17 additions & 0 deletions tests/functional/test_snap_dcgm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import subprocess
from time import sleep

Deezzir marked this conversation as resolved.
Show resolved Hide resolved

def test_dcgm_exporter_endpoint():
Deezzir marked this conversation as resolved.
Show resolved Hide resolved
"""Smoke test of the dcgm-exporter service and its endpoint."""
dcgm_exporter_endpoint = "localhost:9400/metrics"
subprocess.run(["sudo", "snap", "disconnect", "dcgm:network-bind"])
Deezzir marked this conversation as resolved.
Show resolved Hide resolved

# The Endpoint should be unavailable with the networking plug
assert 0 != subprocess.run(["curl", dcgm_exporter_endpoint]).returncode

subprocess.run(["sudo", "snap", "connect", "dcgm:network-bind"])
Deezzir marked this conversation as resolved.
Show resolved Hide resolved
print("reconnect")
sleep(5) # should be sufficient for the service to restart
Deezzir marked this conversation as resolved.
Show resolved Hide resolved

assert 0 == subprocess.run(["curl", dcgm_exporter_endpoint]).returncode
Deezzir marked this conversation as resolved.
Show resolved Hide resolved
47 changes: 47 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This is a template `tox.ini` file for snaps
# This file is managed by bootstack-charms-spec and should not be modified
aieri marked this conversation as resolved.
Show resolved Hide resolved
# within individual snap repos. https://launchpad.net/bootstack-charms-spec

[tox]
skipsdist=True
envlist = lint, unit, func
skip_missing_interpreters = True

[testenv]
basepython = python3
setenv = PYTHONPATH={toxinidir}

[testenv:lint]
commands =
pflake8
pylint --recursive=y .
black --check --diff --color .
isort --check --diff --color .
deps =
black
flake8
pyproject-flake8
flake8-docstrings
pep8-naming
flake8-colors
colorama
isort
pylint
{[testenv:func]deps}

[testenv:reformat]
envdir = {toxworkdir}/lint
deps = {[testenv:lint]deps}
commands =
black .
isort .

[testenv:unit]
Deezzir marked this conversation as resolved.
Show resolved Hide resolved

[testenv:func]
deps =
pytest
passenv =
TEST_*
commands =
pytest {toxinidir}/tests/functional {posargs:-v}