changeset 1703:b0e56da7d27f

Fall back to LsbRelease if EtcOsRelease doesn't return useful info. Reviewed-by: omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-June/014123.html PR2453
author Severin Gehwolf <sgehwolf@redhat.com>
date Thu, 18 Jun 2015 18:13:42 +0200
parents ea755f774135
children 512f81ecb539
files system-backend/src/main/java/com/redhat/thermostat/backend/system/DistributionInformation.java system-backend/src/main/java/com/redhat/thermostat/backend/system/EtcOsRelease.java system-backend/src/test/java/com/redhat/thermostat/backend/system/DistributionInformationTest.java system-backend/src/test/java/com/redhat/thermostat/backend/system/EtcOsReleaseTest.java
diffstat 4 files changed, 51 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/system-backend/src/main/java/com/redhat/thermostat/backend/system/DistributionInformation.java	Wed Jun 17 12:07:20 2015 -0400
+++ b/system-backend/src/main/java/com/redhat/thermostat/backend/system/DistributionInformation.java	Thu Jun 18 18:13:42 2015 +0200
@@ -66,7 +66,13 @@
     // package-private for testing
     static DistributionInformation get(EtcOsRelease etcOsRelease, LsbRelease lsbRelease) {
         try {
-            return etcOsRelease.getDistributionInformation();
+            DistributionInformation etcOsDistroInfo = etcOsRelease.getDistributionInformation();
+            // if both name and version are unknown defer to lsb fallback
+            if (!DistributionInformation.UNKNOWN_NAME.equals(etcOsDistroInfo.getName()) &&
+                !DistributionInformation.UNKNOWN_VERSION.equals(etcOsDistroInfo.getVersion())) {
+                return etcOsDistroInfo;
+            }
+            logger.log(Level.FINE, "/etc/os-release existing, but does not contain useful info");
         } catch (IOException e) {
             // Log only at level FINE, since we have the LSB fallback
             logger.log(Level.FINE, "unable to use os-release", e);
--- a/system-backend/src/main/java/com/redhat/thermostat/backend/system/EtcOsRelease.java	Wed Jun 17 12:07:20 2015 -0400
+++ b/system-backend/src/main/java/com/redhat/thermostat/backend/system/EtcOsRelease.java	Thu Jun 18 18:13:42 2015 +0200
@@ -44,6 +44,7 @@
 
 public class EtcOsRelease implements DistributionInformationSource {
 
+    private static final String EMPTY_STRING = "";
     private static final String OS_RELEASE = "/etc/os-release";
     private final String osReleaseFile;
     
@@ -72,10 +73,15 @@
     }
 
     public DistributionInformation getFromOsRelease(BufferedReader reader) throws IOException {
-        String name = "Linux";
         String version = DistributionInformation.UNKNOWN_VERSION;
+        String name = DistributionInformation.UNKNOWN_NAME;
         String line = null;
         while ((line = reader.readLine()) != null) {
+            // skip whitespace only lines
+            line = line.trim();
+            if (line.equals(EMPTY_STRING)) {
+                continue;
+            }
             if (line.matches("^NAME *=.*")) {
                 name = readShellVariable(line);
             }
--- a/system-backend/src/test/java/com/redhat/thermostat/backend/system/DistributionInformationTest.java	Wed Jun 17 12:07:20 2015 -0400
+++ b/system-backend/src/test/java/com/redhat/thermostat/backend/system/DistributionInformationTest.java	Thu Jun 18 18:13:42 2015 +0200
@@ -37,7 +37,10 @@
 package com.redhat.thermostat.backend.system;
 
 import static org.junit.Assert.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
+import java.io.IOException;
 import java.util.logging.Handler;
 import java.util.logging.Logger;
 
@@ -115,6 +118,23 @@
         assertEquals(DistributionInformation.UNKNOWN_NAME, info.getName());
         assertEquals(DistributionInformation.UNKNOWN_VERSION, info.getVersion());
     }
+    
+    @Test
+    public void verifyFallbackToLsbWhenEtcOsReturnsUnknown() throws IOException {
+        EtcOsRelease mockEtcOsRelease = mock(EtcOsRelease.class);
+        DistributionInformation mockDistro = mock(DistributionInformation.class);
+        when(mockEtcOsRelease.getDistributionInformation()).thenReturn(mockDistro);
+        when(mockDistro.getName()).thenReturn(DistributionInformation.UNKNOWN_NAME);
+        when(mockDistro.getVersion()).thenReturn(DistributionInformation.UNKNOWN_VERSION);
+        
+        LsbRelease mockLsbRelease = mock(LsbRelease.class);
+        DistributionInformation mockLsbDistro = mock(DistributionInformation.class);
+        when(mockLsbRelease.getDistributionInformation()).thenReturn(mockLsbDistro);
+        
+        DistributionInformation info = DistributionInformation.get(mockEtcOsRelease, mockLsbRelease);
+        assertSame("Expected lsb info to be used since etc returns unknown",
+                   mockLsbDistro, info);
+    }
 
     private void assertTestHandlerRegistered() {
         assertNotNull(logger);
--- a/system-backend/src/test/java/com/redhat/thermostat/backend/system/EtcOsReleaseTest.java	Wed Jun 17 12:07:20 2015 -0400
+++ b/system-backend/src/test/java/com/redhat/thermostat/backend/system/EtcOsReleaseTest.java	Thu Jun 18 18:13:42 2015 +0200
@@ -87,6 +87,23 @@
         assertEquals("12.1 (Asparagus)", info.getVersion());
     }
     
+    /**
+     * DistributionInformation falls back on LSB when /etc/os-release contains
+     * inconclusive content (empty in this case). It should not return some
+     * info as "Linux".
+     * 
+     * @throws IOException
+     */
+    @Test
+    public void testEmpty() throws IOException {
+        String output = "";
+        BufferedReader reader = new BufferedReader(new StringReader(output));
+        
+        DistributionInformation info = new EtcOsRelease().getFromOsRelease(reader);
+        assertEquals(DistributionInformation.UNKNOWN_NAME, info.getName());
+        assertEquals(DistributionInformation.UNKNOWN_VERSION, info.getVersion());
+    }
+    
     @Test
     public void getDistributionInformationThrowsIOExceptionIfFileNotThere() {
         EtcOsRelease etcOsRelease = new EtcOsRelease(NOT_EXISTING_OS_RELEASE_FILE);