From 3cefa8bd9514337db23eb68207f09a8dc4d1be37 Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Tue, 14 May 2019 15:25:50 +0300 Subject: [PATCH] Add testing for Linux distribution detection --- src/steps/os/linux.rs | 77 +++++++++++++++++++++++++++----- src/steps/os/os_release/antergos | 10 +++++ src/steps/os/os_release/arch | 9 ++++ src/steps/os/os_release/centos | 16 +++++++ src/steps/os/os_release/debian | 8 ++++ src/steps/os/os_release/fedora | 21 +++++++++ src/steps/os/os_release/mint | 11 +++++ src/steps/os/os_release/opensuse | 10 +++++ src/steps/os/os_release/oracle | 16 +++++++ src/steps/os/os_release/ubuntu | 12 +++++ 10 files changed, 180 insertions(+), 10 deletions(-) create mode 100644 src/steps/os/os_release/antergos create mode 100644 src/steps/os/os_release/arch create mode 100644 src/steps/os/os_release/centos create mode 100644 src/steps/os/os_release/debian create mode 100644 src/steps/os/os_release/fedora create mode 100644 src/steps/os/os_release/mint create mode 100644 src/steps/os/os_release/opensuse create mode 100644 src/steps/os/os_release/oracle create mode 100644 src/steps/os/os_release/ubuntu diff --git a/src/steps/os/linux.rs b/src/steps/os/linux.rs index 2773e2ca..4996cfdc 100644 --- a/src/steps/os/linux.rs +++ b/src/steps/os/linux.rs @@ -18,7 +18,7 @@ struct OsRelease { id: String, } -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum Distribution { Arch, CentOS, @@ -31,17 +31,13 @@ pub enum Distribution { } impl Distribution { - fn parse_os_release() -> Result { - let os_release: OsRelease = - serde_ini::de::from_read(fs::File::open(OS_RELEASE_PATH).context(ErrorKind::UnknownLinuxDistribution)?) - .context(ErrorKind::UnknownLinuxDistribution)?; - + fn parse_os_release(os_release: &OsRelease) -> Result { Ok( match (os_release.id.as_ref(), os_release.id_like.as_ref().map(String::as_str)) { - (_, Some("debian")) | (_, Some("ubuntu")) => Distribution::Debian, + ("debian", _) | (_, Some("debian")) | (_, Some("ubuntu")) => Distribution::Debian, (_, Some("\"suse\"")) => Distribution::Suse, - ("arch", _) | (_, Some("archlinux")) => Distribution::Arch, - ("centos", _) | ("\"ol\"", _) => Distribution::CentOS, + ("arch", _) | (_, Some("archlinux")) | (_, Some("\"arch\"")) => Distribution::Arch, + ("\"centos\"", _) | ("\"ol\"", _) => Distribution::CentOS, ("fedora", _) => Distribution::Fedora, ("void", _) => Distribution::Void, ("solus", _) => Distribution::Solus, @@ -52,7 +48,11 @@ impl Distribution { pub fn detect() -> Result { if PathBuf::from(OS_RELEASE_PATH).exists() { - return Self::parse_os_release(); + let os_release: OsRelease = + serde_ini::de::from_read(fs::File::open(OS_RELEASE_PATH).context(ErrorKind::UnknownLinuxDistribution)?) + .context(ErrorKind::UnknownLinuxDistribution)?; + + return Self::parse_os_release(&os_release); } if PathBuf::from("/etc/gentoo-release").exists() { @@ -311,3 +311,60 @@ pub fn run_etc_update(sudo: Option<&PathBuf>, run_type: RunType) -> Result<(), E run_type.execute(sudo).arg(&etc_update.to_str().unwrap()).check_run() } + +#[cfg(test)] +mod tests { + use super::*; + + fn test_template(os_release_file: &str, expected_distribution: Distribution) { + let os_release: OsRelease = serde_ini::de::from_str(os_release_file).unwrap(); + assert_eq!( + Distribution::parse_os_release(&os_release).unwrap(), + expected_distribution + ); + } + #[test] + fn test_arch_linux() { + test_template(&include_str!("os_release/arch"), Distribution::Arch); + } + + #[test] + fn test_centos() { + test_template(&include_str!("os_release/centos"), Distribution::CentOS); + } + + #[test] + fn test_debian() { + test_template(&include_str!("os_release/debian"), Distribution::Debian); + } + + #[test] + fn test_ubuntu() { + test_template(&include_str!("os_release/ubuntu"), Distribution::Debian); + } + + #[test] + fn test_mint() { + test_template(&include_str!("os_release/mint"), Distribution::Debian); + } + + #[test] + fn test_opensuse() { + test_template(&include_str!("os_release/opensuse"), Distribution::Suse); + } + + #[test] + fn test_oraclelinux() { + test_template(&include_str!("os_release/oracle"), Distribution::CentOS); + } + + #[test] + fn test_fedora() { + test_template(&include_str!("os_release/fedora"), Distribution::Fedora); + } + + #[test] + fn test_antergos() { + test_template(&include_str!("os_release/antergos"), Distribution::Arch); + } +} diff --git a/src/steps/os/os_release/antergos b/src/steps/os/os_release/antergos new file mode 100644 index 00000000..b9542698 --- /dev/null +++ b/src/steps/os/os_release/antergos @@ -0,0 +1,10 @@ +NAME="Antergos Linux" +VERSION="18.7-ISO-Rolling" +ID="antergos" +ID_LIKE="arch" +PRETTY_NAME="Antergos Linux" +CPE_NAME="cpe:/o:antergosproject:antergos:18.7" +ANSI_COLOR="1;34;40" +HOME_URL="antergos.com" +SUPPORT_URL="forum.antergos.com" +BUG_REPORT_URL="@antergos" diff --git a/src/steps/os/os_release/arch b/src/steps/os/os_release/arch new file mode 100644 index 00000000..87ad4622 --- /dev/null +++ b/src/steps/os/os_release/arch @@ -0,0 +1,9 @@ +NAME="Arch Linux" +PRETTY_NAME="Arch Linux" +ID=arch +BUILD_ID=rolling +ANSI_COLOR="0;36" +HOME_URL="https://www.archlinux.org/" +DOCUMENTATION_URL="https://wiki.archlinux.org/" +SUPPORT_URL="https://bbs.archlinux.org/" +BUG_REPORT_URL="https://bugs.archlinux.org/" diff --git a/src/steps/os/os_release/centos b/src/steps/os/os_release/centos new file mode 100644 index 00000000..7037a940 --- /dev/null +++ b/src/steps/os/os_release/centos @@ -0,0 +1,16 @@ +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/src/steps/os/os_release/debian b/src/steps/os/os_release/debian new file mode 100644 index 00000000..120c51b0 --- /dev/null +++ b/src/steps/os/os_release/debian @@ -0,0 +1,8 @@ +PRETTY_NAME="Debian GNU/Linux 8 (jessie)" +NAME="Debian GNU/Linux" +VERSION_ID="8" +VERSION="8 (jessie)" +ID=debian +HOME_URL="http://www.debian.org/" +SUPPORT_URL="http://www.debian.org/support" +BUG_REPORT_URL="https://bugs.debian.org/" diff --git a/src/steps/os/os_release/fedora b/src/steps/os/os_release/fedora new file mode 100644 index 00000000..1bf2bdb1 --- /dev/null +++ b/src/steps/os/os_release/fedora @@ -0,0 +1,21 @@ +NAME=Fedora +VERSION="29 (Container Image)" +ID=fedora +VERSION_ID=29 +VERSION_CODENAME="" +PLATFORM_ID="platform:f29" +PRETTY_NAME="Fedora 29 (Container Image)" +ANSI_COLOR="0;34" +LOGO=fedora-logo-icon +CPE_NAME="cpe:/o:fedoraproject:fedora:29" +HOME_URL="https://fedoraproject.org/" +DOCUMENTATION_URL="https://docs.fedoraproject.org/en-US/fedora/f29/system-administrators-guide/" +SUPPORT_URL="https://fedoraproject.org/wiki/Communicating_and_getting_help" +BUG_REPORT_URL="https://bugzilla.redhat.com/" +REDHAT_BUGZILLA_PRODUCT="Fedora" +REDHAT_BUGZILLA_PRODUCT_VERSION=29 +REDHAT_SUPPORT_PRODUCT="Fedora" +REDHAT_SUPPORT_PRODUCT_VERSION=29 +PRIVACY_POLICY_URL="https://fedoraproject.org/wiki/Legal:PrivacyPolicy" +VARIANT="Container Image" +VARIANT_ID=container diff --git a/src/steps/os/os_release/mint b/src/steps/os/os_release/mint new file mode 100644 index 00000000..88600993 --- /dev/null +++ b/src/steps/os/os_release/mint @@ -0,0 +1,11 @@ +NAME="Linux Mint" +VERSION="18.2 (Sonya)" +ID=linuxmint +ID_LIKE=ubuntu +PRETTY_NAME="Linux Mint 18.2" +VERSION_ID="18.2" +HOME_URL="http://www.linuxmint.com/" +SUPPORT_URL="http://forums.linuxmint.com/" +BUG_REPORT_URL="http://bugs.launchpad.net/linuxmint/" +VERSION_CODENAME=sonya +UBUNTU_CODENAME=xenial diff --git a/src/steps/os/os_release/opensuse b/src/steps/os/os_release/opensuse new file mode 100644 index 00000000..206c74cd --- /dev/null +++ b/src/steps/os/os_release/opensuse @@ -0,0 +1,10 @@ +NAME="openSUSE Leap" +VERSION="42.3" +ID=opensuse +ID_LIKE="suse" +VERSION_ID="42.3" +PRETTY_NAME="openSUSE Leap 42.3" +ANSI_COLOR="0;32" +CPE_NAME="cpe:/o:opensuse:leap:42.3" +BUG_REPORT_URL="https://bugs.opensuse.org" +HOME_URL="https://www.opensuse.org/" diff --git a/src/steps/os/os_release/oracle b/src/steps/os/os_release/oracle new file mode 100644 index 00000000..04206ee1 --- /dev/null +++ b/src/steps/os/os_release/oracle @@ -0,0 +1,16 @@ +NAME="Oracle Linux Server" +VERSION="7.6" +ID="ol" +VARIANT="Server" +VARIANT_ID="server" +VERSION_ID="7.6" +PRETTY_NAME="Oracle Linux Server 7.6" +ANSI_COLOR="0;31" +CPE_NAME="cpe:/o:oracle:linux:7:6:server" +HOME_URL="https://linux.oracle.com/" +BUG_REPORT_URL="https://bugzilla.oracle.com/" + +ORACLE_BUGZILLA_PRODUCT="Oracle Linux 7" +ORACLE_BUGZILLA_PRODUCT_VERSION=7.6 +ORACLE_SUPPORT_PRODUCT="Oracle Linux" +ORACLE_SUPPORT_PRODUCT_VERSION=7.6 diff --git a/src/steps/os/os_release/ubuntu b/src/steps/os/os_release/ubuntu new file mode 100644 index 00000000..6a4c8f7a --- /dev/null +++ b/src/steps/os/os_release/ubuntu @@ -0,0 +1,12 @@ +NAME="Ubuntu" +VERSION="18.04.2 LTS (Bionic Beaver)" +ID=ubuntu +ID_LIKE=debian +PRETTY_NAME="Ubuntu 18.04.2 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