diff --git a/src/main/java/emissary/util/os/OSReleaseUtil.java b/src/main/java/emissary/util/os/OSReleaseUtil.java index 6a70169fc4..5105998f1a 100644 --- a/src/main/java/emissary/util/os/OSReleaseUtil.java +++ b/src/main/java/emissary/util/os/OSReleaseUtil.java @@ -2,6 +2,7 @@ import org.apache.commons.lang3.StringUtils; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Optional; @@ -44,11 +45,11 @@ static String getVersionId(Path osReleasePath) { * * @return the major OS version */ - public static String getMajorVersion() { - return getMajorVersion(OS_RELEASE_PATH); + public static String getMajorReleaseVersion() { + return getMajorReleaseVersion(OS_RELEASE_PATH); } - static String getMajorVersion(Path osReleasePath) { + static String getMajorReleaseVersion(Path osReleasePath) { return String.format("%.0f", Float.parseFloat(getVersionId(osReleasePath))); } @@ -62,10 +63,40 @@ public static boolean isUbuntu() { } static boolean isUbuntu(Path osReleasePath) { + return isOsName(osReleasePath, "ubuntu"); + } + + /** + * Use the /etc/os-release file to determine if the OS is CentOS + * + * @return true if CentOS is found, false otherwise + */ + public static boolean isCentOs() { + return isCentOs(OS_RELEASE_PATH); + } + + static boolean isCentOs(Path osReleasePath) { + return isOsName(osReleasePath, "centos"); + } + + /** + * Use the /etc/os-release file to determine if the OS is RHEL + * + * @return true if RHEL is found, false otherwise + */ + public static boolean isRhel() { + return isRhel(OS_RELEASE_PATH); + } + + static boolean isRhel(Path osReleasePath) { + return isOsName(osReleasePath, "rhel"); + } + + private static boolean isOsName(Path osReleasePath, String osName) { if (Files.exists(osReleasePath)) { try (Stream lines = Files.lines(osReleasePath)) { - return lines.anyMatch(s -> StringUtils.containsIgnoreCase(s, "ubuntu")); - } catch (Exception e) { + return lines.filter(line -> line.startsWith("ID=")).anyMatch(entry -> StringUtils.containsIgnoreCase(entry, osName)); + } catch (IOException ignored) { // ignore } } diff --git a/src/test/java/emissary/util/os/OSReleaseUtilTest.java b/src/test/java/emissary/util/os/OSReleaseUtilTest.java index f86388f0da..a84c7a5faa 100644 --- a/src/test/java/emissary/util/os/OSReleaseUtilTest.java +++ b/src/test/java/emissary/util/os/OSReleaseUtilTest.java @@ -2,6 +2,7 @@ import emissary.util.io.ResourceReader; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.nio.file.Path; @@ -12,33 +13,54 @@ class OSReleaseUtilTest { - @Test - void testGetVersionId() throws Exception { + @SuppressWarnings("NonFinalStaticField") + static Path centos7Path; + @SuppressWarnings("NonFinalStaticField") + static Path rhel8Path; + @SuppressWarnings("NonFinalStaticField") + static Path ubuntu20Path; + + @BeforeAll + static void getResources() throws Exception { ResourceReader rr = new ResourceReader(); - assertEquals("7", OSReleaseUtil.getVersionId(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "centos7")).toURI()))); - assertEquals("8.10", OSReleaseUtil.getVersionId(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "rhel8")).toURI()))); - assertEquals("20.04", - OSReleaseUtil.getVersionId(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "ubuntu")).toURI()))); + centos7Path = Path.of(rr.getResource(rr.getResourceName(OSReleaseUtil.class.getPackage(), "centos7")).toURI()); + rhel8Path = Path.of(rr.getResource(rr.getResourceName(OSReleaseUtil.class.getPackage(), "rhel8")).toURI()); + ubuntu20Path = Path.of(rr.getResource(rr.getResourceName(OSReleaseUtil.class.getPackage(), "ubuntu20")).toURI()); } @Test - void testGetMajorVersion() throws Exception { - ResourceReader rr = new ResourceReader(); + void testGetVersionId() { + assertEquals("7", OSReleaseUtil.getVersionId(centos7Path)); + assertEquals("8.10", OSReleaseUtil.getVersionId(rhel8Path)); + assertEquals("20.04", OSReleaseUtil.getVersionId(ubuntu20Path)); + } - assertEquals("7", - OSReleaseUtil.getMajorVersion(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "centos7")).toURI()))); - assertEquals("8", OSReleaseUtil.getMajorVersion(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "rhel8")).toURI()))); - assertEquals("20", - OSReleaseUtil.getMajorVersion(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "ubuntu")).toURI()))); + @Test + void testGetMajorVersion() { + assertEquals("7", OSReleaseUtil.getMajorReleaseVersion(centos7Path)); + assertEquals("8", OSReleaseUtil.getMajorReleaseVersion(rhel8Path)); + assertEquals("20", OSReleaseUtil.getMajorReleaseVersion(ubuntu20Path)); } @Test - void testIsUbuntu() throws Exception { - ResourceReader rr = new ResourceReader(); + void testIsUbuntu() { + assertFalse(OSReleaseUtil.isUbuntu(centos7Path)); + assertFalse(OSReleaseUtil.isUbuntu(rhel8Path)); + assertTrue(OSReleaseUtil.isUbuntu(ubuntu20Path)); + } - assertFalse(OSReleaseUtil.isUbuntu(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "centos7")).toURI()))); - assertFalse(OSReleaseUtil.isUbuntu(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "rhel8")).toURI()))); - assertTrue(OSReleaseUtil.isUbuntu(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "ubuntu")).toURI()))); + @Test + void testIsCentOS() { + assertTrue(OSReleaseUtil.isCentOs(centos7Path)); + assertFalse(OSReleaseUtil.isCentOs(rhel8Path)); + assertFalse(OSReleaseUtil.isCentOs(ubuntu20Path)); + } + + @Test + void testIsRhel() { + assertFalse(OSReleaseUtil.isRhel(centos7Path)); + assertTrue(OSReleaseUtil.isRhel(rhel8Path)); + assertFalse(OSReleaseUtil.isRhel(ubuntu20Path)); } } diff --git a/src/test/resources/emissary/util/os/ubuntu b/src/test/resources/emissary/util/os/ubuntu20 similarity index 100% rename from src/test/resources/emissary/util/os/ubuntu rename to src/test/resources/emissary/util/os/ubuntu20