Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into template-groups
Browse files Browse the repository at this point in the history
  • Loading branch information
pederhan committed Nov 22, 2023
2 parents 684c16f + 638a712 commit e4431b5
Show file tree
Hide file tree
Showing 13 changed files with 499 additions and 107 deletions.
18 changes: 10 additions & 8 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- '3.10'
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Cache pip
uses: actions/cache@v2
with:
Expand All @@ -24,12 +24,14 @@ jobs:
v1-pip-${{ runner.os }}-${{ matrix.python-version }}
v1-pip-${{ runner.os }}
v1-pip-
- name: Install Python
uses: actions/setup-python@v2
- name: Install Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
pip install -e .[test]
- name: Test
run: python -m pytest -vv
- name: Update pip
run: python -m pip install --upgrade pip
- name: Install Hatch
run: pip install hatch
- name: Run tests
run: hatch run test -vv

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ __pycache__/
venv/
.vscode/
.hypothesis/
build/

path/to/
config.toml
Expand Down
73 changes: 71 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Zabbix-auto-config is an utility that aims to automatically configure hosts, hos

Note: This is only tested with Zabbix 5.0 LTS.

## Requirements

* Python >=3.8
* pip >=21.3
* Zabbix >=5.0

# Quick start

This is a crash course in how to quickly get this application up and running in a local test environment:
Expand Down Expand Up @@ -45,10 +51,23 @@ EOF

## Application

### Installation (production)

For production, installing the project in a virtual environment directly with pip is the recommended way to go:

```bash
python3 -m venv venv
python -m venv venv
. venv/bin/activate
pip install -e .
```

When installing from source, installing in editable mode is recommended, as it allows for pulling in changes from git without having to reinstall the project.

### Configuration (mock environment)

A ZAC environment with mock source collectors, host modifiers, and mapping files can be set up with the following commands:

```bash
cp config.sample.toml config.toml
sed -i 's/^dryrun = true$/dryrun = false/g' config.toml
mkdir -p path/to/source_collector_dir/ path/to/host_modifier_dir/ path/to/map_dir/
Expand Down Expand Up @@ -104,7 +123,9 @@ [email protected]:Hostgroup-bob-hosts
EOF
```

Run the application:
### Running

Installing the application adds the `zac` command to your path. You can run the application with:

```bash
zac
Expand Down Expand Up @@ -238,3 +259,51 @@ Zac manages only inventory properties configured as `managed_inventory` in `conf
2. Remove the "location" property from the host in the source
3. "location=x" will remain in Zabbix

## Development

We use the project management tool [Hatch](https://hatch.pypa.io/latest/) for developing the project. The tool manages virtual environment creation, dependency installation, as well as building and publishing of the project, and more.

Install Hatch with pipx:

```bash
pipx install hatch
```

Install the application with Hatch and enter the virtual environment:

```bash
hatch shell
```

The path to the current Hatch environment can always be found with:

```bash
hatch env find
```

### Testing

Inside a Hatch environment, tests can be run in two ways.

With Hatch:

```bash
hatch run test
```

Or by directly invoking pytest:

```bash
pytest
```

The only difference is that Hatch will automatically check dependencies and install/upgrade them if necessary before running the tests.

#### Testing without Hatch

If you just want to run tests without Hatch, you can do so by installing the development dependencies independently:

```bash
# Set up venv or similar ...
pip install .[test]
```
59 changes: 57 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,58 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "zabbix-auto-config"
dynamic = ["version"]
description = "Zabbix auto config - ZAC"
readme = "README.md"
requires-python = ">=3.8"
license = "MIT"
keywords = []
authors = [{ name = "Paal Braathen", email = "[email protected]" }]
maintainers = [{ name = "Peder Hovdan Andresen", email = "[email protected]" }]
classifiers = [
"Intended Audience :: System Administrators",
"Natural Language :: English",
"Operating System :: POSIX :: Linux",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
]
dependencies = [
"multiprocessing-logging==0.3.1",
"psycopg2>=2.9.5",
"pydantic>=2.0.0",
"pyzabbix>=1.3.0",
"requests>=1.0.0",
"tomli>=2.0.0",
]

[project.optional-dependencies]
test = ["pytest>=7.4.3", "pytest-timeout>=2.2.0", "hypothesis>=6.62.1"]

[project.urls]
Source = "https://github.com/unioslo/zabbix-auto-config"

[project.scripts]
zac = "zabbix_auto_config:main"

[tool.hatch.version]
path = "zabbix_auto_config/__about__.py"

[tool.hatch.envs.default]
dependencies = ["zabbix-auto-config[test]"]

[tool.hatch.envs.default.scripts]
test = "pytest {args}"

[tool.hatch.build.targets.sdist]
exclude = ["/.github", "/tests", "/path"]

[tool.hatch.build.targets.wheel]
packages = ["zabbix_auto_config"]
46 changes: 0 additions & 46 deletions setup.cfg

This file was deleted.

4 changes: 0 additions & 4 deletions setup.py

This file was deleted.

11 changes: 6 additions & 5 deletions tests/test_processing/test_sourcecollectorprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from zabbix_auto_config.processing import SourceCollectorProcess
from zabbix_auto_config.models import Host, SourceCollectorSettings
from zabbix_auto_config.state import get_manager


class SourceCollector:
Expand All @@ -22,7 +23,7 @@ def collect(*args, **kwargs) -> List[Host]:
def test_source_collector_process():
process = SourceCollectorProcess(
name="test-source",
state=multiprocessing.Manager().dict(),
state=get_manager().State(),
module=SourceCollector,
config=SourceCollectorSettings(
module_name="source_collector",
Expand All @@ -40,7 +41,7 @@ def test_source_collector_process():
hosts = process.source_hosts_queue.get()
assert len(hosts["hosts"]) == 2
assert hosts["hosts"][0].hostname == "foo.example.com"
assert process.state["ok"] is True
assert process.state.ok is True
finally:
process.stop_event.set()
process.join(timeout=0.01)
Expand All @@ -57,7 +58,7 @@ def collect(*args, **kwargs) -> List[Host]:
def test_source_collector_disable_on_failure():
process = SourceCollectorProcess(
name="test-source",
state=multiprocessing.Manager().dict(),
state=get_manager().State(),
module=FaultySourceCollector,
config=SourceCollectorSettings(
module_name="faulty_source_collector",
Expand All @@ -73,9 +74,9 @@ def test_source_collector_disable_on_failure():
# Start process and wait until it fails
try:
process.start()
while process.state["ok"] is True:
while process.state.ok is True:
time.sleep(0.01)
assert process.state["ok"] is False
assert process.state.ok is False
assert process.source_hosts_queue.empty() is True
process.stop_event.set()
finally:
Expand Down
13 changes: 7 additions & 6 deletions tests/test_processing/test_zabbixupdater.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import multiprocessing
from pathlib import Path
import time
from unittest.mock import patch
Expand All @@ -10,6 +9,7 @@

from zabbix_auto_config.models import ZabbixSettings
from zabbix_auto_config.processing import ZabbixUpdater
from zabbix_auto_config.state import get_manager


def raises_connect_timeout(*args, **kwargs):
Expand All @@ -32,7 +32,7 @@ def test_zabbixupdater_connect_timeout(mock_psycopg2_connect):
ZabbixUpdater(
name="connect-timeout",
db_uri="",
state=multiprocessing.Manager().dict(),
state=get_manager().State(),
zabbix_config=ZabbixSettings(
map_dir="",
url="",
Expand All @@ -50,7 +50,6 @@ def do_update(self):
raise requests.exceptions.ReadTimeout("read timeout")



@pytest.mark.timeout(5)
def test_zabbixupdater_read_timeout(tmp_path: Path, mock_psycopg2_connect):
# TODO: use mapping file fixtures from #67
Expand All @@ -63,7 +62,7 @@ def test_zabbixupdater_read_timeout(tmp_path: Path, mock_psycopg2_connect):
process = TimeoutUpdater(
name="read-timeout",
db_uri="",
state=multiprocessing.Manager().dict(),
state=get_manager().State(),
zabbix_config=ZabbixSettings(
map_dir=str(map_dir),
url="",
Expand All @@ -77,9 +76,11 @@ def test_zabbixupdater_read_timeout(tmp_path: Path, mock_psycopg2_connect):
# Start the process and wait for it to be marked as unhealthy
try:
process.start()
while process.state["ok"] is True:
while process.state.ok is True:
time.sleep(0.1)
assert process.state["ok"] is False
assert process.state.ok is False
assert process.state.error_type == "ReadTimeout"
assert process.state.error_count == 1
process.stop_event.set()
finally:
process.join(timeout=0.01)
Loading

0 comments on commit e4431b5

Please sign in to comment.