From fe86641a4c352ab70816167ee7dff173ce7e1b25 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 24 May 2022 11:53:38 -0700 Subject: [PATCH] Improve RHEL detection in /etc/os-release --- os_info/src/linux/file_release.rs | 28 +++++++++++++++++++---- os_info/src/linux/tests/os-release-rhel-7 | 17 ++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 os_info/src/linux/tests/os-release-rhel-7 diff --git a/os_info/src/linux/file_release.rs b/os_info/src/linux/file_release.rs index f38abfed..8ca40ace 100644 --- a/os_info/src/linux/file_release.rs +++ b/os_info/src/linux/file_release.rs @@ -54,7 +54,14 @@ fn retrieve(distributions: &[ReleaseInfo]) -> Option { } fn get_type(name: &str) -> Option { - match name.to_lowercase().as_ref() { + let name = name.to_lowercase(); + + // RHEL sometimes has a suffix, like Server or Workstation + if name.starts_with("red hat enterprise linux") { + return Some(Type::RedHatEnterprise); + } + + match name.as_str() { "alpine linux" => Some(Type::Alpine), "amazon linux" => Some(Type::Amazon), "amazon linux ami" => Some(Type::Amazon), @@ -66,7 +73,6 @@ fn get_type(name: &str) -> Option { "linux mint" => Some(Type::Mint), "mariner" => Some(Type::Mariner), "nixos" => Some(Type::NixOS), - "red hat enterprise linux" => Some(Type::Redhat), "sles" => Some(Type::SUSE), "ubuntu" => Some(Type::Ubuntu), _ => None, @@ -116,7 +122,7 @@ const DISTRIBUTIONS: [ReleaseInfo; 6] = [ version_matcher: Matcher::KeyValue { key: "VERSION_ID" }, }, ReleaseInfo { - os_type: Type::Redhat, + os_type: Type::RedHatEnterprise, path: "/etc/redhat-release", version_matcher: Matcher::PrefixedVersion { prefix: "release" }, }, @@ -244,12 +250,24 @@ mod tests { distributions[0].path = "src/linux/tests/os-release-rhel"; let info = retrieve(&distributions).unwrap(); - assert_eq!(info.os_type(), Type::Redhat); + assert_eq!(info.os_type(), Type::RedHatEnterprise); assert_eq!(info.version, Version::Semantic(8, 2, 0)); assert_eq!(info.edition, None); assert_eq!(info.codename, None); } + #[test] + fn os_release_rhel_7() { + let mut distributions = [DISTRIBUTIONS[4].clone()]; + distributions[0].path = "src/linux/tests/os-release-rhel-7"; + + let info = retrieve(&distributions).unwrap(); + assert_eq!(info.os_type(), Type::RedHatEnterprise); + assert_eq!(info.version, Version::Semantic(7, 9, 0)); + assert_eq!(info.edition, None); + assert_eq!(info.codename, None); + } + #[test] fn os_release_suse_12() { let mut distributions = [DISTRIBUTIONS[4].clone()]; @@ -328,7 +346,7 @@ mod tests { distributions[0].path = "src/linux/tests/redhat-release"; let info = retrieve(&distributions).unwrap(); - assert_eq!(info.os_type(), Type::Redhat); + assert_eq!(info.os_type(), Type::RedHatEnterprise); assert_eq!(info.version, Version::Custom("XX".to_owned())); assert_eq!(info.edition, None); assert_eq!(info.codename, None); diff --git a/os_info/src/linux/tests/os-release-rhel-7 b/os_info/src/linux/tests/os-release-rhel-7 new file mode 100644 index 00000000..ec038b12 --- /dev/null +++ b/os_info/src/linux/tests/os-release-rhel-7 @@ -0,0 +1,17 @@ +NAME="Red Hat Enterprise Linux Server" +VERSION="7.9 (Maipo)" +ID="rhel" +ID_LIKE="fedora" +VARIANT="Server" +VARIANT_ID="server" +VERSION_ID="7.9" +PRETTY_NAME="Red Hat Enterprise Linux Server 7.9 (Maipo)" +ANSI_COLOR="0;31" +CPE_NAME="cpe:/o:redhat:enterprise_linux:7.9:GA:server" +HOME_URL="https://www.redhat.com/" +BUG_REPORT_URL="https://bugzilla.redhat.com/" + +REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 7" +REDHAT_BUGZILLA_PRODUCT_VERSION=7.9 +REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux" +REDHAT_SUPPORT_PRODUCT_VERSION="7.9"