changeset 382:4b2b830097f3

PR1047: CLI list-vms should display status of VM (dead/alive) Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-June/001866.html
author Omair Majid <omajid@redhat.com>
date Mon, 18 Jun 2012 12:48:34 -0400
parents e78dfab194a5
children 1974926fb7a0
files tools/src/main/java/com/redhat/thermostat/tools/cli/ListVMsCommand.java tools/src/main/java/com/redhat/thermostat/tools/cli/VMListFormatter.java tools/src/test/java/com/redhat/thermostat/tools/cli/ListVMsCommandTest.java
diffstat 3 files changed, 43 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/tools/src/main/java/com/redhat/thermostat/tools/cli/ListVMsCommand.java	Mon Jun 18 11:23:40 2012 -0400
+++ b/tools/src/main/java/com/redhat/thermostat/tools/cli/ListVMsCommand.java	Mon Jun 18 12:48:34 2012 -0400
@@ -49,6 +49,7 @@
 import com.redhat.thermostat.common.dao.HostRef;
 import com.redhat.thermostat.common.dao.VmInfoDAO;
 import com.redhat.thermostat.common.dao.VmRef;
+import com.redhat.thermostat.common.model.VmInfo;
 
 public class ListVMsCommand implements Command {
 
@@ -70,7 +71,8 @@
         for (HostRef host : hosts) {
             Collection<VmRef> vms = vmsDAO.getVMs(host);
             for (VmRef vm : vms) {
-                formatter.addVM(vm);
+                VmInfo info = vmsDAO.getVmInfo(vm);
+                formatter.addVM(vm, info);
             }
         }
         formatter.format(ctx.getConsole().getOutput());
--- a/tools/src/main/java/com/redhat/thermostat/tools/cli/VMListFormatter.java	Mon Jun 18 11:23:40 2012 -0400
+++ b/tools/src/main/java/com/redhat/thermostat/tools/cli/VMListFormatter.java	Mon Jun 18 12:48:34 2012 -0400
@@ -39,6 +39,7 @@
 import java.io.PrintStream;
 
 import com.redhat.thermostat.common.dao.VmRef;
+import com.redhat.thermostat.common.model.VmInfo;
 
 class VMListFormatter {
 
@@ -51,15 +52,19 @@
 
     private static final String VM_NAME = "VM_NAME";
 
+    private static final String VM_STATUS = "STATUS";
+    private static final String STATUS_ALIVE = "RUNNING";
+    private static final String STATUS_DEAD = "EXITED";
+
     private TableRenderer tableRenderer;
 
     VMListFormatter() {
-        tableRenderer = new TableRenderer(4);
+        tableRenderer = new TableRenderer(5);
         printHeader();
     }
 
-    void addVM(VmRef vm) {
-        printVM(vm);
+    void addVM(VmRef vm, VmInfo info) {
+        printVM(vm, info);
     }
 
     void format(PrintStream output) {
@@ -67,15 +72,19 @@
     }
 
     private void printHeader() {
-        printLine(HOST_ID, HOST, VM_ID, VM_NAME);
+        printLine(HOST_ID, HOST, VM_ID, VM_STATUS, VM_NAME);
     }
 
-    private void printVM(VmRef vm) {
-        printLine(vm.getAgent().getAgentId(), vm.getAgent().getHostName(), vm.getId().toString(), vm.getName());
+    private void printVM(VmRef vm, VmInfo info) {
+        printLine(vm.getAgent().getAgentId(),
+                  vm.getAgent().getHostName(),
+                  vm.getId().toString(),
+                  info.isAlive() ? STATUS_ALIVE : STATUS_DEAD,
+                  vm.getName());
     }
 
-    private void printLine(String hostId, String host, String vmId, String vmName) {
-        tableRenderer.printLine(hostId, host, vmId, vmName);
+    private void printLine(String hostId, String host, String vmId, String status, String vmName) {
+        tableRenderer.printLine(hostId, host, vmId, status, vmName);
     }
 
 }
--- a/tools/src/test/java/com/redhat/thermostat/tools/cli/ListVMsCommandTest.java	Mon Jun 18 11:23:40 2012 -0400
+++ b/tools/src/test/java/com/redhat/thermostat/tools/cli/ListVMsCommandTest.java	Mon Jun 18 12:48:34 2012 -0400
@@ -39,6 +39,8 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.isA;
+import static org.mockito.Mockito.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -64,6 +66,7 @@
 import com.redhat.thermostat.common.dao.HostRef;
 import com.redhat.thermostat.common.dao.VmInfoDAO;
 import com.redhat.thermostat.common.dao.VmRef;
+import com.redhat.thermostat.common.model.VmInfo;
 import com.redhat.thermostat.test.TestCommandContextFactory;
 
 public class ListVMsCommandTest {
@@ -118,8 +121,11 @@
     public void verifyOutputFormatOneLine() throws CommandException {
 
         HostRef host1 = new HostRef("123", "h1");
+        VmRef vm1 = new VmRef(host1, 1, "n");
+        VmInfo vm1Info = new VmInfo(1, 0, 1, "", "", "", "", "", "", "", "", null, null, null);
         when(hostsDAO.getHosts()).thenReturn(Arrays.asList(host1));
-        when(vmsDAO.getVMs(host1)).thenReturn(Arrays.asList(new VmRef(host1, 1, "n")));
+        when(vmsDAO.getVMs(host1)).thenReturn(Arrays.asList(vm1));
+        when(vmsDAO.getVmInfo(eq(vm1))).thenReturn(vm1Info);
 
         SimpleArguments args = new SimpleArguments();
         args.addArgument("--dbUrl", "fluff");
@@ -128,8 +134,8 @@
         cmd.run(ctx);
 
         String output = cmdCtxFactory.getOutput();
-        assertEquals("HOST_ID HOST VM_ID VM_NAME\n" +
-                     "123     h1   1     n\n", output);
+        assertEquals("HOST_ID HOST VM_ID STATUS VM_NAME\n" +
+                     "123     h1   1     EXITED n\n", output);
     }
 
     @Test
@@ -139,8 +145,16 @@
         HostRef host2 = new HostRef("456", "longhostname");
         when(hostsDAO.getHosts()).thenReturn(Arrays.asList(host1, host2));
 
-        when(vmsDAO.getVMs(host1)).thenReturn(Arrays.asList(new VmRef(host1, 1, "n"), new VmRef(host1, 2, "n1")));
-        when(vmsDAO.getVMs(host2)).thenReturn(Arrays.asList(new VmRef(host2, 123456, "longvmname")));
+        VmRef vm1 = new VmRef(host1, 1, "n");
+        VmRef vm2 = new VmRef(host1, 2, "n1");
+        VmRef vm3 = new VmRef(host2, 123456, "longvmname");
+
+        VmInfo vmInfo = new VmInfo(1, 0, 1, "", "", "", "", "", "", "", "", null, null, null);
+
+        when(vmsDAO.getVMs(host1)).thenReturn(Arrays.asList(vm1, vm2));
+        when(vmsDAO.getVMs(host2)).thenReturn(Arrays.asList(vm3));
+
+        when(vmsDAO.getVmInfo(isA(VmRef.class))).thenReturn(vmInfo);
 
         SimpleArguments args = new SimpleArguments();
         args.addArgument("--dbUrl", "fluff");
@@ -149,10 +163,10 @@
         cmd.run(ctx);
 
         String output = cmdCtxFactory.getOutput();
-        assertEquals("HOST_ID HOST         VM_ID  VM_NAME\n" +
-                     "123     h1           1      n\n" +
-                     "123     h1           2      n1\n" +
-                     "456     longhostname 123456 longvmname\n", output);
+        assertEquals("HOST_ID HOST         VM_ID  STATUS VM_NAME\n" +
+                     "123     h1           1      EXITED n\n" +
+                     "123     h1           2      EXITED n1\n" +
+                     "456     longhostname 123456 EXITED longvmname\n", output);
     }
 
     @Test