From e643badaf77d85d767044f12c0c6a45a94dbf6e9 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 1 Oct 2023 17:42:00 -0700 Subject: [PATCH] add os-release parser --- CHANGELOG | 1 + README.md | 1 + completions/jc_bash_completion.sh | 2 +- completions/jc_zsh_completion.sh | 3 +- jc/lib.py | 1 + jc/parsers/os_release.py | 113 ++++++++++++++++++ man/jc.1 | 5 + tests/fixtures/generic/os-release-centos | 15 +++ tests/fixtures/generic/os-release-centos.json | 1 + tests/fixtures/generic/os-release-ubuntu | 12 ++ tests/fixtures/generic/os-release-ubuntu.json | 1 + tests/test_os_release.py | 59 +++++++++ 12 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 jc/parsers/os_release.py create mode 100644 tests/fixtures/generic/os-release-centos create mode 100644 tests/fixtures/generic/os-release-centos.json create mode 100644 tests/fixtures/generic/os-release-ubuntu create mode 100644 tests/fixtures/generic/os-release-ubuntu.json create mode 100644 tests/test_os_release.py diff --git a/CHANGELOG b/CHANGELOG index e7002048..c01ab8a7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,7 @@ jc changelog 20231001 v1.23.5 - Add `host` command parser - Add `nsd-control` command parser +- Add `/etc/os-release` file parser - Enhance `xml` parser to include comments in the JSON output - Fix `pidstat` command parser when using `-T ALL` - Fix `x509-cert` parser to allow negative serial numbers diff --git a/README.md b/README.md index 283ee93b..db234e42 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,7 @@ option. | `--ntpq` | `ntpq -p` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/ntpq) | | `--openvpn` | openvpn-status.log file parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/openvpn) | | `--os-prober` | `os-prober` command parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/os_prober) | +| `--os-release` | `/etc/os-release` file parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/os_release) | | `--passwd` | `/etc/passwd` file parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/passwd) | | `--pci-ids` | `pci.ids` file parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/pci_ids) | | `--pgpass` | PostgreSQL password file parser | [details](https://kellyjonbrazil.github.io/jc/docs/parsers/pgpass) | diff --git a/completions/jc_bash_completion.sh b/completions/jc_bash_completion.sh index c78602c9..f163c931 100644 --- a/completions/jc_bash_completion.sh +++ b/completions/jc_bash_completion.sh @@ -4,7 +4,7 @@ _jc() jc_about_options jc_about_mod_options jc_help_options jc_special_options jc_commands=(acpi airport arp blkid bluetoothctl cbt certbot chage cksum crontab date df dig dmidecode dpkg du env file findmnt finger free git gpg hciconfig host id ifconfig iostat ip iptables iw iwconfig jobs last lastb ls lsattr lsblk lsmod lsof lspci lsusb md5 md5sum mdadm mount mpstat netstat nmcli nsd-control ntpq os-prober pidstat ping ping6 pip pip3 postconf printenv ps route rpm rsync sfdisk sha1sum sha224sum sha256sum sha384sum sha512sum shasum ss ssh sshd stat sum sysctl systemctl systeminfo timedatectl top tracepath tracepath6 traceroute traceroute6 udevadm ufw uname update-alternatives upower uptime vdir veracrypt vmstat w wc who xrandr zipinfo zpool) - jc_parsers=(--acpi --airport --airport-s --arp --asciitable --asciitable-m --blkid --bluetoothctl --cbt --cef --cef-s --certbot --chage --cksum --clf --clf-s --crontab --crontab-u --csv --csv-s --date --datetime-iso --df --dig --dir --dmidecode --dpkg-l --du --email-address --env --file --find --findmnt --finger --free --fstab --git-log --git-log-s --git-ls-remote --gpg --group --gshadow --hash --hashsum --hciconfig --history --host --hosts --id --ifconfig --ini --ini-dup --iostat --iostat-s --ip-address --iptables --ip-route --iw-scan --iwconfig --jar-manifest --jobs --jwt --kv --last --ls --ls-s --lsattr --lsblk --lsmod --lsof --lspci --lsusb --m3u --mdadm --mount --mpstat --mpstat-s --netstat --nmcli --nsd-control --ntpq --openvpn --os-prober --passwd --pci-ids --pgpass --pidstat --pidstat-s --ping --ping-s --pip-list --pip-show --plist --postconf --proc --proc-buddyinfo --proc-consoles --proc-cpuinfo --proc-crypto --proc-devices --proc-diskstats --proc-filesystems --proc-interrupts --proc-iomem --proc-ioports --proc-loadavg --proc-locks --proc-meminfo --proc-modules --proc-mtrr --proc-pagetypeinfo --proc-partitions --proc-slabinfo --proc-softirqs --proc-stat --proc-swaps --proc-uptime --proc-version --proc-vmallocinfo --proc-vmstat --proc-zoneinfo --proc-driver-rtc --proc-net-arp --proc-net-dev --proc-net-dev-mcast --proc-net-if-inet6 --proc-net-igmp --proc-net-igmp6 --proc-net-ipv6-route --proc-net-netlink --proc-net-netstat --proc-net-packet --proc-net-protocols --proc-net-route --proc-net-tcp --proc-net-unix --proc-pid-fdinfo --proc-pid-io --proc-pid-maps --proc-pid-mountinfo --proc-pid-numa-maps --proc-pid-smaps --proc-pid-stat --proc-pid-statm --proc-pid-status --ps --resolve-conf --route --rpm-qi --rsync --rsync-s --semver --sfdisk --shadow --srt --ss --ssh-conf --sshd-conf --stat --stat-s --sysctl --syslog --syslog-s --syslog-bsd --syslog-bsd-s --systemctl --systemctl-lj --systemctl-ls --systemctl-luf --systeminfo --time --timedatectl --timestamp --toml --top --top-s --tracepath --traceroute --udevadm --ufw --ufw-appinfo --uname --update-alt-gs --update-alt-q --upower --uptime --url --ver --veracrypt --vmstat --vmstat-s --w --wc --who --x509-cert --x509-csr --xml --xrandr --yaml --zipinfo --zpool-iostat --zpool-status) + jc_parsers=(--acpi --airport --airport-s --arp --asciitable --asciitable-m --blkid --bluetoothctl --cbt --cef --cef-s --certbot --chage --cksum --clf --clf-s --crontab --crontab-u --csv --csv-s --date --datetime-iso --df --dig --dir --dmidecode --dpkg-l --du --email-address --env --file --find --findmnt --finger --free --fstab --git-log --git-log-s --git-ls-remote --gpg --group --gshadow --hash --hashsum --hciconfig --history --host --hosts --id --ifconfig --ini --ini-dup --iostat --iostat-s --ip-address --iptables --ip-route --iw-scan --iwconfig --jar-manifest --jobs --jwt --kv --last --ls --ls-s --lsattr --lsblk --lsmod --lsof --lspci --lsusb --m3u --mdadm --mount --mpstat --mpstat-s --netstat --nmcli --nsd-control --ntpq --openvpn --os-prober --os-release --passwd --pci-ids --pgpass --pidstat --pidstat-s --ping --ping-s --pip-list --pip-show --plist --postconf --proc --proc-buddyinfo --proc-consoles --proc-cpuinfo --proc-crypto --proc-devices --proc-diskstats --proc-filesystems --proc-interrupts --proc-iomem --proc-ioports --proc-loadavg --proc-locks --proc-meminfo --proc-modules --proc-mtrr --proc-pagetypeinfo --proc-partitions --proc-slabinfo --proc-softirqs --proc-stat --proc-swaps --proc-uptime --proc-version --proc-vmallocinfo --proc-vmstat --proc-zoneinfo --proc-driver-rtc --proc-net-arp --proc-net-dev --proc-net-dev-mcast --proc-net-if-inet6 --proc-net-igmp --proc-net-igmp6 --proc-net-ipv6-route --proc-net-netlink --proc-net-netstat --proc-net-packet --proc-net-protocols --proc-net-route --proc-net-tcp --proc-net-unix --proc-pid-fdinfo --proc-pid-io --proc-pid-maps --proc-pid-mountinfo --proc-pid-numa-maps --proc-pid-smaps --proc-pid-stat --proc-pid-statm --proc-pid-status --ps --resolve-conf --route --rpm-qi --rsync --rsync-s --semver --sfdisk --shadow --srt --ss --ssh-conf --sshd-conf --stat --stat-s --sysctl --syslog --syslog-s --syslog-bsd --syslog-bsd-s --systemctl --systemctl-lj --systemctl-ls --systemctl-luf --systeminfo --time --timedatectl --timestamp --toml --top --top-s --tracepath --traceroute --udevadm --ufw --ufw-appinfo --uname --update-alt-gs --update-alt-q --upower --uptime --url --ver --veracrypt --vmstat --vmstat-s --w --wc --who --x509-cert --x509-csr --xml --xrandr --yaml --zipinfo --zpool-iostat --zpool-status) jc_options=(--force-color -C --debug -d --monochrome -m --meta-out -M --pretty -p --quiet -q --raw -r --unbuffer -u --yaml-out -y) jc_about_options=(--about -a) jc_about_mod_options=(--pretty -p --yaml-out -y --monochrome -m --force-color -C) diff --git a/completions/jc_zsh_completion.sh b/completions/jc_zsh_completion.sh index 9e4cc9ba..f4f0846a 100644 --- a/completions/jc_zsh_completion.sh +++ b/completions/jc_zsh_completion.sh @@ -111,7 +111,7 @@ _jc() { 'zipinfo:run "zipinfo" command with magic syntax.' 'zpool:run "zpool" command with magic syntax.' ) - jc_parsers=(--acpi --airport --airport-s --arp --asciitable --asciitable-m --blkid --bluetoothctl --cbt --cef --cef-s --certbot --chage --cksum --clf --clf-s --crontab --crontab-u --csv --csv-s --date --datetime-iso --df --dig --dir --dmidecode --dpkg-l --du --email-address --env --file --find --findmnt --finger --free --fstab --git-log --git-log-s --git-ls-remote --gpg --group --gshadow --hash --hashsum --hciconfig --history --host --hosts --id --ifconfig --ini --ini-dup --iostat --iostat-s --ip-address --iptables --ip-route --iw-scan --iwconfig --jar-manifest --jobs --jwt --kv --last --ls --ls-s --lsattr --lsblk --lsmod --lsof --lspci --lsusb --m3u --mdadm --mount --mpstat --mpstat-s --netstat --nmcli --nsd-control --ntpq --openvpn --os-prober --passwd --pci-ids --pgpass --pidstat --pidstat-s --ping --ping-s --pip-list --pip-show --plist --postconf --proc --proc-buddyinfo --proc-consoles --proc-cpuinfo --proc-crypto --proc-devices --proc-diskstats --proc-filesystems --proc-interrupts --proc-iomem --proc-ioports --proc-loadavg --proc-locks --proc-meminfo --proc-modules --proc-mtrr --proc-pagetypeinfo --proc-partitions --proc-slabinfo --proc-softirqs --proc-stat --proc-swaps --proc-uptime --proc-version --proc-vmallocinfo --proc-vmstat --proc-zoneinfo --proc-driver-rtc --proc-net-arp --proc-net-dev --proc-net-dev-mcast --proc-net-if-inet6 --proc-net-igmp --proc-net-igmp6 --proc-net-ipv6-route --proc-net-netlink --proc-net-netstat --proc-net-packet --proc-net-protocols --proc-net-route --proc-net-tcp --proc-net-unix --proc-pid-fdinfo --proc-pid-io --proc-pid-maps --proc-pid-mountinfo --proc-pid-numa-maps --proc-pid-smaps --proc-pid-stat --proc-pid-statm --proc-pid-status --ps --resolve-conf --route --rpm-qi --rsync --rsync-s --semver --sfdisk --shadow --srt --ss --ssh-conf --sshd-conf --stat --stat-s --sysctl --syslog --syslog-s --syslog-bsd --syslog-bsd-s --systemctl --systemctl-lj --systemctl-ls --systemctl-luf --systeminfo --time --timedatectl --timestamp --toml --top --top-s --tracepath --traceroute --udevadm --ufw --ufw-appinfo --uname --update-alt-gs --update-alt-q --upower --uptime --url --ver --veracrypt --vmstat --vmstat-s --w --wc --who --x509-cert --x509-csr --xml --xrandr --yaml --zipinfo --zpool-iostat --zpool-status) + jc_parsers=(--acpi --airport --airport-s --arp --asciitable --asciitable-m --blkid --bluetoothctl --cbt --cef --cef-s --certbot --chage --cksum --clf --clf-s --crontab --crontab-u --csv --csv-s --date --datetime-iso --df --dig --dir --dmidecode --dpkg-l --du --email-address --env --file --find --findmnt --finger --free --fstab --git-log --git-log-s --git-ls-remote --gpg --group --gshadow --hash --hashsum --hciconfig --history --host --hosts --id --ifconfig --ini --ini-dup --iostat --iostat-s --ip-address --iptables --ip-route --iw-scan --iwconfig --jar-manifest --jobs --jwt --kv --last --ls --ls-s --lsattr --lsblk --lsmod --lsof --lspci --lsusb --m3u --mdadm --mount --mpstat --mpstat-s --netstat --nmcli --nsd-control --ntpq --openvpn --os-prober --os-release --passwd --pci-ids --pgpass --pidstat --pidstat-s --ping --ping-s --pip-list --pip-show --plist --postconf --proc --proc-buddyinfo --proc-consoles --proc-cpuinfo --proc-crypto --proc-devices --proc-diskstats --proc-filesystems --proc-interrupts --proc-iomem --proc-ioports --proc-loadavg --proc-locks --proc-meminfo --proc-modules --proc-mtrr --proc-pagetypeinfo --proc-partitions --proc-slabinfo --proc-softirqs --proc-stat --proc-swaps --proc-uptime --proc-version --proc-vmallocinfo --proc-vmstat --proc-zoneinfo --proc-driver-rtc --proc-net-arp --proc-net-dev --proc-net-dev-mcast --proc-net-if-inet6 --proc-net-igmp --proc-net-igmp6 --proc-net-ipv6-route --proc-net-netlink --proc-net-netstat --proc-net-packet --proc-net-protocols --proc-net-route --proc-net-tcp --proc-net-unix --proc-pid-fdinfo --proc-pid-io --proc-pid-maps --proc-pid-mountinfo --proc-pid-numa-maps --proc-pid-smaps --proc-pid-stat --proc-pid-statm --proc-pid-status --ps --resolve-conf --route --rpm-qi --rsync --rsync-s --semver --sfdisk --shadow --srt --ss --ssh-conf --sshd-conf --stat --stat-s --sysctl --syslog --syslog-s --syslog-bsd --syslog-bsd-s --systemctl --systemctl-lj --systemctl-ls --systemctl-luf --systeminfo --time --timedatectl --timestamp --toml --top --top-s --tracepath --traceroute --udevadm --ufw --ufw-appinfo --uname --update-alt-gs --update-alt-q --upower --uptime --url --ver --veracrypt --vmstat --vmstat-s --w --wc --who --x509-cert --x509-csr --xml --xrandr --yaml --zipinfo --zpool-iostat --zpool-status) jc_parsers_describe=( '--acpi:`acpi` command parser' '--airport:`airport -I` command parser' @@ -196,6 +196,7 @@ _jc() { '--ntpq:`ntpq -p` command parser' '--openvpn:openvpn-status.log file parser' '--os-prober:`os-prober` command parser' + '--os-release:`/etc/os-release` file parser' '--passwd:`/etc/passwd` file parser' '--pci-ids:`pci.ids` file parser' '--pgpass:PostgreSQL password file parser' diff --git a/jc/lib.py b/jc/lib.py index 8cee087e..d67009d7 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -96,6 +96,7 @@ parsers: List[str] = [ 'ntpq', 'openvpn', 'os-prober', + 'os-release', 'passwd', 'pci-ids', 'pgpass', diff --git a/jc/parsers/os_release.py b/jc/parsers/os_release.py new file mode 100644 index 00000000..baf7cf68 --- /dev/null +++ b/jc/parsers/os_release.py @@ -0,0 +1,113 @@ +"""jc - JSON Convert `/etc/os-release` file parser + +This parser is an alias to the Key/Value parser (`--kv`). + +Usage (cli): + + $ cat /etc/os-release | jc --os-release + +Usage (module): + + import jc + result = jc.parse('os_release', os_release_command_output) + +Schema: + + { + "": string + } + +Examples: + + $ cat /etc/os-release | jc --os-release -p + { + "NAME": "CentOS Linux", + "VERSION": "7 (Core)", + "ID": "centos", + "ID_LIKE": "rhel fedora", + "VERSION_ID": "7", + "PRETTY_NAME": "CentOS Linux 7 (Core)", + "ANSI_COLOR": "0;31", + "CPE_NAME": "cpe:/o:centos:centos:7", + "HOME_URL": "https://www.centos.org/", + "BUG_REPORT_URL": "https://bugs.centos.org/", + "CENTOS_MANTISBT_PROJECT": "CentOS-7", + "CENTOS_MANTISBT_PROJECT_VERSION": "7", + "REDHAT_SUPPORT_PRODUCT": "centos", + "REDHAT_SUPPORT_PRODUCT_VERSION": "7" + } + + $ cat /etc/os-release | jc --os-release -p -r + { + "NAME": "\\"CentOS Linux\\"", + "VERSION": "\\"7 (Core)\\"", + "ID": "\\"centos\\"", + "ID_LIKE": "\\"rhel fedora\\"", + "VERSION_ID": "\\"7\\"", + "PRETTY_NAME": "\\"CentOS Linux 7 (Core)\\"", + "ANSI_COLOR": "\\"0;31\\"", + "CPE_NAME": "\\"cpe:/o:centos:centos:7\\"", + "HOME_URL": "\\"https://www.centos.org/\\"", + "BUG_REPORT_URL": "\\"https://bugs.centos.org/\\"", + "CENTOS_MANTISBT_PROJECT": "\\"CentOS-7\\"", + "CENTOS_MANTISBT_PROJECT_VERSION": "\\"7\\"", + "REDHAT_SUPPORT_PRODUCT": "\\"centos\\"", + "REDHAT_SUPPORT_PRODUCT_VERSION": "\\"7\\"" + } +""" +from jc.jc_types import JSONDictType +import jc.parsers.kv +import jc.utils + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = '`/etc/os-release` file parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + details = 'Using the Key/Value parser' + compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] + tags = ['file', 'standard', 'string'] + + +__version__ = info.version + + +def _process(proc_data: JSONDictType) -> JSONDictType: + """ + Final processing to conform to the schema. + + Parameters: + + proc_data: (Dictionary) raw structured data to process + + Returns: + + Dictionary. Structured to conform to the schema. + """ + return jc.parsers.kv._process(proc_data) + + +def parse( + data: str, + raw: bool = False, + quiet: bool = False +) -> 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: + + Dictionary. Raw or processed structured data. + """ + jc.utils.compatibility(__name__, info.compatible, quiet) + raw_output = jc.parsers.kv.parse(data, raw, quiet) + + return raw_output if raw else _process(raw_output) diff --git a/man/jc.1 b/man/jc.1 index 382b51cc..52126a80 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -457,6 +457,11 @@ openvpn-status.log file parser \fB--os-prober\fP `os-prober` command parser +.TP +.B +\fB--os-release\fP +`/etc/os-release` file parser + .TP .B \fB--passwd\fP diff --git a/tests/fixtures/generic/os-release-centos b/tests/fixtures/generic/os-release-centos new file mode 100644 index 00000000..c276e3ae --- /dev/null +++ b/tests/fixtures/generic/os-release-centos @@ -0,0 +1,15 @@ +NAME="CentOS Linux" +VERSION="7 (Core)" +ID="centos" +ID_LIKE="rhel fedora" +VERSION_ID="7" +PRETTY_NAME="CentOS Linux 7 (Core)" +ANSI_COLOR="0;31" +CPE_NAME="cpe:/o:centos:centos:7" +HOME_URL="https://www.centos.org/" +BUG_REPORT_URL="https://bugs.centos.org/" + +CENTOS_MANTISBT_PROJECT="CentOS-7" +CENTOS_MANTISBT_PROJECT_VERSION="7" +REDHAT_SUPPORT_PRODUCT="centos" +REDHAT_SUPPORT_PRODUCT_VERSION="7" diff --git a/tests/fixtures/generic/os-release-centos.json b/tests/fixtures/generic/os-release-centos.json new file mode 100644 index 00000000..dd17463d --- /dev/null +++ b/tests/fixtures/generic/os-release-centos.json @@ -0,0 +1 @@ +{"NAME":"CentOS Linux","VERSION":"7 (Core)","ID":"centos","ID_LIKE":"rhel fedora","VERSION_ID":"7","PRETTY_NAME":"CentOS Linux 7 (Core)","ANSI_COLOR":"0;31","CPE_NAME":"cpe:/o:centos:centos:7","HOME_URL":"https://www.centos.org/","BUG_REPORT_URL":"https://bugs.centos.org/","CENTOS_MANTISBT_PROJECT":"CentOS-7","CENTOS_MANTISBT_PROJECT_VERSION":"7","REDHAT_SUPPORT_PRODUCT":"centos","REDHAT_SUPPORT_PRODUCT_VERSION":"7"} diff --git a/tests/fixtures/generic/os-release-ubuntu b/tests/fixtures/generic/os-release-ubuntu new file mode 100644 index 00000000..f1839e72 --- /dev/null +++ b/tests/fixtures/generic/os-release-ubuntu @@ -0,0 +1,12 @@ +NAME="Ubuntu" +VERSION="18.04.5 LTS (Bionic Beaver)" +ID=ubuntu +ID_LIKE=debian +PRETTY_NAME="Ubuntu 18.04.5 LTS" +VERSION_ID="18.04" +HOME_URL="https://www.ubuntu.com/" +SUPPORT_URL="https://help.ubuntu.com/" +BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" +PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" +VERSION_CODENAME=bionic +UBUNTU_CODENAME=bionic diff --git a/tests/fixtures/generic/os-release-ubuntu.json b/tests/fixtures/generic/os-release-ubuntu.json new file mode 100644 index 00000000..86607162 --- /dev/null +++ b/tests/fixtures/generic/os-release-ubuntu.json @@ -0,0 +1 @@ +{"NAME":"Ubuntu","VERSION":"18.04.5 LTS (Bionic Beaver)","ID":"ubuntu","ID_LIKE":"debian","PRETTY_NAME":"Ubuntu 18.04.5 LTS","VERSION_ID":"18.04","HOME_URL":"https://www.ubuntu.com/","SUPPORT_URL":"https://help.ubuntu.com/","BUG_REPORT_URL":"https://bugs.launchpad.net/ubuntu/","PRIVACY_POLICY_URL":"https://www.ubuntu.com/legal/terms-and-policies/privacy-policy","VERSION_CODENAME":"bionic","UBUNTU_CODENAME":"bionic"} diff --git a/tests/test_os_release.py b/tests/test_os_release.py new file mode 100644 index 00000000..f8a5e1fc --- /dev/null +++ b/tests/test_os_release.py @@ -0,0 +1,59 @@ +import os +import unittest +import json +from typing import Dict +from jc.parsers.os_release import parse + +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) + + +class MyTests(unittest.TestCase): + f_in: Dict = {} + f_json: Dict = {} + + @classmethod + def setUpClass(cls): + fixtures = { + 'os_release_centos': ( + 'fixtures/generic/os-release-centos', + 'fixtures/generic/os-release-centos.json'), + 'os_release_ubuntu': ( + 'fixtures/generic/os-release-ubuntu', + 'fixtures/generic/os-release-ubuntu.json') + } + + for file, filepaths in fixtures.items(): + with open(os.path.join(THIS_DIR, filepaths[0]), 'r', encoding='utf-8') as a, \ + open(os.path.join(THIS_DIR, filepaths[1]), 'r', encoding='utf-8') as b: + cls.f_in[file] = a.read() + cls.f_json[file] = json.loads(b.read()) + + + def test_os_release_nodata(self): + """ + Test 'os_release' with no data + """ + self.assertEqual(parse('', quiet=True), {}) + + + def test_os_release_centos(self): + """ + Test 'os_release' on Centos + """ + self.assertEqual( + parse(self.f_in['os_release_centos'], quiet=True), + self.f_json['os_release_centos'] + ) + + def test_os_release_ubuntu(self): + """ + Test 'os_release' on ubuntu + """ + self.assertEqual( + parse(self.f_in['os_release_ubuntu'], quiet=True), + self.f_json['os_release_ubuntu'] + ) + + +if __name__ == '__main__': + unittest.main()