Add testing for Linux distribution detection

This commit is contained in:
Roey Darwish Dror
2019-05-14 15:25:50 +03:00
parent 80587aa03f
commit 3cefa8bd95
10 changed files with 180 additions and 10 deletions

View File

@@ -18,7 +18,7 @@ struct OsRelease {
id: String, id: String,
} }
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Distribution { pub enum Distribution {
Arch, Arch,
CentOS, CentOS,
@@ -31,17 +31,13 @@ pub enum Distribution {
} }
impl Distribution { impl Distribution {
fn parse_os_release() -> Result<Self, Error> { fn parse_os_release(os_release: &OsRelease) -> Result<Self, Error> {
let os_release: OsRelease =
serde_ini::de::from_read(fs::File::open(OS_RELEASE_PATH).context(ErrorKind::UnknownLinuxDistribution)?)
.context(ErrorKind::UnknownLinuxDistribution)?;
Ok( Ok(
match (os_release.id.as_ref(), os_release.id_like.as_ref().map(String::as_str)) { 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, (_, Some("\"suse\"")) => Distribution::Suse,
("arch", _) | (_, Some("archlinux")) => Distribution::Arch, ("arch", _) | (_, Some("archlinux")) | (_, Some("\"arch\"")) => Distribution::Arch,
("centos", _) | ("\"ol\"", _) => Distribution::CentOS, ("\"centos\"", _) | ("\"ol\"", _) => Distribution::CentOS,
("fedora", _) => Distribution::Fedora, ("fedora", _) => Distribution::Fedora,
("void", _) => Distribution::Void, ("void", _) => Distribution::Void,
("solus", _) => Distribution::Solus, ("solus", _) => Distribution::Solus,
@@ -52,7 +48,11 @@ impl Distribution {
pub fn detect() -> Result<Self, Error> { pub fn detect() -> Result<Self, Error> {
if PathBuf::from(OS_RELEASE_PATH).exists() { 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() { 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() 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);
}
}

View File

@@ -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"

View File

@@ -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/"

View File

@@ -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"

View File

@@ -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/"

View File

@@ -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

View File

@@ -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

View File

@@ -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/"

View File

@@ -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

View File

@@ -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