cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edi...@apache.org
Subject git commit: updated refs/heads/master to 882e5fa
Date Thu, 27 Jun 2013 18:10:52 GMT
Updated Branches:
  refs/heads/master 6ea38bff1 -> 882e5fa4e


FileUtil simplified

- writeToFile removed since no references to it
- readFileAsString replaced with FileUtils.readFileToString
- minor code duplication removed in dependent method getNicStats
- unit test added

Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/882e5fa4
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/882e5fa4
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/882e5fa4

Branch: refs/heads/master
Commit: 882e5fa4e0c54ff53172b132c6184edadbc38b42
Parents: 6ea38bf
Author: Laszlo Hornyak <laszlo.hornyak@gmail.com>
Authored: Wed Jun 19 22:54:51 2013 +0200
Committer: Edison Su <sudison@gmail.com>
Committed: Thu Jun 27 11:08:03 2013 -0700

----------------------------------------------------------------------
 .../kvm/resource/LibvirtComputingResource.java  | 28 ++++++-------
 .../resource/LibvirtComputingResourceTest.java  | 14 +++++++
 utils/src/com/cloud/utils/FileUtil.java         | 41 +-------------------
 3 files changed, 26 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/882e5fa4/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
index b1bc99d..e0c00fc 100755
--- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
+++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
@@ -64,6 +64,7 @@ import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
 import org.apache.cloudstack.utils.qemu.QemuImgException;
 import org.apache.cloudstack.utils.qemu.QemuImgFile;
 import org.apache.log4j.Logger;
+import org.apache.commons.io.FileUtils;
 import org.libvirt.Connect;
 import org.libvirt.Domain;
 import org.libvirt.DomainBlockStats;
@@ -229,7 +230,6 @@ import com.cloud.storage.template.Processor.FormatInfo;
 import com.cloud.storage.template.QCOW2Processor;
 import com.cloud.storage.template.TemplateLocation;
 import com.cloud.storage.template.TemplateProp;
-import com.cloud.utils.FileUtil;
 import com.cloud.utils.NumbersUtil;
 import com.cloud.utils.Pair;
 import com.cloud.utils.PropertiesUtil;
@@ -4847,24 +4847,18 @@ ServerResource {
         }
     }
 
-    private Pair<Double, Double> getNicStats(String nicName) {
-        double rx = 0.0;
-        String rxFile = "/sys/class/net/" + nicName + "/statistics/rx_bytes";
-        String rxContent = FileUtil.readFileAsString(rxFile);
-        if (rxContent == null) {
-            s_logger.warn("Failed to read the rx_bytes for " + nicName + " from " + rxFile);
-        }
-        rx = Double.parseDouble(rxContent);
+    static Pair<Double, Double> getNicStats(String nicName) {
+        return new Pair<Double, Double>(readDouble(nicName, "rx_bytes"), readDouble(nicName,
"tx_bytes"));
+    }
 
-        double tx = 0.0;
-        String txFile = "/sys/class/net/" + nicName + "/statistics/tx_bytes";
-        String txContent = FileUtil.readFileAsString(txFile);
-        if (txContent == null) {
-            s_logger.warn("Failed to read the tx_bytes for " + nicName + " from " + txFile);
+    static double readDouble(String nicName, String fileName) {
+        final String path = "/sys/class/net/" + nicName + "/statistics/" + fileName;
+        try {
+            return Double.parseDouble(FileUtils.readFileToString(new File(path)));
+        } catch (IOException ioe) {
+            s_logger.warn("Failed to read the " + fileName + " for " + nicName + " from "
+ path, ioe);
+            return 0.0;
         }
-        tx = Double.parseDouble(txContent);
-
-        return new Pair<Double, Double>(rx, tx);
     }
 
     private Answer execute(NetworkRulesSystemVmCommand cmd) {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/882e5fa4/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java
b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java
index c82c31f..56d6536 100644
--- a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java
+++ b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java
@@ -21,12 +21,17 @@ package com.cloud.hypervisor.kvm.resource;
 
 import com.cloud.agent.api.to.VirtualMachineTO;
 import com.cloud.template.VirtualMachineTemplate.BootloaderType;
+import com.cloud.utils.Pair;
 import com.cloud.vm.VirtualMachine;
+
+import org.apache.commons.lang.SystemUtils;
+import org.junit.Assume;
 import org.junit.Test;
 
 import java.util.Random;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 
 public class LibvirtComputingResourceTest {
 
@@ -183,4 +188,13 @@ public class LibvirtComputingResourceTest {
 
         assertEquals(vmStr, vm.toString());
     }
+
+    @Test
+    public void testGetNicStats() {
+        //this test is only working on linux because of the loopback interface name
+        //also the tested code seems to work only on linux
+        Assume.assumeTrue(SystemUtils.IS_OS_LINUX);
+        Pair<Double, Double> stats = LibvirtComputingResource.getNicStats("lo");
+        assertNotNull(stats);
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/882e5fa4/utils/src/com/cloud/utils/FileUtil.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/FileUtil.java b/utils/src/com/cloud/utils/FileUtil.java
index 74f4088..a6f0c91 100644
--- a/utils/src/com/cloud/utils/FileUtil.java
+++ b/utils/src/com/cloud/utils/FileUtil.java
@@ -16,54 +16,15 @@
 // under the License.
 package com.cloud.utils;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
-import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 
 public class FileUtil {
-	
-    public static String readFileAsString(String filePath) {
-        File file = new File(filePath);
-        if(!file.exists())
-            return null;
-        
-        try {
-            byte[] buffer = new byte[(int)file.length()];
-            BufferedInputStream f = null;
-            try {
-                f = new BufferedInputStream(new FileInputStream(filePath));
-                f.read(buffer);
-            } finally {
-                if (f != null) { 
-                    try { 
-                        f.close(); 
-                    } catch (IOException ignored) {
-                    }
-                }
-            }
-            return new String(buffer);
-        } catch(IOException e) {
-            return null;
-        }
-    }
-    
-    public static void writeToFile(String content, String filePath) throws IOException {
-        BufferedWriter out = null;
-        try {
-            out = new BufferedWriter(new FileWriter(filePath));
-            out.write(content);
-        } finally {
-            if(out != null)
-                out.close();
-        }
-    }
-    
+
     public static void copyfile(File f1, File f2) throws IOException {
         InputStream in = new FileInputStream(f1);
         OutputStream out = new FileOutputStream(f2);


Mime
View raw message