Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update methods in OSReleaseUtil #1038

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions src/main/java/emissary/util/os/OSReleaseUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
jpdahlke marked this conversation as resolved.
Show resolved Hide resolved
return getMajorReleaseVersion(OS_RELEASE_PATH);
}

static String getMajorVersion(Path osReleasePath) {
static String getMajorReleaseVersion(Path osReleasePath) {
return String.format("%.0f", Float.parseFloat(getVersionId(osReleasePath)));
}

Expand All @@ -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<String> 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
}
}
Expand Down
58 changes: 40 additions & 18 deletions src/test/java/emissary/util/os/OSReleaseUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}
Loading