14
14
from ipaddress import IPv4Address
15
15
from typing import TYPE_CHECKING , ClassVar
16
16
17
- from anta .models import AntaCommand , AntaTest
17
+ from anta .custom_types import LogSeverityLevel
18
+ from anta .models import AntaCommand , AntaTemplate , AntaTest
18
19
19
20
if TYPE_CHECKING :
20
21
import logging
21
22
22
- from anta .models import AntaTemplate
23
-
24
23
25
24
def _get_logging_states (logger : logging .Logger , command_output : str ) -> str :
26
25
"""Parse `show logging` output and gets operational logging states used in the tests in this module.
@@ -201,35 +200,43 @@ class VerifyLoggingLogsGeneration(AntaTest):
201
200
202
201
This test performs the following checks:
203
202
204
- 1. Sends a test log message at the **informational** level
205
- 2. Retrieves the most recent logs (last 30 seconds)
206
- 3. Verifies that the test message was successfully logged
207
-
208
- !!! warning
209
- EOS logging buffer should be set to severity level `informational` or higher for this test to work.
203
+ 1. Sends a test log message at the specified severity log level.
204
+ 2. Retrieves the most recent logs (last 30 seconds).
205
+ 3. Verifies that the test message was successfully logged.
210
206
211
207
Expected Results
212
208
----------------
213
209
* Success: If logs are being generated and the test message is found in recent logs.
214
210
* Failure: If any of the following occur:
215
- - The test message is not found in recent logs
216
- - The logging system is not capturing new messages
217
- - No logs are being generated
211
+ - The test message is not found in recent logs.
212
+ - The logging system is not capturing new messages.
213
+ - No logs are being generated.
218
214
219
215
Examples
220
216
--------
221
217
```yaml
222
218
anta.tests.logging:
223
219
- VerifyLoggingLogsGeneration:
220
+ severity_level: informational
224
221
```
225
222
"""
226
223
227
224
categories : ClassVar [list [str ]] = ["logging" ]
228
225
commands : ClassVar [list [AntaCommand | AntaTemplate ]] = [
229
- AntaCommand ( command = "send log level informational message ANTA VerifyLoggingLogsGeneration validation" , ofmt = "text" ),
230
- AntaCommand ( command = "show logging informational last 30 seconds | grep ANTA" , ofmt = "text" , use_cache = False ),
226
+ AntaTemplate ( template = "send log level {severity_level} message ANTA VerifyLoggingLogsGeneration validation" , ofmt = "text" ),
227
+ AntaTemplate ( template = "show logging {severity_level} last 30 seconds | grep ANTA" , ofmt = "text" , use_cache = False ),
231
228
]
232
229
230
+ class Input (AntaTest .Input ):
231
+ """Input model for the VerifyLoggingLogsGeneration test."""
232
+
233
+ severity_level : LogSeverityLevel = "informational"
234
+ """Log severity level. Defaults to informational."""
235
+
236
+ def render (self , template : AntaTemplate ) -> list [AntaCommand ]:
237
+ """Render the template for log severity level in the input."""
238
+ return [template .render (severity_level = self .inputs .severity_level )]
239
+
233
240
@AntaTest .anta_test
234
241
def test (self ) -> None :
235
242
"""Main test function for VerifyLoggingLogsGeneration."""
@@ -248,37 +255,45 @@ class VerifyLoggingHostname(AntaTest):
248
255
249
256
This test performs the following checks:
250
257
251
- 1. Retrieves the device's configured FQDN
252
- 2. Sends a test log message at the **informational** level
253
- 3. Retrieves the most recent logs (last 30 seconds)
254
- 4. Verifies that the test message includes the complete FQDN of the device
255
-
256
- !!! warning
257
- EOS logging buffer should be set to severity level `informational` or higher for this test to work.
258
+ 1. Retrieves the device's configured FQDN.
259
+ 2. Sends a test log message at the specified severity log level.
260
+ 3. Retrieves the most recent logs (last 30 seconds).
261
+ 4. Verifies that the test message includes the complete FQDN of the device.
258
262
259
263
Expected Results
260
264
----------------
261
265
* Success: If logs are generated with the device's complete FQDN.
262
266
* Failure: If any of the following occur:
263
- - The test message is not found in recent logs
264
- - The log message does not include the device's FQDN
265
- - The FQDN in the log message doesn't match the configured FQDN
267
+ - The test message is not found in recent logs.
268
+ - The log message does not include the device's FQDN.
269
+ - The FQDN in the log message doesn't match the configured FQDN.
266
270
267
271
Examples
268
272
--------
269
273
```yaml
270
274
anta.tests.logging:
271
275
- VerifyLoggingHostname:
276
+ severity_level: informational
272
277
```
273
278
"""
274
279
275
280
categories : ClassVar [list [str ]] = ["logging" ]
276
281
commands : ClassVar [list [AntaCommand | AntaTemplate ]] = [
277
282
AntaCommand (command = "show hostname" , revision = 1 ),
278
- AntaCommand ( command = "send log level informational message ANTA VerifyLoggingHostname validation" , ofmt = "text" ),
279
- AntaCommand ( command = "show logging informational last 30 seconds | grep ANTA" , ofmt = "text" , use_cache = False ),
283
+ AntaTemplate ( template = "send log level {severity_level} message ANTA VerifyLoggingHostname validation" , ofmt = "text" ),
284
+ AntaTemplate ( template = "show logging {severity_level} last 30 seconds | grep ANTA" , ofmt = "text" , use_cache = False ),
280
285
]
281
286
287
+ class Input (AntaTest .Input ):
288
+ """Input model for the VerifyLoggingHostname test."""
289
+
290
+ severity_level : LogSeverityLevel = "informational"
291
+ """Log severity level. Defaults to informational."""
292
+
293
+ def render (self , template : AntaTemplate ) -> list [AntaCommand ]:
294
+ """Render the template for log severity level in the input."""
295
+ return [template .render (severity_level = self .inputs .severity_level )]
296
+
282
297
@AntaTest .anta_test
283
298
def test (self ) -> None :
284
299
"""Main test function for VerifyLoggingHostname."""
@@ -303,37 +318,45 @@ class VerifyLoggingTimestamp(AntaTest):
303
318
304
319
This test performs the following checks:
305
320
306
- 1. Sends a test log message at the **informational** level
307
- 2. Retrieves the most recent logs (last 30 seconds)
308
- 3. Verifies that the test message is present with a high-resolution RFC3339 timestamp format
309
- - Example format: `2024-01-25T15:30:45.123456+00:00`
310
- - Includes microsecond precision
311
- - Contains timezone offset
312
-
313
- !!! warning
314
- EOS logging buffer should be set to severity level `informational` or higher for this test to work.
321
+ 1. Sends a test log message at the specified severity log level.
322
+ 2. Retrieves the most recent logs (last 30 seconds).
323
+ 3. Verifies that the test message is present with a high-resolution RFC3339 timestamp format.
324
+ - Example format: `2024-01-25T15:30:45.123456+00:00`.
325
+ - Includes microsecond precision.
326
+ - Contains timezone offset.
315
327
316
328
Expected Results
317
329
----------------
318
330
* Success: If logs are generated with the correct high-resolution RFC3339 timestamp format.
319
331
* Failure: If any of the following occur:
320
- - The test message is not found in recent logs
321
- - The timestamp format does not match the expected RFC3339 format
332
+ - The test message is not found in recent logs.
333
+ - The timestamp format does not match the expected RFC3339 format.
322
334
323
335
Examples
324
336
--------
325
337
```yaml
326
338
anta.tests.logging:
327
339
- VerifyLoggingTimestamp:
340
+ severity_level: informational
328
341
```
329
342
"""
330
343
331
344
categories : ClassVar [list [str ]] = ["logging" ]
332
345
commands : ClassVar [list [AntaCommand | AntaTemplate ]] = [
333
- AntaCommand ( command = "send log level informational message ANTA VerifyLoggingTimestamp validation" , ofmt = "text" ),
334
- AntaCommand ( command = "show logging informational last 30 seconds | grep ANTA" , ofmt = "text" , use_cache = False ),
346
+ AntaTemplate ( template = "send log level {severity_level} message ANTA VerifyLoggingTimestamp validation" , ofmt = "text" ),
347
+ AntaTemplate ( template = "show logging {severity_level} last 30 seconds | grep ANTA" , ofmt = "text" , use_cache = False ),
335
348
]
336
349
350
+ class Input (AntaTest .Input ):
351
+ """Input model for the VerifyLoggingTimestamp test."""
352
+
353
+ severity_level : LogSeverityLevel = "informational"
354
+ """Log severity level. Defaults to informational."""
355
+
356
+ def render (self , template : AntaTemplate ) -> list [AntaCommand ]:
357
+ """Render the template for log severity level in the input."""
358
+ return [template .render (severity_level = self .inputs .severity_level )]
359
+
337
360
@AntaTest .anta_test
338
361
def test (self ) -> None :
339
362
"""Main test function for VerifyLoggingTimestamp."""
0 commit comments