pytest-testinfra-exporter 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,280 @@
1
+ Metadata-Version: 2.4
2
+ Name: pytest-testinfra-exporter
3
+ Version: 0.3.0
4
+ Summary: Pytest plugin that reports testinfra results to MariaDB or PostgreSQL backends and visualizes on Grafana.
5
+ Maintainer-email: Udeshya Giri <udeshyagiri@gmail.com>, Udeshya Giri <udeshya.giri@phonepe.com>, Aniruddha Thombre <aniruddha@aniruddhas.com>, Aniruddha Thombre <aniruddha.t@phonepe.com>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/phonepe/pytest-testinfra-exporter
8
+ Project-URL: Issues, https://github.com/phonepe/pytest-testinfra-exporter/issues
9
+ Keywords: pytest,plugin,testinfra,mariadb,postgres,pytest-testinfra,grafana
10
+ Classifier: Framework :: Pytest
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3 :: Only
13
+ Classifier: Programming Language :: Python :: 3.9
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Requires-Python: >=3.9
18
+ Description-Content-Type: text/markdown
19
+ Requires-Dist: pytest>=7.0
20
+ Requires-Dist: PyYAML>=6.0
21
+ Provides-Extra: mariadb
22
+ Requires-Dist: PyMySQL>=1.1; extra == "mariadb"
23
+ Provides-Extra: postgres
24
+ Requires-Dist: psycopg2-binary>=2.9; extra == "postgres"
25
+
26
+ # pytest-testinfra-exporter
27
+
28
+ Backend-pluggable pytest reporting plugin with preserved `pytest-testinfra` host parsing behavior.
29
+
30
+ ## Installation
31
+
32
+ From the `pytest-testinfra-exporter` directory, install the plugin in editable mode:
33
+
34
+ ```bash
35
+ pip install -e .
36
+ ```
37
+
38
+ If you are standing one level above this directory, use:
39
+
40
+ ```bash
41
+ pip install -e ./pytest-testinfra-exporter
42
+ ```
43
+
44
+ Pytest will auto-discover the plugin through the package's `pytest11` entry point, so no `conftest.py` changes are required.
45
+
46
+ Install a database driver if you plan to use a backend:
47
+
48
+ ```bash
49
+ pip install -e ".[mariadb]"
50
+ ```
51
+
52
+ or:
53
+
54
+ ```bash
55
+ pip install -e ".[postgres]"
56
+ ```
57
+
58
+ ## Usage
59
+
60
+ Once installed, enable reporting directly from the pytest command line:
61
+
62
+ ```bash
63
+ pytest --storage-report --report-backend=mariadb
64
+ ```
65
+
66
+ The plugin is loaded automatically during pytest startup.
67
+
68
+ ## Overview
69
+
70
+ This project now follows a **Strategy + Adapter** architecture:
71
+
72
+ - Core pytest hooks are implemented in `plugin.py`.
73
+ - Storage is delegated through `AbstractStorageBackend` in `backend.py`.
74
+ - MariaDB support is implemented as `MariaDBBackend` in `backends/mariadb.py`.
75
+ - PostgreSQL support is implemented as `PostgresBackend` in `backends/postgres.py`.
76
+ - Normalized payloads are represented by dataclasses in `models.py`.
77
+
78
+ The testinfra-specific logic is intentionally unchanged (including parsing of `salt://`, `ssh://`, fixture host resolution, and nodeid parsing).
79
+
80
+ ---
81
+
82
+ ## Package structure
83
+
84
+ - `models.py` — dataclasses used by the core reporter and backends.
85
+ - `backend.py` — backend interface contract.
86
+ - `backends/mariadb.py` — MariaDB adapter implementation.
87
+ - `backends/postgres.py` — PostgreSQL adapter implementation.
88
+ - `plugin.py` — pytest hooks, helper utilities, and failure tagging.
89
+ - `schema/db.sql` — full MariaDB schema reset/apply script.
90
+ - `failure_mapper/failure_map.yaml` — failure tag mapping rules.
91
+ - `docs/index.rst` — Sphinx documentation entry point.
92
+ - `docs/usage.rst` — Sphinx usage guide.
93
+ - `docs/api.rst` — Sphinx API reference.
94
+
95
+ ---
96
+
97
+ ## Runtime architecture
98
+
99
+ ```text
100
+ pytest lifecycle hooks (plugin.py)
101
+ -> build backend strategy (--report-backend)
102
+ -> accumulate TestResultRecord objects in memory
103
+ -> backend.save_results(run_id, results)
104
+ -> backend.session_finish(run_id, counters)
105
+ ```
106
+
107
+ ### Hook flow
108
+
109
+ 1. `pytest_sessionstart`
110
+ - creates run metadata (`TestRunSummary`)
111
+ - initializes selected backend
112
+ - persists session start
113
+ 2. `pytest_runtest_makereport`
114
+ - captures metadata per nodeid (test name, suite, class, host, markers)
115
+ 3. `pytest_runtest_logreport`
116
+ - merges phase reports (`setup/call/teardown`)
117
+ - computes final status and captured artifacts
118
+ - appends `TestResultRecord`
119
+ 4. `pytest_sessionfinish`
120
+ - computes final counters
121
+ - delegates persistence to backend
122
+ 5. `pytest_terminal_summary`
123
+ - prints reporter and tagger status
124
+
125
+ ---
126
+
127
+ ## CLI options
128
+
129
+ | Option | Default | Description |
130
+ |---|---|---|
131
+ | `--storage-report` | `False` | Enable reporting pipeline. |
132
+ | `--report-backend` | `mariadb` | Storage backend strategy selector (`mariadb`, `postgres`). |
133
+ | `--datastore-config` | `datastores/default.yaml` | Path to a YAML file with datastore connection settings. The `datastore.<report-backend>` section is used. Explicit CLI options override its values. |
134
+ | `--run-name` | Start datetime | Human-readable run name stored with the run. Defaults to `YYYY-MM-DD HH:MM:SS`. |
135
+ | `--mariadb-host` | `localhost` | MariaDB host. |
136
+ | `--mariadb-port` | `3306` | MariaDB port. |
137
+ | `--mariadb-user` | `testinfra_user` | MariaDB username. |
138
+ | `--mariadb-password` | `password` | MariaDB password. |
139
+ | `--mariadb-database` | `testinfra_reports` | MariaDB database name. |
140
+ | `--mariadb-suite-version` | `None` | Suite version string (for example git SHA). |
141
+ | `--mariadb-init-schema` | `False` | Run idempotent schema creation/migrations. |
142
+ | `--failure-map` | `failure_mapper/failure_map.yaml` | Failure tagging rules file. |
143
+ | `--postgres-host` | `localhost` | PostgreSQL host. |
144
+ | `--postgres-port` | `5432` | PostgreSQL port. |
145
+ | `--postgres-user` | `postgres` | PostgreSQL username. |
146
+ | `--postgres-password` | `password` | PostgreSQL password. |
147
+ | `--postgres-database` | `testinfra_reports` | PostgreSQL database name. |
148
+ | `--postgres-init-schema` | `False` | Run idempotent PostgreSQL schema creation. |
149
+
150
+ ---
151
+
152
+ ## Example usage
153
+
154
+ ### MariaDB
155
+
156
+ ```bash
157
+ pytest tests/ \
158
+ --storage-report \
159
+ --report-backend mariadb \
160
+ --run-name "manual-run" \
161
+ --mariadb-host 127.0.0.1 \
162
+ --mariadb-port 3306 \
163
+ --mariadb-user testinfra_user \
164
+ --mariadb-password password \
165
+ --mariadb-database testinfra_reports \
166
+ --mariadb-suite-version "$(git rev-parse --short HEAD)" \
167
+ --mariadb-init-schema
168
+ ```
169
+
170
+ ### PostgreSQL
171
+
172
+ ```bash
173
+ pytest tests/ \
174
+ --storage-report \
175
+ --report-backend postgres \
176
+ --postgres-host 127.0.0.1 \
177
+ --postgres-port 5432 \
178
+ --postgres-user postgres \
179
+ --postgres-password password \
180
+ --postgres-database testinfra_reports \
181
+ --postgres-init-schema
182
+ ```
183
+
184
+ ### Datastore config file
185
+
186
+ Instead of passing connection flags on every invocation, define them once in a
187
+ YAML file and select the backend with `--report-backend`. When
188
+ `--datastore-config` is not supplied, the bundled
189
+ [`datastores/default.yaml`](datastores/default.yaml) is used:
190
+
191
+ ```yaml
192
+ datastore:
193
+ mariadb:
194
+ host: 'localhost'
195
+ port: 3306
196
+ user: 'testinfra_user'
197
+ password: 'password'
198
+ database: 'testinfra_reports'
199
+ postgres:
200
+ host: 'localhost'
201
+ port: 5432
202
+ user: 'postgres'
203
+ password: 'password'
204
+ database: 'testinfra_reports'
205
+ ```
206
+
207
+ ```bash
208
+ pytest tests/ \
209
+ --storage-report \
210
+ --report-backend mariadb \
211
+ --datastore-config datastore.yaml
212
+ ```
213
+
214
+ Any explicit CLI option (for example `--mariadb-user`, `--mariadb-port`)
215
+ overrides the corresponding value from the YAML file.
216
+
217
+ ---
218
+
219
+ ## Dependencies
220
+
221
+ - MariaDB backend: `PyMySQL`
222
+ - PostgreSQL backend: `psycopg2-binary`
223
+ - Failure tagging: `PyYAML`
224
+
225
+ ```bash
226
+ pip install PyMySQL psycopg2-binary pyyaml
227
+ ```
228
+
229
+ ---
230
+
231
+ ## Failure tagging
232
+
233
+ `FailureTagger` loads YAML rules from `failure_mapper/failure_map.yaml` and applies first-match classification for `fail`/`error` results.
234
+
235
+ Supported matching:
236
+
237
+ - `match_type: exact` (substring)
238
+ - `match_type: regex` (`re.search`)
239
+
240
+ ---
241
+
242
+ ## Sphinx documentation
243
+
244
+ A Sphinx-ready docs tree is now included:
245
+
246
+ - `docs/index.rst`
247
+ - `docs/usage.rst`
248
+ - `docs/api.rst`
249
+
250
+ These files use autodoc directives for:
251
+
252
+ - `models`
253
+ - `backend`
254
+ - `plugin`
255
+ - `backends.mariadb`
256
+ - `backends.postgres`
257
+
258
+ Example minimal `docs/conf.py`:
259
+
260
+ ```python
261
+ extensions = [
262
+ "sphinx.ext.autodoc",
263
+ "sphinx.ext.napoleon",
264
+ "sphinx.ext.viewcode",
265
+ ]
266
+ ```
267
+
268
+ Build docs from project root:
269
+
270
+ ```bash
271
+ sphinx-build -b html docs docs/_build/html
272
+ ```
273
+
274
+ ---
275
+
276
+ ## Notes
277
+
278
+ - Timestamp persistence remains naive IST for compatibility with existing dashboards.
279
+ - Existing testinfra host extraction behavior is preserved.
280
+ - Current backend implementations: `mariadb`, `postgres`.
@@ -0,0 +1,255 @@
1
+ # pytest-testinfra-exporter
2
+
3
+ Backend-pluggable pytest reporting plugin with preserved `pytest-testinfra` host parsing behavior.
4
+
5
+ ## Installation
6
+
7
+ From the `pytest-testinfra-exporter` directory, install the plugin in editable mode:
8
+
9
+ ```bash
10
+ pip install -e .
11
+ ```
12
+
13
+ If you are standing one level above this directory, use:
14
+
15
+ ```bash
16
+ pip install -e ./pytest-testinfra-exporter
17
+ ```
18
+
19
+ Pytest will auto-discover the plugin through the package's `pytest11` entry point, so no `conftest.py` changes are required.
20
+
21
+ Install a database driver if you plan to use a backend:
22
+
23
+ ```bash
24
+ pip install -e ".[mariadb]"
25
+ ```
26
+
27
+ or:
28
+
29
+ ```bash
30
+ pip install -e ".[postgres]"
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ Once installed, enable reporting directly from the pytest command line:
36
+
37
+ ```bash
38
+ pytest --storage-report --report-backend=mariadb
39
+ ```
40
+
41
+ The plugin is loaded automatically during pytest startup.
42
+
43
+ ## Overview
44
+
45
+ This project now follows a **Strategy + Adapter** architecture:
46
+
47
+ - Core pytest hooks are implemented in `plugin.py`.
48
+ - Storage is delegated through `AbstractStorageBackend` in `backend.py`.
49
+ - MariaDB support is implemented as `MariaDBBackend` in `backends/mariadb.py`.
50
+ - PostgreSQL support is implemented as `PostgresBackend` in `backends/postgres.py`.
51
+ - Normalized payloads are represented by dataclasses in `models.py`.
52
+
53
+ The testinfra-specific logic is intentionally unchanged (including parsing of `salt://`, `ssh://`, fixture host resolution, and nodeid parsing).
54
+
55
+ ---
56
+
57
+ ## Package structure
58
+
59
+ - `models.py` — dataclasses used by the core reporter and backends.
60
+ - `backend.py` — backend interface contract.
61
+ - `backends/mariadb.py` — MariaDB adapter implementation.
62
+ - `backends/postgres.py` — PostgreSQL adapter implementation.
63
+ - `plugin.py` — pytest hooks, helper utilities, and failure tagging.
64
+ - `schema/db.sql` — full MariaDB schema reset/apply script.
65
+ - `failure_mapper/failure_map.yaml` — failure tag mapping rules.
66
+ - `docs/index.rst` — Sphinx documentation entry point.
67
+ - `docs/usage.rst` — Sphinx usage guide.
68
+ - `docs/api.rst` — Sphinx API reference.
69
+
70
+ ---
71
+
72
+ ## Runtime architecture
73
+
74
+ ```text
75
+ pytest lifecycle hooks (plugin.py)
76
+ -> build backend strategy (--report-backend)
77
+ -> accumulate TestResultRecord objects in memory
78
+ -> backend.save_results(run_id, results)
79
+ -> backend.session_finish(run_id, counters)
80
+ ```
81
+
82
+ ### Hook flow
83
+
84
+ 1. `pytest_sessionstart`
85
+ - creates run metadata (`TestRunSummary`)
86
+ - initializes selected backend
87
+ - persists session start
88
+ 2. `pytest_runtest_makereport`
89
+ - captures metadata per nodeid (test name, suite, class, host, markers)
90
+ 3. `pytest_runtest_logreport`
91
+ - merges phase reports (`setup/call/teardown`)
92
+ - computes final status and captured artifacts
93
+ - appends `TestResultRecord`
94
+ 4. `pytest_sessionfinish`
95
+ - computes final counters
96
+ - delegates persistence to backend
97
+ 5. `pytest_terminal_summary`
98
+ - prints reporter and tagger status
99
+
100
+ ---
101
+
102
+ ## CLI options
103
+
104
+ | Option | Default | Description |
105
+ |---|---|---|
106
+ | `--storage-report` | `False` | Enable reporting pipeline. |
107
+ | `--report-backend` | `mariadb` | Storage backend strategy selector (`mariadb`, `postgres`). |
108
+ | `--datastore-config` | `datastores/default.yaml` | Path to a YAML file with datastore connection settings. The `datastore.<report-backend>` section is used. Explicit CLI options override its values. |
109
+ | `--run-name` | Start datetime | Human-readable run name stored with the run. Defaults to `YYYY-MM-DD HH:MM:SS`. |
110
+ | `--mariadb-host` | `localhost` | MariaDB host. |
111
+ | `--mariadb-port` | `3306` | MariaDB port. |
112
+ | `--mariadb-user` | `testinfra_user` | MariaDB username. |
113
+ | `--mariadb-password` | `password` | MariaDB password. |
114
+ | `--mariadb-database` | `testinfra_reports` | MariaDB database name. |
115
+ | `--mariadb-suite-version` | `None` | Suite version string (for example git SHA). |
116
+ | `--mariadb-init-schema` | `False` | Run idempotent schema creation/migrations. |
117
+ | `--failure-map` | `failure_mapper/failure_map.yaml` | Failure tagging rules file. |
118
+ | `--postgres-host` | `localhost` | PostgreSQL host. |
119
+ | `--postgres-port` | `5432` | PostgreSQL port. |
120
+ | `--postgres-user` | `postgres` | PostgreSQL username. |
121
+ | `--postgres-password` | `password` | PostgreSQL password. |
122
+ | `--postgres-database` | `testinfra_reports` | PostgreSQL database name. |
123
+ | `--postgres-init-schema` | `False` | Run idempotent PostgreSQL schema creation. |
124
+
125
+ ---
126
+
127
+ ## Example usage
128
+
129
+ ### MariaDB
130
+
131
+ ```bash
132
+ pytest tests/ \
133
+ --storage-report \
134
+ --report-backend mariadb \
135
+ --run-name "manual-run" \
136
+ --mariadb-host 127.0.0.1 \
137
+ --mariadb-port 3306 \
138
+ --mariadb-user testinfra_user \
139
+ --mariadb-password password \
140
+ --mariadb-database testinfra_reports \
141
+ --mariadb-suite-version "$(git rev-parse --short HEAD)" \
142
+ --mariadb-init-schema
143
+ ```
144
+
145
+ ### PostgreSQL
146
+
147
+ ```bash
148
+ pytest tests/ \
149
+ --storage-report \
150
+ --report-backend postgres \
151
+ --postgres-host 127.0.0.1 \
152
+ --postgres-port 5432 \
153
+ --postgres-user postgres \
154
+ --postgres-password password \
155
+ --postgres-database testinfra_reports \
156
+ --postgres-init-schema
157
+ ```
158
+
159
+ ### Datastore config file
160
+
161
+ Instead of passing connection flags on every invocation, define them once in a
162
+ YAML file and select the backend with `--report-backend`. When
163
+ `--datastore-config` is not supplied, the bundled
164
+ [`datastores/default.yaml`](datastores/default.yaml) is used:
165
+
166
+ ```yaml
167
+ datastore:
168
+ mariadb:
169
+ host: 'localhost'
170
+ port: 3306
171
+ user: 'testinfra_user'
172
+ password: 'password'
173
+ database: 'testinfra_reports'
174
+ postgres:
175
+ host: 'localhost'
176
+ port: 5432
177
+ user: 'postgres'
178
+ password: 'password'
179
+ database: 'testinfra_reports'
180
+ ```
181
+
182
+ ```bash
183
+ pytest tests/ \
184
+ --storage-report \
185
+ --report-backend mariadb \
186
+ --datastore-config datastore.yaml
187
+ ```
188
+
189
+ Any explicit CLI option (for example `--mariadb-user`, `--mariadb-port`)
190
+ overrides the corresponding value from the YAML file.
191
+
192
+ ---
193
+
194
+ ## Dependencies
195
+
196
+ - MariaDB backend: `PyMySQL`
197
+ - PostgreSQL backend: `psycopg2-binary`
198
+ - Failure tagging: `PyYAML`
199
+
200
+ ```bash
201
+ pip install PyMySQL psycopg2-binary pyyaml
202
+ ```
203
+
204
+ ---
205
+
206
+ ## Failure tagging
207
+
208
+ `FailureTagger` loads YAML rules from `failure_mapper/failure_map.yaml` and applies first-match classification for `fail`/`error` results.
209
+
210
+ Supported matching:
211
+
212
+ - `match_type: exact` (substring)
213
+ - `match_type: regex` (`re.search`)
214
+
215
+ ---
216
+
217
+ ## Sphinx documentation
218
+
219
+ A Sphinx-ready docs tree is now included:
220
+
221
+ - `docs/index.rst`
222
+ - `docs/usage.rst`
223
+ - `docs/api.rst`
224
+
225
+ These files use autodoc directives for:
226
+
227
+ - `models`
228
+ - `backend`
229
+ - `plugin`
230
+ - `backends.mariadb`
231
+ - `backends.postgres`
232
+
233
+ Example minimal `docs/conf.py`:
234
+
235
+ ```python
236
+ extensions = [
237
+ "sphinx.ext.autodoc",
238
+ "sphinx.ext.napoleon",
239
+ "sphinx.ext.viewcode",
240
+ ]
241
+ ```
242
+
243
+ Build docs from project root:
244
+
245
+ ```bash
246
+ sphinx-build -b html docs docs/_build/html
247
+ ```
248
+
249
+ ---
250
+
251
+ ## Notes
252
+
253
+ - Timestamp persistence remains naive IST for compatibility with existing dashboards.
254
+ - Existing testinfra host extraction behavior is preserved.
255
+ - Current backend implementations: `mariadb`, `postgres`.
@@ -0,0 +1,24 @@
1
+ # Copyright (c) 2026 Original Author(s), PhonePe India Pvt. Ltd.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """pytest-testinfra-exporter package entrypoint.
16
+
17
+ This package is exposed to pytest via the ``pytest11`` entry point so the
18
+ plugin is auto-discovered after installation without requiring a ``conftest.py``
19
+ registration step.
20
+ """
21
+
22
+ from .plugin import pytest_addoption, pytest_configure
23
+
24
+ __all__ = ["__version__", "pytest_addoption", "pytest_configure"]