From 5ac36befe296fde912bb7f47fa74d91d65ad1dbb Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 1 Mar 2024 14:25:33 -0800 Subject: [PATCH] add apt-get-sqq parser tests --- docs/parsers/apt_get_sqq.md | 201 ++++++++++++++++++ jc/parsers/apt_get_sqq.py | 146 ++++++++++++- .../fixtures/generic/apt_get_sqq--sample.json | 1 + .../fixtures/generic/apt_get_sqq--sample.out | 9 + tests/test_apt_get_sqq.py | 28 +++ 5 files changed, 377 insertions(+), 8 deletions(-) create mode 100644 docs/parsers/apt_get_sqq.md create mode 100644 tests/fixtures/generic/apt_get_sqq--sample.json create mode 100644 tests/fixtures/generic/apt_get_sqq--sample.out create mode 100644 tests/test_apt_get_sqq.py diff --git a/docs/parsers/apt_get_sqq.md b/docs/parsers/apt_get_sqq.md new file mode 100644 index 00000000..8c57918c --- /dev/null +++ b/docs/parsers/apt_get_sqq.md @@ -0,0 +1,201 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.apt\_get\_sqq + +jc - JSON Convert `apt-get -sqq` command output parser + +Requires the `-sqq` options in `apt-get`. + +Usage (cli): + + $ apt-get -sqq | jc --apt-get-sqq + +or + + $ jc apt-get -sqq + +Usage (module): + + import jc + result = jc.parse('apt_get_sqq', apt_get_sqq_command_output) + +Schema: + + [ + { + "operation": string, # configure, remove, or unpack + "package": string, + "broken": string/null, + "proposed_pkg_ver": string, + "existing_src": string/null, + "architecture": string + } + ] + +Examples: + + $ apt-get -sqq | jc --apt-get-sqq -p + [ + { + "operation": "unpack", + "package": "dpkg", + "broken": "1.19.7", + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "unpack", + "package": "dpkg", + "broken": null, + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "configure", + "package": "dpkg", + "broken": "1.19.7", + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "remove", + "package": "dpkg", + "broken": "1.19.7", + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "unpack", + "package": "base-files", + "broken": "10.3+deb10u4", + "proposed_pkg_ver": "10.3+deb10u13 Debian:10.13/oldstable", + "existing_src": null, + "architecture": "amd64" + }, + { + "operation": "configure", + "package": "base-files", + "broken": null, + "proposed_pkg_ver": "10.3+deb10u13 Debian:10.13/oldstable", + "existing_src": null, + "architecture": "amd64" + }, + { + "operation": "unpack", + "package": "dpkg", + "broken": "1.19.7", + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "configure", + "package": "dpkg", + "broken": null, + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + } + ] + + $ apt-get -sqq | jc --apt-get-sqq -p -r + [ + { + "operation": "Inst", + "package": "dpkg", + "broken": "1.19.7", + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "Inst", + "package": "dpkg", + "broken": null, + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "Conf", + "package": "dpkg", + "broken": "1.19.7", + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "Remv", + "package": "dpkg", + "broken": "1.19.7", + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "Inst", + "package": "base-files", + "broken": "10.3+deb10u4", + "proposed_pkg_ver": "10.3+deb10u13 Debian:10.13/oldstable", + "existing_src": null, + "architecture": "amd64" + }, + { + "operation": "Conf", + "package": "base-files", + "broken": null, + "proposed_pkg_ver": "10.3+deb10u13 Debian:10.13/oldstable", + "existing_src": null, + "architecture": "amd64" + }, + { + "operation": "Inst", + "package": "dpkg", + "broken": "1.19.7", + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "Conf", + "package": "dpkg", + "broken": null, + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + } + ] + + + +### parse + +```python +def parse(data: str, + raw: bool = False, + quiet: bool = False) -> List[JSONDictType] +``` + +Main text parsing function + +Parameters: + + data: (string) text data to parse + raw: (boolean) unprocessed output if True + quiet: (boolean) suppress warning messages if True + +Returns: + + List of Dictionaries. Raw or processed structured data. + +### Parser Information +Compatibility: linux + +Source: [`jc/parsers/apt_get_sqq.py`](https://github.com/kellyjonbrazil/jc/blob/master/jc/parsers/apt_get_sqq.py) + +Version 1.0 by Kelly Brazil (kellyjonbrazil@gmail.com) diff --git a/jc/parsers/apt_get_sqq.py b/jc/parsers/apt_get_sqq.py index c47755d6..eeec6824 100644 --- a/jc/parsers/apt_get_sqq.py +++ b/jc/parsers/apt_get_sqq.py @@ -19,22 +19,152 @@ Schema: [ { - "operation": string, # configure, remove, or unpack + "operation": string, # configure, remove, or unpack "package": string, - "broken": string or null, + "broken": string/null, "proposed_pkg_ver": string, - "existing_src": string, + "existing_src": string/null, "architecture": string } ] Examples: - $ foo | jc --foo -p - [] + $ apt-get -sqq | jc --apt-get-sqq -p + [ + { + "operation": "unpack", + "package": "dpkg", + "broken": "1.19.7", + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "unpack", + "package": "dpkg", + "broken": null, + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "configure", + "package": "dpkg", + "broken": "1.19.7", + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "remove", + "package": "dpkg", + "broken": "1.19.7", + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "unpack", + "package": "base-files", + "broken": "10.3+deb10u4", + "proposed_pkg_ver": "10.3+deb10u13 Debian:10.13/oldstable", + "existing_src": null, + "architecture": "amd64" + }, + { + "operation": "configure", + "package": "base-files", + "broken": null, + "proposed_pkg_ver": "10.3+deb10u13 Debian:10.13/oldstable", + "existing_src": null, + "architecture": "amd64" + }, + { + "operation": "unpack", + "package": "dpkg", + "broken": "1.19.7", + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "configure", + "package": "dpkg", + "broken": null, + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + } + ] - $ foo | jc --foo -p -r - [] + $ apt-get -sqq | jc --apt-get-sqq -p -r + [ + { + "operation": "Inst", + "package": "dpkg", + "broken": "1.19.7", + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "Inst", + "package": "dpkg", + "broken": null, + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "Conf", + "package": "dpkg", + "broken": "1.19.7", + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "Remv", + "package": "dpkg", + "broken": "1.19.7", + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "Inst", + "package": "base-files", + "broken": "10.3+deb10u4", + "proposed_pkg_ver": "10.3+deb10u13 Debian:10.13/oldstable", + "existing_src": null, + "architecture": "amd64" + }, + { + "operation": "Conf", + "package": "base-files", + "broken": null, + "proposed_pkg_ver": "10.3+deb10u13 Debian:10.13/oldstable", + "existing_src": null, + "architecture": "amd64" + }, + { + "operation": "Inst", + "package": "dpkg", + "broken": "1.19.7", + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + }, + { + "operation": "Conf", + "package": "dpkg", + "broken": null, + "proposed_pkg_ver": "1.19.8 Debian:10.13/oldstable", + "existing_src": "Debian-Security:10/oldstable", + "architecture": "amd64" + } + ] """ import re from typing import List, Dict @@ -108,7 +238,7 @@ def parse( # Inst dpkg [1.19.7] (1.19.8 Debian:10.13/oldstable, Debian-Security:10/oldstable [amd64]) # | | | | | \architecture - # | | | | \existing_src + # | | | | \existing_src (optional) # | | | \proposed_pkg_ver # | | \broken (optional) # | \package diff --git a/tests/fixtures/generic/apt_get_sqq--sample.json b/tests/fixtures/generic/apt_get_sqq--sample.json new file mode 100644 index 00000000..e6616ac1 --- /dev/null +++ b/tests/fixtures/generic/apt_get_sqq--sample.json @@ -0,0 +1 @@ +[{"operation":"unpack","package":"dpkg","broken":"1.19.7","proposed_pkg_ver":"1.19.8 Debian:10.13/oldstable","existing_src":"Debian-Security:10/oldstable","architecture":"amd64"},{"operation":"unpack","package":"dpkg","broken":null,"proposed_pkg_ver":"1.19.8 Debian:10.13/oldstable","existing_src":"Debian-Security:10/oldstable","architecture":"amd64"},{"operation":"configure","package":"dpkg","broken":"1.19.7","proposed_pkg_ver":"1.19.8 Debian:10.13/oldstable","existing_src":"Debian-Security:10/oldstable","architecture":"amd64"},{"operation":"remove","package":"dpkg","broken":"1.19.7","proposed_pkg_ver":"1.19.8 Debian:10.13/oldstable","existing_src":"Debian-Security:10/oldstable","architecture":"amd64"},{"operation":"unpack","package":"base-files","broken":"10.3+deb10u4","proposed_pkg_ver":"10.3+deb10u13 Debian:10.13/oldstable","existing_src":null,"architecture":"amd64"},{"operation":"configure","package":"base-files","broken":null,"proposed_pkg_ver":"10.3+deb10u13 Debian:10.13/oldstable","existing_src":null,"architecture":"amd64"},{"operation":"unpack","package":"dpkg","broken":"1.19.7","proposed_pkg_ver":"1.19.8 Debian:10.13/oldstable","existing_src":"Debian-Security:10/oldstable","architecture":"amd64"},{"operation":"configure","package":"dpkg","broken":null,"proposed_pkg_ver":"1.19.8 Debian:10.13/oldstable","existing_src":"Debian-Security:10/oldstable","architecture":"amd64"}] diff --git a/tests/fixtures/generic/apt_get_sqq--sample.out b/tests/fixtures/generic/apt_get_sqq--sample.out new file mode 100644 index 00000000..add7b221 --- /dev/null +++ b/tests/fixtures/generic/apt_get_sqq--sample.out @@ -0,0 +1,9 @@ +Inst dpkg [1.19.7] (1.19.8 Debian:10.13/oldstable, Debian-Security:10/oldstable [amd64]) +Inst dpkg (1.19.8 Debian:10.13/oldstable, Debian-Security:10/oldstable [amd64]) +Conf dpkg [1.19.7] (1.19.8 Debian:10.13/oldstable, Debian-Security:10/oldstable [amd64]) +Remv dpkg [1.19.7] (1.19.8 Debian:10.13/oldstable, Debian-Security:10/oldstable [amd64]) + +Inst base-files [10.3+deb10u4] (10.3+deb10u13 Debian:10.13/oldstable [amd64]) +Conf base-files (10.3+deb10u13 Debian:10.13/oldstable [amd64]) +Inst dpkg [1.19.7] (1.19.8 Debian:10.13/oldstable, Debian-Security:10/oldstable [amd64]) +Conf dpkg (1.19.8 Debian:10.13/oldstable, Debian-Security:10/oldstable [amd64]) diff --git a/tests/test_apt_get_sqq.py b/tests/test_apt_get_sqq.py new file mode 100644 index 00000000..a0243179 --- /dev/null +++ b/tests/test_apt_get_sqq.py @@ -0,0 +1,28 @@ +import unittest + +from tests import utils_for_test as test_utils + +# Execute these steps for standard tests: +# - Save this file as `test_{parser_name}.py` since the helper methods extract parser names from the filename. +# - Organize fixtures in `tests/fixtures` for optimal structure. +# - Format fixtures as follows (using double dashes): +# - `{parser_name}--{some_test_description}.out` for command output. +# - `{parser_name}--{some_test_description}.json` for expected JSON after parsing. + +class MyTests(unittest.TestCase): + + def test_apt_get_sqq_nodata(self): + """ + Test 'apt-get -sqq' with no data + """ + test_utils.run_no_data(self, __file__, []) + + def test_apt_get_sqq_all_fixtures(self): + """ + Test 'apt-get -sqq' with various fixtures + """ + test_utils.run_all_fixtures(self, __file__) + + +if __name__ == '__main__': + unittest.main()