Skip to content

Commit

Permalink
Merge pull request stanislav-tkach#311 from cuviper/os-release-rhel
Browse files Browse the repository at this point in the history
Improve RHEL detection in /etc/os-release
  • Loading branch information
stanislav-tkach authored May 25, 2022
2 parents f255a61 + fe86641 commit 8ecb4df
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
28 changes: 23 additions & 5 deletions os_info/src/linux/file_release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ fn retrieve(distributions: &[ReleaseInfo]) -> Option<Info> {
}

fn get_type(name: &str) -> Option<Type> {
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),
Expand All @@ -66,7 +73,6 @@ fn get_type(name: &str) -> Option<Type> {
"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,
Expand Down Expand Up @@ -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" },
},
Expand Down Expand Up @@ -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()];
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions os_info/src/linux/tests/os-release-rhel-7
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 8ecb4df

Please sign in to comment.